aboutsummaryrefslogtreecommitdiff
path: root/contexts/data/lib/closure-library/closure/goog/testing/continuationtestcase_test.html
blob: d273afeda8d74f032be5a7fee476443e8618d899 (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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
<!DOCTYPE html>
<html>
<!--
Copyright 2009 The Closure Library Authors. All Rights Reserved.

Use of this source code is governed by the Apache License, Version 2.0.
See the COPYING file for details.
-->
<!--
Author: brenneman@google.com (Shawn Brenneman)
-->
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Closure Unit Tests - goog.testing.ContinuationTestCase</title>
<script src="../base.js"></script>
<script>
goog.require('goog.events');
goog.require('goog.events.EventTarget');
goog.require('goog.testing.ContinuationTestCase');
goog.require('goog.testing.MockClock');
goog.require('goog.testing.PropertyReplacer');
goog.require('goog.testing.TestCase.Test');
goog.require('goog.testing.jsunit');
</script>
</head>
<body>
<script>
/**
 * @fileoverview This test file uses the ContinuationTestCase to test itself,
 * which is a little confusing. It's also difficult to write a truly effective
 * test, since testing a failure causes an actual failure in the test runner.
 * All tests have been manually verified using a sophisticated combination of
 * alerts and false assertions.
 */

var testCase = new goog.testing.ContinuationTestCase('Continuation Test Case');
testCase.autoDiscoverTests();

// Standalone Closure Test Runner.
if (typeof G_testRunner != 'undefined') {
  G_testRunner.initialize(testCase);
}


var clock = new goog.testing.MockClock();
var count = 0;
var stubs = new goog.testing.PropertyReplacer();


function setUpPage() {
  count = testCase.getCount();
}


/**
 * Resets the mock clock. Includes a wait step to verify that setUp routines
 * can contain continuations.
 */
function setUp() {
  waitForTimeout(function() {
    // Pointless assertion to verify that setUp methods can contain waits.
    assertEquals(count, testCase.getCount());
  }, 0);

  clock.reset();
}


/**
 * Uninstalls the mock clock if it was installed, and restores the Step timeout
 * functions to the default window implementations.
 */
function tearDown() {
  clock.uninstall();
  stubs.reset();

  waitForTimeout(function() {
    // Pointless assertion to verify that tearDown methods can contain waits.
    assertTrue(testCase.now() >= testCase.startTime_);
  }, 0);
}


/**
 * Installs the Mock Clock and replaces the Step timeouts with the mock
 * implementations.
 */
function installMockClock() {
  clock.install();

  // Overwrite the "protected" setTimeout and clearTimeout with the versions
  // replaced by MockClock. Normal tests should never do this, but we need to
  // test the ContinuationTest itself.
  stubs.set(goog.testing.ContinuationTestCase.Step, 'protectedClearTimeout_',
            window.clearTimeout);
  stubs.set(goog.testing.ContinuationTestCase.Step, 'protectedSetTimeout_',
            window.setTimeout);
}


/**
 * @return {goog.testing.ContinuationTestCase.Step} A generic step in a
 *     continuation test.
 */
function getSampleStep() {
  return new goog.testing.ContinuationTestCase.Step('test', function() {});
}


/**
 * @return {goog.testing.ContinuationTestCase.Test} A simple continuation test
 *     with generic setUp, test, and tearDown functions.
 */
function getSampleTest() {
  var setupStep = new goog.testing.TestCase.Test('setup', function() {})
  var testStep = new goog.testing.TestCase.Test('test', function() {})
  var teardownStep = new goog.testing.TestCase.Test('teardown', function() {})

  return new goog.testing.ContinuationTestCase.Test(setupStep,
                                                    testStep,
                                                    teardownStep);
}


function testStepWaiting() {
  var step = getSampleStep();
  assertTrue(step.waiting);
}


function testStepSetTimeout() {
  installMockClock();
  var step = getSampleStep();

  var timeoutReached = false;
  step.setTimeout(function() {timeoutReached = true}, 100);

  clock.tick(50);
  assertFalse(timeoutReached);
  clock.tick(50);
  assertTrue(timeoutReached);
}


function testStepClearTimeout() {
  var step = new goog.testing.ContinuationTestCase.Step('test', function() {});

  var timeoutReached = false;
  step.setTimeout(function() {timeoutReached = true}, 100);

  clock.tick(50);
  assertFalse(timeoutReached);
  step.clearTimeout();
  clock.tick(50);
  assertFalse(timeoutReached);
}


function testTestPhases() {
  var test = getSampleTest();

  assertEquals('setup', test.getCurrentPhase()[0].name);
  test.cancelCurrentPhase();

  assertEquals('test', test.getCurrentPhase()[0].name);
  test.cancelCurrentPhase();

  assertEquals('teardown', test.getCurrentPhase()[0].name);
  test.cancelCurrentPhase();

  assertNull(test.getCurrentPhase());
}


function testTestSetError() {
  var test = getSampleTest();

  var error1 = new Error('Oh noes!');
  var error2 = new Error('B0rken.');

  assertNull(test.getError());
  test.setError(error1);
  assertEquals(error1, test.getError());
  test.setError(error2);
  assertEquals('Once an error has been set, it should not be overwritten.',
               error1, test.getError());
}


function testAddStep() {
  var test = getSampleTest();
  var step = getSampleStep();

  // Try adding a step to each phase and then cancelling the phase.
  for (var i = 0; i < 3; i++) {
    assertEquals(1, test.getCurrentPhase().length);
    test.addStep(step);

    assertEquals(2, test.getCurrentPhase().length);
    assertEquals(step, test.getCurrentPhase()[1]);
    test.cancelCurrentPhase();
  }

  assertNull(test.getCurrentPhase());
}


function testCancelTestPhase() {
  var test = getSampleTest();

  test.cancelTestPhase();
  assertEquals('teardown', test.getCurrentPhase()[0].name);

  test = getSampleTest();
  test.cancelCurrentPhase();
  test.cancelTestPhase();
  assertEquals('teardown', test.getCurrentPhase()[0].name);

  test = getSampleTest();
  test.cancelTestPhase();
  test.cancelTestPhase();
  assertEquals('teardown', test.getCurrentPhase()[0].name);
}


function testWaitForTimeout() {
  var reachedA = false;
  var reachedB = false;
  var reachedC = false;

  waitForTimeout(function a() {
    reachedA = true;

    assertTrue('A must be true at callback a.', reachedA);
    assertFalse('B must be false at callback a.', reachedB);
    assertFalse('C must be false at callback a.', reachedC);
  }, 10);

  waitForTimeout(function b() {
    reachedB = true;

    assertTrue('A must be true at callback b.', reachedA);
    assertTrue('B must be true at callback b.', reachedB);
    assertFalse('C must be false at callback b.', reachedC);
  }, 20);

  waitForTimeout(function c() {
    reachedC = true;

    assertTrue('A must be true at callback c.', reachedA);
    assertTrue('B must be true at callback c.', reachedB);
    assertTrue('C must be true at callback c.', reachedC);
  }, 20);

  assertFalse('a', reachedA);
  assertFalse('b', reachedB);
  assertFalse('c', reachedC);
}


function testWaitForEvent() {
  var et = new goog.events.EventTarget();

  var eventFired = false;
  goog.events.listen(et, 'testPrefire', function() {
    eventFired = true;
    et.dispatchEvent('test');
  });

  waitForEvent(et, 'test', function() {
    assertTrue(eventFired);
  });

  et.dispatchEvent('testPrefire');
}


function testWaitForCondition() {
  var counter = 0;

  waitForCondition(function() {
    return ++counter >= 2;
  }, function() {
    assertEquals(2, counter);
  }, 10, 200);
}


function testOutOfOrderWaits() {
  var counter = 0;

  // Note that if the delta between the timeout is too small, two
  // continuation may be invoked at the same timer tick, using the
  // registration order.
  waitForTimeout(function() {assertEquals(3, ++counter);}, 200);
  waitForTimeout(function() {assertEquals(1, ++counter);}, 0);
  waitForTimeout(function() {assertEquals(2, ++counter);}, 100);
}


/*
 * Any of the test functions below (except the condition check passed into
 * waitForCondition) can raise an assertion successfully. Any level of nested
 * test steps should be possible, in any configuration.
 */

var testObj;


function testCrazyNestedWaitFunction() {
  testObj = {
    lock: true,
    et: new goog.events.EventTarget(),
    steps: 0
  }

  waitForTimeout(handleTimeout, 10);
  waitForEvent(testObj.et, 'test', handleEvent);
  waitForCondition(condition, handleCondition, 1);
}

function handleTimeout() {
  testObj.steps++;
  assertEquals('handleTimeout should be called first.', 1, testObj.steps);
  waitForTimeout(fireEvent, 10);
}

function fireEvent() {
  testObj.steps++;
  assertEquals('fireEvent should be called second.', 2, testObj.steps);
  testObj.et.dispatchEvent('test');
}

function handleEvent() {
  testObj.steps++;
  assertEquals('handleEvent should be called third.', 3, testObj.steps);
  testObj.lock = false;
}

function condition() {
  return !testObj.lock;
}

function handleCondition() {
  assertFalse(testObj.lock);
  testObj.steps++;
  assertEquals('handleCondition should be called last.', 4, testObj.steps);
}

</script>
</body>
</html>