michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: package org.mozilla.gecko.tests; michael@0: michael@0: import java.util.Map; michael@0: michael@0: import org.mozilla.gecko.Actions; michael@0: import org.mozilla.gecko.Assert; michael@0: import org.mozilla.gecko.Driver; michael@0: import org.mozilla.gecko.FennecNativeActions; michael@0: import org.mozilla.gecko.FennecNativeDriver; michael@0: import org.mozilla.gecko.GeckoAppShell; michael@0: import org.mozilla.gecko.GeckoEvent; michael@0: import org.mozilla.gecko.tests.components.AboutHomeComponent; michael@0: import org.mozilla.gecko.tests.components.AppMenuComponent; michael@0: import org.mozilla.gecko.tests.components.BaseComponent; michael@0: import org.mozilla.gecko.tests.components.GeckoViewComponent; michael@0: import org.mozilla.gecko.tests.components.ToolbarComponent; michael@0: import org.mozilla.gecko.tests.helpers.HelperInitializer; michael@0: michael@0: import android.app.Activity; michael@0: import android.content.Intent; michael@0: import android.text.TextUtils; michael@0: michael@0: import com.jayway.android.robotium.solo.Solo; michael@0: michael@0: /** michael@0: * A base test class for Robocop (UI-centric) tests. This and the related classes attempt to michael@0: * provide a framework to improve upon the issues discovered with the previous BaseTest michael@0: * implementation by providing simple test authorship and framework extension, consistency, michael@0: * and reliability. michael@0: * michael@0: * For documentation on writing tests and extending the framework, see michael@0: * https://wiki.mozilla.org/Mobile/Fennec/Android/UITest michael@0: */ michael@0: abstract class UITest extends BaseRobocopTest michael@0: implements UITestContext { michael@0: michael@0: private static final String JUNIT_FAILURE_MSG = "A JUnit method was called. Make sure " + michael@0: "you are using AssertionHelper to make assertions. Try `fAssert*(...);`"; michael@0: michael@0: private Solo mSolo; michael@0: private Driver mDriver; michael@0: private Actions mActions; michael@0: michael@0: // Base to build hostname URLs michael@0: private String mBaseHostnameUrl; michael@0: // Base to build IP URLs michael@0: private String mBaseIpUrl; michael@0: michael@0: protected AboutHomeComponent mAboutHome; michael@0: protected AppMenuComponent mAppMenu; michael@0: protected GeckoViewComponent mGeckoView; michael@0: protected ToolbarComponent mToolbar; michael@0: michael@0: @Override michael@0: protected void setUp() throws Exception { michael@0: super.setUp(); michael@0: michael@0: // Start the activity. michael@0: final Intent intent = createActivityIntent(mConfig); michael@0: setActivityIntent(intent); michael@0: final Activity activity = getActivity(); michael@0: michael@0: mSolo = new Solo(getInstrumentation(), activity); michael@0: mDriver = new FennecNativeDriver(activity, mSolo, mRootPath); michael@0: mActions = new FennecNativeActions(activity, mSolo, getInstrumentation(), mAsserter); michael@0: michael@0: mBaseHostnameUrl = ((String) mConfig.get("host")).replaceAll("(/$)", ""); michael@0: mBaseIpUrl = ((String) mConfig.get("rawhost")).replaceAll("(/$)", ""); michael@0: michael@0: // Helpers depend on components so initialize them first. michael@0: initComponents(); michael@0: initHelpers(); michael@0: } michael@0: michael@0: @Override michael@0: public void tearDown() throws Exception { michael@0: try { michael@0: mAsserter.endTest(); michael@0: // request a force quit of the browser and wait for it to take effect michael@0: GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent("Robocop:Quit", null)); michael@0: mSolo.sleep(7000); michael@0: // if still running, finish activities as recommended by Robotium michael@0: mSolo.finishOpenedActivities(); michael@0: } catch (Throwable e) { michael@0: e.printStackTrace(); michael@0: } michael@0: michael@0: super.tearDown(); michael@0: } michael@0: michael@0: private void initComponents() { michael@0: mAboutHome = new AboutHomeComponent(this); michael@0: mAppMenu = new AppMenuComponent(this); michael@0: mGeckoView = new GeckoViewComponent(this); michael@0: mToolbar = new ToolbarComponent(this); michael@0: } michael@0: michael@0: private void initHelpers() { michael@0: HelperInitializer.init(this); michael@0: } michael@0: michael@0: @Override michael@0: public Solo getSolo() { michael@0: return mSolo; michael@0: } michael@0: michael@0: @Override michael@0: public Assert getAsserter() { michael@0: return mAsserter; michael@0: } michael@0: michael@0: @Override michael@0: public Driver getDriver() { michael@0: return mDriver; michael@0: } michael@0: michael@0: @Override michael@0: public Actions getActions() { michael@0: return mActions; michael@0: } michael@0: michael@0: @Override michael@0: public void dumpLog(final String logtag, final String message) { michael@0: mAsserter.dumpLog(logtag + ": " + message); michael@0: } michael@0: michael@0: @Override michael@0: public void dumpLog(final String logtag, final String message, final Throwable t) { michael@0: mAsserter.dumpLog(logtag + ": " + message, t); michael@0: } michael@0: michael@0: @Override michael@0: public BaseComponent getComponent(final ComponentType type) { michael@0: switch (type) { michael@0: case ABOUTHOME: michael@0: return mAboutHome; michael@0: michael@0: case APPMENU: michael@0: return mAppMenu; michael@0: michael@0: case GECKOVIEW: michael@0: return mGeckoView; michael@0: michael@0: case TOOLBAR: michael@0: return mToolbar; michael@0: michael@0: default: michael@0: fail("Unknown component type, " + type + "."); michael@0: return null; // Should not reach this statement but required by javac. michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Returns the test type. By default this returns MOCHITEST, but tests can override this michael@0: * method in order to change the type of the test. michael@0: */ michael@0: protected Type getTestType() { michael@0: return Type.MOCHITEST; michael@0: } michael@0: michael@0: @Override michael@0: public String getAbsoluteHostnameUrl(final String url) { michael@0: return getAbsoluteUrl(mBaseHostnameUrl, url); michael@0: } michael@0: michael@0: @Override michael@0: public String getAbsoluteIpUrl(final String url) { michael@0: return getAbsoluteUrl(mBaseIpUrl, url); michael@0: } michael@0: michael@0: private String getAbsoluteUrl(final String baseUrl, final String url) { michael@0: return baseUrl + "/" + url.replaceAll("(^/)", ""); michael@0: } michael@0: michael@0: private static Intent createActivityIntent(final Map config) { michael@0: final Intent intent = new Intent(Intent.ACTION_MAIN); michael@0: michael@0: final String profile = (String) config.get("profile"); michael@0: intent.putExtra("args", "-no-remote -profile " + profile); michael@0: michael@0: final String envString = (String) config.get("envvars"); michael@0: if (!TextUtils.isEmpty(envString)) { michael@0: final String[] envStrings = envString.split(","); michael@0: michael@0: for (int iter = 0; iter < envStrings.length; iter++) { michael@0: intent.putExtra("env" + iter, envStrings[iter]); michael@0: } michael@0: } michael@0: michael@0: return intent; michael@0: } michael@0: michael@0: /** michael@0: * Throws an Exception. Called from overridden JUnit methods to ensure JUnit assertions michael@0: * are not accidentally used over AssertionHelper assertions (the latter of which contains michael@0: * additional logging facilities for use in our test harnesses). michael@0: */ michael@0: private static void junit() { michael@0: throw new UnsupportedOperationException(JUNIT_FAILURE_MSG); michael@0: } michael@0: michael@0: // Note: inexplicably, javac does not think we're overriding these methods, michael@0: // so we can't use the @Override annotation. michael@0: public static void assertEquals(short e, short a) { junit(); } michael@0: public static void assertEquals(String m, int e, int a) { junit(); } michael@0: public static void assertEquals(String m, short e, short a) { junit(); } michael@0: public static void assertEquals(char e, char a) { junit(); } michael@0: public static void assertEquals(String m, String e, String a) { junit(); } michael@0: public static void assertEquals(int e, int a) { junit(); } michael@0: public static void assertEquals(String m, double e, double a, double delta) { junit(); } michael@0: public static void assertEquals(String m, long e, long a) { junit(); } michael@0: public static void assertEquals(byte e, byte a) { junit(); } michael@0: public static void assertEquals(Object e, Object a) { junit(); } michael@0: public static void assertEquals(boolean e, boolean a) { junit(); } michael@0: public static void assertEquals(String m, float e, float a, float delta) { junit(); } michael@0: public static void assertEquals(String m, boolean e, boolean a) { junit(); } michael@0: public static void assertEquals(String e, String a) { junit(); } michael@0: public static void assertEquals(float e, float a, float delta) { junit(); } michael@0: public static void assertEquals(String m, byte e, byte a) { junit(); } michael@0: public static void assertEquals(double e, double a, double delta) { junit(); } michael@0: public static void assertEquals(String m, char e, char a) { junit(); } michael@0: public static void assertEquals(String m, Object e, Object a) { junit(); } michael@0: public static void assertEquals(long e, long a) { junit(); } michael@0: michael@0: public static void assertFalse(String m, boolean c) { junit(); } michael@0: public static void assertFalse(boolean c) { junit(); } michael@0: michael@0: public static void assertNotNull(String m, Object o) { junit(); } michael@0: public static void assertNotNull(Object o) { junit(); } michael@0: michael@0: public static void assertNotSame(Object e, Object a) { junit(); } michael@0: public static void assertNotSame(String m, Object e, Object a) { junit(); } michael@0: michael@0: public static void assertNull(Object o) { junit(); } michael@0: public static void assertNull(String m, Object o) { junit(); } michael@0: michael@0: public static void assertSame(Object e, Object a) { junit(); } michael@0: public static void assertSame(String m, Object e, Object a) { junit(); } michael@0: michael@0: public static void assertTrue(String m, boolean c) { junit(); } michael@0: public static void assertTrue(boolean c) { junit(); } michael@0: michael@0: public static void fail(String m) { junit(); } michael@0: public static void fail() { junit(); } michael@0: michael@0: public static void failNotEquals(String m, Object e, Object a) { junit(); } michael@0: public static void failNotSame(String m, Object e, Object a) { junit(); } michael@0: public static void failSame(String m) { junit(); } michael@0: michael@0: public static String format(String m, Object e, Object a) { junit(); return null; } michael@0: }