Bug 1252444 - Rewrite some dom mochitests to use pushPrefEnv instead of setCharPref. r=jmaher
authorMartijn Wargers <mwargers@mozilla.com>
Thu, 03 Mar 2016 09:41:14 -0500
changeset 286685 1b0a91b8897fae77ec44b4bc7f98b11808db19f5
parent 286684 f5d666bda9a75c46cc8ed753015c3f4a8b0e41ba
child 286686 53cb9d108ec54eb82a1ba98dee11f27b125973ed
push id30053
push usercbook@mozilla.com
push dateFri, 04 Mar 2016 10:51:57 +0000
treeherdermozilla-central@33d36bf6ca0c [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjmaher
bugs1252444
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 1252444 - Rewrite some dom mochitests to use pushPrefEnv instead of setCharPref. r=jmaher
dom/base/test/test_bug466409.html
dom/base/test/test_navigator_language.html
dom/contacts/tests/test_migration.html
image/test/mochitest/test_bug496292.html
toolkit/components/url-classifier/tests/mochitest/test_trackingprotection_whitelist.html
--- a/dom/base/test/test_bug466409.html
+++ b/dom/base/test/test_bug466409.html
@@ -14,32 +14,26 @@ https://bugzilla.mozilla.org/show_bug.cg
   <iframe id="testframe"></iframe>
 </div>
 <pre id="test">
 <script class="testbody" type="text/javascript">
 
 /** Test for Bug 466409 **/
 
 
-function setUniversalDetector(detector)
-{
-  var olddetector = SpecialPowers.getCharPref("intl.charset.detector");
-  SpecialPowers.setCharPref("intl.charset.detector", detector);
-  return olddetector;
-}
-
 SimpleTest.waitForExplicitFinish();
 var testframe = document.getElementById('testframe');
 
-testframe.onload = function ()
-  {
-    setUniversalDetector(olddetector);
-    ok(true, "page loaded successfully");
-    SimpleTest.finish();
+
+
+SpecialPowers.pushPrefEnv({"set": [['intl.charset.detector', 'universal_charset_detector']]}, function() {
+  testframe.onload = function () {
+      ok(true, "page loaded successfully");
+      SimpleTest.finish();
   };
+  testframe.src = "bug466409-page.html";
+});
 
-var olddetector = setUniversalDetector("universal_charset_detector");
-testframe.src = "bug466409-page.html";
 
 </script>
 </pre>
 </body>
 </html>
--- a/dom/base/test/test_navigator_language.html
+++ b/dom/base/test/test_navigator_language.html
@@ -17,32 +17,20 @@ https://bugzilla.mozilla.org/show_bug.cg
 <pre id="test">
 </pre>
 <script type="application/javascript;version=1.7">
   "use strict";
 
   SimpleTest.waitForExplicitFinish();
 
   /** Test for NavigatorLanguage **/
-  var prefValue = null;
   var actualLanguageChangesFromHandler = 0;
   var actualLanguageChangesFromAVL = 0;
   var expectedLanguageChanges = 0;
 
-  function setUp() {
-    try {
-      prefValue = SpecialPowers.getCharPref('intl.accept_languages');
-    } catch (e) {
-    }
-  }
-
-  function tearDown() {
-    SpecialPowers.setCharPref('intl.accept_languages', prefValue);
-  }
-
   var testValues = [
     { accept_languages: 'foo', language: 'foo', languages: ['foo'] },
     { accept_languages: '', language: '', languages: [] },
     { accept_languages: 'foo,bar', language: 'foo', languages: [ 'foo', 'bar' ] },
     { accept_languages: '  foo , bar ', language: 'foo', languages: [ 'foo', 'bar' ] },
     { accept_languages: '  foo ; bar ', language: 'foo ; bar', languages: [ 'foo ; bar' ] },
     { accept_languages: '_foo_', language: '_foo_', languages: ['_foo_'] },
     { accept_languages: 'en_', language: 'en-', languages: ['en-'] },
@@ -53,17 +41,16 @@ https://bugzilla.mozilla.org/show_bug.cg
     { accept_languages: 'en_us-cal, en_us-c', language: 'en-US-cal', languages: ['en-US-cal', 'en-US-c'] },
   ];
 
   var currentTestIdx = 0;
   var tests = [];
   function nextTest() {
     currentTestIdx++;
     if (currentTestIdx >= tests.length) {
-      tearDown();
       SimpleTest.finish();
       return;
     }
 
     tests[currentTestIdx]();
   }
 
   // Check that the API is there.
@@ -85,30 +72,30 @@ https://bugzilla.mozilla.org/show_bug.cg
     window.onlanguagechange = function() {
       isnot(navigator.languages, previous, "navigator.languages cached value was updated");
       window.onlanguagechange = null;
 
       nextTest();
     }
 
     setTimeout(function() {
-      SpecialPowers.setCharPref('intl.accept_languages', 'testArrayCached');
+      SpecialPowers.pushPrefEnv({"set": [['intl.accept_languages', 'testArrayCached']]});
     }, 0);
   });
 
   // Test that event handler inside the <body> works as expected and that the
   // event has the expected properties.
   tests.push(function testEventProperties() {
     document.body.setAttribute('onlanguagechange',
       "document.body.removeAttribute('onlanguagechange');" +
       "is(event.cancelable, false); is(event.bubbles, false);" +
       "nextTest();");
 
     setTimeout(function() {
-      SpecialPowers.setCharPref('intl.accept_languages', 'testEventProperties');
+      SpecialPowers.pushPrefEnv({"set": [['intl.accept_languages', 'testEventProperties']]}, function() {});
     }, 0);
   });
 
   // Check that the returned values such as the behavior when the underlying
   // languages change.
   tests.push(function testBasicBehaviour() {
     function checkIfDoneAndProceed() {
       if (actualLanguageChangesFromHandler == actualLanguageChangesFromAVL) {
@@ -128,17 +115,17 @@ https://bugzilla.mozilla.org/show_bug.cg
       checkIfDoneAndProceed();
     }
     window.addEventListener('languagechange', languageChangeAVL);
 
     function* testEvents() {
       for (var i = 0; i < testValues.length; ++i) {
         var data = testValues[i];
         setTimeout(function(data) {
-          SpecialPowers.setCharPref('intl.accept_languages', data.accept_languages);
+          SpecialPowers.pushPrefEnv({"set": [['intl.accept_languages', data.accept_languages]]});
         }, 0, data);
         expectedLanguageChanges++;
         yield undefined;
 
         is(actualLanguageChangesFromAVL, expectedLanguageChanges);
         is(actualLanguageChangesFromHandler, expectedLanguageChanges);
 
         is(navigator.language, data.language);
@@ -151,32 +138,32 @@ https://bugzilla.mozilla.org/show_bug.cg
         }
       }
     }
 
     var genEvents = testEvents();
     genEvents.next();
   });
 
