- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / test / data / extensions / api_test / wallpaper_manager / test.js
1 // Copyright (c) 2012 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 // wallpaperPrivate api test
6 // browser_tests --gtest_filter=ExtensionApiTest.wallpaperPrivate
7
8 var pass = chrome.test.callbackPass;
9 var fail = chrome.test.callbackFail;
10
11 chrome.test.getConfig(function(config) {
12   var wallpaperJpeg;
13   var wallpaperPng;
14   var wallpaperStrings;
15   var requestImage = function(url, onLoadCallback) {
16     var wallpaperRequest = new XMLHttpRequest();
17     wallpaperRequest.open('GET', url, true);
18     wallpaperRequest.responseType = 'arraybuffer';
19     try {
20       wallpaperRequest.send(null);
21       wallpaperRequest.onloadend = function(e) {
22         onLoadCallback(wallpaperRequest.status, wallpaperRequest.response);
23       };
24     } catch (e) {
25       console.error(e);
26       chrome.test.fail('An error thrown when requesting wallpaper.');
27     };
28   };
29   chrome.test.runTests([
30     function getWallpaperStrings() {
31       chrome.wallpaperPrivate.getStrings(pass(function(strings) {
32         wallpaperStrings = strings;
33       }));
34     },
35     function setOnlineJpegWallpaper() {
36       var url = "http://a.com:PORT/extensions/api_test" +
37           "/wallpaper_manager/test.jpg";
38       url = url.replace(/PORT/, config.testServer.port);
39       requestImage(url, function(requestStatus, response) {
40         if (requestStatus === 200) {
41           wallpaperJpeg = response;
42           chrome.wallpaperPrivate.setWallpaper(wallpaperJpeg,
43                                                'CENTER_CROPPED',
44                                                url,
45                                                pass(function() {
46             chrome.wallpaperPrivate.setCustomWallpaperLayout('CENTER',
47                 fail('Only custom wallpaper can change layout.'));
48           }));
49         } else {
50           chrome.test.fail('Failed to load test.jpg from local server.');
51         }
52       });
53     },
54     function setCustomJpegWallpaper() {
55       chrome.wallpaperPrivate.setCustomWallpaper(wallpaperJpeg,
56                                                  'CENTER_CROPPED',
57                                                  true,
58                                                  '123',
59                                                  pass(function(thumbnail) {
60         chrome.wallpaperPrivate.setCustomWallpaperLayout('CENTER',
61                                                          pass(function() {
62           chrome.wallpaperPrivate.setCustomWallpaperLayout('STRETCH', pass());
63         }));
64       }));
65     },
66     function setCustomPngWallpaper() {
67       var url = "http://a.com:PORT/extensions/api_test" +
68           "/wallpaper_manager/test.png";
69       url = url.replace(/PORT/, config.testServer.port);
70       requestImage(url, function(requestStatus, response) {
71         if (requestStatus === 200) {
72           wallpaperPng = response;
73           chrome.wallpaperPrivate.setCustomWallpaper(wallpaperPng,
74                                                      'CENTER_CROPPED',
75                                                      true,
76                                                      '123',
77                                                      pass(function(thumbnail) {
78             chrome.wallpaperPrivate.setCustomWallpaperLayout('CENTER',
79                                                              pass(function() {
80               chrome.wallpaperPrivate.setCustomWallpaperLayout('STRETCH',
81                                                                pass());
82             }));
83           }));
84         } else {
85           chrome.test.fail('Failed to load test.png from local server.');
86         }
87       });
88     },
89     function setCustomJepgBadWallpaper() {
90       var url = "http://a.com:PORT/extensions/api_test" +
91           "/wallpaper_manager/test_bad.jpg";
92       url = url.replace(/PORT/, config.testServer.port);
93       requestImage(url, function(requestStatus, response) {
94         if (requestStatus === 200) {
95           var badWallpaper = response;
96           chrome.wallpaperPrivate.setCustomWallpaper(badWallpaper,
97               'CENTER_CROPPED', false, '123',
98               fail(wallpaperStrings.invalidWallpaper));
99         } else {
100           chrome.test.fail('Failed to load test_bad.jpg from local server.');
101         }
102       });
103     },
104     function setWallpaperFromFileSystem() {
105       var url = "http://a.com:PORT/extensions/api_test" +
106           "/wallpaper_manager/test.jpg";
107       url = url.replace(/PORT/, config.testServer.port);
108       chrome.wallpaperPrivate.setWallpaperIfExists(url, 'CENTER_CROPPED',
109                                                    pass(function(exists) {
110         chrome.test.assertTrue(exists);
111         chrome.wallpaperPrivate.setWallpaperIfExists(
112             'http://dummyurl/test1.jpg', 'CENTER_CROPPED',
113             fail('Failed to set wallpaper test1.jpg from file system.'));
114       }));
115     },
116     function getAndSetThumbnail() {
117       var url = "http://a.com:PORT/extensions/api_test" +
118           "/wallpaper_manager/test.jpg";
119       url = url.replace(/PORT/, config.testServer.port);
120       chrome.wallpaperPrivate.getThumbnail(url, 'ONLINE', pass(function(data) {
121         chrome.test.assertNoLastError();
122         if (data) {
123           chrome.test.fail('Thumbnail is not found. getThumbnail should not ' +
124                            'return any data.');
125         }
126         chrome.wallpaperPrivate.saveThumbnail(url, wallpaperJpeg,
127                                               pass(function() {
128           chrome.test.assertNoLastError();
129           chrome.wallpaperPrivate.getThumbnail(url, 'ONLINE',
130                                                pass(function(data) {
131             chrome.test.assertNoLastError();
132             // Thumbnail should already be saved to thumbnail directory.
133             chrome.test.assertEq(wallpaperJpeg, data);
134           }));
135         }));
136       }));
137     },
138     function getOfflineWallpaperList() {
139       chrome.wallpaperPrivate.getOfflineWallpaperList(pass(function(list) {
140         // We have previously saved test.jpg in wallpaper directory.
141         chrome.test.assertEq('test.jpg', list[0]);
142         // Saves the same wallpaper to wallpaper directory but name it as
143         // test1.jpg.
144         chrome.wallpaperPrivate.setWallpaper(wallpaperJpeg,
145                                              'CENTER_CROPPED',
146                                              'http://dummyurl/test1.jpg',
147                                              pass(function() {
148           chrome.wallpaperPrivate.getOfflineWallpaperList(pass(function(list) {
149             chrome.test.assertEq('test.jpg', list[0]);
150             chrome.test.assertEq('test1.jpg', list[1]);
151           }));
152         }));
153       }));
154     }
155   ]);
156 });