Bug 1312260 - Part 2: Test access to "paste" command with clipboardRead permission draft
authorTomislav Jovanovic <tomica@gmail.com>
Fri, 10 Feb 2017 20:57:25 +0100
changeset 481950 7122106481d66493eb840b8273d2c576c9a9f583
parent 481308 8add08e8d2be902be711a9085c32bee6f809fe4f
child 481951 c7c98df4698ba6c577dcc1b832a3831c5fbd547c
push id44965
push userbmo:tomica@gmail.com
push dateFri, 10 Feb 2017 20:00:14 +0000
bugs1312260
milestone54.0a1
Bug 1312260 - Part 2: Test access to "paste" command with clipboardRead permission MozReview-Commit-ID: GTdI8cJArXs
browser/locales/en-US/chrome/browser/browser.properties
toolkit/components/extensions/schemas/manifest.json
toolkit/components/extensions/test/mochitest/test_clipboard.html
--- a/browser/locales/en-US/chrome/browser/browser.properties
+++ b/browser/locales/en-US/chrome/browser/browser.properties
@@ -77,16 +77,18 @@ webextPerms.updateMenuItem=%S requires n
 # %S is replaced with the localized name of the updated extension.
 # Note, this string will be used as raw markup. Avoid characters like <, >, &
 webextPerms.updateText=%S has been updated. You must approve new permissions before the updated version will install. Choosing “Cancel” will maintain your current add-on version.
 
 webextPerms.updateAccept.label=Update
 webextPerms.updateAccept.accessKey=U
 
 webextPerms.description.bookmarks=Read and modify bookmarks
+webextPerms.description.clipboardRead=Get data from the clipboard
+webextPerms.description.clipboardWrite=Input data to the clipboard
 webextPerms.description.downloads=Download files and read and modify the browser’s download history
 webextPerms.description.geolocation=Access your location
 webextPerms.description.history=Access browsing history
 # LOCALIZATION NOTE (webextPerms.description.nativeMessaging)
 # %S will be replaced with the name of the application
 webextPerms.description.nativeMessaging=Exchange messages with programs other than %S
 webextPerms.description.notifications=Display notifications to you
 webextPerms.description.sessions=Access recently closed tabs
