Bug 1148544 - Update tests to work with new way of handling user agent overrides. r=jchen
☠☠ backed out by ee4201100b15 ☠ ☠
authorDylan Roeh <droeh@mozilla.com>
Tue, 09 Feb 2016 13:50:44 -0600
changeset 283681 5073d841f77d266cdd0a790a31e5d38d31d8d547
parent 283680 64a2c41c6f204bf9b413f6976e84d9b5c01b508d
child 283682 038d8e322378574d96d44fd6c2ebd2ac49224d90
push id29988
push usercbook@mozilla.com
push dateWed, 10 Feb 2016 10:47:59 +0000
treeherdermozilla-central@7042e8a19f94 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjchen
bugs1148544
milestone47.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 1148544 - Update tests to work with new way of handling user agent overrides. r=jchen
netwerk/test/mochitests/test_user_agent_overrides.html
netwerk/test/mochitests/test_user_agent_updates.html
--- a/netwerk/test/mochitests/test_user_agent_overrides.html
+++ b/netwerk/test/mochitests/test_user_agent_overrides.html
@@ -24,106 +24,89 @@ const UA_WHOLE_OVERRIDE = "DummyUserAgen
 const UA_WHOLE_EXPECTED = UA_WHOLE_OVERRIDE;
 
 const UA_PARTIAL_FROM = "\\wozilla"; // /\wozilla
 const UA_PARTIAL_SEP = "#";
 const UA_PARTIAL_TO = UA_WHOLE_OVERRIDE;
 const UA_PARTIAL_OVERRIDE = UA_PARTIAL_FROM + UA_PARTIAL_SEP + UA_PARTIAL_TO;
 const UA_PARTIAL_EXPECTED = DEFAULT_UA.replace(new RegExp(UA_PARTIAL_FROM, 'g'), UA_PARTIAL_TO);
 
