トラッキング コード

Showing posts with label Android JUnit Test. Show all posts
Showing posts with label Android JUnit Test. 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.
    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.
@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.
        @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

    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.
        /**
         * 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;
                }
            };
        }
    

    1/21/2015

    IllegalStateException at ActivityUnitTestCase with ActionBarActivity

    I need to do ActivityUnitTestCase which activity is extends ActionBarActivity.
    However, I get the following exception.

    java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
    at android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:151)
    at android.support.v7.app.ActionBarActivityDelegateBase.onCreate(ActionBarActivityDelegateBase.java:138)
    at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:123)
    


    If call startActivity, Context does not have Theme.AppCompat...
    So, We need to call setActivityContext() due to set Context with theme.
        @Override
        protected void setUp() throws Exception {
            super.setUp();
            Context context = getInstrumentation().getTargetContext();
            ContextThemeWrapper contextTheme = new ContextThemeWrapper(context, R.style.AppTheme);
            setActivityContext(contextTheme);
            startActivity(new Intent(), null, null);
        }
    

    7/18/2012

    Starting for Android JUnit Test

    If you would like to publish application to Google Market, I recommend that you test.
    Android has test framework which helping you to test .

    Check develoer's site!!

    http://developer.android.com/tools/testing/index.html
    The Android framework includes an integrated testing framework that helps you test all aspects of your application and the SDK tools include tools for setting up and running test applications. Whether you are working in Eclipse with ADT or working from the command line, the SDK tools help you set up and run your tests within an emulator or the device you are targeting.
    If you aren't yet familiar with the Android testing framework, start by reading Testing Fundamentals. For a step-by-step introduction to Android testing, try the Activity Testing Tutorial.

    How to set up Project for eclipse:

    http://developer.android.com/tools/testing/testing_android.html