トラッキング コード

Showing posts with label Espresso. Show all posts
Showing posts with label Espresso. Show all posts

4/29/2015

How to IntentsTestRule, in AndroidTest




About IntentsTestRule

IntentsTestRule is AndroidTest which can do intent test.

If use, we have to add Espresso-Intents library in build.gradle.
1
androidTestCompile 'com.android.support.test.espresso:espresso-intents:2.1'

Test Sample

The following code is sample which Intent has Intent.ACTION_CALL by user operation.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
@RunWith(AndroidJUnit4.class)
@LargeTest
public class MainActivityIntentTest {
 
    @Rule
    public IntentsTestRule<MainActivity> mActivityRule = new IntentsTestRule<>(
            MainActivity.class);
 
    @Before
    public void stubAllExternalIntents() {
        // By default Espresso Intents does not stub any Intents. Stubbing needs to be setup before
        // every test run. In this case all external Intents will be blocked.
        intending(not(isInternal()))
                .respondWith(new Instrumentation.ActivityResult(Activity.RESULT_OK, null));
    }
 
    @Test
    public void callPhone() {
        // call action
        onView(withId(R.id.callButton)).perform(click());
 
        // test
        intended(allOf(
                hasAction(Intent.ACTION_CALL),
                hasData("tel:0123456789"),
                toPackage("com.android.server.telecom")));
 
    }
}
How to create test class.
  • create test class with RunWith annotation
  • add IntentsTestRule with Rule annotation

We can check whether Intent has occurred.
  • Intended method which check the intended Intent
  • matches method


  • Add Intent to Starting Activity

    If need to add Intent to Activity, we need to do override IntentsTestRule#getActivityIntent.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    @Rule
    public IntentsTestRule<MainActivity> mActivityRule = new IntentsTestRule<>(MainActivity.class) {
     
        /**
        * add Intent to Activity
        */
        @Override
        protected Intent getActivityIntent() {
            Intent intent = new Intent();
            intent.putExtra(KEY_DATA,data);
            return intent;
        }
    };

    https://github.com/googlesamples/android-testing

    3/01/2015

    How to check Toast window, on android test-kit Espresso

    If we read the following public document, we can check other window.
    Using inRoot to target non-default windows

    1
    2
    3
    onView(withText("South China Sea"))
      .inRoot(withDecorView(not(is(getActivity().getWindow().getDecorView()))))
      .perform(click());

    But, I think this is not cool.

    We can create a custom matcher.
    Toast window has the WindowManager.LayoutParams.TYPE_TOAST.

    The followinf code is sample.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    /**
     * Matcher that is Toast window.
     */
    public static Matcher<Root> isToast() {
        return new TypeSafeMatcher<Root>() {
     
            @Override
            public void describeTo(Description description) {
                description.appendText("is toast");
            }
     
            @Override
            public boolean matchesSafely(Root root) {
                int type = root.getWindowLayoutParams().get().type;
                if ((type == WindowManager.LayoutParams.TYPE_TOAST)) {
                    IBinder windowToken = root.getDecorView().getWindowToken();
                    IBinder appToken = root.getDecorView().getApplicationWindowToken();
                    if (windowToken == appToken) {
                        // windowToken == appToken means this window isn't contained by any other windows.
                        // if it was a window for an activity, it would have TYPE_BASE_APPLICATION.
                        return true;
                    }
                }
                return false;
            }
        };
    }