Commit 9c99a1d7 authored by bshe's avatar bshe Committed by Commit bot

Unit test for sync online wallpaper

BUG=416130

Review URL: https://codereview.chromium.org/618743003

Cr-Commit-Position: refs/heads/master@{#297542}
parent f72920ef
...@@ -7,6 +7,9 @@ ...@@ -7,6 +7,9 @@
#include "chrome/browser/apps/app_browsertest_util.h" #include "chrome/browser/apps/app_browsertest_util.h"
#include "chrome/browser/extensions/component_loader.h" #include "chrome/browser/extensions/component_loader.h"
#include "chrome/browser/extensions/extension_service.h" #include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/test/base/ui_test_utils.h"
#include "content/public/test/browser_test_utils.h"
#include "extensions/browser/extension_registry.h" #include "extensions/browser/extension_registry.h"
#include "extensions/test/extension_test_message_listener.h" #include "extensions/test/extension_test_message_listener.h"
#include "grit/browser_resources.h" #include "grit/browser_resources.h"
...@@ -68,3 +71,25 @@ IN_PROC_BROWSER_TEST_F(WallpaperManagerBrowserTest, StableLaunchApp) { ...@@ -68,3 +71,25 @@ IN_PROC_BROWSER_TEST_F(WallpaperManagerBrowserTest, StableLaunchApp) {
extensions::ScopedCurrentChannel channel(chrome::VersionInfo::CHANNEL_STABLE); extensions::ScopedCurrentChannel channel(chrome::VersionInfo::CHANNEL_STABLE);
VerifyWallpaperManagerLoaded(); VerifyWallpaperManagerLoaded();
} }
class WallpaperManagerJsTest : public InProcessBrowserTest {
public:
void RunTest(const base::FilePath& file) {
GURL url = ui_test_utils::GetTestUrl(
base::FilePath(
FILE_PATH_LITERAL("chromeos/wallpaper_manager/unit_tests")),
file);
ui_test_utils::NavigateToURL(browser(), url);
content::WebContents* web_contents =
browser()->tab_strip_model()->GetActiveWebContents();
ASSERT_TRUE(web_contents);
const std::vector<int> empty_libraries;
EXPECT_TRUE(ExecuteWebUIResourceTest(web_contents, empty_libraries));
}
};
IN_PROC_BROWSER_TEST_F(WallpaperManagerJsTest, EventPageTest) {
RunTest(base::FilePath(FILE_PATH_LITERAL("event_page_unittest.html")));
}
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
var TestConstants = {
wallpaperURL: 'https://test.com/test.jpg',
// A dummy string which is used to mock an image.
image: '*#*@#&'
};
// Mock a few chrome apis.
var chrome = {
storage: {
local: {
get: function(key, callback) {
var items = {};
switch (key) {
case Constants.AccessLocalWallpaperInfoKey:
items[Constants.AccessLocalWallpaperInfoKey] = {
'url': 'dummy',
'layout': 'dummy',
'source': 'dummy'
};
}
callback(items);
},
set: function(items, callback) {
}
},
sync: {
get: function(key, callback) {
},
set: function(items, callback) {
}
},
onChanged: {
addListener: function(listener) {
this.dispatch = listener;
}
}
},
app: {
runtime: {
onLaunched: {
addListener: function(listener) {
}
}
}
},
alarms: {
onAlarm: {
addListener: function(listener) {
}
}
}
};
(function (exports) {
var originalXMLHttpRequest = window.XMLHttpRequest;
// Mock XMLHttpRequest. It dispatches a 'load' event immediately with status
// equals to 200 in function |send|.
function MockXMLHttpRequest() {
}
MockXMLHttpRequest.prototype = {
responseType: null,
url: null,
send: function(data) {
this.status = 200;
this.dispatchEvent('load');
},
addEventListener: function(type, listener) {
this.eventListeners = this.eventListeners || {};
this.eventListeners[type] = this.eventListeners[type] || [];
this.eventListeners[type].push(listener);
},
removeEventListener: function (type, listener) {
var listeners = this.eventListeners && this.eventListeners[type] || [];
for (var i = 0; i < listeners.length; ++i) {
if (listeners[i] == listener) {
return listeners.splice(i, 1);
}
}
},
dispatchEvent: function(type) {
var listeners = this.eventListeners && this.eventListeners[type] || [];
if (/test.jpg$/g.test(this.url)) {
this.response = TestConstants.image;
} else {
this.response = '';
}
for (var i = 0; i < listeners.length; ++i)
listeners[i].call(this, new Event(type));
},
open: function(method, url, async) {
this.url = url;
}
};
function installMockXMLHttpRequest() {
window['XMLHttpRequest'] = MockXMLHttpRequest;
};
function uninstallMockXMLHttpRequest() {
window['XMLHttpRequest'] = originalXMLHttpRequest;
};
exports.installMockXMLHttpRequest = installMockXMLHttpRequest;
exports.uninstallMockXMLHttpRequest = uninstallMockXMLHttpRequest;
})(window);
<!DOCTYPE html>
<!-- Copyright 2014 The Chromium Authors. All rights reserved.
-- Use of this source code is governed by a BSD-style license that can be
-- found in the LICENSE file.
-->
<html>
<body>
<script src="../../../webui/mock_controller.js"></script>
<script src="api_mock.js"></script>
<script src="../../../../../../chrome/browser/resources/chromeos/wallpaper_manager/js/constants.js"></script>
<script src="../../../../../../chrome/browser/resources/chromeos/wallpaper_manager/js/util.js"></script>
<script src="../../../../../../chrome/browser/resources/chromeos/wallpaper_manager/js/event_page.js"></script>
<script src="event_page_unittest.js"></script>
</body>
</html>
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
'use strict';
var mockController;
function setUp() {
mockController = new MockController();
installMockXMLHttpRequest();
}
function tearDown() {
mockController.verifyMocks();
mockController.reset();
uninstallMockXMLHttpRequest();
}
// Test sync online wallpaper. When the synced wallpaper info is not the same as
// the local wallpaper info, wallpaper should set to the synced one.
function testSyncOnlineWallpaper() {
// Setup sync value.
var changes = {};
changes[Constants.AccessSyncWallpaperInfoKey] = {};
changes[Constants.AccessSyncWallpaperInfoKey].newValue = {
'url': TestConstants.wallpaperURL,
'layout': 'custom',
'source': Constants.WallpaperSourceEnum.Online
};
chrome.wallpaperPrivate = {};
var mockSetWallpaperIfExists = mockController.createFunctionMock(
chrome.wallpaperPrivate, 'setWallpaperIfExists');
mockSetWallpaperIfExists.addExpectation(
changes[Constants.AccessSyncWallpaperInfoKey].newValue.url,
changes[Constants.AccessSyncWallpaperInfoKey].newValue.layout);
mockSetWallpaperIfExists.callbackData = [false];
var mockSetWallpaper = mockController.createFunctionMock(
chrome.wallpaperPrivate, 'setWallpaper');
mockSetWallpaper.addExpectation(
TestConstants.image,
changes[Constants.AccessSyncWallpaperInfoKey].newValue.layout,
changes[Constants.AccessSyncWallpaperInfoKey].newValue.url);
chrome.storage.onChanged.dispatch(changes);
}
...@@ -10,8 +10,20 @@ ...@@ -10,8 +10,20 @@
function MockMethod() { function MockMethod() {
var fn = function() { var fn = function() {
var args = Array.prototype.slice.call(arguments); var args = Array.prototype.slice.call(arguments);
var callbacks =
args.filter(function(arg) { return (typeof arg == 'function'); });
if (callbacks.length > 1) {
console.error('Only support mocking function with at most one callback.');
return;
}
fn.recordCall(args); fn.recordCall(args);
return this.returnValue; if (callbacks.length == 1) {
callbacks[0].apply(undefined, fn.callbackData);
return;
}
return fn.returnValue;
}; };
/** /**
...@@ -34,6 +46,12 @@ function MockMethod() { ...@@ -34,6 +46,12 @@ function MockMethod() {
*/ */
fn.returnValue = undefined; fn.returnValue = undefined;
/**
* List of arguments for callback function.
* @type {!Array.<!Array>}
*/
fn.callbackData = [];
fn.__proto__ = MockMethod.prototype; fn.__proto__ = MockMethod.prototype;
return fn; return fn;
} }
...@@ -45,7 +63,7 @@ MockMethod.prototype = { ...@@ -45,7 +63,7 @@ MockMethod.prototype = {
*/ */
addExpectation: function() { addExpectation: function() {
var args = Array.prototype.slice.call(arguments); var args = Array.prototype.slice.call(arguments);
this.expectations_.push(args); this.expectations_.push(args.filter(this.notFunction_));
}, },
/** /**
...@@ -53,7 +71,7 @@ MockMethod.prototype = { ...@@ -53,7 +71,7 @@ MockMethod.prototype = {
* @param {!Array} args. * @param {!Array} args.
*/ */
recordCall: function(args) { recordCall: function(args) {
this.calls_.push(args); this.calls_.push(args.filter(this.notFunction_));
}, },
/** /**
...@@ -84,6 +102,15 @@ MockMethod.prototype = { ...@@ -84,6 +102,15 @@ MockMethod.prototype = {
validateCall: function(index, expected, observed) { validateCall: function(index, expected, observed) {
assertDeepEquals(expected, observed); assertDeepEquals(expected, observed);
}, },
/**
* Test if arg is a function.
* @param {*} arg The argument to test.
* @return True if arg is not function type.
*/
notFunction_: function(arg) {
return typeof arg != 'function';
}
}; };
/** /**
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment