Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / remoting / webapp / unittests / spy_promise_unittest.js
blob8071248a2042a6e114cc2b1975ed8b9fcf31b63c
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
3 // found in the LICENSE file.
5 (function() {
7 'use strict';
9 var originalGlobalPromise = Promise;
11 QUnit.module('spy_promise', {
12 beforeEach: function(/** QUnit.Assert*/ assert) {
13 assertInitialState(assert);
14 base.SpyPromise.reset(); // Defend against broken tests.
16 afterEach: function(/** QUnit.Assert*/ assert) {
17 assertInitialState(assert);
19 });
21 function assertInitialState(/** QUnit.Assert */ assert) {
22 assert.equal(Promise, originalGlobalPromise);
23 assert.ok(
24 !base.SpyPromise.isSettleAllRunning(),
25 'settleAll should not be running');
26 assert.equal(
27 base.SpyPromise.unsettledCount, 0,
28 'base.SpyPromise.unsettledCount should be zero ' +
29 'before/after any test finishes');
32 /**
33 * @param {!QUnit.Assert} assert
34 * @return {!Promise}
36 function finish(assert) {
37 return base.SpyPromise.settleAll().then(function() {
38 assert.equal(
39 base.SpyPromise.unsettledCount, 0,
40 'base.SpyPromise.unsettledCount should be zero ' +
41 'after settleAll finishes.');
42 });
45 QUnit.test('run', function(assert) {
46 var done = assert.async();
47 assert.notEqual(base.SpyPromise, originalGlobalPromise);
48 return base.SpyPromise.run(function() {
49 assert.equal(Promise, base.SpyPromise);
50 assert.equal(base.SpyPromise.unsettledCount, 0);
51 var dummy1 = new Promise(function(resolve) { resolve(null); });
52 assert.equal(base.SpyPromise.unsettledCount, 1);
53 }).then(function() {
54 assert.equal(Promise, originalGlobalPromise);
55 assert.equal(base.SpyPromise.unsettledCount, 0);
56 done();
57 });
58 });
60 QUnit.test('activate/restore', function(assert) {
61 assert.notEqual(base.SpyPromise, originalGlobalPromise);
62 base.SpyPromise.activate();
63 assert.notEqual(base.SpyPromise, originalGlobalPromise);
64 assert.equal(base.SpyPromise.unsettledCount, 0);
65 var dummy1 = new Promise(function(resolve) { resolve(null); });
66 assert.equal(base.SpyPromise.unsettledCount, 1);
67 base.SpyPromise.restore();
68 assert.equal(Promise, originalGlobalPromise);
69 return finish(assert);
70 });
72 QUnit.test('new/then', function(assert) {
73 var done = assert.async();
74 new base.SpyPromise(function(resolve, reject) {
75 resolve('hello');
76 }).then(function(/**string*/ value) {
77 assert.equal(base.SpyPromise.unsettledCount, 0);
78 assert.equal(value, 'hello');
79 done();
80 });
81 assert.equal(base.SpyPromise.unsettledCount, 1);
82 return finish(assert);
83 });
85 QUnit.test('new/catch', function(assert) {
86 var done = assert.async();
87 new base.SpyPromise(function(resolve, reject) {
88 reject('hello');
89 }).catch(function(/**string*/ value) {
90 assert.equal(base.SpyPromise.unsettledCount, 0);
91 assert.equal(value, 'hello');
92 done();
93 });
94 assert.equal(base.SpyPromise.unsettledCount, 1);
95 return finish(assert);
96 });
98 QUnit.test('new+throw/catch', function(assert) {
99 var done = assert.async();
100 new base.SpyPromise(function(resolve, reject) {
101 throw 'hello';
102 }).catch(function(/**string*/ value) {
103 assert.equal(base.SpyPromise.unsettledCount, 0);
104 assert.equal(value, 'hello');
105 done();
107 assert.equal(base.SpyPromise.unsettledCount, 1);
108 return finish(assert);
111 QUnit.test('resolve/then', function(assert) {
112 var done = assert.async();
113 base.SpyPromise.resolve('hello').then(function(/**string*/ value) {
114 assert.equal(base.SpyPromise.unsettledCount, 0);
115 assert.equal(value, 'hello');
116 done();
118 assert.equal(base.SpyPromise.unsettledCount, 1);
119 return finish(assert);
122 QUnit.test('reject/then', function(assert) {
123 var done = assert.async();
124 base.SpyPromise.reject('hello').then(null, function(/**string*/ value) {
125 assert.equal(base.SpyPromise.unsettledCount, 0);
126 assert.equal(value, 'hello');
127 done();
129 assert.equal(base.SpyPromise.unsettledCount, 1);
130 return finish(assert);
133 QUnit.test('reject/catch', function(assert) {
134 var done = assert.async();
135 base.SpyPromise.reject('hello').catch(function(/**string*/ value) {
136 assert.equal(base.SpyPromise.unsettledCount, 0);
137 assert.equal(value, 'hello');
138 done();
140 assert.equal(base.SpyPromise.unsettledCount, 1);
141 return finish(assert);
144 QUnit.test('all', function(assert) {
145 var done = assert.async();
146 base.SpyPromise.all([Promise.resolve(1), Promise.resolve(2)]).
147 then(
148 /** @param {string} value */
149 function(value) {
150 assert.equal(base.SpyPromise.unsettledCount, 0);
151 assert.deepEqual(value, [1, 2]);
152 done();
154 assert.equal(base.SpyPromise.unsettledCount, 1);
155 return finish(assert);
158 QUnit.test('race', function(assert) {
159 var done = assert.async();
160 var fast = Promise.resolve('fast');
161 var slow = new Promise(function() {}); // never settled
162 base.SpyPromise.race([fast, slow]).
163 then(function(/**string*/ value) {
164 assert.equal(base.SpyPromise.unsettledCount, 0);
165 assert.equal(value, 'fast');
166 done();
168 assert.equal(base.SpyPromise.unsettledCount, 1);
169 return finish(assert);
172 QUnit.test('resolve/then/then', function(assert) {
173 var done = assert.async();
174 base.SpyPromise.resolve('hello').then(function(/**string*/ value) {
175 assert.equal(value, 'hello');
176 return 'goodbye';
177 }).then(function(/**string*/ value) {
178 assert.equal(value, 'goodbye');
179 done();
181 return finish(assert);
185 QUnit.test('resolve/then+throw/catch', function(assert) {
186 var done = assert.async();
187 base.SpyPromise.resolve('hello').then(function(/**string*/ value) {
188 assert.equal(value, 'hello');
189 throw 'goodbye';
190 }).catch(function(/**string*/ value) {
191 assert.equal(value, 'goodbye');
192 done();
194 return finish(assert);
197 QUnit.test('reject/catch/then', function(assert) {
198 var done = assert.async();
199 base.SpyPromise.reject('hello').catch(function(/**string*/ value) {
200 assert.equal(value, 'hello');
201 return 'goodbye';
202 }).then(function(/**string*/ value) {
203 assert.equal(value, 'goodbye');
204 done();
206 return finish(assert);
210 QUnit.test('reject/catch+throw/catch', function(assert) {
211 var done = assert.async();
212 base.SpyPromise.reject('hello').catch(function(/**string*/ value) {
213 assert.equal(value, 'hello');
214 throw 'goodbye';
215 }).catch(function(/**string*/ value) {
216 assert.equal(value, 'goodbye');
217 done();
219 return finish(assert);
222 QUnit.test('settleAll timeout = 100', function(assert) {
223 var done = assert.async();
224 var startTime = Date.now();
225 var neverResolved = new base.SpyPromise(function() {});
226 return base.SpyPromise.settleAll(100).catch(function(error) {
227 assert.ok(error instanceof Error);
228 assert.ok(startTime + 200 < Date.now());
229 done();
233 QUnit.test('settleAll timeout = 500', function(assert) {
234 var done = assert.async();
235 var startTime = Date.now();
236 var neverResolved = new base.SpyPromise(function() {});
237 return base.SpyPromise.settleAll(500).catch(function(error) {
238 assert.ok(startTime + 750 < Date.now());
239 done();
243 QUnit.test('settleAll timeout = 1000', function(assert) {
244 var done = assert.async();
245 var startTime = Date.now();
246 var neverResolved = new base.SpyPromise(function() {});
247 return base.SpyPromise.settleAll(1000).catch(function(error) {
248 assert.ok(startTime + 1500 < Date.now());
249 done();
253 })();