summaryrefslogtreecommitdiffstats
path: root/chrome/test/data/webui/async_gen.js
blob: d842800dca32a6325368a33a3ab38af0b5374b7b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
// Copyright (c) 2012 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('tearDown');
    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,

  /** @inheritDoc */
  preLoad: function() {
    if (window.preLoadCount === undefined)
      window.preLoadCount = 0;
    assertEquals(0, Number(window.preLoadCount++));
  },
};

// Include the c++ test fixture.
GEN('#include "chrome/test/data/webui/async_gen.h"');

/**
 * 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_F('WebUIBrowserAsyncGenTest', 'TestPreloadOnceOnNavigate', function() {
  window.addEventListener('hashchange', this.continueTest(
      WhenTestDone.DEFAULT, function() {
        testDone();
      }));
  window.location = DUMMY_URL + '#anchor';
});

// 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() {
  this.makeAndRegisterMockHandler(['testBoundArgs',
                                   'testSavedArgs',
                                   'testMixedArgs',
                                   ]);
  // Bind some arguments.
  var var1, var2;
  this.mockHandler.expects(once()).testBoundArgs().
      will(runAllActionsAsync(WhenTestDone.DEFAULT,
                              callFunction(function(args) {
                                var1 = args[0];
                              }, ['val1']),
                              callFunction(function(args) {
                                var2 = args[0];
                              }, ['val2'])));

  // Receive some saved arguments.
  var var3, var4;
  var savedArgs = new SaveMockArguments();
  var savedArgs2 = new SaveMockArguments();
  this.mockHandler.expects(once()).testSavedArgs(
      savedArgs.match(savedArgs2.match(eq(['passedVal1'])))).
      will(runAllActionsAsync(
          WhenTestDone.DEFAULT,
          callFunctionWithSavedArgs(savedArgs, function(args) {
            var3 = args[0];
          }),
          callFunctionWithSavedArgs(savedArgs2, function(args) {
            var4 = args[0];
          })));

  // Receive some saved arguments and some bound arguments.
  var var5, var6, var7, var8;
  this.mockHandler.expects(once()).testMixedArgs(
      savedArgs.match(savedArgs2.match(eq('passedVal2')))).
      will(runAllActionsAsync(
          WhenTestDone.DEFAULT,
          callFunctionWithSavedArgs(
              savedArgs, function(passedArgs, boundArgs) {
                var5 = passedArgs[0];
                var6 = boundArgs[0];
              }, ['val6']),
          callFunctionWithSavedArgs(
              savedArgs2, function(passedArgs, boundArgs) {
                var7 = passedArgs[0];
                var8 = boundArgs[0];
              }, ['val8'])));

  // Send the cases to the mocked handler & tell the C++ handler to continue2.
  continueTest = this.continueTest(WhenTestDone.ASSERT, function() {
    chrome.send('testBoundArgs');
    chrome.send('testSavedArgs', ['passedVal1']);
    chrome.send('testMixedArgs', ['passedVal2']);
    chrome.send('callJS', ['continueTest2']);
  });

  // Check expectations after mocks have been called.
  continueTest2 = this.continueTest(WhenTestDone.ALWAYS, function() {
    expectEquals('val1', var1);
    expectEquals('val2', var2);
    expectEquals('passedVal1', var3);
    expectEquals('passedVal1', var4);
    expectEquals('passedVal2', var5);
    expectEquals('val6', var6);
    expectEquals('passedVal2', var7);
    expectEquals('val8', var8);
  });

  // Kick off the tests asynchronously.
  chrome.send('callJS', ['continueTest']);
});

/**
 * Set to true when |setTestRanTrue| is called.
 */
var testRan = false;

/**
 * Set |testRan| to true.
 */
function setTestRanTrue() {
  testRan = true;
}

// Test overriding globals.
TEST_F('WebUIBrowserAsyncGenTest', 'TestRegisterMockGlobals', function() {
  this.makeAndRegisterMockGlobals(['setTestRanTrue']);

  // Mock the setTestRanTrue global function.
  this.mockGlobals.expects(once()).setTestRanTrue().
      will(runAllActionsAsync(
          WhenTestDone.ALWAYS,
          callGlobalWithSavedArgs(null, 'setTestRanTrue'),
          callFunction(function() {
            assertTrue(testRan);
          })));

  // Cause setTestRanTrue to be invoked asynchronously.
  chrome.send('callJS', ['setTestRanTrue']);

  // In case the global isn't called, call testDone to collect the results.
  chrome.send('callJS', ['testDone']);
});

/**
 * 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 */
  preLoad: function() {
    deferRunTest = this.deferRunTest(WhenTestDone.DEFAULT);
  },

  /** @inheritDoc */
  setUp: function() {
    continueTest = this.continueTest(WhenTestDone.DEFAULT, function() {
      expectFalse(this.ranTest_);
      chrome.send('callJS', ['deferRunTest']);
    });
    chrome.send('callJS', ['continueTest']);
  },

  /** @inheritDoc */
  tearDown: function() {
    expectTrue(this.ranTest_);
    WebUIBrowserAsyncGenTest.prototype.tearDown.call(this);
  },
};

// Test that the test can be deferred appropriately.
TEST_F('WebUIBrowserAsyncGenDeferredTest', 'TestDeferRunTest', function() {
  this.ranTest_ = true;
});

/**
 * Test fixture for testing async tests are deferred until global is called.
 * @constructor
 */
function WebUIBrowserAsyncGenDeferredToGlobalTest() {}

WebUIBrowserAsyncGenDeferredToGlobalTest.prototype = {
  __proto__: WebUIBrowserAsyncGenDeferredTest.prototype,

  /** @inheritDoc */
  setUp: function() {
    this.makeAndRegisterMockGlobals(['setTestRanTrue']);
    this.mockGlobals.expects(once()).setTestRanTrue().
        will(runAllActionsAsync(
            WhenTestDone.ALWAYS,
            callGlobalWithSavedArgs(null, 'setTestRanTrue'),
            callFunction(deferRunTest)));

    // Cause setTestRanTrue to be invoked asynchronously.
    chrome.send('callJS', ['setTestRanTrue']);
  },
};

TEST_F('WebUIBrowserAsyncGenDeferredToGlobalTest', 'TestDeferRunTestToGlobal',
       function() {
  this.ranTest_ = true;
  assertTrue(testRan);
});