Debian Codegraph @

closure-library/closure/goog/testing/asynctestcase_noasync_test.js

raw
1 // Copyright 2009 The Closure Library Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS-IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 goog.provide('goog.testing.AsyncTestCaseSyncTest');
16 goog.setTestOnly('goog.testing.AsyncTestCaseSyncTest');
17
18 goog.require('goog.testing.AsyncTestCase');
19 goog.require('goog.testing.jsunit');
20
21 // Has the setUp() function been called.
22 var setUpCalled = false;
23 // Has the current test function completed. This helps us to ensure that the
24 // next test is not started before the previous completed.
25 var curTestIsDone = true;
26 // For restoring it later.
27 var oldTimeout = window.setTimeout;
28 // Use an asynchronous test runner for our tests.
29 var asyncTestCase = goog.testing.AsyncTestCase.createAndInstall(document.title);
30
31
32 /**
33 * Uses window.setTimeout() to perform asynchronous behaviour and uses
34 * asyncTestCase.waitForAsync() and asyncTestCase.continueTesting() to mark
35 * the beginning and end of it.
36 * @param {number} numAsyncCalls The number of asynchronous calls to make.
37 * @param {string} name The name of the current step.
38 */
39 function doAsyncStuff(numAsyncCalls, name) {
40 if (numAsyncCalls > 0) {
41 curTestIsDone = false;
42 asyncTestCase.waitForAsync(
43 'doAsyncStuff-' + name + '(' + numAsyncCalls + ')');
44 window.setTimeout(function() { doAsyncStuff(numAsyncCalls - 1, name); }, 0);
45 } else {
46 curTestIsDone = true;
47 asyncTestCase.continueTesting();
48 }
49 }
50
51 function setUpPage() {
52 debug('setUpPage was called.');
53 // Don't do anything asynchronously.
54 window.setTimeout = function(callback, time) { callback(); };
55 doAsyncStuff(3, 'setUpPage');
56 }
57 function setUp() {
58 assertTrue(curTestIsDone);
59 doAsyncStuff(3, 'setUp');
60 }
61 function tearDown() {
62 assertTrue(curTestIsDone);
63 }
64 function test1() {
65 assertTrue(curTestIsDone);
66 doAsyncStuff(1, 'test1');
67 }
68 function test2() {
69 assertTrue(curTestIsDone);
70 doAsyncStuff(2, 'test2');
71 }
72 function test3() {
73 assertTrue(curTestIsDone);
74 doAsyncStuff(5, 'test3');
75 }
76 var callback = function() {
77 curTestIsDone = true;
78 asyncTestCase.signal();
79 };
80 var doAsyncSignals = function() {
81 curTestIsDone = false;
82 window.setTimeout(callback, 0);
83 };
84 function testSignalsReturn() {
85 doAsyncSignals();
86 doAsyncSignals();
87 doAsyncSignals();
88 asyncTestCase.waitForSignals(3);
89 }
90 function testSignalsCallContinueTestingBeforeFinishing() {
91 doAsyncSignals();
92 asyncTestCase.waitForSignals(2);
93
94 window.setTimeout(function() {
95 var thrown = assertThrows(function() { asyncTestCase.continueTesting(); });
96 assertEquals('Still waiting for 1 signals.', thrown.message);
97 }, 0);
98 doAsyncSignals(); // To not timeout.
99 }
100 function tearDownPage() {
101 debug('tearDownPage was called.');
102 assertTrue(curTestIsDone);
103 window.setTimeout = oldTimeout;
104 }