'use strict'; /* * Keyboard, focus and screen-reader basics (code-8, code-9, code-27, code-28). The end dialog is in 03-endflow, the sound * parts are in 09-audio. * * dialogs menu, help, lobby: role=dialog + aria-modal + a name; focus goes in (never into a text field), Tab stays in, the page * behind is inert, focus returns to where it was; Escape and "Got it" behave * labels palette tiles say when you cannot afford them, state is in the names (pressed / selected), contrast of the dim states * sink a hostile room code from the server is shown as text, not parsed as HTML (co-op) */ const { createServer } = require('../../server'); const assert = (c, m) => { if (!c) throw new Error('ASSERT: ' + m); console.log('ok -', m); }; const sleep = (ms) => new Promise((r) => setTimeout(r, ms)); async function until(fn, ms, what) { const t0 = Date.now(); for (;;) { try { const v = await fn(); if (v) return v; } catch (e) { /* retry */ } if (Date.now() - t0 > ms) throw new Error('TIMEOUT: ' + what); await sleep(40); } } const active = (page) => page.evaluate(() => document.activeElement.id || document.activeElement.tagName); const inside = (page, sel) => page.evaluate((s) => !!document.activeElement.closest(s), sel); async function tabsStayIn(page, sel, n) { const out = []; for (let i = 0; i < n; i++) { await page.keyboard.press(i % 5 === 4 ? 'Shift+Tab' : 'Tab'); if (!(await inside(page, sel))) out.push(await active(page)); } return out; } const lum = (c) => { const f = (v) => { v /= 255; return v <= 0.03928 ? v / 12.92 : Math.pow((v + 0.055) / 1.055, 2.4); }; return 0.2126 * f(c[0]) + 0.7152 * f(c[1]) + 0.0722 * f(c[2]); }; const ratio = (a, b) => { const x = lum(a), y = lum(b); return (Math.max(x, y) + 0.05) / (Math.min(x, y) + 0.05); }; module.exports = async function ({ browser, url }) { const errors = []; const ctx = await browser.newContext({ viewport: { width: 1100, height: 700 }, serviceWorkers: 'block' }); await ctx.addInitScript(() => { try { localStorage.setItem('nw.helped', 'true'); } catch (e) { /* ignore */ } }); // (the first-run help is tested in 04-coop) const page = await ctx.newPage(); page.on('pageerror', (e) => errors.push('pageerror: ' + e.message)); page.on('console', (m) => { if (m.type() === 'error') errors.push('console.error: ' + m.text()); }); try { await page.goto(url); await sleep(350); console.log('--- dialogs'); const roles = await page.evaluate(() => ['menu', 'lobby', 'help', 'end'].map((id) => { const e = document.getElementById(id), l = document.getElementById(e.getAttribute('aria-labelledby') || '-'); return [id, e.getAttribute('role'), e.getAttribute('aria-modal'), l ? l.textContent.trim() : '']; })); for (const [id, role, modal, name] of roles) assert(role === 'dialog' && modal === 'true' && name.length > 2, id + ' is a modal dialog named "' + name + '"'); assert((await active(page)) === 'btnSolo', 'the menu opens with focus on Play solo (' + (await active(page)) + ')'); assert(await page.evaluate(() => ['topbar', 'stage', 'side'].every((id) => document.getElementById(id).hasAttribute('inert'))), 'the page behind the menu is inert'); let strays = await tabsStayIn(page, '#menu', 25); assert(strays.length === 0, 'Tab and Shift+Tab never leave the menu (25 presses): ' + JSON.stringify(strays)); await page.click('#btnHelp'); assert((await active(page)) === 'btnHelpClose', 'How to play: focus on "Got it" (' + (await active(page)) + ')'); strays = await tabsStayIn(page, '#help', 6); assert(strays.length === 0, 'Tab stays in the help dialog'); await page.keyboard.press('Enter'); await sleep(200); assert((await page.locator('#menu').isVisible()) && (await page.locator('#help').isHidden()), 'Enter on "Got it" returns to the menu'); assert((await active(page)) === 'btnSolo', 'with focus back on Play solo'); await page.click('#btnCoop'); assert((await active(page)) === 'btnCreate', 'the lobby puts focus on Create room, not into a text field (that would raise the keyboard on a phone): ' + (await active(page))); strays = await tabsStayIn(page, '#lobby', 14); assert(strays.length === 0, 'Tab stays in the lobby: ' + JSON.stringify(strays)); await page.click('#btnLobbyBack'); await sleep(150); assert((await page.locator('#menu').isVisible()), 'Back returns to the menu'); // an invite link opens the lobby with the code filled in: the focused button must join that room (Enter on Create room would open a new one) const pi = await ctx.newPage(); try { await pi.goto(url + '?room=ABCD'); await sleep(350); assert((await pi.locator('#lobby').isVisible()) && (await pi.inputValue('#inCode')) === 'ABCD', 'an invite link opens the lobby with the code filled in'); assert((await active(pi)) === 'btnJoin', 'and focus is on Join, not Create room (' + (await active(pi)) + ')'); } finally { await pi.close(); } await page.click('#btnSolo'); await sleep(200); assert(await page.evaluate(() => ['topbar', 'stage', 'side'].every((id) => !document.getElementById(id).hasAttribute('inert'))), 'in the game nothing is inert'); // keyboard users: the help opens from a focused control and gives focus back to it await page.focus('#btnPause'); await page.keyboard.press('?'); await sleep(200); assert((await page.locator('#help').isVisible()) && (await active(page)) === 'btnHelpClose', 'the ? key opens help with focus inside'); await page.keyboard.press('Escape'); await sleep(200); assert(await page.locator('#help').isHidden(), 'Escape closes help'); assert((await active(page)) === 'btnPause', 'and focus is back on the Pause button where it was (' + (await active(page)) + ')'); console.log('--- labels'); await page.evaluate(() => { NW.app.ctrl.sim.gold = 0; }); await until(() => page.evaluate(() => [...document.querySelectorAll('#palette .tbtn')].every((b) => /not enough credits/.test(b.getAttribute('aria-label')))), 1500, 'palette tiles say "not enough credits"'); assert(await page.evaluate(() => [...document.querySelectorAll('#palette .tbtn')].every((b) => b.classList.contains('poor'))), 'the same tiles are the dimmed ones'); await page.evaluate(() => { NW.app.ctrl.sim.gold = 100000; }); await until(() => page.evaluate(() => [...document.querySelectorAll('#palette .tbtn')].every((b) => !/not enough/.test(b.getAttribute('aria-label')) && /credits$/.test(b.getAttribute('aria-label')))), 1500, 'and the plain name again once affordable'); await page.keyboard.press('2'); assert((await page.getAttribute('#palette .tbtn[data-type="filter"]', 'aria-pressed')) === 'true', 'the selected tower tile is aria-pressed'); await page.keyboard.press('Escape'); assert((await page.getAttribute('#mapPick .mapcard[aria-checked="true"]', 'role')) === 'radio', 'map cards are radios with a checked state'); // contrast (guard: these were fixed before this work): the dimmed cost and a disabled button against their backgrounds, WCAG AA 4.5:1 await page.evaluate(() => { NW.app.ctrl.sim.gold = 0; }); await sleep(200); const cols = await page.evaluate(() => { const rgb = (s) => (s.match(/[\d.]+/g) || []).slice(0, 3).map(Number); const cost = document.querySelector('#palette .tbtn.poor .cost'), nm = document.querySelector('#palette .tbtn.poor .nm'), tile = document.querySelector('#palette .tbtn.poor'); const bg = rgb(getComputedStyle(tile).backgroundColor); const eff = (el) => { const cs = getComputedStyle(el); return { c: rgb(cs.color), op: parseFloat(cs.opacity) }; }; return { bg, cost: eff(cost), nm: eff(nm), opTile: parseFloat(getComputedStyle(tile).opacity) }; }); assert(cols.cost.op === 1 && cols.nm.op === 1 && cols.opTile === 1, '(guard) the unaffordable state does not fade text with opacity'); assert(ratio(cols.cost.c, cols.bg) >= 4.5 && ratio(cols.nm.c, cols.bg) >= 4.5, '(guard) unaffordable cost and name keep >= 4.5:1 contrast (' + ratio(cols.cost.c, cols.bg).toFixed(1) + ', ' + ratio(cols.nm.c, cols.bg).toFixed(1) + ')'); console.log('--- sink'); const srv = await createServer({ port: 0, host: '127.0.0.1', log: () => {}, pingMs: 0 }); const ctx2 = await browser.newContext({ viewport: { width: 1100, height: 700 }, serviceWorkers: 'block' }); await ctx2.addInitScript(() => { try { localStorage.setItem('nw.helped', 'true'); } catch (e) { /* ignore */ } }); const p2 = await ctx2.newPage(); p2.on('pageerror', (e) => errors.push('pageerror: ' + e.message)); try { await p2.goto('http://127.0.0.1:' + srv.port + '/'); await sleep(300); await p2.click('#btnCoop'); await p2.fill('#inName', 'Alice'); await p2.click('#btnCreate'); await until(() => p2.evaluate(() => NW.app.ctrl.kind === 'remote' && NW.app.ctrl.status === 'joined'), 6000, 'joined'); await p2.evaluate(() => { const c = NW.app.ctrl; c.room = 'x'; c.emit('players', c.players); }); await sleep(300); const r = await p2.evaluate(() => ({ injected: !!document.querySelector('#players #pwn, #players img'), text: document.querySelector('#players h4').textContent, xss: window.__xss || 0 })); assert(!r.injected && r.xss === 0, 'a hostile room code creates no elements in the players panel'); assert(r.text.includes(''), 'it is shown as plain text: ' + r.text.slice(0, 40)); await p2.evaluate(() => { NW.app.ctrl.room = 'A&B C'; navigator.share = (d) => { window.__shared = d; return Promise.resolve(); }; }); await p2.click('#netChip'); await sleep(150); const shared = await p2.evaluate(() => window.__shared); assert(shared && /\?room=A%26B%20C$/.test(shared.url), 'the invite link carries the code encoded: ' + (shared && shared.url)); } finally { await ctx2.close(); await srv.close(); } } finally { await ctx.close(); } if (errors.length) throw new Error('console errors:\n' + errors.join('\n')); console.log('no console errors'); };