Share
Embed
Annual Pro sale · Save $30
Have presentations of your own?
Share with private links, password protection, and more control over your work.
Transcript
48 slides
-
Android Instrumentation Instrumentation AndroidJunitRunner defaultConfig { applicationId "com.example.devfest" minSdkVersion 10
targetSdkVersion 23 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } androidTestCompile 'com.android.support.test:runner:0.4' -
public class MainActivityTest { @Rule public ActivityTestRule<MainActivity> activityTestRule =
new ActivityTestRule<>(MainActivity.class); @Test public void findViewPerformActionDone() { } } @Rule public final ServiceTestRule serviceRule = new ServiceTestRule(); Rules -
Espresso public static Matcher<View> withId(final int id) { return new
TypeSafeMatcher<View>() { Resources resources = null; @Override public void describeTo(Description description) { String idDescription = Integer.toString(id); if (resources != null) { try { idDescription = resources.getResourceName(id); } catch (Resources.NotFoundException e) { // No big deal, will just use the int value. idDescription = String.format("%s (resource name not found)", id); } } description.appendText("with id: " + idDescription); } @Override public boolean matchesSafely(View view) { resources = view.getResources(); return id == view.getId(); } }; } -
Espresso public static Matcher<View> withId(final int id) { return new
TypeSafeMatcher<View>() { Resources resources = null; @Override public void describeTo(Description description) { String idDescription = Integer.toString(id); if (resources != null) { try { idDescription = resources.getResourceName(id); } catch (Resources.NotFoundException e) { // No big deal, will just use the int value. idDescription = String.format("%s (resource name not found)", id); } } description.appendText("with id: " + idDescription); } @Override public boolean matchesSafely(View view) { resources = view.getResources(); return id == view.getId(); } }; } -
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1' androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.2.1' androidTestCompile 'com.android.support.test.espresso:espresso-idling-resource:2.2.1' androidTestCompile 'com.android.support.test.espresso:espresso-intents:2.2.1' androidTestCompile 'com.android.support.test.espresso:espresso-web:2.2.1'
↳ synchronization with background jobs ↳ DatePicker, RecyclerView, Drawer actions,CountingIdlingResource ↳ intent rules ↳ WebView support Espresso -
Intended @Test public void typeNumber_ValidInput_InitiatesCall() { onView(withId(R.id.edit_text_caller_number)) .perform(typeText(VALID_PHONE_NUMBER), closeSoftKeyboard()); onView(withId(R.id.button_call_number)).perform(click());
intended(allOf( hasAction(Intent.ACTION_CALL), hasData(INTENT_DATA_PHONE_NUMBER), toPackage(PACKAGE_ANDROID_DIALER))); } Espresso Intents -
Intended @Test public void typeNumber_ValidInput_InitiatesCall() { onView(withId(R.id.edit_text_caller_number)) .perform(typeText(VALID_PHONE_NUMBER), closeSoftKeyboard()); onView(withId(R.id.button_call_number)).perform(click());
intended(allOf( hasAction(Intent.ACTION_CALL), hasData(INTENT_DATA_PHONE_NUMBER), toPackage(PACKAGE_ANDROID_DIALER))); } Espresso Intents -
Intended @Test public void typeNumber_ValidInput_InitiatesCall() { onView(withId(R.id.edit_text_caller_number)) .perform(typeText(VALID_PHONE_NUMBER), closeSoftKeyboard()); onView(withId(R.id.button_call_number)).perform(click());
intended(allOf( hasAction(Intent.ACTION_CALL), hasData(INTENT_DATA_PHONE_NUMBER), toPackage(PACKAGE_ANDROID_DIALER))); } Espresso Intents -
@Test public void pickContactButton_click_SelectsPhoneNumber() { intending(hasComponent(hasShortClassName(".ContactsActivity"))) .respondWith(new ActivityResult(Activity.RESULT_OK, ContactsActivity.createResultData(VALID_PHONE_NUMBER)
onView(withId(R.id.button_pick_contact)).perform(click()); onView(withId(R.id.edit_text_caller_number)) .check(matches(withText(VALID_PHONE_NUMBER))); } Espresso Intents Intending -
@Test public void pickContactButton_click_SelectsPhoneNumber() { intending(hasComponent(hasShortClassName(".ContactsActivity"))) .respondWith(new ActivityResult(Activity.RESULT_OK, ContactsActivity.createResultData(VALID_PHONE_NUMBER)
onView(withId(R.id.button_pick_contact)).perform(click()); onView(withId(R.id.edit_text_caller_number)) .check(matches(withText(VALID_PHONE_NUMBER))); } Espresso Intents Intending -
@Test public void pickContactButton_click_SelectsPhoneNumber() { intending(hasComponent(hasShortClassName(".ContactsActivity"))) .respondWith(new ActivityResult(Activity.RESULT_OK, ContactsActivity.createResultData(VALID_PHONE_NUMBER)
onView(withId(R.id.button_pick_contact)).perform(click()); onView(withId(R.id.edit_text_caller_number)) .check(matches(withText(VALID_PHONE_NUMBER))); } Espresso Intents Intending -
Idling Resources public interface IdlingResource { public String getName();
public boolean isIdleNow(); public void registerIdleTransitionCallback(ResourceCallback callback); public interface ResourceCallback { public void onTransitionToIdle(); } } -
Idling Resources public interface IdlingResource { public String getName();
public boolean isIdleNow(); public void registerIdleTransitionCallback(ResourceCallback callback); public interface ResourceCallback { public void onTransitionToIdle(); } } @Override public boolean isIdleNow() { return videoView == null || videoView.isPlaying(); } -
CountingIdlingResource public class DecoratedFooServer implements FooServer { private final FooServer
realFooServer; private final CountingIdlingResource fooServerIdlingResource; public DecoratedFooServer(FooServer realFooServer, CountingIdlingResource fooServerIdlingResource) { this.realFooServer = checkNotNull(realFooServer); this.fooServerIdlingResource = checkNotNull(fooServerIdlingResource); } public Foo newFoo() { fooServerIdlingResource.increment(); try { return realFooServer.newFoo(); } finally { fooServerIdlingResource.decrement(); } } public void updateFoo(Foo foo) { fooServerIdlingResource.increment(); try { realFooServer.updateFoo(foo); } finally { fooServerIdlingResource.decrement(); } } } -
CountingIdlingResource public class DecoratedFooServer implements FooServer { private final FooServer
realFooServer; private final CountingIdlingResource fooServerIdlingResource; public DecoratedFooServer(FooServer realFooServer, CountingIdlingResource fooServerIdlingResource) { this.realFooServer = checkNotNull(realFooServer); this.fooServerIdlingResource = checkNotNull(fooServerIdlingResource); } public Foo newFoo() { fooServerIdlingResource.increment(); try { return realFooServer.newFoo(); } finally { fooServerIdlingResource.decrement(); } } public void updateFoo(Foo foo) { fooServerIdlingResource.increment(); try { realFooServer.updateFoo(foo); } finally { fooServerIdlingResource.decrement(); } } } -
CountingIdlingResource public class DecoratedFooServer implements FooServer { private final FooServer
realFooServer; private final CountingIdlingResource fooServerIdlingResource; public DecoratedFooServer(FooServer realFooServer, CountingIdlingResource fooServerIdlingResource) { this.realFooServer = checkNotNull(realFooServer); this.fooServerIdlingResource = checkNotNull(fooServerIdlingResource); } public Foo newFoo() { fooServerIdlingResource.increment(); try { return realFooServer.newFoo(); } finally { fooServerIdlingResource.decrement(); } } public void updateFoo(Foo foo) { fooServerIdlingResource.increment(); try { realFooServer.updateFoo(foo); } finally { fooServerIdlingResource.decrement(); } } } -
CustomError Handler class NoHierarchyException extends RuntimeException implements EspressoException { public
NoHierarchyException(NoMatchingViewException e) { super(new NoMatchingViewException.Builder().from(e).includeViewHierarchy(false).build()); } public NoHierarchyException(AmbiguousViewMatcherException e) { super(new AmbiguousViewMatcherException.Builder().from(e).includeViewHierarchy(false).build()); } } -
UiController viewInteraction = new ViewAction() { @Override public Matcher<View>
getConstraints() { return null; } @Override public String getDescription() { return DESCRIPTION; } @Override public void perform(UiController uiController, View view) { uiController.loopMainThreadForAtLeast(500); } }; Thread.sleep(200);