samedi 2 janvier 2016

espresso -intent: always return false although real code runs successfully

I have a sample application that when pressing button, will open photo application for getting image. Here is my code:

  private View.OnClickListener mPhotoSelectClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent getPhotoIntent = new Intent(Intent.ACTION_GET_CONTENT);
            getPhotoIntent.setType("image/*");
            if (getPhotoIntent.resolveActivity(getPackageManager()) != null) {
                startActivityForResult(getPhotoIntent, IntentCode.PICK_PHOTO_INTENT);
            } else {
                DialogUtils.displayDialog(ImageListActivity.this,
                        "No photo viewer in your system");
            }
        }
    };

After that. I use expresso-intent for testing this intent:

@RunWith(AndroidJUnit4.class)
@SmallTest
public class IntentTest {

    @Rule
    public IntentsTestRule<ImageListActivity> mActivityRule = new IntentsTestRule<>(
            ImageListActivity.class);

    @Before
    public void stubAllExternalIntents() {
        // stub all external intent
        intending(not(isInternal())).respondWith(new Instrumentation.ActivityResult(Activity.RESULT_OK, null));
    }

    @Test
    public void clickPhotoButton() {
        onView(withId(R.id.fab_menu)).perform(click());
        onView(withId(R.id.fab_photos))
                .perform(click());

        // verify that send suitable intent to system.
        intended(allOf(
                hasAction(Intent.ACTION_GET_CONTENT)
                //hasType("image/*")
        ));
    }
}

I see animation is true. But test result always returns false.

Recorded intents:[]
at dalvik.system.VMStack.getThreadStackTrace(Native Method)
at java.lang.Thread.getStackTrace(Thread.java:579)
at android.support.test.espresso.base.DefaultFailureHandler.getUserFriendlyError(DefaultFailureHandler.java:82)
at android.support.test.espresso.base.DefaultFailureHandler.handle(DefaultFailureHandler.java:53)
at android.support.test.espresso.ViewInteraction.runSynchronouslyOnUiThread(ViewInteraction.java:184)
at android.support.test.espresso.ViewInteraction.check(ViewInteraction.java:158)
at android.support.test.espresso.intent.Intents.intended(Intents.java:186)
at android.support.test.espresso.intent.Intents.intended(Intents.java:169)
at com.silicons.android.uploader.instrument.screen.imagelist.IntentTest.clickPhotoButton(IntentTest.java:70)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at android.support.test.internal.statement.UiThreadStatement.evaluate(UiThreadStatement.java:55)
at android.support.test.rule.ActivityTestRule$ActivityStatement.evaluate(ActivityTestRule.java:257)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:54)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:240)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1729)
Caused by: junit.framework.AssertionFailedError: Wanted to match 1 intents. Actually matched 0 intents.

IntentMatcher: (has action: is "android.intent.action.GET_CONTENT")

Please help me figure out why.

Thanks :)

Aucun commentaire:

Enregistrer un commentaire