/* Click on the first news item. It should open NewsDetailActivity */ @Test publicvoid testClickOnAnyNewsItem() { onView(allOf(withId(R.id.news_list), isDisplayed())).perform(RecyclerViewActions .actionOnItemAtPosition(1, click())); intended(hasComponent(NewsDetailsActivity.class.getName())); }
/** * To test the correct text on the button */ @Test publicvoid testChangeLanguageFeature() { int count = UserPreferenceUtil.getSelectedLanguagesCount(); if (count == 1) { onView(withText("Choose your Language")).check(matches(isDisplayed())); } elseif (count > 1) { onView(withText("Choose your Languages")).check(matches(isDisplayed())); } ?}
/* Click on any news item. It should open NewsDetailActivity */ @Test publicvoid testClickOnAnyNewsItem() { try { /*To test this case, we need to have recyclerView present. If we don't have the recyclerview present either due to the presence of error_screen, then we should consider this test case successful. The test case should be unsuccesful only when we click on a news item and it doesn't open NewsDetail activity */ ViewInteraction viewInteraction = onView(withId(R.id.news_list)); viewInteraction.check(matches(isDisplayed())); } catch (NoMatchingViewException e) { return; } catch (AssertionFailedError e) { return; } //在这里我们确信,news_list的 recyclerview 对用户是可见的。 onView(allOf(withId(R.id.news_list), isDisplayed())).perform(RecyclerViewActions .actionOnItemAtPosition(1, click())); intended(hasComponent(NewsDetailsActivity.class.getName())); } }
/** * To test the correct text on the button when only one language is selected. */ @Test publicvoidtestChangeLanguageFeatureForSingeLanguage() { //Other initializations UserPreferenceUtil.saveSelectedLanguagesCount(1); Intent intent = new Intent(); activityTestRule.launchActivity(intent); onView(withText("Choose your Language")).check(matches(isDisplayed())); }
/** * To test the correct text on the button when more than one language is selected. */ @Test publicvoidtestChangeLanguageFeatureForMultipleLanguages() { //Other initializations UserPreferenceUtil.saveSelectedLanguagesCount(5); //Write anything greater than 1. Intent intent = new Intent(); activityTestRule.launchActivity(intent); onView(withText("Choose your Languages")).check(matches(isDisplayed())); }