[ews-build] Add support for JSC-Tests-EWS
[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 waitForKeyboardToHide()
115     {
116         return new Promise(resolve => {
117             testRunner.runUIScript(`
118                 (function() {
119                     uiController.didHideKeyboardCallback = () => uiController.uiScriptComplete();
120                 })()`, resolve);
121         });
122     }
123
124     static getUICaretRect()
125     {
126         if (!this.isWebKit2() || !this.isIOS())
127             return Promise.resolve();
128
129         return new Promise(resolve => {
130             testRunner.runUIScript(`(function() {
131                 uiController.doAfterNextStablePresentationUpdate(function() {
132                     uiController.uiScriptComplete(JSON.stringify(uiController.textSelectionCaretRect));
133                 });
134             })()`, jsonString => {
135                 resolve(JSON.parse(jsonString));
136             });
137         });
138     }
139
140     static getUISelectionRects()
141     {
142         if (!this.isWebKit2() || !this.isIOS())
143             return Promise.resolve();
144
145         return new Promise(resolve => {
146             testRunner.runUIScript(`(function() {
147                 uiController.doAfterNextStablePresentationUpdate(function() {
148                     uiController.uiScriptComplete(JSON.stringify(uiController.selectionRangeViewRects));
149                 });
150             })()`, jsonString => {
151                 resolve(JSON.parse(jsonString));
152             });
153         });
154     }
155
156     static replaceTextAtRange(text, location, length) {
157         return new Promise(resolve => {
158             testRunner.runUIScript(`(() => {
159                 uiController.replaceTextAtRange("${text}", ${location}, ${length});
160                 uiController.uiScriptComplete('Done');
161             })()`, resolve);
162         });
163     }
164
165     static wait(promise)
166     {
167         testRunner.waitUntilDone();
168         if (window.finishJSTest)
169             window.jsTestIsAsync = true;
170
171         let finish = () => {
172             if (window.finishJSTest)
173                 finishJSTest();
174             else
175                 testRunner.notifyDone();
176         }
177
178         return promise.then(finish, finish);
179     }
180
181     static withUserGesture(callback)
182     {
183         internals.withUserGesture(callback);
184     }
185
186     static selectFormAccessoryPickerRow(rowIndex)
187     {
188         const selectRowScript = `(() => uiController.selectFormAccessoryPickerRow(${rowIndex}))()`;
189         return new Promise(resolve => testRunner.runUIScript(selectRowScript, resolve));
190     }
191
192     static selectFormPopoverTitle()
193     {
194         return new Promise(resolve => {
195             testRunner.runUIScript(`(() => {
196                 uiController.uiScriptComplete(uiController.selectFormPopoverTitle);
197             })()`, resolve);
198         });
199     }
200
201     static enterText(text)
202     {
203         const escapedText = text.replace(/`/g, "\\`");
204         const enterTextScript = `(() => uiController.enterText(\`${escapedText}\`))()`;
205         return new Promise(resolve => testRunner.runUIScript(enterTextScript, resolve));
206     }
207
208     static setTimePickerValue(hours, minutes)
209     {
210         const setValueScript = `(() => uiController.setTimePickerValue(${hours}, ${minutes}))()`;
211         return new Promise(resolve => testRunner.runUIScript(setValueScript, resolve));
212     }
213
214     static textContentType()
215     {
216         return new Promise(resolve => {
217             testRunner.runUIScript(`(() => {
218                 uiController.uiScriptComplete(uiController.textContentType);
219             })()`, resolve);
220         });
221     }
222
223     static formInputLabel()
224     {
225         return new Promise(resolve => {
226             testRunner.runUIScript(`(() => {
227                 uiController.uiScriptComplete(uiController.formInputLabel);
228             })()`, resolve);
229         });
230     }
231
232     static zoomScale()
233     {
234         return new Promise(resolve => {
235             testRunner.runUIScript(`(() => {
236                 uiController.uiScriptComplete(uiController.zoomScale);
237             })()`, resolve);
238         });
239     }
240 }