Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(browsers-smoketests): Run a set of smoke tests on both Chrome an…
…d Firefox This commit introduces tape as the test framework used to define the tests in the test extension contexts and send them to the nodejs script that orchestrate the test run. The nodejs script has also been migrated from mocha to tape, it uses the custom test helpers provided to setup the test environment (e.g. create a temporary dir for the test extension, copy the last polyfill build, bundle tape to be used in the test extension, start the browser which run the test extension and finally collect the results of the test extension) and then it merges all the tap logs collected from every test extension into a single "per browser" test suite. - updated travis nodejs environment to nodejs 8 - uses tape to collect test results from inside the test extension - added test case to check polyfill 'existing browser API object' detection - added test for expected rejection on tabs.sendMessage with an invalid tabId - added test with multiple listeners which resolves to undefined and null - optionally run chrome smoketests with --enable-features=NativeCrxBindings
- Loading branch information
Showing
20 changed files
with
538 additions
and
159 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
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
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 |
---|---|---|
|
@@ -10,6 +10,7 @@ | |
], | ||
"js": [ | ||
"browser-polyfill.js", | ||
"tape.js", | ||
"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,36 @@ | ||
console.log(name, "background page loaded"); | ||
|
||
browser.runtime.onMessage.addListener(async (msg, sender, sendResponse) => { | ||
console.log(name, "background received msg", {msg, sender}); | ||
|
||
// We only expect messages coming from a content script in this test. | ||
if (!sender.tab || msg != "test-tabssendMessage-unknown-tabid") { | ||
return { | ||
success: false, | ||
failureReason: `An unexpected message has been received: ${JSON.stringify({msg, sender})}`, | ||
}; | ||
} | ||
|
||
try { | ||
const tabs = await browser.tabs.query({}); | ||
const lastValidTabId = tabs.reduce((acc, tab) => { | ||
return Math.max(acc, tab.id); | ||
}, 0); | ||
const INVALID_TABID = lastValidTabId + 100; | ||
|
||
await browser.tabs.sendMessage(INVALID_TABID, "message-to-unknown-tab"); | ||
|
||
return { | ||
success: false, | ||
failureReason: `browser.tabs.sendMessage should reject on sending messages to non-existing tab`, | ||
}; | ||
} catch (err) { | ||
return { | ||
success: true, | ||
isRejected: true, | ||
errorMessage: err.message, | ||
}; | ||
} | ||
}); | ||
|
||
console.log(name, "background page ready to receive a content script message..."); |
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,8 @@ | ||
test("tabs.sendMessage reject when sending to unknown tab id", async (t) => { | ||
const res = await browser.runtime.sendMessage("test-tabssendMessage-unknown-tabid"); | ||
t.deepEqual(res, { | ||
success: true, | ||
isRejected: true, | ||
errorMessage: "Could not establish connection. Receiving end does not exist.", | ||
}, "The background page got a rejection as expected"); | ||
}); |
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-tabs-sendmessage", | ||
"version": "0.1", | ||
"description": "test-tabs-sendmessage", | ||
"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.