- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / test / data / webui / mock_controller.js
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.
4
5 /**
6  * Create a mock function that records function calls and validates against
7  * expectations.
8  * @constructor.
9  */
10 function MockMethod() {
11   var fn = function() {
12     var args = Array.prototype.slice.call(arguments);
13     fn.recordCall(args);
14     return this.returnValue;
15   };
16
17   /**
18    * List of signatures for fucntion calls.
19    * @type {!Array.<!Array>}
20    * @private
21    */
22   fn.calls_ = [];
23
24   /**
25    * List of expected call signatures.
26    * @type {!Array.<!Array>}
27    * @private
28    */
29   fn.expectations_ = [];
30
31   /**
32    * Value returned from call to function.
33    * @type {*}
34    */
35   fn.returnValue = undefined;
36
37   fn.__proto__ = MockMethod.prototype;
38   return fn;
39 }
40
41 MockMethod.prototype = {
42   /**
43    * Adds an expected call signature.
44    * @param {...}  var_args Expected arguments for the function call.
45    */
46   addExpectation: function() {
47     var args = Array.prototype.slice.call(arguments);
48     this.expectations_.push(args);
49   },
50
51   /**
52    * Adds a call signature.
53    * @param {!Array} args.
54    */
55   recordCall: function(args) {
56     this.calls_.push(args);
57   },
58
59   /**
60    * Verifies that the function is called the expected number of times and with
61    * the correct signature for each call.
62    */
63   verifyMock: function() {
64     assertEquals(this.expectations_.length,
65                  this.calls_.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]);
69     }
70   },
71
72   /**
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
77    *     overrides.
78    * @param {!Array} expected The expected arguments.
79    * @parma {!Array} observed The observed arguments.
80    */
81   validateCall: function(index, expected, observed) {
82     assertDeepEquals(expected, observed);
83   },
84 };
85
86 /**
87  * Controller for mocking methods. Tracks calls to mocked methods and verifies
88  * that call signatures match expectations.
89  * @constructor.
90  */
91 function MockController() {
92   /**
93    * Original functions implementations, which are restored when |reset| is
94    * called.
95    * @type {!Array.<!Object>}
96    * @private
97    */
98   this.overrides_ = [];
99
100   /**
101    * List of registered mocks.
102    * @type {!Array.<!MockMethod>}
103    * @private
104    */
105   this.mocks_ = [];
106 }
107
108 MockController.prototype = {
109   /**
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
115    *     reset.
116    */
117   createFunctionMock: function(opt_parent, opt_functionName) {
118     var fn = new MockMethod();
119
120     // Register mock.
121     if (opt_parent && opt_functionName) {
122       this.overrides_.push({
123         parent: opt_parent,
124         functionName: opt_functionName,
125         originalFunction: opt_parent[opt_functionName]
126       });
127       opt_parent[opt_functionName] = fn;
128     }
129     this.mocks_.push(fn);
130
131     return fn;
132   },
133
134   /**
135    * Validates all mocked methods. An exception is thrown if the
136    * expected and actual calls to a mocked function to not align.
137    */
138   verifyMocks: function() {
139     for (var i = 0; i < this.mocks_.length; i++) {
140       this.mocks_[i].verifyMock();
141     }
142   },
143
144   /**
145    * Discard mocks reestoring default behavior.
146    */
147   reset: function() {
148     for (var i = 0; i < this.overrides_.length; i++) {
149       var override = this.overrides_[i];
150       override.parent[override.functionName] = override.originalFunction;
151     }
152   },
153
154 };