Bug 1042252 - Add some tests for NativeWindow APIs r=wesj
authorMark Finkle <mfinkle@mozilla.com>
Mon, 18 Aug 2014 17:46:10 -0400
changeset 200253 b1785455229890b2899e1a525070925b477267d7
parent 200252 bcf55557a6b42c2d4f3e5672dc17a224dbd3f7cd
child 200254 0a297d95632accea1e971d4514afdf113aebdd35
push id27338
push useremorley@mozilla.com
push dateTue, 19 Aug 2014 13:33:27 +0000
treeherdermozilla-central@cd2d406df655 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerswesj
bugs1042252
milestone34.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1042252 - Add some tests for NativeWindow APIs r=wesj
mobile/android/base/tests/robocop.ini
mobile/android/base/tests/roboextender/testNativeWindow.html
mobile/android/base/tests/testNativeWindow.java
--- a/mobile/android/base/tests/robocop.ini
+++ b/mobile/android/base/tests/robocop.ini
@@ -97,16 +97,17 @@ skip-if = android_version == "10"
 # Using JavascriptTest
 [testAccounts]
 [testAndroidLog]
 [testBrowserDiscovery]
 [testDebuggerServer]
 [testDeviceSearchEngine]
 [testJNI]
 # [testMozPay] # see bug 945675
+[testNativeWindow]
 [testOrderedBroadcast]
 [testResourceSubstitutions]
 [testSharedPreferences]
 [testSimpleDiscovery]
 [testUITelemetry]
 [testVideoDiscovery]
 
 # Used for Talos, please don't use in mochitest
