OLD | NEW |
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 JSON.stringify(this.expectedValue_) === |
| 65 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 } |
OLD | NEW |