|
| 1 | +// Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | + |
| 3 | +/** |
| 4 | + * @file font-loading.spec.ts |
| 5 | + * @input Production docsite: the home page at a desktop size and at a phone |
| 6 | + * size, the community page, and the playground's preview frame |
| 7 | + * @output Proof that the hero reel loads ahead of time each font weight that |
| 8 | + * the cards of a slide ask for, and no other; that the themes card draws |
| 9 | + * its "Aa" with no text, so that no text of the home page asks for the |
| 10 | + * display font of a theme; and that the home page asks for Playwrite US |
| 11 | + * Trad at weight 400 only, and the preview frame for the range after it |
| 12 | + * @position Docsite browser tests. hero-theme-content.test.ts pins the list |
| 13 | + * of fonts; only a browser computes the font that card text asks for. |
| 14 | + * Each check reads what a page asks the browser for, and not which font |
| 15 | + * files arrive, so no check needs the font host. |
| 16 | + */ |
| 17 | + |
| 18 | +import {expect, test, type Page} from '@playwright/test'; |
| 19 | +import {expectTooltipsLive} from './liveTooltips'; |
| 20 | + |
| 21 | +const SLIDE_COUNT = 5; |
| 22 | + |
| 23 | +const FONT_STYLESHEETS = |
| 24 | + 'link[rel="stylesheet"][href^="https://fonts.googleapis.com/"]'; |
| 25 | + |
| 26 | +/** The weights that each font stylesheet of the page asks Playwrite for. */ |
| 27 | +function playwriteWeights(page: Page) { |
| 28 | + return page |
| 29 | + .locator(FONT_STYLESHEETS) |
| 30 | + .evaluateAll((links: HTMLLinkElement[]) => |
| 31 | + links.map(link => |
| 32 | + new URL(link.href).searchParams |
| 33 | + .getAll('family') |
| 34 | + .filter(family => family.startsWith('Playwrite US Trad')) |
| 35 | + .join(), |
| 36 | + ), |
| 37 | + ); |
| 38 | +} |
| 39 | + |
| 40 | +// The display fonts of the reel's themes. The site's font stylesheet has |
| 41 | +// them for other pages. The home page draws no text in them. |
| 42 | +const DISPLAY_FONTS = ['Sarina', 'Manufacturing Consent', 'Crimson Text']; |
| 43 | + |
| 44 | +/** |
| 45 | + * Records each `document.fonts.load()` call of the page. Returns a function |
| 46 | + * that reads the specifiers of the calls so far. |
| 47 | + */ |
| 48 | +async function recordFontLoads(page: Page) { |
| 49 | + await page.addInitScript(() => { |
| 50 | + const loads: string[] = []; |
| 51 | + Object.assign(window, {recordedFontLoads: loads}); |
| 52 | + const load = document.fonts.load.bind(document.fonts); |
| 53 | + document.fonts.load = (font: string, text?: string) => { |
| 54 | + loads.push(font); |
| 55 | + return load(font, text); |
| 56 | + }; |
| 57 | + }); |
| 58 | + return () => |
| 59 | + page.evaluate( |
| 60 | + () => |
| 61 | + (window as unknown as {recordedFontLoads: string[]}).recordedFontLoads, |
| 62 | + ); |
| 63 | +} |
| 64 | + |
| 65 | +/** |
| 66 | + * The fonts that drawn text asks for, as `document.fonts.load()` specifiers: |
| 67 | + * the first family and the weight of each piece of text that has a box. |
| 68 | + * With `inHeroCards`, only the text of the hero cards layer that shows. |
| 69 | + */ |
| 70 | +function fontsOfText(page: Page, {inHeroCards}: {inHeroCards: boolean}) { |
| 71 | + return page.evaluate(heroOnly => { |
| 72 | + const reelPhoto = Array.from(document.images).find( |
| 73 | + img => |
| 74 | + img.getClientRects().length > 0 && |
| 75 | + /\/landing\/hero\/(?!avatar-)/u.test( |
| 76 | + decodeURIComponent(img.currentSrc), |
| 77 | + ), |
| 78 | + ); |
| 79 | + const root = heroOnly ? reelPhoto?.closest('[inert]') : document.body; |
| 80 | + if (root == null) { |
| 81 | + return []; |
| 82 | + } |
| 83 | + const fonts = new Set<string>(); |
| 84 | + const add = (element: Element) => { |
| 85 | + const style = getComputedStyle(element); |
| 86 | + const family = style.fontFamily |
| 87 | + .split(',')[0] |
| 88 | + .trim() |
| 89 | + .replace(/^["']|["']$/gu, ''); |
| 90 | + fonts.add(`${style.fontWeight} 1rem "${family}"`); |
| 91 | + }; |
| 92 | + const range = document.createRange(); |
| 93 | + const texts = document.createTreeWalker(root, NodeFilter.SHOW_TEXT); |
| 94 | + for (let text = texts.nextNode(); text != null; text = texts.nextNode()) { |
| 95 | + range.selectNodeContents(text); |
| 96 | + if ( |
| 97 | + text.nodeValue?.trim() !== '' && |
| 98 | + text.parentElement != null && |
| 99 | + range.getClientRects().length > 0 |
| 100 | + ) { |
| 101 | + add(text.parentElement); |
| 102 | + } |
| 103 | + } |
| 104 | + // A field draws its placeholder in its own font. |
| 105 | + for (const field of root.querySelectorAll('input, textarea')) { |
| 106 | + if (field.getClientRects().length > 0) { |
| 107 | + add(field); |
| 108 | + } |
| 109 | + } |
| 110 | + return Array.from(fonts).sort(); |
| 111 | + }, inHeroCards); |
| 112 | +} |
| 113 | + |
| 114 | +/** The reel's dot for the slide at `position`, from 1. */ |
| 115 | +function dot(page: Page, position: number) { |
| 116 | + return page |
| 117 | + .getByRole('navigation', {name: 'Preview Astryx themes'}) |
| 118 | + .getByRole('radio', {name: `Page ${position}`}); |
| 119 | +} |
| 120 | + |
| 121 | +/** |
| 122 | + * Moves keyboard focus to the reel's dot at `position`, which shows its |
| 123 | + * slide. The reel can become live after the shell, so the focus repeats. |
| 124 | + */ |
| 125 | +async function showSlide(page: Page, position: number) { |
| 126 | + await expect(async () => { |
| 127 | + await dot(page, position).focus(); |
| 128 | + await expect(dot(page, position)).toBeChecked({timeout: 1000}); |
| 129 | + }).toPass(); |
| 130 | +} |
| 131 | + |
| 132 | +/** Waits until the page's shell is live, and the page has been idle. */ |
| 133 | +async function settle(page: Page) { |
| 134 | + await expectTooltipsLive( |
| 135 | + page, |
| 136 | + page |
| 137 | + .getByRole('navigation', {name: 'Astryx navigation'}) |
| 138 | + .getByRole('button', {name: 'Search'}) |
| 139 | + .filter({visible: true}) |
| 140 | + .first(), |
| 141 | + ); |
| 142 | + await page.waitForLoadState('networkidle'); |
| 143 | + await page.evaluate( |
| 144 | + () => |
| 145 | + new Promise(resolve => { |
| 146 | + requestIdleCallback(() => resolve(null), {timeout: 5000}); |
| 147 | + }), |
| 148 | + ); |
| 149 | +} |
| 150 | + |
| 151 | +test.describe('at a desktop size', () => { |
| 152 | + // With reduced motion the reel does not advance by itself while a test |
| 153 | + // goes through the slides. It loads the same fonts ahead of time. |
| 154 | + test.use({ |
| 155 | + viewport: {width: 1440, height: 900}, |
| 156 | + contextOptions: {reducedMotion: 'reduce'}, |
| 157 | + }); |
| 158 | + |
| 159 | + test('the reel loads ahead of time each font weight that a hero card asks for, once, and no other', async ({ |
| 160 | + page, |
| 161 | + }) => { |
| 162 | + const fontLoads = await recordFontLoads(page); |
| 163 | + await page.goto('/'); |
| 164 | + await settle(page); |
| 165 | + |
| 166 | + const asked = new Set<string>(); |
| 167 | + for (let position = 1; position <= SLIDE_COUNT; position++) { |
| 168 | + await showSlide(page, position); |
| 169 | + const fonts = await fontsOfText(page, {inHeroCards: true}); |
| 170 | + expect(fonts.length, `slide ${position}`).toBeGreaterThan(0); |
| 171 | + fonts.forEach(font => asked.add(font)); |
| 172 | + } |
| 173 | + await expect |
| 174 | + .poll(async () => (await fontLoads()).sort()) |
| 175 | + .toEqual(Array.from(asked).sort()); |
| 176 | + }); |
| 177 | + |
| 178 | + test('no text of the page asks for the display font of a theme', async ({ |
| 179 | + page, |
| 180 | + }) => { |
| 181 | + await page.goto('/'); |
| 182 | + await settle(page); |
| 183 | + for (let position = 1; position <= SLIDE_COUNT; position++) { |
| 184 | + await showSlide(page, position); |
| 185 | + const fonts = await fontsOfText(page, {inHeroCards: false}); |
| 186 | + expect( |
| 187 | + fonts.filter(font => |
| 188 | + DISPLAY_FONTS.some(family => font.includes(`"${family}"`)), |
| 189 | + ), |
| 190 | + `slide ${position}`, |
| 191 | + ).toEqual([]); |
| 192 | + } |
| 193 | + }); |
| 194 | + |
| 195 | + test('the font stylesheet of the home page asks for Playwrite US Trad at weight 400 only', async ({ |
| 196 | + page, |
| 197 | + }) => { |
| 198 | + await page.goto('/'); |
| 199 | + expect(await playwriteWeights(page)).toEqual([ |
| 200 | + 'Playwrite US Trad:wght@400', |
| 201 | + ]); |
| 202 | + }); |
| 203 | + |
| 204 | + test("the playground's preview frame asks for the weight range of Playwrite US Trad, after weight 400", async ({ |
| 205 | + page, |
| 206 | + }) => { |
| 207 | + // The frame's document. A browser gives each weight to the later of two |
| 208 | + // faces that have it. |
| 209 | + await page.goto('/playground/preview'); |
| 210 | + expect(await playwriteWeights(page)).toEqual([ |
| 211 | + 'Playwrite US Trad:wght@400', |
| 212 | + 'Playwrite US Trad:wght@100..400', |
| 213 | + ]); |
| 214 | + }); |
| 215 | + |
| 216 | + for (const path of ['/', '/community']) { |
| 217 | + for (const colorScheme of ['light', 'dark'] as const) { |
| 218 | + test(`on ${path} in ${colorScheme} mode, the themes card draws its "Aa" in the place of the text, in the color of the first swatch`, async ({ |
| 219 | + page, |
| 220 | + }) => { |
| 221 | + await page.emulateMedia({colorScheme}); |
| 222 | + await page.goto(path); |
| 223 | + await settle(page); |
| 224 | + |
| 225 | + // Two ems of a 2048-unit font wide, one em high. |
| 226 | + const drawing = page.locator('svg[viewBox$=" 4096 2048"]'); |
| 227 | + await expect(drawing).toHaveCount(1); |
| 228 | + await expect(drawing).toHaveAttribute('aria-hidden', 'true'); |
| 229 | + await drawing.scrollIntoViewIfNeeded(); |
| 230 | + expect(await drawing.boundingBox()).toMatchObject({ |
| 231 | + width: 48, |
| 232 | + height: 24, |
| 233 | + }); |
| 234 | + |
| 235 | + const {ink, color, swatch, text} = await drawing.evaluate( |
| 236 | + (svg: SVGSVGElement) => { |
| 237 | + const box = svg.getBoundingClientRect(); |
| 238 | + const letters = svg.querySelector('path')?.getBoundingClientRect(); |
| 239 | + const firstSwatch = svg.parentElement?.firstElementChild; |
| 240 | + return { |
| 241 | + // In px from the left and from the top of the drawing's box. |
| 242 | + ink: |
| 243 | + letters == null |
| 244 | + ? [] |
| 245 | + : [ |
| 246 | + letters.left - box.left, |
| 247 | + letters.top - box.top, |
| 248 | + letters.right - box.left, |
| 249 | + letters.bottom - box.top, |
| 250 | + ], |
| 251 | + color: getComputedStyle(svg).color, |
| 252 | + swatch: |
| 253 | + firstSwatch == null |
| 254 | + ? null |
| 255 | + : getComputedStyle(firstSwatch).backgroundColor, |
| 256 | + text: svg.parentElement?.textContent, |
| 257 | + }; |
| 258 | + }, |
| 259 | + ); |
| 260 | + // The extent of the glyphs "A" and "a" of Sarina at 24px, with the |
| 261 | + // "a" 30px after the start of the "A" and the baseline 19px below |
| 262 | + // the top of the box. |
| 263 | + const extent = [-0.129, 2.957, 50.73, 19.785]; |
| 264 | + expect(ink).toHaveLength(extent.length); |
| 265 | + ink.forEach((edge, index) => |
| 266 | + expect(edge).toBeCloseTo(extent[index], 1), |
| 267 | + ); |
| 268 | + expect(color).toBe(swatch); |
| 269 | + expect(text).toBe(''); |
| 270 | + }); |
| 271 | + } |
| 272 | + } |
| 273 | +}); |
| 274 | + |
| 275 | +test.describe('at a phone size', () => { |
| 276 | + test.use({ |
| 277 | + viewport: {width: 390, height: 844}, |
| 278 | + hasTouch: true, |
| 279 | + isMobile: true, |
| 280 | + }); |
| 281 | + |
| 282 | + test('the reel loads for each slide that it does not show first the font weights that the cards of that slide ask for', async ({ |
| 283 | + page, |
| 284 | + }) => { |
| 285 | + const fontLoads = await recordFontLoads(page); |
| 286 | + await page.goto('/'); |
| 287 | + await settle(page); |
| 288 | + expect(await fontLoads()).toEqual([]); |
| 289 | + |
| 290 | + // The last link of the hero text, which is before the dots. Keyboard |
| 291 | + // focus on the dots loads the slides next to the first one. |
| 292 | + await page.getByRole('link', {name: 'StyleX', exact: true}).focus(); |
| 293 | + await page.keyboard.press('Tab'); |
| 294 | + await expect(dot(page, 1)).toBeFocused(); |
| 295 | + |
| 296 | + const asked = new Set<string>(); |
| 297 | + for (let position = 2; position <= SLIDE_COUNT; position++) { |
| 298 | + await page.keyboard.press('ArrowRight'); |
| 299 | + await expect(dot(page, position)).toBeChecked(); |
| 300 | + const fonts = await fontsOfText(page, {inHeroCards: true}); |
| 301 | + expect(fonts.length, `slide ${position}`).toBeGreaterThan(0); |
| 302 | + fonts.forEach(font => asked.add(font)); |
| 303 | + } |
| 304 | + expect((await fontLoads()).sort()).toEqual(Array.from(asked).sort()); |
| 305 | + }); |
| 306 | +}); |
0 commit comments