-
Notifications
You must be signed in to change notification settings - Fork 36
Let the candidate resize the code editor font #226
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+275
−5
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,183 @@ | ||
| // Run with: node --test tests/browser/editor-font-size.test.js | ||
| // | ||
| // The code font size is a candidate's choice kept between visits. What can | ||
| // break is the agreement between the textarea, the highlight overlay and the | ||
| // gutter: one layer at a different size or offset and the caret drifts away | ||
| // from the glyphs it edits, so every offered size is checked in a real browser. | ||
|
|
||
| import { after, before, test } from "node:test"; | ||
| import assert from "node:assert/strict"; | ||
|
|
||
| import { | ||
| DEFAULT_RUNTIME_CONFIG, | ||
| launchChromium, | ||
| startStaticServer, | ||
| } from "./source.js"; | ||
|
|
||
| let browser = null; | ||
| let server = null; | ||
| let base = ""; | ||
|
|
||
| before(async () => { | ||
| browser = await launchChromium(); | ||
| if (!browser) return; | ||
| ({ server, base } = await startStaticServer({ | ||
| runtimeConfig: DEFAULT_RUNTIME_CONFIG, | ||
| })); | ||
| }); | ||
|
|
||
| after(async () => { | ||
| await browser?.close(); | ||
| await new Promise((closed) => (server ? server.close(closed) : closed())); | ||
| }); | ||
|
|
||
| /// Out from behind the media preflight, which covers the toolbar until a device | ||
| /// check clears it. Hidden rather than reached through with a scripted click, | ||
| /// so the clicks below still have to land on the buttons a candidate sees. | ||
| /// Waits on the placeholder `init()` sets, not on static markup, so the stored | ||
| /// size has been applied and the buttons have their handlers. | ||
| async function openEditor(page) { | ||
| await page.waitForFunction( | ||
| () => document.querySelector("#candidate-case-input").placeholder !== "", | ||
| ); | ||
| await page.evaluate(() => { | ||
| document.querySelector("#audio-check").hidden = true; | ||
| }); | ||
| } | ||
|
|
||
| /// Four-digit line numbers, which the gutter is tightest for at the smallest | ||
| /// size. Refilled after every reload, because the buffer is not kept. Set with | ||
| /// one input event rather than `page.fill`, which takes half a minute to type | ||
| /// this many lines. | ||
| const fillLongBuffer = (page) => | ||
| page.evaluate(() => { | ||
| const editor = document.querySelector("#editor"); | ||
| editor.value = Array.from({ length: 1200 }, (_, i) => `x = ${i}`).join( | ||
| "\n", | ||
| ); | ||
| editor.dispatchEvent(new Event("input", { bubbles: true })); | ||
| }); | ||
|
|
||
| const editorMetrics = (page) => | ||
| page.evaluate(() => { | ||
| const layers = ["#editor", "#editor-highlight", "#editor-lines"].map((id) => | ||
| document.querySelector(id), | ||
| ); | ||
| const [editor, , gutter] = layers; | ||
| return { | ||
| sizes: layers.map((node) => getComputedStyle(node).fontSize), | ||
| lineHeights: layers.map((node) => getComputedStyle(node).lineHeight), | ||
| gutterRight: gutter.getBoundingClientRect().right, | ||
| editorLeft: editor.getBoundingClientRect().left, | ||
| highlightLeft: document | ||
| .querySelector("#editor-highlight") | ||
| .getBoundingClientRect().left, | ||
| gutterOverflows: gutter.scrollWidth > gutter.clientWidth, | ||
| smallerDisabled: | ||
| document | ||
| .querySelector("#editor-font-smaller") | ||
| .getAttribute("aria-disabled") === "true", | ||
| largerDisabled: | ||
| document | ||
| .querySelector("#editor-font-larger") | ||
| .getAttribute("aria-disabled") === "true", | ||
| focused: document.activeElement?.id ?? "", | ||
| stored: localStorage.getItem("codetrial:editorFontSize"), | ||
| }; | ||
| }); | ||
|
|
||
| function assertLayersAgree(metrics, label) { | ||
| assert.equal(new Set(metrics.sizes).size, 1, `${label}: ${metrics.sizes}`); | ||
| assert.equal( | ||
| new Set(metrics.lineHeights).size, | ||
| 1, | ||
| `${label}: ${metrics.lineHeights}`, | ||
| ); | ||
| assert.equal(metrics.editorLeft, metrics.highlightLeft, label); | ||
| assert.equal(metrics.editorLeft, metrics.gutterRight, label); | ||
| assert.equal(metrics.gutterOverflows, false, `${label}: gutter overflows`); | ||
| } | ||
|
|
||
| /// Clicks once per expected size rather than until the button disables, so a | ||
| /// button that stops stepping or never disables fails here instead of | ||
| /// clicking until CI times out. | ||
| async function stepThrough(page, direction, sizes) { | ||
| const disabled = `${direction}Disabled`; | ||
| for (const [index, size] of sizes.entries()) { | ||
| await page.click(`#editor-font-${direction}`); | ||
| const metrics = await editorMetrics(page); | ||
| assert.equal(metrics.sizes[0], size, `${direction} to ${size}`); | ||
| assertLayersAgree(metrics, `${direction} ${size}`); | ||
| assert.equal( | ||
| metrics[disabled], | ||
| index === sizes.length - 1, | ||
| `${direction} at ${size}: disabled only at the end of the ladder`, | ||
| ); | ||
| assert.equal( | ||
| metrics.focused, | ||
| `editor-font-${direction}`, | ||
| `${direction} at ${size}: focus stays on the button`, | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| test("every offered code font size keeps the editor layers in line, and is remembered", async (t) => { | ||
| if (!browser) return t.skip("playwright chromium unavailable"); | ||
| const page = await browser.newPage(); | ||
| try { | ||
| await page.goto(`${base}/interview.html`, { | ||
| waitUntil: "domcontentloaded", | ||
| }); | ||
| await openEditor(page); | ||
| await fillLongBuffer(page); | ||
|
|
||
| const initial = await editorMetrics(page); | ||
| assert.equal(initial.sizes[0], "14px", "0.875rem unless changed"); | ||
| assert.equal(initial.smallerDisabled, false); | ||
| assert.equal(initial.largerDisabled, false); | ||
| assertLayersAgree(initial, "default"); | ||
|
|
||
| await stepThrough(page, "larger", ["16px", "18px", "20px", "24px"]); | ||
|
|
||
| await page.reload({ waitUntil: "domcontentloaded" }); | ||
| await openEditor(page); | ||
| await fillLongBuffer(page); | ||
| const returning = await editorMetrics(page); | ||
| assert.equal(returning.sizes[0], "24px", "the choice is remembered"); | ||
| assert.equal(returning.largerDisabled, true); | ||
|
|
||
| await stepThrough(page, "smaller", [ | ||
| "20px", | ||
| "18px", | ||
| "16px", | ||
| "14px", | ||
| "12px", | ||
| ]); | ||
|
|
||
| await page.click("#editor-font-larger"); | ||
| const back = await editorMetrics(page); | ||
| assert.equal(back.sizes[0], "14px"); | ||
| assert.equal(back.stored, null, "the default is stored as no preference"); | ||
| } finally { | ||
| await page.close(); | ||
| } | ||
| }); | ||
|
|
||
| test("a stored size the page does not offer falls back to the default", async (t) => { | ||
| if (!browser) return t.skip("playwright chromium unavailable"); | ||
| const page = await browser.newPage(); | ||
| try { | ||
| await page.addInitScript(() => | ||
| localStorage.setItem("codetrial:editorFontSize", "9"), | ||
| ); | ||
| await page.goto(`${base}/interview.html`, { | ||
| waitUntil: "domcontentloaded", | ||
| }); | ||
| await openEditor(page); | ||
| const metrics = await editorMetrics(page); | ||
| assert.equal(metrics.sizes[0], "14px"); | ||
| assertLayersAgree(metrics, "fallback"); | ||
| } finally { | ||
| await page.close(); | ||
| } | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.