promise_test.js 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165
  1. // Copyright 2013 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. goog.provide('goog.PromiseTest');
  15. goog.require('goog.Promise');
  16. goog.require('goog.Thenable');
  17. goog.require('goog.Timer');
  18. goog.require('goog.functions');
  19. goog.require('goog.testing.MockClock');
  20. goog.require('goog.testing.PropertyReplacer');
  21. goog.require('goog.testing.TestCase');
  22. goog.require('goog.testing.jsunit');
  23. goog.require('goog.testing.recordFunction');
  24. goog.require('goog.userAgent');
  25. goog.setTestOnly('goog.PromiseTest');
  26. function setUpPage() {
  27. goog.testing.TestCase.getActiveTestCase().promiseTimeout = 10000; // 10s
  28. }
  29. // TODO(brenneman):
  30. // - Add tests for interoperability with native Promises where available.
  31. // - Add tests for long stack traces.
  32. var SUPPORTS_ACCESSORS = !!window.Object.defineProperty &&
  33. // IE8 and Safari<5.1 have an Object.defineProperty which does not work on
  34. // some objects.
  35. (!goog.userAgent.IE || goog.userAgent.isVersionOrHigher('9')) &&
  36. (!goog.userAgent.SAFARI || goog.userAgent.isVersionOrHigher('534.48.3'));
  37. var mockClock = new goog.testing.MockClock();
  38. var stubs = new goog.testing.PropertyReplacer();
  39. var unhandledRejections;
  40. // Simple shared objects used as test values.
  41. var dummy = {toString: goog.functions.constant('[object dummy]')};
  42. var sentinel = {toString: goog.functions.constant('[object sentinel]')};
  43. function setUp() {
  44. unhandledRejections = goog.testing.recordFunction();
  45. goog.Promise.setUnhandledRejectionHandler(unhandledRejections);
  46. }
  47. function tearDown() {
  48. // The system should leave no pending unhandled rejections. Advance the mock
  49. // clock (if installed) to catch any rethrows waiting in the queue.
  50. mockClock.tick(Infinity);
  51. mockClock.uninstall();
  52. mockClock.reset();
  53. stubs.reset();
  54. }
  55. /**
  56. * Dummy onfulfilled or onrejected function that should not be called.
  57. *
  58. * @param {*} result The result passed into the callback.
  59. */
  60. function shouldNotCall(result) {
  61. fail('This should not have been called (result: ' + String(result) + ')');
  62. }
  63. function fulfillSoon(value, delay) {
  64. return new goog.Promise(function(resolve, reject) {
  65. window.setTimeout(function() { resolve(value); }, delay);
  66. });
  67. }
  68. function fulfillThenableSoon(value, delay) {
  69. return createThenable(value, delay, true /* fulfilled */);
  70. }
  71. function fulfillBuiltInSoon(value, delay) {
  72. // If the environment does not provide a built-in Promise, then just use
  73. // goog.Promise instead to allow tests which use this to continue.
  74. if (!window.Promise) {
  75. return fulfillSoon(value, delay);
  76. }
  77. return new window.Promise(function(resolve, reject) {
  78. window.setTimeout(function() { resolve(value); }, delay);
  79. });
  80. }
  81. function rejectSoon(reason, delay) {
  82. return new goog.Promise(function(resolve, reject) {
  83. window.setTimeout(function() { reject(reason); }, delay);
  84. });
  85. }
  86. function rejectThenableSoon(value, delay) {
  87. return createThenable(value, delay, false /* fulfilled */);
  88. }
  89. function rejectBuiltInSoon(value, delay) {
  90. // If the environment does not provide a built-in Promise, then just use
  91. // goog.Promise instead to allow tests which use this to continue.
  92. if (!window.Promise) {
  93. return rejectSoon(value, delay);
  94. }
  95. return new window.Promise(function(resolve, reject) {
  96. window.setTimeout(function() { reject(value); }, delay);
  97. });
  98. }
  99. /**
  100. * Creates a thenable which isn't formally a promise for testing non-Promise
  101. * thenables.
  102. */
  103. function createThenableResolver() {
  104. var resolver = goog.Promise.withResolver();
  105. var thenable = {};
  106. var then = function(onFulfilled, onRejected) {
  107. var next = createThenableResolver();
  108. next.resolve(resolver.promise.then(onFulfilled, onRejected));
  109. return next.thenable;
  110. };
  111. // Count accesses of the {@code then} property when possible. Otherwise, just
  112. // define the {@code then} method as a regular data property.
  113. if (SUPPORTS_ACCESSORS) {
  114. thenable.thenAccesses = 0;
  115. window.Object.defineProperty(thenable, 'then', {
  116. get: function() {
  117. thenable.thenAccesses++;
  118. return then;
  119. }
  120. });
  121. } else {
  122. thenable.then = then;
  123. }
  124. return {
  125. resolve: resolver.resolve,
  126. reject: resolver.reject,
  127. thenable: thenable
  128. };
  129. }
  130. /**
  131. * @param {*} value The value the thenable should be fulfilled/rejected with.
  132. * @param {number} delay The length of the delay until the thenable is resolved.
  133. * @param {boolean} fulfill Whether to fulfill or reject the thenable.
  134. * @return {!Thenable}
  135. */
  136. function createThenable(value, delay, fulfill) {
  137. var resolver = createThenableResolver();
  138. window.setTimeout(function() {
  139. if (fulfill) {
  140. resolver.resolve(value);
  141. } else {
  142. resolver.reject(value);
  143. }
  144. }, delay);
  145. return resolver.thenable;
  146. }
  147. /**
  148. * Creates a malicious thenable that throws when the {@code then} method is
  149. * accessed to ensure that it is caught and converted to a rejected promise
  150. * instead of allowed to cause a synchronous exception.
  151. * @param {*} value The value to throw.
  152. * @return {!Thenable}
  153. */
  154. function createThrowingThenable(value) {
  155. // If the environment does not provide Object.defineProperty, then just
  156. // use an immediately rejected promise to allow tests which use this to
  157. // continue.
  158. if (!SUPPORTS_ACCESSORS) {
  159. return rejectThenableSoon(value, 0);
  160. }
  161. var thenable = {};
  162. window.Object.defineProperty(
  163. thenable, 'then', {get: function() { throw value; }});
  164. return thenable;
  165. }
  166. function testThenIsFulfilled() {
  167. var timesCalled = 0;
  168. var p = new goog.Promise(function(resolve, reject) { resolve(sentinel); });
  169. p.then(function(value) {
  170. timesCalled++;
  171. assertEquals(sentinel, value);
  172. });
  173. assertEquals(
  174. 'then() must return before callbacks are invoked.', 0, timesCalled);
  175. return p.then(function() {
  176. assertEquals('onFulfilled must be called exactly once.', 1, timesCalled);
  177. });
  178. }
  179. function testThenVoidIsFulfilled() {
  180. var timesCalled = 0;
  181. var p = goog.Promise.resolve(sentinel);
  182. p.thenVoid(function(value) {
  183. timesCalled++;
  184. assertEquals(sentinel, value);
  185. });
  186. assertEquals(
  187. 'thenVoid() must return before callbacks are invoked.', 0, timesCalled);
  188. return p.then(function() {
  189. assertEquals('onFulfilled must be called exactly once.', 1, timesCalled);
  190. });
  191. }
  192. function testThenIsRejected() {
  193. var timesCalled = 0;
  194. var p = goog.Promise.reject(sentinel);
  195. p.then(shouldNotCall, function(value) {
  196. timesCalled++;
  197. assertEquals(sentinel, value);
  198. });
  199. assertEquals(
  200. 'then() must return before callbacks are invoked.', 0, timesCalled);
  201. return p.then(shouldNotCall, function() {
  202. assertEquals('onRejected must be called exactly once.', 1, timesCalled);
  203. });
  204. }
  205. function testThenVoidIsRejected() {
  206. var timesCalled = 0;
  207. var p = goog.Promise.reject(sentinel);
  208. p.thenVoid(shouldNotCall, function(value) {
  209. timesCalled++;
  210. assertEquals(sentinel, value);
  211. assertEquals('onRejected must be called exactly once.', 1, timesCalled);
  212. });
  213. assertEquals(
  214. 'thenVoid() must return before callbacks are invoked.', 0, timesCalled);
  215. return p.then(shouldNotCall, function() {
  216. assertEquals('onRejected must be called exactly once.', 1, timesCalled);
  217. });
  218. }
  219. function testThenAsserts() {
  220. var p = goog.Promise.resolve();
  221. var m = assertThrows(function() { p.then({}); });
  222. assertContains('opt_onFulfilled should be a function.', m.message);
  223. m = assertThrows(function() { p.then(function() {}, {}); });
  224. assertContains('opt_onRejected should be a function.', m.message);
  225. }
  226. function testThenVoidAsserts() {
  227. var p = goog.Promise.resolve();
  228. var m = assertThrows(function() { p.thenVoid({}); });
  229. assertContains('opt_onFulfilled should be a function.', m.message);
  230. m = assertThrows(function() { p.thenVoid(function() {}, {}); });
  231. assertContains('opt_onRejected should be a function.', m.message);
  232. }
  233. function testOptionalOnFulfilled() {
  234. return goog.Promise.resolve(sentinel)
  235. .then(null, null)
  236. .then(null, shouldNotCall)
  237. .then(function(value) { assertEquals(sentinel, value); });
  238. }
  239. function testOptionalOnRejected() {
  240. return goog.Promise.reject(sentinel)
  241. .then(null, null)
  242. .then(shouldNotCall)
  243. .then(null, function(reason) { assertEquals(sentinel, reason); });
  244. }
  245. function testMultipleResolves() {
  246. var timesCalled = 0;
  247. var resolvePromise;
  248. var p = new goog.Promise(function(resolve, reject) {
  249. resolvePromise = resolve;
  250. resolve('foo');
  251. resolve('bar');
  252. });
  253. p.then(function(value) {
  254. timesCalled++;
  255. assertEquals('onFulfilled must be called exactly once.', 1, timesCalled);
  256. });
  257. // Add one more test for fulfilling after a delay.
  258. return goog.Timer.promise(10).then(function() {
  259. resolvePromise('baz');
  260. assertEquals(1, timesCalled);
  261. });
  262. }
  263. function testMultipleRejects() {
  264. var timesCalled = 0;
  265. var rejectPromise;
  266. var p = new goog.Promise(function(resolve, reject) {
  267. rejectPromise = reject;
  268. reject('foo');
  269. reject('bar');
  270. });
  271. p.then(shouldNotCall, function(value) {
  272. timesCalled++;
  273. assertEquals('onRejected must be called exactly once.', 1, timesCalled);
  274. });
  275. // Add one more test for rejecting after a delay.
  276. return goog.Timer.promise(10).then(function() {
  277. rejectPromise('baz');
  278. assertEquals(1, timesCalled);
  279. });
  280. }
  281. function testAsynchronousThenCalls() {
  282. var timesCalled = [0, 0, 0, 0];
  283. var p = new goog.Promise(function(resolve, reject) {
  284. window.setTimeout(function() { resolve(); }, 30);
  285. });
  286. p.then(function() {
  287. timesCalled[0]++;
  288. assertArrayEquals([1, 0, 0, 0], timesCalled);
  289. });
  290. window.setTimeout(function() {
  291. p.then(function() {
  292. timesCalled[1]++;
  293. assertArrayEquals([1, 1, 0, 0], timesCalled);
  294. });
  295. }, 10);
  296. window.setTimeout(function() {
  297. p.then(function() {
  298. timesCalled[2]++;
  299. assertArrayEquals([1, 1, 1, 0], timesCalled);
  300. });
  301. }, 20);
  302. return goog.Timer.promise(40).then(function() {
  303. return p.then(function() {
  304. timesCalled[3]++;
  305. assertArrayEquals([1, 1, 1, 1], timesCalled);
  306. });
  307. });
  308. }
  309. function testResolveWithPromise() {
  310. var resolveBlocker;
  311. var hasFulfilled = false;
  312. var blocker =
  313. new goog.Promise(function(resolve, reject) { resolveBlocker = resolve; });
  314. var p = goog.Promise.resolve(blocker);
  315. p.then(function(value) {
  316. hasFulfilled = true;
  317. assertEquals(sentinel, value);
  318. }, shouldNotCall);
  319. assertFalse(hasFulfilled);
  320. resolveBlocker(sentinel);
  321. return p.then(function() { assertTrue(hasFulfilled); });
  322. }
  323. function testResolveWithRejectedPromise() {
  324. var rejectBlocker;
  325. var hasRejected = false;
  326. var blocker =
  327. new goog.Promise(function(resolve, reject) { rejectBlocker = reject; });
  328. var p = goog.Promise.resolve(blocker);
  329. var child = p.then(shouldNotCall, function(reason) {
  330. hasRejected = true;
  331. assertEquals(sentinel, reason);
  332. });
  333. assertFalse(hasRejected);
  334. rejectBlocker(sentinel);
  335. return child.thenCatch(function() { assertTrue(hasRejected); });
  336. }
  337. function testRejectWithPromise() {
  338. var resolveBlocker;
  339. var hasFulfilled = false;
  340. var blocker =
  341. new goog.Promise(function(resolve, reject) { resolveBlocker = resolve; });
  342. var p = goog.Promise.reject(blocker);
  343. var child = p.then(function(value) {
  344. hasFulfilled = true;
  345. assertEquals(sentinel, value);
  346. }, shouldNotCall);
  347. assertFalse(hasFulfilled);
  348. resolveBlocker(sentinel);
  349. return child.thenCatch(function() { assertTrue(hasRejected); });
  350. }
  351. function testRejectWithRejectedPromise() {
  352. var rejectBlocker;
  353. var hasRejected = false;
  354. var blocker =
  355. new goog.Promise(function(resolve, reject) { rejectBlocker = reject; });
  356. var p = goog.Promise.reject(blocker);
  357. var child = p.then(shouldNotCall, function(reason) {
  358. hasRejected = true;
  359. assertEquals(sentinel, reason);
  360. });
  361. assertFalse(hasRejected);
  362. rejectBlocker(sentinel);
  363. return child.thenCatch(function() { assertTrue(hasRejected); });
  364. }
  365. function testResolveAndReject() {
  366. var onFulfilledCalled = false;
  367. var onRejectedCalled = false;
  368. var p = new goog.Promise(function(resolve, reject) {
  369. resolve();
  370. reject();
  371. });
  372. p.then(
  373. function() { onFulfilledCalled = true; },
  374. function() { onRejectedCalled = true; });
  375. return p.then(function() {
  376. assertTrue(onFulfilledCalled);
  377. assertFalse(onRejectedCalled);
  378. });
  379. }
  380. function testResolveWithSelfRejects() {
  381. var r;
  382. var p = new goog.Promise(function(resolve) { r = resolve; });
  383. r(p);
  384. return p.then(shouldNotCall, function(e) {
  385. assertEquals(e.message, 'Promise cannot resolve to itself');
  386. });
  387. }
  388. function testResolveWithObjectStringResolves() {
  389. return goog.Promise.resolve('[object Object]').then(function(v) {
  390. assertEquals(v, '[object Object]');
  391. });
  392. }
  393. function testRejectAndResolve() {
  394. return new goog
  395. .Promise(function(resolve, reject) {
  396. reject();
  397. resolve();
  398. })
  399. .then(shouldNotCall, function() { return true; });
  400. }
  401. function testThenReturnsBeforeCallbackWithFulfill() {
  402. var thenHasReturned = false;
  403. var p = goog.Promise.resolve();
  404. var child = p.then(function() {
  405. assertTrue(
  406. 'Callback must be called only after then() has returned.',
  407. thenHasReturned);
  408. });
  409. thenHasReturned = true;
  410. return child;
  411. }
  412. function testThenReturnsBeforeCallbackWithReject() {
  413. var thenHasReturned = false;
  414. var p = goog.Promise.reject();
  415. var child = p.then(shouldNotCall, function() {
  416. assertTrue(
  417. 'Callback must be called only after then() has returned.',
  418. thenHasReturned);
  419. });
  420. thenHasReturned = true;
  421. return child;
  422. }
  423. function testResolutionOrder() {
  424. var callbacks = [];
  425. return goog.Promise.resolve()
  426. .then(function() { callbacks.push(1); }, shouldNotCall)
  427. .then(function() { callbacks.push(2); }, shouldNotCall)
  428. .then(function() { callbacks.push(3); }, shouldNotCall)
  429. .then(function() {
  430. assertArrayEquals([1, 2, 3], callbacks);
  431. });
  432. }
  433. function testResolutionOrderWithThrow() {
  434. var callbacks = [];
  435. var p = goog.Promise.resolve();
  436. p.then(function() { callbacks.push(1); }, shouldNotCall);
  437. var child = p.then(function() {
  438. callbacks.push(2);
  439. throw Error();
  440. }, shouldNotCall);
  441. child.then(shouldNotCall, function() {
  442. // The parent callbacks should be evaluated before the child.
  443. callbacks.push(4);
  444. });
  445. p.then(function() { callbacks.push(3); }, shouldNotCall);
  446. return child.then(shouldNotCall, function() {
  447. callbacks.push(5);
  448. assertArrayEquals([1, 2, 3, 4, 5], callbacks);
  449. });
  450. }
  451. function testResolutionOrderWithNestedThen() {
  452. var resolver = goog.Promise.withResolver();
  453. var callbacks = [];
  454. var p = goog.Promise.resolve();
  455. p.then(function() {
  456. callbacks.push(1);
  457. p.then(function() {
  458. callbacks.push(3);
  459. resolver.resolve();
  460. });
  461. });
  462. p.then(function() { callbacks.push(2); });
  463. return resolver.promise.then(function() {
  464. assertArrayEquals([1, 2, 3], callbacks);
  465. });
  466. }
  467. function testRejectionOrder() {
  468. var callbacks = [];
  469. var p = goog.Promise.reject();
  470. p.then(shouldNotCall, function() { callbacks.push(1); });
  471. p.then(shouldNotCall, function() { callbacks.push(2); });
  472. p.then(shouldNotCall, function() { callbacks.push(3); });
  473. return p.then(shouldNotCall, function() {
  474. assertArrayEquals([1, 2, 3], callbacks);
  475. });
  476. }
  477. function testRejectionOrderWithThrow() {
  478. var callbacks = [];
  479. var p = goog.Promise.reject();
  480. p.then(shouldNotCall, function() { callbacks.push(1); });
  481. p.then(shouldNotCall, function() {
  482. callbacks.push(2);
  483. throw Error();
  484. });
  485. p.then(shouldNotCall, function() { callbacks.push(3); });
  486. return p.then(shouldNotCall, function() {
  487. assertArrayEquals([1, 2, 3], callbacks);
  488. });
  489. }
  490. function testRejectionOrderWithNestedThen() {
  491. var resolver = goog.Promise.withResolver();
  492. var callbacks = [];
  493. var p = goog.Promise.reject();
  494. p.then(shouldNotCall, function() {
  495. callbacks.push(1);
  496. p.then(shouldNotCall, function() {
  497. callbacks.push(3);
  498. resolver.resolve();
  499. });
  500. });
  501. p.then(shouldNotCall, function() { callbacks.push(2); });
  502. return resolver.promise.then(function() {
  503. assertArrayEquals([1, 2, 3], callbacks);
  504. });
  505. }
  506. function testBranching() {
  507. var p = goog.Promise.resolve(2);
  508. var branch1 =
  509. p.then(function(value) {
  510. assertEquals('then functions should see the same value', 2, value);
  511. return value / 2;
  512. }).then(function(value) {
  513. assertEquals('branch should receive the returned value', 1, value);
  514. });
  515. var branch2 =
  516. p.then(function(value) {
  517. assertEquals('then functions should see the same value', 2, value);
  518. throw value + 1;
  519. }).then(shouldNotCall, function(reason) {
  520. assertEquals('branch should receive the thrown value', 3, reason);
  521. });
  522. var branch3 =
  523. p.then(function(value) {
  524. assertEquals('then functions should see the same value', 2, value);
  525. return value * 2;
  526. }).then(function(value) {
  527. assertEquals('branch should receive the returned value', 4, value);
  528. });
  529. return goog.Promise.all([branch1, branch2, branch3]);
  530. }
  531. function testThenReturnsPromise() {
  532. var parent = goog.Promise.resolve();
  533. var child = parent.then();
  534. assertTrue(child instanceof goog.Promise);
  535. assertNotEquals(
  536. 'The returned Promise must be different from the input.', parent, child);
  537. }
  538. function testThenVoidReturnsUndefined() {
  539. var parent = goog.Promise.resolve();
  540. var child = parent.thenVoid();
  541. assertUndefined(child);
  542. }
  543. function testBlockingPromise() {
  544. var p = goog.Promise.resolve();
  545. var wasFulfilled = false;
  546. var wasRejected = false;
  547. var p2 = p.then(function() {
  548. return new goog.Promise(function(resolve, reject) {});
  549. });
  550. p2.then(
  551. function() { wasFulfilled = true; }, function() { wasRejected = true; });
  552. return goog.Timer.promise(10).then(function() {
  553. assertFalse('p2 should be blocked on the returned Promise', wasFulfilled);
  554. assertFalse('p2 should be blocked on the returned Promise', wasRejected);
  555. });
  556. }
  557. function testBlockingPromiseFulfilled() {
  558. var blockingPromise = new goog.Promise(function(resolve, reject) {
  559. window.setTimeout(function() { resolve(sentinel); }, 0);
  560. });
  561. var p = goog.Promise.resolve(dummy);
  562. var p2 = p.then(function(value) { return blockingPromise; });
  563. return p2.then(function(value) { assertEquals(sentinel, value); });
  564. }
  565. function testBlockingPromiseRejected() {
  566. var blockingPromise = new goog.Promise(function(resolve, reject) {
  567. window.setTimeout(function() { reject(sentinel); }, 0);
  568. });
  569. var p = goog.Promise.resolve(blockingPromise);
  570. return p.then(
  571. shouldNotCall, function(reason) { assertEquals(sentinel, reason); });
  572. }
  573. function testBlockingThenableFulfilled() {
  574. var thenable = {then: function(onFulfill, onReject) { onFulfill(sentinel); }};
  575. return goog.Promise.resolve(thenable).then(function(reason) {
  576. assertEquals(sentinel, reason);
  577. });
  578. }
  579. function testBlockingThenableRejected() {
  580. var thenable = {then: function(onFulfill, onReject) { onReject(sentinel); }};
  581. return goog.Promise.resolve(thenable).then(
  582. shouldNotCall, function(reason) { assertEquals(sentinel, reason); });
  583. }
  584. function testBlockingThenableThrows() {
  585. var thenable = {then: function(onFulfill, onReject) { throw sentinel; }};
  586. return goog.Promise.resolve(thenable).then(
  587. shouldNotCall, function(reason) { assertEquals(sentinel, reason); });
  588. }
  589. function testBlockingThenableMisbehaves() {
  590. var thenable = {
  591. then: function(onFulfill, onReject) {
  592. onFulfill(sentinel);
  593. onFulfill(dummy);
  594. onReject(dummy);
  595. throw dummy;
  596. }
  597. };
  598. return goog.Promise.resolve(thenable).then(function(value) {
  599. assertEquals(
  600. 'Only the first resolution of the Thenable should have a result.',
  601. sentinel, value);
  602. });
  603. }
  604. function testNestingThenables() {
  605. var thenableA = {
  606. then: function(onFulfill, onReject) { onFulfill(sentinel); }
  607. };
  608. var thenableB = {
  609. then: function(onFulfill, onReject) { onFulfill(thenableA); }
  610. };
  611. var thenableC = {
  612. then: function(onFulfill, onReject) { onFulfill(thenableB); }
  613. };
  614. return goog.Promise.resolve(thenableC).then(function(value) {
  615. assertEquals(
  616. 'Should resolve to the fulfillment value of thenableA', sentinel,
  617. value);
  618. });
  619. }
  620. function testNestingThenablesRejected() {
  621. var thenableA = {then: function(onFulfill, onReject) { onReject(sentinel); }};
  622. var thenableB = {
  623. then: function(onFulfill, onReject) { onReject(thenableA); }
  624. };
  625. var thenableC = {
  626. then: function(onFulfill, onReject) { onReject(thenableB); }
  627. };
  628. return goog.Promise.reject(thenableC).then(shouldNotCall, function(reason) {
  629. assertEquals(
  630. 'Should resolve to rejection reason of thenableA', sentinel, reason);
  631. });
  632. }
  633. function testThenCatch() {
  634. var catchCalled = false;
  635. return goog.Promise.reject()
  636. .thenCatch(function(reason) {
  637. catchCalled = true;
  638. return sentinel;
  639. })
  640. .then(function(value) {
  641. assertTrue(catchCalled);
  642. assertEquals(sentinel, value);
  643. });
  644. }
  645. function testRaceWithEmptyList() {
  646. return goog.Promise.race([]).then(function(value) {
  647. assertUndefined(value);
  648. });
  649. }
  650. function testRaceWithFulfill() {
  651. var a = fulfillSoon('a', 40);
  652. var b = fulfillSoon('b', 30);
  653. var c = fulfillSoon('c', 10);
  654. var d = fulfillSoon('d', 20);
  655. return goog.Promise.race([a, b, c, d])
  656. .then(function(value) {
  657. assertEquals('c', value);
  658. // Return the slowest input promise to wait for it to complete.
  659. return a;
  660. })
  661. .then(function(value) {
  662. assertEquals(
  663. 'The slowest promise should resolve eventually.', 'a', value);
  664. });
  665. }
  666. function testRaceWithThenables() {
  667. var a = fulfillThenableSoon('a', 40);
  668. var b = fulfillThenableSoon('b', 30);
  669. var c = fulfillThenableSoon('c', 10);
  670. var d = fulfillThenableSoon('d', 20);
  671. return goog.Promise.race([a, b, c, d])
  672. .then(function(value) {
  673. assertEquals('c', value);
  674. // Ensure that the {@code then} property was only accessed once by
  675. // {@code goog.Promise.race}.
  676. if (SUPPORTS_ACCESSORS) {
  677. assertEquals(1, c.thenAccesses);
  678. }
  679. // Return the slowest input thenable to wait for it to complete.
  680. return a;
  681. })
  682. .then(function(value) {
  683. assertEquals(
  684. 'The slowest thenable should resolve eventually.', 'a', value);
  685. });
  686. }
  687. function testRaceWithBuiltIns() {
  688. var a = fulfillBuiltInSoon('a', 40);
  689. var b = fulfillBuiltInSoon('b', 30);
  690. var c = fulfillBuiltInSoon('c', 10);
  691. var d = fulfillBuiltInSoon('d', 20);
  692. return goog.Promise.race([a, b, c, d])
  693. .then(function(value) {
  694. assertEquals('c', value);
  695. // Return the slowest input promise to wait for it to complete.
  696. return a;
  697. })
  698. .then(function(value) {
  699. assertEquals(
  700. 'The slowest promise should resolve eventually.', 'a', value);
  701. });
  702. }
  703. function testRaceWithNonThenable() {
  704. var a = fulfillSoon('a', 40);
  705. var b = 'b';
  706. var c = fulfillSoon('c', 10);
  707. var d = fulfillSoon('d', 20);
  708. return goog.Promise.race([a, b, c, d])
  709. .then(function(value) {
  710. assertEquals('b', value);
  711. // Return the slowest input promise to wait for it to complete.
  712. return a;
  713. })
  714. .then(function(value) {
  715. assertEquals(
  716. 'The slowest promise should resolve eventually.', 'a', value);
  717. });
  718. }
  719. function testRaceWithFalseyNonThenable() {
  720. var a = fulfillSoon('a', 40);
  721. var b = 0;
  722. var c = fulfillSoon('c', 10);
  723. var d = fulfillSoon('d', 20);
  724. return goog.Promise.race([a, b, c, d])
  725. .then(function(value) {
  726. assertEquals(0, value);
  727. // Return the slowest input promise to wait for it to complete.
  728. return a;
  729. })
  730. .then(function(value) {
  731. assertEquals(
  732. 'The slowest promise should resolve eventually.', 'a', value);
  733. });
  734. }
  735. function testRaceWithFulfilledBeforeNonThenable() {
  736. var a = fulfillSoon('a', 40);
  737. var b = goog.Promise.resolve('b');
  738. var c = 'c';
  739. var d = fulfillSoon('d', 20);
  740. return goog.Promise.race([a, b, c, d])
  741. .then(function(value) {
  742. assertEquals('b', value);
  743. // Return the slowest input promise to wait for it to complete.
  744. return a;
  745. })
  746. .then(function(value) {
  747. assertEquals(
  748. 'The slowest promise should resolve eventually.', 'a', value);
  749. });
  750. }
  751. function testRaceWithReject() {
  752. var a = rejectSoon('rejected-a', 40);
  753. var b = rejectSoon('rejected-b', 30);
  754. var c = rejectSoon('rejected-c', 10);
  755. var d = rejectSoon('rejected-d', 20);
  756. return goog.Promise.race([a, b, c, d])
  757. .then(
  758. shouldNotCall,
  759. function(value) {
  760. assertEquals('rejected-c', value);
  761. return a;
  762. })
  763. .then(shouldNotCall, function(reason) {
  764. assertEquals(
  765. 'The slowest promise should resolve eventually.', 'rejected-a',
  766. reason);
  767. });
  768. }
  769. function testRaceWithRejectThenable() {
  770. var a = rejectThenableSoon('rejected-a', 40);
  771. var b = rejectThenableSoon('rejected-b', 30);
  772. var c = rejectThenableSoon('rejected-c', 10);
  773. var d = rejectThenableSoon('rejected-d', 20);
  774. return goog.Promise.race([a, b, c, d])
  775. .then(
  776. shouldNotCall,
  777. function(value) {
  778. assertEquals('rejected-c', value);
  779. return a;
  780. })
  781. .then(shouldNotCall, function(reason) {
  782. assertEquals(
  783. 'The slowest promise should resolve eventually.', 'rejected-a',
  784. reason);
  785. });
  786. }
  787. function testRaceWithRejectBuiltIn() {
  788. var a = rejectBuiltInSoon('rejected-a', 40);
  789. var b = rejectBuiltInSoon('rejected-b', 30);
  790. var c = rejectBuiltInSoon('rejected-c', 10);
  791. var d = rejectBuiltInSoon('rejected-d', 20);
  792. return goog.Promise.race([a, b, c, d])
  793. .then(
  794. shouldNotCall,
  795. function(value) {
  796. assertEquals('rejected-c', value);
  797. return a;
  798. })
  799. .then(shouldNotCall, function(reason) {
  800. assertEquals(
  801. 'The slowest promise should resolve eventually.', 'rejected-a',
  802. reason);
  803. });
  804. }
  805. function testRaceWithRejectAndThrowingThenable() {
  806. var a = rejectSoon('rejected-a', 40);
  807. var b = rejectThenableSoon('rejected-b', 30);
  808. var c = rejectBuiltInSoon('rejected-c', 10);
  809. var d = createThrowingThenable('rejected-d');
  810. return goog.Promise.race([a, b, c, d])
  811. .then(
  812. shouldNotCall,
  813. function(value) {
  814. assertEquals('rejected-d', value);
  815. return a;
  816. })
  817. .then(shouldNotCall, function(reason) {
  818. assertEquals(
  819. 'The slowest promise should resolve eventually.', 'rejected-a',
  820. reason);
  821. });
  822. }
  823. function testAllWithEmptyList() {
  824. return goog.Promise.all([]).then(function(value) {
  825. assertArrayEquals([], value);
  826. });
  827. }
  828. function testAllWithFulfill() {
  829. var a = fulfillSoon('a', 40);
  830. var b = fulfillSoon('b', 30);
  831. var c = fulfillSoon('c', 10);
  832. var d = fulfillSoon('d', 20);
  833. // Test a falsey value.
  834. var z = fulfillSoon(0, 30);
  835. return goog.Promise.all([a, b, c, d, z]).then(function(value) {
  836. assertArrayEquals(['a', 'b', 'c', 'd', 0], value);
  837. });
  838. }
  839. function testAllWithThenable() {
  840. var a = fulfillSoon('a', 40);
  841. var b = fulfillThenableSoon('b', 30);
  842. var c = fulfillSoon('c', 10);
  843. var d = fulfillSoon('d', 20);
  844. return goog.Promise.all([a, b, c, d]).then(function(value) {
  845. assertArrayEquals(['a', 'b', 'c', 'd'], value);
  846. // Ensure that the {@code then} property was only accessed once by
  847. // {@code goog.Promise.all}.
  848. if (SUPPORTS_ACCESSORS) {
  849. assertEquals(1, b.thenAccesses);
  850. }
  851. });
  852. }
  853. function testAllWithBuiltIn() {
  854. var a = fulfillSoon('a', 40);
  855. var b = fulfillBuiltInSoon('b', 30);
  856. var c = fulfillSoon('c', 10);
  857. var d = fulfillSoon('d', 20);
  858. return goog.Promise.all([a, b, c, d]).then(function(value) {
  859. assertArrayEquals(['a', 'b', 'c', 'd'], value);
  860. });
  861. }
  862. function testAllWithNonThenable() {
  863. var a = fulfillSoon('a', 40);
  864. var b = 'b';
  865. var c = fulfillSoon('c', 10);
  866. var d = fulfillSoon('d', 20);
  867. // Test a falsey value.
  868. var z = 0;
  869. return goog.Promise.all([a, b, c, d, z]).then(function(value) {
  870. assertArrayEquals(['a', 'b', 'c', 'd', 0], value);
  871. });
  872. }
  873. function testAllWithReject() {
  874. var a = fulfillSoon('a', 40);
  875. var b = rejectSoon('rejected-b', 30);
  876. var c = fulfillSoon('c', 10);
  877. var d = fulfillSoon('d', 20);
  878. return goog.Promise.all([a, b, c, d])
  879. .then(
  880. shouldNotCall,
  881. function(reason) {
  882. assertEquals('rejected-b', reason);
  883. return a;
  884. })
  885. .then(function(value) {
  886. assertEquals(
  887. 'Promise "a" should be fulfilled even though the all()' +
  888. 'was rejected.',
  889. 'a', value);
  890. });
  891. }
  892. function testAllSettledWithEmptyList() {
  893. return goog.Promise.allSettled([]).then(function(results) {
  894. assertArrayEquals([], results);
  895. });
  896. }
  897. function testAllSettledWithFulfillAndReject() {
  898. var a = fulfillSoon('a', 40);
  899. var b = rejectSoon('rejected-b', 30);
  900. var c = 'c';
  901. var d = rejectBuiltInSoon('rejected-d', 20);
  902. var e = fulfillThenableSoon('e', 40);
  903. var f = fulfillBuiltInSoon('f', 30);
  904. var g = rejectThenableSoon('rejected-g', 10);
  905. var h = createThrowingThenable('rejected-h');
  906. // Test a falsey value.
  907. var z = 0;
  908. return goog.Promise.allSettled([a, b, c, d, e, f, g, h, z])
  909. .then(function(results) {
  910. assertArrayEquals(
  911. [
  912. {fulfilled: true, value: 'a'},
  913. {fulfilled: false, reason: 'rejected-b'},
  914. {fulfilled: true, value: 'c'},
  915. {fulfilled: false, reason: 'rejected-d'},
  916. {fulfilled: true, value: 'e'}, {fulfilled: true, value: 'f'},
  917. {fulfilled: false, reason: 'rejected-g'},
  918. {fulfilled: false, reason: 'rejected-h'},
  919. {fulfilled: true, value: 0}
  920. ],
  921. results);
  922. // Ensure that the {@code then} property was only accessed once by
  923. // {@code goog.Promise.allSettled}.
  924. if (SUPPORTS_ACCESSORS) {
  925. assertEquals(1, e.thenAccesses);
  926. assertEquals(1, g.thenAccesses);
  927. }
  928. });
  929. }
  930. function testFirstFulfilledWithEmptyList() {
  931. return goog.Promise.firstFulfilled([]).then(function(value) {
  932. assertUndefined(value);
  933. });
  934. }
  935. function testFirstFulfilledWithFulfill() {
  936. var a = fulfillSoon('a', 40);
  937. var b = rejectSoon('rejected-b', 30);
  938. var c = rejectSoon('rejected-c', 10);
  939. var d = fulfillSoon('d', 20);
  940. return goog.Promise.firstFulfilled([a, b, c, d])
  941. .then(function(value) {
  942. assertEquals('d', value);
  943. return c;
  944. })
  945. .then(
  946. shouldNotCall,
  947. function(reason) {
  948. assertEquals(
  949. 'Promise "c" should be rejected before firstFulfilled() resolves.',
  950. 'rejected-c', reason);
  951. return a;
  952. })
  953. .then(function(value) {
  954. assertEquals(
  955. 'Promise "a" should be fulfilled after firstFulfilled() resolves.',
  956. 'a', value);
  957. });
  958. }
  959. function testFirstFulfilledWithThenables() {
  960. var a = fulfillThenableSoon('a', 40);
  961. var b = rejectThenableSoon('rejected-b', 30);
  962. var c = rejectThenableSoon('rejected-c', 10);
  963. var d = fulfillThenableSoon('d', 20);
  964. return goog.Promise.firstFulfilled([a, b, c, d])
  965. .then(function(value) {
  966. assertEquals('d', value);
  967. // Ensure that the {@code then} property was only accessed once by
  968. // {@code goog.Promise.firstFulfilled}.
  969. if (SUPPORTS_ACCESSORS) {
  970. assertEquals(1, d.thenAccesses);
  971. }
  972. return c;
  973. })
  974. .then(
  975. shouldNotCall,
  976. function(reason) {
  977. assertEquals(
  978. 'Thenable "c" should be rejected before firstFulfilled() resolves.',
  979. 'rejected-c', reason);
  980. return a;
  981. })
  982. .then(function(value) {
  983. assertEquals(
  984. 'Thenable "a" should be fulfilled after firstFulfilled() resolves.',
  985. 'a', value);
  986. });
  987. }
  988. function testFirstFulfilledWithBuiltIns() {
  989. var a = fulfillBuiltInSoon('a', 40);
  990. var b = rejectBuiltInSoon('rejected-b', 30);
  991. var c = rejectBuiltInSoon('rejected-c', 10);
  992. var d = fulfillBuiltInSoon('d', 20);
  993. return goog.Promise.firstFulfilled([a, b, c, d])
  994. .then(function(value) {
  995. assertEquals('d', value);
  996. return c;
  997. })
  998. .then(
  999. shouldNotCall,
  1000. function(reason) {
  1001. assertEquals(
  1002. 'Promise "c" should be rejected before firstFulfilled() resolves.',
  1003. 'rejected-c', reason);
  1004. return a;
  1005. })
  1006. .then(function(value) {
  1007. assertEquals(
  1008. 'Promise "a" should be fulfilled after firstFulfilled() resolves.',
  1009. 'a', value);
  1010. });
  1011. }
  1012. function testFirstFulfilledWithNonThenable() {
  1013. var a = fulfillSoon('a', 40);
  1014. var b = rejectSoon('rejected-b', 30);
  1015. var c = rejectSoon('rejected-c', 10);
  1016. var d = 'd';
  1017. return goog.Promise.firstFulfilled([a, b, c, d])
  1018. .then(function(value) {
  1019. assertEquals('d', value);
  1020. // Return the slowest input promise to wait for it to complete.
  1021. return a;
  1022. })
  1023. .then(function(value) {
  1024. assertEquals(
  1025. 'The slowest promise should resolve eventually.', 'a', value);
  1026. });
  1027. }
  1028. function testFirstFulfilledWithFalseyNonThenable() {
  1029. var a = fulfillSoon('a', 40);
  1030. var b = rejectSoon('rejected-b', 30);
  1031. var c = rejectSoon('rejected-c', 10);
  1032. var d = 0;
  1033. return goog.Promise.firstFulfilled([a, b, c, d])
  1034. .then(function(value) {
  1035. assertEquals(0, value);
  1036. // Return the slowest input promise to wait for it to complete.
  1037. return a;
  1038. })
  1039. .then(function(value) {
  1040. assertEquals(
  1041. 'The slowest promise should resolve eventually.', 'a', value);
  1042. });
  1043. }
  1044. function testFirstFulfilledWithFulfilledBeforeNonThenable() {
  1045. var a = fulfillSoon('a', 40);
  1046. var b = goog.Promise.resolve('b');
  1047. var c = rejectSoon('rejected-c', 10);
  1048. var d = 'd';
  1049. return goog.Promise.firstFulfilled([a, b, c, d])
  1050. .then(function(value) {
  1051. assertEquals('b', value);
  1052. // Return the slowest input promise to wait for it to complete.
  1053. return a;
  1054. })
  1055. .then(function(value) {
  1056. assertEquals(
  1057. 'The slowest promise should resolve eventually.', 'a', value);
  1058. });
  1059. }
  1060. function testFirstFulfilledWithReject() {
  1061. var a = rejectSoon('rejected-a', 40);
  1062. var b = rejectThenableSoon('rejected-b', 30);
  1063. var c = rejectBuiltInSoon('rejected-c', 10);
  1064. var d = createThrowingThenable('rejected-d');
  1065. return goog.Promise.firstFulfilled([a, b, c, d])
  1066. .then(shouldNotCall, function(reason) {
  1067. assertArrayEquals(
  1068. ['rejected-a', 'rejected-b', 'rejected-c', 'rejected-d'], reason);
  1069. // Ensure that the {@code then} property was only accessed once by
  1070. // {@code goog.Promise.firstFulfilled}.
  1071. if (SUPPORTS_ACCESSORS) {
  1072. assertEquals(1, b.thenAccesses);
  1073. }
  1074. });
  1075. }
  1076. function testThenAlwaysWithFulfill() {
  1077. var thenAlwaysCalled = false;
  1078. return goog.Promise.resolve(sentinel)
  1079. .thenAlways(function() {
  1080. assertEquals(
  1081. 'thenAlways should have no arguments', 0, arguments.length);
  1082. thenAlwaysCalled = true;
  1083. })
  1084. .then(function(value) {
  1085. assertEquals(sentinel, value);
  1086. assertTrue(thenAlwaysCalled);
  1087. });
  1088. }
  1089. function testThenAlwaysWithReject() {
  1090. var thenAlwaysCalled = false;
  1091. return goog.Promise.reject(sentinel)
  1092. .thenAlways(function(arg) {
  1093. assertEquals(
  1094. 'thenAlways should have no arguments', 0, arguments.length);
  1095. thenAlwaysCalled = true;
  1096. })
  1097. .then(shouldNotCall, function(err) {
  1098. assertEquals(sentinel, err);
  1099. return null;
  1100. });
  1101. }
  1102. function testThenAlwaysCalledMultipleTimes() {
  1103. var calls = [];
  1104. var p = goog.Promise.resolve(sentinel);
  1105. p.then(function(value) {
  1106. assertEquals(sentinel, value);
  1107. calls.push(1);
  1108. return value;
  1109. });
  1110. p.thenAlways(function() {
  1111. assertEquals(0, arguments.length);
  1112. calls.push(2);
  1113. throw Error('thenAlways throw');
  1114. });
  1115. p.then(function(value) {
  1116. assertEquals(
  1117. 'Promise result should not mutate after throw from thenAlways.',
  1118. sentinel, value);
  1119. calls.push(3);
  1120. });
  1121. p.thenAlways(function() { assertArrayEquals([1, 2, 3], calls); });
  1122. p.thenAlways(function() {
  1123. assertEquals(
  1124. 'Should be one unhandled exception from the "thenAlways throw".', 1,
  1125. unhandledRejections.getCallCount());
  1126. var rejectionCall = unhandledRejections.popLastCall();
  1127. assertEquals(1, rejectionCall.getArguments().length);
  1128. var err = rejectionCall.getArguments()[0];
  1129. assertEquals('thenAlways throw', err.message);
  1130. assertEquals(goog.global, rejectionCall.getThis());
  1131. });
  1132. return p.thenAlways(function() { assertEquals(3, calls.length); });
  1133. }
  1134. function testContextWithInit() {
  1135. var initContext;
  1136. var p = new goog.Promise(function(resolve, reject) {
  1137. initContext = this;
  1138. }, sentinel);
  1139. assertEquals(sentinel, initContext);
  1140. }
  1141. function testContextWithInitDefault() {
  1142. var initContext;
  1143. var p = new goog.Promise(function(resolve, reject) { initContext = this; });
  1144. assertEquals(
  1145. 'initFunc should default to being called in the global scope',
  1146. goog.global, initContext);
  1147. }
  1148. function testContextWithFulfillment() {
  1149. return goog.Promise.resolve()
  1150. .then(function() {
  1151. assertEquals(
  1152. 'Call should be made in the global scope if no context is specified.',
  1153. goog.global, this);
  1154. })
  1155. .then(
  1156. function() { assertEquals(sentinel, this); }, shouldNotCall, sentinel)
  1157. .thenAlways(function() { assertEquals(sentinel, this); }, sentinel);
  1158. }
  1159. function testContextWithRejection() {
  1160. return goog.Promise.reject()
  1161. .then(
  1162. shouldNotCall,
  1163. function() {
  1164. assertEquals(
  1165. 'Call should be in the default scope when no context is set.',
  1166. goog.global, this);
  1167. throw new Error('Intentional rejection');
  1168. })
  1169. .then(
  1170. shouldNotCall, function() { assertEquals(sentinel, this); }, sentinel)
  1171. .thenAlways(function() { assertEquals(sentinel, this); }, sentinel)
  1172. .thenCatch(function() { assertEquals(sentinel, this); }, sentinel);
  1173. }
  1174. function testCancel() {
  1175. var p = new goog.Promise(goog.nullFunction);
  1176. var child = p.then(shouldNotCall, function(reason) {
  1177. assertTrue(reason instanceof goog.Promise.CancellationError);
  1178. assertEquals('cancellation message', reason.message);
  1179. // Return a non-Error to resolve the cancellation rejection.
  1180. return null;
  1181. });
  1182. p.cancel('cancellation message');
  1183. return child;
  1184. }
  1185. function testThenVoidCancel() {
  1186. var thenVoidCalled = false;
  1187. var p = new goog.Promise(goog.nullFunction);
  1188. p.thenVoid(shouldNotCall, function(reason) {
  1189. assertTrue(reason instanceof goog.Promise.CancellationError);
  1190. assertEquals('cancellation message', reason.message);
  1191. thenVoidCalled = true;
  1192. });
  1193. p.cancel('cancellation message');
  1194. assertFalse(thenVoidCalled);
  1195. return p.thenCatch(function() {
  1196. assertTrue(thenVoidCalled);
  1197. // Return a non-Error to resolve the cancellation rejection.
  1198. return null;
  1199. });
  1200. }
  1201. function testCancelAfterResolve() {
  1202. var p = goog.Promise.resolve();
  1203. p.cancel();
  1204. return p.then(null, shouldNotCall);
  1205. }
  1206. function testThenVoidCancelAfterResolve() {
  1207. var p = goog.Promise.resolve();
  1208. p.cancel();
  1209. p.thenVoid(null, shouldNotCall);
  1210. return p;
  1211. }
  1212. function testCancelAfterReject() {
  1213. var p = goog.Promise.reject(sentinel);
  1214. p.cancel();
  1215. return p.then(
  1216. shouldNotCall, function(reason) { assertEquals(sentinel, reason); });
  1217. }
  1218. function testThenVoidCancelAfterReject() {
  1219. var thenVoidCalled = false;
  1220. var p = goog.Promise.reject(sentinel);
  1221. p.cancel();
  1222. p.thenVoid(shouldNotCall, function(reason) {
  1223. assertEquals(sentinel, reason);
  1224. thenVoidCalled = true;
  1225. });
  1226. return p.thenCatch(function() { assertTrue(thenVoidCalled); });
  1227. }
  1228. function testCancelPropagation() {
  1229. var cancelError;
  1230. var p = new goog.Promise(goog.nullFunction);
  1231. var p2 = p.then(shouldNotCall, function(reason) {
  1232. cancelError = reason;
  1233. assertTrue(reason instanceof goog.Promise.CancellationError);
  1234. assertEquals('parent cancel message', reason.message);
  1235. return sentinel;
  1236. }).then(function(value) {
  1237. assertEquals(
  1238. 'Child promises should receive the returned value of the parent.',
  1239. sentinel, value);
  1240. }, shouldNotCall);
  1241. var p3 = p.then(shouldNotCall, function(reason) {
  1242. assertEquals(
  1243. 'Every onRejected handler should receive the same cancel error.',
  1244. cancelError, reason);
  1245. assertEquals('parent cancel message', reason.message);
  1246. // Return a non-Error to resolve the cancellation rejection.
  1247. return null;
  1248. });
  1249. p.cancel('parent cancel message');
  1250. return goog.Promise.all([p2, p3]);
  1251. }
  1252. function testThenVoidCancelPropagation() {
  1253. var resolver = goog.Promise.withResolver();
  1254. var toResolveCount = 2;
  1255. var partialResolve = function() {
  1256. if (--toResolveCount == 0) {
  1257. resolver.resolve();
  1258. }
  1259. };
  1260. var cancelError;
  1261. var p = new goog.Promise(goog.nullFunction);
  1262. var p2 = p.then(shouldNotCall, function(reason) {
  1263. cancelError = reason;
  1264. assertTrue(reason instanceof goog.Promise.CancellationError);
  1265. assertEquals('parent cancel message', reason.message);
  1266. return sentinel;
  1267. });
  1268. p2.thenVoid(function(value) {
  1269. assertEquals(
  1270. 'Child promises should receive the returned value of the parent.',
  1271. sentinel, value);
  1272. partialResolve();
  1273. }, shouldNotCall);
  1274. p.thenVoid(shouldNotCall, function(reason) {
  1275. assertEquals(
  1276. 'Every onRejected handler should receive the same cancel error.',
  1277. cancelError, reason);
  1278. assertEquals('parent cancel message', reason.message);
  1279. partialResolve();
  1280. });
  1281. p.cancel('parent cancel message');
  1282. return resolver.promise;
  1283. }
  1284. function testCancelPropagationUpward() {
  1285. var cancelError;
  1286. var cancelCalls = [];
  1287. var parent = new goog.Promise(goog.nullFunction);
  1288. var child = parent.then(shouldNotCall, function(reason) {
  1289. assertTrue(reason instanceof goog.Promise.CancellationError);
  1290. assertEquals('grandChild cancel message', reason.message);
  1291. cancelError = reason;
  1292. cancelCalls.push('parent');
  1293. });
  1294. var grandChild = child.then(shouldNotCall, function(reason) {
  1295. assertEquals(
  1296. 'Child should receive the same cancel error.', cancelError, reason);
  1297. cancelCalls.push('child');
  1298. });
  1299. var descendant = grandChild.then(shouldNotCall, function(reason) {
  1300. assertEquals(
  1301. 'GrandChild should receive the same cancel error.', cancelError,
  1302. reason);
  1303. cancelCalls.push('grandChild');
  1304. assertArrayEquals(
  1305. 'Each promise in the hierarchy has a single child, so canceling the ' +
  1306. 'grandChild should cancel each ancestor in order.',
  1307. ['parent', 'child', 'grandChild'], cancelCalls);
  1308. // Return a non-Error to resolve the cancellation rejection.
  1309. return null;
  1310. });
  1311. grandChild.cancel('grandChild cancel message');
  1312. return descendant;
  1313. }
  1314. function testThenVoidCancelPropagationUpward() {
  1315. var cancelError;
  1316. var cancelCalls = [];
  1317. var parent = new goog.Promise(goog.nullFunction);
  1318. var child = parent.then(shouldNotCall, function(reason) {
  1319. assertTrue(reason instanceof goog.Promise.CancellationError);
  1320. assertEquals('grandChild cancel message', reason.message);
  1321. cancelError = reason;
  1322. cancelCalls.push('parent');
  1323. });
  1324. var grandChild = child.then(shouldNotCall, function(reason) {
  1325. assertEquals(
  1326. 'Child should receive the same cancel error.', cancelError, reason);
  1327. cancelCalls.push('child');
  1328. });
  1329. grandChild.thenVoid(shouldNotCall, function(reason) {
  1330. assertEquals(
  1331. 'GrandChild should receive the same cancel error.', cancelError,
  1332. reason);
  1333. cancelCalls.push('grandChild');
  1334. });
  1335. grandChild.cancel('grandChild cancel message');
  1336. return grandChild.thenCatch(function(reason) {
  1337. assertEquals(cancelError, reason);
  1338. assertArrayEquals(
  1339. 'Each promise in the hierarchy has a single child, so canceling the ' +
  1340. 'grandChild should cancel each ancestor in order.',
  1341. ['parent', 'child', 'grandChild'], cancelCalls);
  1342. // Return a non-Error to resolve the cancellation rejection.
  1343. return null;
  1344. });
  1345. }
  1346. function testCancelPropagationUpwardWithMultipleChildren() {
  1347. var cancelError;
  1348. var cancelCalls = [];
  1349. var parent = fulfillSoon(sentinel, 0);
  1350. parent.then(function(value) {
  1351. assertEquals(
  1352. 'Non-canceled callbacks should be called after a sibling is canceled.',
  1353. sentinel, value);
  1354. });
  1355. var child = parent.then(shouldNotCall, function(reason) {
  1356. assertTrue(reason instanceof goog.Promise.CancellationError);
  1357. assertEquals('grandChild cancel message', reason.message);
  1358. cancelError = reason;
  1359. cancelCalls.push('child');
  1360. });
  1361. var grandChild = child.then(shouldNotCall, function(reason) {
  1362. assertEquals(reason, cancelError);
  1363. cancelCalls.push('grandChild');
  1364. });
  1365. grandChild.cancel('grandChild cancel message');
  1366. return grandChild.then(shouldNotCall, function(reason) {
  1367. assertEquals(reason, cancelError);
  1368. assertArrayEquals(
  1369. 'The parent promise has multiple children, so only the child and ' +
  1370. 'grandChild should be canceled.',
  1371. ['child', 'grandChild'], cancelCalls);
  1372. // Return a non-Error to resolve the cancellation rejection.
  1373. return null;
  1374. });
  1375. }
  1376. function testThenVoidCancelPropagationUpwardWithMultipleChildren() {
  1377. var cancelError;
  1378. var cancelCalls = [];
  1379. var parent = fulfillSoon(sentinel, 0);
  1380. parent.thenVoid(function(value) {
  1381. assertEquals(
  1382. 'Non-canceled callbacks should be called after a sibling is canceled.',
  1383. sentinel, value);
  1384. }, shouldNotCall);
  1385. var child = parent.then(shouldNotCall, function(reason) {
  1386. assertTrue(reason instanceof goog.Promise.CancellationError);
  1387. assertEquals('grandChild cancel message', reason.message);
  1388. cancelError = reason;
  1389. cancelCalls.push('child');
  1390. });
  1391. var grandChild = child.then(shouldNotCall, function(reason) {
  1392. assertEquals(reason, cancelError);
  1393. cancelCalls.push('grandChild');
  1394. });
  1395. grandChild.cancel('grandChild cancel message');
  1396. grandChild.thenVoid(shouldNotCall, function(reason) {
  1397. assertEquals(reason, cancelError);
  1398. cancelCalls.push('void grandChild');
  1399. });
  1400. return grandChild.then(shouldNotCall, function(reason) {
  1401. assertEquals(reason, cancelError);
  1402. assertArrayEquals(
  1403. 'The parent promise has multiple children, so only the child and ' +
  1404. 'grandChildren should be canceled.',
  1405. ['child', 'grandChild', 'void grandChild'], cancelCalls);
  1406. // Return a non-Error to resolve the cancellation rejection.
  1407. return null;
  1408. });
  1409. }
  1410. function testCancelRecovery() {
  1411. var cancelError;
  1412. var cancelCalls = [];
  1413. var parent = fulfillSoon(sentinel, 100);
  1414. var sibling1 = parent.then(function(value) {
  1415. assertEquals(
  1416. 'Non-canceled callbacks should be called after a sibling is canceled.',
  1417. sentinel, value);
  1418. });
  1419. var sibling2 = parent.then(shouldNotCall, function(reason) {
  1420. assertTrue(reason instanceof goog.Promise.CancellationError);
  1421. cancelError = reason;
  1422. cancelCalls.push('sibling2');
  1423. return sentinel;
  1424. });
  1425. var grandChild = sibling2.then(function(value) {
  1426. cancelCalls.push('child');
  1427. assertEquals(
  1428. 'Returning a non-cancel value should uncancel the grandChild.', value,
  1429. sentinel);
  1430. assertArrayEquals(['sibling2', 'child'], cancelCalls);
  1431. }, shouldNotCall);
  1432. grandChild.cancel();
  1433. return goog.Promise.all([sibling1, grandChild]);
  1434. }
  1435. function testCancellationError() {
  1436. var err = new goog.Promise.CancellationError('cancel message');
  1437. assertTrue(err instanceof Error);
  1438. assertTrue(err instanceof goog.Promise.CancellationError);
  1439. assertEquals('cancel', err.name);
  1440. assertEquals('cancel message', err.message);
  1441. }
  1442. function testMockClock() {
  1443. mockClock.install();
  1444. var resolveA;
  1445. var resolveB;
  1446. var calls = [];
  1447. var p = new goog.Promise(function(resolve, reject) { resolveA = resolve; });
  1448. p.then(function(value) {
  1449. assertEquals(sentinel, value);
  1450. calls.push('then');
  1451. });
  1452. var fulfilledChild = p.then(function(value) {
  1453. assertEquals(sentinel, value);
  1454. return goog.Promise.resolve(1);
  1455. }).then(function(value) {
  1456. assertEquals(1, value);
  1457. calls.push('fulfilledChild');
  1458. });
  1459. var rejectedChild = p.then(function(value) {
  1460. assertEquals(sentinel, value);
  1461. return goog.Promise.reject(2);
  1462. }).then(shouldNotCall, function(reason) {
  1463. assertEquals(2, reason);
  1464. calls.push('rejectedChild');
  1465. });
  1466. var unresolvedChild =
  1467. p.then(function(value) {
  1468. assertEquals(sentinel, value);
  1469. return new goog.Promise(function(r) { resolveB = r; });
  1470. }).then(function(value) {
  1471. assertEquals(3, value);
  1472. calls.push('unresolvedChild');
  1473. });
  1474. resolveA(sentinel);
  1475. assertArrayEquals(
  1476. 'Calls must not be resolved until the clock ticks.', [], calls);
  1477. mockClock.tick();
  1478. assertArrayEquals(
  1479. 'All resolved Promises should execute in the same timestep.',
  1480. ['then', 'fulfilledChild', 'rejectedChild'], calls);
  1481. resolveB(3);
  1482. assertArrayEquals(
  1483. 'New calls must not resolve until the clock ticks.',
  1484. ['then', 'fulfilledChild', 'rejectedChild'], calls);
  1485. mockClock.tick();
  1486. assertArrayEquals(
  1487. 'All callbacks should have executed.',
  1488. ['then', 'fulfilledChild', 'rejectedChild', 'unresolvedChild'], calls);
  1489. }
  1490. function testHandledRejection() {
  1491. mockClock.install();
  1492. goog.Promise.reject(sentinel).then(shouldNotCall, function(reason) {});
  1493. mockClock.tick();
  1494. assertEquals(0, unhandledRejections.getCallCount());
  1495. }
  1496. function testThenVoidHandledRejection() {
  1497. mockClock.install();
  1498. goog.Promise.reject(sentinel).thenVoid(shouldNotCall, function(reason) {});
  1499. mockClock.tick();
  1500. assertEquals(0, unhandledRejections.getCallCount());
  1501. }
  1502. function testUnhandledRejection1() {
  1503. mockClock.install();
  1504. goog.Promise.reject(sentinel);
  1505. mockClock.tick();
  1506. assertEquals(1, unhandledRejections.getCallCount());
  1507. var rejectionCall = unhandledRejections.popLastCall();
  1508. assertArrayEquals([sentinel], rejectionCall.getArguments());
  1509. assertEquals(goog.global, rejectionCall.getThis());
  1510. }
  1511. function testUnhandledRejection2() {
  1512. mockClock.install();
  1513. goog.Promise.reject(sentinel).then(shouldNotCall);
  1514. mockClock.tick();
  1515. assertEquals(1, unhandledRejections.getCallCount());
  1516. var rejectionCall = unhandledRejections.popLastCall();
  1517. assertArrayEquals([sentinel], rejectionCall.getArguments());
  1518. assertEquals(goog.global, rejectionCall.getThis());
  1519. }
  1520. function testThenVoidUnhandledRejection() {
  1521. mockClock.install();
  1522. goog.Promise.reject(sentinel).thenVoid(shouldNotCall);
  1523. mockClock.tick();
  1524. assertEquals(1, unhandledRejections.getCallCount());
  1525. var rejectionCall = unhandledRejections.popLastCall();
  1526. assertArrayEquals([sentinel], rejectionCall.getArguments());
  1527. assertEquals(goog.global, rejectionCall.getThis());
  1528. }
  1529. function testUnhandledRejection() {
  1530. var resolver = goog.Promise.withResolver();
  1531. goog.Promise.setUnhandledRejectionHandler(function(err) {
  1532. assertEquals(sentinel, err);
  1533. resolver.resolve();
  1534. });
  1535. goog.Promise.reject(sentinel);
  1536. return resolver.promise;
  1537. }
  1538. function testUnhandledThrow() {
  1539. var resolver = goog.Promise.withResolver();
  1540. goog.Promise.setUnhandledRejectionHandler(function(err) {
  1541. assertEquals(sentinel, err);
  1542. resolver.resolve();
  1543. });
  1544. goog.Promise.resolve().then(function() { throw sentinel; });
  1545. return resolver.promise;
  1546. }
  1547. function testThenVoidUnhandledThrow() {
  1548. var resolver = goog.Promise.withResolver();
  1549. goog.Promise.setUnhandledRejectionHandler(function(error) {
  1550. assertEquals(sentinel, error);
  1551. resolver.resolve();
  1552. });
  1553. goog.Promise.resolve().thenVoid(function() { throw sentinel; });
  1554. return resolver.promise;
  1555. }
  1556. function testUnhandledBlockingRejection() {
  1557. mockClock.install();
  1558. var blocker = goog.Promise.reject(sentinel);
  1559. goog.Promise.resolve(blocker);
  1560. mockClock.tick();
  1561. assertEquals(1, unhandledRejections.getCallCount());
  1562. var rejectionCall = unhandledRejections.popLastCall();
  1563. assertArrayEquals([sentinel], rejectionCall.getArguments());
  1564. assertEquals(goog.global, rejectionCall.getThis());
  1565. }
  1566. function testUnhandledRejectionAfterThenAlways() {
  1567. mockClock.install();
  1568. var resolver = goog.Promise.withResolver();
  1569. resolver.promise.thenAlways(function() {});
  1570. resolver.reject(sentinel);
  1571. mockClock.tick();
  1572. assertEquals(1, unhandledRejections.getCallCount());
  1573. var rejectionCall = unhandledRejections.popLastCall();
  1574. assertArrayEquals([sentinel], rejectionCall.getArguments());
  1575. assertEquals(goog.global, rejectionCall.getThis());
  1576. }
  1577. function testHandledBlockingRejection() {
  1578. mockClock.install();
  1579. var blocker = goog.Promise.reject(sentinel);
  1580. goog.Promise.resolve(blocker).then(shouldNotCall, function(reason) {});
  1581. mockClock.tick();
  1582. assertEquals(0, unhandledRejections.getCallCount());
  1583. }
  1584. function testThenVoidHandledBlockingRejection() {
  1585. var shouldCall = goog.testing.recordFunction();
  1586. mockClock.install();
  1587. var blocker = goog.Promise.reject(sentinel);
  1588. goog.Promise.resolve(blocker).thenVoid(shouldNotCall, shouldCall);
  1589. mockClock.tick();
  1590. assertEquals(0, unhandledRejections.getCallCount());
  1591. assertEquals(1, shouldCall.getCallCount());
  1592. }
  1593. function testUnhandledRejectionWithTimeout() {
  1594. mockClock.install();
  1595. stubs.replace(goog.Promise, 'UNHANDLED_REJECTION_DELAY', 200);
  1596. goog.Promise.reject(sentinel);
  1597. mockClock.tick(199);
  1598. assertEquals(0, unhandledRejections.getCallCount());
  1599. mockClock.tick(1);
  1600. assertEquals(1, unhandledRejections.getCallCount());
  1601. }
  1602. function testHandledRejectionWithTimeout() {
  1603. mockClock.install();
  1604. stubs.replace(goog.Promise, 'UNHANDLED_REJECTION_DELAY', 200);
  1605. var p = goog.Promise.reject(sentinel);
  1606. mockClock.tick(199);
  1607. p.then(shouldNotCall, function(reason) {});
  1608. mockClock.tick(1);
  1609. assertEquals(0, unhandledRejections.getCallCount());
  1610. }
  1611. function testUnhandledRejectionDisabled() {
  1612. mockClock.install();
  1613. stubs.replace(goog.Promise, 'UNHANDLED_REJECTION_DELAY', -1);
  1614. goog.Promise.reject(sentinel);
  1615. mockClock.tick();
  1616. assertEquals(0, unhandledRejections.getCallCount());
  1617. }
  1618. function testThenableInterface() {
  1619. var promise = new goog.Promise(function(resolve, reject) {});
  1620. assertTrue(goog.Thenable.isImplementedBy(promise));
  1621. assertFalse(goog.Thenable.isImplementedBy({}));
  1622. assertFalse(goog.Thenable.isImplementedBy('string'));
  1623. assertFalse(goog.Thenable.isImplementedBy(1));
  1624. assertFalse(goog.Thenable.isImplementedBy({then: function() {}}));
  1625. function T() {}
  1626. T.prototype.then = function(opt_a, opt_b, opt_c) {};
  1627. goog.Thenable.addImplementation(T);
  1628. assertTrue(goog.Thenable.isImplementedBy(new T));
  1629. // Test COMPILED code path.
  1630. try {
  1631. COMPILED = true;
  1632. function C() {}
  1633. C.prototype.then = function(opt_a, opt_b, opt_c) {};
  1634. goog.Thenable.addImplementation(C);
  1635. assertTrue(goog.Thenable.isImplementedBy(new C));
  1636. } finally {
  1637. COMPILED = false;
  1638. }
  1639. }
  1640. function testCreateWithResolver_Resolved() {
  1641. mockClock.install();
  1642. var timesCalled = 0;
  1643. var resolver = goog.Promise.withResolver();
  1644. resolver.promise.then(function(value) {
  1645. timesCalled++;
  1646. assertEquals(sentinel, value);
  1647. }, fail);
  1648. assertEquals(
  1649. 'then() must return before callbacks are invoked.', 0, timesCalled);
  1650. mockClock.tick();
  1651. assertEquals(
  1652. 'promise is not resolved until resolver is invoked.', 0, timesCalled);
  1653. resolver.resolve(sentinel);
  1654. assertEquals('resolution is delayed until the next tick', 0, timesCalled);
  1655. mockClock.tick();
  1656. assertEquals('onFulfilled must be called exactly once.', 1, timesCalled);
  1657. }
  1658. function testCreateWithResolver_Rejected() {
  1659. mockClock.install();
  1660. var timesCalled = 0;
  1661. var resolver = goog.Promise.withResolver();
  1662. resolver.promise.then(fail, function(reason) {
  1663. timesCalled++;
  1664. assertEquals(sentinel, reason);
  1665. });
  1666. assertEquals(
  1667. 'then() must return before callbacks are invoked.', 0, timesCalled);
  1668. mockClock.tick();
  1669. assertEquals(
  1670. 'promise is not resolved until resolver is invoked.', 0, timesCalled);
  1671. resolver.reject(sentinel);
  1672. assertEquals('resolution is delayed until the next tick', 0, timesCalled);
  1673. mockClock.tick();
  1674. assertEquals('onFulfilled must be called exactly once.', 1, timesCalled);
  1675. }
  1676. function testLinksBetweenParentsAndChildrenAreCutOnResolve() {
  1677. mockClock.install();
  1678. var parentResolver = goog.Promise.withResolver();
  1679. var parent = parentResolver.promise;
  1680. var child = parent.then(function() {});
  1681. assertNotNull(child.parent_);
  1682. assertEquals(null, parent.callbackEntries_.next);
  1683. parentResolver.resolve();
  1684. mockClock.tick();
  1685. assertNull(child.parent_);
  1686. assertEquals(null, parent.callbackEntries_);
  1687. }
  1688. function testLinksBetweenParentsAndChildrenAreCutWithUnresolvedChild() {
  1689. mockClock.install();
  1690. var parentResolver = goog.Promise.withResolver();
  1691. var parent = parentResolver.promise;
  1692. var child = parent.then(function() {
  1693. // Will never resolve.
  1694. return new goog.Promise(function() {});
  1695. });
  1696. assertNotNull(child.parent_);
  1697. assertNull(parent.callbackEntries_.next);
  1698. parentResolver.resolve();
  1699. mockClock.tick();
  1700. assertNull(child.parent_);
  1701. assertEquals(null, parent.callbackEntries_);
  1702. }
  1703. function testLinksBetweenParentsAndChildrenAreCutOnCancel() {
  1704. mockClock.install();
  1705. var parent = new goog.Promise(function() {});
  1706. var child = parent.then(function() {});
  1707. var grandChild = child.then(function() {});
  1708. assertEquals(null, child.callbackEntries_.next);
  1709. assertNotNull(child.parent_);
  1710. assertEquals(null, parent.callbackEntries_.next);
  1711. parent.cancel();
  1712. mockClock.tick();
  1713. assertNull(child.parent_);
  1714. assertNull(grandChild.parent_);
  1715. assertEquals(null, parent.callbackEntries_);
  1716. assertEquals(null, child.callbackEntries_);
  1717. }