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