ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / ui / file_manager / integration_tests / remote_call.js
blob07ac44300d9784b873ebb48ed368d3cdbd2a70fd
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 'use strict';
7 /**
8 * Class to manipulate the window in the remote extension.
10 * @param {string} extensionId ID of extension to be manipulated.
11 * @constructor
13 function RemoteCall(extensionId) {
14 this.extensionId_ = extensionId;
17 /**
18 * Checks whether step by step tests are enabled or not.
19 * @return {Promise<bool>}
21 RemoteCall.isStepByStepEnabled = function() {
22 return new Promise(function(fulfill) {
23 chrome.commandLinePrivate.hasSwitch(
24 'enable-file-manager-step-by-step-tests', fulfill);
25 });
28 /**
29 * Calls a remote test util in Files.app's extension. See: test_util.js.
31 * @param {string} func Function name.
32 * @param {?string} appId Target window's App ID or null for functions
33 * not requiring a window.
34 * @param {Array.<*>} args Array of arguments.
35 * @param {function(*)=} opt_callback Callback handling the function's result.
36 * @return {Promise} Promise to be fulfilled with the result of the remote
37 * utility.
39 RemoteCall.prototype.callRemoteTestUtil =
40 function(func, appId, args, opt_callback) {
41 return RemoteCall.isStepByStepEnabled().then(function(stepByStep) {
42 if (!stepByStep)
43 return false;
44 return new Promise(function(onFulfilled) {
45 console.info('Executing: ' + func + ' on ' + appId + ' with args: ');
46 console.info(args);
47 console.info('Type step() to continue...');
48 window.step = function() {
49 window.step = null;
50 onFulfilled(stepByStep);
52 });
53 }).then(function(stepByStep) {
54 return new Promise(function(onFulfilled) {
55 chrome.runtime.sendMessage(
56 this.extensionId_,
58 func: func,
59 appId: appId,
60 args: args
62 function(var_args) {
63 if (stepByStep) {
64 console.info('Returned value:');
65 console.info(arguments);
67 if (opt_callback)
68 opt_callback.apply(null, arguments);
69 onFulfilled(arguments[0]);
70 });
71 }.bind(this));
72 }.bind(this));
75 /**
76 * Waits until a window having the given ID prefix appears.
77 * @param {string} windowIdPrefix ID prefix of the requested window.
78 * @return {Promise} promise Promise to be fulfilled with a found window's ID.
80 RemoteCall.prototype.waitForWindow = function(windowIdPrefix) {
81 return repeatUntil(function() {
82 return this.callRemoteTestUtil('getWindows', null, []).
83 then(function(windows) {
84 for (var id in windows) {
85 if (id.indexOf(windowIdPrefix) === 0)
86 return id;
88 return pending('Window with the prefix %s is not found.', windowIdPrefix);
89 });
90 }.bind(this));
93 /**
94 * Closes a window and waits until the window is closed.
96 * @param {string} windowId ID of the window to close.
97 * @return {Promise} promise Promise to be fulfilled with the result (true:
98 * success, false: failed).
100 RemoteCall.prototype.closeWindowAndWait = function(windowId) {
101 // Closes the window.
102 return this.callRemoteTestUtil('closeWindow', null, [windowId]).then(
103 function(result) {
104 // Returns false when the closing is failed.
105 if (!result)
106 return false;
108 return repeatUntil(function() {
109 return this.callRemoteTestUtil('getWindows', null, []).then(
110 function(windows) {
111 for (var id in windows) {
112 if (id === windowId) {
113 // Window is still available. Continues waiting.
114 return pending('Window with the prefix %s is not found.',
115 windowId);
118 // Window is not available. Closing is done successfully.
119 return true;
122 }.bind(this));
123 }.bind(this)
128 * Waits until the window turns to the given size.
129 * @param {string} windowId Target window ID.
130 * @param {number} width Requested width in pixels.
131 * @param {number} height Requested height in pixels.
133 RemoteCall.prototype.waitForWindowGeometry =
134 function(windowId, width, height) {
135 return repeatUntil(function() {
136 return this.callRemoteTestUtil('getWindows', null, []).
137 then(function(windows) {
138 if (!windows[windowId])
139 return pending('Window %s is not found.', windowId);
140 if (windows[windowId].outerWidth !== width ||
141 windows[windowId].outerHeight !== height) {
142 return pending('Expected window size is %j, but it is %j',
143 {width: width, height: height},
144 windows[windowId]);
147 }.bind(this));
151 * Waits for the specified element appearing in the DOM.
152 * @param {string} windowId Target window ID.
153 * @param {string} query Query string for the element.
154 * @param {string=} opt_iframeQuery Query string for the iframe containing the
155 * element.
156 * @return {Promise} Promise to be fulfilled when the element appears.
158 RemoteCall.prototype.waitForElement =
159 function(windowId, query, opt_iframeQuery) {
160 return repeatUntil(function() {
161 return this.callRemoteTestUtil(
162 'queryAllElements',
163 windowId,
164 [query, opt_iframeQuery]
165 ).then(function(elements) {
166 if (elements.length > 0)
167 return elements[0];
168 else
169 return pending(
170 'Element %s (maybe in iframe %s) is not found.',
171 query,
172 opt_iframeQuery);
174 }.bind(this));
178 * Waits for the specified element leaving from the DOM.
179 * @param {string} windowId Target window ID.
180 * @param {string} query Query string for the element.
181 * @param {string=} opt_iframeQuery Query string for the iframe containing the
182 * element.
183 * @return {Promise} Promise to be fulfilled when the element is lost.
185 RemoteCall.prototype.waitForElementLost =
186 function(windowId, query, opt_iframeQuery) {
187 return repeatUntil(function() {
188 return this.callRemoteTestUtil(
189 'queryAllElements',
190 windowId,
191 [query, opt_iframeQuery]
192 ).then(function(elements) {
193 if (elements.length > 0)
194 return pending('Elements %j is still exists.', elements);
195 return true;
197 }.bind(this));
201 * Sends a fake key down event.
202 * @param {string} windowId Window ID.
203 * @param {string} query Query for the target element.
204 * @param {string} keyIdentifer Key identifier.
205 * @param {boolean} ctrlKey Control key flag.
206 * @return {Promise} Promise to be fulfilled or rejected depending on the
207 * result.
209 RemoteCall.prototype.fakeKeyDown =
210 function(windowId, query, keyIdentifer, ctrlKey) {
211 var resultPromise = this.callRemoteTestUtil(
212 'fakeKeyDown', windowId, [query, keyIdentifer, ctrlKey]);
213 return resultPromise.then(function(result) {
214 if (result)
215 return true;
216 else
217 return Promise.reject('Fail to fake key down.');
222 * Class to manipulate the window in the remote extension.
224 * @param {string} extensionId ID of extension to be manipulated.
225 * @extends {RemoteCall}
226 * @constructor
228 function RemoteCallFilesApp() {
229 RemoteCall.apply(this, arguments);
232 RemoteCallFilesApp.prototype.__proto__ = RemoteCall.prototype;
235 * Waits for the file list turns to the given contents.
236 * @param {string} windowId Target window ID.
237 * @param {Array.<Array.<string>>} expected Expected contents of file list.
238 * @param {{orderCheck:boolean=, ignoreLastModifiedTime:boolean=}=} opt_options
239 * Options of the comparison. If orderCheck is true, it also compares the
240 * order of files. If ignoreLastModifiedTime is true, it compares the file
241 * without its last modified time.
242 * @return {Promise} Promise to be fulfilled when the file list turns to the
243 * given contents.
245 RemoteCallFilesApp.prototype.waitForFiles =
246 function(windowId, expected, opt_options) {
247 var options = opt_options || {};
248 return repeatUntil(function() {
249 return this.callRemoteTestUtil(
250 'getFileList', windowId, []).then(function(files) {
251 if (!options.orderCheck) {
252 files.sort();
253 expected.sort();
255 for (var i = 0; i < Math.min(files.length, expected.length); i++) {
256 if (options.ignoreFileSize) {
257 files[i][1] = '';
258 expected[i][1] = '';
260 if (options.ignoreLastModifiedTime) {
261 files[i][3] = '';
262 expected[i][3] = '';
265 if (!chrome.test.checkDeepEq(expected, files)) {
266 return pending('waitForFiles: expected: %j actual %j.',
267 expected,
268 files);
271 }.bind(this));
275 * Waits until the number of files in the file list is changed from the given
276 * number.
277 * TODO(hirono): Remove the function.
279 * @param {string} windowId Target window ID.
280 * @param {number} lengthBefore Number of items visible before.
281 * @return {Promise} Promise to be fulfilled with the contents of files.
283 RemoteCallFilesApp.prototype.waitForFileListChange =
284 function(windowId, lengthBefore) {
285 return repeatUntil(function() {
286 return this.callRemoteTestUtil(
287 'getFileList', windowId, []).then(function(files) {
288 files.sort();
289 var notReadyRows = files.filter(function(row) {
290 return row.filter(function(cell) { return cell == '...'; }).length;
292 if (notReadyRows.length === 0 &&
293 files.length !== lengthBefore &&
294 files.length !== 0) {
295 return files;
296 } else {
297 return pending('The number of file is %d. Not changed.', lengthBefore);
300 }.bind(this));
304 * Waits until the given taskId appears in the executed task list.
305 * @param {string} windowId Target window ID.
306 * @param {string} taskId Task ID to watch.
307 * @return {Promise} Promise to be fulfilled when the task appears in the
308 * executed task list.
310 RemoteCallFilesApp.prototype.waitUntilTaskExecutes =
311 function(windowId, taskId) {
312 return repeatUntil(function() {
313 return this.callRemoteTestUtil('getExecutedTasks', windowId, []).
314 then(function(executedTasks) {
315 if (executedTasks.indexOf(taskId) === -1)
316 return pending('Executed task is %j', executedTasks);
318 }.bind(this));
322 * Waits until the current directory is changed.
323 * @param {string} windowId Target window ID.
324 * @param {string} expectedPath Path to be changed to.
325 * @return {Promise} Promise to be fulfilled when the current directory is
326 * changed to expectedPath.
328 RemoteCallFilesApp.prototype.waitUntilCurrentDirectoryIsChanged =
329 function(windowId, expectedPath) {
330 return repeatUntil(function () {
331 return this.callRemoteTestUtil('getBreadcrumbPath', windowId, []).then(
332 function(path) {
333 if(path !== expectedPath)
334 return pending('Expected path is %s', expectedPath);
336 }.bind(this));