Bug 1253381 - Add JavascriptBridgeTest. r=sebastian draft
authorMichael Comella <michael.l.comella@gmail.com>
Thu, 03 Mar 2016 13:33:12 -0800
changeset 336601 49384ca95cecea2c9fca312957f8775c034da3a0
parent 336157 82e8f730b3e18f3be117c5a202f9fead8e7a9e78
child 336602 66f0881e7b7b94f719a8337112711fffadf894bb
push id12141
push usermichael.l.comella@gmail.com
push dateThu, 03 Mar 2016 21:39:01 +0000
reviewerssebastian
bugs1253381
milestone47.0a1
Bug 1253381 - Add JavascriptBridgeTest. r=sebastian MozReview-Commit-ID: HVBXqDmFVc0
mobile/android/tests/browser/robocop/src/org/mozilla/gecko/tests/JavascriptBridgeTest.java
new file mode 100644
--- /dev/null
+++ b/mobile/android/tests/browser/robocop/src/org/mozilla/gecko/tests/JavascriptBridgeTest.java
@@ -0,0 +1,47 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+package org.mozilla.gecko.tests;
+
+import org.mozilla.gecko.tests.helpers.GeckoHelper;
+import org.mozilla.gecko.tests.helpers.JavascriptBridge;
+import org.mozilla.gecko.tests.helpers.NavigationHelper;
+
+/**
+ * Extended to write tests using JavascriptBridge, which allows Java and JS to communicate back-and-forth.
+ * If you don't need back-and-forth communication, consider {@link JavascriptTest}.
+ *
+ * To write a test:
+ *   * Extend this class
+ *   * Add your javascript file to the base robocop directory (see where `testJavascriptBridge.js` is located)
+ *   * In the main test method, call {@link #blockForReadyAndLoadJS(String)} with your javascript file name
+ * (don't include the path) or if you're loading a non-harness url, be sure to call {@link GeckoHelper#blockForReady()}
+ *   * You can access js calls via the {@link #js} field
+ *     - Read {@link JavascriptBridge} javadoc for more information about using the API.
+ */
+public class JavascriptBridgeTest extends UITest {
+
+    // A protected field for easy access by sub-classes and a private field so
+    // we can close the JS bridge if the sub-classes re-assign the protected field.
+    // Not very Java-like but simpler than a method.
+    private JavascriptBridge _js;
+    protected JavascriptBridge js;
+
+    @Override
+    public void setUp() throws Exception {
+        super.setUp();
+        _js = new JavascriptBridge(this);
+        js = _js;
+    }
+
+    @Override
+    public void tearDown() throws Exception {
+        _js.disconnect();
+        super.tearDown();
+    }
+
+    protected void blockForReadyAndLoadJS(final String jsFilename) {
+        NavigationHelper.enterAndLoadUrl(mStringHelper.getHarnessUrlForJavascript(jsFilename));
+    }
+}