|
| 1 | +import { test, expect } from '@playwright/test' |
| 2 | +import { readFileSync } from 'fs' |
| 3 | +import { mockWindowsMessaging, wrapWindowsScripts } from '@duckduckgo/messaging/lib/test-utils.mjs' |
| 4 | + |
| 5 | +test('Windows Permissions Usage', async ({ page }) => { |
| 6 | + const perms = new WindowsPermissionsSpec(page) |
| 7 | + await perms.enabled() |
| 8 | + const results = await page.evaluate(() => { |
| 9 | + // @ts-expect-error - this is added by the test framework |
| 10 | + return window.results['Disabled Windows Permissions'] |
| 11 | + }) |
| 12 | + |
| 13 | + for (const result of results) { |
| 14 | + expect(result.result).toEqual(result.expected) |
| 15 | + } |
| 16 | +}) |
| 17 | + |
| 18 | +export class WindowsPermissionsSpec { |
| 19 | + htmlPage = '/permissions/index.html' |
| 20 | + config = './integration-test/test-pages/permissions/config/permissions.json' |
| 21 | + build = './build/windows/contentScope.js' |
| 22 | + /** |
| 23 | + * @param {import("@playwright/test").Page} page |
| 24 | + */ |
| 25 | + constructor (page) { |
| 26 | + this.page = page |
| 27 | + } |
| 28 | + |
| 29 | + async enabled () { |
| 30 | + await this.installPolyfills() |
| 31 | + const config = JSON.parse(readFileSync(this.config, 'utf8')) |
| 32 | + await this.setup({ config }) |
| 33 | + await this.page.goto(this.htmlPage) |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * In CI, the global objects such as USB might not be installed on the |
| 38 | + * version of chromium running there. |
| 39 | + */ |
| 40 | + async installPolyfills () { |
| 41 | + await this.page.addInitScript(() => { |
| 42 | + // @ts-expect-error - testing |
| 43 | + if (typeof Bluetooth === 'undefined') { |
| 44 | + globalThis.Bluetooth = {} |
| 45 | + globalThis.Bluetooth.prototype = { requestDevice: async () => { /* noop */ } } |
| 46 | + } |
| 47 | + // @ts-expect-error - testing |
| 48 | + if (typeof USB === 'undefined') { |
| 49 | + globalThis.USB = {} |
| 50 | + globalThis.USB.prototype = { requestDevice: async () => { /* noop */ } } |
| 51 | + } |
| 52 | + |
| 53 | + // @ts-expect-error - testing |
| 54 | + if (typeof Serial === 'undefined') { |
| 55 | + globalThis.Serial = {} |
| 56 | + globalThis.Serial.prototype = { requestPort: async () => { /* noop */ } } |
| 57 | + } |
| 58 | + // @ts-expect-error - testing |
| 59 | + if (typeof HID === 'undefined') { |
| 60 | + globalThis.HID = {} |
| 61 | + globalThis.HID.prototype = { requestDevice: async () => { /* noop */ } } |
| 62 | + } |
| 63 | + }) |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * @param {object} params |
| 68 | + * @param {Record<string, any>} params.config |
| 69 | + * @return {Promise<void>} |
| 70 | + */ |
| 71 | + async setup (params) { |
| 72 | + const { config } = params |
| 73 | + |
| 74 | + // read the built file from disk and do replacements |
| 75 | + const injectedJS = wrapWindowsScripts(this.buildArtefact, { |
| 76 | + $CONTENT_SCOPE$: config, |
| 77 | + $USER_UNPROTECTED_DOMAINS$: [], |
| 78 | + $USER_PREFERENCES$: { |
| 79 | + platform: { name: 'windows' }, |
| 80 | + debug: true |
| 81 | + } |
| 82 | + }) |
| 83 | + |
| 84 | + await this.page.addInitScript(mockWindowsMessaging, { |
| 85 | + messagingContext: { |
| 86 | + env: 'development', |
| 87 | + context: 'contentScopeScripts', |
| 88 | + featureName: 'n/a' |
| 89 | + }, |
| 90 | + responses: {} |
| 91 | + }) |
| 92 | + |
| 93 | + // attach the JS |
| 94 | + await this.page.addInitScript(injectedJS) |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * @return {string} |
| 99 | + */ |
| 100 | + get buildArtefact () { |
| 101 | + const buildArtefact = readFileSync(this.build, 'utf8') |
| 102 | + return buildArtefact |
| 103 | + } |
| 104 | +} |
0 commit comments