|
| 1 | +import * as NodeVM from "node:vm"; |
| 2 | + |
1 | 3 | import { describe, expect, it, vi } from "vite-plus/test"; |
2 | 4 |
|
3 | 5 | import { sendPageCommand, toKeyboardInputEvents } from "./GuestInput.ts"; |
@@ -42,6 +44,86 @@ describe("preview guest input", () => { |
42 | 44 | expect(guest.sendCommand).not.toHaveBeenCalledWith("Input.insertText", expect.anything()); |
43 | 45 | }); |
44 | 46 |
|
| 47 | + it.each([ |
| 48 | + { |
| 49 | + sessionId: undefined, |
| 50 | + method: "Input.dispatchMouseEvent", |
| 51 | + type: "mousePressed", |
| 52 | + outcome: "restore", |
| 53 | + }, |
| 54 | + { |
| 55 | + sessionId: "child-frame", |
| 56 | + method: "Input.dispatchMouseEvent", |
| 57 | + type: "mousePressed", |
| 58 | + outcome: "restore", |
| 59 | + }, |
| 60 | + { |
| 61 | + sessionId: undefined, |
| 62 | + method: "Input.dispatchTouchEvent", |
| 63 | + type: "touchStart", |
| 64 | + outcome: "restore", |
| 65 | + }, |
| 66 | + { |
| 67 | + sessionId: undefined, |
| 68 | + method: "Input.dispatchMouseEvent", |
| 69 | + type: "mousePressed", |
| 70 | + outcome: "other-field", |
| 71 | + }, |
| 72 | + { |
| 73 | + sessionId: undefined, |
| 74 | + method: "Input.dispatchMouseEvent", |
| 75 | + type: "mousePressed", |
| 76 | + outcome: "removed", |
| 77 | + }, |
| 78 | + { |
| 79 | + sessionId: undefined, |
| 80 | + method: "Input.dispatchMouseEvent", |
| 81 | + type: "mousePressed", |
| 82 | + outcome: "failure", |
| 83 | + }, |
| 84 | + ])( |
| 85 | + "preserves host focus for $method in $sessionId with $outcome", |
| 86 | + async ({ sessionId, method, type, outcome }) => { |
| 87 | + const document = { activeElement: {} as object }; |
| 88 | + const composer = { |
| 89 | + isConnected: true, |
| 90 | + focus: () => { |
| 91 | + document.activeElement = composer; |
| 92 | + }, |
| 93 | + }; |
| 94 | + const preview = { tagName: "WEBVIEW", getWebContentsId: () => 7 }; |
| 95 | + const otherField = { tagName: "INPUT" }; |
| 96 | + document.activeElement = composer; |
| 97 | + const context = NodeVM.createContext({ document }); |
| 98 | + const host = { |
| 99 | + isDestroyed: () => false, |
| 100 | + executeJavaScript: async (script: string) => NodeVM.runInContext(script, context), |
| 101 | + }; |
| 102 | + const guest = { id: 7, focusedFrame: null, hostWebContents: host }; |
| 103 | + const debuggee = { |
| 104 | + sendCommand: async () => { |
| 105 | + document.activeElement = outcome === "other-field" ? otherField : preview; |
| 106 | + if (outcome === "removed") composer.isConnected = false; |
| 107 | + if (outcome === "failure") throw new Error("Pointer failed"); |
| 108 | + return {}; |
| 109 | + }, |
| 110 | + }; |
| 111 | + const result = sendPageCommand( |
| 112 | + guest as unknown as Electron.WebContents, |
| 113 | + debuggee as unknown as Electron.Debugger, |
| 114 | + method, |
| 115 | + { type, x: 10, y: 10, button: "left" }, |
| 116 | + sessionId, |
| 117 | + ); |
| 118 | + if (outcome === "failure") await expect(result).rejects.toThrow("Pointer failed"); |
| 119 | + else await result; |
| 120 | + expect(document.activeElement).toBe( |
| 121 | + outcome === "other-field" ? otherField : outcome === "removed" ? preview : composer, |
| 122 | + ); |
| 123 | + expect(Object.keys(context)).toEqual(["document"]); |
| 124 | + }, |
| 125 | + ); |
| 126 | + |
45 | 127 | it("refuses input when a cross-site frame holds the unfocused page's focus", async () => { |
46 | 128 | const guest = makeGuest({ focused: false, focusedDocument: false }); |
47 | 129 | await expect(guest.send("Input.insertText", { text: "secret" })).rejects.toThrow( |
|
0 commit comments