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
3 // found in the LICENSE file.
6 * Create a mock function that records function calls and validates against
10 function MockMethod() {
12 var args = Array.prototype.slice.call(arguments);
14 return this.returnValue;
18 * List of signatures for fucntion calls.
19 * @type {!Array.<!Array>}
25 * List of expected call signatures.
26 * @type {!Array.<!Array>}
29 fn.expectations_ = [];
32 * Value returned from call to function.
35 fn.returnValue = undefined;
37 fn.__proto__ = MockMethod.prototype;
41 MockMethod.prototype = {
43 * Adds an expected call signature.
44 * @param {...} var_args Expected arguments for the function call.
46 addExpectation: function() {
47 var args = Array.prototype.slice.call(arguments);
48 this.expectations_.push(args);
52 * Adds a call signature.
53 * @param {!Array} args.
55 recordCall: function(args) {
56 this.calls_.push(args);
60 * Verifies that the function is called the expected number of times and with
61 * the correct signature for each call.
63 verifyMock: function() {
64 assertEquals(this.expectations_.length,
66 'Number of method calls did not match expectation.');
67 for (var i = 0; i < this.expectations_.length; i++) {
68 this.validateCall(i, this.expectations_[i], this.calls_[i]);
73 * Verifies that the observed function arguments match expectations.
74 * Override if strict equality is not required.
75 * @param {number} index Canonical index of the function call. Unused in the
76 * base implementation, but provides context that may be useful for
78 * @param {!Array} expected The expected arguments.
79 * @parma {!Array} observed The observed arguments.
81 validateCall: function(index, expected, observed) {
82 assertDeepEquals(expected, observed);
87 * Controller for mocking methods. Tracks calls to mocked methods and verifies
88 * that call signatures match expectations.
91 function MockController() {
93 * Original functions implementations, which are restored when |reset| is
95 * @type {!Array.<!Object>}
101 * List of registered mocks.
102 * @type {!Array.<!MockMethod>}
108 MockController.prototype = {
110 * Creates a mock function.
111 * @param {Object=} opt_parent Optional parent object for the function.
112 * @param {string=} opt_functionName Optional name of the function being
113 * mocked. If the parent and function name are both provided, the
114 * mock is automatically substituted for the original and replaced on
117 createFunctionMock: function(opt_parent, opt_functionName) {
118 var fn = new MockMethod();
121 if (opt_parent && opt_functionName) {
122 this.overrides_.push({
124 functionName: opt_functionName,
125 originalFunction: opt_parent[opt_functionName]
127 opt_parent[opt_functionName] = fn;
129 this.mocks_.push(fn);
135 * Validates all mocked methods. An exception is thrown if the
136 * expected and actual calls to a mocked function to not align.
138 verifyMocks: function() {
139 for (var i = 0; i < this.mocks_.length; i++) {
140 this.mocks_[i].verifyMock();
145 * Discard mocks reestoring default behavior.
148 for (var i = 0; i < this.overrides_.length; i++) {
149 var override = this.overrides_[i];
150 override.parent[override.functionName] = override.originalFunction;