Bug 1262565 - Replace usage of timeouts in session store form data test with waiting for appropriate events. r=margaret
authorJan Henning <jh+bugzilla@buttercookie.de>
Wed, 06 Apr 2016 22:32:52 +0200
changeset 292396 ef111ba6ddf3962149355da51fc6bb8caf2bfcaf
parent 292395 9dc2be94c435ced2cae1c607a56bc9ec8995bd08
child 292397 dfd50ecc52914277b66cb4db758fce69950cde6c
push id18619
push userryanvm@gmail.com
push dateSat, 09 Apr 2016 19:51:30 +0000
treeherderfx-team@ef111ba6ddf3 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmargaret
bugs1262565
milestone48.0a1
Bug 1262565 - Replace usage of timeouts in session store form data test with waiting for appropriate events. r=margaret MozReview-Commit-ID: 55WuGx5P98c
mobile/android/components/SessionStore.js
mobile/android/tests/browser/chrome/test_session_form_data.html
--- a/mobile/android/components/SessionStore.js
+++ b/mobile/android/components/SessionStore.js
@@ -376,16 +376,19 @@ SessionStore.prototype = {
       if (length > this._maxTabsUndo)
         this._windows[aWindow.__SSID].closedTabs.splice(this._maxTabsUndo, length - this._maxTabsUndo);
 
       this._lastClosedTabIndex = aTabIndex;
 
       if (this._notifyClosedTabs) {
         this._sendClosedTabsToJava(aWindow);
       }
+
+      let evt = new Event("SSTabCloseProcessed", {"bubbles":true, "cancelable":false});
+      aBrowser.dispatchEvent(evt);
     }
   },
 
   onTabLoad: function ss_onTabLoad(aWindow, aBrowser) {
     // If this browser is being restored, skip any session save activity
     if (aBrowser.__SS_restore)
       return;
 
@@ -427,16 +430,18 @@ SessionStore.prototype = {
     } else {
       // When navigating via the forward/back buttons, Gecko restores
       // the form data all by itself and doesn't invoke any input events.
       // As _collectTabData() doesn't save any form data, we need to manually
       // capture it to bridge the time until the next input event arrives.
       this.onTabInput(aWindow, aBrowser);
     }
 
+    let evt = new Event("SSTabDataUpdated", {"bubbles":true, "cancelable":false});
+    aBrowser.dispatchEvent(evt);
     this.saveStateDelayed();
 
     this._updateCrashReportURL(aWindow);
   },
 
   onTabSelect: function ss_onTabSelect(aWindow, aBrowser) {
     if (this._loadState != STATE_RUNNING)
       return;
--- a/mobile/android/tests/browser/chrome/test_session_form_data.html
+++ b/mobile/android/tests/browser/chrome/test_session_form_data.html
@@ -23,31 +23,16 @@ Cu.import("resource://gre/modules/XPCOMU
 Cu.import("resource://gre/modules/Services.jsm");
 Cu.import("resource://gre/modules/Task.jsm");
 
 let gChromeWin;
 let gBrowserApp;
 
 setup_browser();
 
-// Make the timer global so it doesn't get GC'd
-let gTimer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
-
-function sleep(wait) {
-  return new Promise((resolve, reject) => {
-    dump("sleep start");
-    gTimer.initWithCallback({
-      notify: function () {
-        dump("sleep end");
-        resolve();
-      },
-    }, wait, gTimer.TYPE_ONE_SHOT);
-  });
-}
-
 function queryElement(contentWindow, data) {
   let frame = contentWindow;
   if (data.hasOwnProperty("frame")) {
     frame = contentWindow.frames[data.frame];
   }
 
   let doc = frame.document;
 
@@ -81,20 +66,16 @@ function setInputValue(browser, data) {
 
 function getInputValue(browser, data) {
   let input = queryElement(browser.contentWindow, data);
   return input.value;
 }
 
 let ss = Cc["@mozilla.org/browser/sessionstore;1"].getService(Ci.nsISessionStore);
 
-// Wait 2 seconds for the async Java/JS messsaging dance to settle down. We don't have
-// enough pre/post events, messages or notifications to handle this without a delay.
-const CLOSE_TAB_WAIT = 2000;
-
 function setup_browser() {
   gChromeWin = Services.wm.getMostRecentWindow("navigator:browser");
   gBrowserApp = gChromeWin.BrowserApp;
 }
 
 /**
  * This test ensures that form data collection respects the privacy level as
  * set by the user.
@@ -115,17 +96,17 @@ add_task(function* test_formdata() {
       yield promiseBrowserEvent(browser, "load");
 
       // Modify form data.
       setInputValue(browser, {id: "txt", value: OUTER_VALUE});
       setInputValue(browser, {id: "txt", value: INNER_VALUE, frame: 0});
 
       // Remove the tab.
       gBrowserApp.closeTab(tab);
-      yield sleep(CLOSE_TAB_WAIT);
+      yield promiseTabEvent(browser, "SSTabCloseProcessed");
     });
   }
 
   yield createAndRemoveTab();
   let state = ss.getClosedTabs(gChromeWin);
   let [{formdata}] = state;
   is(formdata.id.txt, OUTER_VALUE, "outer value is correct");
   is(formdata.children[0].id.txt, INNER_VALUE, "inner value is correct");
@@ -170,17 +151,17 @@ add_task(function* test_formdata2() {
       yield promiseBrowserEvent(browser, "load");
 
       // Modify form data.
       setInputValue(browser, {id: "txt", value: OUTER_VALUE});
       setInputValue(browser, {id: "txt", value: INNER_VALUE, frame: 0});
 
       // Remove the tab.
       gBrowserApp.closeTab(tab);
-      yield sleep(CLOSE_TAB_WAIT);
+      yield promiseTabEvent(browser, "SSTabCloseProcessed");
     });
   }
 
   yield createAndRemoveTab();
   let state = ss.getClosedTabs(gChromeWin);
   let [{formdata}] = state;
   is(formdata.id.txt, OUTER_VALUE, "outer value is correct");
   is(formdata.children[0].id.txt, INNER_VALUE, "inner value is correct");
@@ -225,29 +206,26 @@ add_task(function* test_formdata_navigat
       // Visit a different page.
       gBrowserApp.loadURI(otherURL, browser);
       yield promiseBrowserEvent(browser, "DOMContentLoaded");
       is(browser.currentURI.spec, otherURL, "navigated to a different page");
 
       // Go back.
       is(browser.canGoBack, true, "can go back");
       browser.goBack();
-      yield promiseTabEvent(gBrowserApp.deck, "pageshow");
+      yield promiseTabEvent(browser, "SSTabDataUpdated");
       is(browser.currentURI.spec, URL, "navigated back to form data page");
 
       // Make sure form data is still present.
       is(getInputValue(browser, {id: "txt"}), OUTER_VALUE, "outer value present after navigation");
       is(getInputValue(browser, {id: "txt", frame: 0}), INNER_VALUE, "inner value present after navigation");
 
-      // Allow the session store a bit of time to actually capture the form data.
-      yield sleep(CLOSE_TAB_WAIT);
-
       // Remove the tab.
       gBrowserApp.closeTab(tab);
-      yield sleep(CLOSE_TAB_WAIT);
+      yield promiseTabEvent(browser, "SSTabCloseProcessed");
     });
   }
 
   yield createNavigateAndRemoveTab();
   let state = ss.getClosedTabs(gChromeWin);
   let [{formdata}] = state;
   is(formdata.id.txt, OUTER_VALUE, "outer value is correct");
   is(formdata.children[0].id.txt, INNER_VALUE, "inner value is correct");