Bug 1236431 - Use contentDescription to verify URL in tests. r=mcomella draft
authorSebastian Kaspari <s.kaspari@gmail.com>
Fri, 12 Feb 2016 18:42:40 +0100
changeset 331460 5cf0a2dc496f67e000016a9f9a0094a7e1354122
parent 331459 2c315e1c84416e77fd534f54eb9e5aa4e50fb8da
child 514392 f6120c3d003e655e7a77bd1287657f57d76f1105
push id10995
push users.kaspari@gmail.com
push dateWed, 17 Feb 2016 10:24:29 +0000
reviewersmcomella
bugs1236431
milestone47.0a1
Bug 1236431 - Use contentDescription to verify URL in tests. r=mcomella MozReview-Commit-ID: IrcsLaNExEM
mobile/android/base/java/org/mozilla/gecko/toolbar/ToolbarDisplayLayout.java
mobile/android/tests/browser/robocop/src/org/mozilla/gecko/tests/BaseTest.java
mobile/android/tests/browser/robocop/src/org/mozilla/gecko/tests/components/ToolbarComponent.java
mobile/android/tests/browser/robocop/src/org/mozilla/gecko/tests/testAboutPage.java
mobile/android/tests/browser/robocop/src/org/mozilla/gecko/tests/testPictureLinkContextMenu.java
--- a/mobile/android/base/java/org/mozilla/gecko/toolbar/ToolbarDisplayLayout.java
+++ b/mobile/android/base/java/org/mozilla/gecko/toolbar/ToolbarDisplayLayout.java
@@ -230,41 +230,47 @@ public class ToolbarDisplayLayout extend
         }
 
         final String url = tab.getURL();
 
         // Setting a null title will ensure we just see the
         // "Enter Search or Address" placeholder text.
         if (AboutPages.isTitlelessAboutPage(url)) {
             setTitle(null);
+            setContentDescription(null);
             return;
         }
 
         // Show the about:blocked page title in red, regardless of prefs
         if (tab.getErrorType() == Tab.ErrorType.BLOCKED) {
             final String title = tab.getDisplayTitle();
 
             final SpannableStringBuilder builder = new SpannableStringBuilder(title);
             builder.setSpan(mBlockedColor, 0, title.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
 
             setTitle(builder);
+            setContentDescription(null);
             return;
         }
 
         final String baseDomain = tab.getBaseDomain();
 
         String strippedURL = stripAboutReaderURL(url);
 
         if (mPrefs.shouldTrimUrls()) {
             strippedURL = StringUtils.stripCommonSubdomains(StringUtils.stripScheme(strippedURL));
         }
 
+        // This value is not visible to screen readers but we rely on it when running UI tests. Screen
+        // readers will instead focus BrowserToolbar and read the "base domain" from there. UI tests
+        // will read the content description to obtain the full URL for performing assertions.
+        setContentDescription(strippedURL);
+
         if (!TextUtils.isEmpty(baseDomain)) {
             setTitle(baseDomain);
-            setContentDescription(strippedURL);
         } else {
             setTitle(strippedURL);
         }
     }
 
     private String stripAboutReaderURL(final String url) {
         if (!AboutPages.isAboutReader(url)) {
             return url;
--- a/mobile/android/tests/browser/robocop/src/org/mozilla/gecko/tests/BaseTest.java
+++ b/mobile/android/tests/browser/robocop/src/org/mozilla/gecko/tests/BaseTest.java
@@ -244,16 +244,32 @@ abstract class BaseTest extends BaseRobo
 
         @Override
         public boolean isSatisfied() {
             String textValue = mTextView.getText().toString();
             return mExpected.equals(textValue);
         }
     }
 
+    class VerifyContentDescription implements Condition {
+        private final View view;
+        private final String expected;
+
+        public VerifyContentDescription(View view, String expected) {
+            this.view = view;
+            this.expected = expected;
+        }
+
+        @Override
+        public boolean isSatisfied() {
+            final CharSequence actual = view.getContentDescription();
+            return TextUtils.equals(actual, expected);
+        }
+    }
+
     protected final String getAbsoluteUrl(String url) {
         return mBaseHostnameUrl + "/" + url.replaceAll("(^/)", "");
     }
 
     protected final String getAbsoluteRawUrl(String url) {
         return mBaseIpUrl + "/" + url.replaceAll("(^/)", "");
     }
 
@@ -466,16 +482,43 @@ abstract class BaseTest extends BaseRobo
             // Wait for the title to make sure it has been displayed in case the view
             // does not update fast enough
             waitForCondition(new VerifyTextViewText(urlBarTitle, expected), MAX_WAIT_VERIFY_PAGE_TITLE_MS);
             pageTitle = urlBarTitle.getText().toString();
         }
         mAsserter.is(pageTitle, expected, "Page title is correct");
     }
 
