Skip to content

Commit

Permalink
Convert shim correctness tests to puppeteer
Browse files Browse the repository at this point in the history
  • Loading branch information
muodov committed May 1, 2024
1 parent a61cb2b commit 9ef4685
Show file tree
Hide file tree
Showing 9 changed files with 450 additions and 4,645 deletions.
18 changes: 15 additions & 3 deletions integration-test/test-pages.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,15 @@ describe('Test integration pages', () => {
await teardown()
})

async function testPage (pageName, configName, evalBeforeInit = null) {
/**
* @param {string} pageName
* @param {string} configPath
* @param {string} [evalBeforeInit]
*/
async function testPage (pageName, configPath, evalBeforeInit) {
const port = server.address().port
const page = await browser.newPage()
const res = fs.readFileSync(process.cwd() + '/integration-test/test-pages/' + configName)
const res = fs.readFileSync(configPath)
// @ts-expect-error - JSON.parse returns any
const config = JSON.parse(res)
polyfillProcessGlobals()
Expand Down Expand Up @@ -78,8 +83,15 @@ describe('Test integration pages', () => {
for (const pageName in pages) {
const configName = pages[pageName]
it(`${pageName}`, async () => {
await testPage(pageName, configName)
await testPage(pageName, process.cwd() + '/integration-test/test-pages/' + configName)
})
}
})

it('Web compat shims correctness', async () => {
await testPage(
'webcompat/pages/shims.html',
`${process.cwd()}/integration-test/test-pages/webcompat/config/shims.json`
)
})
})
11 changes: 11 additions & 0 deletions integration-test/test-pages/webcompat/config/shims.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"features": {
"webCompat": {
"state": "enabled",
"settings": {
"mediaSession": "enabled",
"presentation": "enabled"
}
}
}
}
129 changes: 129 additions & 0 deletions integration-test/test-pages/webcompat/pages/shims.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Webcompat shims</title>
<link rel="stylesheet" href="../../shared/style.css">
</head>
<body>
<script src="../../shared/utils.js"></script>
<p><a href="../index.html">[Webcompat shims]</a></p>

<p>This page verifies that shims created with shim APIs have correct descriptors</p>

<script>
/**
* assert that two descriptors are similar, allowing different values
* @param {import('../src/wrapper-utils').StrictPropertyDescriptor} origDesc
* @param {import('../src/wrapper-utils').StrictPropertyDescriptor} newDesc
* @returns {string} - the error message if the test fails
*/
function compareDescriptorShape (origDesc, newDesc) {
const origKeys = Object.keys(origDesc)
const newKeys = Object.keys(newDesc)
// verify that both descriptors have the same keys
if (origKeys.sort().join(',') !== newKeys.sort().join(','))
return 'property keys do not match';

for (const key of origKeys) {
if (key === 'get' || key === 'set' || key === 'value') {
if (typeof newDesc[key] !== typeof origDesc[key])
return `property ${key} does not match`;
} else {
if (newDesc[key] !== origDesc[key])
return `property ${key} does not match`;
}
}
}

/**
* Use this function to test the interfaces shimmed in the web-compat feature
* @param {string} interfaceName - the name of the interface to test. It should be available in the global scope
* @param {import('../src/wrapper-utils').StrictPropertyDescriptor} origInterfaceDescriptor - the descriptor of the original interface
* @returns {string} - the error message if the test fails
*/
function testInterfaceShimCorrectness (interfaceName, origInterfaceDescriptor) {
if (!interfaceName) {
return 'Nothing to test.';
}

if (!globalThis[interfaceName])
return 'native class is not found after shimming';
if (globalThis[interfaceName][globalThis.ddgShimMark] !== true)
return 'class should be marked as shimmed';

const newInterfaceDescriptor = Object.getOwnPropertyDescriptor(globalThis, interfaceName)

return compareDescriptorShape(origInterfaceDescriptor, newInterfaceDescriptor)
}

/**
* Use this function to test the global properties shimmed in the web-compat feature
* @param {any} instanceHost - object under which the global instance is defined
* @param {string} instanceProp - the name of the instance property
* @param {import('../src/wrapper-utils').StrictPropertyDescriptor} origInstanceDescriptor - the descriptor of the original instance property
* @returns {string} - the error message if the test fails
*/
function testInstanceShimCorrectness (instanceHost, instanceProp, origInstanceDescriptor) {
if (!instanceHost || !instanceProp) {
return 'Nothing to test.';
}

if (!instanceHost[instanceProp])
return 'global instance is not found after shimming';

const newInstanceDescriptor = Object.getOwnPropertyDescriptor(instanceHost, instanceProp)

return compareDescriptorShape(origInstanceDescriptor, newInstanceDescriptor)
}


test('Interface shims', async () => {
const results = [];
results.push({
name: 'origInterfaceDescriptors found',
result: Boolean(globalThis.origInterfaceDescriptors),
expected: true
});
results.push({
name: 'ddgShimMark found',
result: Boolean(globalThis.ddgShimMark),
expected: true
});

for (const [interfaceName, origDescriptor] of Object.entries(globalThis.origInterfaceDescriptors)) {
results.push({
name: `${interfaceName}'s descriptor is correct`,
result: testInterfaceShimCorrectness(interfaceName, origDescriptor),
expected: undefined
});
}

return results;
});

test('Instance shims', async () => {
const results = [];
results.push({
name: 'origPropDescriptors found',
result: Boolean(globalThis.origPropDescriptors),
expected: true
});

for (const [instanceHost, instanceProp, origDescriptor] of globalThis.origPropDescriptors) {
results.push({
name: `${instanceHost}.${instanceProp}'s descriptor is correct`,
result: testInstanceShimCorrectness(instanceHost, instanceProp, origDescriptor),
expected: undefined
});
}

return results;
});

// eslint-disable-next-line no-undef
renderResults();
</script>
</body>
</html>

0 comments on commit 9ef4685

Please sign in to comment.