Created
April 14, 2016 21:37
-
-
Save rpl/b96a909cc357430cfe5364e849d5158e to your computer and use it in GitHub Desktop.
share content scripts helper function in WebExtensions mochitest
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| diff --git a/toolkit/components/extensions/test/mochitest/test_ext_web_accessible_resources.html b/toolkit/components/extensions/test/mochitest/test_ext_web_accessible_resources.html | |
| --- a/toolkit/components/extensions/test/mochitest/test_ext_web_accessible_resources.html | |
| +++ b/toolkit/components/extensions/test/mochitest/test_ext_web_accessible_resources.html | |
| @@ -18,47 +18,45 @@ | |
| SimpleTest.registerCleanupFunction(() => { | |
| SpecialPowers.clearUserPref("security.mixed_content.block_display_content"); | |
| }); | |
| let image = atob("iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQMAAAAl21bKAAAAA1BMVEUAA" + | |
| "ACnej3aAAAAAXRSTlMAQObYZgAAAApJREFUCNdjYAAAAAIAAeIhvDMAAAAASUVORK5CYII="); | |
| const IMAGE_ARRAYBUFFER = Uint8Array.from(image, byte => byte.charCodeAt(0)).buffer; | |
| -function contentScriptHelper() { | |
| - this.testImageLoading = function(src, expectedAction) { | |
| - let imageLoadingPromise = new Promise((resolve, reject) => { | |
| - let testImage = document.createElement("img"); | |
| - testImage.setAttribute("src", src); | |
| +function testImageLoading(src, expectedAction) { | |
| + let imageLoadingPromise = new Promise((resolve, reject) => { | |
| + let testImage = document.createElement("img"); | |
| + testImage.setAttribute("src", src); | |
| - let loadListener = () => { | |
| - cleanupListeners(); | |
| - resolve(expectedAction === "loaded"); | |
| - }; | |
| + let loadListener = () => { | |
| + cleanupListeners(); | |
| + resolve(expectedAction === "loaded"); | |
| + }; | |
| - let errorListener = () => { | |
| - cleanupListeners(); | |
| - resolve(expectedAction === "blocked"); | |
| - }; | |
| + let errorListener = () => { | |
| + cleanupListeners(); | |
| + resolve(expectedAction === "blocked"); | |
| + }; | |
| - let cleanupListeners = () => { | |
| - testImage.removeEventListener("load", loadListener); | |
| - testImage.removeEventListener("error", errorListener); | |
| - } | |
| + let cleanupListeners = () => { | |
| + testImage.removeEventListener("load", loadListener); | |
| + testImage.removeEventListener("error", errorListener); | |
| + } | |
| - testImage.addEventListener("load", loadListener); | |
| - testImage.addEventListener("error", errorListener); | |
| + testImage.addEventListener("load", loadListener); | |
| + testImage.addEventListener("error", errorListener); | |
| - document.body.appendChild(testImage); | |
| - }); | |
| + document.body.appendChild(testImage); | |
| + }); | |
| - imageLoadingPromise.then(success => { | |
| - browser.runtime.sendMessage({name: "image-loading", expectedAction, success}); | |
| - }); | |
| - } | |
| + imageLoadingPromise.then(success => { | |
| + browser.runtime.sendMessage({name: "image-loading", expectedAction, success}); | |
| + }); | |
| } | |
| add_task(function* test_web_accessible_resources() { | |
| function background() { | |
| let gotURL; | |
| let tabId; | |
| function loadFrame(url) { | |
| @@ -201,17 +199,17 @@ add_task(function* test_web_accessible_r | |
| } | |
| function content() { | |
| browser.runtime.onMessage.addListener(msg => { | |
| if (msg !== "load-resources") { | |
| return; | |
| } | |
| - this.testImageLoading(browser.extension.getURL("image.png"), "loaded"); | |
| + testImageLoading(browser.extension.getURL("image.png"), "loaded"); | |
| let testScript = document.createElement("script"); | |
| testScript.setAttribute("src", browser.extension.getURL("test_script.js")); | |
| document.head.appendChild(testScript); | |
| browser.runtime.sendMessage("script-loaded"); | |
| }); | |
| window.addEventListener("message", function rcv(event) { | |
| @@ -235,17 +233,17 @@ add_task(function* test_web_accessible_r | |
| }], | |
| "web_accessible_resources": [ | |
| "image.png", | |
| "test_script.js", | |
| ], | |
| }, | |
| background: `(${background})()`, | |
| files: { | |
| - "content_script_helper.js": `(${contentScriptHelper})()`, | |
| + "content_script_helper.js": `${testImageLoading}`, | |
| "content_script.js": `(${content})()`, | |
| "test_script.js": `(${testScript})()`, | |
| "image.png": IMAGE_ARRAYBUFFER, | |
| }, | |
| }); | |
| // This is used to watch the blocked data bounce off CSP. | |
| function examiner() { | |
| @@ -300,18 +298,18 @@ add_task(function* test_web_accessible_r | |
| browser.test.sendMessage(msg); | |
| } | |
| }); | |
| browser.test.sendMessage("background-ready"); | |
| } | |
| function content() { | |
| - this.testImageLoading("http://example.com/tests/toolkit/components/extensions/test/mochitest/file_image_bad.png", "blocked"); | |
| - this.testImageLoading(browser.extension.getURL("image.png"), "loaded"); | |
| + testImageLoading("http://example.com/tests/toolkit/components/extensions/test/mochitest/file_image_bad.png", "blocked"); | |
| + testImageLoading(browser.extension.getURL("image.png"), "loaded"); | |
| let testScript = document.createElement("script"); | |
| testScript.setAttribute("src", browser.extension.getURL("test_script.js")); | |
| document.head.appendChild(testScript); | |
| window.addEventListener("message", event => { | |
| browser.runtime.sendMessage(event.data); | |
| }); | |
| @@ -330,17 +328,17 @@ add_task(function* test_web_accessible_r | |
| }], | |
| "web_accessible_resources": [ | |
| "image.png", | |
| "test_script.js", | |
| ], | |
| }, | |
| background: `(${background})()`, | |
| files: { | |
| - "content_script_helper.js": `(${contentScriptHelper})()`, | |
| + "content_script_helper.js": `${testImageLoading}`, | |
| "content_script.js": `(${content})()`, | |
| "test_script.js": `(${testScript})()`, | |
| "image.png": IMAGE_ARRAYBUFFER, | |
| }, | |
| }); | |
| SpecialPowers.setBoolPref("security.mixed_content.block_display_content", true); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment