ff0c60c493cd4a4259753204531b07d9a2bd001c
[WebKit-https.git] / LayoutTests / resources / ui-helper.js
1
2 window.UIHelper = class UIHelper {
3     static isIOS()
4     {
5         return navigator.userAgent.includes('iPhone') || navigator.userAgent.includes('iPad');
6     }
7
8     static isWebKit2()
9     {
10         return window.testRunner.isWebKit2;
11     }
12
13     static tapAt(x, y)
14     {
15         console.assert(this.isIOS());
16
17         if (!this.isWebKit2()) {
18             eventSender.addTouchPoint(x, y);
19             eventSender.touchStart();
20             eventSender.releaseTouchPoint(0);
21             eventSender.touchEnd();
22             return Promise.resolve();
23         }
24
25         return new Promise((resolve) => {
26             testRunner.runUIScript(`
27                 uiController.singleTapAtPoint(${x}, ${y}, function() {
28                     uiController.uiScriptComplete('Done');
29                 });`, resolve);
30         });
31     }
32
33     static activateAt(x, y)
34     {
35         if (!this.isWebKit2() || !this.isIOS()) {
36             eventSender.mouseMoveTo(x, y);
37             eventSender.mouseDown();
38             eventSender.mouseUp();
39             return Promise.resolve();
40         }
41
42         return new Promise((resolve) => {
43             testRunner.runUIScript(`
44                 uiController.singleTapAtPoint(${x}, ${y}, function() {
45                     uiController.uiScriptComplete('Done');
46                 });`, resolve);
47         });
48     }
49
50     static activateElement(element)
51     {
52         const x = element.offsetLeft + element.offsetWidth / 2;
53         const y = element.offsetTop + element.offsetHeight / 2;
54         return UIHelper.activateAt(x, y);
55     }
56
57     static keyDown(key)
58     {
59         if (!this.isWebKit2() || !this.isIOS()) {
60             eventSender.keyDown(key);
61             return Promise.resolve();
62         }
63
64         return new Promise((resolve) => {
65             testRunner.runUIScript(`
66                 uiController.keyDownUsingHardwareKeyboard("downArrow", function() {
67                     uiController.uiScriptComplete("Done");
68                 });`, resolve);
69         });
70     }
71
72     static ensurePresentationUpdate()
73     {
74         if (!this.isWebKit2()) {
75             testRunner.display();
76             return Promise.resolve();
77         }
78
79         return new Promise(resolve => {
80             testRunner.runUIScript(`
81                 uiController.doAfterPresentationUpdate(function() {
82                     uiController.uiScriptComplete('Done');
83                 });`, resolve);
84         });
85     }
86
87     static ensureVisibleContentRectUpdate()
88     {
89         if (!this.isWebKit2())
90             return Promise.resolve();
91
92         return new Promise(resolve => {
93             const visibleContentRectUpdateScript = "uiController.doAfterVisibleContentRectUpdate(() => uiController.uiScriptComplete())";
94             testRunner.runUIScript(visibleContentRectUpdateScript, resolve);
95         });
96     }
97
98     static activateAndWaitForInputSessionAt(x, y)
99     {
100         if (!this.isWebKit2() || !this.isIOS())
101             return this.activateAt(x, y);
102
103         return new Promise(resolve => {
104             testRunner.runUIScript(`
105                 (function() {
106                     uiController.didShowKeyboardCallback = function() {
107                         uiController.uiScriptComplete("Done");
108                     };
109                     uiController.singleTapAtPoint(${x}, ${y}, function() { });
110                 })()`, resolve);
111         });
112     }
113
114     static activateFormControl(element)
115     {
116         if (!this.isWebKit2() || !this.isIOS())
117             return this.activateElement(element);
118
119         const x = element.offsetLeft + element.offsetWidth / 2;
120         const y = element.offsetTop + element.offsetHeight / 2;
121
122         return new Promise(resolve => {
123             testRunner.runUIScript(`
124                 (function() {
125                     uiController.didStartFormControlInteractionCallback = function() {
126                         uiController.uiScriptComplete("Done");
127                     };
128                     uiController.singleTapAtPoint(${x}, ${y}, function() { });
129                 })()`, resolve);
130         });
131     }
132
133     static waitForKeyboardToHide()
134     {
135         return new Promise(resolve => {
136             testRunner.runUIScript(`
137                 (function() {
138                     uiController.didHideKeyboardCallback = () => uiController.uiScriptComplete();
139                 })()`, resolve);
140         });
141     }
142
143     static getUICaretRect()
144     {
145         if (!this.isWebKit2() || !this.isIOS())
146             return Promise.resolve();
147
148         return new Promise(resolve => {
149             testRunner.runUIScript(`(function() {
150                 uiController.doAfterNextStablePresentationUpdate(function() {
151                     uiController.uiScriptComplete(JSON.stringify(uiController.textSelectionCaretRect));
152                 });
153             })()`, jsonString => {
154                 resolve(JSON.parse(jsonString));
155             });
156         });
157     }
158
159     static getUISelectionRects()
160     {
161         if (!this.isWebKit2() || !this.isIOS())
162             return Promise.resolve();
163
164         return new Promise(resolve => {
165             testRunner.runUIScript(`(function() {
166                 uiController.doAfterNextStablePresentationUpdate(function() {
167                     uiController.uiScriptComplete(JSON.stringify(uiController.selectionRangeViewRects));
168                 });
169             })()`, jsonString => {
170                 resolve(JSON.parse(jsonString));
171             });
172         });
173     }
174
175     static getSelectionStartGrabberViewRect()
176     {
177         if (!this.isWebKit2() || !this.isIOS())
178             return Promise.resolve();
179
180         return new Promise(resolve => {
181             testRunner.runUIScript(`(function() {
182                 uiController.doAfterNextStablePresentationUpdate(function() {
183                     uiController.uiScriptComplete(JSON.stringify(uiController.selectionStartGrabberViewRect));
184                 });
185             })()`, jsonString => {
186                 resolve(JSON.parse(jsonString));
187             });
188         });
189     }
190
191     static getSelectionEndGrabberViewRect()
192     {
193         if (!this.isWebKit2() || !this.isIOS())
194             return Promise.resolve();
195
196         return new Promise(resolve => {
197             testRunner.runUIScript(`(function() {
198                 uiController.doAfterNextStablePresentationUpdate(function() {
199                     uiController.uiScriptComplete(JSON.stringify(uiController.selectionEndGrabberViewRect));
200                 });
201             })()`, jsonString => {
202                 resolve(JSON.parse(jsonString));
203             });
204         });
205     }
206
207     static replaceTextAtRange(text, location, length) {
208         return new Promise(resolve => {
209             testRunner.runUIScript(`(() => {
210                 uiController.replaceTextAtRange("${text}", ${location}, ${length});
211                 uiController.uiScriptComplete('Done');
212             })()`, resolve);
213         });
214     }
215
216     static wait(promise)
217     {
218         testRunner.waitUntilDone();
219         if (window.finishJSTest)
220             window.jsTestIsAsync = true;
221
222         let finish = () => {
223             if (window.finishJSTest)
224                 finishJSTest();
225             else
226                 testRunner.notifyDone();
227         }
228
229         return promise.then(finish, finish);
230     }
231
232     static withUserGesture(callback)
233     {
234         internals.withUserGesture(callback);
235     }
236
237     static selectFormAccessoryPickerRow(rowIndex)
238     {
239         const selectRowScript = `(() => uiController.selectFormAccessoryPickerRow(${rowIndex}))()`;
240         return new Promise(resolve => testRunner.runUIScript(selectRowScript, resolve));
241     }
242
243     static selectFormPopoverTitle()
244     {
245         return new Promise(resolve => {
246             testRunner.runUIScript(`(() => {
247                 uiController.uiScriptComplete(uiController.selectFormPopoverTitle);
248             })()`, resolve);
249         });
250     }
251
252     static enterText(text)
253     {
254         const escapedText = text.replace(/`/g, "\\`");
255         const enterTextScript = `(() => uiController.enterText(\`${escapedText}\`))()`;
256         return new Promise(resolve => testRunner.runUIScript(enterTextScript, resolve));
257     }
258
259     static setTimePickerValue(hours, minutes)
260     {
261         const setValueScript = `(() => uiController.setTimePickerValue(${hours}, ${minutes}))()`;
262         return new Promise(resolve => testRunner.runUIScript(setValueScript, resolve));
263     }
264
265     static setShareSheetCompletesImmediatelyWithResolution(resolved)
266     {
267         const resolveShareSheet = `(() => uiController.setShareSheetCompletesImmediatelyWithResolution(${resolved}))()`;
268         return new Promise(resolve => testRunner.runUIScript(resolveShareSheet, resolve));
269     }
270
271     static textContentType()
272     {
273         return new Promise(resolve => {
274             testRunner.runUIScript(`(() => {
275                 uiController.uiScriptComplete(uiController.textContentType);
276             })()`, resolve);
277         });
278     }
279
280     static formInputLabel()
281     {
282         return new Promise(resolve => {
283             testRunner.runUIScript(`(() => {
284                 uiController.uiScriptComplete(uiController.formInputLabel);
285             })()`, resolve);
286         });
287     }
288
289     static isShowingDataListSuggestions()
290     {
291         return new Promise(resolve => {
292             testRunner.runUIScript(`(() => {
293                 uiController.uiScriptComplete(uiController.isShowingDataListSuggestions);
294             })()`, resolve);
295         });
296     }
297
298     static zoomScale()
299     {
300         return new Promise(resolve => {
301             testRunner.runUIScript(`(() => {
302                 uiController.uiScriptComplete(uiController.zoomScale);
303             })()`, resolve);
304         });
305     }
306
307     static typeCharacter(characterString)
308     {
309         if (!this.isWebKit2() || !this.isIOS()) {
310             eventSender.keyDown(characterString);
311             return;
312         }
313
314         const escapedString = characterString.replace(/\\/g, "\\\\").replace(/`/g, "\\`");
315         const uiScript = `uiController.typeCharacterUsingHardwareKeyboard(\`${escapedString}\`, () => uiController.uiScriptComplete())`;
316         return new Promise(resolve => testRunner.runUIScript(uiScript, resolve));
317     }
318
319     static applyAutocorrection(newText, oldText)
320     {
321         if (!this.isWebKit2())
322             return;
323
324         const [escapedNewText, escapedOldText] = [newText.replace(/`/g, "\\`"), oldText.replace(/`/g, "\\`")];
325         const uiScript = `uiController.applyAutocorrection(\`${escapedNewText}\`, \`${escapedOldText}\`, () => uiController.uiScriptComplete())`;
326         return new Promise(resolve => testRunner.runUIScript(uiScript, resolve));
327     }
328
329     static inputViewBounds()
330     {
331         if (!this.isWebKit2() || !this.isIOS())
332             return Promise.resolve();
333
334         return new Promise(resolve => {
335             testRunner.runUIScript(`(() => {
336                 uiController.uiScriptComplete(JSON.stringify(uiController.inputViewBounds));
337             })()`, jsonString => {
338                 resolve(JSON.parse(jsonString));
339             });
340         });
341     }
342
343     static setViewScale(scale)
344     {
345         if (!this.isWebKit2())
346             return Promise.resolve();
347
348         return new Promise(resolve => testRunner.runUIScript(`uiController.setViewScale(${scale})`, resolve));
349     }
350 }