Move Webstore URL concepts to //extensions and out
[chromium-blink-merge.git] / chrome / test / data / webui / async_gen.js
blobd842800dca32a6325368a33a3ab38af0b5374b7b
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.
5 /**
6 * Test fixture for generated async tests.
7 * @extends {testing.Test}
8 */
9 function WebUIBrowserAsyncGenTest() {}
11 WebUIBrowserAsyncGenTest.prototype = {
12 __proto__: testing.Test.prototype,
14 /**
15 * Define the C++ class and include it.
16 * @type {?string}
17 * @override
19 typedefCppFixture: null,
21 /** @inheritDoc */
22 tearDown: function() {
23 expectFalse(this.tornDown);
24 expectFalse(this.running);
25 this.tornDown = true;
26 chrome.send('tearDown');
27 testing.Test.prototype.tearDown.call(this);
30 /** @inheritDoc */
31 browsePreload: DUMMY_URL,
33 /** @inheritDoc */
34 isAsync: true,
36 /**
37 * True when the tearDown method is called.
38 * @type {boolean}
40 tornDown: false,
42 /**
43 * True when running sync portion of test.
44 * @type {boolean}
46 running: false,
48 /** @inheritDoc */
49 preLoad: function() {
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"');
59 /**
60 * Will be set to continuation test #1.
61 * @type {Function}
62 * @this {WebUIBrowserAsyncGenTest}
64 var continueTest;
66 /**
67 * Will be set to continuation test #2.
68 * @type {Function}
69 * @this {WebUIBrowserAsyncGenTest}
71 var continueTest2;
73 TEST_F('WebUIBrowserAsyncGenTest', 'TestPreloadOnceOnNavigate', function() {
74 window.addEventListener('hashchange', this.continueTest(
75 WhenTestDone.DEFAULT, function() {
76 testDone();
77 }));
78 window.location = DUMMY_URL + '#anchor';
79 });
81 // Test that tearDown isn't called until the callback test runs.
82 TEST_F('WebUIBrowserAsyncGenTest', 'TestTearDown', function() {
83 assertFalse(this.tornDown);
84 this.running = true;
85 continueTest = this.continueTest(WhenTestDone.ALWAYS, function() {
86 this.running = false;
87 });
88 chrome.send('callJS', ['continueTest']);
89 });
91 // Test that continuing can be done multiple times and have access to closure
92 // variables.
93 TEST_F('WebUIBrowserAsyncGenTest', 'TestContinue', function() {
94 var xyz = false;
95 continueTest = this.continueTest(WhenTestDone.DEFAULT, function() {
96 assertFalse(xyz);
97 xyz = true;
98 chrome.send('callJS', ['continueTest2']);
99 });
100 continueTest2 = this.continueTest(WhenTestDone.ALWAYS, function() {
101 assertTrue(xyz);
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',
110 'testSavedArgs',
111 'testMixedArgs',
113 // Bind some arguments.
114 var var1, var2;
115 this.mockHandler.expects(once()).testBoundArgs().
116 will(runAllActionsAsync(WhenTestDone.DEFAULT,
117 callFunction(function(args) {
118 var1 = args[0];
119 }, ['val1']),
120 callFunction(function(args) {
121 var2 = args[0];
122 }, ['val2'])));
124 // Receive some saved arguments.
125 var var3, var4;
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) {
133 var3 = args[0];
135 callFunctionWithSavedArgs(savedArgs2, function(args) {
136 var4 = args[0];
137 })));
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];
148 var6 = boundArgs[0];
149 }, ['val6']),
150 callFunctionWithSavedArgs(
151 savedArgs2, function(passedArgs, boundArgs) {
152 var7 = passedArgs[0];
153 var8 = boundArgs[0];
154 }, ['val8'])));
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.
183 var testRan = false;
186 * Set |testRan| to true.
188 function setTestRanTrue() {
189 testRan = true;
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(
199 WhenTestDone.ALWAYS,
200 callGlobalWithSavedArgs(null, 'setTestRanTrue'),
201 callFunction(function() {
202 assertTrue(testRan);
203 })));
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.
214 * @type {Function}
216 var deferRunTest;
219 * Test fixture for testing deferred async tests.
220 * @extends {WebUIBrowserAsyncGenTest}
222 function WebUIBrowserAsyncGenDeferredTest() {}
224 WebUIBrowserAsyncGenDeferredTest.prototype = {
225 __proto__: WebUIBrowserAsyncGenTest.prototype,
227 /** @inheritDoc */
228 typedefCppFixture: 'WebUIBrowserAsyncGenTest',
231 * True when runTest is called.
232 * @type {boolean}
233 * @private
235 ranTest_: false,
237 /** @inheritDoc */
238 preLoad: function() {
239 deferRunTest = this.deferRunTest(WhenTestDone.DEFAULT);
242 /** @inheritDoc */
243 setUp: function() {
244 continueTest = this.continueTest(WhenTestDone.DEFAULT, function() {
245 expectFalse(this.ranTest_);
246 chrome.send('callJS', ['deferRunTest']);
248 chrome.send('callJS', ['continueTest']);
251 /** @inheritDoc */
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.
265 * @constructor
267 function WebUIBrowserAsyncGenDeferredToGlobalTest() {}
269 WebUIBrowserAsyncGenDeferredToGlobalTest.prototype = {
270 __proto__: WebUIBrowserAsyncGenDeferredTest.prototype,
272 /** @inheritDoc */
273 setUp: function() {
274 this.makeAndRegisterMockGlobals(['setTestRanTrue']);
275 this.mockGlobals.expects(once()).setTestRanTrue().
276 will(runAllActionsAsync(
277 WhenTestDone.ALWAYS,
278 callGlobalWithSavedArgs(null, 'setTestRanTrue'),
279 callFunction(deferRunTest)));
281 // Cause setTestRanTrue to be invoked asynchronously.
282 chrome.send('callJS', ['setTestRanTrue']);
286 TEST_F('WebUIBrowserAsyncGenDeferredToGlobalTest', 'TestDeferRunTestToGlobal',
287 function() {
288 this.ranTest_ = true;
289 assertTrue(testRan);