gpt4 book ai didi

android - 如果没有按钮,Espresso 不会记录任何 Intent

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:40:48 26 4
gpt4 key购买 nike

我正在尝试编写一个测试来验证使用 Espresso 启动的 Intent ,问题是 intended() 没有记录任何 Intent 。

我有这个测试

  @Test
public void shoulddosomething(){
startActivity();
intended(hasComponent(hasClassName(TemplatePictureCaptureActivity.class.getName())));

}

在我的 Activity 中我有这段代码

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(onRequestLayout());
Intent intent = new Intent(this, TemplatePictureCaptureActivity.class);
startActivity(intent);
}

测试结果是这样的

android.support.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError: Wanted to match 1 intents. Actually matched 0 intents.

IntentMatcher: has component: has component with: class name: is "cat.helm.recertel.ui.templatepicturecapture.TemplatePictureCaptureActivity" package name: an instance of java.lang.String short class name: an instance of java.lang.String

Matched intents:[]

Recorded intents:[]

我已经尝试在 onClickListen 中启动 Intent 并且它起作用了,但是没有它我无法让它起作用。我也尝试过使用闲置资源但没有运气。你知道如何实现吗?

最佳答案

解决办法是注册一个空闲资源等待第二个activity。

在我的例子中,测试将保持如下:

  @Test
public void shoulddosomething() {
startActivity();
String templatePictureActivityClassName = TemplatePictureCaptureActivity.class.getName();
Espresso.registerIdlingResources(new WaitActivityIsResumedIdlingResource(templatePictureActivityClassName));
intended(hasComponent(hasClassName(templatePictureActivityClassName)));
}

这里是空闲资源。

 private static class WaitActivityIsResumedIdlingResource implements IdlingResource {
private final ActivityLifecycleMonitor instance;
private final String activityToWaitClassName;
private volatile ResourceCallback resourceCallback;
boolean resumed = false;
public WaitActivityIsResumedIdlingResource(String activityToWaitClassName) {
instance = ActivityLifecycleMonitorRegistry.getInstance();
this.activityToWaitClassName = activityToWaitClassName;
}

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

@Override
public boolean isIdleNow() {
resumed = isActivityLaunched();
if(resumed && resourceCallback != null) {
resourceCallback.onTransitionToIdle();
}

return resumed;
}

private boolean isActivityLaunched() {
Collection<Activity> activitiesInStage = instance.getActivitiesInStage(Stage.RESUMED);
for (Activity activity : activitiesInStage) {
if(activity.getClass().getName().equals(activityToWaitClassName)){
return true;
}
}
return false;
}

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

关于android - 如果没有按钮,Espresso 不会记录任何 Intent ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36544360/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com