Bug 1348086 - Part 5 - Convert session store robocop tests to be based on UITest. r?gbrown draft
authorJan Henning <jh+bugzilla@buttercookie.de>
Fri, 17 Mar 2017 22:25:57 +0100
changeset 503109 791cb08a11cba64f232ac1e2e059d721980d9c4f
parent 503108 422ee95de18a8bb510b8b2fbc3d49b4d268bbfdf
child 503110 81e1ee75cd4a9eb79fc3395ac99518ef3a90e4b2
push id50503
push usermozilla@buttercookie.de
push dateWed, 22 Mar 2017 21:30:04 +0000
reviewersgbrown
bugs1348086
milestone55.0a1
Bug 1348086 - Part 5 - Convert session store robocop tests to be based on UITest. r?gbrown UITest is our more modern framework for Robocop tests, so using that will hopefully make these tests more reliable and cut down on the intermittent failures which led to them being deactivated. MozReview-Commit-ID: 6iZCS75FUvT
mobile/android/tests/browser/robocop/src/org/mozilla/gecko/tests/SessionTest.java
mobile/android/tests/browser/robocop/src/org/mozilla/gecko/tests/components/ToolbarComponent.java
mobile/android/tests/browser/robocop/src/org/mozilla/gecko/tests/testSessionOOMRestore.java
mobile/android/tests/browser/robocop/src/org/mozilla/gecko/tests/testSessionOOMSave.java
--- a/mobile/android/tests/browser/robocop/src/org/mozilla/gecko/tests/SessionTest.java
+++ b/mobile/android/tests/browser/robocop/src/org/mozilla/gecko/tests/SessionTest.java
@@ -1,36 +1,31 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 package org.mozilla.gecko.tests;
 
+import android.util.Log;
+
 import java.io.File;
 import java.io.FileReader;
 import java.io.FileWriter;
 import java.io.IOException;
 
 import org.json.JSONArray;
 import org.json.JSONException;
 import org.json.JSONObject;
-import org.mozilla.gecko.Actions;
 import org.mozilla.gecko.Assert;
 import org.mozilla.gecko.FennecMochitestAssert;