--- a/toolkit/components/extensions/schemas/manifest.json
+++ b/toolkit/components/extensions/schemas/manifest.json
@@ -206,16 +206,17 @@
       },
       {
         "id": "Permission",
         "choices": [
           {
             "type": "string",
             "enum": [
               "alarms",
+              "clipboardRead",
               "clipboardWrite",
               "geolocation",
               "idle",
               "notifications",
               "storage"
             ]
           },
           { "$ref": "MatchPattern" }
--- a/toolkit/components/extensions/test/mochitest/test_clipboard.html
+++ b/toolkit/components/extensions/test/mochitest/test_clipboard.html
@@ -1,39 +1,51 @@
 <!DOCTYPE HTML>
 <html>
 <head>
-  <title>clipboard permission test</title>
+  <title>Clipboard permissions tests</title>
   <script src="/tests/SimpleTest/SimpleTest.js"></script>
   <script src="/tests/SimpleTest/SpawnTask.js"></script>
   <script src="/tests/SimpleTest/ExtensionTestUtils.js"></script>
   <script src="head.js"></script>
   <link rel="stylesheet" href="/tests/SimpleTest/test.css">
 </head>
 <body>
 
 <script>
 "use strict";
 
-function doCopy(txt) {
+/* globals doCopy, doPaste */
+function shared() {
   let field = document.createElement("textarea");
   document.body.appendChild(field);
-  field.value = txt;
-  field.select();
-  return document.execCommand("copy");
+  field.contentEditable = true;
+
+  this.doCopy = function(txt) {
+    field.value = txt;
+    field.select();
+    return document.execCommand("copy");
+  };
+
+  this.doPaste = function() {
+    field.select();
+    return document.execCommand("paste") && field.value;
+  };
 }
 
-add_task(function* no_permission_deny_copy() {
+add_task(function* test_background_clipboard_permissions() {
   function backgroundScript() {
     browser.test.assertEq(false, doCopy("whatever"),
       "copy should be denied without permission");
+    browser.test.assertEq(false, doPaste(),
+      "paste should be denied without permission");
     browser.test.sendMessage("ready");
   }
   let extensionData = {
-    background: `${doCopy};(${backgroundScript})();`,
+    background: `(${shared})();(${backgroundScript})();`,
   };
   let extension = ExtensionTestUtils.loadExtension(extensionData);
   yield extension.startup();
 
   yield extension.awaitMessage("ready");
 
   yield extension.unload();
 });
@@ -64,63 +76,67 @@ add_task(function* with_permission_allow
     SimpleTest.waitForClipboard(DUMMY_STR, () => {
       extension.sendMessage(DUMMY_STR);
     }, resolve, resolve);
   });
 
   yield extension.unload();
 }); */
 
-add_task(function* content_script_no_permission_deny_copy() {
+add_task(function* test_contentscript_clipboard_permissions() {
   function contentScript() {
     browser.test.assertEq(false, doCopy("whatever"),
       "copy should be denied without permission");
+    browser.test.assertEq(false, doPaste(),
+      "paste should be denied without permission");
     browser.test.sendMessage("ready");
   }
   let extensionData = {
     manifest: {
       content_scripts: [{
-        js: ["contentscript.js"],
+        js: ["shared.js", "contentscript.js"],
         matches: ["http://mochi.test/*/file_sample.html"],
       }],
     },
     files: {
-      "contentscript.js": `${doCopy};(${contentScript})();`,
+      "shared.js": shared,
+      "contentscript.js": contentScript,
     },
   };
   let extension = ExtensionTestUtils.loadExtension(extensionData);
   yield extension.startup();
 
   let win = window.open("file_sample.html");
   yield extension.awaitMessage("ready");
   win.close();
 
   yield extension.unload();
 });
 
-add_task(function* content_script_with_permission_allow_copy() {
+add_task(function* test_contentscript_clipboard_copy() {
   function contentScript() {
     browser.test.onMessage.addListener(txt => {
       browser.test.assertEq(true, doCopy(txt),
         "copy should be allowed with permission");
     });
     browser.test.sendMessage("ready");
   }
   let extensionData = {
     manifest: {
       content_scripts: [{
-        js: ["contentscript.js"],
+        js: ["shared.js", "contentscript.js"],
         matches: ["http://mochi.test/*/file_sample.html"],
       }],
       permissions: [
         "clipboardWrite",
       ],
     },
     files: {
-      "contentscript.js": `${doCopy};(${contentScript})();`,
+      "shared.js": shared,
+      "contentscript.js": contentScript,
     },
   };
   let extension = ExtensionTestUtils.loadExtension(extensionData);
   yield extension.startup();
 
   let win = window.open("file_sample.html");
   yield extension.awaitMessage("ready");
 
@@ -130,11 +146,44 @@ add_task(function* content_script_with_p
       extension.sendMessage(DUMMY_STR);
     }, resolve, resolve);
   });
 
   win.close();
 
   yield extension.unload();
 });
+
+add_task(function* test_contentscript_clipboard_paste() {
+  const extension = ExtensionTestUtils.loadExtension({
+    manifest: {
+      permissions: [
+        "clipboardRead",
+      ],
+      content_scripts: [{
+        matches: ["http://mochi.test/*/file_sample.html"],
+        js: ["shared.js", "content_script.js"],
+      }],
+    },
+    files: {
+      "shared.js": shared,
+      "content_script.js": () => {
+        browser.test.sendMessage("paste", doPaste());
+      },
+    },
+  });
+
+  const STRANGE = "A Strange Thing";
+  SpecialPowers.clipboardCopyString(STRANGE);
+
+  yield extension.startup();
+  const win = window.open("file_sample.html");
+
+  const paste = yield extension.awaitMessage("paste");
+  is(paste, STRANGE, "the correct string was pasted");
+
+  win.close();
+  yield extension.unload();
+});
+
 </script>
 </body>
 </html>