new file mode 100644
--- /dev/null
+++ b/mobile/android/base/tests/roboextender/testNativeWindow.html
@@ -0,0 +1,113 @@
+<html>
+  <head>
+    <title>NativeWindow test page</title>
+    <meta name="viewport" content="initial-scale=1.0"/>
+    <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"></script>
+    <script type="application/javascript">
+Components.utils.import("resource://gre/modules/Messaging.jsm");
+Components.utils.import("resource://gre/modules/Services.jsm");
+
+const TOAST_TEXT = "TOAST!";
+const MENU_TEXT1 = "MENU-A";
+const MENU_TEXT2 = "MENU-B";
+const DOORHANGER_TEXT = "DOORHANGER";
+const DOORHANGER_BUTTON1 = "BUTTON1";
+const DOORHANGER_BUTTON2 = "BUTTON2";
+const DOORHANGER_CHECK = "CHECKBOX";
+
+// Entry point for the roboextender style tests. Called from <body onload>
+function start() {
+  // Grad the name of the test from the hash/anchor
+  var test = location.hash.substring(1);
+  window[test]();
+}
+
+// Show a toast and expect Java to watch for it to appear
+function showToast() {
+  // Signal Java that we are ready so Java can start looking for the toast text
+  sendMessageToJava({ type: "TestNativeWindow:ShowToast" });
+
+  // Show the toast
+  var chromeWin = Services.wm.getMostRecentWindow("navigator:browser");
+  chromeWin.NativeWindow.toast.show(TOAST_TEXT, "short");
+}
+
+// Add a menu and expect Java to find it and click it
+function addMenu() {
+  // Add the menu
+  var chromeWin = Services.wm.getMostRecentWindow("navigator:browser");
+  var menuID = chromeWin.NativeWindow.menu.add({
+    name: MENU_TEXT1,
+    icon: null,
+    callback: function() {
+      sendMessageToJava({ type: "TestNativeWindow:FireMenu" });
+    }
+  });
+
+  // Save the menuID for the next tests
+  Services.prefs.setIntPref("test.nativewindow.menuID", menuID);
+
+  // Signal Java that we are ready. We signal last so there is no race.
+  sendMessageToJava({ type: "TestNativeWindow:AddMenu" });
+}
+
+// Update the menu text and expect Java to find it and click it
+function updateMenu() {
+  // Get the menu's ID from storage
+  menuID = Services.prefs.getIntPref("test.nativewindow.menuID");
+
+  var chromeWin = Services.wm.getMostRecentWindow("navigator:browser");
+  chromeWin.NativeWindow.menu.update(menuID, {
+    name: MENU_TEXT2,
+  });
+
+  // Signal Java that we are ready. We signal last so there is no race.
+  sendMessageToJava({ type: "TestNativeWindow:UpdateMenu" });
+}
+
+// Remove the menu and expect Java to not find it
+function removeMenu() {
+  // Get the menu's ID from storage
+  menuID = Services.prefs.getIntPref("test.nativewindow.menuID");
+
+  // Remove the menu
+  var chromeWin = Services.wm.getMostRecentWindow("navigator:browser");
+  chromeWin.NativeWindow.menu.remove(menuID);
+
+  // Signal Java that we are ready. We signal last so there is no race.
+  sendMessageToJava({ type: "TestNativeWindow:RemoveMenu" });
+}
+
+// Add a doorhanger
+function addDoorhanger() {
+  var buttons = [
+    {
+      label: DOORHANGER_BUTTON1,
+      callback: function(aChecked) {
+        // If the user checked "Don't ask again", make a permanent exception
+        if (aChecked)
+          sendMessageToJava({ type: "TestNativeWindow:FireDoorhangerChecked" });
+        else
+          sendMessageToJava({ type: "TestNativeWindow:FireDoorhanger" });
+      }
+    },
+    {
+      label: DOORHANGER_BUTTON2,
+      callback: function(aChecked) {
+        // Do nothing
+      }
+    }
+  ];
+
+  // Show the doorhanger
+  var chromeWin = Services.wm.getMostRecentWindow("navigator:browser");
+  chromeWin.NativeWindow.doorhanger.show(DOORHANGER_TEXT, "test-nativewindow", buttons, null, { checkbox: DOORHANGER_CHECK });
+
+  // Signal Java that we are ready. We signal last so there is no race.
+  sendMessageToJava({ type: "TestNativeWindow:AddDoorhanger" });
+}
+    </script>
+  </head>
+  <body onload="start();">
+  </body>
+</html>
new file mode 100644
--- /dev/null
+++ b/mobile/android/base/tests/testNativeWindow.java
@@ -0,0 +1,130 @@
+package org.mozilla.gecko.tests;
+
+import org.mozilla.gecko.Actions;
+
+import android.widget.CheckBox;
+
+import java.util.ArrayList;
+
+public class testNativeWindow extends BaseTest {
+
+    private static final String TEST_URL = "chrome://roboextender/content/testNativeWindow.html";
+    private static final String TOAST_TEXT = "TOAST!";
+    private static final String MENU_TEXT1 = "MENU-A";
+    private static final String MENU_TEXT2 = "MENU-B";
+    private static final String DOORHANGER_TEXT = "DOORHANGER";
+    private static final String DOORHANGER_BUTTON1 = "BUTTON1";
+    private static final String DOORHANGER_BUTTON2 = "BUTTON2";
+    private static final String DOORHANGER_CHECK = "CHECKBOX";
+
+    public void testNativeWindow() {
+        blockForGeckoReady();
+
+        // NOTE: These test methods depend on being run in this order.
+        addToastTest();
+
+        prepNextTest();
+        addMenuTest();
+
+        prepNextTest();
+        updateMenuTest();
+
+        prepNextTest();
+        removeMenuTest();
+
+        prepNextTest();
+        addDoorhangerTest();
+    }
+
+    /**
+     * Load about:blank between each test to ensure we reset state.
+     */
+    private void prepNextTest() {
+        loadUrl("about:blank");
+        mAsserter.ok(waitForText("about:blank"), "Loaded blank page", "page title match");
+    }
+
+    /**
+     * Shows a toast, verifies that it appears when it should.
+     */
+    private void addToastTest() {
+        Actions.EventExpecter eventExpecter = mActions.expectGeckoEvent("TestNativeWindow:ShowToast");
+        loadUrl(TEST_URL + "#showToast");
+        eventExpecter.blockForEvent();
+
+        // Verify that the toast is visible with the correct text.
+        mAsserter.ok(waitForText(TOAST_TEXT), "Waiting for the toast", "Toast has been displayed");
+    }
+
+    /**
+     * Adds a menu and verifies the callback fires when clicked.
+     */
+    private void addMenuTest() {
+        Actions.EventExpecter eventExpecter = mActions.expectGeckoEvent("TestNativeWindow:AddMenu");
+        loadUrl(TEST_URL + "#addMenu");
+        eventExpecter.blockForEvent();
+
+        // Press the menu and wait for the callback to send a message
+        Actions.EventExpecter menuExpecter = mActions.expectGeckoEvent("TestNativeWindow:FireMenu");
+        selectMenuItem(MENU_TEXT1);
+        menuExpecter.blockForEvent();
+    }
+
+    /**
+     * Updates a menu and verifies the callback fires when clicked.
+     */
+    private void updateMenuTest() {
+        // Load about:home and make sure the onshown handler is called.
+        Actions.EventExpecter eventExpecter = mActions.expectGeckoEvent("TestNativeWindow:UpdateMenu");
+        loadUrl(TEST_URL + "#updateMenu");
+        eventExpecter.blockForEvent();
+
+        // Press the menu and wait for the callback to send a message
+        Actions.EventExpecter menuExpecter = mActions.expectGeckoEvent("TestNativeWindow:FireMenu");
+        selectMenuItem(MENU_TEXT2);
+        menuExpecter.blockForEvent();
+    }
+
+    /**
+     * Removes a menu and verifies the menu is not found.
+     */
+    private void removeMenuTest() {
+        Actions.EventExpecter eventExpecter = mActions.expectGeckoEvent("TestNativeWindow:RemoveMenu");
+        loadUrl(TEST_URL + "#removeMenu");
+        eventExpecter.blockForEvent();
+
+        // Do a simple search for the menu text on the main menu
+        mActions.sendSpecialKey(Actions.SpecialKey.MENU);
+        waitForText("^New Tab$");
+        mAsserter.ok(mSolo.searchText("^" + MENU_TEXT2 + "$") == false, "Checking for menu", "Menu has been removed");
+
+        // Close the menu, if it's still open
+        if (mSolo.searchText("^New Tab$")) {
+            mActions.sendSpecialKey(Actions.SpecialKey.BACK);
+        }
+    }
+
+    /**
+     * Adds a doorhanger and verifies the callback fires when clicked.
+     */
+    private void addDoorhangerTest() {
+        Actions.EventExpecter eventExpecter = mActions.expectGeckoEvent("TestNativeWindow:AddDoorhanger");
+        loadUrl(TEST_URL + "#addDoorhanger");
+        eventExpecter.blockForEvent();
+
+        // Press the doorhanger button and wait for the callback to send a message
+        Actions.EventExpecter menuExpecter = mActions.expectGeckoEvent("TestNativeWindow:FireDoorhanger");
+        waitForText(DOORHANGER_TEXT);
+
+        // Uncheck the checkbox
+        ArrayList<CheckBox> checkBoxes = mSolo.getCurrentViews(CheckBox.class);
+        mAsserter.ok(checkBoxes.size() == 1, "checkbox count", "only one checkbox visible");
+        mAsserter.ok(mSolo.isCheckBoxChecked(0), "checkbox checked", "checkbox is checked");
+        mSolo.clickOnCheckBox(0);
+        mAsserter.ok(!mSolo.isCheckBoxChecked(0), "checkbox not checked", "checkbox is not checked");
+
+        mSolo.clickOnText(DOORHANGER_BUTTON1);
+
+        menuExpecter.blockForEvent();
+    }
+}