Bug 1292426 - Rewrite browser_visituri_privatebrowsing_perwindowpb.js to use modern async facilities and ensure we await on the final check to avoid intermittents. r?mak draft
authorMark Banner <standard8@mozilla.com>
Mon, 17 Jul 2017 12:31:12 +0100
changeset 609805 6188622942801e0c288f1333137cbf2c9cad6318
parent 609768 d43779e278d2e4d3e21dba2fcb585a3bf4b1288e
child 637666 948f5041cab7fca5ae92af100fc53eaf00e2a4e4
push id68681
push userbmo:standard8@mozilla.com
push dateMon, 17 Jul 2017 14:14:46 +0000
reviewersmak
bugs1292426
milestone56.0a1
Bug 1292426 - Rewrite browser_visituri_privatebrowsing_perwindowpb.js to use modern async facilities and ensure we await on the final check to avoid intermittents. r?mak MozReview-Commit-ID: G2znd7sq5x5
toolkit/components/places/tests/browser/browser_visituri_privatebrowsing_perwindowpb.js
--- a/toolkit/components/places/tests/browser/browser_visituri_privatebrowsing_perwindowpb.js
+++ b/toolkit/components/places/tests/browser/browser_visituri_privatebrowsing_perwindowpb.js
@@ -1,73 +1,61 @@
 /* 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/. */
 
-function test() {
-  // initialization
-  waitForExplicitFinish();
-  let windowsToClose = [];
-  let initialURL =
-    "http://example.com/tests/toolkit/components/places/tests/browser/begin.html";
-  let finalURL =
-    "http://example.com/tests/toolkit/components/places/tests/browser/final.html";
-  let observer = null;
-  let enumerator = null;
-  let currentObserver = null;
-  let uri = null;
+const initialURL =
+  "http://example.com/tests/toolkit/components/places/tests/browser/begin.html";
+const finalURL =
+ "http://example.com/tests/toolkit/components/places/tests/browser/final.html";
+
+var observer;
+var visitSavedPromise;
 
-  function doTest(aIsPrivateMode, aWindow, aTestURI, aCallback) {
+add_task(async function setup() {
+  visitSavedPromise = new Promise(resolve => {
     observer = {
-      observe(aSubject, aTopic, aData) {
+      observe(subject, topic, data) {
         // The uri-visit-saved topic should only work when on normal mode.
-        if (aTopic == "uri-visit-saved") {
-          // Remove the observers set on per window private mode and normal
-          // mode.
-          enumerator = aWindow.Services.obs.enumerateObservers("uri-visit-saved");
-          while (enumerator.hasMoreElements()) {
-            currentObserver = enumerator.getNext();
-            aWindow.Services.obs.removeObserver(currentObserver, "uri-visit-saved");
-          }
+        if (topic == "uri-visit-saved") {
+          Services.obs.removeObserver(observer, "uri-visit-saved");
 
           // The expected visit should be the finalURL because private mode
           // should not register a visit with the initialURL.
-          uri = aSubject.QueryInterface(Ci.nsIURI);
-          is(uri.spec, finalURL, "Check received expected visit");
+          let uri = subject.QueryInterface(Ci.nsIURI);
+          resolve(uri.spec);
         }
       }
     };
-
-    aWindow.Services.obs.addObserver(observer, "uri-visit-saved");
-
-    BrowserTestUtils.browserLoaded(aWindow.gBrowser.selectedBrowser).then(aCallback);
-    aWindow.gBrowser.selectedBrowser.loadURI(aTestURI);
-  }
-
-  function testOnWindow(aOptions, aCallback) {
-    whenNewWindowLoaded(aOptions, function(aWin) {
-      windowsToClose.push(aWin);
-      // execute should only be called when need, like when you are opening
-      // web pages on the test. If calling executeSoon() is not necesary, then
-      // call whenNewWindowLoaded() instead of testOnWindow() on your test.
-      executeSoon(() => aCallback(aWin));
-    });
-  }
-
-   // This function is called after calling finish() on the test.
-  registerCleanupFunction(function() {
-    windowsToClose.forEach(function(aWin) {
-      aWin.close();
-    });
   });
 
-  // test first when on private mode
-  testOnWindow({private: true}, function(aWin) {
-    doTest(true, aWin, initialURL, function() {
-      // then test when not on private mode
-      testOnWindow({}, function(aWin2) {
-        doTest(false, aWin2, finalURL, function() {
-          PlacesTestUtils.clearHistory().then(finish);
-        });
-      });
-    });
+  Services.obs.addObserver(observer, "uri-visit-saved");
+
+  registerCleanupFunction(async function() {
+    await PlacesTestUtils.clearHistory()
   });
+});
+
+// Note: The private window test must be the first one to run, since we'll listen
+// to the first uri-visit-saved notification, and we expect this test to not
+// fire any, so we'll just find the non-private window test notification.
+add_task(async function test_private_browsing_window() {
+  await testLoadInWindow({private: true}, initialURL);
+});
+
+add_task(async function test_normal_window() {
+  await testLoadInWindow({private: false}, finalURL);
+
+  let url = await visitSavedPromise;
+  Assert.equal(url, finalURL, "Check received expected visit");
+});
+
+async function testLoadInWindow(options, url) {
+  let win = await BrowserTestUtils.openNewBrowserWindow(options);
+
+  registerCleanupFunction(async function() {
+    await BrowserTestUtils.closeWindow(win);
+  });
+
+  let loadedPromise = BrowserTestUtils.browserLoaded(win.gBrowser.selectedBrowser);
+  await BrowserTestUtils.loadURI(win.gBrowser.selectedBrowser, url);
+  await loadedPromise;
 }