Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / LayoutTests / fast / forms / resources / picker-common.js
blob65dfb1692c94f6a7a706e7cbbe1adf81a5e6f92a
1 window.jsTestIsAsync = true;
3 var popupWindow = null;
5 var popupOpenCallback = null;
7 function popupOpenCallbackWrapper() {
8     popupWindow.removeEventListener("didOpenPicker", popupOpenCallbackWrapper);
9     setTimeout(popupOpenCallback, 0);
12 function waitUntilClosing(callback) {
13     setTimeout(callback, 1);
16 function sendKey(input, keyName, ctrlKey, altKey) {
17     var event = document.createEvent('KeyboardEvent');
18     event.initKeyboardEvent('keydown', true, true, document.defaultView, keyName, 0, ctrlKey, altKey);
19     input.dispatchEvent(event);
22 function rootWindow() {
23     var currentWindow = window;
24     while (currentWindow !== currentWindow.parent) {
25         currentWindow = currentWindow.parent;
26     }
27     return currentWindow;
30 // openPicker opens a picker UI for the following types:
31 // - menulist SELECT
32 // - INPUT color
33 // - INPUT date/datetime-local/month/week
35 // |callback| is called if we successfully open the picker UI. However it is
36 // called only for the following types:
37 // - menulist SELECT on Windows, Linux, and CrOS
38 // - INPUT color with DATALIST
39 // - INPUT date/datetime-local/month/week
40 function openPicker(element, callback, errorCallback) {
41     rootWindow().moveTo(window.screenX, window.screenY);
42     element.offsetTop; // Force to lay out
43     element.focus();
44     if (element.tagName === "SELECT") {
45         sendKey(element, "Down", false, true);
46     } else if (element.tagName === "INPUT") {
47         if (element.type === "color") {
48             element.focus();
49             eventSender.keyDown(" ");
50         } else {
51             sendKey(element, "Down", false, true);
52         }
53     }
54     popupWindow = window.internals.pagePopupWindow;
55     if (typeof callback === "function" && popupWindow)
56         setPopupOpenCallback(callback);
57     else if (typeof errorCallback === "function" && !popupWindow)
58         errorCallback();
61 function clickToOpenPicker(x, y, callback, errorCallback) {
62     rootWindow().moveTo(window.screenX, window.screenY);
63     eventSender.mouseMoveTo(x, y);
64     eventSender.mouseDown();
65     eventSender.mouseUp();
66     popupWindow = window.internals.pagePopupWindow;
67     if (typeof callback === "function" && popupWindow)
68         setPopupOpenCallback(callback);
69     else if (typeof errorCallback === "function" && !popupWindow)
70         errorCallback();
73 function setPopupOpenCallback(callback) {
74     console.assert(popupWindow);
75     popupOpenCallback = (function(callback) {
76         // We need to move the window to the top left of available space
77         // because the window will move back to (0, 0) when the
78         // ShellViewMsg_SetTestConfiguration IPC arrives.
79         rootWindow().moveTo(window.screenX, window.screenY);
80         callback();
81     }).bind(this, callback);
82     try {
83         popupWindow.addEventListener("didOpenPicker", popupOpenCallbackWrapper, false);
84     } catch(e) {
85         debug(e.name);
86     }