Skip to content

Commit

Permalink
android support for integration tests (#1002)
Browse files Browse the repository at this point in the history
Co-authored-by: Shane Osbourne <sosbourne@duckduckgo.com>
  • Loading branch information
shakyShane and Shane Osbourne authored Jul 31, 2024
1 parent df3219c commit e4a01f6
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 6 deletions.
4 changes: 2 additions & 2 deletions integration-test/playwright/type-helpers.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export class Build {
*/
static supported (name) {
/** @type {ImportMeta['injectName'][]} */
const items = ['apple', 'apple-isolated', 'windows', 'integration']
const items = ['apple', 'apple-isolated', 'windows', 'integration', 'android']
if (items.includes(name)) {
return name
}
Expand All @@ -93,7 +93,7 @@ export class PlatformInfo {
*/
static supported (name) {
/** @type {ImportMeta['platform'][]} */
const items = ['macos', 'windows']
const items = ['macos', 'ios', 'windows', 'android']
if (items.includes(name)) {
return name
}
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@
"serve": "http-server -c-1 --port 3220 integration-test/test-pages",
"playwright": "playwright test",
"playwright-headed": "playwright test --headed",
"preplaywright": "npm run build-windows && npm run build-apple",
"preplaywright-headed": "npm run build-windows && npm run build-apple",
"preplaywright": "npm run build-windows && npm run build-apple && npm run build-android",
"preplaywright-headed": "npm run build-windows && npm run build-apple && npm run build-android",
"playwright-e2e": "playwright test -c playwright-e2e.config.js --project duckplayer-e2e",
"playwright-e2e-headed": "npm run playwright-e2e -- --headed",
"preplaywright-e2e": "npm run build-windows && npm run build-apple"
Expand Down
56 changes: 56 additions & 0 deletions packages/messaging/lib/test-utils.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,62 @@ export function mockWebkitMessaging(params) {
}
}

/**
* Install a mock interface for android messaging
* @param {{
* messagingContext: import('../index.js').MessagingContext,
* responses: Record<string, any>,
* messageCallback: string
* }} params
*/
export function mockAndroidMessaging(params) {
window.__playwright_01 = {
mockResponses: params.responses,
subscriptionEvents: [],
mocks: {
outgoing: []
}
}
window[params.messagingContext.context] = {
/**
* @param {string} jsonString
* @param {string} secret
* @return {Promise<void>}
*/
process: async (jsonString, secret) => {

/** @type {RequestMessage | NotificationMessage} */
const msg = JSON.parse(jsonString);

window.__playwright_01.mocks.outgoing.push(JSON.parse(JSON.stringify({
payload: msg
})))

// if it's a notification, simulate the empty response and don't check for a response
if (!('id' in msg)) {
return console.warn("no id");
}

if (!(msg.method in window.__playwright_01.mockResponses)) {
throw new Error('response not found for ' + msg.method)
}

const response = window.__playwright_01.mockResponses[msg.method]

/** @type {Omit<MessageResponse, 'error'>} */
const r = {
result: response,
context: msg.context,
featureName: msg.featureName,
// @ts-ignore - shane: fix this
id: msg.id,
}

globalThis['messageCallback']?.(secret, r);
}
}
}

/**
* @param {object} params
* @param {Record<string, any>} params.responses
Expand Down
10 changes: 10 additions & 0 deletions packages/special-pages/shared/create-special-page-messaging.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
AndroidMessagingConfig,
Messaging,
MessagingContext,
TestTransportConfig,
Expand Down Expand Up @@ -39,6 +40,15 @@ export function createSpecialPageMessaging (opts) {
webkitMessageHandlerNames: ['specialPages']
})
return new Messaging(messageContext, opts)
} else if (opts.injectName === 'android') {
const opts = new AndroidMessagingConfig({
messageSecret: 'duckduckgo-android-messaging-secret',
messageCallback: 'messageCallback',
javascriptInterface: messageContext.context,
target: globalThis,
debug: true
})
return new Messaging(messageContext, opts)
}
} catch (e) {
console.error('could not access handlers for %s, falling back to mock interface', opts.injectName)
Expand Down
2 changes: 1 addition & 1 deletion packages/special-pages/shared/environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,6 @@ export class Environment {
*/
function isPlatform (input) {
/** @type {ImportMeta['injectName'][]} */
const allowed = ['windows', 'apple', 'integration']
const allowed = ['windows', 'apple', 'integration', 'android']
return allowed.includes(input)
}
2 changes: 1 addition & 1 deletion src/globals.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ declare namespace contentScopeFeatures {
*/
interface ImportMeta {
env: 'production' | 'development'
platform?: 'windows' | 'macos'
platform?: 'windows' | 'macos' | 'android' | 'ios'
// this represents the different build artifact names
injectName?: 'firefox' | 'apple' | 'apple-isolated' | 'android' | 'windows' | 'integration' | 'chrome-mv3' | 'chrome'
trackerLookup?: Record<string, unknown>
Expand Down

0 comments on commit e4a01f6

Please sign in to comment.