f81f6b692168e7cc77ec1995949cbc083789f801
[platform/framework/web/crosswalk.git] / src / chrome / test / data / extensions / api_test / file_manager_browsertest / gallery / test_util.js
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.
4
5 'use strict';
6
7 /**
8  * Gets file entries just under the volume.
9  *
10  * @param {VolumeManagerCommon.VolumeType} volumeType Volume type.
11  * @param {Array.<string>} names File name list.
12  * @return {Promise} Promise to be fulflled with Array.<FileEntry>.
13  */
14 function getFilesUnderVolume(volumeType, names) {
15   var displayRootPromise = backgroundComponentsPromise.then(
16       function(backgroundComponent) {
17         var volumeManager = backgroundComponent.volumeManager;
18         var volumeInfo = volumeManager.getCurrentProfileVolumeInfo(volumeType);
19         return new Promise(function(fulfill, reject) {
20           volumeInfo.resolveDisplayRoot(fulfill, reject);
21         });
22       });
23   return displayRootPromise.then(function(displayRoot) {
24     var filesPromise = names.map(function(name) {
25       return new Promise(
26           displayRoot.getFile.bind(displayRoot, name, {}));
27     });
28     return Promise.all(filesPromise);
29   });
30 }
31
32 /**
33  * Waits until an element appears and returns it.
34  *
35  * @param {AppWindow} appWindow Application window.
36  * @param {string} query Query for the element.
37  * @return {Promise} Promise to be fulfilled with the element.
38  */
39 function waitForElement(appWindow, query) {
40   return repeatUntil(function() {
41     var element = appWindow.contentWindow.document.querySelector(query);
42     if (element)
43       return element;
44     else
45       return pending('The element %s is not found.', query);
46   });
47 }
48
49 /**
50  * Waits until an element disappears.
51  *
52  * @param {AppWindow} appWindow Application window.
53  * @param {string} query Query for the element.
54  * @return {Promise} Promise to be fulfilled with the element.
55  */
56 function waitForElementLost(appWindow, query) {
57   return repeatUntil(function() {
58     var element = appWindow.contentWindow.document.querySelector(query);
59     if (element)
60       return pending('The element %s does not disappear.', query);
61     else
62       return true;
63   });
64 }
65
66 /**
67  * Launches the Gallery app with the test entries.
68  *
69  * @param {string} testVolumeName Test volume name passed to the addEntries
70  *     function. Either 'drive' or 'local'.
71  * @param {VolumeManagerCommon.VolumeType} volumeType Volume type.
72  * @param {Array.<TestEntryInfo>} entries Entries to be parepared and passed to
73  *     the application.
74  * @param {Array.<TestEntryInfo>=} opt_selected Entries to be selected. Should
75  *     be a sub-set of the entries argument.
76  */
77 function launchWithTestEntries(
78     testVolumeName, volumeType, entries, opt_selected) {
79   var entriesPromise = addEntries([testVolumeName], entries).then(function() {
80     var selectedEntries = opt_selected || entries;
81     return getFilesUnderVolume(
82         volumeType,
83         selectedEntries.map(function(entry) { return entry.nameText; }));
84   });
85   return launch(entriesPromise).then(function() {
86     var launchedPromise = Promise.all([appWindowPromise, entriesPromise]);
87     return launchedPromise.then(function(results) {
88       return {appWindow: results[0], entries: results[1]};
89     });
90   });
91 }
92
93 /**
94  * Waits until the expected image is shown.
95  *
96  * @param {document} document Document.
97  * @param {number} width Expected width of the image.
98  * @param {number} height Expected height of the image.
99  * @param {string} name Expected name of the image.
100  * @return {Promise} Promsie to be fulfilled when the check is passed.
101  */
102 function waitForSlideImage(document, width, height, name) {
103   var expected = {width: width, height: height, name: name};
104   return repeatUntil(function() {
105     var fullResCanvas = document.querySelector(
106         '.gallery[mode="slide"] .content canvas.fullres');
107     var nameBox = document.querySelector('.namebox');
108     var actual = {
109       width: fullResCanvas && fullResCanvas.width,
110       height: fullResCanvas && fullResCanvas.height,
111       name: nameBox && nameBox.value
112     };
113     if (!chrome.test.checkDeepEq(expected, actual)) {
114       return pending('Slide mode state, expected is %j, actual is %j.',
115                      expected, actual);
116     }
117     return actual;
118   });
119 }
120
121 /**
122  * Shorthand for clicking an element.
123  * @param {AppWindow} appWindow Application window.
124  * @param {string} query Query for the element.
125  * @param {Promise} Promise to be fulfilled with the clicked element.
126  */
127 function waitAndClickElement(appWindow, query) {
128   return waitForElement(appWindow, query).then(function(element) {
129     element.click();
130     return element;
131   });
132 }
133
134 /**
135  * Sends a fake key down event.
136  *
137  * @param {AppWindow} appWindow Application window.
138  * @param {string} query Query for the element to be dispatched an event to.
139  * @param {string} keyIdentifier Key identifier.
140  * @return {boolean} True on success.
141  */
142 function sendKeyDown(appWindow, query, keyIdentifier) {
143   return appWindow.contentWindow.document.querySelector(query).dispatchEvent(
144       new KeyboardEvent(
145           'keydown',
146           {bubbles: true, keyIdentifier: keyIdentifier}));
147 }