-
-public abstract class SessionTest extends BaseTest {
-    protected Navigation mNavigation;
+import org.mozilla.gecko.tests.helpers.NavigationHelper;
 
-    @Override
-    public void setUp() throws Exception {
-        super.setUp();
+import static org.mozilla.gecko.tests.components.AppMenuComponent.MenuItem;
 
-        mNavigation = new Navigation(mDevice);
-    }
-
+public abstract class SessionTest extends UITest {
     /**
      * A generic session object representing a collection of items that has a
      * selected index.
      */
     protected abstract class SessionObject<T> {
         private final int mIndex;
         private final T[] mItems;
 
@@ -130,99 +125,83 @@ public abstract class SessionTest extend
 
     /**
      * Loads a set of tabs in the browser specified by the given session.
      *
      * @param session Session to load
      */
     protected void loadSessionTabs(Session session) {
         // Verify initial about:home tab
-        verifyTabCount(1);
-        verifyUrl(mStringHelper.ABOUT_HOME_URL);
+        mToolbar.assertTabCount(1);
+        mToolbar.assertTitle(mStringHelper.ABOUT_HOME_URL);
 
         SessionTab[] tabs = session.getItems();
         for (int i = 0; i < tabs.length; i++) {
             final SessionTab tab = tabs[i];
             final PageInfo[] pages = tab.getItems();
 
             // New tabs always start with about:home, so make sure about:home
             // is always the first entry.
             mAsserter.is(pages[0].url, mStringHelper.ABOUT_HOME_URL, "first page in tab is " +
                     mStringHelper.ABOUT_HOME_URL);
 
             // If this is the first tab, the tab already exists, so no need to
             // create a new one. Otherwise, create a new tab if we're loading
             // the first the first page in the set.
             if (i > 0) {
-                addTab();
+                mAppMenu.pressMenuItem(MenuItem.NEW_TAB);
             }
 
             for (int j = 1; j < pages.length; j++) {
-                final Actions.EventExpecter pageShowExpecter =
-                        mActions.expectGlobalEvent(Actions.EventType.UI, "Content:PageShow");
-
-                loadUrl(pages[j].url);
-
-                pageShowExpecter.blockForEvent();
-                pageShowExpecter.unregisterListener();
+                NavigationHelper.enterAndLoadUrl(pages[j].url);
             }
 
             final int index = tab.getIndex();
             for (int j = pages.length - 1; j > index; j--) {
-                mNavigation.back();
+                NavigationHelper.goBack();
             }
         }
 
-        selectTabAt(session.getIndex());
+        mTabsPanel.selectTabAt(session.getIndex());
     }
 
     /**
      * Verifies that the set of open tabs matches the given session.
      *
      * @param session Session to verify
      */
     protected void verifySessionTabs(Session session) {
-        verifyTabCount(session.getItems().length);
+        mToolbar.assertTabCount(session.getItems().length);
 
         (new NavigationWalker<SessionTab>(session) {
             boolean mFirstTabVisited;
 
             @Override
             public void onItem(SessionTab tab, int currentIndex) {
                 // The first tab to check should already be selected at startup
                 if (mFirstTabVisited) {
-                    selectTabAt(currentIndex);
+                    mTabsPanel.selectTabAt(currentIndex);
                 } else {
                     mFirstTabVisited = true;
                 }
 
                 (new NavigationWalker<PageInfo>(tab) {
                     @Override
                     public void onItem(PageInfo page, int currentIndex) {
-                        final String text;
-                        if (mStringHelper.ABOUT_HOME_URL.equals(page.url)) {
-                            text = mStringHelper.TITLE_PLACE_HOLDER;
-                        } else if (page.url.startsWith(URL_HTTP_PREFIX)) {
-                            text = page.url.substring(URL_HTTP_PREFIX.length());
-                        } else {
-                            text = page.url;
-                        }
-                        waitForText(text);
-
-                        verifyUrlBarTitle(page.url);
+                        mToolbar.assertTitle(page.url);
                     }
 
                     @Override
                     public void goBack() {
-                        mNavigation.back();
+                        NavigationHelper.goBack();
                     }
 
                     @Override
                     public void goForward() {
-                        mNavigation.forward();
+                        NavigationHelper.goForward();
                     }
                 }).walk();
             }
         }).walk();
     }
 
     /**
      * Gets session restore JSON corresponding to the open session.
@@ -259,17 +238,17 @@ public abstract class SessionTest extend
                 tabs.put(tab);
             }
 
             JSONObject window = new JSONObject();
             window.put("tabs", tabs);
             window.put("selected", session.getIndex() + 1);
             sessionString = new JSONObject().put("windows", new JSONArray().put(window)).toString();
         } catch (JSONException e) {
-            mAsserter.ok(false, "JSON exception", getStackTraceString(e));
+            mAsserter.ok(false, "JSON exception", Log.getStackTraceString(e));
         }
 
         return sessionString;
     }
 
     /**
      * @see SessionTest#verifySessionJSON(Session, String, Assert)
      */
@@ -311,17 +290,17 @@ public abstract class SessionTest extend
 
                     asserter.is(url, page.url, "URL in JSON matches session URL");
                     if (!page.url.startsWith("about:")) {
                         asserter.is(title, page.title, "title in JSON matches session title");
                     }
                 }
             }
         } catch (JSONException e) {
-            asserter.ok(false, "JSON exception", getStackTraceString(e));
+            asserter.ok(false, "JSON exception", Log.getStackTraceString(e));
         }
     }
 
     /**
      * Exception thrown by NonFatalAsserter for assertion failures.
      */
     public static class AssertException extends RuntimeException {
         public AssertException(String msg) {
@@ -352,33 +331,33 @@ public abstract class SessionTest extend
      *
      * The page will have a URL unique to the given ID, and the page's title
      * will match the given ID.
      *
      * @param id ID used to generate page URL
      * @return URL of the page
      */
     protected String getPage(String id) {
-        return getAbsoluteUrl("/robocop/robocop_dynamic.sjs?id=" + id);
+        return getAbsoluteHostnameUrl("/robocop/robocop_dynamic.sjs?id=" + id);
     }
 
     protected String readProfileFile(String filename) {
         try {
             return readFile(new File(mProfile, filename));
         } catch (IOException e) {
-            mAsserter.ok(false, "Error reading" + filename, getStackTraceString(e));
+            mAsserter.ok(false, "Error reading" + filename, Log.getStackTraceString(e));
         }
         return null;
     }
 
     protected void writeProfileFile(String filename, String data) {
         try {
             writeFile(new File(mProfile, filename), data);
         } catch (IOException e) {
-            mAsserter.ok(false, "Error writing to " + filename, getStackTraceString(e));
+            mAsserter.ok(false, "Error writing to " + filename, Log.getStackTraceString(e));
         }
     }
 
     private String readFile(File target) throws IOException {
         if (!target.exists()) {
             return null;
         }
 
--- 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
@@ -24,18 +24,17 @@ import android.widget.TextView;
 
 import com.robotium.solo.Condition;
 import com.robotium.solo.Solo;
 
 /**
  * A class representing any interactions that take place on the Toolbar.
  */
 public class ToolbarComponent extends BaseComponent {
-
-    private static final String URL_HTTP_PREFIX = "http://";
+    public static final String URL_HTTP_PREFIX = "http://";
 
     // We are waiting up to 30 seconds instead of the default waiting time because reader mode
     // parsing can take quite some time on slower devices (Bug 1142699)
     private static final int READER_MODE_WAIT_MS = 30000;
 
     public ToolbarComponent(final UITestContext testContext) {
         super(testContext);
     }
--- a/mobile/android/tests/browser/robocop/src/org/mozilla/gecko/tests/testSessionOOMRestore.java
+++ b/mobile/android/tests/browser/robocop/src/org/mozilla/gecko/tests/testSessionOOMRestore.java
@@ -1,15 +1,17 @@
 package org.mozilla.gecko.tests;
 
 import android.content.Context;
 import android.content.Intent;
 import android.content.SharedPreferences;
 import android.os.Bundle;
 
+import org.mozilla.gecko.tests.helpers.GeckoHelper;
+
 /**
  * Tests session OOM restore behavior.
  *
  * Loads a session and tests that it is restored correctly.
  */
 public class testSessionOOMRestore extends SessionTest {
     private Session mSession;
     private static final String PREFS_NAME = "GeckoApp";
@@ -43,12 +45,12 @@ public class testSessionOOMRestore exten
         Bundle bundle = new Bundle();
         bundle.putString("privateSession", null);
         intent.putExtra("stateBundle", bundle);
 
         super.setActivityIntent(intent);
     }
 
     public void testSessionOOMRestore() throws Exception {
-        blockForGeckoReady();
+        GeckoHelper.blockForReady();
         verifySessionTabs(mSession);
     }
 }
--- a/mobile/android/tests/browser/robocop/src/org/mozilla/gecko/tests/testSessionOOMSave.java
+++ b/mobile/android/tests/browser/robocop/src/org/mozilla/gecko/tests/testSessionOOMSave.java
@@ -1,10 +1,12 @@
 package org.mozilla.gecko.tests;
 
+import android.util.Log;
+
 import org.mozilla.gecko.Actions;
 
 import com.robotium.solo.Condition;
 
 /**
  * Tests session OOM save behavior.
  *
  * Builds a session and tests that the saved state is correct.
@@ -36,22 +38,22 @@ public class testSessionOOMSave extends 
         loadSessionTabs(session);
 
         // Verify sessionstore.js written by Gecko. The session write is
         // delayed for certain interactions (such as changing the selected
         // tab), so the file is repeatedly read until it matches the expected
         // output. Because of the delay, this part of the test takes ~9 seconds
         // to pass.
         VerifyJSONCondition verifyJSONCondition = new VerifyJSONCondition(session);
-        boolean success = waitForCondition(verifyJSONCondition, SESSION_TIMEOUT);
+        boolean success = mSolo.waitForCondition(verifyJSONCondition, SESSION_TIMEOUT);
         if (success) {
             mAsserter.ok(true, "verified session JSON", null);
         } else {
             mAsserter.ok(false, "failed to verify session JSON",
-                    getStackTraceString(verifyJSONCondition.getLastException()));
+                    Log.getStackTraceString(verifyJSONCondition.getLastException()));
         }
     }
 
     private class VerifyJSONCondition implements Condition {
         private AssertException mLastException;
         private final NonFatalAsserter mAsserter = new NonFatalAsserter();
         private final Session mSession;