mobile/android/base/tests/UITest.java

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/base/tests/UITest.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,255 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +package org.mozilla.gecko.tests;
     1.9 +
    1.10 +import java.util.Map;
    1.11 +
    1.12 +import org.mozilla.gecko.Actions;
    1.13 +import org.mozilla.gecko.Assert;
    1.14 +import org.mozilla.gecko.Driver;
    1.15 +import org.mozilla.gecko.FennecNativeActions;
    1.16 +import org.mozilla.gecko.FennecNativeDriver;
    1.17 +import org.mozilla.gecko.GeckoAppShell;
    1.18 +import org.mozilla.gecko.GeckoEvent;
    1.19 +import org.mozilla.gecko.tests.components.AboutHomeComponent;
    1.20 +import org.mozilla.gecko.tests.components.AppMenuComponent;
    1.21 +import org.mozilla.gecko.tests.components.BaseComponent;
    1.22 +import org.mozilla.gecko.tests.components.GeckoViewComponent;
    1.23 +import org.mozilla.gecko.tests.components.ToolbarComponent;
    1.24 +import org.mozilla.gecko.tests.helpers.HelperInitializer;
    1.25 +
    1.26 +import android.app.Activity;
    1.27 +import android.content.Intent;
    1.28 +import android.text.TextUtils;
    1.29 +
    1.30 +import com.jayway.android.robotium.solo.Solo;
    1.31 +
    1.32 +/**
    1.33 + * A base test class for Robocop (UI-centric) tests. This and the related classes attempt to
    1.34 + * provide a framework to improve upon the issues discovered with the previous BaseTest
    1.35 + * implementation by providing simple test authorship and framework extension, consistency,
    1.36 + * and reliability.
    1.37 + *
    1.38 + * For documentation on writing tests and extending the framework, see
    1.39 + * https://wiki.mozilla.org/Mobile/Fennec/Android/UITest
    1.40 + */
    1.41 +abstract class UITest extends BaseRobocopTest
    1.42 +                      implements UITestContext {
    1.43 +
    1.44 +    private static final String JUNIT_FAILURE_MSG = "A JUnit method was called. Make sure " +
    1.45 +        "you are using AssertionHelper to make assertions. Try `fAssert*(...);`";
    1.46 +
    1.47 +    private Solo mSolo;
    1.48 +    private Driver mDriver;
    1.49 +    private Actions mActions;
    1.50 +
    1.51 +    // Base to build hostname URLs
    1.52 +    private String mBaseHostnameUrl;
    1.53 +    // Base to build IP URLs
    1.54 +    private String mBaseIpUrl;
    1.55 +
    1.56 +    protected AboutHomeComponent mAboutHome;
    1.57 +    protected AppMenuComponent mAppMenu;
    1.58 +    protected GeckoViewComponent mGeckoView;
    1.59 +    protected ToolbarComponent mToolbar;
    1.60 +
    1.61 +    @Override
    1.62 +    protected void setUp() throws Exception {
    1.63 +        super.setUp();
    1.64 +
    1.65 +        // Start the activity.
    1.66 +        final Intent intent = createActivityIntent(mConfig);
    1.67 +        setActivityIntent(intent);
    1.68 +        final Activity activity = getActivity();
    1.69 +
    1.70 +        mSolo = new Solo(getInstrumentation(), activity);
    1.71 +        mDriver = new FennecNativeDriver(activity, mSolo, mRootPath);
    1.72 +        mActions = new FennecNativeActions(activity, mSolo, getInstrumentation(), mAsserter);
    1.73 +
    1.74 +        mBaseHostnameUrl = ((String) mConfig.get("host")).replaceAll("(/$)", "");
    1.75 +        mBaseIpUrl = ((String) mConfig.get("rawhost")).replaceAll("(/$)", "");
    1.76 +
    1.77 +        // Helpers depend on components so initialize them first.
    1.78 +        initComponents();
    1.79 +        initHelpers();
    1.80 +    }
    1.81 +
    1.82 +    @Override
    1.83 +    public void tearDown() throws Exception {
    1.84 +        try {
    1.85 +            mAsserter.endTest();
    1.86 +            // request a force quit of the browser and wait for it to take effect
    1.87 +            GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent("Robocop:Quit", null));
    1.88 +            mSolo.sleep(7000);
    1.89 +            // if still running, finish activities as recommended by Robotium
    1.90 +            mSolo.finishOpenedActivities();
    1.91 +        } catch (Throwable e) {
    1.92 +            e.printStackTrace();
    1.93 +        }
    1.94 +
    1.95 +        super.tearDown();
    1.96 +    }
    1.97 +
    1.98 +    private void initComponents() {
    1.99 +        mAboutHome = new AboutHomeComponent(this);
   1.100 +        mAppMenu = new AppMenuComponent(this);
   1.101 +        mGeckoView = new GeckoViewComponent(this);
   1.102 +        mToolbar = new ToolbarComponent(this);
   1.103 +    }
   1.104 +
   1.105 +    private void initHelpers() {
   1.106 +        HelperInitializer.init(this);
   1.107 +    }
   1.108 +
   1.109 +    @Override
   1.110 +    public Solo getSolo() {
   1.111 +        return mSolo;
   1.112 +    }
   1.113 +
   1.114 +    @Override
   1.115 +    public Assert getAsserter() {
   1.116 +        return mAsserter;
   1.117 +    }
   1.118 +
   1.119 +    @Override
   1.120 +    public Driver getDriver() {
   1.121 +        return mDriver;
   1.122 +    }
   1.123 +
   1.124 +    @Override
   1.125 +    public Actions getActions() {
   1.126 +        return mActions;
   1.127 +    }
   1.128 +
   1.129 +    @Override
   1.130 +    public void dumpLog(final String logtag, final String message) {
   1.131 +        mAsserter.dumpLog(logtag + ": " + message);
   1.132 +    }
   1.133 +
   1.134 +    @Override
   1.135 +    public void dumpLog(final String logtag, final String message, final Throwable t) {
   1.136 +        mAsserter.dumpLog(logtag + ": " + message, t);
   1.137 +    }
   1.138 +
   1.139 +    @Override
   1.140 +    public BaseComponent getComponent(final ComponentType type) {
   1.141 +        switch (type) {
   1.142 +            case ABOUTHOME:
   1.143 +                return mAboutHome;
   1.144 +
   1.145 +            case APPMENU:
   1.146 +                return mAppMenu;
   1.147 +
   1.148 +            case GECKOVIEW:
   1.149 +                return mGeckoView;
   1.150 +
   1.151 +            case TOOLBAR:
   1.152 +                return mToolbar;
   1.153 +
   1.154 +            default:
   1.155 +                fail("Unknown component type, " + type + ".");
   1.156 +                return null; // Should not reach this statement but required by javac.
   1.157 +        }
   1.158 +    }
   1.159 +
   1.160 +    /**
   1.161 +     * Returns the test type. By default this returns MOCHITEST, but tests can override this
   1.162 +     * method in order to change the type of the test.
   1.163 +     */
   1.164 +    protected Type getTestType() {
   1.165 +        return Type.MOCHITEST;
   1.166 +    }
   1.167 +
   1.168 +    @Override
   1.169 +    public String getAbsoluteHostnameUrl(final String url) {
   1.170 +        return getAbsoluteUrl(mBaseHostnameUrl, url);
   1.171 +    }
   1.172 +
   1.173 +    @Override
   1.174 +    public String getAbsoluteIpUrl(final String url) {
   1.175 +        return getAbsoluteUrl(mBaseIpUrl, url);
   1.176 +    }
   1.177 +
   1.178 +    private String getAbsoluteUrl(final String baseUrl, final String url) {
   1.179 +        return baseUrl + "/" + url.replaceAll("(^/)", "");
   1.180 +    }
   1.181 +
   1.182 +    private static Intent createActivityIntent(final Map<String, String> config) {
   1.183 +        final Intent intent = new Intent(Intent.ACTION_MAIN);
   1.184 +
   1.185 +        final String profile = (String) config.get("profile");
   1.186 +        intent.putExtra("args", "-no-remote -profile " + profile);
   1.187 +
   1.188 +        final String envString = (String) config.get("envvars");
   1.189 +        if (!TextUtils.isEmpty(envString)) {
   1.190 +            final String[] envStrings = envString.split(",");
   1.191 +
   1.192 +            for (int iter = 0; iter < envStrings.length; iter++) {
   1.193 +                intent.putExtra("env" + iter, envStrings[iter]);
   1.194 +            }
   1.195 +        }
   1.196 +
   1.197 +        return intent;
   1.198 +    }
   1.199 +
   1.200 +    /**
   1.201 +     * Throws an Exception. Called from overridden JUnit methods to ensure JUnit assertions
   1.202 +     * are not accidentally used over AssertionHelper assertions (the latter of which contains
   1.203 +     * additional logging facilities for use in our test harnesses).
   1.204 +     */
   1.205 +    private static void junit() {
   1.206 +        throw new UnsupportedOperationException(JUNIT_FAILURE_MSG);
   1.207 +    }
   1.208 +
   1.209 +    // Note: inexplicably, javac does not think we're overriding these methods,
   1.210 +    // so we can't use the @Override annotation.
   1.211 +    public static void assertEquals(short e, short a) { junit(); }
   1.212 +    public static void assertEquals(String m, int e, int a) { junit(); }
   1.213 +    public static void assertEquals(String m, short e, short a) { junit(); }
   1.214 +    public static void assertEquals(char e, char a) { junit(); }
   1.215 +    public static void assertEquals(String m, String e, String a) { junit(); }
   1.216 +    public static void assertEquals(int e, int a) { junit(); }
   1.217 +    public static void assertEquals(String m, double e, double a, double delta) { junit(); }
   1.218 +    public static void assertEquals(String m, long e, long a) { junit(); }
   1.219 +    public static void assertEquals(byte e, byte a) { junit(); }
   1.220 +    public static void assertEquals(Object e, Object a) { junit(); }
   1.221 +    public static void assertEquals(boolean e, boolean a) { junit(); }
   1.222 +    public static void assertEquals(String m, float e, float a, float delta) { junit(); }
   1.223 +    public static void assertEquals(String m, boolean e, boolean a) { junit(); }
   1.224 +    public static void assertEquals(String e, String a) { junit(); }
   1.225 +    public static void assertEquals(float e, float a, float delta) { junit(); }
   1.226 +    public static void assertEquals(String m, byte e, byte a) { junit(); }
   1.227 +    public static void assertEquals(double e, double a, double delta) { junit(); }
   1.228 +    public static void assertEquals(String m, char e, char a) { junit(); }
   1.229 +    public static void assertEquals(String m, Object e, Object a) { junit(); }
   1.230 +    public static void assertEquals(long e, long a) { junit(); }
   1.231 +
   1.232 +    public static void assertFalse(String m, boolean c) { junit(); }
   1.233 +    public static void assertFalse(boolean c) { junit(); }
   1.234 +
   1.235 +    public static void assertNotNull(String m, Object o) { junit(); }
   1.236 +    public static void assertNotNull(Object o) { junit(); }
   1.237 +
   1.238 +    public static void assertNotSame(Object e, Object a) { junit(); }
   1.239 +    public static void assertNotSame(String m, Object e, Object a) { junit(); }
   1.240 +
   1.241 +    public static void assertNull(Object o) { junit(); }
   1.242 +    public static void assertNull(String m, Object o) { junit(); }
   1.243 +
   1.244 +    public static void assertSame(Object e, Object a) { junit(); }
   1.245 +    public static void assertSame(String m, Object e, Object a) { junit(); }
   1.246 +
   1.247 +    public static void assertTrue(String m, boolean c) { junit(); }
   1.248 +    public static void assertTrue(boolean c) { junit(); }
   1.249 +
   1.250 +    public static void fail(String m) { junit(); }
   1.251 +    public static void fail() { junit(); }
   1.252 +
   1.253 +    public static void failNotEquals(String m, Object e, Object a) { junit(); }
   1.254 +    public static void failNotSame(String m, Object e, Object a) { junit(); }
   1.255 +    public static void failSame(String m) { junit(); }
   1.256 +
   1.257 +    public static String format(String m, Object e, Object a) { junit(); return null; }
   1.258 +}

mercurial