+    public final void verifyUrlInContentDescription(String url) {
+        mAsserter.isnot(url, null, "The url argument is not null");
+
+        final String expected;
+        if (mStringHelper.ABOUT_HOME_URL.equals(url)) {
+            expected = mStringHelper.ABOUT_HOME_TITLE;
+        } else if (url.startsWith(URL_HTTP_PREFIX)) {
+            expected = url.substring(URL_HTTP_PREFIX.length());
+        } else {
+            expected = url;
+        }
+
+        final View urlDisplayLayout = mSolo.getView(R.id.display_layout);
+        assertNotNull("ToolbarDisplayLayout is not null", urlDisplayLayout);
+
+        String actualUrl = null;
+
+        // Wait for the title to make sure it has been displayed in case the view
+        // does not update fast enough
+        waitForCondition(new VerifyContentDescription(urlDisplayLayout, expected), MAX_WAIT_VERIFY_PAGE_TITLE_MS);
+        if (urlDisplayLayout.getContentDescription() != null) {
+            actualUrl = urlDisplayLayout.getContentDescription().toString();
+        }
+
+        mAsserter.is(actualUrl, expected, "Url is correct");
+    }
+
     public final void verifyTabCount(int expectedTabCount) {
         Element tabCount = mDriver.findElement(getActivity(), R.id.tabs_counter);
         String tabCountText = tabCount.getText();
         int tabCountInt = Integer.parseInt(tabCountText);
         mAsserter.is(tabCountInt, expectedTabCount, "The correct number of tabs are opened");
     }
 
     public void verifyPinned(final boolean isPinned, final String gridItemTitle) {
--- a/mobile/android/tests/browser/robocop/src/org/mozilla/gecko/tests/components/ToolbarComponent.java
+++ b/mobile/android/tests/browser/robocop/src/org/mozilla/gecko/tests/components/ToolbarComponent.java
@@ -58,17 +58,19 @@ public class ToolbarComponent extends Ba
         if (mStringHelper.ABOUT_HOME_URL.equals(absoluteURL)) {
             expected = mStringHelper.ABOUT_HOME_TITLE;
         } else if (absoluteURL.startsWith(URL_HTTP_PREFIX)) {
             expected = absoluteURL.substring(URL_HTTP_PREFIX.length());
         } else {
             expected = absoluteURL;
         }
 
-        fAssertEquals("The Toolbar title is " + expected, expected, getTitle());
+        // Since we only display a shortened "base domain" (See bug 1236431) we use the content
+        // description to obtain the full URL.
+        fAssertEquals("The Toolbar title is " + expected, expected, getUrlFromContentDescription());
         return this;
     }
 
     public ToolbarComponent assertUrl(final String expected) {
         assertIsEditing();
         fAssertEquals("The Toolbar url is " + expected, expected, getUrlEditText().getText());
         return this;
     }
@@ -143,18 +145,25 @@ public class ToolbarComponent extends Ba
 
     /**
      * Returns the View for the edit cancel button in the browser toolbar.
      */
     private View getEditCancelButton() {
         return getToolbarView().findViewById(R.id.edit_cancel);
     }
 