-  // Check that the orientationchange event isn't sent twice if the preference
+  // Check that the languagechange event isn't sent twice if the preference
   // is set to the same value.
   tests.push(function testOnlyFireIfRealChange() {
     function* changeLanguage() {
       setTimeout(function() {
-        SpecialPowers.setCharPref('intl.accept_languages', 'fr-CA');
+        SpecialPowers.pushPrefEnv({"set": [['intl.accept_languages', 'fr-CA']]});
       });
       yield undefined;
 
       setTimeout(function() {
         // Twice the same change, should fire only one event.
-        SpecialPowers.setCharPref('intl.accept_languages', 'fr-CA');
+        SpecialPowers.pushPrefEnv({"set": [['intl.accept_languages', 'fr-CA']]});
         setTimeout(function() {
           // A real change to tell the test it should now count how many changes were
           // received until now.
-          SpecialPowers.setCharPref('intl.accept_languages', 'fr-FR');
+          SpecialPowers.pushPrefEnv({"set": [['intl.accept_languages', 'fr-FR']]});
         });
       });
       yield undefined;
     }
 
     var genChanges = changeLanguage();
 
     var doubleEventCount = 0;
@@ -205,24 +192,21 @@ https://bugzilla.mozilla.org/show_bug.cg
     document.body.appendChild(frame);
 
     frame.contentWindow.onload = function() {
       document.body.removeChild(frame);
       frame = null;
 
       SpecialPowers.exactGC(window, function() {
         // This should not crash.
-        SpecialPowers.setCharPref('intl.accept_languages', 'en-GB');
-
-        nextTest();
+        SpecialPowers.pushPrefEnv({"set": [['intl.accept_languages', 'en-GB']]}, nextTest);
       });
     }
   });
 
   // There is one test using document.body.
   addLoadEvent(function() {
-    setUp();
     tests[0]();
   });
 
 </script>
 </body>
 </html>
--- a/dom/contacts/tests/test_migration.html
+++ b/dom/contacts/tests/test_migration.html
@@ -49,42 +49,25 @@ function createDB(version) {
   backend.sendAsyncMessage("createDB", version);
 }
 
 function deleteDB() {
   info("Will delete the DB.");
   backend.sendAsyncMessage("deleteDB");
 }
 
