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.
8 * Class to manipulate the window in the remote extension.
10 * @param {string} extensionId ID of extension to be manipulated.
13 function RemoteCall(extensionId
) {
14 this.extensionId_
= extensionId
;
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
);
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
39 RemoteCall
.prototype.callRemoteTestUtil
=
40 function(func
, appId
, args
, opt_callback
) {
41 return RemoteCall
.isStepByStepEnabled().then(function(stepByStep
) {
44 return new Promise(function(onFulfilled
) {
45 console
.info('Executing: ' + func
+ ' on ' + appId
+ ' with args: ');
47 console
.info('Type step() to continue...');
48 window
.step = function() {
50 onFulfilled(stepByStep
);
53 }).then(function(stepByStep
) {
54 return new Promise(function(onFulfilled
) {
55 chrome
.runtime
.sendMessage(
64 console
.info('Returned value:');
65 console
.info(arguments
);
68 opt_callback
.apply(null, arguments
);
69 onFulfilled(arguments
[0]);
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)
88 return pending('Window with the prefix %s is not found.', windowIdPrefix
);
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(
104 // Returns false when the closing is failed.
108 return repeatUntil(function() {
109 return this.callRemoteTestUtil('getWindows', null, []).then(
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.',
118 // Window is not available. Closing is done successfully.
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
},
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
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(
164 [query
, opt_iframeQuery
]
165 ).then(function(elements
) {
166 if (elements
.length
> 0)
170 'Element %s (maybe in iframe %s) is not found.',
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
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(
191 [query
, opt_iframeQuery
]
192 ).then(function(elements
) {
193 if (elements
.length
> 0)
194 return pending('Elements %j is still exists.', elements
);
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
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
) {
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}
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
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
) {
255 for (var i
= 0; i
< Math
.min(files
.length
, expected
.length
); i
++) {
256 if (options
.ignoreFileSize
) {
260 if (options
.ignoreLastModifiedTime
) {
265 if (!chrome
.test
.checkDeepEq(expected
, files
)) {
266 return pending('waitForFiles: expected: %j actual %j.',
275 * Waits until the number of files in the file list is changed from the given
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
) {
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) {
297 return pending('The number of file is %d. Not changed.', lengthBefore
);
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
);
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(
333 if(path
!== expectedPath
)
334 return pending('Expected path is %s', expectedPath
);