OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2011 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 * Test fixture for generated async tests. | |
7 * @extends {testing.Test} | |
8 */ | |
9 function WebUIBrowserAsyncGenTest() {} | |
10 | |
11 WebUIBrowserAsyncGenTest.prototype = { | |
12 __proto__: testing.Test.prototype, | |
13 | |
14 /** | |
15 * Define the C++ class and include it. | |
16 * @type {?string} | |
17 * @override | |
18 */ | |
19 typedefCppFixture: null, | |
20 | |
21 /** @inheritDoc */ | |
22 tearDown: function() { | |
23 expectFalse(this.tornDown); | |
24 expectFalse(this.running); | |
25 this.tornDown = true; | |
26 chrome.send('tornDown'); | |
27 testing.Test.prototype.tearDown.call(this); | |
28 }, | |
29 | |
30 /** @inheritDoc */ | |
31 browsePreload: DUMMY_URL, | |
32 | |
33 /** @inheritDoc */ | |
34 isAsync: true, | |
35 | |
36 /** | |
37 * True when the tearDown method is called. | |
38 * @type {boolean} | |
39 */ | |
40 tornDown: false, | |
41 | |
42 /** | |
43 * True when running sync portion of test. | |
44 * @type {boolean} | |
45 */ | |
46 running: false, | |
47 }; | |
48 | |
49 GEN('#include "chrome/test/data/webui/async_gen-inl.h"'); | |
50 GEN(''); | |
51 GEN('WebUIBrowserAsyncGenTest::WebUIBrowserAsyncGenTest() {}'); | |
52 GEN('WebUIBrowserAsyncGenTest::~WebUIBrowserAsyncGenTest() {}'); | |
53 GEN('WebUIBrowserAsyncGenTest::AsyncWebUIMessageHandler::'); | |
54 GEN(' AsyncWebUIMessageHandler() {}'); | |
55 GEN('WebUIBrowserAsyncGenTest::AsyncWebUIMessageHandler::'); | |
56 GEN(' ~AsyncWebUIMessageHandler() {}'); | |
57 GEN(''); | |
58 | |
59 /** | |
60 * Will be set to continuation test #1. | |
61 * @type {Function} | |
62 * @this {WebUIBrowserAsyncGenTest} | |
63 */ | |
64 var continueTest; | |
65 | |
66 /** | |
67 * Will be set to continuation test #2. | |
68 * @type {Function} | |
69 * @this {WebUIBrowserAsyncGenTest} | |
70 */ | |
71 var continueTest2; | |
72 | |
73 // Test that tearDown isn't called until the callback test runs. | |
74 TEST_F('WebUIBrowserAsyncGenTest', 'TestTearDown', function() { | |
75 assertFalse(this.tornDown); | |
76 this.running = true; | |
77 continueTest = this.continueTest(WhenTestDone.ALWAYS, function() { | |
78 this.running = false; | |
79 }); | |
80 chrome.send('callJS', ['continueTest']); | |
81 }); | |
82 | |
83 // Test that continuing can be done multiple times and have access to closure | |
84 // variables. | |
85 TEST_F('WebUIBrowserAsyncGenTest', 'TestContinue', function() { | |
86 var xyz = false; | |
87 continueTest = this.continueTest(WhenTestDone.DEFAULT, function() { | |
88 assertFalse(xyz); | |
89 xyz = true; | |
90 chrome.send('callJS', ['continueTest2']); | |
91 }); | |
92 continueTest2 = this.continueTest(WhenTestDone.ALWAYS, function() { | |
93 assertTrue(xyz); | |
94 }); | |
95 chrome.send('callJS', ['continueTest']); | |
96 }); | |
97 | |
98 // Test that runAllActionsAsync can be called with multiple functions, and with | |
99 // bound, saved, or mixed arguments. | |
100 TEST_F('WebUIBrowserAsyncGenTest', 'TestRunAllActionsAsyncMock', function() { | |
101 /** | |
102 * Create a handler class with empty methods to allow mocking to register | |
103 * expectations and for registration of handlers with chrome.send. | |
104 * @constructor | |
105 */ | |
106 function MockHandler() {} | |
107 | |
108 MockHandler.prototype = { | |
109 testBoundArgs: function() {}, | |
110 testSavedArgs: function() {}, | |
111 testMixedArgs: function() {}, | |
112 }; | |
113 | |
114 var mockHandler = mock(MockHandler); | |
115 registerMockMessageCallbacks(mockHandler, MockHandler); | |
116 | |
117 // Bind some arguments. | |
118 var var1, var2; | |
119 mockHandler.expects(once()).testBoundArgs('not used'). | |
120 will(runAllActionsAsync(WhenTestDone.DEFAULT, | |
121 callFunction(function(val1) { | |
122 var1 = val1; | |
123 }, 123), | |
mmenke
2011/08/24 15:27:50
Think this test would be a little cleaner with str
Sheridan Rawlins
2011/08/24 21:15:45
First off, wow, what a coincidence that 123 fell o
| |
124 callFunction(function(val2) { | |
125 var2 = val2; | |
126 }, 456))); | |
127 | |
128 // Receive some saved arguments. | |
129 var var3, var4; | |
130 var savedArgs = new SaveMockArguments(); | |
131 var savedArgs2 = new SaveMockArguments(); | |
132 mockHandler.expects(once()).testSavedArgs( | |
133 savedArgs.match(savedArgs2.match(eq(10)))). | |
134 will(runAllActionsAsync( | |
135 WhenTestDone.DEFAULT, | |
136 callFunctionWithSavedArgs(savedArgs, function(val3) { | |
137 var3 = val3; | |
138 }), | |
139 callFunctionWithSavedArgs(savedArgs2, function(val4) { | |
140 var4 = val4; | |
141 }))); | |
142 | |
143 // Receive some saved arguments and some bound arguments. | |
144 var var5, var6, var7, var8; | |
145 mockHandler.expects(once()).testMixedArgs( | |
146 savedArgs.match(savedArgs2.match(eq(11)))). | |
147 will(runAllActionsAsync( | |
148 WhenTestDone.DEFAULT, | |
149 callFunctionWithSavedArgs(savedArgs, function(val5, val6) { | |
150 var5 = val5; | |
151 var6 = val6; | |
152 }, 78), | |
153 callFunctionWithSavedArgs(savedArgs2, function(val7, val8) { | |
154 var7 = val7; | |
155 var8 = val8; | |
156 }, 910))); | |
157 | |
158 // Send the cases to the mocked handler & tell the C++ handler to continue2. | |
159 continueTest = this.continueTest(WhenTestDone.ASSERT, function() { | |
160 chrome.send('testBoundArgs', ['not used']); | |
161 chrome.send('testSavedArgs', [10]); | |
162 chrome.send('testMixedArgs', [11]); | |
163 chrome.send('callJS', ['continueTest2']); | |
164 }); | |
165 | |
166 // Check expectations after mocks have been called. | |
167 continueTest2 = this.continueTest(WhenTestDone.ALWAYS, function() { | |
168 expectEquals(123, var1); | |
169 expectEquals(456, var2); | |
170 expectEquals(10, var3); | |
171 expectEquals(10, var4); | |
172 expectEquals(11, var5); | |
173 expectEquals(78, var6); | |
174 expectEquals(11, var7); | |
175 expectEquals(910, var8); | |
176 }); | |
177 | |
178 // Kick off the tests asynchronously. | |
179 chrome.send('callJS', ['continueTest']); | |
180 }); | |
181 | |
182 /** | |
183 * Will be set to the runTest continuation by the following test fixture. | |
184 * @type {Function} | |
185 */ | |
186 var deferRunTest; | |
187 | |
188 /** | |
189 * Test fixture for testing deferred async tests. | |
190 * @extends {WebUIBrowserAsyncGenTest} | |
191 */ | |
192 function WebUIBrowserAsyncGenDeferredTest() {} | |
193 | |
194 WebUIBrowserAsyncGenDeferredTest.prototype = { | |
195 __proto__: WebUIBrowserAsyncGenTest.prototype, | |
196 | |
197 /** @inheritDoc */ | |
198 typedefCppFixture: 'WebUIBrowserAsyncGenTest', | |
199 | |
200 /** | |
201 * True when runTest is called. | |
202 * @type {boolean} | |
203 * @private | |
204 */ | |
205 ranTest_: false, | |
206 | |
207 /** @inheritDoc */ | |
208 setUp: function() { | |
209 deferRunTest = this.deferRunTest(WhenTestDone.DEFAULT); | |
210 continueTest = this.continueTest(WhenTestDone.DEFAULT, function() { | |
211 expectFalse(this.ranTest_); | |
212 chrome.send('callJS', ['deferRunTest']); | |
213 }); | |
214 chrome.send('callJS', ['continueTest']); | |
215 }, | |
216 | |
217 /** @inheritDoc */ | |
218 runTest: function() { | |
219 this.ranTest_ = true; | |
220 WebUIBrowserAsyncGenTest.prototype.runTest.apply(this, arguments); | |
221 }, | |
222 }; | |
223 | |
224 // Test that the test can be deferred appropriately. | |
225 TEST_F('WebUIBrowserAsyncGenDeferredTest', 'TestDeferRunTest', function() { | |
226 expectTrue(this.ranTest_); | |
227 }); | |
OLD | NEW |