-function getUA(host) {
-  var url = location.pathname;
+function testUATab(host, expected, sameQ, message, testNavQ, navSameQ, navMessage, callback) {
+  let url = location.pathname;
   url = host + url.slice(0, url.lastIndexOf('/')) + '/user_agent.sjs';
+  let uri = Services.io.newURI(url, null, null);
+  let tab = BrowserApp.addTab(uri.spec, { selected: true, parentId: BrowserApp.selectedTab.id });
+  let browser = tab.browser;
 
-  var xhr = new XMLHttpRequest();
-  xhr.open('GET', url, false); // sync request
-  xhr.send();
-  is(xhr.status, 200, 'request failed');
-  is(typeof xhr.response, 'string', 'invalid response');
-  return xhr.response;
+  browser.addEventListener("load", function handle() {
+    ok(sameQ == (browser.contentDocument.body.innerHTML.indexOf(expected) != -1), message);
+    if(testNavQ) {
+      ok(navSameQ == (browser.contentWindow.navigator.userAgent.indexOf(expected) != -1), navMessage);
+    }
+    browser.removeEventListener("load", handle, true);
+    BrowserApp.closeTab(tab);
+    callback();
+  }, true);
+}
+
+function testUATabNoNav(host, expected, sameQ, message, callback) {
+  testUATab(host, expected, sameQ, message, false, true, '', callback);
 }
 
 function testUA(options, callback) {
-
   var [domain, override, test_hosts, expected] =
     [options.domain, options.override, options.test_hosts, options.expected];
 
-  info('Overriding ' + domain + ' with ' + override);
+  (function nextTest() {
+    let test_host = test_hosts.shift();
 
-  function is_subdomain(host) {
-    var [test_domain] = host.slice(host.lastIndexOf('/') + 1).split(':', 1);
-    return test_domain === domain || test_domain.endsWith('.' + domain);
-  }
-
-  var localhost = location.origin;
-  var overrideNavigator = is_subdomain(localhost);
-  var navigator_ua, test_ua = [];
+    info("Overriding " + domain + " with " + override + " for " + test_host);
 
-  // store UA before pref change, to be compared later
-  if (overrideNavigator) {
-    navigator_ua = navigator.userAgent;
-  }
-  test_hosts.forEach(function (test_host) {
-    test_ua.push(getUA(test_host));
-  });
-  // set the override pref to override the UA
-  SpecialPowers.pushPrefEnv({
-    set: [[PREF_OVERRIDES_BRANCH + domain, override]],
-  }, function () {
-    var ifr = document.createElement('IFRAME');
-    ifr.src = "about:blank";
+    function is_subdomain(host) {
+      var [test_domain] = host.slice(host.lastIndexOf('/') + 1).split(':', 1);
+      return test_domain === domain || test_domain.endsWith('.' + domain);
+    }
 
-    ifr.addEventListener('load', function() {
-      var nav = ifr.contentWindow.navigator;
+    var localhost = location.origin;
+    var overrideNavigator = is_subdomain(localhost);
+    var navigator_ua, test_ua;
+
+    if (overrideNavigator) {
+      navigator_ua = navigator.userAgent;
+    }
 
-      // check that the UA has changed after pref change
-      if (overrideNavigator) {
-        is(nav.userAgent, expected,
-          'Navigator UA not overridden at step ' + (++step));
-      } else {
-        is(nav.userAgent, DEFAULT_UA,
-          'Navigator UA should not be overridden at step ' + (++step));
-      }
-
-      test_hosts.forEach(function (test_host) {
-        is(getUA(test_host), expected,
-          'Header UA not overridden at step ' + (++step));
-      });
-
-      // clear the override pref to undo overriding the UA
-      SpecialPowers.pushPrefEnv({
-        clear: [[PREF_OVERRIDES_BRANCH + domain]],
-      }, function () {
-        var ifr = document.createElement('IFRAME');
-        ifr.src = "about:blank";
+    let url = location.pathname;
+    url = test_host + url.slice(0, url.lastIndexOf('/')) + '/user_agent.sjs';
+    let uri = Services.io.newURI(url, null, null);
+    let tab = BrowserApp.addTab(uri.spec, { selected: true, parentId: BrowserApp.selectedTab.id });
+    let browser = tab.browser;
 
-        ifr.addEventListener('load', function() {
-          var nav = ifr.contentWindow.navigator;
-
-          // check that the UA has changed back
-          if (overrideNavigator) {
-            is(nav.userAgent, navigator_ua,
-              'Navigator UA not restored at step ' + (++step));
-          }
-          test_hosts.forEach(function (test_host, i) {
-            is(getUA(test_host), test_ua[i],
-              'Header UA not restored at step ' + (++step));
+    browser.addEventListener("load", function handle() {
+      test_ua = browser.contentDocument.body.innerHTML;
+      browser.removeEventListener("load", handle, true);
+      BrowserApp.closeTab(tab);
+      SpecialPowers.pushPrefEnv({
+        set: [[PREF_OVERRIDES_BRANCH + domain, override]],
+      }, function () {
+        testUATab(test_host, expected, true, 'Header UA not overridden at step ' + (++step), true,
+          true, 'Navigator UA not overridden at step ' + (++step), function () {
+          // clear the override pref to undo overriding the UA
+          SpecialPowers.pushPrefEnv({
+            clear: [[PREF_OVERRIDES_BRANCH + domain]],
+          }, function () {
+            testUATabNoNav(test_host, test_ua, true, 'Header UA not restored at step ' + (++step), function() {
+              test_hosts.length ? nextTest() : callback();
+            });
           });
-          callback();
         });
-
-        document.getElementById('content').appendChild(ifr);
       });
-    }, false);
-
-    document.getElementById('content').appendChild(ifr);
-  });
+    }, true);
+  })();
 }
 
-
 var step = 0; // for logging
 var tests = [
   // should override both header and navigator.userAgent
   {
     domain: location.hostname,
     override: UA_WHOLE_OVERRIDE,
     test_hosts: [location.origin],
     expected: UA_WHOLE_EXPECTED
@@ -170,26 +153,25 @@ var tests = [
 // test that UA is not overridden when the 'site_specific_overrides' pref is off
 function testInactive(callback) {
   SpecialPowers.pushPrefEnv({
     set: [
       [PREF_OVERRIDES_ENABLED, false],
       [PREF_OVERRIDES_BRANCH + location.hostname, UA_WHOLE_OVERRIDE]
     ]
   }, function () {
-    isnot(navigator.userAgent, UA_WHOLE_OVERRIDE,
-      'Failed to disable navigator UA override');
-    isnot(getUA(location.origin), UA_WHOLE_OVERRIDE,
-      'Failed to disable header UA override');
-    SpecialPowers.pushPrefEnv({
-      clear: [
-        [PREF_OVERRIDES_ENABLED],
-        [PREF_OVERRIDES_BRANCH + location.hostname]
-      ]
-    }, callback);
+    testUATab(location.origin, UA_WHOLE_OVERRIDE, false, 'Failed to disabled header UA override at step ' + (++step),
+      true, false, 'Failed to disable navigator UA override at step + ' + (++step), function () {
+      SpecialPowers.pushPrefEnv({
+        clear: [
+          [PREF_OVERRIDES_ENABLED],
+          [PREF_OVERRIDES_BRANCH + location.hostname]
+        ]
+      }, callback);
+    });
   });
 }
 
 function testPriority(callback) {
   // foo.bar.com override should have priority over bar.com override
   var tests = [
     ['example.org', 'test1.example.org', 'sub1.test1.example.org'],
     ['example.org', 'test1.example.org', 'sub2.test1.example.org'],
@@ -201,32 +183,32 @@ function testPriority(callback) {
     var host = 'http://' + level2;
     SpecialPowers.pushPrefEnv({
       set: [
         [PREF_OVERRIDES_ENABLED, true],
         [PREF_OVERRIDES_BRANCH + level1, UA_WHOLE_OVERRIDE]
       ]
     }, function () {
       // should use first override at this point
-      is(getUA(host),
-        UA_WHOLE_EXPECTED, 'UA not overridden');
-      // add a second override that should be used
-      testUA({
-        domain: level2,
-        override: UA_PARTIAL_OVERRIDE,
-        test_hosts: [host],
-        expected: UA_PARTIAL_EXPECTED
-      }, function () {
-        // add a third override that should not be used
+      testUATabNoNav(host, UA_WHOLE_EXPECTED, true, 'UA not overridden at step ' + (++step), function() {
+        // add a second override that should be used
         testUA({
-          domain: level0,
+          domain: level2,
           override: UA_PARTIAL_OVERRIDE,
           test_hosts: [host],
-          expected: UA_WHOLE_EXPECTED
-        }, tests.length ? nextTest : callback);
+          expected: UA_PARTIAL_EXPECTED
+        }, function () {
+          // add a third override that should not be used
+          testUA({
+            domain: level0,
+            override: UA_PARTIAL_OVERRIDE,
+            test_hosts: [host],
+            expected: UA_WHOLE_EXPECTED
+          }, tests.length ? nextTest : callback);
+        });
       });
     });
   })();
 }
 
 function testOverrides(callback) {
   SpecialPowers.pushPrefEnv({
     set: [[PREF_OVERRIDES_ENABLED, true]]
@@ -235,19 +217,25 @@ function testOverrides(callback) {
   });
 }
 
 SpecialPowers.Cu.import('resource://gre/modules/UserAgentOverrides.jsm', window);
 SpecialPowers.wrap(UserAgentOverrides).init();
 
 SimpleTest.waitForExplicitFinish();
 SimpleTest.requestCompleteLog();
+SimpleTest.requestLongerTimeout(5);
 
 info(SpecialPowers.Cc["@mozilla.org/dom/site-specific-user-agent;1"].number);
 
+const Services = SpecialPowers.Cu.import("resource://gre/modules/Services.jsm").Services;
+
+let chromeWin = Services.wm.getMostRecentWindow("navigator:browser");
+let BrowserApp = chromeWin.BrowserApp;
+
 testOverrides(function() {
   testInactive(function() {
     testPriority(SimpleTest.finish)
   });
 });
 
 </script>
 </pre>
--- a/netwerk/test/mochitests/test_user_agent_updates.html
+++ b/netwerk/test/mochitests/test_user_agent_updates.html
@@ -43,16 +43,38 @@ function getUA(host) {
   var xhr = new XMLHttpRequest();
   xhr.open('GET', url, false); // sync request
   xhr.send();
   is(xhr.status, 200, 'request failed');
   is(typeof xhr.response, 'string', 'invalid response');
   return xhr.response;
 }
 
+function testUATab(host, expected, sameQ, message, testNavQ, navSameQ, navMessage, callback) {
+  let url = location.pathname;
+  url = host + url.slice(0, url.lastIndexOf('/')) + '/user_agent.sjs';
+  let uri = Services.io.newURI(url, null, null);
+  let tab = BrowserApp.addTab(uri.spec, { selected: true, parentId: BrowserApp.selectedTab.id });
+  let browser = tab.browser;
+
+  browser.addEventListener("load", function handle() {
+    ok(sameQ == (browser.contentDocument.body.innerHTML.indexOf(expected) != -1), message);
+    if(testNavQ) {
+      ok(navSameQ == (browser.contentWindow.navigator.userAgent.indexOf(expected) != -1), navMessage);
+    }
+    browser.removeEventListener("load", handle, true);
+    BrowserApp.closeTab(tab);
+    callback();
+  }, true);
+}
+
+function testUATabNoNav(host, expected, sameQ, message, callback) {
+  testUATab(host, expected, sameQ, message, false, true, '', callback);
+}
+
 const OVERRIDES = [
   {
     domain: 'example.org',
     override: '%DATE%',
     host: 'http://example.org'
   },
   {
     domain: 'test1.example.org',
@@ -115,43 +137,35 @@ function testDownload(callback) {
   info('Waiting for UA update: ' + url);
   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 () {
-    var ifr = document.createElement('IFRAME');
-    ifr.src = "about:blank";
-
-    ifr.addEventListener('load', function() {
-      var nav = ifr.contentWindow.navigator;
-      if (nav.userAgent !== UA_OVERRIDE) {
-        waitForUpdate();
-        return;
-      }
-
-      info('Overrode navigator UA');
-      is(getUA(location.origin), UA_OVERRIDE, 'Header UA not overridden');
+  }, setTimeout( function waitForUpdate() {
+    testUATabNoNav(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');
 
-      var updateTime = parseInt(getUA('http://example.org'));
-      ok(startTime <= updateTime, 'Update was before start time');
-      ok(updateTime <= Date.now(), 'Update was after present time');
-
-      OVERRIDES.forEach(function (val) {
-        val.expected && is(getUA(val.host), val.expected,
-          'Incorrect URL parameter: ' + val.override);
-      });
-      callback();
-    }, false);
-
-    document.getElementById('content').appendChild(ifr);
-  }, 100); });
+      let overs = OVERRIDES;
+      (function nextOverride() {
+        val = overs.shift();
+        if (val.expected) {
+           testUATabNoNav(val.host, val.expected, true, 'Incorrect URL parameter: ' + val.override, function() {
+            overs.length ? nextOverride() : callback();
+          });
+        } else {
+          nextOverride();
+        }
+      })();
+    });
+  }, 2000));
 }
 
 function testBadUpdate(callback) {
   var url = getServerURL() + 'invalid-json';
   var prevOverride = navigator.userAgent;
   SpecialPowers.pushPrefEnv({
     set: [
       [PREF_UPDATES_URL, url],
@@ -199,20 +213,21 @@ function testProfileLoad(callback) {
           ifr.src = "about:blank";
 
           ifr.addEventListener('load', function() {
             var nav = ifr.contentWindow.navigator;
             if (nav.userAgent !== UA_ALT_OVERRIDE) {
               setTimeout(waitForLoad, 100);
               return;
             }
-            is(getUA(location.origin), UA_ALT_OVERRIDE, 'Did not apply saved override');
-            saveFilePreviousSize = file.fileSize;
-            callback();
-          }, false);
+            testUATabNoNav(location.origin, UA_ALT_OVERRIDE, true, 'Did not apply saved override', function () {
+              saveFilePreviousSize = file.fileSize;
+              callback();
+            });
+          }, true);
 
           document.getElementById('content').appendChild(ifr);
         })();
       });
     },
     (reason) => {
       throw reason
     }
@@ -255,16 +270,21 @@ SimpleTest.waitForExplicitFinish();
 SimpleTest.requestFlakyTimeout("untriaged");
 
 SpecialPowers.Cu.import("resource://gre/modules/FileUtils.jsm", window);
 var FU = SpecialPowers.wrap(FileUtils);
 
 SpecialPowers.Cu.import("resource://gre/modules/osfile.jsm", window);
 var OSF = SpecialPowers.wrap(OS).File;
 
+const Services = SpecialPowers.Cu.import("resource://gre/modules/Services.jsm").Services;
+
+let chromeWin = Services.wm.getMostRecentWindow("navigator:browser");
+let BrowserApp = chromeWin.BrowserApp;
+
 // Load UserAgentOverrides.jsm after we load update timer manager
 var UAO = null;
 
 var saveFilePreviousSize = 0;
 
 SpecialPowers.pushPrefEnv({
   set: [
     [PREF_APP_UPDATE_TIMERMINIMUMDELAY, 0]