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.
5 var pass = chrome.test.callbackPass;
6 var fail = chrome.test.callbackFail;
8 chrome.test.getConfig(function(config) {
10 var baseURL = "http://a.com:" + config.testServer.port +
11 "/extensions/api_test/wallpaper/";
14 * Calls chrome.wallpaper.setWallpaper using an arraybuffer.
15 * @param {string} filePath An extension relative file path.
17 var testSetWallpaperFromArrayBuffer = function (filePath) {
18 // Loads an extension local file to an arraybuffer.
19 var url = chrome.runtime.getURL(filePath);
20 var wallpaperRequest = new XMLHttpRequest();
21 wallpaperRequest.open('GET', url, true);
22 wallpaperRequest.responseType = 'arraybuffer';
24 wallpaperRequest.send(null);
25 wallpaperRequest.onloadend = function(e) {
26 if (wallpaperRequest.status === 200) {
27 chrome.wallpaper.setWallpaper(
28 {'data': wallpaperRequest.response,
29 'layout': 'CENTER_CROPPED',
31 // Set wallpaper directly with an arraybuffer should pass.
35 chrome.test.fail('Failed to load local file: ' + filePath + '.');
40 chrome.test.fail('An error thrown when requesting wallpaper.');
45 * Calls chrome.wallpaper.setWallpaper using an URL.
46 * @param {string} relativeURL The relative URL of an online image.
47 * @param {boolean} success True if expecting the API call success.
48 * @param {string=} optExpectedError The expected error string if API call
49 * failed. An error string must be provided if success is set to false.
51 var testSetWallpaperFromURL = function (relativeURL,
54 var url = baseURL + relativeURL;
56 chrome.wallpaper.setWallpaper(
58 'layout': 'CENTER_CROPPED',
60 // A valid url should set wallpaper correctly.
64 if (optExpectedError == undefined) {
65 chrome.test.fail('No expected error string is provided.');
68 chrome.wallpaper.setWallpaper(
70 'layout': 'CENTER_CROPPED',
73 fail(optExpectedError));
77 chrome.test.runTests([
78 function setJpgWallpaperFromAppLocalFile() {
79 testSetWallpaperFromArrayBuffer('test.jpg');
81 function setPngWallpaperFromAppLocalFile() {
82 testSetWallpaperFromArrayBuffer('test.png');
84 function setJpgWallpaperFromURL () {
85 testSetWallpaperFromURL('test.jpg', true);
87 function setPngWallpaperFromURL () {
88 testSetWallpaperFromURL('test.png', true);
90 function setNoExistingWallpaperFromURL () {
91 // test1.jpg doesn't exist. Expect a 404 error.
93 'Downloading wallpaper test1.jpg failed. The response code is 404.';
94 testSetWallpaperFromURL('test1.jpg',
98 function newRequestCancelPreviousRequest() {
99 // The first request should be canceled.
100 testSetWallpaperFromURL('test.png',
102 'Set wallpaper was canceled.');
103 testSetWallpaperFromURL('test.jpg', true);