New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
test: Add cross browsers smoke tests and add support for sendMessage rejected promise behavior #115
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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" | ||
] | ||
} | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Try
Promise.reject({message: "rejected-non-error-value"})
instead.If you wish, keep the existing test case, but check that no useful error message is passed through.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If keeping the existing test case, maybe check specifically for the message "An unexpected error occurred"? (EDIT: I guess it's kind of an implementation detail, but I think it might be easier to do it this way.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For what it's worth, I think this mistake was already present in the code, not introduced by the pull request.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added both the cases, to be fair I think that the behavior that Firefox have when an extension code rejects a plain object with a message property is actually an involuntary side-effect of the behavior we wanted when an API implementation rejects that kind of plain objects:
https://searchfox.org/mozilla-central/search?q=Promise.reject(%7Bmessage%3A&case=false®exp=false&path=
Anyway, the polyfill is currently matching this behavior and I added an explicit test case for it.
@james-cnz Like I described in one of the above comments I'm not asserting the "An unexpected error occurred" error message because Firefox 59 and Firefox 60 have different behaviors (and so the test was failing on travis only on Firefox).