Bug 1250739 - test_user_agent_updates.html should use notification instead of flaky setTimeout r=nwgh draft
authorValentin Gosu <valentin.gosu@gmail.com>
Mon, 04 Jul 2016 13:35:29 +0200
changeset 383498 1fa17ecf6274a4dfcf156f4d16477c95279d80c7
parent 382798 82e1f1b9c0559f38a8460e2f2f3044de4c7712d6
child 524498 b43732df5b904071b1e8164bb2fbc91fabe3920f
push id22044
push uservalentin.gosu@gmail.com
push dateMon, 04 Jul 2016 12:17:04 +0000
reviewersnwgh
bugs1250739
milestone50.0a1
Bug 1250739 - test_user_agent_updates.html should use notification instead of flaky setTimeout r=nwgh MozReview-Commit-ID: 9mA0pTRP6tV
netwerk/test/mochitests/test_user_agent_updates.html
--- a/netwerk/test/mochitests/test_user_agent_updates.html
+++ b/netwerk/test/mochitests/test_user_agent_updates.html
@@ -127,24 +127,29 @@ function getUpdateURL() {
   return url;
 }
 
 function testDownload(callback) {
   var startTime = Date.now();
   var url = getUpdateURL();
   isnot(navigator.userAgent, UA_OVERRIDE, 'UA already overridden');
   info('Waiting for UA update: ' + url);
+
+  chromeScript.sendAsyncMessage("notify-on-update");
   SpecialPowers.pushPrefEnv({
     set: [
       [PREF_UPDATES_ENABLED, true],
       [PREF_UPDATES_URL, url],
       [PREF_UPDATES_TIMEOUT, 10000],
       [PREF_UPDATES_INTERVAL, 1] // 1 second interval
     ]
-  }, function waitForUpdate() { setTimeout( function() {
+  });
+
+  function waitForUpdate() {
+    info("Update Happened");
     testUAIFrameNoNav(location.origin, UA_OVERRIDE, true, 'Header UA not overridden', function() {
       var updateTime = parseInt(getUA('http://example.org'));
       todo(startTime <= updateTime, 'Update was before start time');
       todo(updateTime <= Date.now(), 'Update was after present time');
 
       let overs = OVERRIDES;
       (function nextOverride() {
         val = overs.shift();
@@ -152,17 +157,19 @@ function testDownload(callback) {
            testUAIFrameNoNav(val.host, val.expected, true, 'Incorrect URL parameter: ' + val.override, function() {
             overs.length ? nextOverride() : callback();
           });
         } else {
           nextOverride();
         }
       })();
     });
-  }, 1500); });  // Timeout should be slightly larger than the update interval.
+  }
+
+  chromeScript.addMessageListener("useragent-update-complete", waitForUpdate);
 }
 
 function testBadUpdate(callback) {
   var url = getServerURL() + 'invalid-json';
   var prevOverride = navigator.userAgent;
   SpecialPowers.pushPrefEnv({
     set: [
       [PREF_UPDATES_URL, url],
@@ -203,16 +210,19 @@ SpecialPowers.pushPrefEnv({
 
 
 const chromeScript = SpecialPowers.loadChromeScript(_ => {
   // Enter update timer manager test mode
   Components.classes["@mozilla.org/updates/timer-manager;1"].getService(
     Components.interfaces.nsIObserver).observe(null, "utm-test-init", "");
 
   Components.utils.import("resource://gre/modules/UserAgentOverrides.jsm");
+
+  var _notifyOnUpdate = false;
+
   var UAO = UserAgentOverrides;
   UAO.uninit();
 
   Components.utils.import("resource://gre/modules/FileUtils.jsm");
   var FU = FileUtils;
 
   const { TextDecoder, TextEncoder, OS } = Components.utils.import("resource://gre/modules/osfile.jsm");
   var OSF = OS.File;
@@ -221,16 +231,25 @@ const chromeScript = SpecialPowers.loadC
   const KEY_APPDIR = "XCurProcD";
   const FILE_UPDATES = "ua-update.json";
 
   const UA_OVERRIDE = "DummyUserAgent";
   const UA_ALT_OVERRIDE = "AltUserAgent";
 
   const PREF_UPDATES = "general.useragent.updates.";
   const PREF_UPDATES_ENABLED = PREF_UPDATES + "enabled";
+  const PREF_UPDATES_LASTUPDATED = PREF_UPDATES + "lastupdated";
+
+  Components.utils.import("resource://gre/modules/Services.jsm");
+  Services.prefs.addObserver(PREF_UPDATES_LASTUPDATED, () => {
+    if (_notifyOnUpdate) {
+      _notifyOnUpdate = false; // Only notify once, for the first update.
+      sendAsyncMessage("useragent-update-complete");
+    }
+  } , false);
 
   var OVERRIDES = null;
 
   function is(value, expected, message) {
     sendAsyncMessage("is-message", {value, expected, message});
   }
 
   function info(message) {
@@ -292,16 +311,17 @@ const chromeScript = SpecialPowers.loadC
   }
 
 
   addMessageListener("testProfileSave", testProfileSave);
   addMessageListener("testProfileLoad", testProfileLoad);
   addMessageListener("set-overrides", function(overrides) { OVERRIDES = overrides});
   addMessageListener("UAO-init", function() { UAO.init(); });
   addMessageListener("UAO-uninit", function() { UAO.uninit(); });
+  addMessageListener("notify-on-update", () => { _notifyOnUpdate = true });
 });
 
 chromeScript.addMessageListener("testProfileSaveDone", SimpleTest.finish);
 chromeScript.addMessageListener("testProfileLoadDone", function() {
   SpecialPowers.pushPrefEnv({
     set: [[PREF_UPDATES_ENABLED, true]]
   }, function () {
     // initialize UserAgentOverrides.jsm and