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
|
<!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: agrieve@google.com (Andrew Grieve)
This tests that the AsyncTestCase can handle asynchronous behaviour in:
setUpPage(),
setUp(),
test*(),
tearDown()
-->
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Closure Unit Tests - goog.testing.AsyncTestCase Asyncronous Tests</title>
<script src="../base.js"></script>
<script type="text/javascript">
goog.require('goog.testing.AsyncTestCase');
goog.require('goog.testing.jsunit');
</script>
</head>
<body>
<script>
// Has the setUp() function been called.
var setUpCalled = false;
// Has the current test function completed. This helps us to ensure that
// the next test is not started before the previous completed.
var curTestIsDone = true;
// Use an asynchronous test runner for our tests.
var asyncTestCase =
goog.testing.AsyncTestCase.createAndInstall(document.title);
/**
* Uses window.setTimeout() to perform asynchronous behaviour and uses
* asyncTestCase.waitForAsync() and asyncTestCase.continueTesting() to mark
* the beginning and end of it.
* @param {number} numAsyncCalls The number of asynchronous calls to make.
* @param {string} name The name of the current step.
*/
function doAsyncStuff(numAsyncCalls, name) {
if (numAsyncCalls > 0) {
curTestIsDone = false;
asyncTestCase.waitForAsync('doAsyncStuff-' + name + '(' + numAsyncCalls
+ ')');
window.setTimeout(function() {
doAsyncStuff(numAsyncCalls - 1, name);
}, 0);
} else {
curTestIsDone = true;
asyncTestCase.continueTesting();
}
}
function setUpPage() {
debug('setUpPage was called.');
doAsyncStuff(3, 'setUpPage');
}
function setUp() {
assertTrue(curTestIsDone);
doAsyncStuff(3, 'setUp');
}
function tearDown() {
assertTrue(curTestIsDone);
}
function test1() {
assertTrue(curTestIsDone);
doAsyncStuff(1, 'test1');
}
function test2_asyncContinueThenWait() {
var activeTest = asyncTestCase.activeTest_;
function async1() {
asyncTestCase.continueTesting();
asyncTestCase.waitForAsync('2');
window.setTimeout(async2, 0);
}
function async2() {
asyncTestCase.continueTesting();
assertEquals('Did not wait for inner waitForAsync',
activeTest,
asyncTestCase.activeTest_);
}
asyncTestCase.waitForAsync('1');
window.setTimeout(async1, 0);
}
function test3() {
assertTrue(curTestIsDone);
doAsyncStuff(2, 'test3');
}
function tearDownPage() {
debug('tearDownPage was called.');
assertTrue(curTestIsDone);
}
</script>
</body>
</html>
|