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 // Include the bulk of c++ code. |
| 50 GEN('#include "chrome/test/data/webui/async_gen-inl.h"'); |
| 51 GEN(''); |
| 52 |
| 53 // Constructors and destructors must be provided in .cc to prevent clang errors. |
| 54 GEN('WebUIBrowserAsyncGenTest::WebUIBrowserAsyncGenTest() {}'); |
| 55 GEN('WebUIBrowserAsyncGenTest::~WebUIBrowserAsyncGenTest() {}'); |
| 56 GEN('WebUIBrowserAsyncGenTest::AsyncWebUIMessageHandler::'); |
| 57 GEN(' AsyncWebUIMessageHandler() {}'); |
| 58 GEN('WebUIBrowserAsyncGenTest::AsyncWebUIMessageHandler::'); |
| 59 GEN(' ~AsyncWebUIMessageHandler() {}'); |
| 60 GEN(''); |
| 61 |
| 62 /** |
| 63 * Will be set to continuation test #1. |
| 64 * @type {Function} |
| 65 * @this {WebUIBrowserAsyncGenTest} |
| 66 */ |
| 67 var continueTest; |
| 68 |
| 69 /** |
| 70 * Will be set to continuation test #2. |
| 71 * @type {Function} |
| 72 * @this {WebUIBrowserAsyncGenTest} |
| 73 */ |
| 74 var continueTest2; |
| 75 |
| 76 // Test that tearDown isn't called until the callback test runs. |
| 77 TEST_F('WebUIBrowserAsyncGenTest', 'TestTearDown', function() { |
| 78 assertFalse(this.tornDown); |
| 79 this.running = true; |
| 80 continueTest = this.continueTest(WhenTestDone.ALWAYS, function() { |
| 81 this.running = false; |
| 82 }); |
| 83 chrome.send('callJS', ['continueTest']); |
| 84 }); |
| 85 |
| 86 // Test that continuing can be done multiple times and have access to closure |
| 87 // variables. |
| 88 TEST_F('WebUIBrowserAsyncGenTest', 'TestContinue', function() { |
| 89 var xyz = false; |
| 90 continueTest = this.continueTest(WhenTestDone.DEFAULT, function() { |
| 91 assertFalse(xyz); |
| 92 xyz = true; |
| 93 chrome.send('callJS', ['continueTest2']); |
| 94 }); |
| 95 continueTest2 = this.continueTest(WhenTestDone.ALWAYS, function() { |
| 96 assertTrue(xyz); |
| 97 }); |
| 98 chrome.send('callJS', ['continueTest']); |
| 99 }); |
| 100 |
| 101 // Test that runAllActionsAsync can be called with multiple functions, and with |
| 102 // bound, saved, or mixed arguments. |
| 103 TEST_F('WebUIBrowserAsyncGenTest', 'TestRunAllActionsAsyncMock', function() { |
| 104 /** |
| 105 * Create a handler class with empty methods to allow mocking to register |
| 106 * expectations and for registration of handlers with chrome.send. |
| 107 * @constructor |
| 108 */ |
| 109 function MockHandler() {} |
| 110 |
| 111 MockHandler.prototype = { |
| 112 testBoundArgs: function() {}, |
| 113 testSavedArgs: function() {}, |
| 114 testMixedArgs: function() {}, |
| 115 }; |
| 116 |
| 117 var mockHandler = mock(MockHandler); |
| 118 registerMockMessageCallbacks(mockHandler, MockHandler); |
| 119 |
| 120 // Bind some arguments. |
| 121 var var1, var2; |
| 122 mockHandler.expects(once()).testBoundArgs(). |
| 123 will(runAllActionsAsync(WhenTestDone.DEFAULT, |
| 124 callFunction(function(args) { |
| 125 var1 = args[0]; |
| 126 }, ['val1']), |
| 127 callFunction(function(args) { |
| 128 var2 = args[0]; |
| 129 }, ['val2']))); |
| 130 |
| 131 // Receive some saved arguments. |
| 132 var var3, var4; |
| 133 var savedArgs = new SaveMockArguments(); |
| 134 var savedArgs2 = new SaveMockArguments(); |
| 135 mockHandler.expects(once()).testSavedArgs( |
| 136 savedArgs.match(savedArgs2.match(eq(['passedVal1'])))). |
| 137 will(runAllActionsAsync( |
| 138 WhenTestDone.DEFAULT, |
| 139 callFunctionWithSavedArgs(savedArgs, function(args) { |
| 140 var3 = args[0]; |
| 141 }), |
| 142 callFunctionWithSavedArgs(savedArgs2, function(args) { |
| 143 var4 = args[0]; |
| 144 }))); |
| 145 |
| 146 // Receive some saved arguments and some bound arguments. |
| 147 var var5, var6, var7, var8; |
| 148 mockHandler.expects(once()).testMixedArgs( |
| 149 savedArgs.match(savedArgs2.match(eq('passedVal2')))). |
| 150 will(runAllActionsAsync( |
| 151 WhenTestDone.DEFAULT, |
| 152 callFunctionWithSavedArgs( |
| 153 savedArgs, function(passedArgs, boundArgs) { |
| 154 var5 = passedArgs[0]; |
| 155 var6 = boundArgs[0]; |
| 156 }, ['val6']), |
| 157 callFunctionWithSavedArgs( |
| 158 savedArgs2, function(passedArgs, boundArgs) { |
| 159 var7 = passedArgs[0]; |
| 160 var8 = boundArgs[0]; |
| 161 }, ['val8']))); |
| 162 |
| 163 // Send the cases to the mocked handler & tell the C++ handler to continue2. |
| 164 continueTest = this.continueTest(WhenTestDone.ASSERT, function() { |
| 165 chrome.send('testBoundArgs'); |
| 166 chrome.send('testSavedArgs', ['passedVal1']); |
| 167 chrome.send('testMixedArgs', ['passedVal2']); |
| 168 chrome.send('callJS', ['continueTest2']); |
| 169 }); |
| 170 |
| 171 // Check expectations after mocks have been called. |
| 172 continueTest2 = this.continueTest(WhenTestDone.ALWAYS, function() { |
| 173 expectEquals('val1', var1); |
| 174 expectEquals('val2', var2); |
| 175 expectEquals('passedVal1', var3); |
| 176 expectEquals('passedVal1', var4); |
| 177 expectEquals('passedVal2', var5); |
| 178 expectEquals('val6', var6); |
| 179 expectEquals('passedVal2', var7); |
| 180 expectEquals('val8', var8); |
| 181 }); |
| 182 |
| 183 // Kick off the tests asynchronously. |
| 184 chrome.send('callJS', ['continueTest']); |
| 185 }); |
| 186 |
| 187 /** |
| 188 * Set to true when |setTestRanTrue| is called. |
| 189 */ |
| 190 var testRan = false; |
| 191 |
| 192 /** |
| 193 * Set |testRan| to true. |
| 194 */ |
| 195 function setTestRanTrue() { |
| 196 testRan = true; |
| 197 } |
| 198 |
| 199 // Test overriding globals. |
| 200 TEST_F('WebUIBrowserAsyncGenTest', 'TestRegisterMockGlobals', function() { |
| 201 /** |
| 202 * Create a mock class to describe the globals to mock. |
| 203 * @constructor |
| 204 */ |
| 205 function MockGlobals() {} |
| 206 |
| 207 MockGlobals.prototype = { |
| 208 setTestRanTrue: function() {}, |
| 209 }; |
| 210 |
| 211 // Mock the setTestRanTrue global function. |
| 212 var mockGlobals = mock(MockGlobals); |
| 213 registerMockGlobals(mockGlobals, MockGlobals); |
| 214 mockGlobals.expects(once()).setTestRanTrue(). |
| 215 will(runAllActionsAsync( |
| 216 WhenTestDone.ALWAYS, |
| 217 callGlobalWithSavedArgs(null, 'setTestRanTrue'), |
| 218 callFunction(function() { |
| 219 assertTrue(testRan); |
| 220 }))); |
| 221 |
| 222 // Cause setTestRanTrue to be invoked asynchronously. |
| 223 chrome.send('callJS', ['setTestRanTrue']); |
| 224 |
| 225 // In case the global isn't called, call testDone to collect the results. |
| 226 chrome.send('callJS', ['testDone']); |
| 227 }); |
| 228 |
| 229 /** |
| 230 * Will be set to the runTest continuation by the following test fixture. |
| 231 * @type {Function} |
| 232 */ |
| 233 var deferRunTest; |
| 234 |
| 235 /** |
| 236 * Test fixture for testing deferred async tests. |
| 237 * @extends {WebUIBrowserAsyncGenTest} |
| 238 */ |
| 239 function WebUIBrowserAsyncGenDeferredTest() {} |
| 240 |
| 241 WebUIBrowserAsyncGenDeferredTest.prototype = { |
| 242 __proto__: WebUIBrowserAsyncGenTest.prototype, |
| 243 |
| 244 /** @inheritDoc */ |
| 245 typedefCppFixture: 'WebUIBrowserAsyncGenTest', |
| 246 |
| 247 /** |
| 248 * True when runTest is called. |
| 249 * @type {boolean} |
| 250 * @private |
| 251 */ |
| 252 ranTest_: false, |
| 253 |
| 254 /** @inheritDoc */ |
| 255 preLoad: function() { |
| 256 deferRunTest = this.deferRunTest(WhenTestDone.DEFAULT); |
| 257 }, |
| 258 |
| 259 /** @inheritDoc */ |
| 260 setUp: function() { |
| 261 continueTest = this.continueTest(WhenTestDone.DEFAULT, function() { |
| 262 expectFalse(this.ranTest_); |
| 263 chrome.send('callJS', ['deferRunTest']); |
| 264 }); |
| 265 chrome.send('callJS', ['continueTest']); |
| 266 }, |
| 267 |
| 268 /** @inheritDoc */ |
| 269 tearDown: function() { |
| 270 expectTrue(this.ranTest_); |
| 271 WebUIBrowserAsyncGenTest.prototype.tearDown.call(this); |
| 272 }, |
| 273 }; |
| 274 |
| 275 // Test that the test can be deferred appropriately. |
| 276 TEST_F('WebUIBrowserAsyncGenDeferredTest', 'TestDeferRunTest', function() { |
| 277 this.ranTest_ = true; |
| 278 }); |
| 279 |
| 280 /** |
| 281 * Test fixture for testing async tests are deferred until global is called. |
| 282 * @constructor |
| 283 */ |
| 284 function WebUIBrowserAsyncGenDeferredToGlobalTest() {} |
| 285 |
| 286 WebUIBrowserAsyncGenDeferredToGlobalTest.prototype = { |
| 287 __proto__: WebUIBrowserAsyncGenDeferredTest.prototype, |
| 288 |
| 289 /** @inheritDoc */ |
| 290 setUp: function() { |
| 291 /** |
| 292 * Create a mock class to describe the globals to mock. |
| 293 * @constructor |
| 294 */ |
| 295 function MockGlobals() {} |
| 296 |
| 297 MockGlobals.prototype = { |
| 298 setTestRanTrue: function() {}, |
| 299 }; |
| 300 |
| 301 // Mock the setTestRanTrue global function. |
| 302 var mockGlobals = mock(MockGlobals); |
| 303 registerMockGlobals(mockGlobals, MockGlobals); |
| 304 mockGlobals.expects(once()).setTestRanTrue(). |
| 305 will(runAllActionsAsync( |
| 306 WhenTestDone.ALWAYS, |
| 307 callGlobalWithSavedArgs(null, 'setTestRanTrue'), |
| 308 callFunction(deferRunTest))); |
| 309 |
| 310 // Cause setTestRanTrue to be invoked asynchronously. |
| 311 chrome.send('callJS', ['setTestRanTrue']); |
| 312 }, |
| 313 }; |
| 314 |
| 315 TEST_F('WebUIBrowserAsyncGenDeferredToGlobalTest', 'TestDeferRunTestToGlobal', |
| 316 function() { |
| 317 this.ranTest_ = true; |
| 318 assertTrue(testRan); |
| 319 }); |
OLD | NEW |