下面列出了怎么用android.support.test.espresso.IdlingPolicies的API类实例代码及写法,或者点击链接到github查看源代码。
@Test
public void register_existingUsername() {
onView(withId(R.id.username)).perform(typeText(EXISTING_USERNAME), closeSoftKeyboard());
onView(withId(R.id.register_button)).perform(click());
IdlingPolicies.setIdlingResourceTimeout(NETWORK_TIMEOUT_SECONDS, TimeUnit.SECONDS);
IdlingPolicies.setMasterPolicyTimeout(NETWORK_TIMEOUT_SECONDS * 2, TimeUnit.SECONDS);
IdlingResource idlingResource = new NetworkRequestIdlingResources(rule.getActivity().registrationPresenter);
IdlingRegistry.getInstance().register(idlingResource);
onView(withText(R.string.error_dialog_title))
.inRoot(isDialog())
.check(matches(isDisplayed()));
onView(withText(R.string.username_already_exists_error_dialog_message))
.inRoot(isDialog())
.check(matches(isDisplayed()));
onView(withText(android.R.string.ok))
.inRoot(isDialog())
.check(matches(isDisplayed()));
IdlingRegistry.getInstance().unregister(idlingResource);
}
private void makeScreenshots0(Context context, String languageTag, String theme)
{
SuntimesTestConfig configuration = defaultConfig;
if (config.containsKey(languageTag)) {
configuration = config.get(languageTag);
}
configureAppForTesting(context, languageTag, configuration, theme);
activityRule.getActivity().finish();
activityRule.launchActivity(activityRule.getActivity().getIntent());
onView( withId(android.R.id.content)).perform(ViewActions.swipeUp());
long waitTime = 6 * 1000; // wait a moment
IdlingResource waitForResource = new ElapsedTimeIdlingResource(waitTime);
IdlingPolicies.setMasterPolicyTimeout(waitTime * 2, TimeUnit.MILLISECONDS);
IdlingPolicies.setIdlingResourceTimeout(waitTime * 2, TimeUnit.MILLISECONDS);
registerIdlingResources(waitForResource);
captureScreenshot(activityRule.getActivity(), version + "/" + languageTag, "activity-alarms0-" + theme);
unregisterIdlingResources(waitForResource);
}
private void makeScreenshots0(Context context, String languageTag, String theme)
{
SuntimesTestConfig configuration = defaultConfig;
if (config.containsKey(languageTag)) {
configuration = config.get(languageTag);
}
configureAppForTesting(context, languageTag, configuration, theme);
activityRule.launchActivity(activityRule.getActivity().getIntent());
long waitTime = 3 * 1000; // wait a moment
IdlingResource waitForResource = new ElapsedTimeIdlingResource(waitTime);
IdlingPolicies.setMasterPolicyTimeout(waitTime * 2, TimeUnit.MILLISECONDS);
IdlingPolicies.setIdlingResourceTimeout(waitTime * 2, TimeUnit.MILLISECONDS);
registerIdlingResources(waitForResource);
captureScreenshot(activityRule.getActivity(), version + "/" + languageTag, "activity-main0-" + theme);
unregisterIdlingResources(waitForResource);
}
@Test
public void register_validUsername() {
onView(withId(R.id.username)).perform(typeText(username), closeSoftKeyboard());
onView(withId(R.id.register_button)).perform(click());
IdlingPolicies.setIdlingResourceTimeout(NETWORK_TIMEOUT_SECONDS, TimeUnit.SECONDS);
IdlingPolicies.setMasterPolicyTimeout(NETWORK_TIMEOUT_SECONDS * 2, TimeUnit.SECONDS);
IdlingResource idlingResource = new NetworkRequestIdlingResources(rule.getActivity().registrationPresenter);
IdlingRegistry.getInstance().register(idlingResource);
intended(hasComponent(MainActivity.class.getName()));
IdlingRegistry.getInstance().unregister(idlingResource);
}
@Test
public void experimentActivationForWhitelistUser() throws Exception {
IdlingPolicies.setMasterPolicyTimeout(3, TimeUnit.MINUTES);
IdlingPolicies.setIdlingResourceTimeout(3, TimeUnit.MINUTES);
// Check that the text was changed.
// These tests are pointed at a real project.
// The user 'test_user` is in the whitelist for variation_a for experiment background_experiment
onView(withId(R.id.tv_variation_a_text_1))
.check(matches(isDisplayed()));
// here i am rescheduling the data file service. this is because in the splash activity after optimizely startup
// the app unschedules the data file service.
serviceScheduler.schedule(dataFileServiceIntent, TimeUnit.DAYS.toMillis(1L));
// Espresso will wait for Optimizely to start due to the registered idling resources
assertTrue(serviceScheduler.isScheduled(dataFileServiceIntent));
onView(withId(R.id.btn_variation_conversion)) // withId(R.id.my_view) is a ViewMatcher
.perform(click()); // click() is a ViewAction
List<Pair<String, String>> events = CountingIdlingResourceManager.getEvents();
assertTrue(events.size() == 2);
Iterator<Pair<String, String>> iterator = events.iterator();
while (iterator.hasNext()) {
Pair<String, String> event = iterator.next();
final String url = event.first;
final String payload = event.second;
if (url.equals("https://logx.optimizely.com/v1/events") && payload.contains("11178792174") && payload.contains("11146534908")
|| url.equals("https://logx.optimizely.com/v1/events") && payload.contains("sample_conversion")) {
iterator.remove();
}
}
assertTrue(events.isEmpty());
}
private void userSwappedCard()
{
Matcher<View> cardFlipper = withId(R.id.info_time_flipper1);
onView(cardFlipper).check(assertShown); // flipper should be visible
boolean cardSetToToday = viewIsDisplayed(R.id.info_time_all_today, "Today");
// pre-click checks
if (cardSetToToday)
verifyTimeCard_today();
else verifyTimeCard_tomorrow();
// click the next/prev button
if (cardSetToToday)
swapCardNext();
else swapCardPrev();
cardSetToToday = !cardSetToToday;
// post-click checks
if (cardSetToToday)
verifyTimeCard_today();
else verifyTimeCard_tomorrow();
// wait a minute (and check again)
long waitTime = 60 * 1000;
IdlingResource waitForResource = new ElapsedTimeIdlingResource(waitTime);
IdlingPolicies.setMasterPolicyTimeout(waitTime * 2, TimeUnit.MILLISECONDS);
IdlingPolicies.setIdlingResourceTimeout(waitTime * 2, TimeUnit.MILLISECONDS);
// during that minute
// the app will update the clock and note area at least once
registerIdlingResources(waitForResource); // afterward...
verifyTimeCard_today();
unregisterIdlingResources(waitForResource);
}
private void makeScreenshots1(Context context, String languageTag, String theme)
{
SuntimesTestConfig configuration = defaultConfig;
if (config.containsKey(languageTag)) {
configuration = config.get(languageTag);
}
configureAppForTesting(context, languageTag, configuration, theme);
activityRule.launchActivity(activityRule.getActivity().getIntent());
long waitTime = 3 * 1000; // wait a moment
IdlingResource waitForResource = new ElapsedTimeIdlingResource(waitTime);
IdlingPolicies.setMasterPolicyTimeout(waitTime * 2, TimeUnit.MILLISECONDS);
IdlingPolicies.setIdlingResourceTimeout(waitTime * 2, TimeUnit.MILLISECONDS);
registerIdlingResources(waitForResource);
// main activity
captureScreenshot(activityRule.getActivity(), version + "/" + languageTag, "activity-main0-" + theme);
// dialogs
DialogTest.showAboutDialog(context, false);
captureScreenshot(activityRule.getActivity(), version + "/" + languageTag, "dialog-about-" + theme);
DialogTest.cancelAboutDialog();
DialogTest.showHelpDialog(context, false);
captureScreenshot(activityRule.getActivity(), version + "/" + languageTag, "dialog-help-" + theme);
DialogTest.cancelHelpDialog();
DialogTest.showEquinoxDialog(context, false);
captureScreenshot(activityRule.getActivity(), version + "/" + languageTag, "dialog-equinox-" + theme);
DialogTest.cancelEquinoxDialog();
DialogTest.showLightmapDialog(context, false);
captureScreenshot(activityRule.getActivity(), version + "/" + languageTag, "dialog-lightmap-" + theme);
DialogTest.cancelLightmapDialog();
TimeZoneDialogTest.showTimezoneDialog(activityRule.getActivity(), false);
captureScreenshot(activityRule.getActivity(), version + "/" + languageTag, "dialog-timezone0-" + theme);
TimeZoneDialogTest.inputTimezoneDialog_mode(context, WidgetSettings.TimezoneMode.SOLAR_TIME);
captureScreenshot(activityRule.getActivity(), version + "/" + languageTag, "dialog-timezone1-" + theme);
TimeZoneDialogTest.cancelTimezoneDialog();
AlarmDialogTest.showAlarmDialog(context, false);
captureScreenshot(activityRule.getActivity(), version + "/" + languageTag, "dialog-alarm-" + theme);
AlarmDialogTest.cancelAlarmDialog();
TimeDateDialogTest.showDateDialog(context, false);
captureScreenshot(activityRule.getActivity(), version + "/" + languageTag, "dialog-date-" + theme);
TimeDateDialogTest.cancelDateDialog();
LocationDialogTest.showLocationDialog(false);
captureScreenshot(activityRule.getActivity(), version + "/" + languageTag, "dialog-location0-" + theme);
LocationDialogTest.editLocationDialog(false);
captureScreenshot(activityRule.getActivity(), version + "/" + languageTag, "dialog-location1-" + theme);
LocationDialogTest.cancelLocationDialog(context);
}
@Before
public void espressoPreconditions() {
IdlingPolicies.setMasterPolicyTimeout(10, TimeUnit.MINUTES);
}