Revert of Add button to add new FSP services to Files app. (patchset #8 id:140001...
[chromium-blink-merge.git] / remoting / webapp / unittests / spy_promise_unittest.js
blob099ba09c78c2779dda43733d3ca98e084dfc3f6a
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 * @return {!Promise}
35 function finish(/** QUnit.Assert */ assert) {
36 return base.SpyPromise.settleAll().then(function() {
37 assert.equal(
38 base.SpyPromise.unsettledCount, 0,
39 'base.SpyPromise.unsettledCount should be zero ' +
40 'after settleAll finishes.');
41 });
44 QUnit.test('run', function(assert) {
45 var done = assert.async();
46 assert.notEqual(base.SpyPromise, originalGlobalPromise);
47 return base.SpyPromise.run(function() {
48 assert.equal(Promise, base.SpyPromise);
49 assert.equal(base.SpyPromise.unsettledCount, 0);
50 var dummy1 = new Promise(function(resolve) { resolve(null); });
51 assert.equal(base.SpyPromise.unsettledCount, 1);
52 }).then(function() {
53 assert.equal(Promise, originalGlobalPromise);
54 assert.equal(base.SpyPromise.unsettledCount, 0);
55 done();
56 });
57 });
59 QUnit.test('activate/restore', function(assert) {
60 assert.notEqual(base.SpyPromise, originalGlobalPromise);
61 base.SpyPromise.activate();
62 assert.notEqual(base.SpyPromise, originalGlobalPromise);
63 assert.equal(base.SpyPromise.unsettledCount, 0);
64 var dummy1 = new Promise(function(resolve) { resolve(null); });
65 assert.equal(base.SpyPromise.unsettledCount, 1);
66 base.SpyPromise.restore();
67 assert.equal(Promise, originalGlobalPromise);
68 return finish(assert);
69 });
71 QUnit.test('new/then', function(assert) {
72 var done = assert.async();
73 new base.SpyPromise(function(resolve, reject) {
74 resolve('hello');
75 }).then(function(/**string*/ value) {
76 assert.equal(base.SpyPromise.unsettledCount, 0);
77 assert.equal(value, 'hello');
78 done();
79 });
80 assert.equal(base.SpyPromise.unsettledCount, 1);
81 return finish(assert);
82 });
84 QUnit.test('new/catch', function(assert) {
85 var done = assert.async();
86 new base.SpyPromise(function(resolve, reject) {
87 reject('hello');
88 }).catch(function(/**string*/ value) {
89 assert.equal(base.SpyPromise.unsettledCount, 0);
90 assert.equal(value, 'hello');
91 done();
92 });
93 assert.equal(base.SpyPromise.unsettledCount, 1);
94 return finish(assert);
95 });
97 QUnit.test('new+throw/catch', function(assert) {
98 var done = assert.async();
99 new base.SpyPromise(function(resolve, reject) {
100 throw 'hello';
101 }).catch(function(/**string*/ value) {
102 assert.equal(base.SpyPromise.unsettledCount, 0);
103 assert.equal(value, 'hello');
104 done();
106 assert.equal(base.SpyPromise.unsettledCount, 1);
107 return finish(assert);
110 QUnit.test('resolve/then', function(assert) {
111 var done = assert.async();
112 base.SpyPromise.resolve('hello').then(function(/**string*/ value) {
113 assert.equal(base.SpyPromise.unsettledCount, 0);
114 assert.equal(value, 'hello');
115 done();
117 assert.equal(base.SpyPromise.unsettledCount, 1);
118 return finish(assert);
121 QUnit.test('reject/then', function(assert) {
122 var done = assert.async();
123 base.SpyPromise.reject('hello').then(null, function(/**string*/ value) {
124 assert.equal(base.SpyPromise.unsettledCount, 0);
125 assert.equal(value, 'hello');
126 done();
128 assert.equal(base.SpyPromise.unsettledCount, 1);
129 return finish(assert);
132 QUnit.test('reject/catch', function(assert) {
133 var done = assert.async();
134 base.SpyPromise.reject('hello').catch(function(/**string*/ value) {
135 assert.equal(base.SpyPromise.unsettledCount, 0);
136 assert.equal(value, 'hello');
137 done();
139 assert.equal(base.SpyPromise.unsettledCount, 1);
140 return finish(assert);
143 QUnit.test('all', function(assert) {
144 var done = assert.async();
145 base.SpyPromise.all([Promise.resolve(1), Promise.resolve(2)]).
146 then(function(/**string*/ value) {
147 assert.equal(base.SpyPromise.unsettledCount, 0);
148 assert.deepEqual(value, [1, 2]);
149 done();
151 assert.equal(base.SpyPromise.unsettledCount, 1);
152 return finish(assert);
155 QUnit.test('race', function(assert) {
156 var done = assert.async();
157 var fast = Promise.resolve('fast');
158 var slow = new Promise(function() {}); // never settled
159 base.SpyPromise.race([fast, slow]).
160 then(function(/**string*/ value) {
161 assert.equal(base.SpyPromise.unsettledCount, 0);
162 assert.equal(value, 'fast');
163 done();
165 assert.equal(base.SpyPromise.unsettledCount, 1);
166 return finish(assert);
169 QUnit.test('resolve/then/then', function(assert) {
170 var done = assert.async();
171 base.SpyPromise.resolve('hello').then(function(/**string*/ value) {
172 assert.equal(value, 'hello');
173 return 'goodbye';
174 }).then(function(/**string*/ value) {
175 assert.equal(value, 'goodbye');
176 done();
178 return finish(assert);
182 QUnit.test('resolve/then+throw/catch', function(assert) {
183 var done = assert.async();
184 base.SpyPromise.resolve('hello').then(function(/**string*/ value) {
185 assert.equal(value, 'hello');
186 throw 'goodbye';
187 }).catch(function(/**string*/ value) {
188 assert.equal(value, 'goodbye');
189 done();
191 return finish(assert);
194 QUnit.test('reject/catch/then', function(assert) {
195 var done = assert.async();
196 base.SpyPromise.reject('hello').catch(function(/**string*/ value) {
197 assert.equal(value, 'hello');
198 return 'goodbye';
199 }).then(function(/**string*/ value) {
200 assert.equal(value, 'goodbye');
201 done();
203 return finish(assert);
207 QUnit.test('reject/catch+throw/catch', function(assert) {
208 var done = assert.async();
209 base.SpyPromise.reject('hello').catch(function(/**string*/ value) {
210 assert.equal(value, 'hello');
211 throw 'goodbye';
212 }).catch(function(/**string*/ value) {
213 assert.equal(value, 'goodbye');
214 done();
216 return finish(assert);
219 QUnit.test('settleAll timeout = 100', function(assert) {
220 var done = assert.async();
221 var startTime = Date.now();
222 var neverResolved = new base.SpyPromise(function() {});
223 return base.SpyPromise.settleAll(100).catch(function(error) {
224 assert.ok(error instanceof Error);
225 assert.ok(startTime + 200 < Date.now());
226 done();
230 QUnit.test('settleAll timeout = 500', function(assert) {
231 var done = assert.async();
232 var startTime = Date.now();
233 var neverResolved = new base.SpyPromise(function() {});
234 return base.SpyPromise.settleAll(500).catch(function(error) {
235 assert.ok(startTime + 750 < Date.now());
236 done();
240 QUnit.test('settleAll timeout = 1000', function(assert) {
241 var done = assert.async();
242 var startTime = Date.now();
243 var neverResolved = new base.SpyPromise(function() {});
244 return base.SpyPromise.settleAll(1000).catch(function(error) {
245 assert.ok(startTime + 1500 < Date.now());
246 done();
250 })();