Commit e9008c72 authored by vadimt@chromium.org's avatar vadimt@chromium.org

Unit tests for utility.js.

Adding tests for error reporting and wrapper, the rest will follow.

BUG=164227
TEST=No

Review URL: https://chromiumcodereview.appspot.com/23477006

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@221239 0039d316-1c4b-4281-b951-d872f2087c98
parent 437994a2
...@@ -4,8 +4,6 @@ ...@@ -4,8 +4,6 @@
// Mocks for globals needed for loading background.js. // Mocks for globals needed for loading background.js.
function emptyMock() {}
var wrapper = {instrumentChromeApiFunction: emptyMock}; var wrapper = {instrumentChromeApiFunction: emptyMock};
function buildTaskManager() { function buildTaskManager() {
...@@ -21,21 +19,14 @@ function buildAuthenticationManager() { ...@@ -21,21 +19,14 @@ function buildAuthenticationManager() {
var instrumentApiFunction = emptyMock; var instrumentApiFunction = emptyMock;
var buildAttemptManager = emptyMock; var buildAttemptManager = emptyMock;
var buildCardSet = emptyMock; var buildCardSet = emptyMock;
var emptyListener = {addListener: emptyMock};
var instrumented = {}; var instrumented = {};
instrumented['location'] = {onLocationUpdate: emptyListener}; mockChromeEvent(instrumented, 'location.onLocationUpdate');
instrumented['notifications'] = { mockChromeEvent(instrumented, 'notifications.onButtonClicked');
onButtonClicked: emptyListener, mockChromeEvent(instrumented, 'notifications.onClicked');
onClicked: emptyListener, mockChromeEvent(instrumented, 'notifications.onClosed');
onClosed: emptyListener mockChromeEvent(instrumented, 'omnibox.onInputEntered');
}; mockChromeEvent(
instrumented['omnibox'] = {onInputEntered: emptyListener}; instrumented, 'preferencesPrivate.googleGeolocationAccessEnabled.onChange');
instrumented['preferencesPrivate'] = { mockChromeEvent(instrumented, 'runtime.onInstalled');
googleGeolocationAccessEnabled: { mockChromeEvent(instrumented, 'runtime.onStartup');
onChange: emptyListener
}
};
instrumented['runtime'] = {
onInstalled: emptyListener,
onStartup: emptyListener
};
...@@ -16,6 +16,7 @@ GoogleNowBackgroundUnitTest.prototype = { ...@@ -16,6 +16,7 @@ GoogleNowBackgroundUnitTest.prototype = {
/** @override */ /** @override */
extraLibraries: [ extraLibraries: [
'common_test_util.js',
'background_test_util.js', 'background_test_util.js',
'background.js' 'background.js'
] ]
......
// Copyright 2013 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.
// Common test utilities.
function emptyMock() {}
// Container for event handlers added by mocked 'addListener' functions.
var mockEventHandlers = {};
/**
* Mocks 'addListener' function of an API event. The mocked function will keep
* track of handlers.
* @param {Object} topLevelContainer Top-level container of the original
* function. Can be either 'chrome' or 'instrumented'.
* @param {string} eventIdentifier Event identifier, such as
* 'runtime.onSuspend'.
*/
function mockChromeEvent(topLevelContainer, eventIdentifier) {
var eventIdentifierParts = eventIdentifier.split('.');
var eventName = eventIdentifierParts.pop();
var originalMethodContainer = topLevelContainer;
var mockEventContainer = mockEventHandlers;
eventIdentifierParts.forEach(function(fragment) {
originalMethodContainer =
originalMethodContainer[fragment] =
originalMethodContainer[fragment] || {};
mockEventContainer =
mockEventContainer[fragment] =
mockEventContainer[fragment] || {};
});
mockEventContainer[eventName] = [];
originalMethodContainer[eventName] = {
addListener: function(callback) {
mockEventContainer[eventName].push(callback);
}
};
}
/**
* Gets the array of event handlers added by a mocked 'addListener' function.
* @param {string} eventIdentifier Event identifier, such as
* 'runtime.onSuspend'.
* @return {Array.<Function>} Array of handlers.
*/
function getMockHandlerContainer(eventIdentifier) {
var eventIdentifierParts = eventIdentifier.split('.');
var mockEventContainer = mockEventHandlers;
eventIdentifierParts.forEach(function(fragment) {
mockEventContainer = mockEventContainer[fragment];
});
return mockEventContainer;
}
// Copyright 2013 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.
// Mocks for globals needed for loading utility.js.
var localStorage = {};
chrome['alarms'] = {
get: emptyMock
};
chrome['identity'] = {
getAuthToken: emptyMock,
removeCachedAuthToken: emptyMock
};
mockChromeEvent(chrome, 'alarms.onAlarm');
mockChromeEvent(chrome, 'runtime.onSuspend');
This diff is collapsed.
...@@ -1132,10 +1132,14 @@ ...@@ -1132,10 +1132,14 @@
'browser/renderer_host/plugin_info_message_filter_unittest.cc', 'browser/renderer_host/plugin_info_message_filter_unittest.cc',
'browser/renderer_host/web_cache_manager_unittest.cc', 'browser/renderer_host/web_cache_manager_unittest.cc',
'browser/resources/google_now/background.js', 'browser/resources/google_now/background.js',
'browser/resources/google_now/cards.js',
'browser/resources/google_now/background_test_util.js', 'browser/resources/google_now/background_test_util.js',
'browser/resources/google_now/background_unittest.gtestjs', 'browser/resources/google_now/background_unittest.gtestjs',
'browser/resources/google_now/cards.js',
'browser/resources/google_now/cards_unittest.gtestjs', 'browser/resources/google_now/cards_unittest.gtestjs',
'browser/resources/google_now/common_test_util.js',
'browser/resources/google_now/utility.js',
'browser/resources/google_now/utility_test_util.js',
'browser/resources/google_now/utility_unittest.gtestjs',
'browser/resources/print_preview/data/measurement_system.js', 'browser/resources/print_preview/data/measurement_system.js',
'browser/resources/print_preview/data/measurement_system_unittest.gtestjs', 'browser/resources/print_preview/data/measurement_system_unittest.gtestjs',
'browser/resources/print_preview/print_preview_utils.js', 'browser/resources/print_preview/print_preview_utils.js',
......
...@@ -1628,9 +1628,9 @@ var testing = {}; ...@@ -1628,9 +1628,9 @@ var testing = {};
} }
/** /**
* Mock4JS matcher object that matches the actual agrument and the expected * Mock4JS matcher object that matches the actual argument and the expected
* value iff their JSON represenations are same. * value iff their JSON represenations are same.
* @param {Object} expectedValue Expected value. * @param {Object} expectedValue
* @constructor * @constructor
*/ */
function MatchJSON(expectedValue) { function MatchJSON(expectedValue) {
...@@ -1659,14 +1659,54 @@ var testing = {}; ...@@ -1659,14 +1659,54 @@ var testing = {};
}; };
/** /**
* Builds a MatchJSON agrument matcher for a given expected value. * Builds a MatchJSON argument matcher for a given expected value.
* @param {Object} expectedValue Expected value. * @param {Object} expectedValue
* @return {MatchJSON} Resulting Mock4JS matcher. * @return {MatchJSON} Resulting Mock4JS matcher.
*/ */
function eqJSON(expectedValue) { function eqJSON(expectedValue) {
return new MatchJSON(expectedValue); return new MatchJSON(expectedValue);
} }
/**
* Mock4JS matcher object that matches the actual argument and the expected
* value iff the the string representation of the actual argument is equal to
* the expected value.
* @param {string} expectedValue
* @constructor
*/
function MatchToString(expectedValue) {
this.expectedValue_ = expectedValue;
}
MatchToString.prototype = {
/**
* Checks that the the string representation of the actual argument matches
* the expected value.
* @param {*} actualArgument The argument to match.
* @return {boolean} Result of the comparison.
*/
argumentMatches: function(actualArgument) {
return this.expectedValue_ === String(actualArgument);
},
/**
* Describes the matcher.
* @return {string} Description of this Mock4JS matcher.
*/
describe: function() {
return 'eqToString("' + this.expectedValue_ + '")';
},
};
/**
* Builds a MatchToString argument matcher for a given expected value.
* @param {Object} expectedValue
* @return {MatchToString} Resulting Mock4JS matcher.
*/
function eqToString(expectedValue) {
return new MatchToString(expectedValue);
}
// Exports. // Exports.
testing.Test = Test; testing.Test = Test;
exports.testDone = testDone; exports.testDone = testDone;
...@@ -1684,6 +1724,7 @@ var testing = {}; ...@@ -1684,6 +1724,7 @@ var testing = {};
exports.callFunctionWithSavedArgs = callFunctionWithSavedArgs; exports.callFunctionWithSavedArgs = callFunctionWithSavedArgs;
exports.callGlobalWithSavedArgs = callGlobalWithSavedArgs; exports.callGlobalWithSavedArgs = callGlobalWithSavedArgs;
exports.eqJSON = eqJSON; exports.eqJSON = eqJSON;
exports.eqToString = eqToString;
exports.expectTrue = createExpect(assertTrue); exports.expectTrue = createExpect(assertTrue);
exports.expectFalse = createExpect(assertFalse); exports.expectFalse = createExpect(assertFalse);
exports.expectGE = createExpect(assertGE); exports.expectGE = createExpect(assertGE);
......
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