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

Side by Side Diff: chrome/test/data/webui/mock_controller.js

Issue 618743003: Unit test for sync online wallpaper (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 2 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
« no previous file with comments | « chrome/test/data/chromeos/wallpaper_manager/unit_tests/event_page_unittest.js ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 /** 5 /**
6 * Create a mock function that records function calls and validates against 6 * Create a mock function that records function calls and validates against
7 * expectations. 7 * expectations.
8 * @constructor. 8 * @constructor.
9 */ 9 */
10 function MockMethod() { 10 function MockMethod() {
11 var fn = function() { 11 var fn = function() {
12 var args = Array.prototype.slice.call(arguments); 12 var args = Array.prototype.slice.call(arguments);
13 var callbacks =
14 args.filter(function(arg) { return (typeof arg == 'function'); });
15
16 if (callbacks.length > 1) {
17 console.error('Only support mocking function with at most one callback.');
18 return;
19 }
20
13 fn.recordCall(args); 21 fn.recordCall(args);
14 return this.returnValue; 22 if (callbacks.length == 1) {
23 callbacks[0].apply(undefined, fn.callbackData);
24 return;
25 }
26 return fn.returnValue;
15 }; 27 };
16 28
17 /** 29 /**
18 * List of signatures for fucntion calls. 30 * List of signatures for fucntion calls.
19 * @type {!Array.<!Array>} 31 * @type {!Array.<!Array>}
20 * @private 32 * @private
21 */ 33 */
22 fn.calls_ = []; 34 fn.calls_ = [];
23 35
24 /** 36 /**
25 * List of expected call signatures. 37 * List of expected call signatures.
26 * @type {!Array.<!Array>} 38 * @type {!Array.<!Array>}
27 * @private 39 * @private
28 */ 40 */
29 fn.expectations_ = []; 41 fn.expectations_ = [];
30 42
31 /** 43 /**
32 * Value returned from call to function. 44 * Value returned from call to function.
33 * @type {*} 45 * @type {*}
34 */ 46 */
35 fn.returnValue = undefined; 47 fn.returnValue = undefined;
36 48
49 /**
50 * List of arguments for callback function.
51 * @type {!Array.<!Array>}
52 * @private
kevers 2014/09/30 18:12:03 Based on direct access from test case, this is not
bshe 2014/09/30 21:59:48 Dooh. It should not be private.
53 */
54 fn.callbackData = [];
55
37 fn.__proto__ = MockMethod.prototype; 56 fn.__proto__ = MockMethod.prototype;
38 return fn; 57 return fn;
39 } 58 }
40 59
41 MockMethod.prototype = { 60 MockMethod.prototype = {
42 /** 61 /**
43 * Adds an expected call signature. 62 * Adds an expected call signature.
44 * @param {...} var_args Expected arguments for the function call. 63 * @param {...} var_args Expected arguments for the function call.
45 */ 64 */
46 addExpectation: function() { 65 addExpectation: function() {
47 var args = Array.prototype.slice.call(arguments); 66 var args = Array.prototype.slice.call(arguments);
48 this.expectations_.push(args); 67 this.expectations_.push(args.filter(this.notFunction_));
49 }, 68 },
50 69
51 /** 70 /**
52 * Adds a call signature. 71 * Adds a call signature.
53 * @param {!Array} args. 72 * @param {!Array} args.
54 */ 73 */
55 recordCall: function(args) { 74 recordCall: function(args) {
56 this.calls_.push(args); 75 this.calls_.push(args.filter(this.notFunction_));
57 }, 76 },
58 77
59 /** 78 /**
60 * Verifies that the function is called the expected number of times and with 79 * Verifies that the function is called the expected number of times and with
61 * the correct signature for each call. 80 * the correct signature for each call.
62 */ 81 */
63 verifyMock: function() { 82 verifyMock: function() {
64 var errorMessage = 'Number of method calls did not match expectation.'; 83 var errorMessage = 'Number of method calls did not match expectation.';
65 if (this.functionName) 84 if (this.functionName)
66 errorMessage = 'Error in ' + this.functionName + ':\n' + errorMessage; 85 errorMessage = 'Error in ' + this.functionName + ':\n' + errorMessage;
(...skipping 10 matching lines...) Expand all
77 * Override if strict equality is not required. 96 * Override if strict equality is not required.
78 * @param {number} index Canonical index of the function call. Unused in the 97 * @param {number} index Canonical index of the function call. Unused in the
79 * base implementation, but provides context that may be useful for 98 * base implementation, but provides context that may be useful for
80 * overrides. 99 * overrides.
81 * @param {!Array} expected The expected arguments. 100 * @param {!Array} expected The expected arguments.
82 * @parma {!Array} observed The observed arguments. 101 * @parma {!Array} observed The observed arguments.
83 */ 102 */
84 validateCall: function(index, expected, observed) { 103 validateCall: function(index, expected, observed) {
85 assertDeepEquals(expected, observed); 104 assertDeepEquals(expected, observed);
86 }, 105 },
106
107 /**
108 * Test if arg is a function.
109 * @param {*} arg The argument to test.
110 * @return True if arg is not function type.
111 */
112 notFunction_: function(arg) {
113 return typeof arg != 'function';
114 }
87 }; 115 };
88 116
89 /** 117 /**
90 * Controller for mocking methods. Tracks calls to mocked methods and verifies 118 * Controller for mocking methods. Tracks calls to mocked methods and verifies
91 * that call signatures match expectations. 119 * that call signatures match expectations.
92 * @constructor. 120 * @constructor.
93 */ 121 */
94 function MockController() { 122 function MockController() {
95 /** 123 /**
96 * Original functions implementations, which are restored when |reset| is 124 * Original functions implementations, which are restored when |reset| is
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
149 * Discard mocks reestoring default behavior. 177 * Discard mocks reestoring default behavior.
150 */ 178 */
151 reset: function() { 179 reset: function() {
152 for (var i = 0; i < this.overrides_.length; i++) { 180 for (var i = 0; i < this.overrides_.length; i++) {
153 var override = this.overrides_[i]; 181 var override = this.overrides_[i];
154 override.parent[override.functionName] = override.originalFunction; 182 override.parent[override.functionName] = override.originalFunction;
155 } 183 }
156 }, 184 },
157 185
158 }; 186 };
OLDNEW
« no previous file with comments | « chrome/test/data/chromeos/wallpaper_manager/unit_tests/event_page_unittest.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698