Update editor, themes, menus, backgrounds, and release tooling
This commit is contained in:
@@ -0,0 +1,92 @@
|
||||
import { nativeRequest } from './native-support.mjs';
|
||||
import { chromium } from 'playwright';
|
||||
import { spawn, execFileSync } from 'node:child_process';
|
||||
import { mkdir } from 'node:fs/promises';
|
||||
import { resolve, basename, dirname } from 'node:path';
|
||||
import net from 'node:net';
|
||||
import assert from 'node:assert/strict';
|
||||
|
||||
const host = resolve(process.env.RUNESTONE_ROOT || '..');
|
||||
const portServer = net.createServer();
|
||||
await new Promise(resolve => portServer.listen(0, '127.0.0.1', resolve));
|
||||
const port = portServer.address().port;
|
||||
await new Promise(resolve => portServer.close(resolve));
|
||||
const native = spawn(resolve(host, 'src-tauri/target/debug/runestone.exe'), [], { windowsHide: true, stdio: 'ignore',
|
||||
env: { ...process.env, WEBVIEW2_ADDITIONAL_BROWSER_ARGUMENTS: `--remote-debugging-port=${port}` } });
|
||||
let browser, page;
|
||||
try {
|
||||
for (let attempt = 0; attempt < 60; attempt++) {
|
||||
try { browser = await chromium.connectOverCDP(`http://127.0.0.1:${port}`); break; }
|
||||
catch { await new Promise(resolve => setTimeout(resolve, 500)); }
|
||||
}
|
||||
if (!browser) throw Error('The isolated host did not start.');
|
||||
page = browser.contexts()[0].pages()[0];
|
||||
await page.waitForFunction(() => !!window.__TAURI_INTERNALS__?.invoke);
|
||||
const invoke = (command, args = {}) => nativeRequest(page, command, args);
|
||||
const snapshot = await invoke('plugins_snapshot');
|
||||
assert.equal(basename(snapshot.root), 'plugins');
|
||||
assert.equal(basename(dirname(snapshot.root)), 'com.runestone.browser-test', 'The test must use its isolated profile.');
|
||||
await page.evaluate(() => window.dispatchEvent(new Event('focus')));
|
||||
await page.getByRole('button', { name: 'Open browser', exact: true }).click();
|
||||
await page.locator('[data-browser-state="ready"]').waitFor();
|
||||
const address = page.getByRole('textbox', { name: 'Address or search', exact: true });
|
||||
const fixture = '<html><body><input id="typing" style="position:absolute;left:40px;top:40px;width:300px;height:40px"><textarea id="other" style="position:absolute;left:40px;top:140px;width:300px;height:80px"></textarea><div id="rich" contenteditable="true" style="position:absolute;left:40px;top:280px;width:300px;height:80px;border:1px solid">Rich text</div></body></html>';
|
||||
await address.fill(`https://httpbin.org/base64/${Buffer.from(fixture).toString('base64')}`);
|
||||
await address.press('Enter');
|
||||
await page.waitForTimeout(2500);
|
||||
await mkdir(resolve(host, '.audit/browser-fields-native'), { recursive: true });
|
||||
await page.screenshot({ path: resolve(host, '.audit/browser-fields-native/before-input.png') });
|
||||
const surface = page.getByRole('application', { name: 'Web page', exact: true });
|
||||
const box = await surface.boundingBox();
|
||||
await page.mouse.click(box.x + 100, box.y + 60);
|
||||
const expectCaret = () => page.waitForFunction(() => {
|
||||
const image = document.querySelector('.surface img');
|
||||
if (!image?.complete || !image.naturalWidth) return false;
|
||||
const ratio = image.naturalWidth / document.querySelector('.surface').clientWidth;
|
||||
const canvas = document.createElement('canvas'); canvas.width = 12; canvas.height = 28;
|
||||
const context = canvas.getContext('2d');
|
||||
context.drawImage(image, 42 * ratio, 46 * ratio, 12 * ratio, 28 * ratio, 0, 0, 12, 28);
|
||||
const pixels = context.getImageData(0, 0, 12, 28).data;
|
||||
let dark = 0;
|
||||
for (let offset = 0; offset < pixels.length; offset += 4) if (pixels[offset] < 100 && pixels[offset + 1] < 100 && pixels[offset + 2] < 100) dark++;
|
||||
return dark >= 8;
|
||||
}, null, { timeout: 5000, polling: 50 });
|
||||
await expectCaret();
|
||||
await page.screenshot({ path: resolve(host, '.audit/browser-fields-native/visible-caret.png') });
|
||||
const clipboard = () => execFileSync('powershell.exe', ['-NoProfile', '-Command', 'Get-Clipboard -Raw'], { encoding: 'utf8', windowsHide: true }).trim();
|
||||
for (const [name, y, value] of [['input', 60, 'RuneStone input'], ['textarea', 170, 'RuneStone textarea'], ['contenteditable', 310, 'RuneStone rich text']]) {
|
||||
await page.mouse.click(box.x + 100, box.y + y);
|
||||
const focused = await page.evaluate(() => document.activeElement?.getAttribute('aria-label'));
|
||||
assert.equal(focused, 'Web page text input', 'The native host keeps its input bridge focused.');
|
||||
await page.keyboard.press('Control+a');
|
||||
await page.keyboard.type(value);
|
||||
await page.waitForTimeout(800);
|
||||
await page.keyboard.press('Control+a');
|
||||
await page.keyboard.press('Control+c');
|
||||
await page.waitForTimeout(300);
|
||||
await page.screenshot({ path: resolve(host, `.audit/browser-fields-native/${name}.png`) });
|
||||
assert.equal(clipboard(), value, `The native ${name} accepts text and selection.`);
|
||||
console.log(`PASS: native ${name} accepts focus, typing and selection`);
|
||||
}
|
||||
for (const mode of ['dark', 'light']) {
|
||||
await page.evaluate(mode => {
|
||||
localStorage.setItem('runestone.appearance', JSON.stringify({ theme: 'default', mode }));
|
||||
window.dispatchEvent(new StorageEvent('storage', { key: 'runestone.appearance' }));
|
||||
}, mode);
|
||||
await page.waitForFunction(mode => document.documentElement.dataset.theme === mode, mode);
|
||||
await page.setViewportSize({ width: 880, height: 650 });
|
||||
await page.waitForTimeout(500);
|
||||
const narrowBox = await surface.boundingBox();
|
||||
await page.mouse.click(narrowBox.x + 60, narrowBox.y + 60);
|
||||
await page.keyboard.press('Control+a');
|
||||
await page.keyboard.press('Backspace');
|
||||
await expectCaret();
|
||||
await page.screenshot();
|
||||
await page.waitForTimeout(100);
|
||||
await page.screenshot({ path: resolve(host, `.audit/browser-fields-native/caret-${mode}-880.png`) });
|
||||
}
|
||||
console.log('PASS: native caret pixels in dark and light appearances at 880px');
|
||||
} finally {
|
||||
if (page) await nativeRequest(page, 'browser_stop').catch(() => {});
|
||||
await browser?.close(); native.kill();
|
||||
}
|
||||
@@ -18,6 +18,27 @@ export async function createEngine({ directory, emit = () => {}, canEmitFrame =
|
||||
const runtimeId = randomUUID();
|
||||
let revision = 0;
|
||||
const tab = id => { const found = tabs.get(id); if (!found) throw new Error('Unknown browser tab.'); return found; };
|
||||
const pageFocusStates = new WeakMap();
|
||||
async function focusPage(wc, enabled) {
|
||||
// Hidden offscreen windows cannot gain native focus. Chromium still needs it to paint the caret.
|
||||
if (!wc.debugger.isAttached()) {
|
||||
if (!enabled) return;
|
||||
wc.debugger.attach('1.3');
|
||||
pageFocusStates.delete(wc);
|
||||
}
|
||||
if (pageFocusStates.get(wc) === enabled) return;
|
||||
await wc.debugger.sendCommand('Emulation.setFocusEmulationEnabled', { enabled });
|
||||
pageFocusStates.set(wc, enabled);
|
||||
if (enabled) wc.focus();
|
||||
}
|
||||
function emulateViewport(item) {
|
||||
if (!item.viewport) return;
|
||||
const { width, height } = item.viewport, scale = item.scale;
|
||||
item.view.setContentSize(Math.round(width * scale), Math.round(height * scale));
|
||||
item.view.webContents.enableDeviceEmulation({ screenPosition: 'desktop', screenSize: { width, height },
|
||||
deviceScaleFactor: scale, viewPosition: { x: 0, y: 0 }, viewSize: { width, height }, scale });
|
||||
item.view.webContents.invalidate();
|
||||
}
|
||||
const agent = new AgentController(tab);
|
||||
const snapshot = () => ({
|
||||
runtimeId, revision: ++revision,
|
||||
@@ -134,6 +155,7 @@ export async function createEngine({ directory, emit = () => {}, canEmitFrame =
|
||||
});
|
||||
wc.on('destroyed', () => { item.iconRequest?.abort(); clearTimeout(item.settle); clearTimeout(item.retryFrame); });
|
||||
wc.on('did-stop-loading', () => { changed(); void cosmetics(item); });
|
||||
wc.on('did-finish-load', () => emulateViewport(item));
|
||||
wc.on('did-fail-load', (_event, code, _description, _url, main) => { if (main && code !== -3) emit({ event: 'error', tabId: item.id, message: `Page load failed (${code}).` }); });
|
||||
wc.on('paint', (_event, _dirty, image) => {
|
||||
item.latestImage = image;
|
||||
@@ -196,15 +218,20 @@ export async function createEngine({ directory, emit = () => {}, canEmitFrame =
|
||||
const scale = Number(args.scale ?? 1);
|
||||
if (!Number.isFinite(scale) || scale < 1 || scale > 3) throw new Error('Invalid browser scale.');
|
||||
current.scale = scale;
|
||||
current.view.setContentSize(Math.round(args.width * scale), Math.round(args.height * scale));
|
||||
current.view.webContents.enableDeviceEmulation({ screenPosition: 'desktop', screenSize: { width: args.width, height: args.height },
|
||||
deviceScaleFactor: scale, viewPosition: { x: 0, y: 0 }, viewSize: { width: args.width, height: args.height }, scale });
|
||||
current.viewport = { width: args.width, height: args.height };
|
||||
emulateViewport(current);
|
||||
current.delivery.clear(); current.view.webContents.invalidate(); return null;
|
||||
}
|
||||
case 'visible': { const item = tab(args.tabId), wc = item.view.webContents; item.delivery.clear(); if (args.visible === true) { wc.startPainting(); wc.invalidate(); } else { wc.stopPainting(); clearTimeout(item.settle); clearTimeout(item.retryFrame); } return null; }
|
||||
case 'visible': { const item = tab(args.tabId), wc = item.view.webContents; item.delivery.clear(); if (args.visible === true) { wc.startPainting(); wc.invalidate(); } else { await focusPage(wc, false); wc.stopPainting(); clearTimeout(item.settle); clearTimeout(item.retryFrame); } return null; }
|
||||
case 'focus': {
|
||||
if (typeof args.focused !== 'boolean') throw new Error('Invalid browser focus.');
|
||||
await focusPage(tab(args.tabId).view.webContents, args.focused); return null;
|
||||
}
|
||||
case 'insertText': {
|
||||
if (typeof args.text !== 'string' || args.text.length > 100000) throw new Error('Invalid browser text.');
|
||||
await tab(args.tabId).view.webContents.insertText(args.text); return null;
|
||||
const wc = tab(args.tabId).view.webContents;
|
||||
await focusPage(wc, true);
|
||||
await wc.insertText(args.text); return null;
|
||||
}
|
||||
case 'edit': {
|
||||
const wc = tab(args.tabId).view.webContents;
|
||||
@@ -244,7 +271,7 @@ export async function createEngine({ directory, emit = () => {}, canEmitFrame =
|
||||
event = { type: input.type, keyCode: input.keyCode, modifiers };
|
||||
}
|
||||
// Pointer motion and wheel events must not repeatedly activate the native window.
|
||||
if (input.type === 'mouseDown' || input.type === 'keyDown' || input.type === 'char') wc.focus();
|
||||
if (input.type === 'mouseDown' || input.type === 'keyDown' || input.type === 'char') await focusPage(wc, true);
|
||||
wc.sendInputEvent(event); return null;
|
||||
}
|
||||
case 'createProfile': store.createProfile(args.name); break;
|
||||
|
||||
@@ -90,6 +90,20 @@ app.whenReady().then(async () => {
|
||||
await engine.command('input', { tabId: first, input: { type: 'mouseUp', x: 50, y: 35, button: 'left' } });
|
||||
await engine.command('input', { tabId: first, input: { type: 'char', keyCode: 'x' } });
|
||||
await until(async () => await wc.executeJavaScript("document.querySelector('input').value") === 'x');
|
||||
assert.equal(await wc.executeJavaScript('document.hasFocus()'), true);
|
||||
await engine.command('focus', { tabId: first, focused: false });
|
||||
assert.equal(await wc.executeJavaScript('document.hasFocus()'), false);
|
||||
await engine.command('focus', { tabId: first, focused: true });
|
||||
assert.equal(await wc.executeJavaScript('document.hasFocus()'), true);
|
||||
wc.debugger.detach();
|
||||
await engine.command('focus', { tabId: first, focused: true });
|
||||
assert.equal(await wc.executeJavaScript('document.hasFocus()'), true);
|
||||
await engine.command('visible', { tabId: first, visible: false });
|
||||
assert.equal(await wc.executeJavaScript('document.hasFocus()'), false);
|
||||
await engine.command('visible', { tabId: first, visible: true });
|
||||
await engine.command('focus', { tabId: first, focused: true });
|
||||
await assert.rejects(engine.command('focus', { tabId: first, focused: 'true' }), /Invalid browser focus/);
|
||||
passed.push('offscreen focus supports the caret, blur, hidden tabs, and debugger reattachment');
|
||||
passed.push('offscreen frames and forwarded pointer and keyboard input work');
|
||||
await engine.command('insertText', { tabId: first, text: '日本語 🙂' });
|
||||
await until(async () => (await wc.executeJavaScript("document.querySelector('input').value")).endsWith('日本語 🙂'));
|
||||
|
||||
Reference in New Issue
Block a user