Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Espresso does no record any intent if there are no buttons

I'm trying to write a test to verify intent launching with espresso, the problem is that intended() does not record any intent.

I have this test

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

}

and in my activity i have this code

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

The test result is this.

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:[]

I have tried to launch the intent inside onClickListen and it worked, but without it i can't get it to work. I also tried with idling resources with no luck. Do you know how to achieve this?

like image 280
Borja Avatar asked Apr 11 '16 09:04

Borja


Video Answer


1 Answers

The solution is to register an idling resource to wait the second activity.

In my case the test will remain as follows:

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

And here the idling resource.

 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;
    }
}
like image 192
Borja Avatar answered Sep 19 '22 15:09

Borja