RuneStone 1.0.0
This commit is contained in:
@@ -0,0 +1,162 @@
|
||||
// Embedded browser fields with the real Electron engine: visible caret, page focus, toolbar blur, inputs, rich text, iframes, and navigation.
|
||||
// @requires electron
|
||||
import { createRequire } from 'node:module';
|
||||
import { spawn } from 'node:child_process';
|
||||
import { createServer } from 'node:http';
|
||||
import { mkdtemp } from 'node:fs/promises';
|
||||
import path from 'node:path';
|
||||
import os from 'node:os';
|
||||
import assert from 'node:assert/strict';
|
||||
import { mockTauri, openPage, outputDir, repoRoot, runSmoke, smokeUrl } from '../_harness/index.mjs';
|
||||
|
||||
const runtime = path.join(repoRoot, 'browser-runtime');
|
||||
const electron = createRequire(path.join(runtime, 'package.json'))('electron');
|
||||
const scale = Number(process.env.BROWSER_TEST_SCALE || 1);
|
||||
|
||||
/** Start the Electron browser engine and relay its frames and state events to `page` once it is set. */
|
||||
async function startEngine() {
|
||||
const root = await mkdtemp(path.join(os.tmpdir(), 'runestone-browser-ui-'));
|
||||
const child = spawn(electron, [path.join(runtime, 'test/interaction-engine.mjs')], {
|
||||
windowsHide: true, stdio: ['ignore', 'inherit', 'inherit', 'ipc'], env: { ...process.env, RS_INTERACTION_ROOT: root },
|
||||
});
|
||||
const engine = { page: undefined, commands: [], child };
|
||||
let sequence = 0, pendingFrame, painting = false;
|
||||
const pending = new Map();
|
||||
engine.rpc = (command, args = {}) => new Promise((resolve, reject) => {
|
||||
const id = ++sequence;
|
||||
const timeout = setTimeout(() => { pending.delete(id); reject(Error(`Runtime timeout: ${command}`)); }, 15000);
|
||||
pending.set(id, { resolve: value => { clearTimeout(timeout); resolve(value); }, reject: error => { clearTimeout(timeout); reject(error); } });
|
||||
child.send({ id, command, args });
|
||||
});
|
||||
async function paint() {
|
||||
if (painting || !engine.page || !pendingFrame) return;
|
||||
painting = true;
|
||||
const frame = pendingFrame; pendingFrame = undefined;
|
||||
await engine.page.evaluate(value => window.__TEST__?.emit('browser-frame', value), frame).catch(() => {});
|
||||
painting = false;
|
||||
if (pendingFrame) void paint();
|
||||
}
|
||||
await new Promise((resolve, reject) => {
|
||||
child.on('error', reject);
|
||||
child.on('message', message => {
|
||||
if (message.ready) resolve();
|
||||
if (message.id) {
|
||||
const callback = pending.get(message.id); pending.delete(message.id);
|
||||
if (message.error) callback?.reject(Error(message.error)); else callback?.resolve(message.result);
|
||||
}
|
||||
if (message.event?.event === 'frame') { pendingFrame = message.event; void paint(); }
|
||||
else if (message.event && engine.page) void engine.page.evaluate(value => window.__TEST__?.emit('browser-state', value), message.event).catch(() => {});
|
||||
});
|
||||
});
|
||||
engine.stop = async () => { await engine.rpc('shutdown').catch(() => {}); child.kill(); };
|
||||
return engine;
|
||||
}
|
||||
|
||||
/** Serve an empty page. A tab on about:blank shows the host background instead of frames (docs/ui-design.md:218). */
|
||||
async function startFixture() {
|
||||
const server = createServer((_request, response) => { response.writeHead(200, { 'content-type': 'text/html' }); response.end('<!doctype html><title>Fixture</title><body></body>'); });
|
||||
await new Promise(resolve => server.listen(0, '127.0.0.1', resolve));
|
||||
return { url: `http://127.0.0.1:${server.address().port}/`, close: () => new Promise(resolve => server.close(resolve)) };
|
||||
}
|
||||
|
||||
await runSmoke('browser/fields', async ({ browser }) => {
|
||||
const engine = await startEngine();
|
||||
const fixture = await startFixture();
|
||||
const { rpc, commands } = engine;
|
||||
try {
|
||||
const page = engine.page = await openPage(browser, { viewport: { width: 1280, height: 800 }, deviceScaleFactor: scale, timeout: 30000 });
|
||||
await page.exposeFunction('browserRpc', async (command, args) => { const entry = { command, args, start: Date.now() }; commands.push(entry); const result = await rpc(command, args); entry.elapsed = Date.now() - entry.start; return result; });
|
||||
await mockTauri(page, {
|
||||
commands: () => ({
|
||||
browser_connect: () => 'test-session',
|
||||
browser_request: ({ command, args }) => window.browserRpc(command, args),
|
||||
workspace_last_path: () => null,
|
||||
plugins_snapshot: () => ({ root: 'test', plugins: [] }),
|
||||
'plugin:window|is_maximized': () => false,
|
||||
}),
|
||||
});
|
||||
const output = await outputDir('browser/fields');
|
||||
await page.goto(smokeUrl);
|
||||
await page.getByRole('button', { name: 'Open browser', exact: true }).click();
|
||||
const surface = page.getByRole('application', { name: 'Web page', exact: true });
|
||||
await page.locator('[data-browser-state="ready"]').waitFor();
|
||||
const tabId = await page.locator('[data-browser-state="ready"]').getAttribute('data-browser-tab');
|
||||
const evaluate = code => rpc('testEvaluate', { tabId, code });
|
||||
// Load an empty page first: the host shows frames only for loaded pages (src/lib/browser/Browser.svelte:18,264).
|
||||
await rpc('navigate', { tabId, input: fixture.url });
|
||||
await page.locator('.browser:not(.blank)[data-browser-state="ready"]').waitFor();
|
||||
async function expectRemote(code, expected) {
|
||||
for (let attempt = 0; attempt < 100; attempt++) {
|
||||
if (await evaluate(code) === expected) return;
|
||||
await page.waitForTimeout(50);
|
||||
}
|
||||
assert.equal(await evaluate(code), expected);
|
||||
}
|
||||
await evaluate(`document.body.innerHTML='<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>'; true;`);
|
||||
await page.locator('.surface img').waitFor();
|
||||
await page.waitForTimeout(300);
|
||||
const box = await surface.boundingBox();
|
||||
await page.mouse.click(box.x + 100, box.y + 60);
|
||||
await page.waitForTimeout(150);
|
||||
assert.deepEqual(await evaluate('({active:document.activeElement.id,focused:document.hasFocus()})'), { active: 'typing', focused: true });
|
||||
await page.waitForFunction(() => {
|
||||
const image = document.querySelector('.surface img');
|
||||
if (!image?.complete || !image.naturalWidth) return false;
|
||||
const surface = document.querySelector('.surface');
|
||||
const ratio = image.naturalWidth / 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 page.screenshot({ path: path.join(output, `caret-${scale}x.png`) });
|
||||
await page.getByRole('textbox', { name: 'Address or search' }).focus();
|
||||
await page.waitForTimeout(100);
|
||||
assert.equal(await evaluate('document.hasFocus()'), false, 'The toolbar removes focus from the page');
|
||||
await page.mouse.click(box.x + 100, box.y + 60);
|
||||
await page.waitForTimeout(100);
|
||||
assert.equal(await evaluate('document.hasFocus()'), true, 'A page click restores focus');
|
||||
await page.keyboard.type('Hello World!');
|
||||
await expectRemote('document.querySelector("#typing").value', 'Hello World!');
|
||||
await page.mouse.move(box.x + 50, box.y + 60);
|
||||
await page.mouse.down();
|
||||
await page.mouse.move(box.x + 100, box.y + 60, { steps: 8 });
|
||||
await page.mouse.up();
|
||||
await page.waitForTimeout(200);
|
||||
assert(await evaluate('document.querySelector("#typing").selectionEnd > document.querySelector("#typing").selectionStart'), 'Dragging selects text in the embedded field');
|
||||
await page.mouse.click(box.x + 100, box.y + 170);
|
||||
await page.keyboard.type('Second field');
|
||||
await expectRemote('document.querySelector("#other").value', 'Second field');
|
||||
await page.mouse.click(box.x + 100, box.y + 310);
|
||||
await page.keyboard.press('Control+a');
|
||||
await page.keyboard.type('Edited rich text');
|
||||
await expectRemote('document.querySelector("#rich").textContent', 'Edited rich text');
|
||||
await evaluate(`document.body.insertAdjacentHTML('beforeend', '<iframe id="frame" style="position:absolute;left:40px;top:390px;width:300px;height:100px" srcdoc="<input id=framed style=width:200px;height:40px>"></iframe>'); true;`);
|
||||
await page.waitForTimeout(200);
|
||||
await page.mouse.click(box.x + 100, box.y + 415);
|
||||
await page.keyboard.type('Frame input');
|
||||
await expectRemote('document.querySelector("#frame").contentDocument.querySelector("#framed").value', 'Frame input');
|
||||
assert.equal(await evaluate('document.querySelector("#frame").contentDocument.hasFocus()'), true);
|
||||
await page.getByRole('textbox', { name: 'Address or search' }).focus();
|
||||
await page.waitForTimeout(100);
|
||||
assert.equal(await evaluate('document.querySelector("#frame").contentDocument.hasFocus()'), false);
|
||||
await rpc('navigate', { tabId, input: 'about:blank' });
|
||||
await page.waitForTimeout(100);
|
||||
await evaluate(`document.body.innerHTML='<input id="after-navigation" style="position:absolute;left:40px;top:40px;width:300px;height:40px">'; true;`);
|
||||
await page.waitForTimeout(400);
|
||||
await page.mouse.click(box.x + 100, box.y + 60);
|
||||
await page.keyboard.type('After navigation');
|
||||
await page.waitForTimeout(500);
|
||||
assert.equal(await evaluate('devicePixelRatio'), scale, 'Navigation preserves the host pixel ratio');
|
||||
await expectRemote('document.querySelector("#after-navigation").value', 'After navigation');
|
||||
assert.equal(await evaluate('document.hasFocus()'), true);
|
||||
assert.deepEqual(page.errors, []);
|
||||
console.log('PASS: visible caret, page focus, toolbar blur, input, textarea, contenteditable, iframe and navigation');
|
||||
} finally {
|
||||
await engine.stop();
|
||||
await fixture.close();
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user