Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(930)

Unified Diff: chrome/test/data/webui/async_gen.js

Issue 7645007: WebUI Testing: async support - global mocking, deferred runs, continued run. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Test runAllActionsAsync with various combinations of arguments. Created 9 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/test/data/webui/async_gen.js
diff --git a/chrome/test/data/webui/async_gen.js b/chrome/test/data/webui/async_gen.js
new file mode 100644
index 0000000000000000000000000000000000000000..ae541227d1ad59004c23e79ea24b66b39726aab1
--- /dev/null
+++ b/chrome/test/data/webui/async_gen.js
@@ -0,0 +1,227 @@
+// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+/**
+ * Test fixture for generated async tests.
+ * @extends {testing.Test}
+ */
+function WebUIBrowserAsyncGenTest() {}
+
+WebUIBrowserAsyncGenTest.prototype = {
+ __proto__: testing.Test.prototype,
+
+ /**
+ * Define the C++ class and include it.
+ * @type {?string}
+ * @override
+ */
+ typedefCppFixture: null,
+
+ /** @inheritDoc */
+ tearDown: function() {
+ expectFalse(this.tornDown);
+ expectFalse(this.running);
+ this.tornDown = true;
+ chrome.send('tornDown');
+ testing.Test.prototype.tearDown.call(this);
+ },
+
+ /** @inheritDoc */
+ browsePreload: DUMMY_URL,
+
+ /** @inheritDoc */
+ isAsync: true,
+
+ /**
+ * True when the tearDown method is called.
+ * @type {boolean}
+ */
+ tornDown: false,
+
+ /**
+ * True when running sync portion of test.
+ * @type {boolean}
+ */
+ running: false,
+};
+
+GEN('#include "chrome/test/data/webui/async_gen-inl.h"');
+GEN('');
+GEN('WebUIBrowserAsyncGenTest::WebUIBrowserAsyncGenTest() {}');
+GEN('WebUIBrowserAsyncGenTest::~WebUIBrowserAsyncGenTest() {}');
+GEN('WebUIBrowserAsyncGenTest::AsyncWebUIMessageHandler::');
+GEN(' AsyncWebUIMessageHandler() {}');
+GEN('WebUIBrowserAsyncGenTest::AsyncWebUIMessageHandler::');
+GEN(' ~AsyncWebUIMessageHandler() {}');
+GEN('');
+
+/**
+ * Will be set to continuation test #1.
+ * @type {Function}
+ * @this {WebUIBrowserAsyncGenTest}
+ */
+var continueTest;
+
+/**
+ * Will be set to continuation test #2.
+ * @type {Function}
+ * @this {WebUIBrowserAsyncGenTest}
+ */
+var continueTest2;
+
+// Test that tearDown isn't called until the callback test runs.
+TEST_F('WebUIBrowserAsyncGenTest', 'TestTearDown', function() {
+ assertFalse(this.tornDown);
+ this.running = true;
+ continueTest = this.continueTest(WhenTestDone.ALWAYS, function() {
+ this.running = false;
+ });
+ chrome.send('callJS', ['continueTest']);
+});
+
+// Test that continuing can be done multiple times and have access to closure
+// variables.
+TEST_F('WebUIBrowserAsyncGenTest', 'TestContinue', function() {
+ var xyz = false;
+ continueTest = this.continueTest(WhenTestDone.DEFAULT, function() {
+ assertFalse(xyz);
+ xyz = true;
+ chrome.send('callJS', ['continueTest2']);
+ });
+ continueTest2 = this.continueTest(WhenTestDone.ALWAYS, function() {
+ assertTrue(xyz);
+ });
+ chrome.send('callJS', ['continueTest']);
+});
+
+// Test that runAllActionsAsync can be called with multiple functions, and with
+// bound, saved, or mixed arguments.
+TEST_F('WebUIBrowserAsyncGenTest', 'TestRunAllActionsAsyncMock', function() {
+ /**
+ * Create a handler class with empty methods to allow mocking to register
+ * expectations and for registration of handlers with chrome.send.
+ * @constructor
+ */
+ function MockHandler() {}
+
+ MockHandler.prototype = {
+ testBoundArgs: function() {},
+ testSavedArgs: function() {},
+ testMixedArgs: function() {},
+ };
+
+ var mockHandler = mock(MockHandler);
+ registerMockMessageCallbacks(mockHandler, MockHandler);
+
+ // Bind some arguments.
+ var var1, var2;
+ mockHandler.expects(once()).testBoundArgs('not used').
+ will(runAllActionsAsync(WhenTestDone.DEFAULT,
+ callFunction(function(val1) {
+ var1 = val1;
+ }, 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
+ callFunction(function(val2) {
+ var2 = val2;
+ }, 456)));
+
+ // Receive some saved arguments.
+ var var3, var4;
+ var savedArgs = new SaveMockArguments();
+ var savedArgs2 = new SaveMockArguments();
+ mockHandler.expects(once()).testSavedArgs(
+ savedArgs.match(savedArgs2.match(eq(10)))).
+ will(runAllActionsAsync(
+ WhenTestDone.DEFAULT,
+ callFunctionWithSavedArgs(savedArgs, function(val3) {
+ var3 = val3;
+ }),
+ callFunctionWithSavedArgs(savedArgs2, function(val4) {
+ var4 = val4;
+ })));
+
+ // Receive some saved arguments and some bound arguments.
+ var var5, var6, var7, var8;
+ mockHandler.expects(once()).testMixedArgs(
+ savedArgs.match(savedArgs2.match(eq(11)))).
+ will(runAllActionsAsync(
+ WhenTestDone.DEFAULT,
+ callFunctionWithSavedArgs(savedArgs, function(val5, val6) {
+ var5 = val5;
+ var6 = val6;
+ }, 78),
+ callFunctionWithSavedArgs(savedArgs2, function(val7, val8) {
+ var7 = val7;
+ var8 = val8;
+ }, 910)));
+
+ // Send the cases to the mocked handler & tell the C++ handler to continue2.
+ continueTest = this.continueTest(WhenTestDone.ASSERT, function() {
+ chrome.send('testBoundArgs', ['not used']);
+ chrome.send('testSavedArgs', [10]);
+ chrome.send('testMixedArgs', [11]);
+ chrome.send('callJS', ['continueTest2']);
+ });
+
+ // Check expectations after mocks have been called.
+ continueTest2 = this.continueTest(WhenTestDone.ALWAYS, function() {
+ expectEquals(123, var1);
+ expectEquals(456, var2);
+ expectEquals(10, var3);
+ expectEquals(10, var4);
+ expectEquals(11, var5);
+ expectEquals(78, var6);
+ expectEquals(11, var7);
+ expectEquals(910, var8);
+ });
+
+ // Kick off the tests asynchronously.
+ chrome.send('callJS', ['continueTest']);
+});
+
+/**
+ * Will be set to the runTest continuation by the following test fixture.
+ * @type {Function}
+ */
+var deferRunTest;
+
+/**
+ * Test fixture for testing deferred async tests.
+ * @extends {WebUIBrowserAsyncGenTest}
+ */
+function WebUIBrowserAsyncGenDeferredTest() {}
+
+WebUIBrowserAsyncGenDeferredTest.prototype = {
+ __proto__: WebUIBrowserAsyncGenTest.prototype,
+
+ /** @inheritDoc */
+ typedefCppFixture: 'WebUIBrowserAsyncGenTest',
+
+ /**
+ * True when runTest is called.
+ * @type {boolean}
+ * @private
+ */
+ ranTest_: false,
+
+ /** @inheritDoc */
+ setUp: function() {
+ deferRunTest = this.deferRunTest(WhenTestDone.DEFAULT);
+ continueTest = this.continueTest(WhenTestDone.DEFAULT, function() {
+ expectFalse(this.ranTest_);
+ chrome.send('callJS', ['deferRunTest']);
+ });
+ chrome.send('callJS', ['continueTest']);
+ },
+
+ /** @inheritDoc */
+ runTest: function() {
+ this.ranTest_ = true;
+ WebUIBrowserAsyncGenTest.prototype.runTest.apply(this, arguments);
+ },
+};
+
+// Test that the test can be deferred appropriately.
+TEST_F('WebUIBrowserAsyncGenDeferredTest', 'TestDeferRunTest', function() {
+ expectTrue(this.ranTest_);
+});

Powered by Google App Engine
This is Rietveld 408576698