Bug 774585 - Tests. r=sicking
authorMounir Lamouri <mounir.lamouri@gmail.com>
Fri, 20 Jul 2012 00:26:05 -0700
changeset 105367 8906997e57e648b6a6751976c432e2e8436848e0
parent 105366 365d6054c8b6128f3d0e3f91742399be25dff376
child 105368 1f976bd5e456116f84ecf32dd9e7f5d3e0290f29
push idunknown
push userunknown
push dateunknown
reviewerssicking
bugs774585
milestone17.0a1
Bug 774585 - Tests. r=sicking
caps/tests/mochitest/test_principal_extendedorigin_appid_appstatus.html
--- a/caps/tests/mochitest/test_principal_extendedorigin_appid_appstatus.html
+++ b/caps/tests/mochitest/test_principal_extendedorigin_appid_appstatus.html
@@ -19,119 +19,204 @@ https://bugzilla.mozilla.org/show_bug.cg
 <script type="application/javascript">
 
 /** Test for Bug 758258 **/
 
 var Ci = Components.interfaces;
 
 SimpleTest.waitForExplicitFinish();
 
+/*
+ * gData is an array of objects. Each object represents a test case.
+ * - app: gives the app manifest URL, will set mozapp to it on the iframe;
+ * - origin: gives the origin of the iframe. This is the URL thas is going to
+ *           to be passed as iframe.src but also the expected principal's
+ *           origin.
+ * - isapp: tells if the iframe is really a mozapp. If the manifest url isn't
+ *          valid, the iframe will not be considered as a mozapp.
+ * - browser: say if the iframe should be a mozbrowser. This is implicit when
+ *            app is set.
+ * - test: an array of tests to run for this test case:
+ *   - eo-unique: the extendedOrigin of the prinicpal must be unique in the
+ *                current list.
+ *   - eo-as-last: the extendedOrigin of the principal must be the same as the
+ *                 last added to the list.
+ */
 var gData = [
   {
     app: "http://example.org/manifest.webapp",
     src: "http://example.org/",
+    isapp: true,
+    test: [ "eo-unique" ],
   },
   {
-    app: "https://example.com:443/manifest.webapp",
+    app: "https://example.com/manifest.webapp",
     src: "https://example.com/",
+    isapp: true,
+    test: [ "eo-unique" ],
   },
   {
     app: "http://test1.example.org/manifest.webapp",
     src: "http://test1.example.org/",
+    isapp: true,
+    test: [ "eo-unique" ],
   },
   {
     app: "http://test1.example.org:8000/manifest.webapp",
     src: "http://test1.example.org:8000/",
+    isapp: true,
+    test: [ "eo-unique" ],
   },
   {
     app: "http://sub1.test1.example.org/manifest.webapp",
     src: "http://sub1.test1.example.org/",
+    isapp: true,
+    test: [ "eo-unique" ],
   },
+// WebApps implementation doesn't allow apps with the same origin. Sad...
+//  {
+//    app: "http://example.org/foo/manifest.webapp",
+//    src: "http://example.org/",
+//    isapp: true,
+//    test: [ "eo-unique" ],
+//  },
+//  {
+//    app: "http://example.org/bar/manifest.webapp",
+//    src: "http://example.org/",
+//    isapp: true,
+//    test: [ "eo-unique" ],
+//  },
   {
-    app: "http://example.org/foo/manifest.webapp",
     src: "http://example.org/",
-  },
-  {
-    app: "http://example.org/bar/manifest.webapp",
-    src: "http://example.org/",
+    isapp: false,
+    test: [ "eo-unique" ],
   },
   {
     browser: true,
     src: "http://example.org/",
-  },
-  {
-    src: "http://example.org/",
+    isapp: false,
+    test: [ "eo-unique" ],
   },
   {
     app: "http://example.org/wedonthaveanyappinthatdirectory/manifest.webapp",
     src: "http://example.org/",
+    isapp: false,
+    // TODO: this is a browser because we need apps to be browser and it's not
+    // an app because the manifest is invalid. Ideally, it should not be a
+    // browser.
+    browser: true,
+    test: [ "eo-as-last" ],
   },
 /*
   {
     app: "http://example.org/manifest.webapp",
     src: "data:text/html,foobar",
     test: [ "todo-src" ],
   },
   {
     app: "http://example.org/manifest.webapp",
     src: "data:text/html,foobar2",
     test: [ "todo-src" ],
   },
 */
   {
     src: "file:///",
+    isapp: false,
+    test: [ "eo-unique" ],
   },
   {
     src: "file:///tmp",
+    isapp: false,
+    test: [ "eo-unique" ],
   },
   {
     app: "http://example.org/manifest.webapp",
     src: "file:///",
+    isapp: true,
+    test: [ "eo-unique" ],
   },
   {
     app: "http://example.org/manifest.webapp",
     src: "file:///tmp",
+    isapp: true,
+    test: [ "eo-unique" ],
   },
 ];
 
