Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | <!-- |
michael@0 | 2 | Any copyright is dedicated to the Public Domain. |
michael@0 | 3 | http://creativecommons.org/publicdomain/zero/1.0/ |
michael@0 | 4 | --> |
michael@0 | 5 | <html> |
michael@0 | 6 | <head> |
michael@0 | 7 | <title>Basic Promise Test</title> |
michael@0 | 8 | <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script> |
michael@0 | 9 | <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/> |
michael@0 | 10 | </head> |
michael@0 | 11 | <body> |
michael@0 | 12 | <p id="display"></p> |
michael@0 | 13 | <div id="content" style="display: none"> |
michael@0 | 14 | |
michael@0 | 15 | </div> |
michael@0 | 16 | <pre id="test"> |
michael@0 | 17 | <script type="application/javascript"><!-- |
michael@0 | 18 | |
michael@0 | 19 | function promiseResolve() { |
michael@0 | 20 | ok(Promise, "Promise object should exist"); |
michael@0 | 21 | |
michael@0 | 22 | var promise = new Promise(function(resolve, reject) { |
michael@0 | 23 | ok(resolve, "Promise.resolve exists"); |
michael@0 | 24 | ok(reject, "Promise.reject exists"); |
michael@0 | 25 | |
michael@0 | 26 | resolve(42); |
michael@0 | 27 | }).then(function(what) { |
michael@0 | 28 | ok(true, "Then - resolveCb has been called"); |
michael@0 | 29 | is(what, 42, "ResolveCb received 42"); |
michael@0 | 30 | runTest(); |
michael@0 | 31 | }, function() { |
michael@0 | 32 | ok(false, "Then - rejectCb has been called"); |
michael@0 | 33 | runTest(); |
michael@0 | 34 | }); |
michael@0 | 35 | } |
michael@0 | 36 | |
michael@0 | 37 | function promiseResolveNoArg() { |
michael@0 | 38 | var promise = new Promise(function(resolve, reject) { |
michael@0 | 39 | ok(resolve, "Promise.resolve exists"); |
michael@0 | 40 | ok(reject, "Promise.reject exists"); |
michael@0 | 41 | |
michael@0 | 42 | resolve(); |
michael@0 | 43 | }).then(function(what) { |
michael@0 | 44 | ok(true, "Then - resolveCb has been called"); |
michael@0 | 45 | is(what, undefined, "ResolveCb received undefined"); |
michael@0 | 46 | runTest(); |
michael@0 | 47 | }, function() { |
michael@0 | 48 | ok(false, "Then - rejectCb has been called"); |
michael@0 | 49 | runTest(); |
michael@0 | 50 | }); |
michael@0 | 51 | } |
michael@0 | 52 | |
michael@0 | 53 | function promiseReject() { |
michael@0 | 54 | var promise = new Promise(function(resolve, reject) { |
michael@0 | 55 | reject(42); |
michael@0 | 56 | }).then(function(what) { |
michael@0 | 57 | ok(false, "Then - resolveCb has been called"); |
michael@0 | 58 | runTest(); |
michael@0 | 59 | }, function(what) { |
michael@0 | 60 | ok(true, "Then - rejectCb has been called"); |
michael@0 | 61 | is(what, 42, "RejectCb received 42"); |
michael@0 | 62 | runTest(); |
michael@0 | 63 | }); |
michael@0 | 64 | } |
michael@0 | 65 | |
michael@0 | 66 | function promiseRejectNoHandler() { |
michael@0 | 67 | // This test only checks that the code that reports unhandled errors in the |
michael@0 | 68 | // Promises implementation does not crash or leak. |
michael@0 | 69 | var promise = new Promise(function(res, rej) { |
michael@0 | 70 | noSuchMethod(); |
michael@0 | 71 | }); |
michael@0 | 72 | runTest(); |
michael@0 | 73 | } |
michael@0 | 74 | |
michael@0 | 75 | function promiseRejectNoArg() { |
michael@0 | 76 | var promise = new Promise(function(resolve, reject) { |
michael@0 | 77 | reject(); |
michael@0 | 78 | }).then(function(what) { |
michael@0 | 79 | ok(false, "Then - resolveCb has been called"); |
michael@0 | 80 | runTest(); |
michael@0 | 81 | }, function(what) { |
michael@0 | 82 | ok(true, "Then - rejectCb has been called"); |
michael@0 | 83 | is(what, undefined, "RejectCb received undefined"); |
michael@0 | 84 | runTest(); |
michael@0 | 85 | }); |
michael@0 | 86 | } |
michael@0 | 87 | |
michael@0 | 88 | function promiseException() { |
michael@0 | 89 | var promise = new Promise(function(resolve, reject) { |
michael@0 | 90 | throw 42; |
michael@0 | 91 | }).then(function(what) { |
michael@0 | 92 | ok(false, "Then - resolveCb has been called"); |
michael@0 | 93 | runTest(); |
michael@0 | 94 | }, function(what) { |
michael@0 | 95 | ok(true, "Then - rejectCb has been called"); |
michael@0 | 96 | is(what, 42, "RejectCb received 42"); |
michael@0 | 97 | runTest(); |
michael@0 | 98 | }); |
michael@0 | 99 | } |
michael@0 | 100 | |
michael@0 | 101 | function promiseGC() { |
michael@0 | 102 | var resolve; |
michael@0 | 103 | var promise = new Promise(function(r1, r2) { |
michael@0 | 104 | resolve = r1; |
michael@0 | 105 | }).then(function(what) { |
michael@0 | 106 | ok(true, "Then - promise is still alive"); |
michael@0 | 107 | runTest(); |
michael@0 | 108 | }); |
michael@0 | 109 | |
michael@0 | 110 | promise = null; |
michael@0 | 111 | |
michael@0 | 112 | SpecialPowers.gc(); |
michael@0 | 113 | SpecialPowers.forceGC(); |
michael@0 | 114 | SpecialPowers.forceCC(); |
michael@0 | 115 | |
michael@0 | 116 | resolve(42); |
michael@0 | 117 | } |
michael@0 | 118 | |
michael@0 | 119 | function promiseAsync() { |
michael@0 | 120 | var global = "foo"; |
michael@0 | 121 | var f = new Promise(function(r1, r2) { |
michael@0 | 122 | is(global, "foo", "Global should be foo"); |
michael@0 | 123 | r1(42); |
michael@0 | 124 | is(global, "foo", "Global should still be foo"); |
michael@0 | 125 | setTimeout(function() { |
michael@0 | 126 | is(global, "bar", "Global should still be bar!"); |
michael@0 | 127 | runTest(); |
michael@0 | 128 | }, 0); |
michael@0 | 129 | }).then(function() { |
michael@0 | 130 | global = "bar"; |
michael@0 | 131 | }); |
michael@0 | 132 | is(global, "foo", "Global should still be foo (2)"); |
michael@0 | 133 | } |
michael@0 | 134 | |
michael@0 | 135 | function promiseDoubleThen() { |
michael@0 | 136 | var steps = 0; |
michael@0 | 137 | var promise = new Promise(function(r1, r2) { |
michael@0 | 138 | r1(42); |
michael@0 | 139 | }); |
michael@0 | 140 | |
michael@0 | 141 | promise.then(function(what) { |
michael@0 | 142 | ok(true, "Then.resolve has been called"); |
michael@0 | 143 | is(what, 42, "Value == 42"); |
michael@0 | 144 | steps++; |
michael@0 | 145 | }, function(what) { |
michael@0 | 146 | ok(false, "Then.reject has been called"); |
michael@0 | 147 | }); |
michael@0 | 148 | |
michael@0 | 149 | promise.then(function(what) { |
michael@0 | 150 | ok(true, "Then.resolve has been called"); |
michael@0 | 151 | is(steps, 1, "Then.resolve - step == 1"); |
michael@0 | 152 | is(what, 42, "Value == 42"); |
michael@0 | 153 | runTest(); |
michael@0 | 154 | }, function(what) { |
michael@0 | 155 | ok(false, "Then.reject has been called"); |
michael@0 | 156 | }); |
michael@0 | 157 | } |
michael@0 | 158 | |
michael@0 | 159 | function promiseThenException() { |
michael@0 | 160 | var promise = new Promise(function(resolve, reject) { |
michael@0 | 161 | resolve(42); |
michael@0 | 162 | }); |
michael@0 | 163 | |
michael@0 | 164 | promise.then(function(what) { |
michael@0 | 165 | ok(true, "Then.resolve has been called"); |
michael@0 | 166 | throw "booh"; |
michael@0 | 167 | }).catch(function(e) { |
michael@0 | 168 | ok(true, "window.onerror has been called!"); |
michael@0 | 169 | runTest(); |
michael@0 | 170 | }); |
michael@0 | 171 | } |
michael@0 | 172 | |
michael@0 | 173 | function promiseThenCatchThen() { |
michael@0 | 174 | var promise = new Promise(function(resolve, reject) { |
michael@0 | 175 | resolve(42); |
michael@0 | 176 | }); |
michael@0 | 177 | |
michael@0 | 178 | var promise2 = promise.then(function(what) { |
michael@0 | 179 | ok(true, "Then.resolve has been called"); |
michael@0 | 180 | is(what, 42, "Value == 42"); |
michael@0 | 181 | return what + 1; |
michael@0 | 182 | }, function(what) { |
michael@0 | 183 | ok(false, "Then.reject has been called"); |
michael@0 | 184 | }); |
michael@0 | 185 | |
michael@0 | 186 | isnot(promise, promise2, "These 2 promise objs are different"); |
michael@0 | 187 | |
michael@0 | 188 | promise2.then(function(what) { |
michael@0 | 189 | ok(true, "Then.resolve has been called"); |
michael@0 | 190 | is(what, 43, "Value == 43"); |
michael@0 | 191 | return what + 1; |
michael@0 | 192 | }, function(what) { |
michael@0 | 193 | ok(false, "Then.reject has been called"); |
michael@0 | 194 | }).catch(function() { |
michael@0 | 195 | ok(false, "Catch has been called"); |
michael@0 | 196 | }).then(function(what) { |
michael@0 | 197 | ok(true, "Then.resolve has been called"); |
michael@0 | 198 | is(what, 44, "Value == 44"); |
michael@0 | 199 | runTest(); |
michael@0 | 200 | }, function(what) { |
michael@0 | 201 | ok(false, "Then.reject has been called"); |
michael@0 | 202 | }); |
michael@0 | 203 | } |
michael@0 | 204 | |
michael@0 | 205 | function promiseThenNoArg() { |
michael@0 | 206 | var promise = new Promise(function(resolve, reject) { |
michael@0 | 207 | resolve(42); |
michael@0 | 208 | }); |
michael@0 | 209 | |
michael@0 | 210 | var clone = promise.then(); |
michael@0 | 211 | isnot(promise, clone, "These 2 promise objs are different"); |
michael@0 | 212 | promise.then(function(v) { |
michael@0 | 213 | clone.then(function(cv) { |
michael@0 | 214 | is(v, cv, "Both resolve to the same value"); |
michael@0 | 215 | runTest(); |
michael@0 | 216 | }); |
michael@0 | 217 | }); |
michael@0 | 218 | } |
michael@0 | 219 | |
michael@0 | 220 | function promiseThenUndefinedResolveFunction() { |
michael@0 | 221 | var promise = new Promise(function(resolve, reject) { |
michael@0 | 222 | reject(42); |
michael@0 | 223 | }); |
michael@0 | 224 | |
michael@0 | 225 | try { |
michael@0 | 226 | promise.then(undefined, function(v) { |
michael@0 | 227 | is(v, 42, "Promise rejected with 42"); |
michael@0 | 228 | runTest(); |
michael@0 | 229 | }); |
michael@0 | 230 | } catch (e) { |
michael@0 | 231 | ok(false, "then should not throw on undefined resolve function"); |
michael@0 | 232 | } |
michael@0 | 233 | } |
michael@0 | 234 | |
michael@0 | 235 | function promiseThenNullResolveFunction() { |
michael@0 | 236 | var promise = new Promise(function(resolve, reject) { |
michael@0 | 237 | reject(42); |
michael@0 | 238 | }); |
michael@0 | 239 | |
michael@0 | 240 | try { |
michael@0 | 241 | promise.then(null, function(v) { |
michael@0 | 242 | is(v, 42, "Promise rejected with 42"); |
michael@0 | 243 | runTest(); |
michael@0 | 244 | }); |
michael@0 | 245 | } catch (e) { |
michael@0 | 246 | ok(false, "then should not throw on null resolve function"); |
michael@0 | 247 | } |
michael@0 | 248 | } |
michael@0 | 249 | |
michael@0 | 250 | function promiseRejectThenCatchThen() { |
michael@0 | 251 | var promise = new Promise(function(resolve, reject) { |
michael@0 | 252 | reject(42); |
michael@0 | 253 | }); |
michael@0 | 254 | |
michael@0 | 255 | var promise2 = promise.then(function(what) { |
michael@0 | 256 | ok(false, "Then.resolve has been called"); |
michael@0 | 257 | }, function(what) { |
michael@0 | 258 | ok(true, "Then.reject has been called"); |
michael@0 | 259 | is(what, 42, "Value == 42"); |
michael@0 | 260 | return what + 1; |
michael@0 | 261 | }); |
michael@0 | 262 | |
michael@0 | 263 | isnot(promise, promise2, "These 2 promise objs are different"); |
michael@0 | 264 | |
michael@0 | 265 | promise2.then(function(what) { |
michael@0 | 266 | ok(true, "Then.resolve has been called"); |
michael@0 | 267 | is(what, 43, "Value == 43"); |
michael@0 | 268 | return what+1; |
michael@0 | 269 | }).catch(function(what) { |
michael@0 | 270 | ok(false, "Catch has been called"); |
michael@0 | 271 | }).then(function(what) { |
michael@0 | 272 | ok(true, "Then.resolve has been called"); |
michael@0 | 273 | is(what, 44, "Value == 44"); |
michael@0 | 274 | runTest(); |
michael@0 | 275 | }); |
michael@0 | 276 | } |
michael@0 | 277 | |
michael@0 | 278 | function promiseRejectThenCatchThen2() { |
michael@0 | 279 | var promise = new Promise(function(resolve, reject) { |
michael@0 | 280 | reject(42); |
michael@0 | 281 | }); |
michael@0 | 282 | |
michael@0 | 283 | promise.then(function(what) { |
michael@0 | 284 | ok(true, "Then.resolve has been called"); |
michael@0 | 285 | is(what, 42, "Value == 42"); |
michael@0 | 286 | return what+1; |
michael@0 | 287 | }).catch(function(what) { |
michael@0 | 288 | is(what, 42, "Value == 42"); |
michael@0 | 289 | ok(true, "Catch has been called"); |
michael@0 | 290 | return what+1; |
michael@0 | 291 | }).then(function(what) { |
michael@0 | 292 | ok(true, "Then.resolve has been called"); |
michael@0 | 293 | is(what, 43, "Value == 43"); |
michael@0 | 294 | runTest(); |
michael@0 | 295 | }); |
michael@0 | 296 | } |
michael@0 | 297 | |
michael@0 | 298 | function promiseRejectThenCatchExceptionThen() { |
michael@0 | 299 | var promise = new Promise(function(resolve, reject) { |
michael@0 | 300 | reject(42); |
michael@0 | 301 | }); |
michael@0 | 302 | |
michael@0 | 303 | promise.then(function(what) { |
michael@0 | 304 | ok(false, "Then.resolve has been called"); |
michael@0 | 305 | }, function(what) { |
michael@0 | 306 | ok(true, "Then.reject has been called"); |
michael@0 | 307 | is(what, 42, "Value == 42"); |
michael@0 | 308 | throw(what + 1); |
michael@0 | 309 | }).catch(function(what) { |
michael@0 | 310 | ok(true, "Catch has been called"); |
michael@0 | 311 | is(what, 43, "Value == 43"); |
michael@0 | 312 | return what + 1; |
michael@0 | 313 | }).then(function(what) { |
michael@0 | 314 | ok(true, "Then.resolve has been called"); |
michael@0 | 315 | is(what, 44, "Value == 44"); |
michael@0 | 316 | runTest(); |
michael@0 | 317 | }); |
michael@0 | 318 | } |
michael@0 | 319 | |
michael@0 | 320 | function promiseThenCatchOrderingResolve() { |
michael@0 | 321 | var global = 0; |
michael@0 | 322 | var f = new Promise(function(r1, r2) { |
michael@0 | 323 | r1(42); |
michael@0 | 324 | }); |
michael@0 | 325 | |
michael@0 | 326 | f.then(function() { |
michael@0 | 327 | f.then(function() { |
michael@0 | 328 | global++; |
michael@0 | 329 | }); |
michael@0 | 330 | f.catch(function() { |
michael@0 | 331 | global++; |
michael@0 | 332 | }); |
michael@0 | 333 | f.then(function() { |
michael@0 | 334 | global++; |
michael@0 | 335 | }); |
michael@0 | 336 | setTimeout(function() { |
michael@0 | 337 | is(global, 2, "Many steps... should return 2"); |
michael@0 | 338 | runTest(); |
michael@0 | 339 | }, 0); |
michael@0 | 340 | }); |
michael@0 | 341 | } |
michael@0 | 342 | |
michael@0 | 343 | function promiseThenCatchOrderingReject() { |
michael@0 | 344 | var global = 0; |
michael@0 | 345 | var f = new Promise(function(r1, r2) { |
michael@0 | 346 | r2(42); |
michael@0 | 347 | }) |
michael@0 | 348 | |
michael@0 | 349 | f.then(function() {}, function() { |
michael@0 | 350 | f.then(function() { |
michael@0 | 351 | global++; |
michael@0 | 352 | }); |
michael@0 | 353 | f.catch(function() { |
michael@0 | 354 | global++; |
michael@0 | 355 | }); |
michael@0 | 356 | f.then(function() {}, function() { |
michael@0 | 357 | global++; |
michael@0 | 358 | }); |
michael@0 | 359 | setTimeout(function() { |
michael@0 | 360 | is(global, 2, "Many steps... should return 2"); |
michael@0 | 361 | runTest(); |
michael@0 | 362 | }, 0); |
michael@0 | 363 | }); |
michael@0 | 364 | } |
michael@0 | 365 | |
michael@0 | 366 | function promiseCatchNoArg() { |
michael@0 | 367 | var promise = new Promise(function(resolve, reject) { |
michael@0 | 368 | reject(42); |
michael@0 | 369 | }); |
michael@0 | 370 | |
michael@0 | 371 | var clone = promise.catch(); |
michael@0 | 372 | isnot(promise, clone, "These 2 promise objs are different"); |
michael@0 | 373 | promise.catch(function(v) { |
michael@0 | 374 | clone.catch(function(cv) { |
michael@0 | 375 | is(v, cv, "Both reject to the same value"); |
michael@0 | 376 | runTest(); |
michael@0 | 377 | }); |
michael@0 | 378 | }); |
michael@0 | 379 | } |
michael@0 | 380 | |
michael@0 | 381 | function promiseNestedPromise() { |
michael@0 | 382 | new Promise(function(resolve, reject) { |
michael@0 | 383 | resolve(new Promise(function(resolve, reject) { |
michael@0 | 384 | ok(true, "Nested promise is executed"); |
michael@0 | 385 | resolve(42); |
michael@0 | 386 | })); |
michael@0 | 387 | }).then(function(value) { |
michael@0 | 388 | is(value, 42, "Nested promise is executed and then == 42"); |
michael@0 | 389 | runTest(); |
michael@0 | 390 | }); |
michael@0 | 391 | } |
michael@0 | 392 | |
michael@0 | 393 | function promiseNestedNestedPromise() { |
michael@0 | 394 | new Promise(function(resolve, reject) { |
michael@0 | 395 | resolve(new Promise(function(resolve, reject) { |
michael@0 | 396 | ok(true, "Nested promise is executed"); |
michael@0 | 397 | resolve(42); |
michael@0 | 398 | }).then(function(what) { return what+1; })); |
michael@0 | 399 | }).then(function(value) { |
michael@0 | 400 | is(value, 43, "Nested promise is executed and then == 43"); |
michael@0 | 401 | runTest(); |
michael@0 | 402 | }); |
michael@0 | 403 | } |
michael@0 | 404 | |
michael@0 | 405 | function promiseWrongNestedPromise() { |
michael@0 | 406 | new Promise(function(resolve, reject) { |
michael@0 | 407 | resolve(new Promise(function(r, r2) { |
michael@0 | 408 | ok(true, "Nested promise is executed"); |
michael@0 | 409 | r(42); |
michael@0 | 410 | })); |
michael@0 | 411 | reject(42); |
michael@0 | 412 | }).then(function(value) { |
michael@0 | 413 | is(value, 42, "Nested promise is executed and then == 42"); |
michael@0 | 414 | runTest(); |
michael@0 | 415 | }, function(value) { |
michael@0 | 416 | ok(false, "This is wrong"); |
michael@0 | 417 | }); |
michael@0 | 418 | } |
michael@0 | 419 | |
michael@0 | 420 | function promiseLoop() { |
michael@0 | 421 | new Promise(function(resolve, reject) { |
michael@0 | 422 | resolve(new Promise(function(r1, r2) { |
michael@0 | 423 | ok(true, "Nested promise is executed"); |
michael@0 | 424 | r1(new Promise(function(r1, r2) { |
michael@0 | 425 | ok(true, "Nested nested promise is executed"); |
michael@0 | 426 | r1(42); |
michael@0 | 427 | })); |
michael@0 | 428 | })); |
michael@0 | 429 | }).then(function(value) { |
michael@0 | 430 | is(value, 42, "Nested nested promise is executed and then == 42"); |
michael@0 | 431 | runTest(); |
michael@0 | 432 | }, function(value) { |
michael@0 | 433 | ok(false, "This is wrong"); |
michael@0 | 434 | }); |
michael@0 | 435 | } |
michael@0 | 436 | |
michael@0 | 437 | function promiseStaticReject() { |
michael@0 | 438 | var promise = Promise.reject(42).then(function(what) { |
michael@0 | 439 | ok(false, "This should not be called"); |
michael@0 | 440 | }, function(what) { |
michael@0 | 441 | is(what, 42, "Value == 42"); |
michael@0 | 442 | runTest(); |
michael@0 | 443 | }); |
michael@0 | 444 | } |
michael@0 | 445 | |
michael@0 | 446 | function promiseStaticResolve() { |
michael@0 | 447 | var promise = Promise.resolve(42).then(function(what) { |
michael@0 | 448 | is(what, 42, "Value == 42"); |
michael@0 | 449 | runTest(); |
michael@0 | 450 | }, function() { |
michael@0 | 451 | ok(false, "This should not be called"); |
michael@0 | 452 | }); |
michael@0 | 453 | } |
michael@0 | 454 | |
michael@0 | 455 | function promiseResolveNestedPromise() { |
michael@0 | 456 | var promise = Promise.resolve(new Promise(function(r, r2) { |
michael@0 | 457 | ok(true, "Nested promise is executed"); |
michael@0 | 458 | r(42); |
michael@0 | 459 | }, function() { |
michael@0 | 460 | ok(false, "This should not be called"); |
michael@0 | 461 | })).then(function(what) { |
michael@0 | 462 | is(what, 42, "Value == 42"); |
michael@0 | 463 | runTest(); |
michael@0 | 464 | }, function() { |
michael@0 | 465 | ok(false, "This should not be called"); |
michael@0 | 466 | }); |
michael@0 | 467 | } |
michael@0 | 468 | |
michael@0 | 469 | function promiseSimpleThenableResolve() { |
michael@0 | 470 | var thenable = { then: function(resolve) { resolve(5); } }; |
michael@0 | 471 | var promise = new Promise(function(resolve, reject) { |
michael@0 | 472 | resolve(thenable); |
michael@0 | 473 | }); |
michael@0 | 474 | |
michael@0 | 475 | promise.then(function(v) { |
michael@0 | 476 | ok(v === 5, "promiseSimpleThenableResolve"); |
michael@0 | 477 | runTest(); |
michael@0 | 478 | }, function(e) { |
michael@0 | 479 | ok(false, "promiseSimpleThenableResolve: Should not reject"); |
michael@0 | 480 | }); |
michael@0 | 481 | } |
michael@0 | 482 | |
michael@0 | 483 | function promiseSimpleThenableReject() { |
michael@0 | 484 | var thenable = { then: function(resolve, reject) { reject(5); } }; |
michael@0 | 485 | var promise = new Promise(function(resolve, reject) { |
michael@0 | 486 | resolve(thenable); |
michael@0 | 487 | }); |
michael@0 | 488 | |
michael@0 | 489 | promise.then(function() { |
michael@0 | 490 | ok(false, "promiseSimpleThenableReject: Should not resolve"); |
michael@0 | 491 | runTest(); |
michael@0 | 492 | }, function(e) { |
michael@0 | 493 | ok(e === 5, "promiseSimpleThenableReject"); |
michael@0 | 494 | runTest(); |
michael@0 | 495 | }); |
michael@0 | 496 | } |
michael@0 | 497 | |
michael@0 | 498 | function promiseThenableThrowsBeforeCallback() { |
michael@0 | 499 | var thenable = { then: function(resolve) { |
michael@0 | 500 | throw new TypeError("Hi there"); |
michael@0 | 501 | resolve(5); |
michael@0 | 502 | }}; |
michael@0 | 503 | |
michael@0 | 504 | var promise = Promise.resolve(thenable); |
michael@0 | 505 | promise.then(function(v) { |
michael@0 | 506 | ok(false, "promiseThenableThrowsBeforeCallback: Should've rejected"); |
michael@0 | 507 | runTest(); |
michael@0 | 508 | }, function(e) { |
michael@0 | 509 | ok(e instanceof TypeError, "promiseThenableThrowsBeforeCallback"); |
michael@0 | 510 | runTest(); |
michael@0 | 511 | }); |
michael@0 | 512 | } |
michael@0 | 513 | |
michael@0 | 514 | function promiseThenableThrowsAfterCallback() { |
michael@0 | 515 | var thenable = { then: function(resolve) { |
michael@0 | 516 | resolve(5); |
michael@0 | 517 | throw new TypeError("Hi there"); |
michael@0 | 518 | }}; |
michael@0 | 519 | |
michael@0 | 520 | var promise = Promise.resolve(thenable); |
michael@0 | 521 | promise.then(function(v) { |
michael@0 | 522 | ok(v === 5, "promiseThenableThrowsAfterCallback"); |
michael@0 | 523 | runTest(); |
michael@0 | 524 | }, function(e) { |
michael@0 | 525 | ok(false, "promiseThenableThrowsAfterCallback: Should've resolved"); |
michael@0 | 526 | runTest(); |
michael@0 | 527 | }); |
michael@0 | 528 | } |
michael@0 | 529 | |
michael@0 | 530 | function promiseThenableRejectThenResolve() { |
michael@0 | 531 | var thenable = { then: function(resolve, reject) { |
michael@0 | 532 | reject(new TypeError("Hi there")); |
michael@0 | 533 | resolve(5); |
michael@0 | 534 | }}; |
michael@0 | 535 | |
michael@0 | 536 | var promise = Promise.resolve(thenable); |
michael@0 | 537 | promise.then(function(v) { |
michael@0 | 538 | ok(false, "promiseThenableRejectThenResolve should have rejected"); |
michael@0 | 539 | runTest(); |
michael@0 | 540 | }, function(e) { |
michael@0 | 541 | ok(e instanceof TypeError, "promiseThenableRejectThenResolve"); |
michael@0 | 542 | runTest(); |
michael@0 | 543 | }); |
michael@0 | 544 | } |
michael@0 | 545 | |
michael@0 | 546 | function promiseWithThenReplaced() { |
michael@0 | 547 | // Ensure that we call the 'then' on the promise and not the internal then. |
michael@0 | 548 | var promise = new Promise(function(resolve, reject) { |
michael@0 | 549 | resolve(5); |
michael@0 | 550 | }); |
michael@0 | 551 | |
michael@0 | 552 | // Rogue `then` always rejects. |
michael@0 | 553 | promise.then = function(onFulfill, onReject) { |
michael@0 | 554 | onReject(new TypeError("Foo")); |
michael@0 | 555 | } |
michael@0 | 556 | |
michael@0 | 557 | var promise2 = Promise.resolve(promise); |
michael@0 | 558 | promise2.then(function(v) { |
michael@0 | 559 | ok(false, "promiseWithThenReplaced: Should've rejected"); |
michael@0 | 560 | runTest(); |
michael@0 | 561 | }, function(e) { |
michael@0 | 562 | ok(e instanceof TypeError, "promiseWithThenReplaced"); |
michael@0 | 563 | runTest(); |
michael@0 | 564 | }); |
michael@0 | 565 | } |
michael@0 | 566 | |
michael@0 | 567 | function promiseStrictHandlers() { |
michael@0 | 568 | var promise = Promise.resolve(5); |
michael@0 | 569 | promise.then(function() { |
michael@0 | 570 | "use strict"; |
michael@0 | 571 | ok(this === undefined, "Strict mode callback should have this === undefined."); |
michael@0 | 572 | runTest(); |
michael@0 | 573 | }); |
michael@0 | 574 | } |
michael@0 | 575 | |
michael@0 | 576 | function promiseStrictExecutorThisArg() { |
michael@0 | 577 | var promise = new Promise(function(resolve, reject) { |
michael@0 | 578 | "use strict"; |
michael@0 | 579 | ok(this === undefined, "thisArg should be undefined."); |
michael@0 | 580 | runTest(); |
michael@0 | 581 | }); |
michael@0 | 582 | } |
michael@0 | 583 | |
michael@0 | 584 | function promiseResolveArray() { |
michael@0 | 585 | var p = Promise.resolve([1,2,3]); |
michael@0 | 586 | ok(p instanceof Promise, "Should return a Promise."); |
michael@0 | 587 | p.then(function(v) { |
michael@0 | 588 | ok(Array.isArray(v), "Resolved value should be an Array"); |
michael@0 | 589 | is(v.length, 3, "Length should match"); |
michael@0 | 590 | is(v[0], 1, "Resolved value should match original"); |
michael@0 | 591 | is(v[1], 2, "Resolved value should match original"); |
michael@0 | 592 | is(v[2], 3, "Resolved value should match original"); |
michael@0 | 593 | runTest(); |
michael@0 | 594 | }); |
michael@0 | 595 | } |
michael@0 | 596 | |
michael@0 | 597 | function promiseResolveThenable() { |
michael@0 | 598 | var p = Promise.resolve({ then: function(onFulfill, onReject) { onFulfill(2); } }); |
michael@0 | 599 | ok(p instanceof Promise, "Should cast to a Promise."); |
michael@0 | 600 | p.then(function(v) { |
michael@0 | 601 | is(v, 2, "Should resolve to 2."); |
michael@0 | 602 | runTest(); |
michael@0 | 603 | }, function(e) { |
michael@0 | 604 | ok(false, "promiseResolveThenable should've resolved"); |
michael@0 | 605 | runTest(); |
michael@0 | 606 | }); |
michael@0 | 607 | } |
michael@0 | 608 | |
michael@0 | 609 | function promiseResolvePromise() { |
michael@0 | 610 | var original = Promise.resolve(true); |
michael@0 | 611 | var cast = Promise.resolve(original); |
michael@0 | 612 | |
michael@0 | 613 | ok(cast instanceof Promise, "Should cast to a Promise."); |
michael@0 | 614 | is(cast, original, "Should return original Promise."); |
michael@0 | 615 | cast.then(function(v) { |
michael@0 | 616 | is(v, true, "Should resolve to true."); |
michael@0 | 617 | runTest(); |
michael@0 | 618 | }); |
michael@0 | 619 | } |
michael@0 | 620 | |
michael@0 | 621 | var tests = [ promiseResolve, promiseReject, |
michael@0 | 622 | promiseException, promiseGC, promiseAsync, |
michael@0 | 623 | promiseDoubleThen, promiseThenException, |
michael@0 | 624 | promiseThenCatchThen, promiseRejectThenCatchThen, |
michael@0 | 625 | promiseRejectThenCatchThen2, |
michael@0 | 626 | promiseRejectThenCatchExceptionThen, |
michael@0 | 627 | promiseThenCatchOrderingResolve, |
michael@0 | 628 | promiseThenCatchOrderingReject, |
michael@0 | 629 | promiseNestedPromise, promiseNestedNestedPromise, |
michael@0 | 630 | promiseWrongNestedPromise, promiseLoop, |
michael@0 | 631 | promiseStaticReject, promiseStaticResolve, |
michael@0 | 632 | promiseResolveNestedPromise, |
michael@0 | 633 | promiseResolveNoArg, |
michael@0 | 634 | promiseRejectNoArg, |
michael@0 | 635 | promiseThenNoArg, |
michael@0 | 636 | promiseThenUndefinedResolveFunction, |
michael@0 | 637 | promiseThenNullResolveFunction, |
michael@0 | 638 | promiseCatchNoArg, |
michael@0 | 639 | promiseRejectNoHandler, |
michael@0 | 640 | promiseSimpleThenableResolve, |
michael@0 | 641 | promiseSimpleThenableReject, |
michael@0 | 642 | promiseThenableThrowsBeforeCallback, |
michael@0 | 643 | promiseThenableThrowsAfterCallback, |
michael@0 | 644 | promiseThenableRejectThenResolve, |
michael@0 | 645 | promiseWithThenReplaced, |
michael@0 | 646 | promiseStrictHandlers, |
michael@0 | 647 | promiseStrictExecutorThisArg, |
michael@0 | 648 | promiseResolveArray, |
michael@0 | 649 | promiseResolveThenable, |
michael@0 | 650 | promiseResolvePromise, |
michael@0 | 651 | ]; |
michael@0 | 652 | |
michael@0 | 653 | function runTest() { |
michael@0 | 654 | if (!tests.length) { |
michael@0 | 655 | SimpleTest.finish(); |
michael@0 | 656 | return; |
michael@0 | 657 | } |
michael@0 | 658 | |
michael@0 | 659 | var test = tests.shift(); |
michael@0 | 660 | test(); |
michael@0 | 661 | } |
michael@0 | 662 | |
michael@0 | 663 | SimpleTest.waitForExplicitFinish(); |
michael@0 | 664 | runTest(); |
michael@0 | 665 | // --> |
michael@0 | 666 | </script> |
michael@0 | 667 | </pre> |
michael@0 | 668 | </body> |
michael@0 | 669 | </html> |
michael@0 | 670 |