1 // Copyright (c) 2012 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 * Test fixture for generated async tests.
7 * @extends {testing.Test}
9 function WebUIBrowserAsyncGenTest() {}
11 WebUIBrowserAsyncGenTest
.prototype = {
12 __proto__
: testing
.Test
.prototype,
15 * Define the C++ class and include it.
19 typedefCppFixture
: null,
22 tearDown: function() {
23 expectFalse(this.tornDown
);
24 expectFalse(this.running
);
26 chrome
.send('tearDown');
27 testing
.Test
.prototype.tearDown
.call(this);
31 browsePreload
: DUMMY_URL
,
37 * True when the tearDown method is called.
43 * True when running sync portion of test.
50 if (window
.preLoadCount
=== undefined)
51 window
.preLoadCount
= 0;
52 assertEquals(0, Number(window
.preLoadCount
++));
56 // Include the c++ test fixture.
57 GEN('#include "chrome/test/data/webui/async_gen.h"');
60 * Will be set to continuation test #1.
62 * @this {WebUIBrowserAsyncGenTest}
67 * Will be set to continuation test #2.
69 * @this {WebUIBrowserAsyncGenTest}
73 TEST_F('WebUIBrowserAsyncGenTest', 'TestPreloadOnceOnNavigate', function() {
74 window
.addEventListener('hashchange', this.continueTest(
75 WhenTestDone
.DEFAULT
, function() {
78 window
.location
= DUMMY_URL
+ '#anchor';
81 // Test that tearDown isn't called until the callback test runs.
82 TEST_F('WebUIBrowserAsyncGenTest', 'TestTearDown', function() {
83 assertFalse(this.tornDown
);
85 continueTest
= this.continueTest(WhenTestDone
.ALWAYS
, function() {
88 chrome
.send('callJS', ['continueTest']);
91 // Test that continuing can be done multiple times and have access to closure
93 TEST_F('WebUIBrowserAsyncGenTest', 'TestContinue', function() {
95 continueTest
= this.continueTest(WhenTestDone
.DEFAULT
, function() {
98 chrome
.send('callJS', ['continueTest2']);
100 continueTest2
= this.continueTest(WhenTestDone
.ALWAYS
, function() {
103 chrome
.send('callJS', ['continueTest']);
106 // Test that runAllActionsAsync can be called with multiple functions, and with
107 // bound, saved, or mixed arguments.
108 TEST_F('WebUIBrowserAsyncGenTest', 'TestRunAllActionsAsyncMock', function() {
109 this.makeAndRegisterMockHandler(['testBoundArgs',
113 // Bind some arguments.
115 this.mockHandler
.expects(once()).testBoundArgs().
116 will(runAllActionsAsync(WhenTestDone
.DEFAULT
,
117 callFunction(function(args
) {
120 callFunction(function(args
) {
124 // Receive some saved arguments.
126 var savedArgs
= new SaveMockArguments();
127 var savedArgs2
= new SaveMockArguments();
128 this.mockHandler
.expects(once()).testSavedArgs(
129 savedArgs
.match(savedArgs2
.match(eq(['passedVal1'])))).
130 will(runAllActionsAsync(
131 WhenTestDone
.DEFAULT
,
132 callFunctionWithSavedArgs(savedArgs
, function(args
) {
135 callFunctionWithSavedArgs(savedArgs2
, function(args
) {
139 // Receive some saved arguments and some bound arguments.
140 var var5
, var6
, var7
, var8
;
141 this.mockHandler
.expects(once()).testMixedArgs(
142 savedArgs
.match(savedArgs2
.match(eq('passedVal2')))).
143 will(runAllActionsAsync(
144 WhenTestDone
.DEFAULT
,
145 callFunctionWithSavedArgs(
146 savedArgs
, function(passedArgs
, boundArgs
) {
147 var5
= passedArgs
[0];
150 callFunctionWithSavedArgs(
151 savedArgs2
, function(passedArgs
, boundArgs
) {
152 var7
= passedArgs
[0];
156 // Send the cases to the mocked handler & tell the C++ handler to continue2.
157 continueTest
= this.continueTest(WhenTestDone
.ASSERT
, function() {
158 chrome
.send('testBoundArgs');
159 chrome
.send('testSavedArgs', ['passedVal1']);
160 chrome
.send('testMixedArgs', ['passedVal2']);
161 chrome
.send('callJS', ['continueTest2']);
164 // Check expectations after mocks have been called.
165 continueTest2
= this.continueTest(WhenTestDone
.ALWAYS
, function() {
166 expectEquals('val1', var1
);
167 expectEquals('val2', var2
);
168 expectEquals('passedVal1', var3
);
169 expectEquals('passedVal1', var4
);
170 expectEquals('passedVal2', var5
);
171 expectEquals('val6', var6
);
172 expectEquals('passedVal2', var7
);
173 expectEquals('val8', var8
);
176 // Kick off the tests asynchronously.
177 chrome
.send('callJS', ['continueTest']);
181 * Set to true when |setTestRanTrue| is called.
186 * Set |testRan| to true.
188 function setTestRanTrue() {
192 // Test overriding globals.
193 TEST_F('WebUIBrowserAsyncGenTest', 'TestRegisterMockGlobals', function() {
194 this.makeAndRegisterMockGlobals(['setTestRanTrue']);
196 // Mock the setTestRanTrue global function.
197 this.mockGlobals
.expects(once()).setTestRanTrue().
198 will(runAllActionsAsync(
200 callGlobalWithSavedArgs(null, 'setTestRanTrue'),
201 callFunction(function() {
205 // Cause setTestRanTrue to be invoked asynchronously.
206 chrome
.send('callJS', ['setTestRanTrue']);
208 // In case the global isn't called, call testDone to collect the results.
209 chrome
.send('callJS', ['testDone']);
213 * Will be set to the runTest continuation by the following test fixture.
219 * Test fixture for testing deferred async tests.
220 * @extends {WebUIBrowserAsyncGenTest}
222 function WebUIBrowserAsyncGenDeferredTest() {}
224 WebUIBrowserAsyncGenDeferredTest
.prototype = {
225 __proto__
: WebUIBrowserAsyncGenTest
.prototype,
228 typedefCppFixture
: 'WebUIBrowserAsyncGenTest',
231 * True when runTest is called.
238 preLoad: function() {
239 deferRunTest
= this.deferRunTest(WhenTestDone
.DEFAULT
);
244 continueTest
= this.continueTest(WhenTestDone
.DEFAULT
, function() {
245 expectFalse(this.ranTest_
);
246 chrome
.send('callJS', ['deferRunTest']);
248 chrome
.send('callJS', ['continueTest']);
252 tearDown: function() {
253 expectTrue(this.ranTest_
);
254 WebUIBrowserAsyncGenTest
.prototype.tearDown
.call(this);
258 // Test that the test can be deferred appropriately.
259 TEST_F('WebUIBrowserAsyncGenDeferredTest', 'TestDeferRunTest', function() {
260 this.ranTest_
= true;
264 * Test fixture for testing async tests are deferred until global is called.
267 function WebUIBrowserAsyncGenDeferredToGlobalTest() {}
269 WebUIBrowserAsyncGenDeferredToGlobalTest
.prototype = {
270 __proto__
: WebUIBrowserAsyncGenDeferredTest
.prototype,
274 this.makeAndRegisterMockGlobals(['setTestRanTrue']);
275 this.mockGlobals
.expects(once()).setTestRanTrue().
276 will(runAllActionsAsync(
278 callGlobalWithSavedArgs(null, 'setTestRanTrue'),
279 callFunction(deferRunTest
)));
281 // Cause setTestRanTrue to be invoked asynchronously.
282 chrome
.send('callJS', ['setTestRanTrue']);
286 TEST_F('WebUIBrowserAsyncGenDeferredToGlobalTest', 'TestDeferRunTestToGlobal',
288 this.ranTest_
= true;