Bug 935264 - Add more test cases to testHomeBanner (and update it to use UITest). r=mcomella a=test-only
authorMargaret Leibovic <margaret.leibovic@gmail.com>
Fri, 14 Feb 2014 11:02:12 -0800
changeset 183078 fcbc2e04b8c8e663e42f42d25e41d8749023bde1
parent 183077 0f0ec964befbacbfde7732291dc993ae2f4c6c15
child 183079 6f1b2e06c66679a84c27ca394143807af12e3843
push id3343
push userffxbld
push dateMon, 17 Mar 2014 21:55:32 +0000
treeherdermozilla-beta@2f7d3415f79f [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmcomella, test-only
bugs935264
milestone29.0a2
Bug 935264 - Add more test cases to testHomeBanner (and update it to use UITest). r=mcomella a=test-only
mobile/android/base/tests/components/AboutHomeComponent.java
mobile/android/base/tests/helpers/NavigationHelper.java
mobile/android/base/tests/roboextender/robocop_home_banner.html
mobile/android/base/tests/testHomeBanner.java
--- a/mobile/android/base/tests/components/AboutHomeComponent.java
+++ b/mobile/android/base/tests/components/AboutHomeComponent.java
@@ -12,16 +12,17 @@ import org.mozilla.gecko.tests.helpers.*
 import org.mozilla.gecko.tests.UITestContext;
 
 import com.jayway.android.robotium.solo.Condition;
 import com.jayway.android.robotium.solo.Solo;
 
 import android.support.v4.view.PagerAdapter;
 import android.support.v4.view.ViewPager;
 import android.view.View;
+import android.widget.TextView;
 
 /**
  * A class representing any interactions that take place on the Awesomescreen.
  */
 public class AboutHomeComponent extends BaseComponent {
     private static final String LOGTAG = AboutHomeComponent.class.getSimpleName();
 
     // The different types of panels that can be present on about:home
@@ -57,16 +58,20 @@ public class AboutHomeComponent extends 
     public AboutHomeComponent(final UITestContext testContext) {
         super(testContext);
     }
 
     private ViewPager getHomePagerView() {
         return (ViewPager) mSolo.getView(R.id.home_pager);
     }
 
+    private View getHomeBannerView() {
+        return mSolo.getView(R.id.home_banner);
+    }
+
     public AboutHomeComponent assertCurrentPanel(final PanelType expectedPanel) {
         assertVisible();
 
         final int expectedPanelIndex = getPanelIndexForDevice(expectedPanel.ordinal());
         assertEquals("The current HomePager panel is " + expectedPanel,
                      expectedPanelIndex, getHomePagerView().getCurrentItem());
         return this;
     }
@@ -78,16 +83,45 @@ public class AboutHomeComponent extends 
     }
 
     public AboutHomeComponent assertVisible() {
         assertEquals("The HomePager is visible",
                      View.VISIBLE, getHomePagerView().getVisibility());
         return this;
     }
 
+    public AboutHomeComponent assertBannerNotVisible() {
+        assertFalse("The HomeBanner is not visible",
+                    getHomeBannerView().getVisibility() == View.VISIBLE);
+        return this;
+    }
+
+    public AboutHomeComponent assertBannerVisible() {
+        assertEquals("The HomeBanner is visible",
+                     View.VISIBLE, getHomeBannerView().getVisibility());
+        return this;
+    }
+
+    public AboutHomeComponent assertBannerText(String text) {
+        assertBannerVisible();
+
+        final TextView textView = (TextView) getHomeBannerView().findViewById(R.id.text);
+        assertEquals("The correct HomeBanner text is shown",
+                     text, textView.getText().toString());
+        return this;
+    }
+
+    public AboutHomeComponent clickOnBanner() {
+        assertBannerVisible();
+
+        mTestContext.dumpLog(LOGTAG, "Clicking on HomeBanner.");
+        mSolo.clickOnView(getHomeBannerView());
+        return this;
+    }
+
     public AboutHomeComponent swipeToPanelOnRight() {
         mTestContext.dumpLog(LOGTAG, "Swiping to the panel on the right.");
         swipeToPanel(Solo.RIGHT);
         return this;
     }
 
     public AboutHomeComponent swipeToPanelOnLeft() {
         mTestContext.dumpLog(LOGTAG, "Swiping to the panel on the left.");
--- a/mobile/android/base/tests/helpers/NavigationHelper.java
+++ b/mobile/android/base/tests/helpers/NavigationHelper.java
@@ -41,21 +41,21 @@ final public class NavigationHelper {
     }
 
     /**
      * Returns a new URL with the docshell HTTP server host prefix.
      */
     private static String adjustUrl(final String url) {
         assertNotNull("url is not null", url);
 
-        if (!url.startsWith("about:")) {
-            return sContext.getAbsoluteHostnameUrl(url);
+        if (url.startsWith("about:") || url.startsWith("chrome:")) {
+            return url;
         }
 
-        return url;
+        return sContext.getAbsoluteHostnameUrl(url);
     }
 
     public static void goBack() {
         if (DeviceHelper.isTablet()) {
             sToolbar.pressBackButton(); // Waits for page load & asserts isNotEditing.
             return;
         }
 
--- a/mobile/android/base/tests/roboextender/robocop_home_banner.html
+++ b/mobile/android/base/tests/roboextender/robocop_home_banner.html
@@ -13,26 +13,33 @@ function sendMessageToJava(msg) {
 
 const TEXT = "The quick brown fox jumps over the lazy dog.";
 
 function start() {
   var test = location.hash.substring(1);
   window[test]();
 }
 
+var messageId;
+
 function addMessage() {
-  Home.banner.add({
+  messageId = Home.banner.add({
     text: TEXT,
     onclick: function() {
       sendMessageToJava({ type: "TestHomeBanner:MessageClicked" });
     },
     onshown: function() {
       sendMessageToJava({ type: "TestHomeBanner:MessageShown" });
     }
   });
   sendMessageToJava({ type: "TestHomeBanner:MessageAdded" });
 }
 
+function removeMessage() {
+  Home.banner.remove(messageId);
+  sendMessageToJava({ type: "TestHomeBanner:MessageRemoved" });
+}
+
     </script>
   </head>
   <body onload="start();">
   </body>
 </html>
--- a/mobile/android/base/tests/testHomeBanner.java
+++ b/mobile/android/base/tests/testHomeBanner.java
@@ -1,37 +1,71 @@
 package org.mozilla.gecko.tests;
 
-import org.mozilla.gecko.*;
+import static org.mozilla.gecko.tests.helpers.AssertionHelper.*;
 
-public class testHomeBanner extends BaseTest {
+import org.mozilla.gecko.Actions;
+import org.mozilla.gecko.R;
+import org.mozilla.gecko.tests.helpers.*;
+
+import android.view.View;
+
+public class testHomeBanner extends UITest {
 
     private static final String TEST_URL = "chrome://roboextender/content/robocop_home_banner.html";
     private static final String TEXT = "The quick brown fox jumps over the lazy dog.";
 
-    @Override
-    protected int getTestType() {
-        return TEST_MOCHITEST;
+    public void testHomeBanner() {
+        GeckoHelper.blockForReady();
+
+        // Make sure the banner is not visible.
+        mAboutHome.assertVisible()
+                  .assertBannerNotVisible();
+
+        addBannerMessage();
+
+        // Load about:home again, and make sure the onshown handler is called.
+        Actions.EventExpecter eventExpecter = getActions().expectGeckoEvent("TestHomeBanner:MessageShown");
+        NavigationHelper.enterAndLoadUrl("about:home");
+        eventExpecter.blockForEvent();
+
+        // Verify that the banner is visible with the correct text.
+        mAboutHome.assertBannerText(TEXT);
+
+        // Test to make sure the onclick handler is called.
+        eventExpecter = getActions().expectGeckoEvent("TestHomeBanner:MessageClicked");
+        mAboutHome.clickOnBanner();
+        eventExpecter.blockForEvent();
+
+        // Verify that the banner isn't visible after navigating away from about:home.
+        NavigationHelper.enterAndLoadUrl("about:firefox");
+
+        // AboutHomeComponent calls mSolo.getView, which will fail an assertion if the
+        // view is not present, so we need to use findViewById in this case.
+        final View banner = getActivity().findViewById(R.id.home_banner);
+        assertTrue("The HomeBanner is not visible", banner == null || banner.getVisibility() != View.VISIBLE);
+
+        removeBannerMessage();
+
+        // Verify that the banner no longer appears.
+        NavigationHelper.enterAndLoadUrl("about:home");
+        mAboutHome.assertVisible()
+                  .assertBannerNotVisible();
     }
 
-    public void testHomeBanner() {
-        blockForGeckoReady();
-
-        Actions.EventExpecter eventExpecter = mActions.expectGeckoEvent("TestHomeBanner:MessageAdded");
-
-        // Add a message to the home banner
-        inputAndLoadUrl(TEST_URL + "#addMessage");
+    /**
+     * Loads the roboextender page to add a message to the banner.
+     */
+    private void addBannerMessage() {
+        final Actions.EventExpecter eventExpecter = getActions().expectGeckoEvent("TestHomeBanner:MessageAdded");
+        NavigationHelper.enterAndLoadUrl(TEST_URL + "#addMessage");
         eventExpecter.blockForEvent();
+    }
 
-        // Load about:home, and test to make sure the onshown handler is called
-        eventExpecter = mActions.expectGeckoEvent("TestHomeBanner:MessageShown");
-        inputAndLoadUrl("about:home");
-        eventExpecter.blockForEvent();
-
-        // Verify that the correct message text showed up in the banner
-        mAsserter.ok(waitForText(TEXT), "banner text", "correct text appeared in the home banner");
-
-        // Test to make sure the onclick handler is called
-        eventExpecter = mActions.expectGeckoEvent("TestHomeBanner:MessageClicked");
-        mSolo.clickOnText(TEXT);
+    /**
+     * Loads the roboextender page to remove the message from the banner.
+     */
+    private void removeBannerMessage() {
+        final Actions.EventExpecter eventExpecter = getActions().expectGeckoEvent("TestHomeBanner:MessageRemoved");
+        NavigationHelper.enterAndLoadUrl(TEST_URL + "#removeMessage");
         eventExpecter.blockForEvent();
     }
 }