Bug 1263167 - Complete test coverage for browser.extension.inIncognitoContext, r?kmag draft
authorBob Silverberg <bsilverberg@mozilla.com>
Fri, 08 Apr 2016 10:19:19 -0400
changeset 374095 320886556c0183c43015e37e9eb128dca4407b70
parent 374094 359674b9970a98e42c97184e9d7c59c89bac06d7
child 522543 550533de06a33b724b5daee2e081bfcb12f95111
push id19921
push userbmo:bob.silverberg@gmail.com
push dateWed, 01 Jun 2016 19:34:56 +0000
reviewerskmag
bugs1263167
milestone49.0a1
Bug 1263167 - Complete test coverage for browser.extension.inIncognitoContext, r?kmag MozReview-Commit-ID: HQ9cmZMhn1o
toolkit/components/extensions/test/mochitest/mochitest.ini
toolkit/components/extensions/test/mochitest/test_ext_extension.html
toolkit/components/extensions/test/mochitest/test_ext_inIncognitoContext_window.html
--- a/toolkit/components/extensions/test/mochitest/mochitest.ini
+++ b/toolkit/components/extensions/test/mochitest/mochitest.ini
@@ -32,16 +32,18 @@ support-files =
   file_sample.html
   redirection.sjs
   file_privilege_escalation.html
   file_ext_test_api_injection.js
   file_permission_xhr.html
   file_download.txt
 
 [test_ext_extension.html]
+[test_ext_inIncognitoContext_window.html]
+skip-if = os == 'android' # Android does not currently support windows.
 [test_ext_simple.html]
 [test_ext_geturl.html]
 [test_ext_content_security_policy.html]
 [test_ext_contentscript.html]
 skip-if = buildapp == 'b2g' # runat != document_idle is not supported.
 [test_ext_contentscript_api_injection.html]
 [test_ext_contentscript_create_iframe.html]
 [test_ext_contentscript_devtools_metadata.html]
--- a/toolkit/components/extensions/test/mochitest/test_ext_extension.html
+++ b/toolkit/components/extensions/test/mochitest/test_ext_extension.html
@@ -9,33 +9,47 @@
   <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
 </head>
 <body>
 
 <script type="text/javascript">
 "use strict";
 
 add_task(function* test_is_allowed_incognito_access() {
-  function backgroundScript() {
+  function background() {
     browser.extension.isAllowedIncognitoAccess().then(isAllowedIncognitoAccess => {
       browser.test.assertEq(true, isAllowedIncognitoAccess, "isAllowedIncognitoAccess is true");
       browser.test.notifyPass("isAllowedIncognitoAccess");
     });
   }
 
   let extension = ExtensionTestUtils.loadExtension({
-    background: `(${backgroundScript})()`,
+    background: `(${background})()`,
     manifest: {},
   });
 
   yield extension.startup();
-  info("extension loaded");
   yield extension.awaitFinish("isAllowedIncognitoAccess");
   yield extension.unload();
-  info("extension unloaded");
+});
+
+add_task(function* test_in_incognito_context_false() {
+  function background() {
+    browser.test.assertEq(false, browser.extension.inIncognitoContext, "inIncognitoContext returned false");
+    browser.test.notifyPass("inIncognitoContext");
+  }
+
+  let extension = ExtensionTestUtils.loadExtension({
+    background: `(${background})()`,
+    manifest: {},
+  });
+
+  yield extension.startup();
+  yield extension.awaitFinish("inIncognitoContext");
+  yield extension.unload();
 });
 
 add_task(function* test_is_allowed_file_scheme_access() {
   function backgroundScript() {
     browser.extension.isAllowedFileSchemeAccess().then(isAllowedFileSchemeAccess => {
       browser.test.assertEq(false, isAllowedFileSchemeAccess, "isAllowedFileSchemeAccess is false");
       browser.test.notifyPass("isAllowedFileSchemeAccess");
     });
new file mode 100644
--- /dev/null
+++ b/toolkit/components/extensions/test/mochitest/test_ext_inIncognitoContext_window.html
@@ -0,0 +1,50 @@
+<!DOCTYPE HTML>
+<html>
+<head>
+  <title>WebExtension test</title>
+  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
+  <script type="text/javascript" src="/tests/SimpleTest/SpawnTask.js"></script>
+  <script type="text/javascript" src="/tests/SimpleTest/ExtensionTestUtils.js"></script>
+  <script type="text/javascript" src="head.js"></script>
+  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
+</head>
+<body>
+
+<script type="text/javascript">
+"use strict";
+
+add_task(function* test_in_incognito_context_true() {
+  function background() {
+    browser.runtime.onMessage.addListener(msg => {
+      browser.test.assertEq(true, msg, "inIncognitoContext is true");
+      browser.test.notifyPass("inIncognitoContext");
+    });
+
+    browser.windows.create({url: browser.runtime.getURL("/tab.html"), incognito: true});
+  }
+
+  function tabScript() {
+    browser.runtime.sendMessage(browser.extension.inIncognitoContext);
+  }
+
+  let extension = ExtensionTestUtils.loadExtension({
+    background: `(${background})()`,
+    manifest: {},
+    files: {
+      "tab.js": `(${tabScript})()`,
+      "tab.html": `<!DOCTYPE html><html><head>
+        <meta charset="utf-8">
+        <script src="tab.js"></${"script"}>
+      </head></html>`,
+    },
+  });
+
+  yield extension.startup();
+  yield extension.awaitFinish("inIncognitoContext");
+  yield extension.unload();
+});
+
+</script>
+
+</body>
+</html>