Commit 850e8d2b authored by Andrew Luo's avatar Andrew Luo Committed by Commit Bot

Add withTextString, withDescString locators, and some refactorings.

Refactorings include replacing *ByIndex with withIndex wrapper, renamed
locators that take variable number of resource ids to have "Any" in the name, removed
unnecessary params in the Page Controller tests build target.

Bug: 924194
Change-Id: I0a2b01c4d1d7ca52243988c8f028553135843441
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1846480
Commit-Queue: Andrew Luo <aluo@chromium.org>
Reviewed-by: default avatarJohn Budorick <jbudorick@chromium.org>
Reviewed-by: default avatarSky Malice <skym@chromium.org>
Cr-Commit-Position: refs/heads/master@{#703953}
parent 89bfcae4
......@@ -28,6 +28,7 @@ android_library("chrome_java_test_pagecontroller") {
"javatests/src/org/chromium/chrome/test/pagecontroller/utils/BySelectorIndexUi2Locator.java",
"javatests/src/org/chromium/chrome/test/pagecontroller/utils/BySelectorUi2Locator.java",
"javatests/src/org/chromium/chrome/test/pagecontroller/utils/ChildIndexUi2Locator.java",
"javatests/src/org/chromium/chrome/test/pagecontroller/utils/IndexUi2Locator.java",
"javatests/src/org/chromium/chrome/test/pagecontroller/utils/IUi2Locator.java",
"javatests/src/org/chromium/chrome/test/pagecontroller/utils/PathUi2Locator.java",
"javatests/src/org/chromium/chrome/test/pagecontroller/utils/Ui2Locators.java",
......@@ -52,8 +53,6 @@ instrumentation_test_apk("chrome_java_test_pagecontroller_tests") {
apk_name = "ChromePageControllerTests"
apk_under_test = "//chrome/android:chrome_public_apk"
android_manifest = "javatests/src/org/chromium/chrome/test/pagecontroller/tests/AndroidManifest.xml"
target_sdk_version = 28
testonly = true
java_files = [
"javatests/src/org/chromium/chrome/test/pagecontroller/tests/ExampleTest.java",
"javatests/src/org/chromium/chrome/test/pagecontroller/tests/FirstRunControllerTest.java",
......@@ -64,11 +63,6 @@ instrumentation_test_apk("chrome_java_test_pagecontroller_tests") {
":chrome_java_test_pagecontroller",
"//third_party/junit",
]
if (!is_java_debug) {
proguard_enabled = true
proguard_configs = [ "//chrome/android/java/apk_for_test.flags" ]
}
}
junit_binary("chrome_java_test_pagecontroller_unit_tests") {
......@@ -77,6 +71,7 @@ junit_binary("chrome_java_test_pagecontroller_unit_tests") {
"javatests/src/org/chromium/chrome/test/pagecontroller/utils/BySelectorIndexUi2LocatorTest.java",
"javatests/src/org/chromium/chrome/test/pagecontroller/utils/BySelectorUi2LocatorTest.java",
"javatests/src/org/chromium/chrome/test/pagecontroller/utils/ChildIndexUi2LocatorTest.java",
"javatests/src/org/chromium/chrome/test/pagecontroller/utils/IndexUi2LocatorTest.java",
"javatests/src/org/chromium/chrome/test/pagecontroller/utils/PathUi2LocatorTest.java",
"javatests/src/org/chromium/chrome/test/pagecontroller/utils/TestUtils.java",
"javatests/src/org/chromium/chrome/test/pagecontroller/utils/Ui2LocatorsTest.java",
......
......@@ -2,5 +2,7 @@ file://chrome/android/OWNERS
jbudorick@chromium.org
per-file BUILD.gn=*
# COMPONENT: Test>Android
# OS: Android
......@@ -33,9 +33,9 @@ class CoolNewPageController extends PageController {
// Locators allow the controller to find UI elements on the page
// It is preferred to use Resource Ids to find elements since they are
// stable across minor UI changes.
private static final IUi2Locator LOCATOR_COOL_PAGE = Ui2Locators.withResIds("cool_page");
private static final IUi2Locator LOCATOR_COOL_PAGE = Ui2Locators.withAnyResEntry(R.id.cool_page);
// Any of the resource ids in the list will result in a match.
private static final IUi2Locator LOCATOR_COOL_BUTTON = Ui2Locators.withResIds("cool_button_v1", "cool_button_v2");
private static final IUi2Locator LOCATOR_COOL_BUTTON = Ui2Locators.withAnyResEntry(R.id.cool_button_v1, R.id.cool_button_v2);
public CoolerPageController clickButton() {
// [UiAutomatorUtils.click](https://cs.chromium.org/chromium/src/chrome/test/android/javatests/src/org/chromium/chrome/test/pagecontroller/utils/UiAutomatorUtils.java?q=click) operates on UI elements via IUi2Locators.
......
......@@ -8,8 +8,16 @@ import org.chromium.chrome.test.pagecontroller.utils.UiAutomatorUtils;
import org.chromium.chrome.test.pagecontroller.utils.UiLocatorHelper;
/**
* Base class for a Page Controller or Page Element Controller.
* Allows Controllers to perform UI actions and verify UI state.
* Base class for any controller that needs to perform UI actions and verify UI
* state.
*
* Could represent anything from buttons to a news snippet on the New Tab Page,
* although simple elements such as buttons can be controlled by
* helper methods such as {@link UiAutomatorUtils#click} so that implementing
* ElementControllers for them is usually not necessary.
*
* For modal UI such as the 3-dot menu, its controller should subclass
* PageController.
*/
public class ElementController {
protected final UiAutomatorUtils mUtils;
......
......@@ -9,9 +9,24 @@ import android.os.RemoteException;
import org.chromium.chrome.test.pagecontroller.utils.UiLocationException;
/**
* Base class for page controllers.
* A page controller allows tests to interact with a single page (think Android activity)
* in the app-under-test.
* Base class for Page Controllers.
*
* A Page Controller allows tests to interact with a single modal view in the
* app-under-test. "Page" here does not mean a web-page, but more like Android
* Activity. Although special pages such as "chrome://version" could be
* implemented as a distinct PageController which other normal webpages can be
* loaded in a more generic UrlPage controller. Modal menus or dialogs
* controllers are also appropriate to implement as PageControllers.
*
* When implementing methods that would otherwise return void, but does not
* navigate to another PageController, consider returning "this" to facilitate
* chaining.
*
* When implementing methods that navigates to another PageController, consider
* returning a verified instance of that controller by returning the result of
* its verifyActive() method to facilitate chaining. An exception to this
* would be if experiments could cause navigation to different PageControllers,
* then the return value should be void.
*/
public abstract class PageController extends ElementController {
public void pressAndroidBackButton() {
......
......@@ -14,8 +14,8 @@ import org.chromium.chrome.test.pagecontroller.utils.Ui2Locators;
*/
public class DataSaverController extends PageController {
private static final IUi2Locator LOCATOR_DATA_SAVER =
Ui2Locators.withResEntries(R.id.enable_data_saver_switch);
private static final IUi2Locator LOCATOR_NEXT = Ui2Locators.withResEntries(R.id.next_button);
Ui2Locators.withAnyResEntry(R.id.enable_data_saver_switch);
private static final IUi2Locator LOCATOR_NEXT = Ui2Locators.withAnyResEntry(R.id.next_button);
private static final DataSaverController sInstance = new DataSaverController();
private DataSaverController() {}
......
......@@ -13,10 +13,10 @@ import org.chromium.chrome.test.pagecontroller.utils.Ui2Locators;
* Sync Dialog (part of First Run Experience) Page Controller.
*/
public class SyncController extends PageController {
private final static IUi2Locator LOCATOR_SYNC_CONTROLLER =
Ui2Locators.withResEntries(R.id.signin_sync_title);
private final static IUi2Locator LOCATOR_NO_THANKS =
Ui2Locators.withResEntries(R.id.negative_button);
private static final IUi2Locator LOCATOR_SYNC_CONTROLLER =
Ui2Locators.withAnyResEntry(R.id.signin_sync_title);
private static final IUi2Locator LOCATOR_NO_THANKS =
Ui2Locators.withAnyResEntry(R.id.negative_button);
private static final SyncController sInstance = new SyncController();
private SyncController() {}
......
......@@ -13,10 +13,12 @@ import org.chromium.chrome.test.pagecontroller.utils.Ui2Locators;
* TOS Dialog (part of the First Run Experience) Page Controller.
*/
public class TOSController extends PageController {
private static final IUi2Locator LOCATOR_TOS = Ui2Locators.withResEntries(R.id.tos_and_privacy);
private static final IUi2Locator LOCATOR_TOS =
Ui2Locators.withAnyResEntry(R.id.tos_and_privacy);
private static final IUi2Locator LOCATOR_SEND_REPORT_CHECKBOX =
Ui2Locators.withResEntries(R.id.send_report_checkbox);
private static final IUi2Locator LOCATOR_ACCEPT = Ui2Locators.withResEntries(R.id.terms_accept);
Ui2Locators.withAnyResEntry(R.id.send_report_checkbox);
private static final IUi2Locator LOCATOR_ACCEPT =
Ui2Locators.withAnyResEntry(R.id.terms_accept);
private static final TOSController sInstance = new TOSController();
private TOSController() {}
......
......@@ -15,7 +15,7 @@ public class DownloadNotificationController extends PageController {
private static final IUi2Locator LOCATOR_DOWNLOAD_NOTIFICATION =
Ui2Locators.withTextContaining("needs storage access to download files");
private static final IUi2Locator LOCATOR_CONTINUE =
Ui2Locators.withResEntries(android.R.id.button1);
Ui2Locators.withAnyResEntry(android.R.id.button1);
private static final DownloadNotificationController sInstance =
new DownloadNotificationController();
......
......@@ -17,20 +17,25 @@ import org.chromium.chrome.test.pagecontroller.utils.Ui2Locators;
public class ArticleActionsMenu extends PageController {
private static final IUi2Locator LOCATOR_MENU = Ui2Locators.withClassRegex(".*ListView");
private static final IUi2Locator LOCATOR_OPEN_NEW_TAB = Ui2Locators.withResEntriesByIndex(
0, org.chromium.chrome.R.id.title, R.id.feed_simple_list_item);
private static final IUi2Locator LOCATOR_OPEN_INCOGNITO = Ui2Locators.withResEntriesByIndex(
1, org.chromium.chrome.R.id.title, R.id.feed_simple_list_item);
private static final IUi2Locator LOCATOR_DOWNLOAD_LINK = Ui2Locators.withResEntriesByIndex(
2, org.chromium.chrome.R.id.title, R.id.feed_simple_list_item);
private static final IUi2Locator LOCATOR_REMOVE = Ui2Locators.withResEntriesByIndex(
3, org.chromium.chrome.R.id.title, R.id.feed_simple_list_item);
private static final IUi2Locator LOCATOR_LEARN_MORE = Ui2Locators.withResEntriesByIndex(
4, org.chromium.chrome.R.id.title, R.id.feed_simple_list_item);
private static final IUi2Locator LOCATOR_OPEN_NEW_TAB = Ui2Locators.withIndex(0,
Ui2Locators.withAnyResEntry(
org.chromium.chrome.R.id.title, R.id.feed_simple_list_item));
private static final IUi2Locator LOCATOR_OPEN_INCOGNITO = Ui2Locators.withIndex(1,
Ui2Locators.withAnyResEntry(
org.chromium.chrome.R.id.title, R.id.feed_simple_list_item));
private static final IUi2Locator LOCATOR_DOWNLOAD_LINK = Ui2Locators.withIndex(2,
Ui2Locators.withAnyResEntry(
org.chromium.chrome.R.id.title, R.id.feed_simple_list_item));
private static final IUi2Locator LOCATOR_REMOVE = Ui2Locators.withIndex(3,
Ui2Locators.withAnyResEntry(
org.chromium.chrome.R.id.title, R.id.feed_simple_list_item));
private static final IUi2Locator LOCATOR_LEARN_MORE = Ui2Locators.withIndex(4,
Ui2Locators.withAnyResEntry(
org.chromium.chrome.R.id.title, R.id.feed_simple_list_item));
static private ArticleActionsMenu sInstance = new ArticleActionsMenu();
private static ArticleActionsMenu sInstance = new ArticleActionsMenu();
private ArticleActionsMenu() {}
static public ArticleActionsMenu getInstance() {
public static ArticleActionsMenu getInstance() {
return sInstance;
}
......
......@@ -32,7 +32,7 @@ public class ArticleCardController extends ElementController {
* Represents a single article, can be used by the NewTabPageController
* to perform actions.
*/
static public class Info {
public static class Info {
private final String mHeadline, mPublisher, mAge;
private final ImplementationType mImplType;
......@@ -83,9 +83,9 @@ public class ArticleCardController extends ElementController {
}
}
private static abstract class ArticleImpl {
abstract public List<Info> parseScreenForArticles(UiLocatorHelper locatorHelper);
abstract public IUi2Locator getLocator(Info cardInfo);
private abstract static class ArticleImpl {
public abstract List<Info> parseScreenForArticles(UiLocatorHelper locatorHelper);
public abstract IUi2Locator getLocator(Info cardInfo);
protected List<Info> parseScreenForArticles(final UiLocatorHelper locatorHelper,
final ImplementationType implementationType, final IUi2Locator cardsLocator,
final IUi2Locator headlineLocator, final IUi2Locator publisherLocator,
......@@ -104,15 +104,16 @@ public class ArticleCardController extends ElementController {
} else if (isLastAttempt) {
return null;
} else {
if (headline == null)
if (headline == null) {
throw new UiLocationException(
"Headline not found.", headlineLocator, articleCard);
else if (publisher == null)
} else if (publisher == null) {
throw new UiLocationException(
"Publisher not found.", publisherLocator, articleCard);
else
} else {
throw new UiLocationException(
"Age not found.", ageLocator, articleCard);
}
}
}
});
......@@ -122,11 +123,11 @@ public class ArticleCardController extends ElementController {
private static class FeedArticleImpl extends ArticleImpl {
private static final IUi2Locator LOCATOR_NON_EMPTY_STRING = withTextRegex(".+");
private static final IUi2Locator LOCATOR_CARDS = Ui2Locators.withPath(
Ui2Locators.withResEntries(R.id.content),
Ui2Locators.withResEntries(com.google.android.libraries.feed.basicstream.R.id
.feed_stream_recycler_view),
Ui2Locators.withResEntries(com.google.android.libraries.feed.basicstream.internal
.viewholders.R.id.feed_content_card));
Ui2Locators.withAnyResEntry(R.id.content),
Ui2Locators.withAnyResEntry(com.google.android.libraries.feed.basicstream.R.id
.feed_stream_recycler_view),
Ui2Locators.withAnyResEntry(com.google.android.libraries.feed.basicstream.internal
.viewholders.R.id.feed_content_card));
private static final IUi2Locator LOCATOR_HEADLINE =
Ui2Locators.withPath(Ui2Locators.withChildIndex(0, 6), LOCATOR_NON_EMPTY_STRING);
private static final IUi2Locator LOCATOR_PUBLISHER =
......@@ -148,13 +149,14 @@ public class ArticleCardController extends ElementController {
private static class ZineArticleImpl extends ArticleImpl {
private static final IUi2Locator LOCATOR_CARDS =
Ui2Locators.withPath(Ui2Locators.withResEntries(R.id.content),
Ui2Locators.withResEntries(R.id.card_contents));
Ui2Locators.withPath(Ui2Locators.withAnyResEntry(R.id.content),
Ui2Locators.withAnyResEntry(R.id.card_contents));
private static final IUi2Locator LOCATOR_HEADLINE =
Ui2Locators.withResEntries(R.id.article_headline);
Ui2Locators.withAnyResEntry(R.id.article_headline);
private static final IUi2Locator LOCATOR_PUBLISHER =
Ui2Locators.withResEntries(R.id.article_publisher);
private static final IUi2Locator LOCATOR_AGE = Ui2Locators.withResEntries(R.id.article_age);
Ui2Locators.withAnyResEntry(R.id.article_publisher);
private static final IUi2Locator LOCATOR_AGE =
Ui2Locators.withAnyResEntry(R.id.article_age);
@Override
public List<Info> parseScreenForArticles(UiLocatorHelper locatorHelper) {
return parseScreenForArticles(locatorHelper, ImplementationType.ZINE, LOCATOR_CARDS,
......
......@@ -14,18 +14,20 @@ import org.chromium.chrome.test.pagecontroller.utils.Ui2Locators;
*/
public class ChromeMenu extends PageController {
private static final IUi2Locator LOCATOR_CHROME_MENU_BOX =
Ui2Locators.withResEntries(R.id.app_menu_list);
Ui2Locators.withAnyResEntry(R.id.app_menu_list);
private static final IUi2Locator LOCATOR_CHROME_MENU = Ui2Locators.withPath(
LOCATOR_CHROME_MENU_BOX, Ui2Locators.withResEntries(R.id.button_five));
LOCATOR_CHROME_MENU_BOX, Ui2Locators.withAnyResEntry(R.id.button_five));
private static final IUi2Locator LOCATOR_NEW_TAB =
Ui2Locators.withResEntriesByIndex(0, R.id.menu_item_text);
Ui2Locators.withPath(Ui2Locators.withAnyResEntry(R.id.menu_item_text),
Ui2Locators.withTextString(R.string.menu_new_tab));
private static final IUi2Locator LOCATOR_NEW_INCOGNITO_TAB =
Ui2Locators.withResEntriesByIndex(1, R.id.menu_item_text);
Ui2Locators.withPath(Ui2Locators.withAnyResEntry(R.id.menu_item_text),
Ui2Locators.withTextString(R.string.menu_new_incognito_tab));
private static final ChromeMenu sInstance = new ChromeMenu();
private ChromeMenu() {}
static public ChromeMenu getInstance() {
public static ChromeMenu getInstance() {
return sInstance;
}
......
......@@ -14,7 +14,7 @@ import org.chromium.chrome.test.pagecontroller.utils.Ui2Locators;
*/
public class IncognitoNewTabPageController extends PageController {
private static final IUi2Locator LOCATOR_INCOGNITO_PAGE =
Ui2Locators.withResEntries(R.id.new_tab_incognito_container);
Ui2Locators.withAnyResEntry(R.id.new_tab_incognito_container);
private static final IncognitoNewTabPageController sInstance =
new IncognitoNewTabPageController();
......
......@@ -30,31 +30,32 @@ public class NewTabPageController extends PageController {
private static final String REGEX_TEXT_HEADER_STATUS = "^Hide|Show$";
private static final IUi2Locator LOCATOR_SEARCH_BOX_TEXT =
Ui2Locators.withResEntries(R.id.search_box_text);
private static final IUi2Locator LOCATOR_URL_BAR = Ui2Locators.withResEntries(R.id.url_bar);
Ui2Locators.withAnyResEntry(R.id.search_box_text);
private static final IUi2Locator LOCATOR_URL_BAR = Ui2Locators.withAnyResEntry(R.id.url_bar);
private static final IUi2Locator LOCATOR_SEARCH_PROVIDER_LOGO =
Ui2Locators.withResEntries(R.id.search_provider_logo);
Ui2Locators.withAnyResEntry(R.id.search_provider_logo);
private static final IUi2Locator LOCATOR_MORE_BUTTON =
Ui2Locators.withResEntries(R.id.action_button);
Ui2Locators.withAnyResEntry(R.id.action_button);
private static final IUi2Locator LOCATOR_MENU_BUTTON =
Ui2Locators.withResEntries(R.id.menu_button);
Ui2Locators.withAnyResEntry(R.id.menu_button);
private static final IUi2Locator LOCATOR_BOTTOM_OF_PAGE =
Ui2Locators.withResEntries(R.id.progress_indicator, R.id.action_button);
private static final IUi2Locator LOCATOR_FEED_STREAM_RECYCLER_VIEW = Ui2Locators.withResEntries(
com.google.android.libraries.feed.basicstream.R.id.feed_stream_recycler_view);
Ui2Locators.withAnyResEntry(R.id.progress_indicator, R.id.action_button);
private static final IUi2Locator LOCATOR_FEED_STREAM_RECYCLER_VIEW =
Ui2Locators.withAnyResEntry(
com.google.android.libraries.feed.basicstream.R.id.feed_stream_recycler_view);
private static final IUi2Locator LOCATOR_HEADER_STATUS =
Ui2Locators.withPath(Ui2Locators.withResEntries(R.id.header_status),
Ui2Locators.withPath(Ui2Locators.withAnyResEntry(R.id.header_status),
Ui2Locators.withTextRegex(REGEX_TEXT_HEADER_STATUS));
private static final IUi2Locator LOCATOR_INFO_BAR_MESSAGE =
Ui2Locators.withResEntries(R.id.infobar_message, R.id.snackbar_message);
Ui2Locators.withAnyResEntry(R.id.infobar_message, R.id.snackbar_message);
private static final IUi2Locator LOCATOR_INFO_BAR_CLOSE =
Ui2Locators.withResEntries(R.id.infobar_close_button);
Ui2Locators.withAnyResEntry(R.id.infobar_close_button);
private static final IUi2Locator LOCATOR_TAB_SWITCHER =
Ui2Locators.withResEntries(R.id.tab_switcher_button);
Ui2Locators.withAnyResEntry(R.id.tab_switcher_button);
private static final IUi2Locator LOCATOR_NEW_TAB_PAGE =
Ui2Locators.withResEntries(R.id.ntp_content,
Ui2Locators.withAnyResEntry(R.id.ntp_content,
com.google.android.libraries.feed.basicstream.R.id.feed_stream_recycler_view,
R.id.card_contents);
......
......@@ -57,8 +57,8 @@ public class SuggestionTileController extends ElementController {
}
private static final IUi2Locator LOCATOR_TILE_TITLES =
Ui2Locators.withPath(Ui2Locators.withResEntries(R.id.tile_grid_layout),
Ui2Locators.withResEntries(R.id.tile_view_title));
Ui2Locators.withPath(Ui2Locators.withAnyResEntry(R.id.tile_grid_layout),
Ui2Locators.withAnyResEntry(R.id.tile_view_title));
private static final IUi2Locator LOCATOR_TILE_TITLE_TEXT = Ui2Locators.withTextRegex(".+");
private static final SuggestionTileController sInstance = new SuggestionTileController();
......
......@@ -20,16 +20,16 @@ import java.util.regex.Pattern;
public class TabSwitcherController extends PageController {
private static final Pattern PATTERN_NUMBER_OF_OPEN_TABS = Pattern.compile("^(\\d+) .*");
private static final IUi2Locator LOCATOR_CLOSE_ALL_TABS =
Ui2Locators.withResEntries(R.id.close_all_tabs_button);
Ui2Locators.withAnyResEntry(R.id.close_all_tabs_button);
private static final IUi2Locator LOCATOR_NEW_TAB =
Ui2Locators.withResEntries(R.id.tab_switcher_new_tab_button, R.id.new_tab_button);
private static final IUi2Locator LOCATOR_TAB_SWITCHER_BUTTON = Ui2Locators.withResEntries(
Ui2Locators.withAnyResEntry(R.id.tab_switcher_new_tab_button, R.id.new_tab_button);
private static final IUi2Locator LOCATOR_TAB_SWITCHER_BUTTON = Ui2Locators.withAnyResEntry(
R.id.tab_switcher_button, R.id.tab_switcher_mode_tab_switcher_button);
private static final IUi2Locator LOCATOR_MENU = Ui2Locators.withResEntries(R.id.menu_button);
private static final IUi2Locator LOCATOR_MENU = Ui2Locators.withAnyResEntry(R.id.menu_button);
private static final TabSwitcherController sInstance = new TabSwitcherController();
static public TabSwitcherController getInstance() {
public static TabSwitcherController getInstance() {
return sInstance;
}
......
......@@ -14,23 +14,27 @@ import org.chromium.chrome.test.pagecontroller.utils.Ui2Locators;
*/
public class TabSwitcherMenuController extends PageController {
private static final IUi2Locator LOCATOR_MENU_BOX =
Ui2Locators.withResEntries(R.id.app_menu_list);
Ui2Locators.withAnyResEntry(R.id.app_menu_list);
// TODO(aluo): Add resource ids for menus, using text will break when switching languages
private static final IUi2Locator LOCATOR_MENU =
Ui2Locators.withPath(LOCATOR_MENU_BOX, Ui2Locators.withText("Close all tabs"));
private static final IUi2Locator LOCATOR_NEW_TAB =
Ui2Locators.withResEntriesByIndex(0, R.id.menu_item_text);
Ui2Locators.withPath(Ui2Locators.withAnyResEntry(R.id.menu_item_text),
Ui2Locators.withTextString(R.string.button_new_tab));
private static final IUi2Locator LOCATOR_NEW_INCOGNITO_TAB =
Ui2Locators.withResEntriesByIndex(1, R.id.menu_item_text);
Ui2Locators.withPath(Ui2Locators.withAnyResEntry(R.id.menu_item_text),
Ui2Locators.withTextString(R.string.button_new_incognito_tab));
private static final IUi2Locator LOCATOR_CLOSE_ALL_TABS =
Ui2Locators.withResEntriesByIndex(2, R.id.menu_item_text);
Ui2Locators.withPath(Ui2Locators.withAnyResEntry(R.id.menu_item_text),
Ui2Locators.withTextString(R.string.menu_close_all_tabs));
private static final IUi2Locator LOCATOR_SETTINGS =
Ui2Locators.withResEntriesByIndex(3, R.id.menu_item_text);
Ui2Locators.withPath(Ui2Locators.withAnyResEntry(R.id.menu_item_text),
Ui2Locators.withTextString(R.string.menu_preferences));
static private final TabSwitcherMenuController sInstance = new TabSwitcherMenuController();
static public TabSwitcherMenuController getInstance() {
private static final TabSwitcherMenuController sInstance = new TabSwitcherMenuController();
public static TabSwitcherMenuController getInstance() {
return sInstance;
}
......
......@@ -18,12 +18,12 @@ import org.chromium.chrome.test.pagecontroller.utils.UiLocatorHelper;
public class UrlPage extends PageController {
private static final long PAGE_LOAD_TIMEOUT = 10000L;
private static final IUi2Locator LOCATOR_WEB_VIEW =
Ui2Locators.withPath(Ui2Locators.withResEntries(R.id.content),
Ui2Locators.withPath(Ui2Locators.withAnyResEntry(R.id.content),
Ui2Locators.withClassRegex("android\\.webkit\\.WebView"));
private static final IUi2Locator LOCATOR_URL_BAR = Ui2Locators.withResEntries(R.id.url_bar);
private static final IUi2Locator LOCATOR_URL_BAR = Ui2Locators.withAnyResEntry(R.id.url_bar);
private static final IUi2Locator LOCATOR_TAB_SWITCHER =
Ui2Locators.withResEntries(R.id.tab_switcher_button);
private static final IUi2Locator LOCATOR_MENU = Ui2Locators.withResEntries(R.id.menu_button);
Ui2Locators.withAnyResEntry(R.id.tab_switcher_button);
private static final IUi2Locator LOCATOR_MENU = Ui2Locators.withAnyResEntry(R.id.menu_button);
private static final UrlPage sInstance = new UrlPage();
private UrlPage() {}
......
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package org.chromium.chrome.test.pagecontroller.utils;
import android.support.test.uiautomator.UiDevice;
import android.support.test.uiautomator.UiObject2;
import androidx.annotation.NonNull;
import java.util.List;
/**
* Locate the ith node in the nodes found by an IUi2Locator.
*/
class IndexUi2Locator implements IUi2Locator {
private final IUi2Locator mLocator;
private final int mIndex;
/**
* Locates the ith node(s) matching the locator.
*
* @param index Value of i.
* @param locator First locator in the chain.
*/
public IndexUi2Locator(int index, @NonNull IUi2Locator locator) {
mIndex = index;
mLocator = locator;
}
@Override
public UiObject2 locateOne(UiDevice device) {
List<UiObject2> candidates = mLocator.locateAll(device);
return Utils.nullableGet(candidates, mIndex);
}
@Override
public UiObject2 locateOne(UiObject2 root) {
List<UiObject2> candidates = mLocator.locateAll(root);
return Utils.nullableGet(candidates, mIndex);
}
@Override
public List<UiObject2> locateAll(UiDevice device) {
return Utils.nullableIntoList(locateOne(device));
}
@Override
public List<UiObject2> locateAll(UiObject2 root) {
return Utils.nullableIntoList(locateOne(root));
}
@Override
public String toString() {
return "IndexUi2Locator{"
+ "mLocator=" + mLocator + ", index=" + mIndex + "}";
}
}
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package org.chromium.chrome.test.pagecontroller.utils;
import static org.mockito.Mockito.when;
import static org.chromium.chrome.test.pagecontroller.utils.TestUtils.assertLocatorResults;
import android.support.test.uiautomator.UiDevice;
import android.support.test.uiautomator.UiObject2;
import org.junit.Before;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.MethodSorters;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.annotation.Config;
import org.chromium.base.test.BaseRobolectricTestRunner;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* Tests for IndexUi2Locator
*/
@RunWith(BaseRobolectricTestRunner.class)
@Config(manifest = Config.NONE)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class IndexUi2LocatorTest {
@Mock
private IUi2Locator mLocator0;
@Mock
private UiObject2 mResult0;
@Mock
private UiObject2 mResult1;
@Mock
private List<UiObject2> mLocatorResults0;
@Mock
private List<UiObject2> mLocatorResults1;
@Mock
private UiObject2 mRoot;
@Mock
private UiDevice mDevice;
private List<UiObject2> mLocatorResults;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mLocatorResults0 = Collections.singletonList(mResult0);
mLocatorResults1 = Collections.singletonList(mResult1);
mLocatorResults = new ArrayList<>();
mLocatorResults.add(mResult0);
mLocatorResults.add(mResult1);
when(mLocator0.locateAll(mRoot)).thenReturn(mLocatorResults);
when(mLocator0.locateAll(mDevice)).thenReturn(mLocatorResults);
}
@Test
public void locateFirst() {
IndexUi2Locator locator = new IndexUi2Locator(0, mLocator0);
assertLocatorResults(mDevice, mRoot, locator, mResult0, mLocatorResults0);
}
@Test
public void locateSecond() {
IndexUi2Locator locator = new IndexUi2Locator(1, mLocator0);
assertLocatorResults(mDevice, mRoot, locator, mResult1, mLocatorResults1);
}
}
......@@ -12,9 +12,11 @@ import static org.chromium.chrome.test.pagecontroller.utils.TestUtils.assertLoca
import static org.chromium.chrome.test.pagecontroller.utils.TestUtils.matchesByDepth;
import static org.chromium.chrome.test.pagecontroller.utils.TestUtils.matchesByField;
import android.content.res.Resources;
import android.support.test.uiautomator.UiDevice;
import android.support.test.uiautomator.UiObject2;
import org.junit.After;
import org.junit.Before;
import org.junit.FixMethodOrder;
import org.junit.Test;
......@@ -41,6 +43,9 @@ public class Ui2LocatorsTest {
@Mock
UiDevice mDevice;
@Mock
Resources mResources;
List<UiObject2> mRootAsList, mChild0And1, mChild1AsList, mGrandchildren, mGrandchild1AsList;
@Mock
......@@ -75,10 +80,15 @@ public class Ui2LocatorsTest {
when(mChild1.getChildren()).thenReturn(Collections.emptyList());
}
@After
public void tearDown() {
Ui2Locators.setResources(null);
}
@Test
public void withChildIndex() {
Pattern p = Pattern.compile("name");
stuckMocksWithPattern(p, "mRes");
stubMocksWithPattern(p, "mRes");
IUi2Locator locator = Ui2Locators.withChildIndex(0, 1);
assertEquals(mChild1, locator.locateOne(mDevice));
assertEquals(mChild1AsList, locator.locateAll(mDevice));
......@@ -92,37 +102,33 @@ public class Ui2LocatorsTest {
assertLocatorResults(mDevice, mRoot, locator, mGrandchild0, mGrandchildren);
}
private void stuckMocksWithPattern(Pattern p, String fieldName) {
when(mDevice.findObjects(argThat(matchesByField(p, fieldName)))).thenReturn(mChild0And1);
when(mRoot.findObjects(argThat(matchesByField(p, fieldName)))).thenReturn(mChild0And1);
when(mDevice.findObject(argThat(matchesByField(p, fieldName)))).thenReturn(mChild0);
when(mRoot.findObject(argThat(matchesByField(p, fieldName)))).thenReturn(mChild0);
}
private void assertDefaultResults(IUi2Locator locator) {
assertLocatorResults(mDevice, mRoot, locator, mChild0, mChild0And1);
}
@Test
public void withResIdRegex() {
Pattern p = Pattern.compile("^.*:id/a.*b$");
stuckMocksWithPattern(p, "mRes");
stubMocksWithPattern(p, "mRes");
IUi2Locator locator = Ui2Locators.withResIdRegex("a.*b");
assertDefaultResults(locator);
}
@Test
public void withResIds() {
public void withAnyResId() {
Pattern p = Pattern.compile("^.*:id/(a|b)$");
stuckMocksWithPattern(p, "mRes");
IUi2Locator locator = Ui2Locators.withResIds("a", "b");
stubMocksWithPattern(p, "mRes");
IUi2Locator locator = Ui2Locators.withAnyResId("a", "b");
assertDefaultResults(locator);
}
@Test
public void withAnyResEntry() {
stubMocksWithResEntry(123, "someEntry");
IUi2Locator locator = Ui2Locators.withAnyResEntry(123);
assertDefaultResults(locator);
}
@Test
public void withResName() {
Pattern p = Pattern.compile(Pattern.quote("name"));
stuckMocksWithPattern(p, "mRes");
stubMocksWithPattern(p, "mRes");
IUi2Locator locator = Ui2Locators.withResName("name");
assertDefaultResults(locator);
}
......@@ -130,31 +136,30 @@ public class Ui2LocatorsTest {
@Test
public void withResNameRegex() {
Pattern p = Pattern.compile(".*name");
stuckMocksWithPattern(p, "mRes");
stubMocksWithPattern(p, "mRes");
IUi2Locator locator = Ui2Locators.withResNameRegex(".*name");
assertDefaultResults(locator);
}
@Test
public void withResNameRegexByIndex() {
Pattern p = Pattern.compile(".*name");
stuckMocksWithPattern(p, "mRes");
IUi2Locator locator = Ui2Locators.withResNameRegexByIndex(".*name", 1);
assertLocatorResults(mDevice, mRoot, locator, mChild1, mChild1AsList);
}
@Test
public void withContentDesc() {
Pattern p = Pattern.compile(Pattern.quote("desc"));
stuckMocksWithPattern(p, "mDesc");
stubMocksWithPattern(p, "mDesc");
IUi2Locator locator = Ui2Locators.withContentDesc("desc");
assertDefaultResults(locator);
}
@Test
public void withContentDescString() {
stubMocksWithDescString(123, "someDesc");
IUi2Locator locator = Ui2Locators.withContentDescString(123);
assertDefaultResults(locator);
}
@Test
public void withText() {
Pattern p = Pattern.compile(Pattern.quote("text"));
stuckMocksWithPattern(p, "mText");
stubMocksWithPattern(p, "mText");
IUi2Locator locator = Ui2Locators.withText("text");
assertDefaultResults(locator);
}
......@@ -162,7 +167,7 @@ public class Ui2LocatorsTest {
@Test
public void withTextRegex() {
Pattern p = Pattern.compile(".*text");
stuckMocksWithPattern(p, "mText");
stubMocksWithPattern(p, "mText");
IUi2Locator locator = Ui2Locators.withTextRegex(".*text");
assertDefaultResults(locator);
}
......@@ -170,15 +175,22 @@ public class Ui2LocatorsTest {
@Test
public void withTextContaining() {
Pattern p = Pattern.compile("^.*" + Pattern.quote("text") + ".*$");
stuckMocksWithPattern(p, "mText");
stubMocksWithPattern(p, "mText");
IUi2Locator locator = Ui2Locators.withTextContaining("text");
assertDefaultResults(locator);
}
@Test
public void withTextString() {
stubMocksWithTextString(123, "someString");
IUi2Locator locator = Ui2Locators.withTextString(123);
assertDefaultResults(locator);
}
@Test
public void withClassRegex() {
Pattern p = Pattern.compile(".*class");
stuckMocksWithPattern(p, "mClazz");
stubMocksWithPattern(p, "mClazz");
IUi2Locator locator = Ui2Locators.withClassRegex(".*class");
assertDefaultResults(locator);
}
......@@ -186,7 +198,7 @@ public class Ui2LocatorsTest {
@Test
public void withPath() {
Pattern p = Pattern.compile(".*class");
stuckMocksWithPattern(p, "mClazz");
stubMocksWithPattern(p, "mClazz");
IUi2Locator locator0 = Ui2Locators.withClassRegex(".*class");
IUi2Locator locator1 = Ui2Locators.withChildIndex(1);
IUi2Locator locator = Ui2Locators.withPath(locator0, locator1);
......@@ -196,8 +208,40 @@ public class Ui2LocatorsTest {
@Test
public void withPackageName() {
Pattern p = Pattern.compile(Pattern.quote("package"));
stuckMocksWithPattern(p, "mPkg");
stubMocksWithPattern(p, "mPkg");
IUi2Locator locator = Ui2Locators.withPackageName("package");
assertDefaultResults(locator);
}
private void stubMocksWithPattern(Pattern p, String fieldName) {
when(mDevice.findObjects(argThat(matchesByField(p, fieldName)))).thenReturn(mChild0And1);
when(mRoot.findObjects(argThat(matchesByField(p, fieldName)))).thenReturn(mChild0And1);
when(mDevice.findObject(argThat(matchesByField(p, fieldName)))).thenReturn(mChild0);
when(mRoot.findObject(argThat(matchesByField(p, fieldName)))).thenReturn(mChild0);
}
private void stubMocksWithResEntry(int stringId, String stringValue) {
when(mResources.getResourceEntryName(stringId)).thenReturn(stringValue);
Ui2Locators.setResources(mResources);
Pattern p = Pattern.compile("^.*:id/(" + stringValue + ")$");
stubMocksWithPattern(p, "mRes");
}
private void stubMocksWithDescString(int stringId, String stringValue) {
when(mResources.getString(stringId)).thenReturn(stringValue);
Ui2Locators.setResources(mResources);
Pattern p = Pattern.compile(Pattern.quote(stringValue));
stubMocksWithPattern(p, "mDesc");
}
private void stubMocksWithTextString(int stringId, String stringValue) {
when(mResources.getString(stringId)).thenReturn(stringValue);
Ui2Locators.setResources(mResources);
Pattern p = Pattern.compile(Pattern.quote(stringValue));
stubMocksWithPattern(p, "mText");
}
private void assertDefaultResults(IUi2Locator locator) {
assertLocatorResults(mDevice, mRoot, locator, mChild0, mChild0And1);
}
}
......@@ -325,7 +325,7 @@ public class UiLocatorHelper {
* Delegate to be used with getCustomElements.
* @param <T> The type of the element.
*/
static public interface CustomElementMaker<T> {
public static interface CustomElementMaker<T> {
/**
* Should construct an element given a node.
* @param root The input node.
......@@ -403,10 +403,11 @@ public class UiLocatorHelper {
private <T> T getOneElement(IUi2Locator locator, ElementConverter<T> converter) {
List<T> all = getAllElements(locator, converter);
if (all.size() > 0)
if (all.size() > 0) {
return all.get(0);
else
} else {
return null;
}
}
private <T> T getOneElementImmediate(
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment