Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickkettner committed Sep 10, 2024
1 parent 75b7550 commit a216f40
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 16 deletions.
5 changes: 5 additions & 0 deletions webextensions/browser.runtime.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@
<script>
const asyncTests = {};

// Test are executed by the extensions inside of their background script
// or in a content script. The results are sent back to the test runner
// via postMessage.
window.addEventListener("message", async ({data}) => {
// WPTTestResults is a bool set on the test runner being run by
// the extension.
if (data.WPTTestResults) {
const {name} = data

Expand Down
3 changes: 3 additions & 0 deletions webextensions/resources/runtime/tests/getBrowserInfo.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
asyncTests({
"getBrowserInfo is a function": async () => {
return getType(browser.runtime?.getBrowserInfo) === 'Function'
},
"getBrowserInfo returns a Promise": async () => {
try {
return getType(browser.runtime?.getBrowserInfo()) === 'Promise'
Expand Down
3 changes: 3 additions & 0 deletions webextensions/resources/runtime/tests/getFrameId.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
emitTestResult(`browser.runtime.getFrameId works`, (() => {
if (typeof(browser.runtime.getFrameId) !== 'function') {
return false
}
return typeof(browser.runtime.getFrameId(self)) === 'number'
})())
31 changes: 15 additions & 16 deletions webextensions/resources/runtime/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,23 @@ function isForeground() {
return 'window' in globalThis
}

function requestIframeTest() {
function requestIframeTest(testName) {
console.log('requesting iframe test: ', testName)
return new Promise((resolve, reject) => {
const iframe = document.querySelector('iframe');
if (!iframe.dataset.loaded) {
iframe.src = chrome.runtime.getURL('utility_page.html');
iframe.dataset.loaded = true;
iframe.onload = () => {
resolve(iframe);
};
} else {
return resolve(iframe);
const iframe = document.createElement('iframe');
iframe.id = testName.replace(/\s/g, '_');
iframe.src = chrome.runtime.getURL(`utility_page.html?${testName}`);
iframe.dataset.loaded = true;
iframe.onload = () => {
resolve(iframe);
}
document.body.appendChild(iframe);
})
}

function emitAsyncTest(name, result) {
const _thisTest = tests[name];
emitTestResult(name, null, true, result)
function emitAsyncTest(testName, result) {
const _thisTest = tests[testName];
emitTestResult(testName, null, true, result)
}

let tests = {};
Expand All @@ -51,7 +50,6 @@ window.addEventListener("message", async (event) => {
// we do not run the test until a signal is sent from outside the iframe to
// inside of the iframe.
if (insideIframe) {
console.log(`event - ${event.data} - ${location.href}`)
const testName = event.data
// if a lookup testName in our tests object, and then execute the
// relevant test function.
Expand All @@ -65,7 +63,7 @@ window.addEventListener("message", async (event) => {

async function asyncTests(_tests) {
// for every test, we call emitAsyncTest with the test name this will start
// WPT's async_test, which won't completly // resolve until we call it again
// WPT's async_test, which won't completly resolve until we call it again
// with the same test name and test result
for (const _test in _tests) {
if (!(_test in tests)) {
Expand All @@ -79,10 +77,11 @@ async function asyncTests(_tests) {
}
if (!insideIframe) {
// ensure that the iframe where we will actually execute the test is loaded
const iframe = await requestIframeTest()
// send a test name to the iframe via postMessage, which will trigger
// that test to be run

for (const _test in _tests) {
const iframe = await requestIframeTest(_test)
iframe.contentWindow.postMessage(_test, '*')
}
}
Expand Down

0 comments on commit a216f40

Please sign in to comment.