Bug 811307 - [PATCH 2/2] [AccessFu] Add mochitest for enabling. Tear-down bits by MarcoZ. r=eeejay r=marcoz
authorYura Zenevich <yura.zenevich@gmail.com>
Tue, 23 Apr 2013 10:39:15 -0700
changeset 140608 8f39ea436f1dd8240682b5e8d7459833aed4592e
parent 140607 6915e00b6d62c8d7b58c55d3e3ad69e7b5a4b4fa
child 140609 d715a456d2cc49f3931871c2847c319db2fd20ab
push id2579
push userakeybl@mozilla.com
push dateMon, 24 Jun 2013 18:52:47 +0000
treeherdermozilla-beta@b69b7de8a05a [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerseeejay, marcoz
bugs811307
milestone23.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 811307 - [PATCH 2/2] [AccessFu] Add mochitest for enabling. Tear-down bits by MarcoZ. r=eeejay r=marcoz
accessible/tests/mochitest/common.js
accessible/tests/mochitest/jsat/Makefile.in
accessible/tests/mochitest/jsat/jsatcommon.js
accessible/tests/mochitest/jsat/test_alive.html
--- a/accessible/tests/mochitest/common.js
+++ b/accessible/tests/mochitest/common.js
@@ -657,16 +657,33 @@ function shortenString(aString, aMaxLeng
 
   // Trim the string if its length is > MAX_TRIM_LENGTH characters.
   var trimOffset = MAX_TRIM_LENGTH / 2;
   return aString.substring(0, trimOffset - 1) + "..." +
     aString.substring(aString.length - trimOffset, aString.length);
 }
 
 ////////////////////////////////////////////////////////////////////////////////
+// General Utils
+////////////////////////////////////////////////////////////////////////////////
+/**
+ * Return main chrome window (crosses chrome boundary)
+ */
+function getMainChromeWindow(aWindow)
+{
+  return aWindow.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
+                .getInterface(Components.interfaces.nsIWebNavigation)
+                .QueryInterface(Components.interfaces.nsIDocShellTreeItem)
+                .rootTreeItem
+                .QueryInterface(Components.interfaces.nsIInterfaceRequestor)
+                .getInterface(Components.interfaces.nsIDOMWindow);
+}
+
+
+////////////////////////////////////////////////////////////////////////////////
 // Private
 ////////////////////////////////////////////////////////////////////////////////
 
 ////////////////////////////////////////////////////////////////////////////////
 // Accessible general
 
 function getNodePrettyName(aNode)
 {
--- a/accessible/tests/mochitest/jsat/Makefile.in
+++ b/accessible/tests/mochitest/jsat/Makefile.in
@@ -7,12 +7,14 @@ DEPTH = @DEPTH@
 topsrcdir = @top_srcdir@
 srcdir = @srcdir@
 VPATH = @srcdir@
 relativesrcdir = @relativesrcdir@
 
 include $(DEPTH)/config/autoconf.mk
 
 MOCHITEST_A11Y_FILES =\
+jsatcommon.js \
+test_alive.html \
 test_utterance_order.html \
 $(NULL)
 
 include $(topsrcdir)/config/rules.mk
new file mode 100644
--- /dev/null
+++ b/accessible/tests/mochitest/jsat/jsatcommon.js
@@ -0,0 +1,91 @@
+// A common module to run tests on the AccessFu module
+
+/**
+  * A global variable holding an array of test functions.
+  */
+var gTestFuncs = [];
+/**
+  * A global Iterator for the array of test functions.
+  */
+var gIterator;
+
+Components.utils.import('resource://gre/modules/Services.jsm');
+Components.utils.import("resource://gre/modules/accessibility/Utils.jsm");
+Components.utils.import("resource://gre/modules/accessibility/EventManager.jsm");
+
+var AccessFuTest = {
+
+  addFunc: function AccessFuTest_addFunc(aFunc) {
+    if (aFunc) {
+      gTestFuncs.push(aFunc);
+    }
+  },
+
+  _waitForExplicitFinish: false,
+
+  waitForExplicitFinish: function AccessFuTest_waitForExplicitFinish() {
+    this._waitForExplicitFinish = true;
+  },
+
+  finish: function AccessFuTest_finish() {
+    // Disable the console service logging.
+    Logger.test = false;
+    AccessFu.doneCallback = function doneCallback() {
+      // This is being called once AccessFu has been shut down.
+      // Detach AccessFu from everything it attached itself to.
+      AccessFu.detach();
+      // and finish the test run.
+      SimpleTest.finish();
+    };
+    // Tear down accessibility and make AccessFu stop.
+    SpecialPowers.setIntPref("accessibility.accessfu.activate", 0);
+  },
+
+  nextTest: function AccessFuTest_nextTest() {
+    var testFunc;
+    try {
+      // Get the next test function from the iterator. If none left,
+      // StopIteration exception is thrown.
+      testFunc = gIterator.next()[1];
+    } catch (ex) {
+      // StopIteration exception.
+      this.finish();
+      return;
+    }
+    testFunc();
+  },
+
+  runTests: function AccessFuTest_runTests() {
+    if (gTestFuncs.length === 0) {
+      ok(false, "No tests specified!");
+      simpleTest.finish();
+      return;
+    }
+
+    // Create an Iterator for gTestFuncs array.
+    gIterator = Iterator(gTestFuncs);
+
+    // Start AccessFu and put it in stand-by.
+    Components.utils.import("resource://gre/modules/accessibility/AccessFu.jsm");
+
+    AccessFu.attach(getMainChromeWindow(window));
+
+    AccessFu.readyCallback = function readyCallback() {
+      // Enable logging to the console service.
+      Logger.test = true;
+      // This is being called once accessibility has been turned on.
+
+      if (AccessFuTest._waitForExplicitFinish) {
+        // Run all test functions asynchronously.
+        AccessFuTest.nextTest();
+      } else {
+        // Run all test functions synchronously.
+        [testFunc() for (testFunc of gTestFuncs)];
+        AccessFuTest.finish();
+      }
+    };
+
+    // Invoke the whole thing.
+    SpecialPowers.setIntPref("accessibility.accessfu.activate", 1);
+  }
+};
new file mode 100644
--- /dev/null
+++ b/accessible/tests/mochitest/jsat/test_alive.html
@@ -0,0 +1,81 @@
+<html>
+
+<head>
+  <title>AccessFu test for enabling</title>
+
+  <link rel="stylesheet" type="text/css"
+        href="chrome://mochikit/content/tests/SimpleTest/test.css" />
+  <script type="application/javascript"
+          src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
+  <script type="application/javascript"
+          src="../common.js"></script>
+  <script type="application/javascript"
+          src="./jsatcommon.js"></script>
+  <script type="application/javascript">
+
+    function confirmAccessFuStart() {
+      ok(AccessFu._enabled, "AccessFu was started and enabled.");
+      AccessFuTest.nextTest();
+    }
+
+    function makeEventManagerListener(waitForMessage, callback) {
+      return {
+        observe: function observe(aMessage) {
+          // Ignore unexpected messages.
+          if (!(aMessage instanceof Components.interfaces.nsIConsoleMessage)) {
+            return;
+          }
+          if (aMessage.message.indexOf(waitForMessage) < 0) {
+            return;
+          }
+          Services.console.unregisterListener(this);
+          callback();
+        }
+      };
+    }
+
+    function testEventManagerStartStop() {
+      // Firs listen for initial 'EventManager.start' and disable AccessFu.
+      var initialStartListener = makeEventManagerListener("EventManager.start",
+        function () {
+          ok(EventManager._started, "EventManager was started.");
+          Services.console.registerListener(stopListener);
+          AccessFu._disable();
+        });
+      // Listen for 'EventManager.stop' and enable AccessFu again.
+      var stopListener = makeEventManagerListener("EventManager.stop",
+        function () {
+          isnot(EventManager._started, true, "EventManager was stopped.");
+          Services.console.registerListener(finalStartListener);
+          AccessFu._enable();
+        });
+      // Make sure EventManager is started again.
+      var finalStartListener = makeEventManagerListener("EventManager.start",
+        function () {
+          ok(EventManager._started, "EventManager was started again.");
+          AccessFuTest.finish();
+        });
+
+      Services.console.registerListener(initialStartListener);
+    }
+
+    function doTest() {
+      AccessFuTest.addFunc(confirmAccessFuStart);
+      AccessFuTest.addFunc(testEventManagerStartStop);
+      AccessFuTest.waitForExplicitFinish();
+      AccessFuTest.runTests();  // Will call SimpleTest.finish();
+    }
+
+    SimpleTest.waitForExplicitFinish();
+    addA11yLoadEvent(doTest);
+  </script>
+
+</head>
+<body>
+  <a target="_blank"
+     href="https://bugzilla.mozilla.org/show_bug.cgi?id=811307"
+     title="[AccessFu] Add mochitest for enabling">
+    Mozilla Bug 811307
+  </a>
+</body>
+</html>
\ No newline at end of file