Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(137)

Side by Side Diff: chrome/browser/resources/google_now/background_test_util.js

Issue 5151418134560768: Adding first unit tests with mocks (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: arv@ notes Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // Mocks for globals needed for loading background.js. 5 // Mocks for globals needed for loading background.js.
6 6
7 function emptyMock() {} 7 function emptyMock() {}
8 function buildTaskManager() { 8 function buildTaskManager() {
9 return {instrumentApiFunction: emptyMock}; 9 return {instrumentApiFunction: emptyMock};
10 } 10 }
11 var instrumentApiFunction = emptyMock; 11 var instrumentApiFunction = emptyMock;
12 var buildAttemptManager = emptyMock; 12 var buildAttemptManager = emptyMock;
13 var emptyListener = {addListener: emptyMock}; 13 var emptyListener = {addListener: emptyMock};
14 chrome['location'] = {onLocationUpdate: emptyListener}; 14 chrome['location'] = {onLocationUpdate: emptyListener};
15 chrome['notifications'] = { 15 chrome['notifications'] = {
16 onButtonClicked: emptyListener, 16 onButtonClicked: emptyListener,
17 onClicked: emptyListener, 17 onClicked: emptyListener,
18 onClosed: emptyListener 18 onClosed: emptyListener
19 }; 19 };
20 chrome['omnibox'] = {onInputEntered: emptyListener}; 20 chrome['omnibox'] = {onInputEntered: emptyListener};
21 chrome['runtime'] = { 21 chrome['runtime'] = {
22 onInstalled: emptyListener, 22 onInstalled: emptyListener,
23 onStartup: emptyListener 23 onStartup: emptyListener
24 }; 24 };
25
25 var storage = {}; 26 var storage = {};
27
28 /**
29 * Syntactic sugar for use with will() on a Mock4JS.Mock.
30 * Creates an action for will() that invokes a callback that the tested code
31 * passes to a mocked function.
32 * @param {SaveMockArguments} savedArgs Arguments that will contain the
33 * callback once the mocked function is called.
34 * @param {number} callbackParameter Index of the callback parameter in
35 * |savedArgs|.
36 * @param {...Object} var_args Arguments to pass to the callback.
37 * @return {CallFunctionAction} Action for use in will().
38 */
39 function invokeCallback(savedArgs, callbackParameter, var_args) {
40 var callbackArguments = Array.prototype.slice.call(arguments, 2);
41 return callFunction(function() {
42 savedArgs.arguments[callbackParameter].apply(null, callbackArguments);
43 });
44 }
45
46 /**
47 * Mock4JS matcher object that matches the actual agrument and the expected
48 * value iff their JSON represenations are same.
49 * @param {Object} expectedValue Expected value.
50 * @constructor
51 */
52 function MatchJSON(expectedValue) {
53 this.expectedValue_ = expectedValue;
54 }
55
56 MatchJSON.prototype = {
57 /**
58 * Checks that JSON represenation of the actual and expected arguments are
59 * same.
60 * @param {Object} actualArgument The argument to match.
61 * @return {boolean} Result of the comparison.
62 */
63 argumentMatches: function(actualArgument) {
64 return (
arv (Not doing code reviews) 2013/07/12 14:18:34 Or return JSON.stringify(this.expectedValue_)
vadimt 2013/07/12 18:22:35 Done.
65 JSON.stringify(this.expectedValue_) === JSON.stringify(actualArgument));
66 },
67
68 /**
69 * Describes the matcher.
70 * @return {string} Description of this Mock4JS matcher.
71 */
72 describe: function() {
73 return 'eqJSON(' + JSON.stringify(this.expectedValue_) + ')';
74 }
75 };
76
77 /**
78 * Builds a MatchJSON agrument matcher for a given expected value.
79 * @param {Object} expectedValue Expected value.
80 * @return {MatchJSON} Resulting Mock4JS matcher.
81 */
82 function eqJSON(expectedValue) {
83 return new MatchJSON(expectedValue);
84 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698