78 lines
5.6 KiB
JavaScript
78 lines
5.6 KiB
JavaScript
// Native editing menus in inputs, textareas, and dialogs: unsupported WebView2 commands stay hidden, Undo and spelling work, and focus returns, in both themes.
|
|
// Start a debug app with the isolated identifier com.runestone.spelling-test and remote debugging on port 9237:
|
|
// set WEBVIEW2_ADDITIONAL_BROWSER_ARGUMENTS=--remote-debugging-port=9237
|
|
// pnpm tauri dev --config "{\"identifier\":\"com.runestone.spelling-test\"}"
|
|
// Run `node tests/native/menuScope.native.mjs`. NATIVE_CDP_URL (or the older SPELLING_CDP_URL) overrides http://127.0.0.1:9237.
|
|
import { writeFile } from 'node:fs/promises';
|
|
import path from 'node:path';
|
|
import assert from 'node:assert/strict';
|
|
import { outputDir, playwright } from '../smoke/_harness/index.mjs';
|
|
const { chromium } = playwright();
|
|
const browser = await chromium.connectOverCDP(process.env.NATIVE_CDP_URL || process.env.SPELLING_CDP_URL || 'http://127.0.0.1:9237');
|
|
const page = browser.contexts()[0].pages()[0];
|
|
page.setDefaultTimeout(15000);
|
|
const captures = [];
|
|
try {
|
|
await page.waitForFunction(() => !!window.__TAURI_INTERNALS__);
|
|
await page.getByRole('button', { name: 'Settings (Ctrl+,)', exact: true }).waitFor();
|
|
const identity = await page.evaluate(async () => (await import('/node_modules/@tauri-apps/api/path.js')).appDataDir());
|
|
assert.match(identity, /[\\/]com\.runestone\.spelling-test[\\/]?$/);
|
|
await page.evaluate(async () => {
|
|
const { listen } = await import('/node_modules/@tauri-apps/api/event.js');
|
|
window.nativeMenuPayload = null;
|
|
await listen('spelling-menu', ({ payload }) => window.nativeMenuPayload = payload);
|
|
await (await import('/node_modules/@tauri-apps/api/core.js')).invoke('spelling_menu_install');
|
|
});
|
|
const output = await outputDir('native/menuScope');
|
|
const capturesFile = path.join(output, 'native-filtered-items.json');
|
|
const menu = page.getByRole('menu', { name: 'Spelling actions', exact: true });
|
|
for (const mode of ['dark', 'light']) {
|
|
await page.evaluate(async mode => { const { saveAppearance } = await import('/src/lib/theme/appearance.ts'); saveAppearance({ theme: 'default', mode }); }, mode);
|
|
for (const kind of ['input', 'textarea', 'dialog']) {
|
|
await page.evaluate(kind => {
|
|
document.getElementById('native-menu-field')?.remove(); document.getElementById('native-menu-dialog')?.remove();
|
|
const field = document.createElement(kind === 'input' ? 'input' : 'textarea');
|
|
field.id = 'native-menu-field'; field.spellcheck = true; field.value = 'I recieved a letter.';
|
|
field.style.cssText = 'position:fixed;left:100px;top:100px;width:500px;height:100px;font:16px/24px Arial;padding:10px;z-index:100';
|
|
if (kind === 'dialog') { const dialog = document.createElement('dialog'); dialog.id = 'native-menu-dialog'; dialog.append(field); document.body.append(dialog); dialog.showModal(); }
|
|
else document.body.append(field);
|
|
field.focus();
|
|
}, kind);
|
|
const field = page.locator('#native-menu-field');
|
|
await field.press('End');
|
|
await field.pressSequentially(' TEST');
|
|
await field.click({ button: 'right', position: { x: 300, y: kind === 'input' ? 50 : 22 } });
|
|
await menu.waitFor();
|
|
const payload = await page.evaluate(() => window.nativeMenuPayload);
|
|
captures.push({ mode, kind, value: await field.inputValue(), payload });
|
|
await writeFile(capturesFile, JSON.stringify(captures, null, 2));
|
|
const flatten = items => items.flatMap(item => [item, ...flatten(item.children)]);
|
|
const entries = flatten(payload.items);
|
|
assert.ok(entries.every(item => item.kind === 3 || !['share', 'writingDirection', 'voiceTyping', 'other'].includes(item.name)));
|
|
assert.ok(entries.some(item => item.name === 'undo' && item.enabled), mode + ' ' + kind + ' keeps native Undo enabled');
|
|
assert.ok(entries.some(item => item.name === 'emoji'));
|
|
assert.ok(entries.some(item => item.name === 'copy'));
|
|
assert.equal(await menu.getByRole('menuitem', { name: /More tools|Writing direction|Voice typing|Share/ }).count(), 0);
|
|
assert.equal(await menu.evaluate(node => getComputedStyle(node).borderColor), 'rgba(0, 0, 0, 0)');
|
|
if (kind === 'textarea') await page.screenshot({ path: path.join(output, `native-${mode}.png`) });
|
|
await menu.getByRole('menuitem', { name: 'Undo Ctrl+Z', exact: true }).click();
|
|
await page.waitForFunction(() => document.getElementById('native-menu-field').value === 'I recieved a letter.').catch(async error => { console.error('Undo result', await field.inputValue()); throw error; });
|
|
await field.click({ button: 'right', position: { x: 48, y: kind === 'input' ? 50 : 22 } });
|
|
await menu.getByRole('menuitem', { name: 'received', exact: true }).click();
|
|
assert.equal(await field.inputValue(), 'I received a letter.');
|
|
await field.press('Control+z');
|
|
assert.equal(await field.inputValue(), 'I recieved a letter.');
|
|
await field.click({ button: 'right', position: { x: 48, y: kind === 'input' ? 50 : 22 } });
|
|
await menu.waitFor();
|
|
await page.keyboard.press('Escape');
|
|
assert.equal(await field.evaluate(node => document.activeElement === node), true);
|
|
}
|
|
}
|
|
await writeFile(capturesFile, JSON.stringify(captures, null, 2));
|
|
console.log('PASS: real WebView2 menus omit unsupported commands, retain editing and spelling, execute native Undo, correct spelling with undo, and restore focus in fields and dialogs in both themes.');
|
|
} finally {
|
|
await page.evaluate(() => { document.getElementById('native-menu-field')?.remove(); document.getElementById('native-menu-dialog')?.remove(); }).catch(() => {});
|
|
await browser.close();
|
|
}
|
|
|