-    private String getTitle() {
-        return getTitleHelper(true);
+    private String getUrlFromContentDescription() {
+        assertIsNotEditing();
+
+        final CharSequence contentDescription = getUrlDisplayLayout().getContentDescription();
+        if (contentDescription == null) {
+            return "";
+        } else {
+            return contentDescription.toString();
+        }
     }
 
     /**
      * Returns the title of the page. Note that this makes no assertions to Toolbar state and
      * may return a value that may never be visible to the user. Callers likely want to use
      * {@link assertTitle} instead.
      */
     public String getPotentiallyInconsistentTitle() {
--- a/mobile/android/tests/browser/robocop/src/org/mozilla/gecko/tests/testAboutPage.java
+++ b/mobile/android/tests/browser/robocop/src/org/mozilla/gecko/tests/testAboutPage.java
@@ -14,34 +14,34 @@ public class testAboutPage extends Pixel
 
     public void testAboutPage() {
         blockForGeckoReady();
 
         // Load the about: page and verify its title.
         String url = mStringHelper.ABOUT_SCHEME;
         loadAndPaint(url);
 
-        verifyUrlBarTitle(url);
+        verifyUrlInContentDescription(url);
 
         // Open a new page to remove the about: page from the current tab.
         url = getAbsoluteUrl(mStringHelper.ROBOCOP_BLANK_PAGE_01_URL);
         loadUrlAndWait(url);
 
         // At this point the page title should have been set.
-        verifyUrlBarTitle(url);
+        verifyUrlInContentDescription(url);
 
         // Set up listeners to catch the page load we're about to do.
         Actions.EventExpecter tabEventExpecter = mActions.expectGeckoEvent("Tab:Added");
         Actions.EventExpecter contentEventExpecter = mActions.expectGeckoEvent("DOMContentLoaded");
 
         selectSettingsItem(mStringHelper.MOZILLA_SECTION_LABEL, mStringHelper.ABOUT_LABEL);
 
         // Wait for the new tab and page to load
         tabEventExpecter.blockForEvent();
         contentEventExpecter.blockForEvent();
 
         tabEventExpecter.unregisterListener();
         contentEventExpecter.unregisterListener();
 
         // Make sure the about: page was loaded.
-        verifyUrlBarTitle(mStringHelper.ABOUT_SCHEME);
+        verifyUrlInContentDescription(mStringHelper.ABOUT_SCHEME);
     }
 }
--- a/mobile/android/tests/browser/robocop/src/org/mozilla/gecko/tests/testPictureLinkContextMenu.java
+++ b/mobile/android/tests/browser/robocop/src/org/mozilla/gecko/tests/testPictureLinkContextMenu.java
@@ -17,17 +17,17 @@ public class testPictureLinkContextMenu 
         final String PICTURE_PAGE_TITLE = mStringHelper.ROBOCOP_PICTURE_LINK_TITLE;
         final String linkMenuItems [] = mStringHelper.CONTEXT_MENU_ITEMS_IN_NORMAL_TAB;
 
         blockForGeckoReady();
 
         PICTURE_PAGE_URL=getAbsoluteUrl(mStringHelper.ROBOCOP_PICTURE_LINK_URL);
         BLANK_PAGE_URL=getAbsoluteUrl(mStringHelper.ROBOCOP_BLANK_PAGE_02_URL);
         loadAndPaint(PICTURE_PAGE_URL);
-        verifyUrlBarTitle(PICTURE_PAGE_URL);
+        verifyUrlInContentDescription(PICTURE_PAGE_URL);
 
         switchTabs(imageTitle);
         verifyContextMenuItems(photoMenuItems);
         verifyTabs(tabs);
         switchTabs(imageTitle);
         verifyCopyOption(photoMenuItems[0], "Firefox.jpg"); // Test the "Copy Image Location" option
         switchTabs(imageTitle);
         verifyShareOption(photoMenuItems[1], PICTURE_PAGE_TITLE); // Test the "Share Image" option