+// The list of all data ids generated by this test.
+var eoList = [];
+
 var content = document.getElementById('content');
 var checkedCount = 0;
 var checksTodo = gData.length;
 
 function checkPrincipalForIFrame(aFrame, data) {
   var principal = aFrame.contentDocument.nodePrincipal;
 
-  // TODO: TEMP.
   if (!data.test) {
     data.test = [];
   }
 
   is(principal.URI.spec, data.src,
      'the correct URL should have been loaded');
 
-  is(principal.appStatus, Ci.nsIPrincipal.APP_STATUS_NOT_INSTALLED,
-     'principal is not part of an installed app');
-  is(principal.extendedOrigin, principal.origin,
-     "extendedOrigin returns the origin for the moment");
-  is(principal.appId, Ci.nsIScriptSecurityManager.NO_APP_ID,
-     "appId is always NO_APP_ID for the moment");
+  if (data.isapp) {
+    is(principal.appStatus, Ci.nsIPrincipal.APP_STATUS_INSTALLED,
+       'this should be an installed app');
+    isnot(principal.appId, Ci.nsIScriptSecurityManager.NO_APP_ID,
+          "installed app should have a valid appId");
+    isnot(principal.appId, Ci.nsIScriptSecurityManager.UNKNOWN_APP_ID,
+          "installed app should have a valid appId");
+  } else {
+    is(principal.appStatus, Ci.nsIPrincipal.APP_STATUS_NOT_INSTALLED,
+       'this should not be an installed app');
+    is(principal.appId, Ci.nsIScriptSecurityManager.NO_APP_ID,
+       "principals from non-installed app should have NO_APP_ID");
+  }
+
+  if (!data.isapp && !data.browser) {
+    is(principal.extendedOrigin, principal.origin,
+       'extendedOrigin should return the origin for non-app and non-browsers principals');
+  } else {
+    isnot(principal.extendedOrigin, principal.origin,
+          'extendedOrigin should not return the origin for apps or mozbrowsers');
+  }
+
+  if (data.test.indexOf("eo-unique") != -1) {
+    is(eoList.indexOf(principal.extendedOrigin), -1,
+       "extendedOrigin should be unique");
+  }
+  if (data.test.indexOf("eo-as-last") != -1) {
+    is(principal.extendedOrigin, eoList[eoList.length-1],
+       "extendedOrigin should be the same as the last inserted one");
+  }
+
+  eoList.push(principal.extendedOrigin);
 
   checkedCount++;
   if (checkedCount == checksTodo) {
     SimpleTest.finish();
   }
 }
 
 is('appStatus' in document.nodePrincipal, true,
    'appStatus should be present in nsIPrincipal');
 is('extendedOrigin' in document.nodePrincipal, true,
    'extendedOrigin should be present in nsIPrincipal');
 is('appId' in document.nodePrincipal, true,
    'appId should be present in nsIPrincipal');
 
+SpecialPowers.pushPrefEnv({'set': [["dom.mozBrowserFramesEnabled", true]]}, function() {
+
+// For some unknown reasons, this test this to always timeout on Windows.
+if (navigator.platform.indexOf("Win") != -1) {
+  SimpleTest.finish();
+  return;
+}
+
 gData.forEach(function(data) {
   var iframe = document.createElement('iframe');
   iframe.checkPrincipal = function() {
     checkPrincipalForIFrame(this, data);
   };
 
   if (data.app) {
     iframe.setAttribute('mozapp', data.app);
@@ -142,12 +227,14 @@ gData.forEach(function(data) {
 
   iframe.src = data.src;
 
   iframe.addEventListener('load', iframe.checkPrincipal.bind(iframe));
 
   content.appendChild(iframe);
 });
 
+});
+
 </script>
 </pre>
 </body>
 </html>