Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/third_party/closure/goog/testing/mockcontrol.js
4500 views
1
/**
2
* @license
3
* Copyright The Closure Library Authors.
4
* SPDX-License-Identifier: Apache-2.0
5
*/
6
7
/**
8
* @fileoverview A MockControl holds a set of mocks for a particular test.
9
* It consolidates calls to $replay, $verify, and $tearDown, which simplifies
10
* the test and helps avoid omissions.
11
*
12
* You can create and control a mock:
13
* var mockFoo = mockControl.addMock(new MyMock(Foo));
14
*
15
* MockControl also exposes some convenience functions that create
16
* controlled mocks for common mocks: StrictMock, LooseMock,
17
* FunctionMock, MethodMock, and GlobalFunctionMock.
18
*/
19
20
21
goog.setTestOnly('goog.testing.MockControl');
22
goog.provide('goog.testing.MockControl');
23
24
goog.require('goog.Promise');
25
goog.require('goog.testing');
26
goog.require('goog.testing.LooseMock');
27
goog.require('goog.testing.StrictMock');
28
goog.requireType('goog.testing.MockInterface');
29
30
31
32
/**
33
* Controls a set of mocks. Controlled mocks are replayed, verified, and
34
* cleaned-up at the same time.
35
* @constructor
36
*/
37
goog.testing.MockControl = function() {
38
'use strict';
39
/**
40
* The list of mocks being controlled.
41
* @type {Array<goog.testing.MockInterface>}
42
* @private
43
*/
44
this.mocks_ = [];
45
};
46
47
48
/**
49
* Takes control of this mock.
50
* @param {goog.testing.MockInterface} mock Mock to be controlled.
51
* @return {goog.testing.MockInterface} The same mock passed in,
52
* for convenience.
53
*/
54
goog.testing.MockControl.prototype.addMock = function(mock) {
55
'use strict';
56
this.mocks_.push(mock);
57
return mock;
58
};
59
60
61
/**
62
* Calls replay on each controlled mock.
63
*/
64
goog.testing.MockControl.prototype.$replayAll = function() {
65
'use strict';
66
this.mocks_.forEach(function(m) {
67
'use strict';
68
m.$replay();
69
});
70
};
71
72
73
/**
74
* Calls reset on each controlled mock.
75
*/
76
goog.testing.MockControl.prototype.$resetAll = function() {
77
'use strict';
78
this.mocks_.forEach(function(m) {
79
'use strict';
80
m.$reset();
81
});
82
};
83
84
85
/**
86
* Returns a Promise that resolves when all of the controlled mocks have
87
* finished and verified.
88
* @return {!goog.Promise<!Array<undefined>>}
89
*/
90
goog.testing.MockControl.prototype.$waitAndVerifyAll = function() {
91
'use strict';
92
return goog.Promise.all(this.mocks_.map(function(m) {
93
'use strict';
94
return m.$waitAndVerify();
95
}));
96
};
97
98
99
/**
100
* Calls verify on each controlled mock.
101
*/
102
goog.testing.MockControl.prototype.$verifyAll = function() {
103
'use strict';
104
this.mocks_.forEach(function(m) {
105
'use strict';
106
m.$verify();
107
});
108
};
109
110
111
/**
112
* Calls tearDown on each controlled mock, if necesssary.
113
*/
114
goog.testing.MockControl.prototype.$tearDown = function() {
115
'use strict';
116
this.mocks_.forEach(function(m) {
117
'use strict';
118
if (!m) {
119
return;
120
}
121
m = /** @type {?} */ (m);
122
// $tearDown if defined.
123
if (m.$tearDown) {
124
m.$tearDown();
125
}
126
});
127
};
128
129
130
/**
131
* Creates a controlled StrictMock. Passes its arguments through to the
132
* StrictMock constructor.
133
* @param {Object|Function} objectToMock The object that should be mocked, or
134
* the constructor of an object to mock.
135
* @param {boolean=} opt_mockStaticMethods An optional argument denoting that
136
* a mock should be constructed from the static functions of a class.
137
* @param {boolean=} opt_createProxy An optional argument denoting that
138
* a proxy for the target mock should be created.
139
* @return {!goog.testing.StrictMock} The mock object.
140
*/
141
goog.testing.MockControl.prototype.createStrictMock = function(
142
objectToMock, opt_mockStaticMethods, opt_createProxy) {
143
'use strict';
144
var m = new goog.testing.StrictMock(
145
objectToMock, opt_mockStaticMethods, opt_createProxy);
146
this.addMock(m);
147
return m;
148
};
149
150
151
/**
152
* Creates a controlled LooseMock. Passes its arguments through to the
153
* LooseMock constructor.
154
* @param {Object|Function} objectToMock The object that should be mocked, or
155
* the constructor of an object to mock.
156
* @param {boolean=} opt_ignoreUnexpectedCalls Whether to ignore unexpected
157
* calls.
158
* @param {boolean=} opt_mockStaticMethods An optional argument denoting that
159
* a mock should be constructed from the static functions of a class.
160
* @param {boolean=} opt_createProxy An optional argument denoting that
161
* a proxy for the target mock should be created.
162
* @return {!goog.testing.LooseMock} The mock object.
163
*/
164
goog.testing.MockControl.prototype.createLooseMock = function(
165
objectToMock, opt_ignoreUnexpectedCalls, opt_mockStaticMethods,
166
opt_createProxy) {
167
'use strict';
168
var m = new goog.testing.LooseMock(
169
objectToMock, opt_ignoreUnexpectedCalls, opt_mockStaticMethods,
170
opt_createProxy);
171
this.addMock(m);
172
return m;
173
};
174
175
176
/**
177
* Creates a controlled FunctionMock. Passes its arguments through to the
178
* FunctionMock constructor.
179
* @param {string=} opt_functionName The optional name of the function to mock
180
* set to '[anonymous mocked function]' if not passed in.
181
* @param {number=} opt_strictness One of goog.testing.Mock.LOOSE or
182
* goog.testing.Mock.STRICT. The default is STRICT.
183
* @return {!goog.testing.MockInterface} The mocked function.
184
*/
185
goog.testing.MockControl.prototype.createFunctionMock = function(
186
opt_functionName, opt_strictness) {
187
'use strict';
188
var m = goog.testing.createFunctionMock(opt_functionName, opt_strictness);
189
this.addMock(m);
190
return m;
191
};
192
193
194
/**
195
* Creates a controlled MethodMock. Passes its arguments through to the
196
* MethodMock constructor.
197
* @param {Object} scope The scope of the method to be mocked out.
198
* @param {string} functionName The name of the function we're going to mock.
199
* @param {number=} opt_strictness One of goog.testing.Mock.LOOSE or
200
* goog.testing.Mock.STRICT. The default is STRICT.
201
* @return {!goog.testing.MockInterface} The mocked method.
202
*/
203
goog.testing.MockControl.prototype.createMethodMock = function(
204
scope, functionName, opt_strictness) {
205
'use strict';
206
var m = goog.testing.createMethodMock(scope, functionName, opt_strictness);
207
this.addMock(m);
208
return m;
209
};
210
211
212
/**
213
* Creates a controlled MethodMock for a constructor. Passes its arguments
214
* through to the MethodMock constructor. See
215
* {@link goog.testing.createConstructorMock} for details.
216
* @param {Object} scope The scope of the constructor to be mocked out.
217
* @param {string} constructorName The name of the function we're going to mock.
218
* @param {number=} opt_strictness One of goog.testing.Mock.LOOSE or
219
* goog.testing.Mock.STRICT. The default is STRICT.
220
* @return {!goog.testing.MockInterface} The mocked method.
221
*/
222
goog.testing.MockControl.prototype.createConstructorMock = function(
223
scope, constructorName, opt_strictness) {
224
'use strict';
225
var m = goog.testing.createConstructorMock(
226
scope, constructorName, opt_strictness);
227
this.addMock(m);
228
return m;
229
};
230
231
232
/**
233
* Creates a controlled GlobalFunctionMock. Passes its arguments through to the
234
* GlobalFunctionMock constructor.
235
* @param {string} functionName The name of the function we're going to mock.
236
* @param {number=} opt_strictness One of goog.testing.Mock.LOOSE or
237
* goog.testing.Mock.STRICT. The default is STRICT.
238
* @return {!goog.testing.MockInterface} The mocked function.
239
*/
240
goog.testing.MockControl.prototype.createGlobalFunctionMock = function(
241
functionName, opt_strictness) {
242
'use strict';
243
var m = goog.testing.createGlobalFunctionMock(functionName, opt_strictness);
244
this.addMock(m);
245
return m;
246
};
247
248