首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ProgressBars和Espresso

ProgressBars和Espresso
EN

Stack Overflow用户
提问于 2015-10-23 03:30:07
回答 5查看 6.2K关注 0票数 23

当我在运行一些espresso- ProgressBar时显示的布局中有一个测试时,我会遇到:

代码语言:javascript
复制
Caused by: android.support.test.espresso.AppNotIdleException: Looped for 1670 iterations over 60 SECONDS. The following Idle Conditions failed .

解决这个问题的好方法是什么?发现了一些黑客的东西,但正在寻找一种好的方法

EN

回答 5

Stack Overflow用户

发布于 2016-06-17 00:24:16

如果在测试开始时ProgressBar不可见,则可以将Drawable替换为自定义ViewAction

代码语言:javascript
复制
// Replace the drawable with a static color
onView(isAssignableFrom(ProgressBar.class)).perform(replaceProgressBarDrawable());

// Click a button (that will make the ProgressBar visible)
onView(withText("Show ProgressBar").perform(click());

自定义ViewAction

代码语言:javascript
复制
public static ViewAction replaceProgressBarDrawable() {
    return actionWithAssertions(new ViewAction() {
        @Override
        public Matcher<View> getConstraints() {
            return isAssignableFrom(ProgressBar.class);
        }

        @Override
        public String getDescription() {
            return "replace the ProgressBar drawable";
        }

        @Override
        public void perform(final UiController uiController, final View view) {
            // Replace the indeterminate drawable with a static red ColorDrawable
            ProgressBar progressBar = (ProgressBar) view;
            progressBar.setIndeterminateDrawable(new ColorDrawable(0xffff0000));
            uiController.loopMainThreadUntilIdle();
        }
    });
}
票数 15
EN

Stack Overflow用户

发布于 2016-03-24 21:42:56

我也有同样的问题。我不能想出一个完全优雅的解决方案,但我也会发布我的方法。

我尝试做的是覆盖ProgressBar上的indeterminateDrawable。当有一个简单的可绘制时,没有动画发生,并且Espresso测试不会遇到空闲问题。

不幸的是,mainandroidTest被同等对待。我没有找到覆盖我的ProgressBar的样式的方法。

现在,它结合了https://gist.github.com/Mauin/62c24c8a53593c0a605e#file-progressbar-javaHow to detect whether android app is running UI test with Espresso的一些想法。

首先,我创建了定制ProgressBar类,一个用于调试,另一个用于发布。发布版本只调用超级构造函数,不做其他任何事情。调试版本重写方法setIndeterminateDrawable。有了这个,我可以设置一个简单的可绘制的,而不是动画的。

发布代码:

代码语言:javascript
复制
public class ProgressBar extends android.widget.ProgressBar {

  public ProgressBar(Context context) {
    super(context);
  }

  public ProgressBar(Context context, AttributeSet attrs) {
    super(context, attrs);
  }

  public ProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
  }

  @TargetApi(Build.VERSION_CODES.LOLLIPOP)
  public ProgressBar(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);
  }

}

调试代码:

代码语言:javascript
复制
public class ProgressBar extends android.widget.ProgressBar {

  public ProgressBar(Context context) {
    super(context);
  }

  public ProgressBar(Context context, AttributeSet attrs) {
    super(context, attrs);
  }

  public ProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
  }

  @TargetApi(Build.VERSION_CODES.LOLLIPOP)
  public ProgressBar(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);
  }

  @SuppressWarnings("deprecation")
  @Override
  public void setIndeterminateDrawable(Drawable d) {
    if (isRunningTest()) {
        d = getResources().getDrawable(R.drawable.ic_replay);
    }
    super.setIndeterminateDrawable(d);
  }

  private boolean isRunningTest() {
    try {
        Class.forName("base.EspressoTestBase");
        return true;
    } catch (ClassNotFoundException e) {
        /* no-op */
    }
    return false;
  }

}

如你所见,我还添加了一个检查我的应用程序是否正在运行Espresso测试,而我正在搜索的类是我的Espresso测试的基础。

糟糕的是,您必须更新所有代码才能使用您的自定义ProgressBar。但好消息是,您的发布代码不会对此解决方案产生重大影响。

票数 9
EN

Stack Overflow用户

发布于 2016-11-01 20:35:09

我也有类似的问题。早在第一次调用getActivity()时,测试就失败了。因此,在活动开始后,必须替换ProgressBar的不确定的可绘制部分。

代码语言:javascript
复制
 Application application = (Application)this.getInstrumentation().getTargetContext().getApplicationContext();
    application.registerActivityLifecycleCallbacks(new Application.ActivityLifecycleCallbacks() {
        @Override
        public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
            //not here, it's too early
        }

        @Override
        public void onActivityStarted(Activity activity) {
            //find the progressBar in your activity
            ProgressBar progressBar = ((ProgressBar) activity.findViewById(R.id.progress_bar));
            if(progressBar != null) {
                //replace progress bar drawable as not animated
                progressBar.setIndeterminateDrawable(new ColorDrawable(0xffff0000));
            }
        }

        @Override
        public void onActivityResumed(Activity activity) {

        }

        @Override
        public void onActivityPaused(Activity activity) {

        }

        @Override
        public void onActivityStopped(Activity activity) {

        }

        @Override
        public void onActivitySaveInstanceState(Activity activity, Bundle outState) {

        }

        @Override
        public void onActivityDestroyed(Activity activity) {

        }
    });

    //Now you can start the activity
    getActivity();
票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33289152

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档