mActivity = getActivity(); // Get a handle to the Activity object's main UI widget, a Spinner mSpinner = (Spinner) mActivity.findViewById(com.android.example.spinner.R.id.Spinner); // Set the Spinner to a known position mActivity.setSpinnerPosition(TEST_STATE_DESTROY_POSITION); // Stop the activity - The onDestroy() method should save the state of the Spinner mActivity.finish(); // Re-start the Activity - the onResume() method should restore the state of the Spinner mActivity = getActivity(); // Get the Spinner's current position int currentPosition = mActivity.getSpinnerPosition(); // Assert that the current position is the same as the starting position assertEquals(TEST_STATE_DESTROY_POSITION, currentPosition); Test Activity state saving and restoration
if a double has the value NaN (not a number) */ public class IsNotANumber extends TypeSafeMatcher<Double> { @Override public boolean matchesSafely(Double number) { return number.isNaN(); } public void describeTo(Description description) { description.appendText("not a number"); } @Factory public static <T> Matcher<Double> notANumber() { return new IsNotANumber(); } }
// Click on the add note button onView(withId(R.id.fab_add_notes)).perform(click()); // Add note title and description onView(withId(R.id.add_note_title)).perform(typeText(“More”)); onView(withId(R.id.add_note_description)).perform(typeText(“Testing”)); // Save the note onView(withId(R.id.fab_add_notes)).perform(click()); // Verify note is displayed on screen onView(withItemText(“Testing”)).check(matches(isDisplayed())); }
// Click on the add note button onView(withId(R.id.fab_add_notes)).perform(click()); // Add note title and description onView(withId(R.id.add_note_title)).perform(typeText(“More”)); onView(withId(R.id.add_note_description)).perform(typeText(“Testing”)); // Save the note onView(withId(R.id.fab_add_notes)).perform(click()); // Verify note is displayed on screen onView(withItemText(“Testing”)).check(matches(isDisplayed())); }
// Click on the add note button onView(withId(R.id.fab_add_notes)).perform(click()); // Add note title and description onView(withId(R.id.add_note_title)).perform(typeText(“More”)); onView(withId(R.id.add_note_description)).perform(typeText(“Testing”)); // Save the note onView(withId(R.id.fab_add_notes)).perform(click()); // Verify note is displayed on screen onView(withItemText(“Testing”)).check(matches(isDisplayed())); }
that results in an external "phone" activity being launched. user.clickOnView(system.getView(R.id.callButton)); // Using a canned RecordedIntentMatcher to validate that an intent resolving // to the "phone" activity has been sent. intended(toPackage("com.android.phone")); }
// Build a result to return when a particular activity is launched. Intent resultData = new Intent(); String phoneNumber = "012-345-678"; resultData.putExtra("phone", phoneNumber); ActivityResult result = new ActivityResult(Activity.RESULT_OK, resultData); // Set up result stubbing when an intent sent to "contacts" is seen. intending(toPackage("com.android.contacts")).respondWith(result)); // User action that results in "contacts" activity being launched. // Launching activity expects phoneNumber to be returned and displays it on the screen. onView(withId(R.id.pickButton)).perform(click()); // Assert that data we set up above is shown. onView(withId(R.id.phoneNumber).check(matches(withText(phoneNumber))); }
public String getName() { return IdleMonitor.class.getSimpleName(); } @Override public boolean isIdleNow() { // return true if resource is idle return false; } @Override public void registerIdleTransitionCallback(ResourceCallback resourceCallback) { // store a reference to the resourceCallback // notify resourceCallback when idle } } Espresso.registerIdlingResources(idlingResource);
REST adapter which points to the GitHub API endpoint. RestAdapter restAdapter = new RestAdapter.Builder() .setEndpoint(API_URL) .build(); // Wrap our REST adapter to allow mock implementations and fake network delay. MockRestAdapter mockRestAdapter = MockRestAdapter.from(restAdapter); // Instantiate a mock object so we can interact with it later. MockGitHub mockGitHub = new MockGitHub(); // Use the mock REST adapter and our mock object to create the API interface. GitHub gitHub = mockRestAdapter.create(GitHub.class, mockGitHub); // Query for some contributors for a few repositories. printContributors(gitHub, "square", "retrofit"); printContributors(gitHub, "square", "picasso");
new MediaControllerCompat(getContext(), audioPlayerService.getMediaSessionToken()); String uri = "http://stream.be/service/mp3:web/program_ondemand_128.mp3/playlist.m3u8"; Bundle extras = new Bundle(); extras.putBoolean(AudioPlayerService.EXTRA_CONTINUE_ON_RESUME, false); mediaController.getTransportControls().playFromMediaId(uri, extras); await().atMost(10, TimeUnit.SECONDS).until(musicIsActive()); } Test asynchronous system Express expectations in easy to read manner
new MediaControllerCompat(getContext(), audioPlayerService.getMediaSessionToken()); String uri = "http://stream.be/service/mp3:web/program_ondemand_128.mp3/playlist.m3u8"; Bundle extras = new Bundle(); extras.putBoolean(AudioPlayerService.EXTRA_CONTINUE_ON_RESUME, false); mediaController.getTransportControls().playFromMediaId(uri, extras); await().atMost(10, TimeUnit.SECONDS).until(musicIsActive()); } Test asynchronous system Express expectations in easy to read manner
Continuous Integration: Improving Software Quality and Reducing Risk Duvall, Paul M. et al. (978-0321336385) Working Effectively with Legacy Code Feathers, Michael (978-0131177055)