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.
5 // wallpaperPrivate api test
6 // browser_tests --gtest_filter=ExtensionApiTest.WallpaperPrivateApiTest
8 var pass
= chrome
.test
.callbackPass
;
9 var fail
= chrome
.test
.callbackFail
;
11 chrome
.test
.getConfig(function(config
) {
15 var requestImage = function(url
, onLoadCallback
) {
16 var wallpaperRequest
= new XMLHttpRequest();
17 wallpaperRequest
.open('GET', url
, true);
18 wallpaperRequest
.responseType
= 'arraybuffer';
20 wallpaperRequest
.send(null);
21 wallpaperRequest
.onloadend = function(e
) {
22 onLoadCallback(wallpaperRequest
.status
, wallpaperRequest
.response
);
26 chrome
.test
.fail('An error thrown when requesting wallpaper.');
29 chrome
.test
.runTests([
30 function getWallpaperStrings() {
31 chrome
.wallpaperPrivate
.getStrings(pass(function(strings
) {
32 wallpaperStrings
= strings
;
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
,
46 chrome
.wallpaperPrivate
.setCustomWallpaperLayout('CENTER',
47 fail('Only custom wallpaper can change layout.'));
50 chrome
.test
.fail('Failed to load test.jpg from local server.');
54 function setCustomJpegWallpaper() {
55 chrome
.wallpaperPrivate
.setCustomWallpaper(wallpaperJpeg
,
59 pass(function(thumbnail
) {
60 chrome
.wallpaperPrivate
.setCustomWallpaperLayout('CENTER',
62 chrome
.wallpaperPrivate
.setCustomWallpaperLayout('STRETCH', pass());
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
,
77 pass(function(thumbnail
) {
78 chrome
.wallpaperPrivate
.setCustomWallpaperLayout('CENTER',
80 chrome
.wallpaperPrivate
.setCustomWallpaperLayout('STRETCH',
85 chrome
.test
.fail('Failed to load test.png from local server.');
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
));
100 chrome
.test
.fail('Failed to load test_bad.jpg from local server.');
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.'));
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();
123 chrome
.test
.fail('Thumbnail is not found. getThumbnail should not ' +
126 chrome
.wallpaperPrivate
.saveThumbnail(url
, wallpaperJpeg
,
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
);
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
144 chrome
.wallpaperPrivate
.setWallpaper(wallpaperJpeg
,
146 'http://dummyurl/test1.jpg',
148 chrome
.wallpaperPrivate
.getOfflineWallpaperList(pass(function(list
) {
150 chrome
.test
.assertEq('test.jpg', list
[0]);
151 chrome
.test
.assertEq('test1.jpg', list
[1]);