Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / test / data / webui / promise_test.html
blob5da0228048179ff11bb76b73977274078b022aed
1 <!DOCTYPE html>
2 <html>
3 <head>
4 <script src="mock_timer.js"></script>
5 </head>
6 <body>
7 <script>
9 var mockTimer;
10 var Promise;
12 function setUp() {
13 mockTimer = new MockTimer();
14 mockTimer.install();
15 Promise = cr.Promise;
18 function tearDown() {
19 mockTimer.uninstall();
22 function testCallbacks() {
23 var calls1 = 0;
24 var calls2 = 0;
25 var V = {};
26 function f1(v) {
27 calls1++;
28 assertEquals(V, v);
30 function f2(v) {
31 calls2++;
32 assertEquals(V, v);
34 var p = new Promise;
35 p.addListener(f1);
36 p.addListener(f2);
37 p.value = V;
38 assertEquals(1, calls1);
39 assertEquals(1, calls2);
42 function testCallbacks2() {
43 var calls1 = 0;
44 var calls2 = 0;
45 var V = {};
46 function f1(v) {
47 calls1++;
48 assertEquals(V, v);
50 function f2(v) {
51 calls2++;
52 assertEquals(V, v);
54 var p = new Promise;
55 p.addListener(f1);
56 p.addListener(f2);
57 p.removeListener(f1);
58 p.value = V;
59 assertEquals(0, calls1);
60 assertEquals(1, calls2);
63 function testCallbacks3() {
64 var calls1 = 0;
65 var calls2 = 0;
66 var V = {};
67 function f1(v) {
68 calls1++;
69 assertEquals(V, v);
71 function f2(v) {
72 calls2++;
73 assertEquals(V, v);
75 var p = new Promise;
76 p.addListener(f1);
77 assertEquals(0, calls1);
78 assertEquals(0, calls2);
79 p.value = V;
80 assertEquals(1, calls1);
81 assertEquals(0, calls2);
82 p.addListener(f2);
83 assertEquals(1, calls1);
84 assertEquals(1, calls2);
87 function testCallbacks4() {
88 var calls1 = 0;
89 var calls2 = 0;
90 var V = {};
91 function f1(v) {
92 calls1++;
93 assertEquals(V, v);
95 function f2(v) {
96 calls2++;
97 assertEquals(V, v);
99 var p = new Promise(V);
100 p.addListener(f1);
101 p.addListener(f2);
102 assertEquals(1, calls1);
103 assertEquals(1, calls2);
106 function testThisInCallback() {
107 var calls = 0;
108 var V = {};
109 function f(v) {
110 calls++;
111 assertEquals(V, v);
112 assertNotEqual(p, this);
114 var p = new Promise;
115 p.addListener(f);
116 p.value = V;
117 assertEquals(1, calls);
120 function testPending() {
121 var p = new Promise;
122 assertEquals(undefined, p.value);
123 assertFalse(p.done);
126 function testValueCanBeUndefined() {
127 var p = new Promise;
128 p.value = undefined;
129 assertEquals(undefined, p.value);
130 assertTrue(p.done);
133 function testDone() {
134 var p = new Promise;
135 assertFalse(p.done);
136 p.value = 42;
137 assertTrue(p.done);
140 function testWhen() {
141 const V = {};
142 var calls = 0;
143 var p = new Promise;
144 p.value = V;
145 Promise.when(p, function(v) {
146 assertEquals(V, v);
147 calls++;
149 assertEquals(1, calls);
152 function testWhen2() {
153 const V = {};
154 var calls = 0;
155 var p = new Promise;
156 Promise.when(p, function(v) {
157 assertEquals(V, v);
158 calls++;
160 p.value = V;
161 assertEquals(1, calls);
164 function testWait() {
165 const S = {};
166 var p = Promise.wait(1000, S);
167 assertFalse(p.done);
168 mockTimer.tick(500);
169 assertFalse(p.done);
170 mockTimer.tick(500);
171 assertTrue(p.done);
172 assertEquals(S, p.value);
175 function testAny() {
176 var p1 = new Promise;
177 var p2 = new Promise;
178 var p3 = new Promise;
180 var any = Promise.any(p1, p2, p3);
181 p2.value = 2;
182 assertEquals(2, any.value);
183 p1.value = 1;
184 assertEquals(2, any.value);
187 function testAll() {
188 var p1 = new Promise;
189 var p2 = new Promise;
190 var p3 = new Promise;
192 var pAll = Promise.all(p1, p2, p3);
193 p1.value = 1;
194 p2.value = 2;
195 p3.value = 3;
196 assertArrayEquals([1, 2, 3], pAll.value);
199 function testAllEmpty() {
200 var pAll = Promise.all();
201 assertArrayEquals([], pAll.value);
204 function testAllAlreadyDone() {
205 var p1 = new Promise(1);
206 var p2 = new Promise(2);
207 var p3 = new Promise(3);
209 var pAll = Promise.all(p1, p2, p3);
210 assertArrayEquals([1, 2, 3], pAll.value);
213 function testEvent() {
214 var p = Promise.event(document.body, 'foo');
215 var e = new Event('foo');
216 document.body.dispatchEvent(e);
217 assertEquals(e, p.value);
220 function testToString() {
221 var p1 = new Promise;
222 assertEquals('[object Promise]', String(p1));
224 var p2 = new Promise;
225 p2.value = 'Hello world';
226 assertEquals('Hello world', String(p2));
229 function testValueOf() {
230 var p = new Promise;
231 p.value = 42;
233 assertTrue(p < 43);
234 assertTrue(p > 41);
235 assertTrue(p == 42);
238 </script>
239 </body>
240 </html>