|
| 1 | +import { expect, test, type CDPSession, type Page } from "@playwright/test"; |
| 2 | + |
| 3 | +interface ImeSnapshot { |
| 4 | + canonical: string; |
| 5 | + canonicalUpdates: number; |
| 6 | + suggestion: string; |
| 7 | + editorComposing: boolean; |
| 8 | + phase: "idle" | "composing" | "settled"; |
| 9 | + events: Array<{ |
| 10 | + type: string; |
| 11 | + data: string | null; |
| 12 | + inputType: string | null; |
| 13 | + isComposing: boolean | null; |
| 14 | + }>; |
| 15 | + proposal: null | { |
| 16 | + status: string; |
| 17 | + replacement: Array<{ type: string; text?: string }>; |
| 18 | + }; |
| 19 | +} |
| 20 | + |
| 21 | +interface WorkspaceImeHarness { |
| 22 | + snapshot(): ImeSnapshot; |
| 23 | + capture(id: string): ImeSnapshot; |
| 24 | + accept(): ImeSnapshot; |
| 25 | + reject(): ImeSnapshot; |
| 26 | + resetSuggestion(): ImeSnapshot; |
| 27 | +} |
| 28 | + |
| 29 | +declare global { |
| 30 | + interface Window { |
| 31 | + workspaceIme: WorkspaceImeHarness; |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +async function snapshot(page: Page) { |
| 36 | + return page.evaluate(() => window.workspaceIme.snapshot()); |
| 37 | +} |
| 38 | + |
| 39 | +async function compose( |
| 40 | + page: Page, |
| 41 | + cdp: CDPSession, |
| 42 | + id: string, |
| 43 | + steps: [string, ...string[]], |
| 44 | + accepted: string, |
| 45 | + acceptedUpdates: number, |
| 46 | +) { |
| 47 | + await page.locator(".ProseMirror").focus(); |
| 48 | + for (const text of steps) { |
| 49 | + await cdp.send("Input.imeSetComposition", { |
| 50 | + text, |
| 51 | + selectionStart: text.length, |
| 52 | + selectionEnd: text.length, |
| 53 | + }); |
| 54 | + const during = await page.evaluate((proposalId) => |
| 55 | + window.workspaceIme.capture(proposalId), id, |
| 56 | + ); |
| 57 | + expect(during.phase).toBe("composing"); |
| 58 | + expect(during.proposal).toBeNull(); |
| 59 | + expect(during.canonical).toBe(accepted); |
| 60 | + expect(during.canonicalUpdates).toBe(acceptedUpdates); |
| 61 | + } |
| 62 | + await cdp.send("Input.insertText", { text: steps[steps.length - 1] }); |
| 63 | + await expect.poll(async () => (await snapshot(page)).phase).toBe("settled"); |
| 64 | + await expect.poll(async () => (await snapshot(page)).editorComposing).toBe(false); |
| 65 | +} |
| 66 | + |
| 67 | +test("captures Spanish and Hungarian IME text only after composition settles", async ({ |
| 68 | + page, |
| 69 | + context, |
| 70 | +}, testInfo) => { |
| 71 | + test.skip(testInfo.project.name !== "desktop-chromium", "Chromium CDP provides native IME composition"); |
| 72 | + |
| 73 | + await page.goto("/e2e/fixtures/workspace-review-ime.html"); |
| 74 | + await expect.poll(() => page.evaluate(() => Boolean(window.workspaceIme))).toBe(true); |
| 75 | + const cdp = await context.newCDPSession(page); |
| 76 | + |
| 77 | + await compose(page, cdp, "spanish", ["n", "ni", "niñ", "niño"], "Accepted:", 0); |
| 78 | + const spanishSettled = await page.evaluate(() => window.workspaceIme.capture("spanish")); |
| 79 | + expect(spanishSettled.events.map((event) => event.type)).toEqual(expect.arrayContaining([ |
| 80 | + "compositionstart", |
| 81 | + "beforeinput", |
| 82 | + "input", |
| 83 | + "compositionend", |
| 84 | + ])); |
| 85 | + expect(spanishSettled.events.filter((event) => event.type === "beforeinput").every((event) => |
| 86 | + event.inputType === "insertCompositionText" && event.isComposing, |
| 87 | + )).toBe(true); |
| 88 | + expect(spanishSettled.proposal).toEqual({ |
| 89 | + status: "pending", |
| 90 | + replacement: [{ type: "text", text: "niño" }], |
| 91 | + }); |
| 92 | + expect(spanishSettled.canonical).toBe("Accepted:"); |
| 93 | + expect(spanishSettled.canonicalUpdates).toBe(0); |
| 94 | + expect((await page.evaluate(() => window.workspaceIme.accept())).canonical).toBe("Accepted:niño"); |
| 95 | + |
| 96 | + await page.evaluate(() => window.workspaceIme.resetSuggestion()); |
| 97 | + await compose( |
| 98 | + page, |
| 99 | + cdp, |
| 100 | + "hungarian", |
| 101 | + ["t", "tű", "tűr", "tűrő"], |
| 102 | + "Accepted:niño", |
| 103 | + 1, |
| 104 | + ); |
| 105 | + const hungarianSettled = await page.evaluate(() => window.workspaceIme.capture("hungarian")); |
| 106 | + expect(hungarianSettled.proposal).toEqual({ |
| 107 | + status: "pending", |
| 108 | + replacement: [{ type: "text", text: "tűrő" }], |
| 109 | + }); |
| 110 | + expect(hungarianSettled.canonical).toBe("Accepted:niño"); |
| 111 | + const rejected = await page.evaluate(() => window.workspaceIme.reject()); |
| 112 | + expect(rejected.proposal?.status).toBe("rejected"); |
| 113 | + expect(rejected.canonical).toBe("Accepted:niño"); |
| 114 | +}); |
0 commit comments