diff --git 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 index 2d5c5b7e537a..281787b151f5 100644 --- 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 @@ -4,12 +4,13 @@ package org.mozilla.gecko.tests; +import android.content.Context; +import android.content.Intent; +import android.content.SharedPreferences; +import android.os.Bundle; import android.util.Log; -import java.io.File; -import java.io.FileReader; -import java.io.FileWriter; -import java.io.IOException; +import com.robotium.solo.Condition; import org.json.JSONArray; import org.json.JSONException; @@ -18,9 +19,18 @@ import org.mozilla.gecko.Assert; import org.mozilla.gecko.FennecMochitestAssert; import org.mozilla.gecko.tests.helpers.NavigationHelper; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; + +import static org.mozilla.gecko.GeckoApp.PREFS_ALLOW_STATE_BUNDLE; import static org.mozilla.gecko.tests.components.AppMenuComponent.MenuItem; public abstract class SessionTest extends UITest { + private static final String PREFS_NAME = "GeckoApp"; + protected final static int SESSION_TIMEOUT = 25000; + /** * A generic session object representing a collection of items that has a * selected index. @@ -123,6 +133,37 @@ public abstract class SessionTest extends UITest { public void goForward() {} } + protected Session createTestSession(int selectedTabIndex) { + PageInfo home = new PageInfo(StringHelper.STATIC_ABOUT_HOME_URL); + PageInfo page1 = new PageInfo("page1"); + PageInfo page2 = new PageInfo("page2"); + PageInfo page3 = new PageInfo("page3"); + PageInfo page4 = new PageInfo("page4"); + PageInfo page5 = new PageInfo("page5"); + PageInfo page6 = new PageInfo("page6"); + + SessionTab tab1 = new SessionTab(0, home, page1, page2); + SessionTab tab2 = new SessionTab(1, home, page3, page4); + SessionTab tab3 = new SessionTab(2, home, page5, page6); + + return new Session(selectedTabIndex, tab1, tab2, tab3); + } + + protected void injectSessionToRestore(final Intent intent, Session session) { + String sessionString = buildSessionJSON(session); + writeProfileFile("sessionstore.js", sessionString); + + // This feature is pref-protected to prevent other apps from injecting + // a state bundle, so enable it here. + SharedPreferences prefs = getInstrumentation().getTargetContext() + .getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); + prefs.edit().putBoolean(PREFS_ALLOW_STATE_BUNDLE, true).apply(); + + Bundle bundle = new Bundle(); + bundle.putString("privateSession", null); + intent.putExtra("stateBundle", bundle); + } + /** * Loads a set of tabs in the browser specified by the given session. * @@ -249,6 +290,42 @@ public abstract class SessionTest extends UITest { return sessionString; } + protected class VerifyJSONCondition implements Condition { + private AssertException mLastException; + private final NonFatalAsserter mAsserter = new NonFatalAsserter(); + private final Session mSession; + + public VerifyJSONCondition(Session session) { + mSession = session; + } + + @Override + public boolean isSatisfied() { + try { + String sessionString = readProfileFile("sessionstore.js"); + if (sessionString == null) { + mLastException = new AssertException("Could not read sessionstore.js"); + return false; + } + + verifySessionJSON(mSession, sessionString, mAsserter); + } catch (AssertException e) { + mLastException = e; + return false; + } + return true; + } + + /** + * Gets the last AssertException thrown by verifySessionJSON(). + * + * This is useful to get the stack trace if the test fails. + */ + public AssertException getLastException() { + return mLastException; + } + } + /** * @see SessionTest#verifySessionJSON(Session, String, Assert) */ diff --git 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 index 8b4bbfa9812a..6cbf967f0005 100644 --- 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,9 +1,6 @@ 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; @@ -14,37 +11,12 @@ import org.mozilla.gecko.tests.helpers.GeckoHelper; */ public class testSessionOOMRestore extends SessionTest { private Session mSession; - private static final String PREFS_NAME = "GeckoApp"; - private static final String PREFS_ALLOW_STATE_BUNDLE = "allowStateBundle"; @Override public void setActivityIntent(Intent intent) { - PageInfo home = new PageInfo(StringHelper.STATIC_ABOUT_HOME_URL); - PageInfo page1 = new PageInfo("page1"); - PageInfo page2 = new PageInfo("page2"); - PageInfo page3 = new PageInfo("page3"); - PageInfo page4 = new PageInfo("page4"); - PageInfo page5 = new PageInfo("page5"); - PageInfo page6 = new PageInfo("page6"); + mSession = createTestSession(/*selected tab*/ 1); - SessionTab tab1 = new SessionTab(0, home, page1, page2); - SessionTab tab2 = new SessionTab(1, home, page3, page4); - SessionTab tab3 = new SessionTab(2, home, page5, page6); - - mSession = new Session(1, tab1, tab2, tab3); - - String sessionString = buildSessionJSON(mSession); - writeProfileFile("sessionstore.js", sessionString); - - // This feature is pref-protected to prevent other apps from injecting - // a state bundle, so enable it here. - SharedPreferences prefs = getInstrumentation().getTargetContext() - .getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); - prefs.edit().putBoolean(PREFS_ALLOW_STATE_BUNDLE, true).commit(); - - Bundle bundle = new Bundle(); - bundle.putString("privateSession", null); - intent.putExtra("stateBundle", bundle); + injectSessionToRestore(intent, mSession); super.setActivityIntent(intent); } diff --git 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 index c4202d3225ba..ed55ec7db7b9 100644 --- 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 @@ -4,44 +4,27 @@ 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. */ public class testSessionOOMSave extends SessionTest { - private final static int SESSION_TIMEOUT = 25000; - public void testSessionOOMSave() { final Actions.EventExpecter pageShowExpecter = mActions.expectGlobalEvent(Actions.EventType.UI, "Content:PageShow"); pageShowExpecter.blockForEvent(); pageShowExpecter.unregisterListener(); - PageInfo home = new PageInfo(mStringHelper.ABOUT_HOME_URL); - PageInfo page1 = new PageInfo("page1"); - PageInfo page2 = new PageInfo("page2"); - PageInfo page3 = new PageInfo("page3"); - PageInfo page4 = new PageInfo("page4"); - PageInfo page5 = new PageInfo("page5"); - PageInfo page6 = new PageInfo("page6"); - - SessionTab tab1 = new SessionTab(0, home, page1, page2); - SessionTab tab2 = new SessionTab(1, home, page3, page4); - SessionTab tab3 = new SessionTab(2, home, page5, page6); - - final Session session = new Session(1, tab1, tab2, tab3); + final Session session = createTestSession(/*selected tab*/ 1); // Load the tabs into the browser 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. + // Verify sessionstore.js written by Gecko. The session write is delayed + // to batch successive changes, 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 = mSolo.waitForCondition(verifyJSONCondition, SESSION_TIMEOUT); if (success) { @@ -51,40 +34,4 @@ public class testSessionOOMSave extends SessionTest { Log.getStackTraceString(verifyJSONCondition.getLastException())); } } - - private class VerifyJSONCondition implements Condition { - private AssertException mLastException; - private final NonFatalAsserter mAsserter = new NonFatalAsserter(); - private final Session mSession; - - public VerifyJSONCondition(Session session) { - mSession = session; - } - - @Override - public boolean isSatisfied() { - try { - String sessionString = readProfileFile("sessionstore.js"); - if (sessionString == null) { - mLastException = new AssertException("Could not read sessionstore.js"); - return false; - } - - verifySessionJSON(mSession, sessionString, mAsserter); - } catch (AssertException e) { - mLastException = e; - return false; - } - return true; - } - - /** - * Gets the last AssertException thrown by verifySessionJSON(). - * - * This is useful to get the stack trace if the test fails. - */ - public AssertException getLastException() { - return mLastException; - } - } }