Bug 871323 - Fix and enable offline mochitests for B2G - remove most of the enablePrivilege calls and use pushPermissions. r=honzab.moz
authorMartijn Wargers <mwargers@mozilla.com>
Wed, 04 Dec 2013 22:28:36 +0100
changeset 174678 21c6774bb0777e0ad8ebef1b4b909843e8df3219
parent 174677 3979cee47a41912f86ec9f54addcdd44a9c54e00
child 174679 50c0d05fa250ececabe0c54f86dd5eb8325dc367
push id445
push userffxbld
push dateMon, 10 Mar 2014 22:05:19 +0000
treeherdermozilla-release@dc38b741b04e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewershonzab
bugs871323
milestone28.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 871323 - Fix and enable offline mochitests for B2G - remove most of the enablePrivilege calls and use pushPermissions. r=honzab.moz
dom/tests/mochitest/ajax/offline/offlineTests.js
dom/tests/mochitest/ajax/offline/test_bug445544.html
dom/tests/mochitest/ajax/offline/test_bug460353.html
dom/tests/mochitest/ajax/offline/test_bug744719-cancel.html
dom/tests/mochitest/ajax/offline/test_bug744719.html
dom/tests/mochitest/ajax/offline/test_missingManifest.html
dom/tests/mochitest/ajax/offline/test_noManifest.html
dom/tests/mochitest/ajax/offline/test_obsolete.html
dom/tests/mochitest/ajax/offline/updatingImplicit.html
--- a/dom/tests/mochitest/ajax/offline/offlineTests.js
+++ b/dom/tests/mochitest/ajax/offline/offlineTests.js
@@ -18,25 +18,23 @@ OfflineCacheContents.prototype = {
 QueryInterface: function(iid) {
     if (!iid.equals(Ci.nsISupports) &&
         !iid.equals(Ci.nsICacheListener)) {
       throw Cr.NS_ERROR_NO_INTERFACE;
     }
     return this;
   },
 onCacheEntryAvailable: function(desc, accessGranted, status) {
-    netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
-
     if (!desc) {
       this.fetch(this.callback);
       return;
     }
 
     var stream = desc.QueryInterface(Ci.nsICacheEntryDescriptor).openInputStream(0);
-    var sstream = SpecialPowers.Cc["@mozilla.org/scriptableinputstream;1"]
+    var sstream = Cc["@mozilla.org/scriptableinputstream;1"]
                  .createInstance(SpecialPowers.Ci.nsIScriptableInputStream);
     sstream.init(stream);
     this.contents[desc.key] = sstream.read(sstream.available());
     sstream.close();
     desc.close();
     this.fetch(this.callback);
   },
 
@@ -93,48 +91,39 @@ setupChild: function()
  *
  * @return boolean Whether this window is the slave window
  *                 to actually run the test in.
  */
 setup: function()
 {
   netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
 
-  var prefBranch = Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefBranch);
   try {
-    this._allowedByDefault = prefBranch.getBoolPref("offline-apps.allow_by_default");
+    this._allowedByDefault = SpecialPowers.getBoolPref("offline-apps.allow_by_default");
   } catch (e) {}
 
   if (this._allowedByDefault) {
     this._masterWindow = window;
 
     return true;
   }
 
   if (!window.opener || !window.opener.OfflineTest ||
       !window.opener.OfflineTest._hasSlave) {
     // Offline applications must be toplevel windows and have the
     // offline-app permission.  Because we were loaded without the
     // offline-app permission and (probably) in an iframe, we need to
     // enable the pref and spawn a new window to perform the actual
     // tests.  It will use this window to report successes and
     // failures.
-    var pm = Cc["@mozilla.org/permissionmanager;1"]
-      .getService(Ci.nsIPermissionManager);
-    var uri = Cc["@mozilla.org/network/io-service;1"]
-      .getService(Ci.nsIIOService)
-      .newURI(window.location.href, null, null);
-    var principal = SpecialPowers.Cc["@mozilla.org/scriptsecuritymanager;1"]
-                      .getService(Ci.nsIScriptSecurityManager)
-                      .getNoAppCodebasePrincipal(uri);
 
-    if (pm.testPermissionFromPrincipal(principal, "offline-app") != 0) {
+    if (SpecialPowers.testPermission("offline-app", Ci.nsIPermissionManager.ALLOW_ACTION, document)) {
       ok(false, "Previous test failed to clear offline-app permission!  Expect failures.");
     }
-    pm.addFromPrincipal(principal, "offline-app", Ci.nsIPermissionManager.ALLOW_ACTION);
+    SpecialPowers.addPermission("offline-app", Ci.nsIPermissionManager.ALLOW_ACTION, document);
 
     // Tests must run as toplevel windows.  Open a slave window to run
     // the test.
     this._hasSlave = true;
     window.open(window.location, "offlinetest");
 
     return false;
   }
@@ -150,28 +139,17 @@ teardownAndFinish: function()
 },
 
 teardown: function(callback)
 {
   // First wait for any pending scheduled updates to finish
   this.waitForUpdates(function(self) {
     // Remove the offline-app permission we gave ourselves.
 
-    netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
-
-    var pm = Cc["@mozilla.org/permissionmanager;1"]
-             .getService(Ci.nsIPermissionManager);
-    var uri = Cc["@mozilla.org/network/io-service;1"]
-              .getService(Ci.nsIIOService)
-              .newURI(window.location.href, null, null);
-    var principal = SpecialPowers.Cc["@mozilla.org/scriptsecuritymanager;1"]
-                      .getService(Ci.nsIScriptSecurityManager)
-                      .getNoAppCodebasePrincipal(uri);
-
-    pm.removeFromPrincipal(principal, "offline-app");
+    SpecialPowers.removePermission("offline-app", window.document);
 
     // Clear all overrides on the server
     for (override in self._pathOverrides)
       self.deleteData(self._pathOverrides[override]);
     for (statedSJS in self._SJSsStated)
       self.setSJSState(self._SJSsStated[statedSJS], "");
 
     self.clear();
@@ -222,33 +200,31 @@ clear: function()
   var applicationCache = this.getActiveCache();
   if (applicationCache) {
     applicationCache.discard();
   }
 },
 
 waitForUpdates: function(callback)
 {
-  netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
-
   var self = this;
   var observer = {
     notified: false,
     observe: function(subject, topic, data) {
       if (subject) {
         subject.QueryInterface(SpecialPowers.Ci.nsIOfflineCacheUpdate);
         dump("Update of " + subject.manifestURI.spec + " finished\n");
       }
 
       SimpleTest.executeSoon(function() {
         if (observer.notified) {
           return;
         }
 
-        var updateservice = SpecialPowers.Cc["@mozilla.org/offlinecacheupdate-service;1"]
+        var updateservice = Cc["@mozilla.org/offlinecacheupdate-service;1"]
                             .getService(SpecialPowers.Ci.nsIOfflineCacheUpdateService);
         var updatesPending = updateservice.numUpdates;
         if (updatesPending == 0) {
           try {
             SpecialPowers.removeObserver(observer, "offline-cache-update-completed");
           } catch(ex) {}
           dump("All pending updates done\n");
           observer.notified = true;
@@ -275,17 +251,16 @@ failEvent: function(e)
 // The offline API as specified has no way to watch the load of a resource
 // added with applicationCache.mozAdd().
 waitForAdd: function(url, onFinished) {
   // Check every half second for ten seconds.
   var numChecks = 20;
 
   var waitForAddListener = {
     onCacheEntryAvailable: function(entry, access, status) {
-      netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
       if (entry) {
         entry.close();
         onFinished();
         return;
       }
 
       if (--numChecks == 0) {
         onFinished();
@@ -365,17 +340,16 @@ getActiveSession: function()
                                     Ci.nsICache.STORE_OFFLINE,
                                     true);
 },
 
 priv: function(func)
 {
   var self = this;
   return function() {
-    netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
     func(arguments);
   }
 },
 
 checkCacheEntries: function(entries, callback)
 {
   var checkNextEntry = function() {
     if (entries.length == 0) {
@@ -386,17 +360,16 @@ checkCacheEntries: function(entries, cal
     }
   }
 
   checkNextEntry();
 },
 
 checkCache: function(url, expectEntry, callback)
 {
-  netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
   var cacheSession = this.getActiveSession();
   this._checkCache(cacheSession, url, expectEntry, callback);
 },
 
 _checkCache: function(cacheSession, url, expectEntry, callback)
 {
   if (!cacheSession) {
     if (expectEntry) {
@@ -405,17 +378,16 @@ checkCache: function(url, expectEntry, c
       this.ok(true, url + " should not exist in the offline cache (no session)");
     }
     if (callback) setTimeout(this.priv(callback), 0);
     return;
   }
 
   var _checkCacheListener = {
     onCacheEntryAvailable: function(entry, access, status) {
-      netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
       if (entry) {
         if (expectEntry) {
           OfflineTest.ok(true, url + " should exist in the offline cache");
         } else {
           OfflineTest.ok(false, url + " should not exist in the offline cache");
         }
         entry.close();
       } else {
--- a/dom/tests/mochitest/ajax/offline/test_bug445544.html
+++ b/dom/tests/mochitest/ajax/offline/test_bug445544.html
@@ -1,16 +1,17 @@
 <html manifest="445544.cacheManifest">
 <!--
 https://bugzilla.mozilla.org/show_bug.cgi?id=445544
 -->
 
 <head>
 <title>Test for Bug 445544</title>
 
+<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
 <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
 <script type="text/javascript" src="/tests/dom/tests/mochitest/ajax/offline/offlineTests.js"></script>
 
 <script type="text/javascript">
 
 var gTestWin;
 var gTimeoutId;
 
--- a/dom/tests/mochitest/ajax/offline/test_bug460353.html
+++ b/dom/tests/mochitest/ajax/offline/test_bug460353.html
@@ -13,40 +13,33 @@
 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
 <script type="text/javascript" src="/tests/dom/tests/mochitest/ajax/offline/offlineTests.js"></script>
 
 <script class="testbody" type="text/javascript">
 
 var result = new Array();
 var expectedUpdates = 3; // 2 iframes and our self
 
-init();
+applicationCache.oncached = onUpdatePassed;
+applicationCache.onnoupdate = onUpdatePassed;
+
+SpecialPowers.pushPermissions([{'type': 'offline-app', 'allow': true, 'context': document}], init);
 
 function onUpdatePassed()
 {
   if (!(--expectedUpdates))
     SimpleTest.executeSoon(finish);
 }
 
 function init()
 {
-  var Cc = SpecialPowers.Cc;
-  var Ci = SpecialPowers.Ci;
-  var pm = Cc["@mozilla.org/permissionmanager;1"]
-    .getService(Ci.nsIPermissionManager);
-  var uri = Cc["@mozilla.org/network/io-service;1"]
-    .getService(Ci.nsIIOService)
-    .newURI(window.location.href, null, null);
-  var principal = SpecialPowers.Cc["@mozilla.org/scriptsecuritymanager;1"]
-                    .getService(Ci.nsIScriptSecurityManager)
-                    .getNoAppCodebasePrincipal(uri);
-  pm.addFromPrincipal(principal, "offline-app", Ci.nsIPermissionManager.ALLOW_ACTION);
-
-  applicationCache.oncached = onUpdatePassed;
-  applicationCache.onnoupdate = onUpdatePassed;
+  var iframes = document.getElementsByTagName('iframe');
+  iframes[0].src = "460353_iframe_nomanifest.html";
+  iframes[1].src = "460353_iframe_ownmanifest.html";
+  iframes[2].src = "460353_iframe_samemanifest.html";
 }
 
 function frameOnLoad(frameid, status)
 {
   var obj = new Object();
   result[frameid] = obj;
 
   result[frameid].load = true;
@@ -74,28 +67,16 @@ function finish()
   SimpleTest.ok(result["diff"].updateOK || false, "Frame with different manifest cache update passed OK");
   SimpleTest.is(result["diff"].cacheStatus || -1, 1, "Frame with different manifest cache status was IDLE");
 
   SimpleTest.ok(result["noman"].load || false, "Frame with no manifest loads");
   SimpleTest.ok(result["noman"].update == undefined, "Frame with no manifest cache update didn't notify");
   SimpleTest.ok(result["noman"].updateOK == undefined, "Frame with no manifest cache update didn't pass");
   SimpleTest.is(result["noman"].cacheStatus || -1, -1, "Frame with no manifest cache status was undefined");
 
-  var Cc = SpecialPowers.Cc;
-  var Ci = SpecialPowers.Ci;
-  var pm = Cc["@mozilla.org/permissionmanager;1"]
-           .getService(Ci.nsIPermissionManager);
-  var uri = Cc["@mozilla.org/network/io-service;1"]
-            .getService(Ci.nsIIOService)
-            .newURI(window.location.href, null, null);
-  var principal = SpecialPowers.Cc["@mozilla.org/scriptsecuritymanager;1"]
-                    .getService(Ci.nsIScriptSecurityManager)
-                    .getNoAppCodebasePrincipal(uri);
-  pm.removeFromPrincipal(principal, "offline-app");
-
   OfflineTest.waitForUpdates(function() {
     cleanCache("http://mochi.test:8888/tests/dom/tests/mochitest/ajax/offline/simpleManifest.cacheManifest");
     cleanCache("http://mochi.test:8888/tests/dom/tests/mochitest/ajax/offline/updatingManifest.sjs");
 
     SimpleTest.finish();
   });
 }
 
@@ -107,13 +88,13 @@ function cleanCache(manifestURL)
     cache.discard();
 }
 
 SimpleTest.waitForExplicitFinish();
 
 </script>
 
 <body>
-  <iframe src="460353_iframe_nomanifest.html"></iframe>
-  <iframe src="460353_iframe_ownmanifest.html"></iframe>
-  <iframe src="460353_iframe_samemanifest.html"></iframe>
+  <iframe></iframe>
+  <iframe></iframe>
+  <iframe></iframe>
 </body>
 </html>
--- a/dom/tests/mochitest/ajax/offline/test_bug744719-cancel.html
+++ b/dom/tests/mochitest/ajax/offline/test_bug744719-cancel.html
@@ -9,18 +9,20 @@
 <script type="text/javascript">
 
 /*
   Manifest refers a large number of resource to load.  The 10th item however is a reference to a non-existing
   resource that cancels the load.  This test checks we cancel all loads and don't leak any of the other resources
   after cancelation.
 */
 
-ok(applicationCache.mozItems.length == 0,
-   "applicationCache.mozItems should be available and empty before associating with a cache.");
+if (SpecialPowers.isMainProcess()) {
+  ok(applicationCache.mozItems.length == 0,
+     "applicationCache.mozItems should be available and empty before associating with a cache.");
+}
 
 function updateCanceled()
 {
   OfflineTest.checkCache("http://mochi.test:8888/tests/dom/tests/mochitest/ajax/offline/744719-cancel.cacheManifest", false, null);
   OfflineTest.checkCache("http://mochi.test:8888/tests/SimpleTest/SimpleTest.js", false, null);
   OfflineTest.checkCache("http://mochi.test:8888/tests/dom/tests/mochitest/ajax/offline/offlineTests.js", false, null);
 
   OfflineTest.checkCache("http://mochi.test:8888/tests/dom/tests/mochitest/ajax/offline/nonexistent744719?010", false, null);
--- a/dom/tests/mochitest/ajax/offline/test_bug744719.html
+++ b/dom/tests/mochitest/ajax/offline/test_bug744719.html
@@ -8,18 +8,20 @@
 
 <script type="text/javascript">
 
 /*
   Simply load a large number of resources and check all are properly cached.  This should cover all parts
   of the parallel loading code.
 */
 
-ok(applicationCache.mozItems.length == 0,
-   "applicationCache.mozItems should be available and empty before associating with a cache.");
+if (SpecialPowers.isMainProcess()) {
+  ok(applicationCache.mozItems.length == 0,
+     "applicationCache.mozItems should be available and empty before associating with a cache.");
+}
 
 function manifestUpdated()
 {
   OfflineTest.checkCache("http://mochi.test:8888/tests/dom/tests/mochitest/ajax/offline/744719.cacheManifest", true);
   OfflineTest.checkCache("http://mochi.test:8888/tests/SimpleTest/SimpleTest.js", true);
   OfflineTest.checkCache("http://mochi.test:8888/tests/dom/tests/mochitest/ajax/offline/offlineTests.js", true);
 
   var URL = "http://mochi.test:8888/tests/dom/tests/mochitest/ajax/offline/subresource744719.html?";
--- a/dom/tests/mochitest/ajax/offline/test_missingManifest.html
+++ b/dom/tests/mochitest/ajax/offline/test_missingManifest.html
@@ -4,44 +4,30 @@
 
 <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
 
 <script type="text/javascript">
 
 var gTestWin;
 
-var Cc = SpecialPowers.Cc;
-var Ci = SpecialPowers.Ci;
+SimpleTest.waitForExplicitFinish();
+
+SpecialPowers.pushPermissions([{'type': 'offline-app', 'allow': true, 'context': document}], startTest);
 
-// Enable the offline-app permission before loading the new window
-var pm = Cc["@mozilla.org/permissionmanager;1"]
-        .getService(Ci.nsIPermissionManager);
-var uri = Cc["@mozilla.org/network/io-service;1"]
-         .getService(Ci.nsIIOService)
-         .newURI(window.location.href, null, null);
-var principal = SpecialPowers.Cc["@mozilla.org/scriptsecuritymanager;1"]
-                  .getService(Ci.nsIScriptSecurityManager)
-                  .getNoAppCodebasePrincipal(uri);
+function startTest() {
+  // now this will properly load the manifest.
+  gTestWin = window.open("missing.html");
+}
 
-pm.addFromPrincipal(principal, "offline-app", Ci.nsIPermissionManager.ALLOW_ACTION);
-
-// now this will properly load the manifest.
-gTestWin = window.open("missing.html");
-
-function finish()
-{
-  pm.removeFromPrincipal(principal, "offline-app");
-
+function finish() {
   gTestWin.close();
   SimpleTest.finish();
 }
 
-SimpleTest.waitForExplicitFinish();
-
 </script>
 
 </head>
 
 <body>
 
 </body>
 </html>
--- a/dom/tests/mochitest/ajax/offline/test_noManifest.html
+++ b/dom/tests/mochitest/ajax/offline/test_noManifest.html
@@ -17,17 +17,19 @@ function expectInvalidState(fn, desc) {
       gotInvalidState = true;
     }
   }
 
   ok(gotInvalidState, desc);
 }
 
 is(typeof(applicationCache), "object");
-is(applicationCache.mozLength, 0, "applicationCache.mozLength should be 0");
+if (SpecialPowers.isMainProcess()) {
+  is(applicationCache.mozLength, 0, "applicationCache.mozLength should be 0");
+}
 is(applicationCache.status, 0, "applicationCache.status should be 0 (UNCACHED)");
 
 expectInvalidState(function() { applicationCache.update(); },
                    "applicationCache.update should throw InvalidStateError");
 expectInvalidState(function() { applicationCache.swapCache(); },
                    "applicationCache.update should throw InvalidStateError");
 
 
--- a/dom/tests/mochitest/ajax/offline/test_obsolete.html
+++ b/dom/tests/mochitest/ajax/offline/test_obsolete.html
@@ -4,46 +4,33 @@
 
 <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
 
 <script type="text/javascript">
 
 var gTestWin;
 
-var Cc = SpecialPowers.Cc;
-var Ci = SpecialPowers.Ci;
+SpecialPowers.pushPermissions([{'type': 'offline-app', 'allow': true, 'context': document}], startTest);
 
-// Enable the offline-app permission before loading the new window
-var pm = Cc["@mozilla.org/permissionmanager;1"]
-        .getService(Ci.nsIPermissionManager);
-var uri = Cc["@mozilla.org/network/io-service;1"]
-         .getService(Ci.nsIIOService)
-         .newURI(window.location.href, null, null);
-var principal = SpecialPowers.Cc["@mozilla.org/scriptsecuritymanager;1"]
-                  .getService(Ci.nsIScriptSecurityManager)
-                  .getNoAppCodebasePrincipal(uri);
-pm.addFromPrincipal(principal, "offline-app", Ci.nsIPermissionManager.ALLOW_ACTION);
-
-// Make the obsoleting.sjs return a valid manifest
-var req = new XMLHttpRequest();
-req.open("GET", "http://mochi.test:8888/tests/dom/tests/mochitest/ajax/offline/obsoletingManifest.sjs?state=manifestPresent");
-req.setRequestHeader("Content-Type", "text/cache-manifest");
-req.send("");
-req.onreadystatechange = function() {
-  if (req.readyState == 4) {
-    // now this will properly load the manifest.
-    gTestWin = window.open("obsolete.html");
+function startTest() {
+  // Make the obsoleting.sjs return a valid manifest
+  var req = new XMLHttpRequest();
+  req.open("GET", "http://mochi.test:8888/tests/dom/tests/mochitest/ajax/offline/obsoletingManifest.sjs?state=manifestPresent");
+  req.setRequestHeader("Content-Type", "text/cache-manifest");
+  req.send("");
+  req.onreadystatechange = function() {
+    if (req.readyState == 4) {
+      // now this will properly load the manifest.
+      gTestWin = window.open("obsolete.html");
+    }
   }
 }
 
-function finish()
-{
-  pm.removeFromPrincipal(principal, "offline-app");
-
+function finish() {
   gTestWin.close();
   SimpleTest.finish();
 }
 
 SimpleTest.waitForExplicitFinish();
 
 </script>
 
--- a/dom/tests/mochitest/ajax/offline/updatingImplicit.html
+++ b/dom/tests/mochitest/ajax/offline/updatingImplicit.html
@@ -1,12 +1,13 @@
 <html xmlns="http://www.w3.org/1999/xhtml" manifest="http://mochi.test:8888/tests/dom/tests/mochitest/ajax/offline/updatingManifest.sjs">
 <head>
 <title>Updating implicit</title>
 
+<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
 <script type="text/javascript" src="/tests/dom/tests/mochitest/ajax/offline/offlineTests.js"></script>
 
 <script type="text/javascript">
 
 function manifestUpdated()
 {
   // Assert that we are properly associated with the application
   // cache.