|
| 1 | +import { expect, test, type Page } from "@playwright/test"; |
| 2 | + |
| 3 | +const grain = (page: Page) => |
| 4 | + page.locator("[data-slot='generated-image-grain']"); |
| 5 | + |
| 6 | +async function land(page: Page) { |
| 7 | + await page.goto("/media-viewer"); |
| 8 | + await expect( |
| 9 | + page.locator("[data-slot='generated-image-placeholder']"), |
| 10 | + ).toHaveCount(3); |
| 11 | + await expect(page.getByRole("status")).toHaveText("Generating 3 images"); |
| 12 | + await page.getByRole("button", { name: "Land images" }).click(); |
| 13 | +} |
| 14 | + |
| 15 | +test("images made while watched are revealed by the grain", async ({ |
| 16 | + page, |
| 17 | +}) => { |
| 18 | + await land(page); |
| 19 | + await expect(grain(page)).toHaveCount(3); |
| 20 | + await expect(page.getByRole("status")).toHaveText("Made 3 images"); |
| 21 | + // The reveal is one 700ms pass per tile, cascading 90ms apart. |
| 22 | + await expect(grain(page)).toHaveCount(0, { timeout: 3_000 }); |
| 23 | + const tiles = page.locator("[data-slot='generated-image']"); |
| 24 | + await expect(tiles).toHaveCount(3); |
| 25 | + for (const tile of await tiles.all()) { |
| 26 | + await expect(tile).toHaveAttribute("data-state", "loaded"); |
| 27 | + } |
| 28 | +}); |
| 29 | + |
| 30 | +test("with reduced motion, landed images skip the grain", async ({ page }) => { |
| 31 | + await page.emulateMedia({ reducedMotion: "reduce" }); |
| 32 | + await land(page); |
| 33 | + await expect( |
| 34 | + page.locator("[data-slot='generated-image'][data-state='loaded']"), |
| 35 | + ).toHaveCount(3); |
| 36 | + await expect(grain(page)).toHaveCount(0); |
| 37 | +}); |
| 38 | + |
| 39 | +test("arrow keys move between items, Escape closes, focus returns to the tile", async ({ |
| 40 | + page, |
| 41 | +}) => { |
| 42 | + await land(page); |
| 43 | + await expect(grain(page)).toHaveCount(0, { timeout: 3_000 }); |
| 44 | + const tile = page.getByRole("button", { |
| 45 | + name: "Open Swatch 2", |
| 46 | + exact: true, |
| 47 | + }); |
| 48 | + await tile.click(); |
| 49 | + |
| 50 | + const dialog = page.getByRole("dialog"); |
| 51 | + await expect(dialog).toHaveAccessibleName("three-swatches-2.svg"); |
| 52 | + await expect(page.getByRole("button", { name: "Close" })).toBeFocused(); |
| 53 | + await expect(dialog.getByRole("img")).toHaveAttribute("alt", "Swatch 2"); |
| 54 | + |
| 55 | + await page.keyboard.press("ArrowRight"); |
| 56 | + await expect(dialog).toHaveAccessibleName("three-swatches-3.svg"); |
| 57 | + await expect(dialog.getByRole("status")).toHaveText( |
| 58 | + "three-swatches-3.svg, 3 of 3", |
| 59 | + ); |
| 60 | + // Stepping stops at the ends rather than wrapping. |
| 61 | + await page.keyboard.press("ArrowRight"); |
| 62 | + await expect(dialog).toHaveAccessibleName("three-swatches-3.svg"); |
| 63 | + await expect(dialog.getByRole("button", { name: "Next" })).toHaveAttribute( |
| 64 | + "aria-disabled", |
| 65 | + "true", |
| 66 | + ); |
| 67 | + |
| 68 | + await page.keyboard.press("ArrowLeft"); |
| 69 | + await page.keyboard.press("ArrowLeft"); |
| 70 | + await expect(dialog).toHaveAccessibleName("three-swatches-1.svg"); |
| 71 | + await page.keyboard.press("ArrowLeft"); |
| 72 | + await expect(dialog).toHaveAccessibleName("three-swatches-1.svg"); |
| 73 | + |
| 74 | + await page.keyboard.press("Escape"); |
| 75 | + await expect(dialog).toHaveCount(0); |
| 76 | + await expect(tile).toBeFocused(); |
| 77 | +}); |
| 78 | + |
| 79 | +test("the viewer opens from the keyboard and closes from its button or the backdrop", async ({ |
| 80 | + page, |
| 81 | +}) => { |
| 82 | + await land(page); |
| 83 | + await expect(grain(page)).toHaveCount(0, { timeout: 3_000 }); |
| 84 | + const tile = page.getByRole("button", { |
| 85 | + name: "Open Swatch 3", |
| 86 | + exact: true, |
| 87 | + }); |
| 88 | + await tile.focus(); |
| 89 | + await page.keyboard.press("Enter"); |
| 90 | + const dialog = page.getByRole("dialog"); |
| 91 | + await expect(dialog).toHaveAccessibleName("three-swatches-3.svg"); |
| 92 | + await dialog.getByRole("button", { name: "Previous" }).click(); |
| 93 | + await expect(dialog).toHaveAccessibleName("three-swatches-2.svg"); |
| 94 | + await dialog.getByRole("button", { name: "Close" }).click(); |
| 95 | + await expect(page.getByRole("dialog")).toHaveCount(0); |
| 96 | + await expect(tile).toBeFocused(); |
| 97 | + |
| 98 | + // The action button opens it too, and a click outside the media closes it. |
| 99 | + const action = page.getByRole("button", { |
| 100 | + name: "Open Swatch 1 full size", |
| 101 | + }); |
| 102 | + await action.focus(); |
| 103 | + await page.keyboard.press("Enter"); |
| 104 | + await expect(page.getByRole("dialog")).toHaveAccessibleName( |
| 105 | + "three-swatches-1.svg", |
| 106 | + ); |
| 107 | + await page.mouse.click(4, 4); |
| 108 | + await expect(page.getByRole("dialog")).toHaveCount(0); |
| 109 | + await expect(action).toBeFocused(); |
| 110 | +}); |
| 111 | + |
| 112 | +const REMOTE_SVG = |
| 113 | + '<svg xmlns="http://www.w3.org/2000/svg" width="300" height="200"><rect width="300" height="200" fill="gray"/></svg>'; |
| 114 | + |
| 115 | +async function openRemote(page: Page) { |
| 116 | + await page |
| 117 | + .context() |
| 118 | + .route("https://images.example.test/**", (route) => |
| 119 | + route.fulfill({ contentType: "image/svg+xml", body: REMOTE_SVG }), |
| 120 | + ); |
| 121 | + await page.goto("/media-viewer-chat"); |
| 122 | + await page |
| 123 | + .getByRole("button", { name: "Open Remote 2", exact: true }) |
| 124 | + .click(); |
| 125 | + const dialog = page.getByRole("dialog"); |
| 126 | + await expect(dialog).toHaveAccessibleName("remote-swatches-2.svg"); |
| 127 | + return dialog; |
| 128 | +} |
| 129 | + |
| 130 | +const scrolls = (page: Page) => |
| 131 | + page.evaluate(() => ({ |
| 132 | + page: document.scrollingElement?.scrollTop ?? 0, |
| 133 | + log: document.querySelector("[data-testid='log']")?.scrollTop ?? 0, |
| 134 | + })); |
| 135 | + |
| 136 | +test("with the viewer open, the wheel scrolls neither the page nor the log behind it", async ({ |
| 137 | + page, |
| 138 | +}) => { |
| 139 | + await openRemote(page); |
| 140 | + const before = await scrolls(page); |
| 141 | + expect(before.log).toBeGreaterThan(0); |
| 142 | + // Over the backdrop, the picture and the bar, in both directions. |
| 143 | + for (const [x, y] of [ |
| 144 | + [640, 360], |
| 145 | + [20, 20], |
| 146 | + [640, 640], |
| 147 | + ] as const) { |
| 148 | + await page.mouse.move(x, y); |
| 149 | + await page.mouse.wheel(0, 800); |
| 150 | + await page.mouse.wheel(0, -400); |
| 151 | + } |
| 152 | + await page.waitForTimeout(300); |
| 153 | + expect(await scrolls(page)).toEqual(before); |
| 154 | + |
| 155 | + // Closing hands scrolling back to the page. |
| 156 | + await page.keyboard.press("Escape"); |
| 157 | + await expect(page.getByRole("dialog")).toHaveCount(0); |
| 158 | + expect( |
| 159 | + await page.evaluate(() => document.documentElement.style.overflow), |
| 160 | + ).toBe(""); |
| 161 | + await page.mouse.move(640, 700); |
| 162 | + await page.mouse.wheel(0, 400); |
| 163 | + await expect |
| 164 | + .poll(async () => (await scrolls(page)).page) |
| 165 | + .toBeGreaterThan(before.page); |
| 166 | +}); |
| 167 | + |
| 168 | +test("Download of an image from another origin leaves the chat in place", async ({ |
| 169 | + page, |
| 170 | +}) => { |
| 171 | + const dialog = await openRemote(page); |
| 172 | + const popup = page.waitForEvent("popup"); |
| 173 | + await dialog |
| 174 | + .getByRole("link", { name: "Download remote-swatches-2.svg" }) |
| 175 | + .click(); |
| 176 | + await popup; |
| 177 | + expect(new URL(page.url()).pathname).toBe("/media-viewer-chat"); |
| 178 | + await expect(dialog).toBeVisible(); |
| 179 | +}); |
0 commit comments