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

Android Testing

Android Testing

Build a toolset to help you write rock-solid apps

Prateek Srivastava

February 04, 2014
Tweet

More Decks by Prateek Srivastava

Other Decks in Technology

Transcript

  1. public void maximizeVolume(AudioManager audioManager) { if (audioManager.getRingerMode() != RINGER_MODE_SILENT) {

    int max = audioManager.getStreamMaxVolume(STREAM_RING); audioManager.setStreamVolume(STREAM_RING, max, 0); } }
  2. public void testNormalRingerIsMaximized() { audioManager.setRingerMode(RINGER_MODE_NORMAL); maximizeVolume(audioManager); assertEquals(audioManager.getStreamVolume(STREAM_RING), max); } !

    public void testSilentRingerIsNotDisturbed() { audioManager.setRingerMode(RINGER_MODE_SILENT); maximizeVolume(audioManager); assertEquals(audioManager.getStreamVolume(STREAM_RING), 0); }
  3. public void testNormalRingerIsMaximized() { audioManager.setRingerMode(RINGER_MODE_NORMAL); maximizeVolume(audioManager); assertEquals(audioManager.getStreamVolume(STREAM_RING), max); } !

    public void testSilentRingerIsNotDisturbed() { audioManager.setRingerMode(RINGER_MODE_SILENT); maximizeVolume(audioManager); assertEquals(audioManager.getStreamVolume(STREAM_RING), 0); }
  4. @RunWith(RobolectricTestRunner.class) public void testNormalRingerIsMaximized() { audioManager.setRingerMode(RINGER_MODE_NORMAL); maximizeVolume(audioManager); assertEquals(audioManager.getStreamVolume(STREAM_RING), max); }

    ! @RunWith(RobolectricTestRunner.class) public void testSilentRingerIsNotDisturbed() { audioManager.setRingerMode(RINGER_MODE_SILENT); maximizeVolume(audioManager); assertEquals(audioManager.getStreamVolume(STREAM_RING), 0); }
  5. @RunWith(RobolectricTestRunner.class) public void testNormalRingerIsMaximized() { audioManager.setRingerMode(RINGER_MODE_NORMAL); maximizeVolume(audioManager); assertEquals(audioManager.getStreamVolume(STREAM_RING), max); }

    ! @RunWith(RobolectricTestRunner.class) public void testSilentRingerIsNotDisturbed() { audioManager.setRingerMode(RINGER_MODE_SILENT); maximizeVolume(audioManager); assertEquals(audioManager.getStreamVolume(STREAM_RING), 0); }
  6. ?

  7. @RunWith(RobolectricTestRunner.class) public void testNormalRingerIsMaximized() { audioManager.setRingerMode(RINGER_MODE_NORMAL); maximizeVolume(audioManager); assertThat(audioManager).hasStreamVolume(STREAM_RING, max); }

    ! @RunWith(RobolectricTestRunner.class) public void testSilentRingerIsNotDisturbed() { audioManager.setRingerMode(RINGER_MODE_SILENT); maximizeVolume(audioManager); assertThat(audioManager).hasStreamVolume(STREAM_RING, 0); }
  8. @RunWith(RobolectricTestRunner.class) public void testNormalRingerIsMaximized() { audioManager.setRingerMode(RINGER_MODE_NORMAL); maximizeVolume(audioManager); assertThat(audioManager).hasStreamVolume(STREAM_RING, max); }

    ! @RunWith(RobolectricTestRunner.class) public void testSilentRingerIsNotDisturbed() { audioManager.setRingerMode(RINGER_MODE_SILENT); maximizeVolume(audioManager); assertThat(audioManager).hasStreamVolume(STREAM_RING, 0); }
  9. public void testSilentRingerIsNotDisturbed() { // Prepare mocks and script their

    behavior. AudioManager audioManager = Mockito.mock(AudioManager.class); Mockito.when(audioManager.getRingerMode()) .thenReturn(RINGER_MODE_SILENT); ! // Test the code of interest. maximizeVolume(audioManager); ! // Validate that we saw exactly what we wanted. Mockito.verify(audioManager).getRingerMode(); Mockito.verifyNoMoreInteractions(audioManager); }
  10. public void testNormalRingerIsMaximized() { // Prepare mocks and script their

    behavior. AudioManager audioManager = mock(AudioManager.class); when(audioManager.getRingerMode()) .thenReturn(RINGER_MODE_NORMAL); when(audioManager.getStreamMaxVolume(STREAM_RING)) .thenReturn(100); ! // Test the code of interest. maximizeVolume(audioManager); ! // Validate that we saw exactly what we wanted. verify(audioManager) .setStreamVolume(STREAM_RING, 100, 0); }
  11. public void testPasswordTooShort_ShowsError() { // Make sure the initial state

    does not show any errors. assertThat(username).hasNoError(); assertThat(password).hasNoError(); instrumentation.runOnMainSync(new Runnable() { @Override public void run() { // Type a value into the username and password fields. username.setText("prateek"); password.setText(“password”); // Click the "login" button. login.performClick(); } }); instrumentation.waitForIdleSync(); ! // Verify error was shown only for username field. assertThat(username).hasNoError(); assertThat(password).hasError(R.string.password_length); }
  12. public void testPasswordTooShort_ShowsError() { // Make sure the initial state

    does not show any errors. assertThat(username).hasNoError(); assertThat(password).hasNoError(); ! // Type a value into the username and password fields. solo.typeText(username, “prateek”); solo.typeText(password, “password”); // Click the "login" button. solo.clickOnView(login); ! // Verify error was shown only for username field. assertThat(username).hasNoError(); assertThat(password).hasError(R.string.password_length); }
  13. public void testPasswordTooShort_ShowsError() { // Make sure the initial state

    does not show any errors. assertThat(username).hasNoError(); assertThat(password).hasNoError(); ! // Type a value into the username and password fields. onView(withId(R.id.username)).perform(typeText("prateek")); onView(withId(R.id.password)).perform(typeText("password")); onView(withId(R.id.login)).perform(click()); ! // Verify error was shown only for username field. assertThat(username).hasNoError(); assertThat(password).hasError(R.string.password_length); }
  14. public void testPasswordTooShort_ShowsError() { Spoon.screenshot(activity, "initial_state"); assertThat(username).hasNoError(); assertThat(password).hasNoError(); ! onView(withId(R.id.username)).perform(typeText("prateek"));

    onView(withId(R.id.password)).perform(typeText("password")); Spoon.screenshot(activity, "values_entered"); onView(withId(R.id.login)).perform(click()); Spoon.screenshot(activity, "login_clicked"); ! assertThat(username).hasNoError(); assertThat(password).hasError(R.string.password_length); }
  15. public void testPasswordTooShort_ShowsError() { Spoon.screenshot(activity, "initial_state"); assertThat(username).hasNoError(); assertThat(password).hasNoError(); ! onView(withId(R.id.username)).perform(typeText("prateek"));

    onView(withId(R.id.password)).perform(typeText("password")); Spoon.screenshot(activity, "values_entered"); onView(withId(R.id.login)).perform(click()); Spoon.screenshot(activity, "login_clicked"); ! assertThat(username).hasNoError(); assertThat(password).hasError(R.string.password_length); }