Bug 948782 - Make test_alerts_noobserve.html wait until its alerts have disappeared before finishing. r=wchen, a=test-only
authorCameron McCormack <cam@mcc.id.au>
Thu, 12 Dec 2013 13:08:35 +1100
changeset 176038 9c7bc5a8c72407b059b24a360057f85af5c10442
parent 176037 a994c03f25d40882c1a1852e9e8d781aa346f3c2
child 176039 b7124d82a8ef5d782e29cd479742a69c521187ee
push id445
push userffxbld
push dateMon, 10 Mar 2014 22:05:19 +0000
treeherdermozilla-release@dc38b741b04e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerswchen, test-only
bugs948782
milestone28.0a2
Bug 948782 - Make test_alerts_noobserve.html wait until its alerts have disappeared before finishing. r=wchen, a=test-only
toolkit/components/alerts/test/test_alerts_noobserve.html
--- a/toolkit/components/alerts/test/test_alerts_noobserve.html
+++ b/toolkit/components/alerts/test/test_alerts_noobserve.html
@@ -17,44 +17,60 @@
 <br>If there has been no crash when the notification (later) disappears, assume all is good.
 
 <pre id="test">
 <script class="testbody" type="text/javascript">
 
 const Cc = SpecialPowers.Cc;
 const Ci = SpecialPowers.Ci;
 
-var gNotificationIsAvailable;
+const wm = Cc["@mozilla.org/appshell/window-mediator;1"]
+             .getService(Ci.nsIWindowMediator);
+
+function anyXULAlertsVisible() {
+  var windows = wm.getEnumerator('alert:alert');
+  return windows.hasMoreElements();
+}
 
-if (!("@mozilla.org/alerts-service;1" in Cc)) {
-  todo(false, "Alerts service does not exist in this application");
-} else {
-  ok(true, "Alerts service exists in this application");
+function waitForAlertsThenFinish() {
+  if (anyXULAlertsVisible()) {
+    setTimeout(waitForAlertsThenFinish, 1000);
+  } else {
+    ok(true, "Alert disappeared.");
+    SimpleTest.finish();
+  }
+}
 
-  var notifier;
-  try {
-    notifier = Cc["@mozilla.org/alerts-service;1"].
-               getService(Ci.nsIAlertsService);
-    ok(true, "Alerts service is available");
-  } catch (ex) {
-    todo(false, "Alerts service is not available.", ex);
-  }
+function runTest() {
+  if (!("@mozilla.org/alerts-service;1" in Cc)) {
+    todo(false, "Alerts service does not exist in this application");
+  } else {
+    ok(true, "Alerts service exists in this application");
 
-  if (notifier) {
+    var notifier;
     try {
-      notifier.showAlertNotification(null, "Notification test",
-                                     "This notification has no observer");
-      ok(true, "showAlertNotification() succeeded");
+      notifier = Cc["@mozilla.org/alerts-service;1"].
+                 getService(Ci.nsIAlertsService);
+      ok(true, "Alerts service is available");
+    } catch (ex) {
+      todo(false, "Alerts service is not available.", ex);
+    }
 
-      gNotificationIsAvailable = true;
-    } catch (ex) {
-      todo(false, "showAlertNotification() failed.", ex);
+    if (notifier) {
+      try {
+        notifier.showAlertNotification(null, "Notification test",
+                                       "This notification has no observer");
+        ok(true, "showAlertNotification() succeeded");
+      } catch (ex) {
+        todo(false, "showAlertNotification() failed.", ex);
+      }
     }
   }
 }
 
-if (gNotificationIsAvailable) {
-  ok(true, "Notification should actually display during later tests... (Hope this doesn't disturb them!)");
-}
+SimpleTest.waitForExplicitFinish();
+ok(!anyXULAlertsVisible(), "Alerts should not be present at the start of the test.");
+runTest();
+setTimeout(waitForAlertsThenFinish, 1000);
 </script>
 </pre>
 </body>
 </html>