我有RecyclerView
无尽的滚动。因此,当用户在列表中达到last - 2
位置时,我会呼叫服务器获取更多数据,并且在通话过程中,我再添加一项 - 进度一。
现在,我正在尝试使用Espresso编写体面的UI测试,该测试将检查无限滚动当前是否正在运行:
@Test
public void checkIfProgressShown() {
InstaFeed feed = TestDataFactory.makeInstaFeed(20);
InstaFeed oldFeed = TestDataFactory.makeInstaFeed(20);
when(mockDataManager.getFeedItemsFromServer()).thenReturn(Observable.just(feed.getInstaItems()));
when(mockDataManager.getOldFeedItemsFromServer()).thenReturn(Observable.just(oldFeed.getInstaItems())
.delay(2, TimeUnit.SECONDS));
instaActivityActivityTestRule.launchActivity(null);
int position = 0;
for (InstaItem item : feed.getInstaItems()) {
onView(withId(R.id.recycler_view))
.perform(RecyclerViewActions.scrollToPosition(position));
onView(withText(item.getLocation().getName()))
.check(matches(isDisplayed())); // Line of crash
position++;
}
onView(withId(R.id.progress))
.check(matches(isDisplayed()));
}
所以基本上说,我试图延迟Observable的响应,显示新的一批项目,以便Espresso可以向下滚动到最后一项并让我的progress
项目可见。问题在于测试只是堆栈,从UI的角度来看,它看起来像:
不要对这里没有进度条感到困惑 - 在设备上禁用动画,所以没关系。最后,小项目是具有进度的项目
60秒后它崩溃了 -
android.support.test.espresso.AppNotIdleException: Looped for 3019 iterations over 60 SECONDS. The following Idle Conditions failed .
at dalvik.system.VMStack.getThreadStackTrace(Native Method)
at java.lang.Thread.getStackTrace(Thread.java:580)
at android.support.test.espresso.base.DefaultFailureHandler.getUserFriendlyError(DefaultFailureHandler.java:82)
at android.support.test.espresso.base.DefaultFailureHandler.handle(DefaultFailureHandler.java:53)
at android.support.test.espresso.ViewInteraction.runSynchronouslyOnUiThread(ViewInteraction.java:184)
at android.support.test.espresso.ViewInteraction.check(ViewInteraction.java:158)
at org.kidinov.mvp_test.InstaActivityTest.checkIfProgressShown(InstaActivityTest.java:69)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at android.support.test.internal.statement.UiThreadStatement.evaluate(UiThreadStatement.java:55)
at android.support.test.rule.ActivityTestRule$ActivityStatement.evaluate(ActivityTestRule.java:257)
at org.kidinov.mvp_test.test.common.TestComponentRule$1.evaluate(TestComponentRule.java:49)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:54)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:240)
at org.kidinov.mvp_test.runner.UnlockDeviceAndroidJUnitRunner.onStart(UnlockDeviceAndroidJUnitRunner.java:36)
at org.kidinov.mvp_test.runner.RxAndroidJUnitRunner.onStart(RxAndroidJUnitRunner.java:16)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1853)
我使用RxAndroidJUnitRunner作为亚军。
可能我做错了,没有必要模拟延迟?
UPD:如果您想要应对这种情况,请按顺序prepared project。 that class内的问题。
答案 0 :(得分:0)
我花了一些时间来修复你的测试。希望它会对你有所帮助。
1.您以错误的方式生成测试数据。在makeInstaItem
中,您传递i
索引(以及0
值),因此列表中的最后一项与0L
创建的进度项具有相同的创建时间(InstaAdapter
) {1}}:
private static InstaItem makeInstaItem(String prefix, int i, int dateСoefficient) {
InstaItem item = new InstaItem();
item.setLocation(new Location(prefix + "_location_" + i));
item.setCreatedTime(i * 1000000L * dateСoefficient);
item.setImages(makeInstaImages());
item.setId(randomUuid());
return item;
}
这应该改为例如:
private static InstaItem makeInstaItem(String prefix, int i, int dateСoefficient) {
InstaItem item = new InstaItem();
item.setLocation(new Location(prefix + "_location_" + i));
item.setCreatedTime((i + 1) * 1000000L * dateСoefficient);
item.setImages(makeInstaImages());
item.setId(randomUuid());
return item;
}
创建的测试列表作为对适配器的引用传递。添加进度项后,它是最后一项之前的项。在测试中,您尝试匹配此进度条项目,就像它是常规位置项一样,因此它会崩溃。完成所有更改后,将其添加为最后一项,并在测试循环之前将其删除。
2。
在InstaPresenter
中,您在调用loadMore
订阅时不会添加项目:
subscribe(x -> {
Timber.d("load more - items fetched");
}
更改为例如:
subscribe(items -> {
if (!items.isEmpty()) {
getMvpView().showFeed(items);
}
Timber.d("load more - items fetched");
}
3.你根本不需要拖延。保持实现简单,例如:
when(mockDataManager.getOldFeedItemsFromServer()).thenReturn(Observable.just(oldFeed.getInstaItems())
.observeOn(AndroidSchedulers.mainThread())
.doOnNext(x -> {
Timber.d("On next after delay");
when(mockDataManager.subscribeOnFeedItemsChanges())
.thenReturn(Observable.just(oldFeed.getInstaItems()));
}));
instaActivityActivityTestRule.launchActivity(null);
int position = 0;
for (InstaItem item : feed.getInstaItems()) {
onView(withId(R.id.recycler_view))
.perform(RecyclerViewActions.scrollToPosition(position));
onView(withText(item.getLocation().getName()))
.check(matches(isDisplayed()));
onView(withText(InstaAdapter.dateFormat.format(new Date(item.getCreatedTime() * 1000))))
.check(matches(isDisplayed()));
position++;
}
for (InstaItem item : oldFeed.getInstaItems()) {
onView(withId(R.id.recycler_view))
.perform(RecyclerViewActions.scrollToPosition(position));
onView(withText(item.getLocation().getName()))
.check(matches(isDisplayed()));
onView(withText(InstaAdapter.dateFormat.format(new Date(item.getCreatedTime() * 1000))))
.check(matches(isDisplayed()));
position++;
}
我录制了video以显示上述更改后的工作原理
答案 1 :(得分:0)
引用的项目不再使用最新版本的Android Studio和JDK 11构建。
将Espresso与RxJava结合使用时,可以使用https://github.com/square/RxIdler注册所需的IdlingResources并包装所有自定义Rx调度程序。