mobile/android/base/tests/UITest.java

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 package org.mozilla.gecko.tests;
michael@0 6
michael@0 7 import java.util.Map;
michael@0 8
michael@0 9 import org.mozilla.gecko.Actions;
michael@0 10 import org.mozilla.gecko.Assert;
michael@0 11 import org.mozilla.gecko.Driver;
michael@0 12 import org.mozilla.gecko.FennecNativeActions;
michael@0 13 import org.mozilla.gecko.FennecNativeDriver;
michael@0 14 import org.mozilla.gecko.GeckoAppShell;
michael@0 15 import org.mozilla.gecko.GeckoEvent;
michael@0 16 import org.mozilla.gecko.tests.components.AboutHomeComponent;
michael@0 17 import org.mozilla.gecko.tests.components.AppMenuComponent;
michael@0 18 import org.mozilla.gecko.tests.components.BaseComponent;
michael@0 19 import org.mozilla.gecko.tests.components.GeckoViewComponent;
michael@0 20 import org.mozilla.gecko.tests.components.ToolbarComponent;
michael@0 21 import org.mozilla.gecko.tests.helpers.HelperInitializer;
michael@0 22
michael@0 23 import android.app.Activity;
michael@0 24 import android.content.Intent;
michael@0 25 import android.text.TextUtils;
michael@0 26
michael@0 27 import com.jayway.android.robotium.solo.Solo;
michael@0 28
michael@0 29 /**
michael@0 30 * A base test class for Robocop (UI-centric) tests. This and the related classes attempt to
michael@0 31 * provide a framework to improve upon the issues discovered with the previous BaseTest
michael@0 32 * implementation by providing simple test authorship and framework extension, consistency,
michael@0 33 * and reliability.
michael@0 34 *
michael@0 35 * For documentation on writing tests and extending the framework, see
michael@0 36 * https://wiki.mozilla.org/Mobile/Fennec/Android/UITest
michael@0 37 */
michael@0 38 abstract class UITest extends BaseRobocopTest
michael@0 39 implements UITestContext {
michael@0 40
michael@0 41 private static final String JUNIT_FAILURE_MSG = "A JUnit method was called. Make sure " +
michael@0 42 "you are using AssertionHelper to make assertions. Try `fAssert*(...);`";
michael@0 43
michael@0 44 private Solo mSolo;
michael@0 45 private Driver mDriver;
michael@0 46 private Actions mActions;
michael@0 47
michael@0 48 // Base to build hostname URLs
michael@0 49 private String mBaseHostnameUrl;
michael@0 50 // Base to build IP URLs
michael@0 51 private String mBaseIpUrl;
michael@0 52
michael@0 53 protected AboutHomeComponent mAboutHome;
michael@0 54 protected AppMenuComponent mAppMenu;
michael@0 55 protected GeckoViewComponent mGeckoView;
michael@0 56 protected ToolbarComponent mToolbar;
michael@0 57
michael@0 58 @Override
michael@0 59 protected void setUp() throws Exception {
michael@0 60 super.setUp();
michael@0 61
michael@0 62 // Start the activity.
michael@0 63 final Intent intent = createActivityIntent(mConfig);
michael@0 64 setActivityIntent(intent);
michael@0 65 final Activity activity = getActivity();
michael@0 66
michael@0 67 mSolo = new Solo(getInstrumentation(), activity);
michael@0 68 mDriver = new FennecNativeDriver(activity, mSolo, mRootPath);
michael@0 69 mActions = new FennecNativeActions(activity, mSolo, getInstrumentation(), mAsserter);
michael@0 70
michael@0 71 mBaseHostnameUrl = ((String) mConfig.get("host")).replaceAll("(/$)", "");
michael@0 72 mBaseIpUrl = ((String) mConfig.get("rawhost")).replaceAll("(/$)", "");
michael@0 73
michael@0 74 // Helpers depend on components so initialize them first.
michael@0 75 initComponents();
michael@0 76 initHelpers();
michael@0 77 }
michael@0 78
michael@0 79 @Override
michael@0 80 public void tearDown() throws Exception {
michael@0 81 try {
michael@0 82 mAsserter.endTest();
michael@0 83 // request a force quit of the browser and wait for it to take effect
michael@0 84 GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent("Robocop:Quit", null));
michael@0 85 mSolo.sleep(7000);
michael@0 86 // if still running, finish activities as recommended by Robotium
michael@0 87 mSolo.finishOpenedActivities();
michael@0 88 } catch (Throwable e) {
michael@0 89 e.printStackTrace();
michael@0 90 }
michael@0 91
michael@0 92 super.tearDown();
michael@0 93 }
michael@0 94
michael@0 95 private void initComponents() {
michael@0 96 mAboutHome = new AboutHomeComponent(this);
michael@0 97 mAppMenu = new AppMenuComponent(this);
michael@0 98 mGeckoView = new GeckoViewComponent(this);
michael@0 99 mToolbar = new ToolbarComponent(this);
michael@0 100 }
michael@0 101
michael@0 102 private void initHelpers() {
michael@0 103 HelperInitializer.init(this);
michael@0 104 }
michael@0 105
michael@0 106 @Override
michael@0 107 public Solo getSolo() {
michael@0 108 return mSolo;
michael@0 109 }
michael@0 110
michael@0 111 @Override
michael@0 112 public Assert getAsserter() {
michael@0 113 return mAsserter;
michael@0 114 }
michael@0 115
michael@0 116 @Override
michael@0 117 public Driver getDriver() {
michael@0 118 return mDriver;
michael@0 119 }
michael@0 120
michael@0 121 @Override
michael@0 122 public Actions getActions() {
michael@0 123 return mActions;
michael@0 124 }
michael@0 125
michael@0 126 @Override
michael@0 127 public void dumpLog(final String logtag, final String message) {
michael@0 128 mAsserter.dumpLog(logtag + ": " + message);
michael@0 129 }
michael@0 130
michael@0 131 @Override
michael@0 132 public void dumpLog(final String logtag, final String message, final Throwable t) {
michael@0 133 mAsserter.dumpLog(logtag + ": " + message, t);
michael@0 134 }
michael@0 135
michael@0 136 @Override
michael@0 137 public BaseComponent getComponent(final ComponentType type) {
michael@0 138 switch (type) {
michael@0 139 case ABOUTHOME:
michael@0 140 return mAboutHome;
michael@0 141
michael@0 142 case APPMENU:
michael@0 143 return mAppMenu;
michael@0 144
michael@0 145 case GECKOVIEW:
michael@0 146 return mGeckoView;
michael@0 147
michael@0 148 case TOOLBAR:
michael@0 149 return mToolbar;
michael@0 150
michael@0 151 default:
michael@0 152 fail("Unknown component type, " + type + ".");
michael@0 153 return null; // Should not reach this statement but required by javac.
michael@0 154 }
michael@0 155 }
michael@0 156
michael@0 157 /**
michael@0 158 * Returns the test type. By default this returns MOCHITEST, but tests can override this
michael@0 159 * method in order to change the type of the test.
michael@0 160 */
michael@0 161 protected Type getTestType() {
michael@0 162 return Type.MOCHITEST;
michael@0 163 }
michael@0 164
michael@0 165 @Override
michael@0 166 public String getAbsoluteHostnameUrl(final String url) {
michael@0 167 return getAbsoluteUrl(mBaseHostnameUrl, url);
michael@0 168 }
michael@0 169
michael@0 170 @Override
michael@0 171 public String getAbsoluteIpUrl(final String url) {
michael@0 172 return getAbsoluteUrl(mBaseIpUrl, url);
michael@0 173 }
michael@0 174
michael@0 175 private String getAbsoluteUrl(final String baseUrl, final String url) {
michael@0 176 return baseUrl + "/" + url.replaceAll("(^/)", "");
michael@0 177 }
michael@0 178
michael@0 179 private static Intent createActivityIntent(final Map<String, String> config) {
michael@0 180 final Intent intent = new Intent(Intent.ACTION_MAIN);
michael@0 181
michael@0 182 final String profile = (String) config.get("profile");
michael@0 183 intent.putExtra("args", "-no-remote -profile " + profile);
michael@0 184
michael@0 185 final String envString = (String) config.get("envvars");
michael@0 186 if (!TextUtils.isEmpty(envString)) {
michael@0 187 final String[] envStrings = envString.split(",");
michael@0 188
michael@0 189 for (int iter = 0; iter < envStrings.length; iter++) {
michael@0 190 intent.putExtra("env" + iter, envStrings[iter]);
michael@0 191 }
michael@0 192 }
michael@0 193
michael@0 194 return intent;
michael@0 195 }
michael@0 196
michael@0 197 /**
michael@0 198 * Throws an Exception. Called from overridden JUnit methods to ensure JUnit assertions
michael@0 199 * are not accidentally used over AssertionHelper assertions (the latter of which contains
michael@0 200 * additional logging facilities for use in our test harnesses).
michael@0 201 */
michael@0 202 private static void junit() {
michael@0 203 throw new UnsupportedOperationException(JUNIT_FAILURE_MSG);
michael@0 204 }
michael@0 205
michael@0 206 // Note: inexplicably, javac does not think we're overriding these methods,
michael@0 207 // so we can't use the @Override annotation.
michael@0 208 public static void assertEquals(short e, short a) { junit(); }
michael@0 209 public static void assertEquals(String m, int e, int a) { junit(); }
michael@0 210 public static void assertEquals(String m, short e, short a) { junit(); }
michael@0 211 public static void assertEquals(char e, char a) { junit(); }
michael@0 212 public static void assertEquals(String m, String e, String a) { junit(); }
michael@0 213 public static void assertEquals(int e, int a) { junit(); }
michael@0 214 public static void assertEquals(String m, double e, double a, double delta) { junit(); }
michael@0 215 public static void assertEquals(String m, long e, long a) { junit(); }
michael@0 216 public static void assertEquals(byte e, byte a) { junit(); }
michael@0 217 public static void assertEquals(Object e, Object a) { junit(); }
michael@0 218 public static void assertEquals(boolean e, boolean a) { junit(); }
michael@0 219 public static void assertEquals(String m, float e, float a, float delta) { junit(); }
michael@0 220 public static void assertEquals(String m, boolean e, boolean a) { junit(); }
michael@0 221 public static void assertEquals(String e, String a) { junit(); }
michael@0 222 public static void assertEquals(float e, float a, float delta) { junit(); }
michael@0 223 public static void assertEquals(String m, byte e, byte a) { junit(); }
michael@0 224 public static void assertEquals(double e, double a, double delta) { junit(); }
michael@0 225 public static void assertEquals(String m, char e, char a) { junit(); }
michael@0 226 public static void assertEquals(String m, Object e, Object a) { junit(); }
michael@0 227 public static void assertEquals(long e, long a) { junit(); }
michael@0 228
michael@0 229 public static void assertFalse(String m, boolean c) { junit(); }
michael@0 230 public static void assertFalse(boolean c) { junit(); }
michael@0 231
michael@0 232 public static void assertNotNull(String m, Object o) { junit(); }
michael@0 233 public static void assertNotNull(Object o) { junit(); }
michael@0 234
michael@0 235 public static void assertNotSame(Object e, Object a) { junit(); }
michael@0 236 public static void assertNotSame(String m, Object e, Object a) { junit(); }
michael@0 237
michael@0 238 public static void assertNull(Object o) { junit(); }
michael@0 239 public static void assertNull(String m, Object o) { junit(); }
michael@0 240
michael@0 241 public static void assertSame(Object e, Object a) { junit(); }
michael@0 242 public static void assertSame(String m, Object e, Object a) { junit(); }
michael@0 243
michael@0 244 public static void assertTrue(String m, boolean c) { junit(); }
michael@0 245 public static void assertTrue(boolean c) { junit(); }
michael@0 246
michael@0 247 public static void fail(String m) { junit(); }
michael@0 248 public static void fail() { junit(); }
michael@0 249
michael@0 250 public static void failNotEquals(String m, Object e, Object a) { junit(); }
michael@0 251 public static void failNotSame(String m, Object e, Object a) { junit(); }
michael@0 252 public static void failSame(String m) { junit(); }
michael@0 253
michael@0 254 public static String format(String m, Object e, Object a) { junit(); return null; }
michael@0 255 }

mercurial