Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / chrome / test / data / extensions / api_test / file_manager_browsertest / gallery / photo_editor.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  * Waits for the "Press Enter" message.
9  *
10  * @param {AppWindow} appWindow App window.
11  * @return {Promise} Promise to be fulfilled when the element appears.
12  */
13 function waitForPressEnterMessage(appWindow) {
14   return waitForElement(appWindow, '.prompt-wrapper .prompt').
15       then(function(element) {
16         chrome.test.assertEq(
17             'Press Enter when done', element.innerText.trim());
18       });
19 }
20
21 /**
22  * Prepares the photo editor.
23  *
24  * @param {string} testVolumeName Test volume name passed to the addEntries
25  *     function. Either 'drive' or 'local'.
26  * @param {VolumeManagerCommon.VolumeType} volumeType Volume type.
27  * @return {Promise} Promise to be fulfilled with on success.
28  */
29 function setupPhotoEditor(testVolumeName, volumeType) {
30   // Lauch the gallery.
31   observeWindowError(window);
32   var launchedPromise = launchWithTestEntries(
33       testVolumeName,
34       volumeType,
35       [ENTRIES.desktop]);
36   return launchedPromise.then(function(args) {
37     var appWindow = args.appWindow;
38     observeWindowError(appWindow.contentWindow);
39
40     // Show the slide image.
41     var slideImagePromise = waitForSlideImage(
42         appWindow.contentWindow.document,
43         800,
44         600,
45         'My Desktop Background');
46
47     // Lauch the photo editor.
48     var photoEditorPromise = slideImagePromise.then(function() {
49       return waitAndClickElement(
50           appWindow, 'button.edit');
51     });
52
53     return photoEditorPromise.then(function() {
54       return args;
55     });
56   });
57 }
58
59 /**
60  * Tests to rotate an image.
61  *
62  * @param {string} testVolumeName Test volume name passed to the addEntries
63  *     function. Either 'drive' or 'local'.
64  * @param {VolumeManagerCommon.VolumeType} volumeType Volume type.
65  * @return {Promise} Promise to be fulfilled with on success.
66  */
67 function rotateImage(testVolumeName, volumeType) {
68   var launchedPromise = setupPhotoEditor(testVolumeName, volumeType);
69   return launchedPromise.then(function(args) {
70     var appWindow = args.appWindow;
71     return waitAndClickElement(
72         appWindow, '.gallery:not([locked]) button.rotate_right').
73         then(function() {
74           return waitForSlideImage(
75               appWindow.contentWindow.document,
76               600,
77               800,
78               'My Desktop Background');
79         }).
80         then(function() {
81           return waitAndClickElement(
82               appWindow, '.gallery:not([locked]) button.rotate_left');
83         }).
84         then(function() {
85           return waitForSlideImage(
86               appWindow.contentWindow.document,
87               800,
88               600,
89               'My Desktop Background');
90         });
91   });
92 }
93
94 /**
95  * Tests to crop an image and undoes it.
96  *
97  * @param {string} testVolumeName Test volume name passed to the addEntries
98  *     function. Either 'drive' or 'local'.
99  * @param {VolumeManagerCommon.VolumeType} volumeType Volume type.
100  * @return {Promise} Promise to be fulfilled with on success.
101  */
102 function cropImage(testVolumeName, volumeType) {
103   var launchedPromise = setupPhotoEditor(testVolumeName, volumeType);
104   return launchedPromise.then(function(args) {
105     var appWindow = args.appWindow;
106     return waitAndClickElement(appWindow, '.gallery:not([locked]) button.crop').
107         then(function() {
108           return Promise.all([
109             waitForPressEnterMessage(appWindow),
110             waitForElement(appWindow, '.crop-overlay')
111           ]);
112         }).
113         then(function() {
114           chrome.test.assertTrue(sendKeyDown(appWindow, 'body', 'Enter'));
115         }).
116         then(function() {
117           return Promise.all([
118             waitForElementLost(appWindow, '.prompt-wrapper .prompt'),
119             waitForElementLost(appWindow, '.crop-overlay')
120           ]);
121         }).
122         then(function() {
123           return waitForSlideImage(
124               appWindow.contentWindow.document,
125               533,
126               400,
127               'My Desktop Background');
128         }).
129         then(function() {
130           return waitAndClickElement(
131               appWindow, '.gallery:not([locked]) button.undo');
132         }).
133         then(function() {
134           return waitForSlideImage(
135               appWindow.contentWindow.document,
136               800,
137               600,
138               'My Desktop Background');
139         });
140   });
141 }
142
143 /**
144  * Obtains metadata from an entry.
145  *
146  * @param {Entry} entry Entry.
147  * @return {Promise} Promise to be fulfilled with the result metadata.
148  */
149 function getMetadata(entry) {
150   return new Promise(entry.getMetadata.bind(entry));
151 }
152
153 /**
154  * Tests to exposure an image.
155  *
156  * @param {string} testVolumeName Test volume name passed to the addEntries
157  *     function. Either 'drive' or 'local'.
158  * @param {VolumeManagerCommon.VolumeType} volumeType Volume type.
159  * @return {Promise} Promise to be fulfilled with on success.
160  */
161 function exposureImage(testVolumeName, volumeType) {
162   var launchedPromise = setupPhotoEditor(testVolumeName, volumeType);
163   return launchedPromise.then(function(args) {
164     var appWindow = args.appWindow;
165     var entry = args.entries[0];
166     var buttonQuery = '.gallery:not([locked]) button.exposure';
167
168     // Click the exposure button.
169     return waitAndClickElement(appWindow, buttonQuery).then(function() {
170       // Wait until the edit controls appear.
171       return Promise.all([
172         waitForPressEnterMessage(appWindow),
173         waitForElement(appWindow, 'input.range[name="brightness"]'),
174         waitForElement(appWindow, 'input.range[name="contrast"]'),
175       ]);
176     }).then(function(results) {
177       // Update bright.
178       var brightnessRange = results[1];
179       brightnessRange.value = 20;
180       chrome.test.assertTrue(
181           brightnessRange.dispatchEvent(new Event('change')));
182
183       // Update contrast.
184       var contrastRange = results[2];
185       contrastRange.value = -20;
186       chrome.test.assertTrue(
187           contrastRange.dispatchEvent(new Event('change')));
188
189       return getMetadata(entry).then(function(firstMetadata) {
190         // Push the Enter key.
191         chrome.test.assertTrue(sendKeyDown(appWindow, 'body', 'Enter'));
192
193         // Wait until the image is updated.
194         return repeatUntil(function() {
195           return getMetadata(entry).then(function(secondMetadata) {
196             if (firstMetadata.modificationTime !=
197                 secondMetadata.modificationTime) {
198               return true;
199             } else {
200               return pending(
201                   '%s is not updated. ' +
202                       'First last modified: %s, Second last modified: %s.',
203                   entry.name,
204                   firstMetadata.modificationTime.toString(),
205                   secondMetadata.modificationTime.toString());
206             }
207           });
208         });
209       });
210     });
211   });
212 }
213
214 /**
215  * The rotateImage test for Downloads.
216  * @return {Promise} Promise to be fulfilled with on success.
217  */
218 function rotateImageOnDownloads() {
219   return rotateImage('local', VolumeManagerCommon.VolumeType.DOWNLOADS);
220 }
221
222 /**
223  * The rotateImage test for Google Drive.
224  * @return {Promise} Promise to be fulfilled with on success.
225  */
226 function rotateImageOnDrive() {
227   return rotateImage('drive', VolumeManagerCommon.VolumeType.DRIVE);
228 }
229
230 /**
231  * The cropImage test for Downloads.
232  * @return {Promise} Promise to be fulfilled with on success.
233  */
234 function cropImageOnDownloads() {
235   return cropImage('local', VolumeManagerCommon.VolumeType.DOWNLOADS);
236 }
237
238 /**
239  * The cropImage test for Google Drive.
240  * @return {Promise} Promise to be fulfilled with on success.
241  */
242 function cropImageOnDrive() {
243   return cropImage('drive', VolumeManagerCommon.VolumeType.DRIVE);
244 }
245
246 /**
247  * The exposureImage test for Downloads.
248  * @return {Promise} Promise to be fulfilled with on success.
249  */
250 function exposureImageOnDownloads() {
251   return exposureImage('local', VolumeManagerCommon.VolumeType.DOWNLOADS);
252 }
253
254 /**
255  * The exposureImage test for Google Drive.
256  * @return {Promise} Promise to be fulfilled with on success.
257  */
258 function exposureImageOnDrive() {
259   return exposureImage('drive', VolumeManagerCommon.VolumeType.DRIVE);
260 }