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