41579b1ca96bd17f6209bba9b0fde1ec3e861301
[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 activateAndWaitForInputSessionAt(x, y)
88     {
89         if (!this.isWebKit2() || !this.isIOS())
90             return this.activateAt(x, y);
91
92         return new Promise(resolve => {
93             testRunner.runUIScript(`
94                 (function() {
95                     uiController.didShowKeyboardCallback = function() {
96                         uiController.uiScriptComplete("Done");
97                     };
98                     uiController.singleTapAtPoint(${x}, ${y}, function() { });
99                 })()`, resolve);
100         });
101     }
102
103     static waitForKeyboardToHide()
104     {
105         return new Promise(resolve => {
106             testRunner.runUIScript(`
107                 (function() {
108                     uiController.didHideKeyboardCallback = () => uiController.uiScriptComplete();
109                 })()`, resolve);
110         });
111     }
112
113     static getUICaretRect()
114     {
115         if (!this.isWebKit2() || !this.isIOS())
116             return Promise.resolve();
117
118         return new Promise(resolve => {
119             testRunner.runUIScript(`(function() {
120                 uiController.doAfterNextStablePresentationUpdate(function() {
121                     uiController.uiScriptComplete(JSON.stringify(uiController.textSelectionCaretRect));
122                 });
123             })()`, jsonString => {
124                 resolve(JSON.parse(jsonString));
125             });
126         });
127     }
128
129     static getUISelectionRects()
130     {
131         if (!this.isWebKit2() || !this.isIOS())
132             return Promise.resolve();
133
134         return new Promise(resolve => {
135             testRunner.runUIScript(`(function() {
136                 uiController.doAfterNextStablePresentationUpdate(function() {
137                     uiController.uiScriptComplete(JSON.stringify(uiController.selectionRangeViewRects));
138                 });
139             })()`, jsonString => {
140                 resolve(JSON.parse(jsonString));
141             });
142         });
143     }
144
145     static replaceTextAtRange(text, location, length) {
146         return new Promise(resolve => {
147             testRunner.runUIScript(`(() => {
148                 uiController.replaceTextAtRange("${text}", ${location}, ${length});
149                 uiController.uiScriptComplete('Done');
150             })()`, resolve);
151         });
152     }
153
154     static wait(promise)
155     {
156         testRunner.waitUntilDone();
157         if (window.finishJSTest)
158             window.jsTestIsAsync = true;
159
160         let finish = () => {
161             if (window.finishJSTest)
162                 finishJSTest();
163             else
164                 testRunner.notifyDone();
165         }
166
167         return promise.then(finish, finish);
168     }
169
170     static withUserGesture(callback)
171     {
172         internals.withUserGesture(callback);
173     }
174
175     static selectFormAccessoryPickerRow(rowIndex)
176     {
177         const selectRowScript = `(() => uiController.selectFormAccessoryPickerRow(${rowIndex}))()`;
178         return new Promise(resolve => testRunner.runUIScript(selectRowScript, resolve));
179     }
180
181     static selectFormPopoverTitle()
182     {
183         return new Promise(resolve => {
184             testRunner.runUIScript(`(() => {
185                 uiController.uiScriptComplete(uiController.selectFormPopoverTitle);
186             })()`, resolve);
187         });
188     }
189
190     static enterText(text)
191     {
192         const escapedText = text.replace(/`/g, "\\`");
193         const enterTextScript = `(() => uiController.enterText(\`${escapedText}\`))()`;
194         return new Promise(resolve => testRunner.runUIScript(enterTextScript, resolve));
195     }
196
197     static setTimePickerValue(hours, minutes)
198     {
199         const setValueScript = `(() => uiController.setTimePickerValue(${hours}, ${minutes}))()`;
200         return new Promise(resolve => testRunner.runUIScript(setValueScript, resolve));
201     }
202
203     static formInputLabel()
204     {
205         return new Promise(resolve => {
206             testRunner.runUIScript(`(() => {
207                 uiController.uiScriptComplete(uiController.formInputLabel);
208             })()`, resolve);
209         });
210     }
211
212     static zoomScale()
213     {
214         return new Promise(resolve => {
215             testRunner.runUIScript(`(() => {
216                 uiController.uiScriptComplete(uiController.zoomScale);
217             })()`, resolve);
218         });
219     }
220 }