Commit ed44674b authored by John Budorick's avatar John Budorick Committed by Commit Bot

Revert "Add withTextString, withDescString locators, and some refactorings."

This reverts commit 850e8d2b.

Reason for revert: https://ci.chromium.org/p/chromium/builders/ci/android-archive-rel/5470

Original change's description:
> 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: John Budorick <jbudorick@chromium.org>
> Reviewed-by: Sky Malice <skym@chromium.org>
> Cr-Commit-Position: refs/heads/master@{#703953}

TBR=skym@chromium.org,jbudorick@chromium.org,aluo@chromium.org,wuwang@google.com

Change-Id: Ib1a7b7ec8bb2ecce0e857a900218054266c41336
No-Presubmit: true
No-Tree-Checks: true
No-Try: true
Bug: 924194
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1848399Reviewed-by: default avatarJohn Budorick <jbudorick@chromium.org>
Commit-Queue: John Budorick <jbudorick@chromium.org>
Cr-Commit-Position: refs/heads/master@{#703970}
parent 3088155f
...@@ -28,7 +28,6 @@ android_library("chrome_java_test_pagecontroller") { ...@@ -28,7 +28,6 @@ android_library("chrome_java_test_pagecontroller") {
"javatests/src/org/chromium/chrome/test/pagecontroller/utils/BySelectorIndexUi2Locator.java", "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/BySelectorUi2Locator.java",
"javatests/src/org/chromium/chrome/test/pagecontroller/utils/ChildIndexUi2Locator.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/IUi2Locator.java",
"javatests/src/org/chromium/chrome/test/pagecontroller/utils/PathUi2Locator.java", "javatests/src/org/chromium/chrome/test/pagecontroller/utils/PathUi2Locator.java",
"javatests/src/org/chromium/chrome/test/pagecontroller/utils/Ui2Locators.java", "javatests/src/org/chromium/chrome/test/pagecontroller/utils/Ui2Locators.java",
...@@ -53,6 +52,8 @@ instrumentation_test_apk("chrome_java_test_pagecontroller_tests") { ...@@ -53,6 +52,8 @@ instrumentation_test_apk("chrome_java_test_pagecontroller_tests") {
apk_name = "ChromePageControllerTests" apk_name = "ChromePageControllerTests"
apk_under_test = "//chrome/android:chrome_public_apk" apk_under_test = "//chrome/android:chrome_public_apk"
android_manifest = "javatests/src/org/chromium/chrome/test/pagecontroller/tests/AndroidManifest.xml" android_manifest = "javatests/src/org/chromium/chrome/test/pagecontroller/tests/AndroidManifest.xml"
target_sdk_version = 28
testonly = true
java_files = [ java_files = [
"javatests/src/org/chromium/chrome/test/pagecontroller/tests/ExampleTest.java", "javatests/src/org/chromium/chrome/test/pagecontroller/tests/ExampleTest.java",
"javatests/src/org/chromium/chrome/test/pagecontroller/tests/FirstRunControllerTest.java", "javatests/src/org/chromium/chrome/test/pagecontroller/tests/FirstRunControllerTest.java",
...@@ -63,6 +64,11 @@ instrumentation_test_apk("chrome_java_test_pagecontroller_tests") { ...@@ -63,6 +64,11 @@ instrumentation_test_apk("chrome_java_test_pagecontroller_tests") {
":chrome_java_test_pagecontroller", ":chrome_java_test_pagecontroller",
"//third_party/junit", "//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") { junit_binary("chrome_java_test_pagecontroller_unit_tests") {
...@@ -71,7 +77,6 @@ junit_binary("chrome_java_test_pagecontroller_unit_tests") { ...@@ -71,7 +77,6 @@ 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/BySelectorIndexUi2LocatorTest.java",
"javatests/src/org/chromium/chrome/test/pagecontroller/utils/BySelectorUi2LocatorTest.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/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/PathUi2LocatorTest.java",
"javatests/src/org/chromium/chrome/test/pagecontroller/utils/TestUtils.java", "javatests/src/org/chromium/chrome/test/pagecontroller/utils/TestUtils.java",
"javatests/src/org/chromium/chrome/test/pagecontroller/utils/Ui2LocatorsTest.java", "javatests/src/org/chromium/chrome/test/pagecontroller/utils/Ui2LocatorsTest.java",
......
...@@ -2,7 +2,5 @@ file://chrome/android/OWNERS ...@@ -2,7 +2,5 @@ file://chrome/android/OWNERS
jbudorick@chromium.org jbudorick@chromium.org
per-file BUILD.gn=*
# COMPONENT: Test>Android # COMPONENT: Test>Android
# OS: Android # OS: Android
...@@ -33,9 +33,9 @@ class CoolNewPageController extends PageController { ...@@ -33,9 +33,9 @@ class CoolNewPageController extends PageController {
// Locators allow the controller to find UI elements on the page // Locators allow the controller to find UI elements on the page
// It is preferred to use Resource Ids to find elements since they are // It is preferred to use Resource Ids to find elements since they are
// stable across minor UI changes. // stable across minor UI changes.
private static final IUi2Locator LOCATOR_COOL_PAGE = Ui2Locators.withAnyResEntry(R.id.cool_page); private static final IUi2Locator LOCATOR_COOL_PAGE = Ui2Locators.withResIds("cool_page");
// Any of the resource ids in the list will result in a match. // Any of the resource ids in the list will result in a match.
private static final IUi2Locator LOCATOR_COOL_BUTTON = Ui2Locators.withAnyResEntry(R.id.cool_button_v1, R.id.cool_button_v2); private static final IUi2Locator LOCATOR_COOL_BUTTON = Ui2Locators.withResIds("cool_button_v1", "cool_button_v2");
public CoolerPageController clickButton() { 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. // [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,16 +8,8 @@ import org.chromium.chrome.test.pagecontroller.utils.UiAutomatorUtils; ...@@ -8,16 +8,8 @@ import org.chromium.chrome.test.pagecontroller.utils.UiAutomatorUtils;
import org.chromium.chrome.test.pagecontroller.utils.UiLocatorHelper; import org.chromium.chrome.test.pagecontroller.utils.UiLocatorHelper;
/** /**
* Base class for any controller that needs to perform UI actions and verify UI * Base class for a Page Controller or Page Element Controller.
* state. * Allows Controllers 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 { public class ElementController {
protected final UiAutomatorUtils mUtils; protected final UiAutomatorUtils mUtils;
......
...@@ -9,24 +9,9 @@ import android.os.RemoteException; ...@@ -9,24 +9,9 @@ import android.os.RemoteException;
import org.chromium.chrome.test.pagecontroller.utils.UiLocationException; import org.chromium.chrome.test.pagecontroller.utils.UiLocationException;
/** /**
* Base class for Page Controllers. * Base class for page controllers.
* * A page controller allows tests to interact with a single page (think Android activity)
* A Page Controller allows tests to interact with a single modal view in the * in the app-under-test.
* 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 abstract class PageController extends ElementController {
public void pressAndroidBackButton() { public void pressAndroidBackButton() {
......
...@@ -14,8 +14,8 @@ import org.chromium.chrome.test.pagecontroller.utils.Ui2Locators; ...@@ -14,8 +14,8 @@ import org.chromium.chrome.test.pagecontroller.utils.Ui2Locators;
*/ */
public class DataSaverController extends PageController { public class DataSaverController extends PageController {
private static final IUi2Locator LOCATOR_DATA_SAVER = private static final IUi2Locator LOCATOR_DATA_SAVER =
Ui2Locators.withAnyResEntry(R.id.enable_data_saver_switch); Ui2Locators.withResEntries(R.id.enable_data_saver_switch);
private static final IUi2Locator LOCATOR_NEXT = Ui2Locators.withAnyResEntry(R.id.next_button); private static final IUi2Locator LOCATOR_NEXT = Ui2Locators.withResEntries(R.id.next_button);
private static final DataSaverController sInstance = new DataSaverController(); private static final DataSaverController sInstance = new DataSaverController();
private DataSaverController() {} private DataSaverController() {}
......
...@@ -13,10 +13,10 @@ import org.chromium.chrome.test.pagecontroller.utils.Ui2Locators; ...@@ -13,10 +13,10 @@ import org.chromium.chrome.test.pagecontroller.utils.Ui2Locators;
* Sync Dialog (part of First Run Experience) Page Controller. * Sync Dialog (part of First Run Experience) Page Controller.
*/ */
public class SyncController extends PageController { public class SyncController extends PageController {
private static final IUi2Locator LOCATOR_SYNC_CONTROLLER = private final static IUi2Locator LOCATOR_SYNC_CONTROLLER =
Ui2Locators.withAnyResEntry(R.id.signin_sync_title); Ui2Locators.withResEntries(R.id.signin_sync_title);
private static final IUi2Locator LOCATOR_NO_THANKS = private final static IUi2Locator LOCATOR_NO_THANKS =
Ui2Locators.withAnyResEntry(R.id.negative_button); Ui2Locators.withResEntries(R.id.negative_button);
private static final SyncController sInstance = new SyncController(); private static final SyncController sInstance = new SyncController();
private SyncController() {} private SyncController() {}
......
...@@ -13,12 +13,10 @@ import org.chromium.chrome.test.pagecontroller.utils.Ui2Locators; ...@@ -13,12 +13,10 @@ import org.chromium.chrome.test.pagecontroller.utils.Ui2Locators;
* TOS Dialog (part of the First Run Experience) Page Controller. * TOS Dialog (part of the First Run Experience) Page Controller.
*/ */
public class TOSController extends PageController { public class TOSController extends PageController {
private static final IUi2Locator LOCATOR_TOS = private static final IUi2Locator LOCATOR_TOS = Ui2Locators.withResEntries(R.id.tos_and_privacy);
Ui2Locators.withAnyResEntry(R.id.tos_and_privacy);
private static final IUi2Locator LOCATOR_SEND_REPORT_CHECKBOX = private static final IUi2Locator LOCATOR_SEND_REPORT_CHECKBOX =
Ui2Locators.withAnyResEntry(R.id.send_report_checkbox); Ui2Locators.withResEntries(R.id.send_report_checkbox);
private static final IUi2Locator LOCATOR_ACCEPT = private static final IUi2Locator LOCATOR_ACCEPT = Ui2Locators.withResEntries(R.id.terms_accept);
Ui2Locators.withAnyResEntry(R.id.terms_accept);
private static final TOSController sInstance = new TOSController(); private static final TOSController sInstance = new TOSController();
private TOSController() {} private TOSController() {}
......
...@@ -15,7 +15,7 @@ public class DownloadNotificationController extends PageController { ...@@ -15,7 +15,7 @@ public class DownloadNotificationController extends PageController {
private static final IUi2Locator LOCATOR_DOWNLOAD_NOTIFICATION = private static final IUi2Locator LOCATOR_DOWNLOAD_NOTIFICATION =
Ui2Locators.withTextContaining("needs storage access to download files"); Ui2Locators.withTextContaining("needs storage access to download files");
private static final IUi2Locator LOCATOR_CONTINUE = private static final IUi2Locator LOCATOR_CONTINUE =
Ui2Locators.withAnyResEntry(android.R.id.button1); Ui2Locators.withResEntries(android.R.id.button1);
private static final DownloadNotificationController sInstance = private static final DownloadNotificationController sInstance =
new DownloadNotificationController(); new DownloadNotificationController();
......
...@@ -17,25 +17,20 @@ import org.chromium.chrome.test.pagecontroller.utils.Ui2Locators; ...@@ -17,25 +17,20 @@ import org.chromium.chrome.test.pagecontroller.utils.Ui2Locators;
public class ArticleActionsMenu extends PageController { public class ArticleActionsMenu extends PageController {
private static final IUi2Locator LOCATOR_MENU = Ui2Locators.withClassRegex(".*ListView"); private static final IUi2Locator LOCATOR_MENU = Ui2Locators.withClassRegex(".*ListView");
private static final IUi2Locator LOCATOR_OPEN_NEW_TAB = Ui2Locators.withIndex(0, private static final IUi2Locator LOCATOR_OPEN_NEW_TAB = Ui2Locators.withResEntriesByIndex(
Ui2Locators.withAnyResEntry( 0, org.chromium.chrome.R.id.title, R.id.feed_simple_list_item);
org.chromium.chrome.R.id.title, R.id.feed_simple_list_item)); private static final IUi2Locator LOCATOR_OPEN_INCOGNITO = Ui2Locators.withResEntriesByIndex(
private static final IUi2Locator LOCATOR_OPEN_INCOGNITO = Ui2Locators.withIndex(1, 1, org.chromium.chrome.R.id.title, R.id.feed_simple_list_item);
Ui2Locators.withAnyResEntry( private static final IUi2Locator LOCATOR_DOWNLOAD_LINK = Ui2Locators.withResEntriesByIndex(
org.chromium.chrome.R.id.title, R.id.feed_simple_list_item)); 2, org.chromium.chrome.R.id.title, R.id.feed_simple_list_item);
private static final IUi2Locator LOCATOR_DOWNLOAD_LINK = Ui2Locators.withIndex(2, private static final IUi2Locator LOCATOR_REMOVE = Ui2Locators.withResEntriesByIndex(
Ui2Locators.withAnyResEntry( 3, org.chromium.chrome.R.id.title, R.id.feed_simple_list_item);
org.chromium.chrome.R.id.title, R.id.feed_simple_list_item)); private static final IUi2Locator LOCATOR_LEARN_MORE = Ui2Locators.withResEntriesByIndex(
private static final IUi2Locator LOCATOR_REMOVE = Ui2Locators.withIndex(3, 4, org.chromium.chrome.R.id.title, R.id.feed_simple_list_item);
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));
private static ArticleActionsMenu sInstance = new ArticleActionsMenu(); static private ArticleActionsMenu sInstance = new ArticleActionsMenu();
private ArticleActionsMenu() {} private ArticleActionsMenu() {}
public static ArticleActionsMenu getInstance() { static public ArticleActionsMenu getInstance() {
return sInstance; return sInstance;
} }
......
...@@ -32,7 +32,7 @@ public class ArticleCardController extends ElementController { ...@@ -32,7 +32,7 @@ public class ArticleCardController extends ElementController {
* Represents a single article, can be used by the NewTabPageController * Represents a single article, can be used by the NewTabPageController
* to perform actions. * to perform actions.
*/ */
public static class Info { static public class Info {
private final String mHeadline, mPublisher, mAge; private final String mHeadline, mPublisher, mAge;
private final ImplementationType mImplType; private final ImplementationType mImplType;
...@@ -83,9 +83,9 @@ public class ArticleCardController extends ElementController { ...@@ -83,9 +83,9 @@ public class ArticleCardController extends ElementController {
} }
} }
private abstract static class ArticleImpl { private static abstract class ArticleImpl {
public abstract List<Info> parseScreenForArticles(UiLocatorHelper locatorHelper); abstract public List<Info> parseScreenForArticles(UiLocatorHelper locatorHelper);
public abstract IUi2Locator getLocator(Info cardInfo); abstract public IUi2Locator getLocator(Info cardInfo);
protected List<Info> parseScreenForArticles(final UiLocatorHelper locatorHelper, protected List<Info> parseScreenForArticles(final UiLocatorHelper locatorHelper,
final ImplementationType implementationType, final IUi2Locator cardsLocator, final ImplementationType implementationType, final IUi2Locator cardsLocator,
final IUi2Locator headlineLocator, final IUi2Locator publisherLocator, final IUi2Locator headlineLocator, final IUi2Locator publisherLocator,
...@@ -104,16 +104,15 @@ public class ArticleCardController extends ElementController { ...@@ -104,16 +104,15 @@ public class ArticleCardController extends ElementController {
} else if (isLastAttempt) { } else if (isLastAttempt) {
return null; return null;
} else { } else {
if (headline == null) { if (headline == null)
throw new UiLocationException( throw new UiLocationException(
"Headline not found.", headlineLocator, articleCard); "Headline not found.", headlineLocator, articleCard);
} else if (publisher == null) { else if (publisher == null)
throw new UiLocationException( throw new UiLocationException(
"Publisher not found.", publisherLocator, articleCard); "Publisher not found.", publisherLocator, articleCard);
} else { else
throw new UiLocationException( throw new UiLocationException(
"Age not found.", ageLocator, articleCard); "Age not found.", ageLocator, articleCard);
}
} }
} }
}); });
...@@ -123,11 +122,11 @@ public class ArticleCardController extends ElementController { ...@@ -123,11 +122,11 @@ public class ArticleCardController extends ElementController {
private static class FeedArticleImpl extends ArticleImpl { private static class FeedArticleImpl extends ArticleImpl {
private static final IUi2Locator LOCATOR_NON_EMPTY_STRING = withTextRegex(".+"); private static final IUi2Locator LOCATOR_NON_EMPTY_STRING = withTextRegex(".+");
private static final IUi2Locator LOCATOR_CARDS = Ui2Locators.withPath( private static final IUi2Locator LOCATOR_CARDS = Ui2Locators.withPath(
Ui2Locators.withAnyResEntry(R.id.content), Ui2Locators.withResEntries(R.id.content),
Ui2Locators.withAnyResEntry(com.google.android.libraries.feed.basicstream.R.id Ui2Locators.withResEntries(com.google.android.libraries.feed.basicstream.R.id
.feed_stream_recycler_view), .feed_stream_recycler_view),
Ui2Locators.withAnyResEntry(com.google.android.libraries.feed.basicstream.internal Ui2Locators.withResEntries(com.google.android.libraries.feed.basicstream.internal
.viewholders.R.id.feed_content_card)); .viewholders.R.id.feed_content_card));
private static final IUi2Locator LOCATOR_HEADLINE = private static final IUi2Locator LOCATOR_HEADLINE =
Ui2Locators.withPath(Ui2Locators.withChildIndex(0, 6), LOCATOR_NON_EMPTY_STRING); Ui2Locators.withPath(Ui2Locators.withChildIndex(0, 6), LOCATOR_NON_EMPTY_STRING);
private static final IUi2Locator LOCATOR_PUBLISHER = private static final IUi2Locator LOCATOR_PUBLISHER =
...@@ -149,14 +148,13 @@ public class ArticleCardController extends ElementController { ...@@ -149,14 +148,13 @@ public class ArticleCardController extends ElementController {
private static class ZineArticleImpl extends ArticleImpl { private static class ZineArticleImpl extends ArticleImpl {
private static final IUi2Locator LOCATOR_CARDS = private static final IUi2Locator LOCATOR_CARDS =
Ui2Locators.withPath(Ui2Locators.withAnyResEntry(R.id.content), Ui2Locators.withPath(Ui2Locators.withResEntries(R.id.content),
Ui2Locators.withAnyResEntry(R.id.card_contents)); Ui2Locators.withResEntries(R.id.card_contents));
private static final IUi2Locator LOCATOR_HEADLINE = private static final IUi2Locator LOCATOR_HEADLINE =
Ui2Locators.withAnyResEntry(R.id.article_headline); Ui2Locators.withResEntries(R.id.article_headline);
private static final IUi2Locator LOCATOR_PUBLISHER = private static final IUi2Locator LOCATOR_PUBLISHER =
Ui2Locators.withAnyResEntry(R.id.article_publisher); Ui2Locators.withResEntries(R.id.article_publisher);
private static final IUi2Locator LOCATOR_AGE = private static final IUi2Locator LOCATOR_AGE = Ui2Locators.withResEntries(R.id.article_age);
Ui2Locators.withAnyResEntry(R.id.article_age);
@Override @Override
public List<Info> parseScreenForArticles(UiLocatorHelper locatorHelper) { public List<Info> parseScreenForArticles(UiLocatorHelper locatorHelper) {
return parseScreenForArticles(locatorHelper, ImplementationType.ZINE, LOCATOR_CARDS, return parseScreenForArticles(locatorHelper, ImplementationType.ZINE, LOCATOR_CARDS,
......
...@@ -14,20 +14,18 @@ import org.chromium.chrome.test.pagecontroller.utils.Ui2Locators; ...@@ -14,20 +14,18 @@ import org.chromium.chrome.test.pagecontroller.utils.Ui2Locators;
*/ */
public class ChromeMenu extends PageController { public class ChromeMenu extends PageController {
private static final IUi2Locator LOCATOR_CHROME_MENU_BOX = private static final IUi2Locator LOCATOR_CHROME_MENU_BOX =
Ui2Locators.withAnyResEntry(R.id.app_menu_list); Ui2Locators.withResEntries(R.id.app_menu_list);
private static final IUi2Locator LOCATOR_CHROME_MENU = Ui2Locators.withPath( private static final IUi2Locator LOCATOR_CHROME_MENU = Ui2Locators.withPath(
LOCATOR_CHROME_MENU_BOX, Ui2Locators.withAnyResEntry(R.id.button_five)); LOCATOR_CHROME_MENU_BOX, Ui2Locators.withResEntries(R.id.button_five));
private static final IUi2Locator LOCATOR_NEW_TAB = private static final IUi2Locator LOCATOR_NEW_TAB =
Ui2Locators.withPath(Ui2Locators.withAnyResEntry(R.id.menu_item_text), Ui2Locators.withResEntriesByIndex(0, R.id.menu_item_text);
Ui2Locators.withTextString(R.string.menu_new_tab));
private static final IUi2Locator LOCATOR_NEW_INCOGNITO_TAB = private static final IUi2Locator LOCATOR_NEW_INCOGNITO_TAB =
Ui2Locators.withPath(Ui2Locators.withAnyResEntry(R.id.menu_item_text), Ui2Locators.withResEntriesByIndex(1, R.id.menu_item_text);
Ui2Locators.withTextString(R.string.menu_new_incognito_tab));
private static final ChromeMenu sInstance = new ChromeMenu(); private static final ChromeMenu sInstance = new ChromeMenu();
private ChromeMenu() {} private ChromeMenu() {}
public static ChromeMenu getInstance() { static public ChromeMenu getInstance() {
return sInstance; return sInstance;
} }
......
...@@ -14,7 +14,7 @@ import org.chromium.chrome.test.pagecontroller.utils.Ui2Locators; ...@@ -14,7 +14,7 @@ import org.chromium.chrome.test.pagecontroller.utils.Ui2Locators;
*/ */
public class IncognitoNewTabPageController extends PageController { public class IncognitoNewTabPageController extends PageController {
private static final IUi2Locator LOCATOR_INCOGNITO_PAGE = private static final IUi2Locator LOCATOR_INCOGNITO_PAGE =
Ui2Locators.withAnyResEntry(R.id.new_tab_incognito_container); Ui2Locators.withResEntries(R.id.new_tab_incognito_container);
private static final IncognitoNewTabPageController sInstance = private static final IncognitoNewTabPageController sInstance =
new IncognitoNewTabPageController(); new IncognitoNewTabPageController();
......
...@@ -30,32 +30,31 @@ public class NewTabPageController extends PageController { ...@@ -30,32 +30,31 @@ public class NewTabPageController extends PageController {
private static final String REGEX_TEXT_HEADER_STATUS = "^Hide|Show$"; private static final String REGEX_TEXT_HEADER_STATUS = "^Hide|Show$";
private static final IUi2Locator LOCATOR_SEARCH_BOX_TEXT = private static final IUi2Locator LOCATOR_SEARCH_BOX_TEXT =
Ui2Locators.withAnyResEntry(R.id.search_box_text); Ui2Locators.withResEntries(R.id.search_box_text);
private static final IUi2Locator LOCATOR_URL_BAR = Ui2Locators.withAnyResEntry(R.id.url_bar); private static final IUi2Locator LOCATOR_URL_BAR = Ui2Locators.withResEntries(R.id.url_bar);
private static final IUi2Locator LOCATOR_SEARCH_PROVIDER_LOGO = private static final IUi2Locator LOCATOR_SEARCH_PROVIDER_LOGO =
Ui2Locators.withAnyResEntry(R.id.search_provider_logo); Ui2Locators.withResEntries(R.id.search_provider_logo);
private static final IUi2Locator LOCATOR_MORE_BUTTON = private static final IUi2Locator LOCATOR_MORE_BUTTON =
Ui2Locators.withAnyResEntry(R.id.action_button); Ui2Locators.withResEntries(R.id.action_button);
private static final IUi2Locator LOCATOR_MENU_BUTTON = private static final IUi2Locator LOCATOR_MENU_BUTTON =
Ui2Locators.withAnyResEntry(R.id.menu_button); Ui2Locators.withResEntries(R.id.menu_button);
private static final IUi2Locator LOCATOR_BOTTOM_OF_PAGE = private static final IUi2Locator LOCATOR_BOTTOM_OF_PAGE =
Ui2Locators.withAnyResEntry(R.id.progress_indicator, R.id.action_button); Ui2Locators.withResEntries(R.id.progress_indicator, R.id.action_button);
private static final IUi2Locator LOCATOR_FEED_STREAM_RECYCLER_VIEW = private static final IUi2Locator LOCATOR_FEED_STREAM_RECYCLER_VIEW = Ui2Locators.withResEntries(
Ui2Locators.withAnyResEntry( com.google.android.libraries.feed.basicstream.R.id.feed_stream_recycler_view);
com.google.android.libraries.feed.basicstream.R.id.feed_stream_recycler_view);
private static final IUi2Locator LOCATOR_HEADER_STATUS = private static final IUi2Locator LOCATOR_HEADER_STATUS =
Ui2Locators.withPath(Ui2Locators.withAnyResEntry(R.id.header_status), Ui2Locators.withPath(Ui2Locators.withResEntries(R.id.header_status),
Ui2Locators.withTextRegex(REGEX_TEXT_HEADER_STATUS)); Ui2Locators.withTextRegex(REGEX_TEXT_HEADER_STATUS));
private static final IUi2Locator LOCATOR_INFO_BAR_MESSAGE = private static final IUi2Locator LOCATOR_INFO_BAR_MESSAGE =
Ui2Locators.withAnyResEntry(R.id.infobar_message, R.id.snackbar_message); Ui2Locators.withResEntries(R.id.infobar_message, R.id.snackbar_message);
private static final IUi2Locator LOCATOR_INFO_BAR_CLOSE = private static final IUi2Locator LOCATOR_INFO_BAR_CLOSE =
Ui2Locators.withAnyResEntry(R.id.infobar_close_button); Ui2Locators.withResEntries(R.id.infobar_close_button);
private static final IUi2Locator LOCATOR_TAB_SWITCHER = private static final IUi2Locator LOCATOR_TAB_SWITCHER =
Ui2Locators.withAnyResEntry(R.id.tab_switcher_button); Ui2Locators.withResEntries(R.id.tab_switcher_button);
private static final IUi2Locator LOCATOR_NEW_TAB_PAGE = private static final IUi2Locator LOCATOR_NEW_TAB_PAGE =
Ui2Locators.withAnyResEntry(R.id.ntp_content, Ui2Locators.withResEntries(R.id.ntp_content,
com.google.android.libraries.feed.basicstream.R.id.feed_stream_recycler_view, com.google.android.libraries.feed.basicstream.R.id.feed_stream_recycler_view,
R.id.card_contents); R.id.card_contents);
......
...@@ -57,8 +57,8 @@ public class SuggestionTileController extends ElementController { ...@@ -57,8 +57,8 @@ public class SuggestionTileController extends ElementController {
} }
private static final IUi2Locator LOCATOR_TILE_TITLES = private static final IUi2Locator LOCATOR_TILE_TITLES =
Ui2Locators.withPath(Ui2Locators.withAnyResEntry(R.id.tile_grid_layout), Ui2Locators.withPath(Ui2Locators.withResEntries(R.id.tile_grid_layout),
Ui2Locators.withAnyResEntry(R.id.tile_view_title)); Ui2Locators.withResEntries(R.id.tile_view_title));
private static final IUi2Locator LOCATOR_TILE_TITLE_TEXT = Ui2Locators.withTextRegex(".+"); private static final IUi2Locator LOCATOR_TILE_TITLE_TEXT = Ui2Locators.withTextRegex(".+");
private static final SuggestionTileController sInstance = new SuggestionTileController(); private static final SuggestionTileController sInstance = new SuggestionTileController();
......
...@@ -20,16 +20,16 @@ import java.util.regex.Pattern; ...@@ -20,16 +20,16 @@ import java.util.regex.Pattern;
public class TabSwitcherController extends PageController { public class TabSwitcherController extends PageController {
private static final Pattern PATTERN_NUMBER_OF_OPEN_TABS = Pattern.compile("^(\\d+) .*"); private static final Pattern PATTERN_NUMBER_OF_OPEN_TABS = Pattern.compile("^(\\d+) .*");
private static final IUi2Locator LOCATOR_CLOSE_ALL_TABS = private static final IUi2Locator LOCATOR_CLOSE_ALL_TABS =
Ui2Locators.withAnyResEntry(R.id.close_all_tabs_button); Ui2Locators.withResEntries(R.id.close_all_tabs_button);
private static final IUi2Locator LOCATOR_NEW_TAB = private static final IUi2Locator LOCATOR_NEW_TAB =
Ui2Locators.withAnyResEntry(R.id.tab_switcher_new_tab_button, R.id.new_tab_button); Ui2Locators.withResEntries(R.id.tab_switcher_new_tab_button, R.id.new_tab_button);
private static final IUi2Locator LOCATOR_TAB_SWITCHER_BUTTON = Ui2Locators.withAnyResEntry( private static final IUi2Locator LOCATOR_TAB_SWITCHER_BUTTON = Ui2Locators.withResEntries(
R.id.tab_switcher_button, R.id.tab_switcher_mode_tab_switcher_button); R.id.tab_switcher_button, R.id.tab_switcher_mode_tab_switcher_button);
private static final IUi2Locator LOCATOR_MENU = Ui2Locators.withAnyResEntry(R.id.menu_button); private static final IUi2Locator LOCATOR_MENU = Ui2Locators.withResEntries(R.id.menu_button);
private static final TabSwitcherController sInstance = new TabSwitcherController(); private static final TabSwitcherController sInstance = new TabSwitcherController();
public static TabSwitcherController getInstance() { static public TabSwitcherController getInstance() {
return sInstance; return sInstance;
} }
......
...@@ -14,27 +14,23 @@ import org.chromium.chrome.test.pagecontroller.utils.Ui2Locators; ...@@ -14,27 +14,23 @@ import org.chromium.chrome.test.pagecontroller.utils.Ui2Locators;
*/ */
public class TabSwitcherMenuController extends PageController { public class TabSwitcherMenuController extends PageController {
private static final IUi2Locator LOCATOR_MENU_BOX = private static final IUi2Locator LOCATOR_MENU_BOX =
Ui2Locators.withAnyResEntry(R.id.app_menu_list); Ui2Locators.withResEntries(R.id.app_menu_list);
// TODO(aluo): Add resource ids for menus, using text will break when switching languages // TODO(aluo): Add resource ids for menus, using text will break when switching languages
private static final IUi2Locator LOCATOR_MENU = private static final IUi2Locator LOCATOR_MENU =
Ui2Locators.withPath(LOCATOR_MENU_BOX, Ui2Locators.withText("Close all tabs")); Ui2Locators.withPath(LOCATOR_MENU_BOX, Ui2Locators.withText("Close all tabs"));
private static final IUi2Locator LOCATOR_NEW_TAB = private static final IUi2Locator LOCATOR_NEW_TAB =
Ui2Locators.withPath(Ui2Locators.withAnyResEntry(R.id.menu_item_text), Ui2Locators.withResEntriesByIndex(0, R.id.menu_item_text);
Ui2Locators.withTextString(R.string.button_new_tab));
private static final IUi2Locator LOCATOR_NEW_INCOGNITO_TAB = private static final IUi2Locator LOCATOR_NEW_INCOGNITO_TAB =
Ui2Locators.withPath(Ui2Locators.withAnyResEntry(R.id.menu_item_text), Ui2Locators.withResEntriesByIndex(1, R.id.menu_item_text);
Ui2Locators.withTextString(R.string.button_new_incognito_tab));
private static final IUi2Locator LOCATOR_CLOSE_ALL_TABS = private static final IUi2Locator LOCATOR_CLOSE_ALL_TABS =
Ui2Locators.withPath(Ui2Locators.withAnyResEntry(R.id.menu_item_text), Ui2Locators.withResEntriesByIndex(2, R.id.menu_item_text);
Ui2Locators.withTextString(R.string.menu_close_all_tabs));
private static final IUi2Locator LOCATOR_SETTINGS = private static final IUi2Locator LOCATOR_SETTINGS =
Ui2Locators.withPath(Ui2Locators.withAnyResEntry(R.id.menu_item_text), Ui2Locators.withResEntriesByIndex(3, R.id.menu_item_text);
Ui2Locators.withTextString(R.string.menu_preferences));
private static final TabSwitcherMenuController sInstance = new TabSwitcherMenuController(); static private final TabSwitcherMenuController sInstance = new TabSwitcherMenuController();
public static TabSwitcherMenuController getInstance() { static public TabSwitcherMenuController getInstance() {
return sInstance; return sInstance;
} }
......
...@@ -18,12 +18,12 @@ import org.chromium.chrome.test.pagecontroller.utils.UiLocatorHelper; ...@@ -18,12 +18,12 @@ import org.chromium.chrome.test.pagecontroller.utils.UiLocatorHelper;
public class UrlPage extends PageController { public class UrlPage extends PageController {
private static final long PAGE_LOAD_TIMEOUT = 10000L; private static final long PAGE_LOAD_TIMEOUT = 10000L;
private static final IUi2Locator LOCATOR_WEB_VIEW = private static final IUi2Locator LOCATOR_WEB_VIEW =
Ui2Locators.withPath(Ui2Locators.withAnyResEntry(R.id.content), Ui2Locators.withPath(Ui2Locators.withResEntries(R.id.content),
Ui2Locators.withClassRegex("android\\.webkit\\.WebView")); Ui2Locators.withClassRegex("android\\.webkit\\.WebView"));
private static final IUi2Locator LOCATOR_URL_BAR = Ui2Locators.withAnyResEntry(R.id.url_bar); private static final IUi2Locator LOCATOR_URL_BAR = Ui2Locators.withResEntries(R.id.url_bar);
private static final IUi2Locator LOCATOR_TAB_SWITCHER = private static final IUi2Locator LOCATOR_TAB_SWITCHER =
Ui2Locators.withAnyResEntry(R.id.tab_switcher_button); Ui2Locators.withResEntries(R.id.tab_switcher_button);
private static final IUi2Locator LOCATOR_MENU = Ui2Locators.withAnyResEntry(R.id.menu_button); private static final IUi2Locator LOCATOR_MENU = Ui2Locators.withResEntries(R.id.menu_button);
private static final UrlPage sInstance = new UrlPage(); private static final UrlPage sInstance = new UrlPage();
private 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,11 +12,9 @@ import static org.chromium.chrome.test.pagecontroller.utils.TestUtils.assertLoca ...@@ -12,11 +12,9 @@ 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.matchesByDepth;
import static org.chromium.chrome.test.pagecontroller.utils.TestUtils.matchesByField; 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.UiDevice;
import android.support.test.uiautomator.UiObject2; import android.support.test.uiautomator.UiObject2;
import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.FixMethodOrder; import org.junit.FixMethodOrder;
import org.junit.Test; import org.junit.Test;
...@@ -43,9 +41,6 @@ public class Ui2LocatorsTest { ...@@ -43,9 +41,6 @@ public class Ui2LocatorsTest {
@Mock @Mock
UiDevice mDevice; UiDevice mDevice;
@Mock
Resources mResources;
List<UiObject2> mRootAsList, mChild0And1, mChild1AsList, mGrandchildren, mGrandchild1AsList; List<UiObject2> mRootAsList, mChild0And1, mChild1AsList, mGrandchildren, mGrandchild1AsList;
@Mock @Mock
...@@ -80,15 +75,10 @@ public class Ui2LocatorsTest { ...@@ -80,15 +75,10 @@ public class Ui2LocatorsTest {
when(mChild1.getChildren()).thenReturn(Collections.emptyList()); when(mChild1.getChildren()).thenReturn(Collections.emptyList());
} }
@After
public void tearDown() {
Ui2Locators.setResources(null);
}
@Test @Test
public void withChildIndex() { public void withChildIndex() {
Pattern p = Pattern.compile("name"); Pattern p = Pattern.compile("name");
stubMocksWithPattern(p, "mRes"); stuckMocksWithPattern(p, "mRes");
IUi2Locator locator = Ui2Locators.withChildIndex(0, 1); IUi2Locator locator = Ui2Locators.withChildIndex(0, 1);
assertEquals(mChild1, locator.locateOne(mDevice)); assertEquals(mChild1, locator.locateOne(mDevice));
assertEquals(mChild1AsList, locator.locateAll(mDevice)); assertEquals(mChild1AsList, locator.locateAll(mDevice));
...@@ -102,33 +92,37 @@ public class Ui2LocatorsTest { ...@@ -102,33 +92,37 @@ public class Ui2LocatorsTest {
assertLocatorResults(mDevice, mRoot, locator, mGrandchild0, mGrandchildren); 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 @Test
public void withResIdRegex() { public void withResIdRegex() {
Pattern p = Pattern.compile("^.*:id/a.*b$"); Pattern p = Pattern.compile("^.*:id/a.*b$");
stubMocksWithPattern(p, "mRes"); stuckMocksWithPattern(p, "mRes");
IUi2Locator locator = Ui2Locators.withResIdRegex("a.*b"); IUi2Locator locator = Ui2Locators.withResIdRegex("a.*b");
assertDefaultResults(locator); assertDefaultResults(locator);
} }
@Test @Test
public void withAnyResId() { public void withResIds() {
Pattern p = Pattern.compile("^.*:id/(a|b)$"); Pattern p = Pattern.compile("^.*:id/(a|b)$");
stubMocksWithPattern(p, "mRes"); stuckMocksWithPattern(p, "mRes");
IUi2Locator locator = Ui2Locators.withAnyResId("a", "b"); IUi2Locator locator = Ui2Locators.withResIds("a", "b");
assertDefaultResults(locator);
}
@Test
public void withAnyResEntry() {
stubMocksWithResEntry(123, "someEntry");
IUi2Locator locator = Ui2Locators.withAnyResEntry(123);
assertDefaultResults(locator); assertDefaultResults(locator);
} }
@Test @Test
public void withResName() { public void withResName() {
Pattern p = Pattern.compile(Pattern.quote("name")); Pattern p = Pattern.compile(Pattern.quote("name"));
stubMocksWithPattern(p, "mRes"); stuckMocksWithPattern(p, "mRes");
IUi2Locator locator = Ui2Locators.withResName("name"); IUi2Locator locator = Ui2Locators.withResName("name");
assertDefaultResults(locator); assertDefaultResults(locator);
} }
...@@ -136,30 +130,31 @@ public class Ui2LocatorsTest { ...@@ -136,30 +130,31 @@ public class Ui2LocatorsTest {
@Test @Test
public void withResNameRegex() { public void withResNameRegex() {
Pattern p = Pattern.compile(".*name"); Pattern p = Pattern.compile(".*name");
stubMocksWithPattern(p, "mRes"); stuckMocksWithPattern(p, "mRes");
IUi2Locator locator = Ui2Locators.withResNameRegex(".*name"); IUi2Locator locator = Ui2Locators.withResNameRegex(".*name");
assertDefaultResults(locator); assertDefaultResults(locator);
} }
@Test @Test
public void withContentDesc() { public void withResNameRegexByIndex() {
Pattern p = Pattern.compile(Pattern.quote("desc")); Pattern p = Pattern.compile(".*name");
stubMocksWithPattern(p, "mDesc"); stuckMocksWithPattern(p, "mRes");
IUi2Locator locator = Ui2Locators.withContentDesc("desc"); IUi2Locator locator = Ui2Locators.withResNameRegexByIndex(".*name", 1);
assertDefaultResults(locator); assertLocatorResults(mDevice, mRoot, locator, mChild1, mChild1AsList);
} }
@Test @Test
public void withContentDescString() { public void withContentDesc() {
stubMocksWithDescString(123, "someDesc"); Pattern p = Pattern.compile(Pattern.quote("desc"));
IUi2Locator locator = Ui2Locators.withContentDescString(123); stuckMocksWithPattern(p, "mDesc");
IUi2Locator locator = Ui2Locators.withContentDesc("desc");
assertDefaultResults(locator); assertDefaultResults(locator);
} }
@Test @Test
public void withText() { public void withText() {
Pattern p = Pattern.compile(Pattern.quote("text")); Pattern p = Pattern.compile(Pattern.quote("text"));
stubMocksWithPattern(p, "mText"); stuckMocksWithPattern(p, "mText");
IUi2Locator locator = Ui2Locators.withText("text"); IUi2Locator locator = Ui2Locators.withText("text");
assertDefaultResults(locator); assertDefaultResults(locator);
} }
...@@ -167,7 +162,7 @@ public class Ui2LocatorsTest { ...@@ -167,7 +162,7 @@ public class Ui2LocatorsTest {
@Test @Test
public void withTextRegex() { public void withTextRegex() {
Pattern p = Pattern.compile(".*text"); Pattern p = Pattern.compile(".*text");
stubMocksWithPattern(p, "mText"); stuckMocksWithPattern(p, "mText");
IUi2Locator locator = Ui2Locators.withTextRegex(".*text"); IUi2Locator locator = Ui2Locators.withTextRegex(".*text");
assertDefaultResults(locator); assertDefaultResults(locator);
} }
...@@ -175,22 +170,15 @@ public class Ui2LocatorsTest { ...@@ -175,22 +170,15 @@ public class Ui2LocatorsTest {
@Test @Test
public void withTextContaining() { public void withTextContaining() {
Pattern p = Pattern.compile("^.*" + Pattern.quote("text") + ".*$"); Pattern p = Pattern.compile("^.*" + Pattern.quote("text") + ".*$");
stubMocksWithPattern(p, "mText"); stuckMocksWithPattern(p, "mText");
IUi2Locator locator = Ui2Locators.withTextContaining("text"); IUi2Locator locator = Ui2Locators.withTextContaining("text");
assertDefaultResults(locator); assertDefaultResults(locator);
} }
@Test
public void withTextString() {
stubMocksWithTextString(123, "someString");
IUi2Locator locator = Ui2Locators.withTextString(123);
assertDefaultResults(locator);
}
@Test @Test
public void withClassRegex() { public void withClassRegex() {
Pattern p = Pattern.compile(".*class"); Pattern p = Pattern.compile(".*class");
stubMocksWithPattern(p, "mClazz"); stuckMocksWithPattern(p, "mClazz");
IUi2Locator locator = Ui2Locators.withClassRegex(".*class"); IUi2Locator locator = Ui2Locators.withClassRegex(".*class");
assertDefaultResults(locator); assertDefaultResults(locator);
} }
...@@ -198,7 +186,7 @@ public class Ui2LocatorsTest { ...@@ -198,7 +186,7 @@ public class Ui2LocatorsTest {
@Test @Test
public void withPath() { public void withPath() {
Pattern p = Pattern.compile(".*class"); Pattern p = Pattern.compile(".*class");
stubMocksWithPattern(p, "mClazz"); stuckMocksWithPattern(p, "mClazz");
IUi2Locator locator0 = Ui2Locators.withClassRegex(".*class"); IUi2Locator locator0 = Ui2Locators.withClassRegex(".*class");
IUi2Locator locator1 = Ui2Locators.withChildIndex(1); IUi2Locator locator1 = Ui2Locators.withChildIndex(1);
IUi2Locator locator = Ui2Locators.withPath(locator0, locator1); IUi2Locator locator = Ui2Locators.withPath(locator0, locator1);
...@@ -208,40 +196,8 @@ public class Ui2LocatorsTest { ...@@ -208,40 +196,8 @@ public class Ui2LocatorsTest {
@Test @Test
public void withPackageName() { public void withPackageName() {
Pattern p = Pattern.compile(Pattern.quote("package")); Pattern p = Pattern.compile(Pattern.quote("package"));
stubMocksWithPattern(p, "mPkg"); stuckMocksWithPattern(p, "mPkg");
IUi2Locator locator = Ui2Locators.withPackageName("package"); IUi2Locator locator = Ui2Locators.withPackageName("package");
assertDefaultResults(locator); 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 { ...@@ -325,7 +325,7 @@ public class UiLocatorHelper {
* Delegate to be used with getCustomElements. * Delegate to be used with getCustomElements.
* @param <T> The type of the element. * @param <T> The type of the element.
*/ */
public static interface CustomElementMaker<T> { static public interface CustomElementMaker<T> {
/** /**
* Should construct an element given a node. * Should construct an element given a node.
* @param root The input node. * @param root The input node.
...@@ -403,11 +403,10 @@ public class UiLocatorHelper { ...@@ -403,11 +403,10 @@ public class UiLocatorHelper {
private <T> T getOneElement(IUi2Locator locator, ElementConverter<T> converter) { private <T> T getOneElement(IUi2Locator locator, ElementConverter<T> converter) {
List<T> all = getAllElements(locator, converter); List<T> all = getAllElements(locator, converter);
if (all.size() > 0) { if (all.size() > 0)
return all.get(0); return all.get(0);
} else { else
return null; return null;
}
} }
private <T> T getOneElementImmediate( 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