-function setSubstringMatching(value) {
-  info("Setting substring matching to " + value);
-
-  if (value) {
-    SpecialPowers.setIntPref("dom.phonenumber.substringmatching.BR", value);
-
-    // this is the Mcc for Brazil, so that we trigger the previous pref
-    SpecialPowers.setCharPref("ril.lastKnownSimMcc", "724");
-  } else {
-    SpecialPowers.clearUserPref("dom.phonenumber.substringmatching.BR");
-    SpecialPowers.clearUserPref("ril.lastKnownSimMcc");
-  }
-
-  next();
-}
-
 var steps = [
   function setupChromeScript() {
     loadChromeScript();
     addBackendEvents();
     next();
   },
 
   deleteDB, // let's be sure the DB does not exist yet
   createDB.bind(null, 12),
-  setSubstringMatching.bind(null, 7),
 
   function testAccessMozContacts() {
     info("Checking we have the right number of contacts: " + contactsCount);
     var req = mozContacts.getCount();
     req.onsuccess = function onsuccess() {
       ok(true, "Could access the mozContacts API");
       is(this.result, contactsCount, "Contacts count is correct");
       next();
@@ -189,22 +172,23 @@ var steps = [
 
     req.onerror = function onerror() {
       ok(false, "Error while finding contact for substring matching check!");
       next();
     };
   },
 
   deleteDB,
-  setSubstringMatching.bind(null, null),
 
   function finish() {
     backend.destroy();
     info("all done!\n");
     SimpleTest.finish();
   }
 ];
 
-start_tests();
+// this is the Mcc for Brazil, so that we trigger the previous pref
+SpecialPowers.pushPrefEnv({"set": [["dom.phonenumber.substringmatching.BR", 7],
+                                   ["ril.lastKnownSimMcc", "724"]]}, start_tests);
 </script>
 </pre>
 </body>
 </html>
--- a/image/test/mochitest/test_bug496292.html
+++ b/image/test/mochitest/test_bug496292.html
@@ -92,32 +92,31 @@ function loadFirst()
 }
 
 function checkFirst()
 {
   first = canvas.toDataURL();
   is(first, ref, "The default Accept header used by image loader is correct");
 
   SpecialPowers.setCharPref("image.http.accept", "image/png");
-
-  var remoteCanvas = new RemoteCanvas("bug496292-iframe-2.html");
-  remoteCanvas.load(checkSecond);
+  SpecialPowers.pushPrefEnv({"set": [["image.http.accept", "image/png"]]}, function() {
+    var remoteCanvas = new RemoteCanvas("bug496292-iframe-2.html");
+    remoteCanvas.load(checkSecond);
+  });
 }
 
 function checkSecond()
 {
   second = canvas.toDataURL();
   is(second, ref, "The modified Accept header used by image loader is correct");
 
-  try {
-    SpecialPowers.clearUserPref("image.http.accept");
-  } catch (ex) { }
-
-  var remoteCanvas = new RemoteCanvas("bug496292-iframe-1.html");
-  remoteCanvas.load(checkThird);
+  SpecialPowers.pushPrefEnv({"clear": [["image.http.accept"]]}, function() {
+    var remoteCanvas = new RemoteCanvas("bug496292-iframe-1.html");
+    remoteCanvas.load(checkThird);
+  });
 }
 
 function checkThird() {
   third = canvas.toDataURL();
   is(third, ref, "The Accept header used by image loader should be correctly reset");
 
   SimpleTest.finish();
 }
--- a/toolkit/components/url-classifier/tests/mochitest/test_trackingprotection_whitelist.html
+++ b/toolkit/components/url-classifier/tests/mochitest/test_trackingprotection_whitelist.html
@@ -124,22 +124,25 @@ function test() {
       aWindow.close();
 
       // Load the test from a URL that's NOT on the whitelist
       testOnWindow(contentPage2, function(aWindow) {
         checkLoads(aWindow, false);
         aWindow.close();
 
         // Load the test from a URL on the whitelist but without the whitelist
-        SpecialPowers.setCharPref("urlclassifier.trackingWhitelistTable", "");
-        testOnWindow(contentPage1, function(aWindow) {
-        checkLoads(aWindow, false);
-          aWindow.close();
-          SimpleTest.finish();
-        });
+        SpecialPowers.pushPrefEnv({"set" : [["urlclassifier.trackingWhitelistTable", ""]]},
+          function() {
+            testOnWindow(contentPage1, function(aWindow) {
+              checkLoads(aWindow, false);
+              aWindow.close();
+              SimpleTest.finish();
+            });
+          });
+
       });
     });
   });
 }
 
 SimpleTest.waitForExplicitFinish();
 
 </script>