| 1 |
// Copyright 2013 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.async.runTest'); |
| 16 |
|
| 17 |
goog.require('goog.async.run'); |
| 18 |
goog.require('goog.testing.MockClock'); |
| 19 |
goog.require('goog.testing.jsunit'); |
| 20 |
goog.require('goog.testing.recordFunction'); |
| 21 |
|
| 22 |
goog.setTestOnly('goog.async.runTest'); |
| 23 |
|
| 24 |
|
| 25 |
var mockClock; |
| 26 |
var futureCallback1, futureCallback2; |
| 27 |
|
| 28 |
function setUpPage() { |
| 29 |
mockClock = new goog.testing.MockClock(); |
| 30 |
mockClock.install(); |
| 31 |
} |
| 32 |
|
| 33 |
function setUp() { |
| 34 |
mockClock.reset(); |
| 35 |
futureCallback1 = new goog.testing.recordFunction(); |
| 36 |
futureCallback2 = new goog.testing.recordFunction(); |
| 37 |
} |
| 38 |
|
| 39 |
function tearDown() { |
| 40 |
futureCallback1 = null; |
| 41 |
futureCallback2 = null; |
| 42 |
} |
| 43 |
|
| 44 |
function tearDownPage() { |
| 45 |
mockClock.uninstall(); |
| 46 |
goog.dispose(mockClock); |
| 47 |
} |
| 48 |
|
| 49 |
function testCalledAsync() { |
| 50 |
goog.async.run(futureCallback1); |
| 51 |
goog.async.run(futureCallback2); |
| 52 |
|
| 53 |
assertEquals(0, futureCallback1.getCallCount()); |
| 54 |
assertEquals(0, futureCallback2.getCallCount()); |
| 55 |
|
| 56 |
// but the callbacks are scheduled... |
| 57 |
mockClock.tick(); |
| 58 |
|
| 59 |
// and called. |
| 60 |
assertEquals(1, futureCallback1.getCallCount()); |
| 61 |
assertEquals(1, futureCallback2.getCallCount()); |
| 62 |
|
| 63 |
// and aren't called a second time. |
| 64 |
assertEquals(1, futureCallback1.getCallCount()); |
| 65 |
assertEquals(1, futureCallback2.getCallCount()); |
| 66 |
} |
| 67 |
|
| 68 |
function testSequenceCalledInOrder() { |
| 69 |
futureCallback1 = new goog.testing.recordFunction(function() { |
| 70 |
// called before futureCallback2 |
| 71 |
assertEquals(0, futureCallback2.getCallCount()); |
| 72 |
}); |
| 73 |
futureCallback2 = new goog.testing.recordFunction(function() { |
| 74 |
// called after futureCallback1 |
| 75 |
assertEquals(1, futureCallback1.getCallCount()); |
| 76 |
}); |
| 77 |
goog.async.run(futureCallback1); |
| 78 |
goog.async.run(futureCallback2); |
| 79 |
|
| 80 |
// goog.async.run doesn't call the top callback immediately. |
| 81 |
assertEquals(0, futureCallback1.getCallCount()); |
| 82 |
|
| 83 |
// but the callbacks are scheduled... |
| 84 |
mockClock.tick(); |
| 85 |
|
| 86 |
// and called during the same "tick". |
| 87 |
assertEquals(1, futureCallback1.getCallCount()); |
| 88 |
assertEquals(1, futureCallback2.getCallCount()); |
| 89 |
} |
| 90 |
|
| 91 |
function testSequenceScheduledTwice() { |
| 92 |
goog.async.run(futureCallback1); |
| 93 |
goog.async.run(futureCallback1); |
| 94 |
|
| 95 |
// goog.async.run doesn't call the top callback immediately. |
| 96 |
assertEquals(0, futureCallback1.getCallCount()); |
| 97 |
|
| 98 |
// but the callbacks are scheduled... |
| 99 |
mockClock.tick(); |
| 100 |
|
| 101 |
// and called twice during the same "tick". |
| 102 |
assertEquals(2, futureCallback1.getCallCount()); |
| 103 |
} |
| 104 |
|
| 105 |
function testSequenceCalledSync() { |
| 106 |
futureCallback1 = new goog.testing.recordFunction(function() { |
| 107 |
goog.async.run(futureCallback2); |
| 108 |
// goog.async.run doesn't call the inner callback immediately. |
| 109 |
assertEquals(0, futureCallback2.getCallCount()); |
| 110 |
}); |
| 111 |
goog.async.run(futureCallback1); |
| 112 |
|
| 113 |
// goog.async.run doesn't call the top callback immediately. |
| 114 |
assertEquals(0, futureCallback1.getCallCount()); |
| 115 |
|
| 116 |
// but the callbacks are scheduled... |
| 117 |
mockClock.tick(); |
| 118 |
|
| 119 |
// and called during the same "tick". |
| 120 |
assertEquals(1, futureCallback1.getCallCount()); |
| 121 |
assertEquals(1, futureCallback2.getCallCount()); |
| 122 |
} |
| 123 |
|
| 124 |
function testScope() { |
| 125 |
var aScope = {}; |
| 126 |
goog.async.run(futureCallback1); |
| 127 |
goog.async.run(futureCallback2, aScope); |
| 128 |
|
| 129 |
// the callbacks are scheduled... |
| 130 |
mockClock.tick(); |
| 131 |
|
| 132 |
// and called. |
| 133 |
assertEquals(1, futureCallback1.getCallCount()); |
| 134 |
assertEquals(1, futureCallback2.getCallCount()); |
| 135 |
|
| 136 |
// and get the correct scope. |
| 137 |
var last1 = futureCallback1.popLastCall(); |
| 138 |
assertEquals(0, last1.getArguments().length); |
| 139 |
assertEquals(goog.global, last1.getThis()); |
| 140 |
|
| 141 |
var last2 = futureCallback2.popLastCall(); |
| 142 |
assertEquals(0, last2.getArguments().length); |
| 143 |
assertEquals(aScope, last2.getThis()); |
| 144 |
} |