Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into fix/ms-edge
- Loading branch information
Showing
23 changed files
with
821 additions
and
210 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
const browserify = require("browserify"); | ||
|
||
const b = browserify(); | ||
|
||
b.add("./test/fixtures/tape-standalone.js"); | ||
b.transform("global-replaceify", { | ||
global: true, | ||
replacements: { | ||
setImmediate: "require('timers').setImmediate", | ||
}, | ||
}); | ||
b.bundle().pipe(process.stdout); |
18 changes: 18 additions & 0 deletions
18
test/fixtures/detect-browser-api-object-in-content-script/content.js
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
test("browser api object in content script", (t) => { | ||
t.ok(browser && browser.runtime, "a global browser API object should be defined"); | ||
t.ok(chrome && chrome.runtime, "a global chrome API object should be defined"); | ||
|
||
if (navigator.userAgent.includes("Firefox/")) { | ||
// Check that the polyfill didn't create a polyfill wrapped browser API object on Firefox. | ||
t.equal(browser.runtime, chrome.runtime, "browser.runtime and chrome.runtime should be equal on Firefox"); | ||
// On Firefox, window is not the global object for content scripts, and so we expect window.browser to not | ||
// be defined. | ||
t.equal(window.browser, undefined, "window.browser is expected to be undefined on Firefox"); | ||
} else { | ||
// Check that the polyfill has created a wrapped API namespace as expected. | ||
t.notEqual(browser.runtime, chrome.runtime, "browser.runtime and chrome.runtime should not be equal"); | ||
// On chrome, window is the global object and so the polyfilled browser API should | ||
// be also equal to window.browser. | ||
t.equal(browser, window.browser, "browser and window.browser should be the same object"); | ||
} | ||
}); |
19 changes: 19 additions & 0 deletions
19
test/fixtures/detect-browser-api-object-in-content-script/manifest.json
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"manifest_version": 2, | ||
"name": "test-detect-browser-api-object-in-content-script", | ||
"version": "0.1", | ||
"description": "test-detect-browser-api-object-in-content-script", | ||
"content_scripts": [ | ||
{ | ||
"matches": [ | ||
"http://localhost/*" | ||
], | ||
"js": [ | ||
"browser-polyfill.js", | ||
"tape.js", | ||
"content.js" | ||
] | ||
} | ||
], | ||
"permissions": [] | ||
} |
39 changes: 39 additions & 0 deletions
39
test/fixtures/multiple-onmessage-listeners-extension/background.js
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
console.log(name, "background page loaded"); | ||
|
||
async function testMessageHandler(msg, sender) { | ||
console.log(name, "background received msg", {msg, sender}); | ||
|
||
// We only expect messages coming from a content script in this test. | ||
if (!sender.tab || !msg.startsWith("test-multiple-onmessage-listeners:")) { | ||
return { | ||
success: false, | ||
failureReason: `An unexpected message has been received: ${JSON.stringify({msg, sender})}`, | ||
}; | ||
} | ||
|
||
if (msg.endsWith(":resolve-to-undefined")) { | ||
return undefined; | ||
} | ||
|
||
if (msg.endsWith(":resolve-to-null")) { | ||
return null; | ||
} | ||
|
||
return { | ||
success: false, | ||
failureReason: `An unexpected message has been received: ${JSON.stringify({msg, sender})}`, | ||
}; | ||
} | ||
|
||
// Register the same message handler twice. | ||
browser.runtime.onMessage.addListener(testMessageHandler); | ||
browser.runtime.onMessage.addListener(testMessageHandler); | ||
|
||
// Register an additional message handler that always reply after | ||
// a small latency time. | ||
browser.runtime.onMessage.addListener(async (msg, sender) => { | ||
await new Promise(resolve => setTimeout(resolve, 100)); | ||
return "resolved-to-string-with-latency"; | ||
}); | ||
|
||
console.log(name, "background page ready to receive a content script message..."); |
18 changes: 18 additions & 0 deletions
18
test/fixtures/multiple-onmessage-listeners-extension/content.js
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
test("Multiple runtime.onmessage listeners which resolve to undefined", async (t) => { | ||
const res = await browser.runtime.sendMessage("test-multiple-onmessage-listeners:resolve-to-undefined"); | ||
|
||
if (navigator.userAgent.includes("Firefox/")) { | ||
t.deepEqual(res, undefined, "Got an undefined value as expected"); | ||
} else { | ||
// NOTE: When an onMessage listener sends `undefined` in a response, | ||
// Chrome internally converts it to null and the receiver receives it | ||
// as a null object. | ||
t.deepEqual(res, null, "Got a null value as expected on Chrome"); | ||
} | ||
}); | ||
|
||
test("Multiple runtime.onmessage listeners which resolve to null", async (t) => { | ||
const res = await browser.runtime.sendMessage("test-multiple-onmessage-listeners:resolve-to-null"); | ||
|
||
t.deepEqual(res, null, "Got a null value as expected"); | ||
}); |
25 changes: 25 additions & 0 deletions
25
test/fixtures/multiple-onmessage-listeners-extension/manifest.json
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
{ | ||
"manifest_version": 2, | ||
"name": "test-multiple-onmessage-listeners", | ||
"version": "0.1", | ||
"description": "test-multiple-onmessage-listeners", | ||
"content_scripts": [ | ||
{ | ||
"matches": [ | ||
"http://localhost/*" | ||
], | ||
"js": [ | ||
"browser-polyfill.js", | ||
"tape.js", | ||
"content.js" | ||
] | ||
} | ||
], | ||
"permissions": [], | ||
"background": { | ||
"scripts": [ | ||
"browser-polyfill.js", | ||
"background.js" | ||
] | ||
} | ||
} |
Oops, something went wrong.