IdlingResource的Android测试

IdlingResource的Android测试

本文介绍了使用Espresso IdlingResource的Android测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试测试AutoCompleteTextView在键入某些单词后将显示项目.但是在键入和显示弹出窗口之间有一个延迟.首先,我正在使用Thread.sleep(),并且工作正常.但是我知道这种方法尚不清楚,因此我正在尝试使用IdlingResource完成它.但这对我不起作用.我从字面上看了Google响应的前5页,但是我不明白它应该如何工作,或者我的代码中有一些错误.

I'm trying to test that the AutoCompleteTextView will show the items after some word will be typed. But there is a delay between typing and showing the popup. First i was using Thread.sleep() and it was working just fine. But I know that this approach isn't clear so I'm trying to accomplish it with IdlingResource. But it doesn't work for me. I literally read first 5 pages of Google responses, but either I don't understand how it should work, or I have some error in my code.

这是代码:

static class AutocompleteShowIdlingResource implements IdlingResource {

    private Activity activity;
    private @IdRes int resId;
    private ResourceCallback resourceCallback;

    public AutocompleteShowIdlingResource(Activity activity, @IdRes int resId) {
        this.activity = activity;
        this.resId = resId;
    }

    @Override
    public String getName() {
        return this.getClass().getName() + resId;
    }

    @Override
    public boolean isIdleNow() {
        boolean idle = ((AutoCompleteTextView) activity.findViewById(resId)).getAdapter() != null;
        Log.d(TAG, "isIdleNow: " + idle);
        if (idle) {
            resourceCallback.onTransitionToIdle();
        }
        return idle;
    }

    @Override
    public void registerIdleTransitionCallback(ResourceCallback callback) {
        this.resourceCallback = callback;

    }
}

测试本身:

    Activity activity = calibrationActivityRule.getActivity();
    onView(withId(R.id.autocomplete_occupation)).perform(typeText("dok"));
    IdlingResource idlingResource = new AutocompleteShowIdlingResource(activity, R.id.autocomplete_occupation);
    Espresso.registerIdlingResources(idlingResource);
    assertEquals(((AutoCompleteTextView) activity.findViewById(R.id.autocomplete_occupation)).getAdapter().getCount(), 3);
    Espresso.unregisterIdlingResources(idlingResource);

但是,当尝试在空适配器上调用getCount()时,在java.lang.NullPointerException上的测试失败.日志正在打印

But the test fails on java.lang.NullPointerException when trying to call getCount() on null adapter. The log is printing

isIdleNow: false

只有一次,这很奇怪.

没有太多关于如何使用IdlingResource的示例,因此也许有人可以对我说清楚.谢谢.

There isn't much clear examples how to use IdlingResource, so maybe someone can make it clear for me. Thanks.

推荐答案

只有与onView(...).check(...)或onData(...)一起使用时,您的IdlingResource才会起作用. .查看(...).实际上,魔术"将在检查调用中发生-这是Espresso等待直到没有运行的AsyncTasks或没有阻塞的IdlingResources的地方.

Your IdlingResource will have an effect only if you use it together with onView(...).check(...) or onData(...).check(...). Actually, the "magic" will happen in the check call - it's the place where Espresso waits until there are no running AsyncTasks or no blocking IdlingResources.

现在,让我们更正您的代码,使其生效:

Now let's correct your code so that it works:

Activity activity = calibrationActivityRule.getActivity();
onView(withId(R.id.autocomplete_occupation)).perform(typeText("dok"));
IdlingResource idlingResource = new AutocompleteShowIdlingResource(activity, R.id.autocomplete_occupation);

try {
    Espresso.registerIdlingResources(idlingResource);

    //that's where Espresso will wait until the idling resource is idle
    onData(anything()).inAdapter(withId(R.id.autocomplete_occupation)).check(matches(isDisplayed());
finally {
    Espresso.unregisterIdlingResources(idlingResource);
}
assertEquals(((AutoCompleteTextView) activity.findViewById(R.id.autocomplete_occupation)).getAdapter().getCount(), 3);

这篇关于使用Espresso IdlingResource的Android测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 20:21