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 * Gets file entries just under the volume.
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>.
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);
23 return displayRootPromise.then(function(displayRoot) {
24 var filesPromise = names.map(function(name) {
26 displayRoot.getFile.bind(displayRoot, name, {}));
28 return Promise.all(filesPromise);
33 * Waits until an element appears and returns it.
35 * @param {AppWindow} appWindow Application window.
36 * @param {string} query Query for the element.
37 * @return {Promise} Promise to be fulfilled with the element.
39 function waitForElement(appWindow, query) {
40 return repeatUntil(function() {
41 var element = appWindow.contentWindow.document.querySelector(query);
45 return pending('The element %s is not found.', query);
50 * Waits until an element disappears.
52 * @param {AppWindow} appWindow Application window.
53 * @param {string} query Query for the element.
54 * @return {Promise} Promise to be fulfilled with the element.
56 function waitForElementLost(appWindow, query) {
57 return repeatUntil(function() {
58 var element = appWindow.contentWindow.document.querySelector(query);
60 return pending('The element %s does not disappear.', query);
67 * Launches the Gallery app with the test entries.
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
74 * @param {Array.<TestEntryInfo>=} opt_selected Entries to be selected. Should
75 * be a sub-set of the entries argument.
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(
83 selectedEntries.map(function(entry) { return entry.nameText; }));
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]};
94 * Waits until the expected image is shown.
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.
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');
109 width: fullResCanvas && fullResCanvas.width,
110 height: fullResCanvas && fullResCanvas.height,
111 name: nameBox && nameBox.value
113 if (!chrome.test.checkDeepEq(expected, actual)) {
114 return pending('Slide mode state, expected is %j, actual is %j.',
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.
127 function waitAndClickElement(appWindow, query) {
128 return waitForElement(appWindow, query).then(function(element) {
135 * Sends a fake key down event.
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.
142 function sendKeyDown(appWindow, query, keyIdentifier) {
143 return appWindow.contentWindow.document.querySelector(query).dispatchEvent(
146 {bubbles: true, keyIdentifier: keyIdentifier}));