Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Espresso IO Extended Bangkok

Espresso IO Extended Bangkok

25/06/2016

Iñaki Villar

June 24, 2016
Tweet

More Decks by Iñaki Villar

Other Decks in Technology

Transcript

  1. 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'

  2. public class MainActivityTest {
 
 @Rule
 public ActivityTestRule<MainActivity> activityTestRule =

    new ActivityTestRule<>(MainActivity.class);
 
 @Test
 public void findViewPerformActionDone() { }
 
 } @Rule
 public final ServiceTestRule serviceRule = new ServiceTestRule();
 
 
 Rules
  3. 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();
 }
 };
 }

  4. 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();
 }
 };
 }

  5. @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
  6. @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
  7. @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
  8. Idling Resources public interface IdlingResource {
 
 public String getName();


    public boolean isIdleNow();
 public void registerIdleTransitionCallback(ResourceCallback callback);
 public interface ResourceCallback {
 public void onTransitionToIdle();
 } }
  9. 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();
 }
 

  10. 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();
 }
 }
 }

  11. 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();
 }
 }
 }

  12. 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();
 }
 }
 }

  13. 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());
 }
 }
  14. 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);