Bug 1362047 - Fix default value for content script run_at, add tests r=mixedpuppy
authorTomislav Jovanovic <tomica@gmail.com>
Wed, 10 May 2017 02:00:29 +0200
changeset 358358 02c060b287ffd5a1dbca8f9ebd4c339f91beebf6
parent 358357 c9060b0b93ce30351a5f45964460b4a69632cda3
child 358359 20127bd6337b46825b271e8c90662223db05da5f
push id42653
push userryanvm@gmail.com
push dateSun, 14 May 2017 19:23:44 +0000
treeherderautoland@02c060b287ff [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmixedpuppy
bugs1362047
milestone55.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 1362047 - Fix default value for content script run_at, add tests r=mixedpuppy MozReview-Commit-ID: CiBaSkZ0TqQ
browser/components/extensions/test/browser/browser_ext_tabs_executeScript_runAt.js
toolkit/components/extensions/schemas/extension_types.json
toolkit/components/extensions/schemas/manifest.json
toolkit/components/extensions/test/xpcshell/test_ext_contentscript.js
toolkit/components/extensions/test/xpcshell/test_ext_contentscript_xrays.js
toolkit/components/extensions/test/xpcshell/test_ext_i18n.js
--- a/browser/components/extensions/test/browser/browser_ext_tabs_executeScript_runAt.js
+++ b/browser/components/extensions/test/browser/browser_ext_tabs_executeScript_runAt.js
@@ -47,16 +47,20 @@ add_task(async function testExecuteScrip
         await loadingPromise;
 
         let states = await Promise.all([
           // Send the executeScript requests in the reverse order that we expect
           // them to execute in, to avoid them passing only because of timing
           // races.
           browser.tabs.executeScript({
             code: "document.readyState",
+            // Testing default `runAt`.
+          }),
+          browser.tabs.executeScript({
+            code: "document.readyState",
             runAt: "document_idle",
           }),
           browser.tabs.executeScript({
             code: "document.readyState",
             runAt: "document_end",
           }),
           browser.tabs.executeScript({
             code: "document.readyState",
@@ -72,17 +76,18 @@ add_task(async function testExecuteScrip
                                 `document_end state is valid: ${states[1]}`);
         browser.test.assertTrue(states[2] == "complete",
                                 `document_idle state is valid: ${states[2]}`);
 
         // If we have the earliest valid states for each script, we're done.
         // Otherwise, try again.
         success = (states[0] == "loading" &&
                    states[1] == "interactive" &&
-                   states[2] == "complete");
+                   states[2] == "complete" &&
+                   states[3] == "complete");
       }
 
       browser.test.assertTrue(success, "Got the earliest expected states at least once");
 
       browser.test.notifyPass("executeScript-runAt");
     } catch (e) {
       browser.test.fail(`Error: ${e} :: ${e.stack}`);
       browser.test.notifyFail("executeScript-runAt");
--- a/toolkit/components/extensions/schemas/extension_types.json
+++ b/toolkit/components/extensions/schemas/extension_types.json
@@ -57,16 +57,17 @@
             "type": "integer",
             "minimum": 0,
             "optional": true,
             "description": "The ID of the frame to inject the script into. This may not be used in combination with <code>allFrames</code>."
           },
           "runAt": {
             "$ref": "RunAt",
             "optional": true,
+            "default": "document_idle",
             "description": "The soonest that the JavaScript or CSS will be injected into the tab. Defaults to \"document_idle\"."
           },
           "cssOrigin": {
             "$ref": "CSSOrigin",
             "optional": true,
             "description": "The css origin of the stylesheet to inject. Defaults to \"author\"."
           }
         }
--- a/toolkit/components/extensions/schemas/manifest.json
+++ b/toolkit/components/extensions/schemas/manifest.json
@@ -357,16 +357,17 @@
             "description": "The list of CSS files to inject",
             "items": { "$ref": "ExtensionURL" }
           },
           "all_frames": {"type": "boolean", "optional": true, "description": "If allFrames is <code>true</code>, implies that the JavaScript or CSS should be injected into all frames of current page. By default, it's <code>false</code> and is only injected into the top frame."},
           "match_about_blank": {"type": "boolean", "optional": true, "description": "If matchAboutBlank is true, then the code is also injected in about:blank and about:srcdoc frames if your extension has access to its parent document. Code cannot be inserted in top-level about:-frames. By default it is <code>false</code>."},
           "run_at": {
             "$ref": "extensionTypes.RunAt",
             "optional": true,
+            "default": "document_idle",
             "description": "The soonest that the JavaScript or CSS will be injected into the tab. Defaults to \"document_idle\"."
           }
         }
       },
       {
         "id": "IconPath",
         "choices": [
           {
--- a/toolkit/components/extensions/test/xpcshell/test_ext_contentscript.js
+++ b/toolkit/components/extensions/test/xpcshell/test_ext_contentscript.js
@@ -2,17 +2,17 @@
 
 const server = createHttpServer();
 server.registerDirectory("/data/", do_get_file("data"));
 
 const BASE_URL = `http://localhost:${server.identity.primaryPort}/data`;
 
 ExtensionTestUtils.mockAppInfo();
 
-add_task(async function test_contentscript() {
+add_task(async function test_contentscript_runAt() {
   function background() {
     browser.runtime.onMessage.addListener(([msg, expectedStates, readyState], sender) => {
       if (msg == "chrome-namespace-ok") {
         browser.test.sendMessage(msg);
         return;
       }
 
       browser.test.assertEq("script-run", msg, "message type is correct");
@@ -54,16 +54,21 @@ add_task(async function test_contentscri
         },
         {
           "matches": ["http://*/*/file_sample.html"],
           "js": ["content_script_idle.js"],
           "run_at": "document_idle",
         },
         {
           "matches": ["http://*/*/file_sample.html"],
+          "js": ["content_script_idle.js"],
+          // Test default `run_at`.
+        },
+        {
+          "matches": ["http://*/*/file_sample.html"],
           "js": ["content_script.js"],
           "run_at": "document_idle",
         },
       ],
     },
     background,
 
     files: {
@@ -78,27 +83,32 @@ add_task(async function test_contentscri
 
   let loadingCount = 0;
   let interactiveCount = 0;
   let completeCount = 0;
   extension.onMessage("script-run-loading", () => { loadingCount++; });
   extension.onMessage("script-run-interactive", () => { interactiveCount++; });
 
   let completePromise = new Promise(resolve => {
-    extension.onMessage("script-run-complete", () => { completeCount++; resolve(); });
+    extension.onMessage("script-run-complete", () => {
+      completeCount++;
+      if (completeCount > 1) {
+        resolve();
+      }
+    });
   });
 
   let chromeNamespacePromise = extension.awaitMessage("chrome-namespace-ok");
 
   await extension.startup();
 
   let contentPage = await ExtensionTestUtils.loadContentPage(`${BASE_URL}/file_sample.html`);
 
   await Promise.all([completePromise, chromeNamespacePromise]);
 
   await contentPage.close();
 
   equal(loadingCount, 1, "document_start script ran exactly once");
   equal(interactiveCount, 1, "document_end script ran exactly once");
-  equal(completeCount, 1, "document_idle script ran exactly once");
+  equal(completeCount, 2, "document_idle script ran exactly twice");
 
   await extension.unload();
 });
--- a/toolkit/components/extensions/test/xpcshell/test_ext_contentscript_xrays.js
+++ b/toolkit/components/extensions/test/xpcshell/test_ext_contentscript_xrays.js
@@ -1,12 +1,14 @@
 "use strict";
 
 Cu.import("resource://gre/modules/Preferences.jsm");
 
+ExtensionTestUtils.mockAppInfo();
+
 const server = createHttpServer();
 server.registerDirectory("/data/", do_get_file("data"));
 
 const BASE_URL = `http://localhost:${server.identity.primaryPort}/data`;
 const XRAY_PREF = "dom.allow_named_properties_object_for_xrays";
 
 add_task(async function test_contentscript_xrays() {
   async function contentScript() {
@@ -64,9 +66,8 @@ add_task(async function test_contentscri
   await extension.startup();
   let contentPage = await ExtensionTestUtils.loadContentPage(`${BASE_URL}/file_sample.html`);
 
   await extension.awaitFinish("contentScriptXrays");
 
   await contentPage.close();
   await extension.unload();
 });
-
--- a/toolkit/components/extensions/test/xpcshell/test_ext_i18n.js
+++ b/toolkit/components/extensions/test/xpcshell/test_ext_i18n.js
@@ -1,12 +1,13 @@
 "use strict";
 
 Cu.import("resource://gre/modules/Preferences.jsm");
 
+ExtensionTestUtils.mockAppInfo();
 
 const server = createHttpServer();
 server.registerDirectory("/data/", do_get_file("data"));
 
 const BASE_URL = `http://localhost:${server.identity.primaryPort}/data`;
 
 do_register_cleanup(() => {
   Preferences.reset("intl.accept_languages");