Bug 1250739 - test_user_agent_updates.html should use notification instead of flaky setTimeout. r=nwgh, a=test-only
authorValentin Gosu <valentin.gosu@gmail.com>
Mon, 04 Jul 2016 13:35:29 +0200
changeset 339862 94a1e8ddd89e184b5023aef6369f9340e956c418
parent 339861 f0751cffd0c729c9b5f4d6fbe9b14e274f90cf9b
child 339863 0e807dd32f4099f8a5ab55e78260e432c91f50da
push id6249
push userjlund@mozilla.com
push dateMon, 01 Aug 2016 13:59:36 +0000
treeherdermozilla-beta@bad9d4f5bf7e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersnwgh, test-only
bugs1250739
milestone49.0a2
Bug 1250739 - test_user_agent_updates.html should use notification instead of flaky setTimeout. r=nwgh, a=test-only 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