??????????У? ???????????????飬????????????д????????????????????????????? Espresso ???? ????????????????????????????????С????????????????????о???????????
??????????? APP ????????????Щ activity??
????· ??????? - ?????????δ?????? ???????????????????????????????????????У??????????????б? activity??
????· ?????б? - ??????????????б? activity????????????????????????????????????????????????????????????? recycler view ??????????????б?? id?? news_list???? ????????????δ??????????????????????з?????????????? ????????????????? recycler view ??????????????????????????????????б? activity ?и? “Change your Language” ??????????????????????????????? “Change your Languages” ?? ?????????????????鹹??APP ?????
????· ??????? - ????????????? ?????????????б???????????? activity??
??????? APP ????????????????????????о?????????б? activity ??д??????????? ??????????д????????
/*
Click on the first news item.
It should open NewsDetailActivity
*/
@Test
public void 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
public void testChangeLanguageFeature() {
int count = UserPreferenceUtil.getSelectedLanguagesCount();
if (count == 1) {
onView(withText("Choose your Language")).check(matches(isDisplayed()));
} else if (count > 1) {
onView(withText("Choose your Languages")).check(matches(isDisplayed()));
}
}
????????????????
?????????????????? testClickOnAnyNewsItem()?? ???????????з??????????????????????????????? recycler view ???????????????????????????????????? ?????????? PASS ???? FAIL???????????? recycler view ????????? ???????????recycler view ???????????????????????? FAILED??????????????????????????????
/*
Click on any news item.
It should open NewsDetailActivity
*/
@Test
public void 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()));
}
}
??????????????????????????????
??????????????? ????????????????? activity:
????· ???????
????· ?????б?
????· ???????
??????????????????????? activity??????? NewsList activity ?????????????????????????????????????????????б? activity ??????????????????????????????? - ???????????recycler view ???????????? ???????????????????????????? ????????в??????????? ????????????????????????е????????У?????????????????????? activity ??????
@Rule
public ActivityTestRule activityTestRule =
new ActivityTestRule(TopicsActivity.class?? false?? false);
/*
Click on any news item.
It should open NewsDetailActivity
*/
@Test
public void testClickOnAnyNewsItem() {
UserPreferenceUtil.saveUserPrimaryLanguage("english");
Intent intent = new Intent();
activityTestRule.launchActivity(intent);
try {
ViewInteraction viewInteraction = onView(withId(R.id.news_list));
viewInteraction.check(matches(isDisplayed()));
} catch (NoMatchingViewException e) {
return;
} catch (AssertionFailedError e) {
return;
}
onView(allOf(withId(R.id.news_list)?? isDisplayed())).perform(RecyclerViewActions
.actionOnItemAtPosition(1?? click()));
intended(hasComponent(NewsDetailsActivity.class.getName()));
}
??????????????б??????????????
?????????????????????? testChangeLanguageFeature() ?У?????????????????????????????????????????д?? if-else ?????????в???? ???? if-else ???????д?????????У?????????????????????????????????? ??????????У??????д??????????????????д??????????????????
/**
* To test the correct text on the button when only one language is selected.
*/
@Test
public void testChangeLanguageFeatureForSingeLanguage() {
//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
public void testChangeLanguageFeatureForMultipleLanguages() {
//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()));
}
??????????????????????????
??????????????У???????????????????????н????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? - ??????????????????????????????????????????????? ??????????У? ???????????????κθ?????- NO??
?????????????????????????????????????????????????????????????????????????????????????????Щ??????3??????????????????????д??????????????????п??????????????????????????????????????????????????????????????д??????????????????????????????????????
??????????д???????????д??Щ???????????????