mobile/android/base/tests/testSessionOOMSave.java

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/base/tests/testSessionOOMSave.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,87 @@
     1.4 +package org.mozilla.gecko.tests;
     1.5 +
     1.6 +import org.mozilla.gecko.Actions;
     1.7 +
     1.8 +import com.jayway.android.robotium.solo.Condition;
     1.9 +
    1.10 +/**
    1.11 + * Tests session OOM save behavior.
    1.12 + *
    1.13 + * Builds a session and tests that the saved state is correct.
    1.14 + */
    1.15 +public class testSessionOOMSave extends SessionTest {
    1.16 +    private final static int SESSION_TIMEOUT = 25000;
    1.17 +
    1.18 +    public void testSessionOOMSave() {
    1.19 +        Actions.EventExpecter pageShowExpecter = mActions.expectGeckoEvent("Content:PageShow");
    1.20 +        pageShowExpecter.blockForEvent();
    1.21 +        pageShowExpecter.unregisterListener();
    1.22 +
    1.23 +        PageInfo home = new PageInfo("about:home");
    1.24 +        PageInfo page1 = new PageInfo("page1");
    1.25 +        PageInfo page2 = new PageInfo("page2");
    1.26 +        PageInfo page3 = new PageInfo("page3");
    1.27 +        PageInfo page4 = new PageInfo("page4");
    1.28 +        PageInfo page5 = new PageInfo("page5");
    1.29 +        PageInfo page6 = new PageInfo("page6");
    1.30 +
    1.31 +        SessionTab tab1 = new SessionTab(0, home, page1, page2);
    1.32 +        SessionTab tab2 = new SessionTab(1, home, page3, page4);
    1.33 +        SessionTab tab3 = new SessionTab(2, home, page5, page6);
    1.34 +
    1.35 +        final Session session = new Session(1, tab1, tab2, tab3);
    1.36 +
    1.37 +        // Load the tabs into the browser
    1.38 +        loadSessionTabs(session);
    1.39 +
    1.40 +        // Verify sessionstore.js written by Gecko. The session write is
    1.41 +        // delayed for certain interactions (such as changing the selected
    1.42 +        // tab), so the file is repeatedly read until it matches the expected
    1.43 +        // output. Because of the delay, this part of the test takes ~9 seconds
    1.44 +        // to pass.
    1.45 +        VerifyJSONCondition verifyJSONCondition = new VerifyJSONCondition(session);
    1.46 +        boolean success = waitForCondition(verifyJSONCondition, SESSION_TIMEOUT);
    1.47 +        if (success) {
    1.48 +            mAsserter.ok(true, "verified session JSON", null);
    1.49 +        } else {
    1.50 +            mAsserter.ok(false, "failed to verify session JSON",
    1.51 +                    getStackTraceString(verifyJSONCondition.getLastException()));
    1.52 +        }
    1.53 +    }
    1.54 +
    1.55 +    private class VerifyJSONCondition implements Condition {
    1.56 +        private AssertException mLastException;
    1.57 +        private final NonFatalAsserter mAsserter = new NonFatalAsserter();
    1.58 +        private final Session mSession;
    1.59 +
    1.60 +        public VerifyJSONCondition(Session session) {
    1.61 +            mSession = session;
    1.62 +        }
    1.63 +
    1.64 +        @Override
    1.65 +        public boolean isSatisfied() {
    1.66 +            try {
    1.67 +                String sessionString = readProfileFile("sessionstore.js");
    1.68 +                if (sessionString == null) {
    1.69 +                    mLastException = new AssertException("Could not read sessionstore.js");
    1.70 +                    return false;
    1.71 +                }
    1.72 +
    1.73 +                verifySessionJSON(mSession, sessionString, mAsserter);
    1.74 +            } catch (AssertException e) {
    1.75 +                mLastException = e;
    1.76 +                return false;
    1.77 +            }
    1.78 +            return true;
    1.79 +        }
    1.80 +
    1.81 +        /**
    1.82 +         * Gets the last AssertException thrown by verifySessionJSON().
    1.83 +         *
    1.84 +         * This is useful to get the stack trace if the test fails.
    1.85 +         */
    1.86 +        public AssertException getLastException() {
    1.87 +            return mLastException;
    1.88 +        }
    1.89 +    }
    1.90 +}

mercurial