Espresso idlingResource - 如何注册?

时间:2017-10-17 11:39:15

标签: android android-espresso

在我的一个测试的@Before函数中,我正在尝试注册espresso idlingResource,但我不断收到需要在主线程上运行的错误。这是我到目前为止所做的:

 @Before
    public void setUp() throws Exception {
        mIntentsTestRule.getActivity().sharedPrefRepo.clearAll();

        new Thread(){
            @Override
            public void run() {
                super.run();
                IdlingResource loadingDialogIdlingResource = new LoadingDialogIdlingResource();
                IdlingRegistry.getInstance().register(loadingDialogIdlingResource);
            }
        }.start();

}

但这种做法毫无意义。我在另一个线程上注册这些东西但是我会在它完成之前继续运行我的测试吗?真正注册这些东西的方法是什么?我已经在拆解中创建了一个取消注册函数,如下所示:

@After
    public void tearDown() throws Exception {
        //handle any clean up
        mIntentsTestRule.getActivity().sharedPrefRepo.clearAll();
        freeIdlingResources();
    mIntentsTestRule.getActivity().finish();
    }

如果我不使用线程,则错误如下:

java.lang.RuntimeException: java.util.concurrent.ExecutionException: java.lang.RuntimeException: This method can not be called from the main application thread
at android.support.test.espresso.base.IdlingResourceRegistry.runSynchronouslyOnMainThread(IdlingResourceRegistry.java:314)
at android.support.test.espresso.base.IdlingResourceRegistry.sync(IdlingResourceRegistry.java:90)
at android.support.test.espresso.Espresso.onView(Espresso.java:69)

请注意:IdlingRegistry通过getInstance()。register()弃用了以前的Espresso.registerIdlingResources()方法。

更新:这是我的idlresource代码:

public class LoadingDialogIdlingResource implements IdlingResource {
      private ResourceCallback resourceCallback;
      private boolean isIdle;

      @Override
      public String getName() {
          return LoadingDialogIdlingResource.class.getName();
      }

      @Override
      public boolean isIdleNow() {
          if (isIdle) return true;

          Activity activity = getCurrentActivity();
          if (activity == null) return false;

          Fragment f = activity.getFragmentManager().findFragmentByTag(ProgressDialogFragment.class.getName());
          isIdle = f == null;
          if (isIdle) {
              resourceCallback.onTransitionToIdle();
          }
          return isIdle;
      }

      public Activity getCurrentActivity() {
          final Activity[] currentActivity = {null};

          InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable(){
              public void run(){
                  Collection<Activity> resumedActivity = ActivityLifecycleMonitorRegistry.getInstance().getActivitiesInStage(Stage.RESUMED);
                  Iterator<Activity> it = resumedActivity.iterator();
                  currentActivity[0] = it.next();
              }
          });
          return currentActivity[0];
      }

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

1 个答案:

答案 0 :(得分:0)

问题在于这段代码:

public Activity getCurrentActivity() {
    final Activity[] currentActivity = {null};

    InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable(){
        public void run(){
            Collection<Activity> resumedActivity = ActivityLifecycleMonitorRegistry.getInstance().getActivitiesInStage(Stage.RESUMED);
            Iterator<Activity> it = resumedActivity.iterator();
            currentActivity[0] = it.next();
        }
    });
    return currentActivity[0];
}

此处禁止拨打runOnMainSync。我只是将片段管理器传递给构造函数来解决这个问题。