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, a=test-only
authorMark Banner <standard8@mozilla.com>
Mon, 17 Jul 2017 12:31:12 +0100
changeset 414347 eb96c581aa627afdbf884f4b686389f8283617bb
parent 414346 83004a183f631214b2c0375848fae11b5e690252
child 414348 a90f8e94849d6a2fa4f7ccf97c3168c9d4855ea9
push id1490
push usermtabara@mozilla.com
push dateMon, 31 Jul 2017 14:08:16 +0000
treeherdermozilla-release@70e32e6bf15e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmak, test-only
bugs1292426
milestone55.0
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, a=test-only 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;
 }