| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 (function() { | 5 (function() { |
| 6 | 6 |
| 7 'use strict'; | 7 'use strict'; |
| 8 | 8 |
| 9 var originalGlobalPromise = Promise; | 9 var originalGlobalPromise = Promise; |
| 10 | 10 |
| 11 QUnit.module('spy_promise', { | 11 QUnit.module('spy_promise', { |
| 12 beforeEach: function() { | 12 beforeEach: function(/** QUnit.Assert*/ assert) { |
| 13 assertInitialState(); | 13 assertInitialState(assert); |
| 14 base.SpyPromise.reset(); // Defend against broken tests. | 14 base.SpyPromise.reset(); // Defend against broken tests. |
| 15 }, | 15 }, |
| 16 afterEach: function() { | 16 afterEach: function(/** QUnit.Assert*/ assert) { |
| 17 assertInitialState(); | 17 assertInitialState(assert); |
| 18 } | 18 } |
| 19 }); | 19 }); |
| 20 | 20 |
| 21 function assertInitialState() { | 21 function assertInitialState(/** QUnit.Assert */ assert) { |
| 22 QUnit.equal(Promise, originalGlobalPromise); | 22 assert.equal(Promise, originalGlobalPromise); |
| 23 QUnit.ok( | 23 assert.ok( |
| 24 !base.SpyPromise.isSettleAllRunning(), | 24 !base.SpyPromise.isSettleAllRunning(), |
| 25 'settleAll should not be running'); | 25 'settleAll should not be running'); |
| 26 QUnit.equal( | 26 assert.equal( |
| 27 base.SpyPromise.unsettledCount, 0, | 27 base.SpyPromise.unsettledCount, 0, |
| 28 'base.SpyPromise.unsettledCount should be zero ' + | 28 'base.SpyPromise.unsettledCount should be zero ' + |
| 29 'before/after any test finishes'); | 29 'before/after any test finishes'); |
| 30 } | 30 } |
| 31 | 31 |
| 32 /** | 32 /** |
| 33 * @return {!Promise} | 33 * @return {!Promise} |
| 34 */ | 34 */ |
| 35 function finish() { | 35 function finish(/** QUnit.Assert */ assert) { |
| 36 return base.SpyPromise.settleAll().then(function() { | 36 return base.SpyPromise.settleAll().then(function() { |
| 37 QUnit.equal( | 37 assert.equal( |
| 38 base.SpyPromise.unsettledCount, 0, | 38 base.SpyPromise.unsettledCount, 0, |
| 39 'base.SpyPromise.unsettledCount should be zero ' + | 39 'base.SpyPromise.unsettledCount should be zero ' + |
| 40 'after settleAll finishes.'); | 40 'after settleAll finishes.'); |
| 41 }); | 41 }); |
| 42 }; | 42 } |
| 43 | 43 |
| 44 QUnit.test('run', function(/** QUnit.Assert */ assert) { | 44 QUnit.test('run', function(assert) { |
| 45 var done = assert.async(); | 45 var done = assert.async(); |
| 46 QUnit.notEqual(base.SpyPromise, originalGlobalPromise); | 46 assert.notEqual(base.SpyPromise, originalGlobalPromise); |
| 47 return base.SpyPromise.run(function() { | 47 return base.SpyPromise.run(function() { |
| 48 QUnit.equal(Promise, base.SpyPromise); | 48 assert.equal(Promise, base.SpyPromise); |
| 49 QUnit.equal(base.SpyPromise.unsettledCount, 0); | 49 assert.equal(base.SpyPromise.unsettledCount, 0); |
| 50 var dummy1 = new Promise(function(resolve) { resolve(null); }); | 50 var dummy1 = new Promise(function(resolve) { resolve(null); }); |
| 51 QUnit.equal(base.SpyPromise.unsettledCount, 1); | 51 assert.equal(base.SpyPromise.unsettledCount, 1); |
| 52 }).then(function() { | 52 }).then(function() { |
| 53 QUnit.equal(Promise, originalGlobalPromise); | 53 assert.equal(Promise, originalGlobalPromise); |
| 54 QUnit.equal(base.SpyPromise.unsettledCount, 0); | 54 assert.equal(base.SpyPromise.unsettledCount, 0); |
| 55 done(); | 55 done(); |
| 56 }); | 56 }); |
| 57 }); | 57 }); |
| 58 | 58 |
| 59 QUnit.test('activate/restore', function() { | 59 QUnit.test('activate/restore', function(assert) { |
| 60 QUnit.notEqual(base.SpyPromise, originalGlobalPromise); | 60 assert.notEqual(base.SpyPromise, originalGlobalPromise); |
| 61 base.SpyPromise.activate(); | 61 base.SpyPromise.activate(); |
| 62 QUnit.notEqual(base.SpyPromise, originalGlobalPromise); | 62 assert.notEqual(base.SpyPromise, originalGlobalPromise); |
| 63 QUnit.equal(base.SpyPromise.unsettledCount, 0); | 63 assert.equal(base.SpyPromise.unsettledCount, 0); |
| 64 var dummy1 = new Promise(function(resolve) { resolve(null); }); | 64 var dummy1 = new Promise(function(resolve) { resolve(null); }); |
| 65 QUnit.equal(base.SpyPromise.unsettledCount, 1); | 65 assert.equal(base.SpyPromise.unsettledCount, 1); |
| 66 base.SpyPromise.restore(); | 66 base.SpyPromise.restore(); |
| 67 QUnit.equal(Promise, originalGlobalPromise); | 67 assert.equal(Promise, originalGlobalPromise); |
| 68 return finish(); | 68 return finish(assert); |
| 69 }); | 69 }); |
| 70 | 70 |
| 71 QUnit.test('new/then', function(/** QUnit.Assert */ assert) { | 71 QUnit.test('new/then', function(assert) { |
| 72 var done = assert.async(); | 72 var done = assert.async(); |
| 73 new base.SpyPromise(function(resolve, reject) { | 73 new base.SpyPromise(function(resolve, reject) { |
| 74 resolve('hello'); | 74 resolve('hello'); |
| 75 }).then(function(/**string*/ value) { | 75 }).then(function(/**string*/ value) { |
| 76 QUnit.equal(base.SpyPromise.unsettledCount, 0); | 76 assert.equal(base.SpyPromise.unsettledCount, 0); |
| 77 QUnit.equal(value, 'hello'); | 77 assert.equal(value, 'hello'); |
| 78 done(); | 78 done(); |
| 79 }); | 79 }); |
| 80 QUnit.equal(base.SpyPromise.unsettledCount, 1); | 80 assert.equal(base.SpyPromise.unsettledCount, 1); |
| 81 return finish(); | 81 return finish(assert); |
| 82 }); | 82 }); |
| 83 | 83 |
| 84 QUnit.test('new/catch', function(/** QUnit.Assert */ assert) { | 84 QUnit.test('new/catch', function(assert) { |
| 85 var done = assert.async(); | 85 var done = assert.async(); |
| 86 new base.SpyPromise(function(resolve, reject) { | 86 new base.SpyPromise(function(resolve, reject) { |
| 87 reject('hello'); | 87 reject('hello'); |
| 88 }).catch(function(/**string*/ value) { | 88 }).catch(function(/**string*/ value) { |
| 89 QUnit.equal(base.SpyPromise.unsettledCount, 0); | 89 assert.equal(base.SpyPromise.unsettledCount, 0); |
| 90 QUnit.equal(value, 'hello'); | 90 assert.equal(value, 'hello'); |
| 91 done(); | 91 done(); |
| 92 }); | 92 }); |
| 93 QUnit.equal(base.SpyPromise.unsettledCount, 1); | 93 assert.equal(base.SpyPromise.unsettledCount, 1); |
| 94 return finish(); | 94 return finish(assert); |
| 95 }); | 95 }); |
| 96 | 96 |
| 97 QUnit.test('new+throw/catch', function(/** QUnit.Assert */ assert) { | 97 QUnit.test('new+throw/catch', function(assert) { |
| 98 var done = assert.async(); | 98 var done = assert.async(); |
| 99 new base.SpyPromise(function(resolve, reject) { | 99 new base.SpyPromise(function(resolve, reject) { |
| 100 throw 'hello'; | 100 throw 'hello'; |
| 101 }).catch(function(/**string*/ value) { | 101 }).catch(function(/**string*/ value) { |
| 102 QUnit.equal(base.SpyPromise.unsettledCount, 0); | 102 assert.equal(base.SpyPromise.unsettledCount, 0); |
| 103 QUnit.equal(value, 'hello'); | 103 assert.equal(value, 'hello'); |
| 104 done(); | 104 done(); |
| 105 }); | 105 }); |
| 106 QUnit.equal(base.SpyPromise.unsettledCount, 1); | 106 assert.equal(base.SpyPromise.unsettledCount, 1); |
| 107 return finish(); | 107 return finish(assert); |
| 108 }); | 108 }); |
| 109 | 109 |
| 110 QUnit.test('resolve/then', function(/** QUnit.Assert */ assert) { | 110 QUnit.test('resolve/then', function(assert) { |
| 111 var done = assert.async(); | 111 var done = assert.async(); |
| 112 base.SpyPromise.resolve('hello').then(function(/**string*/ value) { | 112 base.SpyPromise.resolve('hello').then(function(/**string*/ value) { |
| 113 QUnit.equal(base.SpyPromise.unsettledCount, 0); | 113 assert.equal(base.SpyPromise.unsettledCount, 0); |
| 114 QUnit.equal(value, 'hello'); | 114 assert.equal(value, 'hello'); |
| 115 done(); | 115 done(); |
| 116 }); | 116 }); |
| 117 QUnit.equal(base.SpyPromise.unsettledCount, 1); | 117 assert.equal(base.SpyPromise.unsettledCount, 1); |
| 118 return finish(); | 118 return finish(assert); |
| 119 }); | 119 }); |
| 120 | 120 |
| 121 QUnit.test('reject/then', function(/** QUnit.Assert */ assert) { | 121 QUnit.test('reject/then', function(assert) { |
| 122 var done = assert.async(); | 122 var done = assert.async(); |
| 123 base.SpyPromise.reject('hello').then(null, function(/**string*/ value) { | 123 base.SpyPromise.reject('hello').then(null, function(/**string*/ value) { |
| 124 QUnit.equal(base.SpyPromise.unsettledCount, 0); | 124 assert.equal(base.SpyPromise.unsettledCount, 0); |
| 125 QUnit.equal(value, 'hello'); | 125 assert.equal(value, 'hello'); |
| 126 done(); | 126 done(); |
| 127 }); | 127 }); |
| 128 QUnit.equal(base.SpyPromise.unsettledCount, 1); | 128 assert.equal(base.SpyPromise.unsettledCount, 1); |
| 129 return finish(); | 129 return finish(assert); |
| 130 }); | 130 }); |
| 131 | 131 |
| 132 QUnit.test('reject/catch', function(/** QUnit.Assert */ assert) { | 132 QUnit.test('reject/catch', function(assert) { |
| 133 var done = assert.async(); | 133 var done = assert.async(); |
| 134 base.SpyPromise.reject('hello').catch(function(/**string*/ value) { | 134 base.SpyPromise.reject('hello').catch(function(/**string*/ value) { |
| 135 QUnit.equal(base.SpyPromise.unsettledCount, 0); | 135 assert.equal(base.SpyPromise.unsettledCount, 0); |
| 136 QUnit.equal(value, 'hello'); | 136 assert.equal(value, 'hello'); |
| 137 done(); | 137 done(); |
| 138 }); | 138 }); |
| 139 QUnit.equal(base.SpyPromise.unsettledCount, 1); | 139 assert.equal(base.SpyPromise.unsettledCount, 1); |
| 140 return finish(); | 140 return finish(assert); |
| 141 }); | 141 }); |
| 142 | 142 |
| 143 QUnit.test('all', function(/** QUnit.Assert */ assert) { | 143 QUnit.test('all', function(assert) { |
| 144 var done = assert.async(); | 144 var done = assert.async(); |
| 145 base.SpyPromise.all([Promise.resolve(1), Promise.resolve(2)]). | 145 base.SpyPromise.all([Promise.resolve(1), Promise.resolve(2)]). |
| 146 then(function(/**string*/ value) { | 146 then(function(/**string*/ value) { |
| 147 QUnit.equal(base.SpyPromise.unsettledCount, 0); | 147 assert.equal(base.SpyPromise.unsettledCount, 0); |
| 148 QUnit.deepEqual(value, [1, 2]); | 148 assert.deepEqual(value, [1, 2]); |
| 149 done(); | 149 done(); |
| 150 }); | 150 }); |
| 151 QUnit.equal(base.SpyPromise.unsettledCount, 1); | 151 assert.equal(base.SpyPromise.unsettledCount, 1); |
| 152 return finish(); | 152 return finish(assert); |
| 153 }); | 153 }); |
| 154 | 154 |
| 155 QUnit.test('race', function(/** QUnit.Assert */ assert) { | 155 QUnit.test('race', function(assert) { |
| 156 var done = assert.async(); | 156 var done = assert.async(); |
| 157 var fast = Promise.resolve('fast'); | 157 var fast = Promise.resolve('fast'); |
| 158 var slow = new Promise(function() {}); // never settled | 158 var slow = new Promise(function() {}); // never settled |
| 159 base.SpyPromise.race([fast, slow]). | 159 base.SpyPromise.race([fast, slow]). |
| 160 then(function(/**string*/ value) { | 160 then(function(/**string*/ value) { |
| 161 QUnit.equal(base.SpyPromise.unsettledCount, 0); | 161 assert.equal(base.SpyPromise.unsettledCount, 0); |
| 162 QUnit.equal(value, 'fast'); | 162 assert.equal(value, 'fast'); |
| 163 done(); | 163 done(); |
| 164 }); | 164 }); |
| 165 QUnit.equal(base.SpyPromise.unsettledCount, 1); | 165 assert.equal(base.SpyPromise.unsettledCount, 1); |
| 166 return finish(); | 166 return finish(assert); |
| 167 }); | 167 }); |
| 168 | 168 |
| 169 QUnit.test('resolve/then/then', function(/** QUnit.Assert */ assert) { | 169 QUnit.test('resolve/then/then', function(assert) { |
| 170 var done = assert.async(); | 170 var done = assert.async(); |
| 171 base.SpyPromise.resolve('hello').then(function(/**string*/ value) { | 171 base.SpyPromise.resolve('hello').then(function(/**string*/ value) { |
| 172 QUnit.equal(value, 'hello'); | 172 assert.equal(value, 'hello'); |
| 173 return 'goodbye'; | 173 return 'goodbye'; |
| 174 }).then(function(/**string*/ value) { | 174 }).then(function(/**string*/ value) { |
| 175 QUnit.equal(value, 'goodbye'); | 175 assert.equal(value, 'goodbye'); |
| 176 done(); | 176 done(); |
| 177 }); | 177 }); |
| 178 return finish(); | 178 return finish(assert); |
| 179 }); | 179 }); |
| 180 | 180 |
| 181 | 181 |
| 182 QUnit.test('resolve/then+throw/catch', function(/** QUnit.Assert */ assert) { | 182 QUnit.test('resolve/then+throw/catch', function(assert) { |
| 183 var done = assert.async(); | 183 var done = assert.async(); |
| 184 base.SpyPromise.resolve('hello').then(function(/**string*/ value) { | 184 base.SpyPromise.resolve('hello').then(function(/**string*/ value) { |
| 185 QUnit.equal(value, 'hello'); | 185 assert.equal(value, 'hello'); |
| 186 throw 'goodbye'; | 186 throw 'goodbye'; |
| 187 }).catch(function(/**string*/ value) { | 187 }).catch(function(/**string*/ value) { |
| 188 QUnit.equal(value, 'goodbye'); | 188 assert.equal(value, 'goodbye'); |
| 189 done(); | 189 done(); |
| 190 }); | 190 }); |
| 191 return finish(); | 191 return finish(assert); |
| 192 }); | 192 }); |
| 193 | 193 |
| 194 QUnit.test('reject/catch/then', function(/** QUnit.Assert */ assert) { | 194 QUnit.test('reject/catch/then', function(assert) { |
| 195 var done = assert.async(); | 195 var done = assert.async(); |
| 196 base.SpyPromise.reject('hello').catch(function(/**string*/ value) { | 196 base.SpyPromise.reject('hello').catch(function(/**string*/ value) { |
| 197 QUnit.equal(value, 'hello'); | 197 assert.equal(value, 'hello'); |
| 198 return 'goodbye'; | 198 return 'goodbye'; |
| 199 }).then(function(/**string*/ value) { | 199 }).then(function(/**string*/ value) { |
| 200 QUnit.equal(value, 'goodbye'); | 200 assert.equal(value, 'goodbye'); |
| 201 done(); | 201 done(); |
| 202 }); | 202 }); |
| 203 return finish(); | 203 return finish(assert); |
| 204 }); | 204 }); |
| 205 | 205 |
| 206 | 206 |
| 207 QUnit.test('reject/catch+throw/catch', function(/** QUnit.Assert */ assert) { | 207 QUnit.test('reject/catch+throw/catch', function(assert) { |
| 208 var done = assert.async(); | 208 var done = assert.async(); |
| 209 base.SpyPromise.reject('hello').catch(function(/**string*/ value) { | 209 base.SpyPromise.reject('hello').catch(function(/**string*/ value) { |
| 210 QUnit.equal(value, 'hello'); | 210 assert.equal(value, 'hello'); |
| 211 throw 'goodbye'; | 211 throw 'goodbye'; |
| 212 }).catch(function(/**string*/ value) { | 212 }).catch(function(/**string*/ value) { |
| 213 QUnit.equal(value, 'goodbye'); | 213 assert.equal(value, 'goodbye'); |
| 214 done(); | 214 done(); |
| 215 }); | 215 }); |
| 216 return finish(); | 216 return finish(assert); |
| 217 }); | 217 }); |
| 218 | 218 |
| 219 QUnit.test('settleAll timeout = 100', function(/** QUnit.Assert */ assert) { | 219 QUnit.test('settleAll timeout = 100', function(assert) { |
| 220 var done = assert.async(); | 220 var done = assert.async(); |
| 221 var startTime = Date.now(); | 221 var startTime = Date.now(); |
| 222 var neverResolved = new base.SpyPromise(function() {}); | 222 var neverResolved = new base.SpyPromise(function() {}); |
| 223 return base.SpyPromise.settleAll(100).catch(function(error) { | 223 return base.SpyPromise.settleAll(100).catch(function(error) { |
| 224 QUnit.ok(error instanceof Error); | 224 assert.ok(error instanceof Error); |
| 225 QUnit.ok(startTime + 200 < Date.now()); | 225 assert.ok(startTime + 200 < Date.now()); |
| 226 done(); | 226 done(); |
| 227 }); | 227 }); |
| 228 }); | 228 }); |
| 229 | 229 |
| 230 QUnit.test('settleAll timeout = 500', function(/** QUnit.Assert */ assert) { | 230 QUnit.test('settleAll timeout = 500', function(assert) { |
| 231 var done = assert.async(); | 231 var done = assert.async(); |
| 232 var startTime = Date.now(); | 232 var startTime = Date.now(); |
| 233 var neverResolved = new base.SpyPromise(function() {}); | 233 var neverResolved = new base.SpyPromise(function() {}); |
| 234 return base.SpyPromise.settleAll(500).catch(function(error) { | 234 return base.SpyPromise.settleAll(500).catch(function(error) { |
| 235 QUnit.ok(startTime + 750 < Date.now()); | 235 assert.ok(startTime + 750 < Date.now()); |
| 236 done(); | 236 done(); |
| 237 }); | 237 }); |
| 238 }); | 238 }); |
| 239 | 239 |
| 240 QUnit.test('settleAll timeout = 1000', function(/** QUnit.Assert */ assert) { | 240 QUnit.test('settleAll timeout = 1000', function(assert) { |
| 241 var done = assert.async(); | 241 var done = assert.async(); |
| 242 var startTime = Date.now(); | 242 var startTime = Date.now(); |
| 243 var neverResolved = new base.SpyPromise(function() {}); | 243 var neverResolved = new base.SpyPromise(function() {}); |
| 244 return base.SpyPromise.settleAll(1000).catch(function(error) { | 244 return base.SpyPromise.settleAll(1000).catch(function(error) { |
| 245 QUnit.ok(startTime + 1500 < Date.now()); | 245 assert.ok(startTime + 1500 < Date.now()); |
| 246 done(); | 246 done(); |
| 247 }); | 247 }); |
| 248 }); | 248 }); |
| 249 | 249 |
| 250 })(); | 250 })(); |
| OLD | NEW |