Bug 806711 - Port do_test_placesTitleNoUpdate.js to the new per-window PB APIs; r=jdm
authorRaymond Lee <raymond@raysquare.com>
Fri, 14 Dec 2012 16:24:59 +0800
changeset 125183 037377a4cd237a8aa3acb73ef9532b93c41d37c2
parent 125182 95d8473212953b3eb9ec036b58293e224fdee60f
child 125184 627a842914d0132d9177a522e0c9708e6ee27ae7
push id2151
push userlsblakk@mozilla.com
push dateTue, 19 Feb 2013 18:06:57 +0000
treeherdermozilla-beta@4952e88741ec [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjdm
bugs806711
milestone20.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 806711 - Port do_test_placesTitleNoUpdate.js to the new per-window PB APIs; r=jdm
browser/components/privatebrowsing/test/browser/perwindow/Makefile.in
browser/components/privatebrowsing/test/browser/perwindow/browser_privatebrowsing_placesTitleNoUpdate.html
browser/components/privatebrowsing/test/browser/perwindow/browser_privatebrowsing_placesTitleNoUpdate.js
--- a/browser/components/privatebrowsing/test/browser/perwindow/Makefile.in
+++ b/browser/components/privatebrowsing/test/browser/perwindow/Makefile.in
@@ -29,16 +29,18 @@ MOCHITEST_BROWSER_FILES =  \
 		browser_privatebrowsing_localStorage_before_after_page.html \
 		browser_privatebrowsing_localStorage_before_after_page2.html \
 		browser_privatebrowsing_localStorage_page1.html \
 		browser_privatebrowsing_localStorage_page2.html \
 		browser_privatebrowsing_opendir.js \
 		browser_privatebrowsing_openlocation.js \
 		browser_privatebrowsing_openLocationLastURL.js \
 		browser_privatebrowsing_placestitle.js \
+		browser_privatebrowsing_placesTitleNoUpdate.js \
+		browser_privatebrowsing_placesTitleNoUpdate.html \
 		browser_privatebrowsing_popupblocker.js \
 		browser_privatebrowsing_protocolhandler.js \
 		browser_privatebrowsing_protocolhandler_page.html \
 		browser_privatebrowsing_theming.js \
 		browser_privatebrowsing_ui.js \
 		browser_privatebrowsing_urlbarfocus.js \
 		browser_privatebrowsing_windowtitle.js \
 		browser_privatebrowsing_windowtitle_page.html \
new file mode 100644
--- /dev/null
+++ b/browser/components/privatebrowsing/test/browser/perwindow/browser_privatebrowsing_placesTitleNoUpdate.html
@@ -0,0 +1,8 @@
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
+<html>
+  <head>
+    <title>Title 1</title>
+  </head>
+  <body>
+  </body>
+</html>
copy from browser/components/privatebrowsing/test/unit/do_test_placesTitleNoUpdate.js
copy to browser/components/privatebrowsing/test/browser/perwindow/browser_privatebrowsing_placesTitleNoUpdate.js
--- a/browser/components/privatebrowsing/test/unit/do_test_placesTitleNoUpdate.js
+++ b/browser/components/privatebrowsing/test/browser/perwindow/browser_privatebrowsing_placesTitleNoUpdate.js
@@ -3,58 +3,122 @@
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 // Test to make sure that the visited page titles do not get updated inside the
 // private browsing mode.
 
 Cu.import("resource://gre/modules/Services.jsm");
 Cu.import("resource://gre/modules/PlacesUtils.jsm");
 
-function do_test()
-{
-  let pb = Cc[PRIVATEBROWSING_CONTRACT_ID].
-           getService(Ci.nsIPrivateBrowsingService);
-
-  const TEST_URI = uri("http://mozilla.com/privatebrowsing");
+function test() {
+  waitForExplicitFinish();
+  const TEST_URL = "http://mochi.test:8888/browser/browser/components/privatebrowsing/test/browser/perwindow/browser_privatebrowsing_placesTitleNoUpdate.html"
+  const TEST_URI = Services.io.newURI(TEST_URL, null, null);
   const TITLE_1 = "Title 1";
   const TITLE_2 = "Title 2";
 
-  do_test_pending();
+  let selectedWin = null;
+  let windowsToClose = [];
+  let tabToClose = null;
+  let testNumber = 0;
+  let historyObserver;
+
+
+  registerCleanupFunction(function() {
+    PlacesUtils.history.removeObserver(historyObserver, false);
+    windowsToClose.forEach(function(aWin) {
+      aWin.close();
+    });
+    gBrowser.removeTab(tabToClose);
+  });
+
+
   waitForClearHistory(function () {
+    historyObserver = {
+      onTitleChanged: function(aURI, aPageTitle) {
+        switch (++testNumber) {
+          case 1:
+            afterFirstVisit();
+          break;
+          case 2:
+            afterUpdateVisit();
+          break;
+        }
+      },
+      onBeginUpdateBatch: function () {},
+      onEndUpdateBatch: function () {},
+      onVisit: function () {},
+      onBeforeDeleteURI: function () {},
+      onDeleteURI: function () {},
+      onClearHistory: function () {},
+      onPageChanged: function () {},
+      onDeleteVisits: function() {},
+      QueryInterface: XPCOMUtils.generateQI([Ci.nsINavHistoryObserver])
+    };
+    PlacesUtils.history.addObserver(historyObserver, false);
+
+    tabToClose = gBrowser.addTab();
+    gBrowser.selectedTab = tabToClose;
+    whenPageLoad(window, function() {});
+  });
+
+  function afterFirstVisit() {
+    is(PlacesUtils.history.getPageTitle(TEST_URI), TITLE_1, "The title matches the orignal title after first visit");
+
     let place = {
       uri: TEST_URI,
-      title: TITLE_1,
+      title: TITLE_2,
       visits: [{
         visitDate: Date.now() * 1000,
         transitionType: Ci.nsINavHistoryService.TRANSITION_LINK
       }]
     };
     PlacesUtils.asyncHistory.updatePlaces(place, {
       handleError: function () do_throw("Unexpected error in adding visit."),
       handleResult: function () { },
-      handleCompletion: function () afterAddFirstVisit()
+      handleCompletion: function () {}
     });
-  });
+  }
+
+  function afterUpdateVisit() {
+    is(PlacesUtils.history.getPageTitle(TEST_URI), TITLE_2, "The title matches the updated title after updating visit");
+
+    testOnWindow(true, function(aWin) {
+      whenPageLoad(aWin, function() {
+        executeSoon(afterFirstVisitInPrivateWindow);
+      });
+    });
+  }
+
+  function afterFirstVisitInPrivateWindow() {
+     is(PlacesUtils.history.getPageTitle(TEST_URI), TITLE_2, "The title remains the same after visiting in private window");
+     waitForClearHistory(finish);
+  }
 
-  function afterAddFirstVisit()
-  {
-    do_check_eq(PlacesUtils.history.getPageTitle(TEST_URI), TITLE_1);
+  function whenPageLoad(aWin, aCallback) {
+    aWin.gBrowser.selectedBrowser.addEventListener("load", function onLoad() {
+      aWin.gBrowser.selectedBrowser.removeEventListener("load", onLoad, true);
+      aCallback();
+    }, true);
+    aWin.gBrowser.selectedBrowser.loadURI(TEST_URL);
+  }
 
-    waitForClearHistory(do_test_finished);
+  function testOnWindow(aPrivate, aCallback) {
+    whenNewWindowLoaded({ private: aPrivate }, function(aWin) {
+      selectedWin = aWin;
+      windowsToClose.push(aWin);
+      executeSoon(function() { aCallback(aWin) });
+    });
+  }
+
+  function waitForClearHistory(aCallback) {
+    let observer = {
+      observe: function(aSubject, aTopic, aData) {
+        Services.obs.removeObserver(this, PlacesUtils.TOPIC_EXPIRATION_FINISHED);
+        aCallback();
+      }
+    };
+    Services.obs.addObserver(observer, PlacesUtils.TOPIC_EXPIRATION_FINISHED, false);
+
+    PlacesUtils.bhistory.removeAllPages();
   }
 }
 
-// Support running tests on both the service itself and its wrapper
-function run_test() {
-  run_test_on_all_services();
-}
-
-function waitForClearHistory(aCallback) {
-  let observer = {
-    observe: function(aSubject, aTopic, aData) {
-      Services.obs.removeObserver(this, PlacesUtils.TOPIC_EXPIRATION_FINISHED);
-      aCallback();
-    }
-  };
-  Services.obs.addObserver(observer, PlacesUtils.TOPIC_EXPIRATION_FINISHED, false);
-
-  PlacesUtils.bhistory.removeAllPages();
-}