PageRenderTime 42ms CodeModel.GetById 1ms RepoModel.GetById 1ms app.codeStats 0ms

/as3-commons-ui/src/test/actionscript/org/as3commons/ui/lifecycle/lifecycle/tests/LifeCycleAdapterTest.as

http://as3-commons.googlecode.com/
ActionScript | 1618 lines | 1241 code | 334 blank | 43 comment | 5 complexity | 038e4feac852b2681865f2666b99b516 MD5 | raw file
Possible License(s): Apache-2.0
  1. package org.as3commons.ui.lifecycle.lifecycle.tests {
  2. import org.as3commons.collections.utils.ArrayUtils;
  3. import org.as3commons.ui.framework.uiservice.errors.AdapterNotRegisteredError;
  4. import org.as3commons.ui.lifecycle.i10n.errors.ValidateNowInRunningCycleError;
  5. import org.as3commons.ui.lifecycle.lifecycle.LifeCycle;
  6. import org.as3commons.ui.lifecycle.lifecycle.errors.InvalidationNotAllowedHereError;
  7. import org.as3commons.ui.lifecycle.lifecycle.testhelper.LifeCycleCallbackWatcher;
  8. import org.as3commons.ui.lifecycle.lifecycle.testhelper.TestLifeCycleAdapter;
  9. import org.as3commons.ui.lifecycle.testhelper.AsyncCallback;
  10. import org.as3commons.ui.testhelper.TestDisplayObject;
  11. import org.flexunit.asserts.assertEquals;
  12. import org.flexunit.asserts.assertFalse;
  13. import org.flexunit.asserts.assertNotNull;
  14. import org.flexunit.asserts.assertTrue;
  15. import flash.display.DisplayObject;
  16. import flash.display.Sprite;
  17. import flash.events.Event;
  18. /**
  19. * @author Jens Struwe 15.09.2011
  20. */
  21. public class LifeCycleAdapterTest {
  22. private var _lifeCycle : LifeCycle;
  23. private var _watcher : LifeCycleCallbackWatcher;
  24. [Before]
  25. public function setUp() : void {
  26. _lifeCycle = new LifeCycle();
  27. _watcher = new LifeCycleCallbackWatcher();
  28. }
  29. [After]
  30. public function tearDown() : void {
  31. _lifeCycle.cleanUp();
  32. _lifeCycle = null;
  33. _watcher = null;
  34. StageProxy.cleanUpRoot();
  35. }
  36. private function setUpCompleteTimer(callback : Function) : void {
  37. AsyncCallback.setUpCompleteTimer(this, callback);
  38. }
  39. [Test(async)]
  40. public function test_invalidate() : void {
  41. var s : DisplayObject = StageProxy.root.addChild(new TestDisplayObject("s"));
  42. var adapter : TestLifeCycleAdapter = new TestLifeCycleAdapter(_watcher);
  43. adapter.validateFunction = validate;
  44. _lifeCycle.registerDisplayObject(s, adapter);
  45. adapter.invalidate();
  46. setUpCompleteTimer(complete);
  47. function validate() : void {
  48. adapter.invalidateDefaults();
  49. adapter.scheduleRendering();
  50. }
  51. function complete(event : Event, data : * = null) : void {
  52. assertTrue(ArrayUtils.arraysEqual([
  53. s, LifeCycle.PHASE_VALIDATE,
  54. s, LifeCycle.PHASE_CALCULATE_DEFAULTS,
  55. s, LifeCycle.PHASE_RENDER
  56. ], _watcher.phasesLog));
  57. }
  58. }
  59. [Test]
  60. public function test_invalidate_beforeRegistrationThrowsError() : void {
  61. var adapter : TestLifeCycleAdapter = new TestLifeCycleAdapter(_watcher);
  62. var errorThrown : Error;
  63. try {
  64. adapter.invalidate();
  65. } catch (e : Error) {
  66. errorThrown = e;
  67. }
  68. assertNotNull(errorThrown);
  69. assertTrue(errorThrown is AdapterNotRegisteredError);
  70. }
  71. [Test(async)]
  72. public function test_invalidate_multipleTimes() : void {
  73. var s : DisplayObject = StageProxy.root.addChild(new TestDisplayObject("s"));
  74. var adapter : TestLifeCycleAdapter = new TestLifeCycleAdapter(_watcher);
  75. adapter.validateFunction = validate;
  76. _lifeCycle.registerDisplayObject(s, adapter);
  77. adapter.invalidate();
  78. adapter.invalidate();
  79. setUpCompleteTimer(complete);
  80. function validate() : void {
  81. adapter.invalidateDefaults();
  82. adapter.scheduleRendering();
  83. adapter.invalidateDefaults();
  84. adapter.scheduleRendering();
  85. }
  86. function complete(event : Event, data : * = null) : void {
  87. assertTrue(ArrayUtils.arraysEqual([
  88. s, LifeCycle.PHASE_VALIDATE,
  89. s, LifeCycle.PHASE_CALCULATE_DEFAULTS,
  90. s, LifeCycle.PHASE_RENDER,
  91. ], _watcher.phasesLog));
  92. }
  93. }
  94. [Test(async)]
  95. public function test_invalidate_inRenderHandler() : void {
  96. var s : DisplayObject = StageProxy.root.addChild(new TestDisplayObject("s"));
  97. var adapter : TestLifeCycleAdapter = new TestLifeCycleAdapter(_watcher);
  98. _lifeCycle.registerDisplayObject(s, adapter);
  99. AsyncCallback.setUpRenderTimer(this, invalidate);
  100. function invalidate(event : Event, data : * = null) : void {
  101. adapter.invalidate();
  102. setUpCompleteTimer(complete);
  103. }
  104. function complete(event : Event, data : * = null) : void {
  105. assertTrue(ArrayUtils.arraysEqual([s, LifeCycle.PHASE_VALIDATE], _watcher.phasesLog));
  106. }
  107. }
  108. [Test(async)]
  109. public function test_invalidate_inValidation_currentPhase() : void {
  110. var s : DisplayObject = StageProxy.root.addChild(new TestDisplayObject("s"));
  111. var adapter : TestLifeCycleAdapter = new TestLifeCycleAdapter(_watcher);
  112. adapter.validateFunction = validate;
  113. _lifeCycle.registerDisplayObject(s, adapter);
  114. adapter.invalidate();
  115. setUpCompleteTimer(complete);
  116. function validate() : void {
  117. if (!adapter.validateCount) {
  118. adapter.invalidate();
  119. }
  120. }
  121. function complete(event : Event, data : * = null) : void {
  122. var log : Array = _watcher.phasesLog;
  123. assertTrue(ArrayUtils.arraysEqual([
  124. s, LifeCycle.PHASE_VALIDATE,
  125. s, LifeCycle.PHASE_VALIDATE
  126. ], log));
  127. }
  128. }
  129. [Test(async)]
  130. public function test_invalidate_inValidation_allProperties() : void {
  131. var s : DisplayObject = StageProxy.root.addChild(new TestDisplayObject("s"));
  132. var adapter : TestLifeCycleAdapter = new TestLifeCycleAdapter(_watcher);
  133. adapter.validateFunction = validate;
  134. _lifeCycle.registerDisplayObject(s, adapter);
  135. adapter.invalidate("test");
  136. var results : Array = new Array();
  137. storeResults();
  138. setUpCompleteTimer(complete);
  139. function validate() : void {
  140. if (!adapter.validateCount) {
  141. storeResults();
  142. adapter.invalidate();
  143. storeResults();
  144. }
  145. if (adapter.validateCount) {
  146. storeResults();
  147. }
  148. }
  149. function storeResults() : void {
  150. results.push(
  151. adapter.isInvalidForAnyPhase(),
  152. adapter.isInvalid(),
  153. adapter.isInvalid("test"),
  154. adapter.isInvalid("test2"),
  155. adapter.isInvalid("all_properties")
  156. );
  157. }
  158. function complete(event : Event, data : * = null) : void {
  159. storeResults();
  160. assertTrue(ArrayUtils.arraysEqual([
  161. // immediately phase1
  162. true, true, true, false, false,
  163. // phase1 first run1
  164. true, true, true, false, false,
  165. // phase1 first run2
  166. true, true, true, true, true,
  167. // phase1 second
  168. true, true, true, true, true,
  169. // complete
  170. false, false, false, false, false
  171. ], results));
  172. }
  173. }
  174. [Test(async)]
  175. /*
  176. * s (validateNow -> invalidate s2, s3)
  177. * -- s2
  178. * s3
  179. * -- s4
  180. */
  181. public function test_invalidate_others_inValidateNow() : void {
  182. var s : Sprite = StageProxy.root.addChild(new TestDisplayObject("s")) as Sprite;
  183. var adapter : TestLifeCycleAdapter = new TestLifeCycleAdapter(_watcher);
  184. adapter.validateFunction = validate;
  185. _lifeCycle.registerDisplayObject(s, adapter);
  186. var s2 : Sprite = s.addChild(new TestDisplayObject("s2")) as Sprite;
  187. var adapter2 : TestLifeCycleAdapter = new TestLifeCycleAdapter(_watcher);
  188. _lifeCycle.registerDisplayObject(s2, adapter2);
  189. var s3 : Sprite = StageProxy.root.addChild(new TestDisplayObject("s3")) as Sprite;
  190. var adapter3 : TestLifeCycleAdapter = new TestLifeCycleAdapter(_watcher);
  191. _lifeCycle.registerDisplayObject(s3, adapter3);
  192. adapter.invalidate();
  193. adapter.validateNow();
  194. assertTrue(ArrayUtils.arraysEqual([
  195. s, LifeCycle.PHASE_VALIDATE,
  196. s2, LifeCycle.PHASE_VALIDATE
  197. ], _watcher.phasesLog));
  198. setUpCompleteTimer(complete);
  199. function validate() : void {
  200. adapter2.invalidate();
  201. adapter3.invalidate();
  202. }
  203. function complete(event : Event, data : * = null) : void {
  204. assertTrue(ArrayUtils.arraysEqual([s3, LifeCycle.PHASE_VALIDATE], _watcher.phasesLog));
  205. }
  206. }
  207. [Test(async)]
  208. /*
  209. * s
  210. * -- s2 (validateNow -> invalidate s, s3)
  211. * -- -- s3
  212. */
  213. public function test_invalidate_others_inValidateNow2() : void {
  214. var s : Sprite = StageProxy.root.addChild(new TestDisplayObject("s")) as Sprite;
  215. var adapter : TestLifeCycleAdapter = new TestLifeCycleAdapter(_watcher);
  216. _lifeCycle.registerDisplayObject(s, adapter);
  217. var s2 : Sprite = s.addChild(new TestDisplayObject("s2")) as Sprite;
  218. var adapter2 : TestLifeCycleAdapter = new TestLifeCycleAdapter(_watcher);
  219. adapter2.validateFunction = validate;
  220. _lifeCycle.registerDisplayObject(s2, adapter2);
  221. var s3 : Sprite = s2.addChild(new TestDisplayObject("s3")) as Sprite;
  222. var adapter3 : TestLifeCycleAdapter = new TestLifeCycleAdapter(_watcher);
  223. _lifeCycle.registerDisplayObject(s3, adapter3);
  224. adapter2.invalidate();
  225. adapter2.validateNow();
  226. assertTrue(ArrayUtils.arraysEqual([
  227. s2, LifeCycle.PHASE_VALIDATE, s3, LifeCycle.PHASE_VALIDATE
  228. ], _watcher.phasesLog));
  229. setUpCompleteTimer(complete);
  230. function validate() : void {
  231. adapter.invalidate();
  232. adapter3.invalidate();
  233. }
  234. function complete(event : Event, data : * = null) : void {
  235. assertTrue(ArrayUtils.arraysEqual([s, LifeCycle.PHASE_VALIDATE], _watcher.phasesLog));
  236. }
  237. }
  238. [Test(async)]
  239. public function test_invalidate_notOnStage() : void {
  240. var s : DisplayObject = new TestDisplayObject("s");
  241. var adapter : TestLifeCycleAdapter = new TestLifeCycleAdapter(_watcher);
  242. _lifeCycle.registerDisplayObject(s, adapter);
  243. adapter.invalidate();
  244. setUpCompleteTimer(complete);
  245. function complete(event : Event, data : * = null) : void {
  246. assertTrue(ArrayUtils.arraysEqual([], _watcher.phasesLog));
  247. }
  248. }
  249. [Test(async)]
  250. public function test_invalidate_notOnStage_autoAdd() : void {
  251. var s : DisplayObject = new TestDisplayObject("s");
  252. var adapter : TestLifeCycleAdapter = new TestLifeCycleAdapter(_watcher);
  253. _lifeCycle.registerDisplayObject(s, adapter);
  254. adapter.invalidate();
  255. setUpCompleteTimer(complete);
  256. function complete(event : Event, data : * = null) : void {
  257. assertTrue(ArrayUtils.arraysEqual([], _watcher.phasesLog));
  258. StageProxy.root.addChild(s);
  259. setUpCompleteTimer(complete2);
  260. }
  261. function complete2(event : Event, data : * = null) : void {
  262. assertTrue(ArrayUtils.arraysEqual([s, LifeCycle.PHASE_VALIDATE], _watcher.phasesLog));
  263. }
  264. }
  265. [Test(async)]
  266. public function test_invalidate_removedFromStageinValidation() : void {
  267. var s : DisplayObject = StageProxy.root.addChild(new TestDisplayObject("s"));
  268. var adapter : TestLifeCycleAdapter = new TestLifeCycleAdapter(_watcher);
  269. adapter.validateFunction = validate;
  270. _lifeCycle.registerDisplayObject(s, adapter);
  271. adapter.invalidate();
  272. setUpCompleteTimer(complete);
  273. function validate() : void {
  274. adapter.invalidateDefaults();
  275. adapter.scheduleRendering();
  276. StageProxy.root.removeChild(s);
  277. }
  278. function complete(event : Event, data : * = null) : void {
  279. var log : Array = _watcher.phasesLog;
  280. assertTrue(log, ArrayUtils.arraysEqual([
  281. s, LifeCycle.PHASE_VALIDATE
  282. ], log));
  283. }
  284. }
  285. [Test(async)]
  286. public function test_invalidate_addedToStageinValidation() : void {
  287. var s : DisplayObject = StageProxy.root.addChild(new TestDisplayObject("s"));
  288. var adapter : TestLifeCycleAdapter = new TestLifeCycleAdapter(_watcher);
  289. adapter.validateFunction = validate;
  290. adapter.calculateFunction = calculate;
  291. _lifeCycle.registerDisplayObject(s, adapter);
  292. adapter.invalidate();
  293. var s2 : DisplayObject = new TestDisplayObject("s2");
  294. var adapter2 : TestLifeCycleAdapter = new TestLifeCycleAdapter(_watcher);
  295. _lifeCycle.registerDisplayObject(s2, adapter2);
  296. adapter2.invalidate();
  297. setUpCompleteTimer(complete);
  298. function validate() : void {
  299. adapter.invalidateDefaults();
  300. }
  301. function calculate() : void {
  302. StageProxy.root.addChild(s2);
  303. }
  304. function complete(event : Event, data : * = null) : void {
  305. var log : Array = _watcher.phasesLog;
  306. assertTrue(ArrayUtils.arraysEqual([
  307. s, LifeCycle.PHASE_VALIDATE,
  308. s, LifeCycle.PHASE_CALCULATE_DEFAULTS,
  309. s2, LifeCycle.PHASE_VALIDATE
  310. ], log));
  311. }
  312. }
  313. [Test(async)]
  314. public function test_invalidate_removedAndAddedToStageinValidation() : void {
  315. var s : DisplayObject = StageProxy.root.addChild(new TestDisplayObject("s"));
  316. var adapter : TestLifeCycleAdapter = new TestLifeCycleAdapter(_watcher);
  317. adapter.validateFunction = validate;
  318. adapter.calculateFunction = calculate;
  319. _lifeCycle.registerDisplayObject(s, adapter);
  320. adapter.invalidate();
  321. var s2 : DisplayObject = StageProxy.root.addChild(new TestDisplayObject("s2"));
  322. var adapter2 : TestLifeCycleAdapter = new TestLifeCycleAdapter(_watcher);
  323. adapter2.validateFunction = validate2;
  324. _lifeCycle.registerDisplayObject(s2, adapter2);
  325. adapter2.invalidate();
  326. setUpCompleteTimer(complete);
  327. function validate() : void {
  328. adapter.invalidateDefaults();
  329. adapter.scheduleRendering();
  330. StageProxy.root.removeChild(s2);
  331. }
  332. function calculate() : void {
  333. StageProxy.root.addChild(s2);
  334. }
  335. function validate2() : void {
  336. adapter2.invalidateDefaults();
  337. adapter2.scheduleRendering();
  338. }
  339. function complete(event : Event, data : * = null) : void {
  340. var log : Array = _watcher.phasesLog;
  341. assertTrue(log, ArrayUtils.arraysEqual([
  342. s, LifeCycle.PHASE_VALIDATE,
  343. s, LifeCycle.PHASE_CALCULATE_DEFAULTS,
  344. s2, LifeCycle.PHASE_VALIDATE,
  345. s2, LifeCycle.PHASE_CALCULATE_DEFAULTS,
  346. s, LifeCycle.PHASE_RENDER,
  347. s2, LifeCycle.PHASE_RENDER
  348. ], log));
  349. }
  350. }
  351. [Test(async)]
  352. public function test_invalidate_removedFromStage_autoAdd() : void {
  353. var s : DisplayObject = StageProxy.root.addChild(new TestDisplayObject("s"));
  354. var adapter : TestLifeCycleAdapter = new TestLifeCycleAdapter(_watcher);
  355. _lifeCycle.registerDisplayObject(s, adapter);
  356. adapter.invalidate();
  357. StageProxy.root.removeChild(s);
  358. setUpCompleteTimer(complete);
  359. function complete(event : Event, data : * = null) : void {
  360. assertTrue(ArrayUtils.arraysEqual([], _watcher.phasesLog));
  361. StageProxy.root.addChild(s);
  362. setUpCompleteTimer(complete2);
  363. }
  364. function complete2(event : Event, data : * = null) : void {
  365. assertTrue(ArrayUtils.arraysEqual([s, LifeCycle.PHASE_VALIDATE], _watcher.phasesLog));
  366. }
  367. }
  368. [Test(async)]
  369. public function test_invalidate_properties() : void {
  370. var s : DisplayObject = StageProxy.root.addChild(new TestDisplayObject("s"));
  371. var adapter : TestLifeCycleAdapter = new TestLifeCycleAdapter(_watcher);
  372. adapter.validateFunction = validate;
  373. _lifeCycle.registerDisplayObject(s, adapter);
  374. adapter.invalidate("test");
  375. var results : Array = new Array();
  376. storeResults();
  377. setUpCompleteTimer(complete);
  378. function storeResults() : void {
  379. results.push(
  380. adapter.isInvalidForAnyPhase(),
  381. adapter.isInvalid(),
  382. adapter.isInvalid("test"),
  383. adapter.isInvalid("test2"),
  384. adapter.isInvalid("all_properties"),
  385. adapter.defaultIsInvalid(),
  386. adapter.defaultIsInvalid("test"),
  387. adapter.defaultIsInvalid("test2")
  388. );
  389. }
  390. function validate() : void {
  391. storeResults();
  392. }
  393. function complete(event : Event, data : * = null) : void {
  394. storeResults();
  395. assertTrue(ArrayUtils.arraysEqual([
  396. // immediately
  397. true,
  398. true, true, false, false,
  399. false, false, false,
  400. // validate
  401. true,
  402. true, true, false, false,
  403. false, false, false,
  404. // complete
  405. false,
  406. false, false,false, false,
  407. false, false, false
  408. ], results));
  409. }
  410. }
  411. [Test(async)]
  412. public function test_invalidate_allProperties() : void {
  413. var s : DisplayObject = StageProxy.root.addChild(new TestDisplayObject("s"));
  414. var adapter : TestLifeCycleAdapter = new TestLifeCycleAdapter(_watcher);
  415. adapter.validateFunction = storeResults;
  416. _lifeCycle.registerDisplayObject(s, adapter);
  417. adapter.invalidate();
  418. var results : Array = new Array();
  419. storeResults();
  420. setUpCompleteTimer(complete);
  421. function storeResults() : void {
  422. results.push(
  423. adapter.isInvalidForAnyPhase(),
  424. adapter.isInvalid(),
  425. adapter.isInvalid("test"),
  426. adapter.isInvalid("test2"),
  427. adapter.isInvalid("all_properties"),
  428. adapter.defaultIsInvalid(),
  429. adapter.defaultIsInvalid("test"),
  430. adapter.defaultIsInvalid("test2"),
  431. adapter.defaultIsInvalid("all_properties")
  432. );
  433. }
  434. function complete(event : Event, data : * = null) : void {
  435. storeResults();
  436. assertTrue(ArrayUtils.arraysEqual([
  437. // immediately
  438. true,
  439. true, true, true, true,
  440. false, false, false, false,
  441. // validate
  442. true,
  443. true, true, true, true,
  444. false, false, false, false,
  445. // complete
  446. false,
  447. false, false, false, false,
  448. false, false, false, false
  449. ], results));
  450. }
  451. }
  452. [Test(async)]
  453. public function test_invalidate_allProperties2() : void {
  454. var s : DisplayObject = StageProxy.root.addChild(new TestDisplayObject("s"));
  455. var adapter : TestLifeCycleAdapter = new TestLifeCycleAdapter(_watcher);
  456. adapter.validateFunction = validate;
  457. _lifeCycle.registerDisplayObject(s, adapter);
  458. adapter.invalidate("test");
  459. adapter.invalidate();
  460. var results : Array = new Array();
  461. storeResults();
  462. setUpCompleteTimer(complete);
  463. function validate() : void {
  464. adapter.invalidateDefaults();
  465. adapter.invalidateDefaults("test");
  466. storeResults();
  467. }
  468. function storeResults() : void {
  469. results.push(
  470. adapter.isInvalidForAnyPhase(),
  471. adapter.isInvalid(),
  472. adapter.isInvalid("test"),
  473. adapter.isInvalid("test2"),
  474. adapter.isInvalid("all_properties"),
  475. adapter.defaultIsInvalid(),
  476. adapter.defaultIsInvalid("test"),
  477. adapter.defaultIsInvalid("test2"),
  478. adapter.defaultIsInvalid("all_properties")
  479. );
  480. }
  481. function complete(event : Event, data : * = null) : void {
  482. storeResults();
  483. assertTrue(ArrayUtils.arraysEqual([
  484. // immediately
  485. true,
  486. true, true, true, true,
  487. false, false, false, false,
  488. // validate
  489. true,
  490. true, true, true, true,
  491. true, true, true, true,
  492. // complete
  493. false,
  494. false, false, false, false,
  495. false, false, false, false
  496. ], results));
  497. }
  498. }
  499. [Test(async)]
  500. public function test_invalidate_propertiesRemovedAfterPhase() : void {
  501. var s : DisplayObject = StageProxy.root.addChild(new TestDisplayObject("s"));
  502. var adapter : TestLifeCycleAdapter = new TestLifeCycleAdapter(_watcher);
  503. adapter.validateFunction = validate;
  504. adapter.calculateFunction = storeResults;
  505. _lifeCycle.registerDisplayObject(s, adapter);
  506. adapter.invalidate("test");
  507. adapter.invalidate("test2");
  508. var results : Array = new Array();
  509. storeResults();
  510. setUpCompleteTimer(complete);
  511. function validate() : void {
  512. adapter.invalidateDefaults();
  513. storeResults();
  514. }
  515. function storeResults() : void {
  516. results.push(
  517. adapter.isInvalidForAnyPhase(),
  518. adapter.isInvalid(),
  519. adapter.isInvalid("test"),
  520. adapter.isInvalid("test2"),
  521. adapter.isInvalid("test3"),
  522. adapter.defaultIsInvalid()
  523. );
  524. }
  525. function complete(event : Event, data : * = null) : void {
  526. storeResults();
  527. assertTrue(ArrayUtils.arraysEqual([
  528. // immediately
  529. true, true, true, true, false, false,
  530. // validate
  531. true, true, true, true, false, true,
  532. // calculate
  533. true, false, false, false, false, true,
  534. // complete
  535. false, false, false, false, false, false
  536. ], results));
  537. }
  538. }
  539. [Test(async)]
  540. /*
  541. * Reinvalidation in phase 2
  542. */
  543. public function test_invalidate_propertiesRemovedAfterPhase2() : void {
  544. var s : DisplayObject = StageProxy.root.addChild(new TestDisplayObject("s"));
  545. var adapter : TestLifeCycleAdapter = new TestLifeCycleAdapter(_watcher);
  546. adapter.validateFunction = validate;
  547. adapter.calculateFunction = calculate;
  548. _lifeCycle.registerDisplayObject(s, adapter);
  549. adapter.invalidate("test");
  550. adapter.invalidate("test2");
  551. var results : Array = new Array();
  552. storeResults();
  553. setUpCompleteTimer(complete);
  554. function validate() : void {
  555. adapter.invalidateDefaults();
  556. storeResults();
  557. }
  558. function calculate() : void {
  559. storeResults();
  560. if (!adapter.calculatedCount) adapter.invalidate("test");
  561. storeResults();
  562. }
  563. function storeResults() : void {
  564. results.push(
  565. adapter.isInvalidForAnyPhase(),
  566. adapter.isInvalid(),
  567. adapter.isInvalid("test"),
  568. adapter.isInvalid("test2")
  569. );
  570. }
  571. function complete(event : Event, data : * = null) : void {
  572. storeResults();
  573. var log : Array = _watcher.phasesLog;
  574. assertTrue(ArrayUtils.arraysEqual([
  575. s, LifeCycle.PHASE_VALIDATE,
  576. s, LifeCycle.PHASE_CALCULATE_DEFAULTS,
  577. s, LifeCycle.PHASE_VALIDATE,
  578. s, LifeCycle.PHASE_CALCULATE_DEFAULTS
  579. ], log));
  580. assertTrue(results, ArrayUtils.arraysEqual([
  581. // immediately
  582. true, true, true, true,
  583. // validate
  584. true, true, true, true,
  585. // calculate defaults - first
  586. true, false, false, false,
  587. // calculate defaults - second
  588. true, true, true, false,
  589. // validate #2
  590. true, true, true, false,
  591. // calculate defaults #2 - first
  592. true, false, false, false,
  593. // calculate defaults #2 - second
  594. true, false, false, false,
  595. // complete
  596. false, false, false, false
  597. ], results));
  598. }
  599. }
  600. [Test]
  601. public function test_isInvalid_allProperties() : void {
  602. var s : DisplayObject = StageProxy.root.addChild(new TestDisplayObject("s"));
  603. var adapter : TestLifeCycleAdapter = new TestLifeCycleAdapter(_watcher);
  604. _lifeCycle.registerDisplayObject(s, adapter);
  605. adapter.invalidate();
  606. assertTrue(adapter.isInvalidForAnyPhase());
  607. assertTrue(adapter.isInvalid());
  608. assertTrue(adapter.isInvalid("test"));
  609. assertTrue(adapter.isInvalid("test2"));
  610. assertFalse(adapter.defaultIsInvalid());
  611. assertFalse(adapter.defaultIsInvalid("test"));
  612. assertFalse(adapter.shouldRender());
  613. assertFalse(adapter.shouldRender("test"));
  614. }
  615. [Test]
  616. public function test_isInvalid() : void {
  617. var s : DisplayObject = StageProxy.root.addChild(new TestDisplayObject("s"));
  618. var adapter : TestLifeCycleAdapter = new TestLifeCycleAdapter(_watcher);
  619. _lifeCycle.registerDisplayObject(s, adapter);
  620. adapter.invalidate("test");
  621. assertTrue(adapter.isInvalidForAnyPhase());
  622. assertTrue(adapter.isInvalid());
  623. assertTrue(adapter.isInvalid("test"));
  624. assertFalse(adapter.isInvalid("test2"));
  625. assertFalse(adapter.defaultIsInvalid());
  626. assertFalse(adapter.defaultIsInvalid("test"));
  627. assertFalse(adapter.shouldRender());
  628. assertFalse(adapter.shouldRender("test"));
  629. }
  630. [Test]
  631. public function test_isInvalid_beforeRegistrationThrowsError() : void {
  632. var adapter : TestLifeCycleAdapter = new TestLifeCycleAdapter(_watcher);
  633. var errorThrown : Error;
  634. try {
  635. assertFalse(adapter.isInvalid());
  636. } catch (e : Error) {
  637. errorThrown = e;
  638. }
  639. assertNotNull(errorThrown);
  640. assertTrue(errorThrown is AdapterNotRegisteredError);
  641. errorThrown = null;
  642. try {
  643. assertFalse(adapter.isInvalid("test"));
  644. } catch (e : Error) {
  645. errorThrown = e;
  646. }
  647. assertNotNull(errorThrown);
  648. assertTrue(errorThrown is AdapterNotRegisteredError);
  649. }
  650. [Test]
  651. public function test_defaultIsInvalid_beforeRegistrationThrowsError() : void {
  652. var adapter : TestLifeCycleAdapter = new TestLifeCycleAdapter(_watcher);
  653. var errorThrown : Error;
  654. try {
  655. assertFalse(adapter.defaultIsInvalid());
  656. } catch (e : Error) {
  657. errorThrown = e;
  658. }
  659. assertNotNull(errorThrown);
  660. assertTrue(errorThrown is AdapterNotRegisteredError);
  661. errorThrown = null;
  662. try {
  663. assertFalse(adapter.defaultIsInvalid("test"));
  664. } catch (e : Error) {
  665. errorThrown = e;
  666. }
  667. assertNotNull(errorThrown);
  668. assertTrue(errorThrown is AdapterNotRegisteredError);
  669. }
  670. [Test]
  671. public function test_shouldRender_beforeRegistrationThrowsError() : void {
  672. var adapter : TestLifeCycleAdapter = new TestLifeCycleAdapter(_watcher);
  673. var errorThrown : Error;
  674. try {
  675. assertFalse(adapter.shouldRender());
  676. } catch (e : Error) {
  677. errorThrown = e;
  678. }
  679. assertNotNull(errorThrown);
  680. assertTrue(errorThrown is AdapterNotRegisteredError);
  681. errorThrown = null;
  682. try {
  683. assertFalse(adapter.shouldRender("test"));
  684. } catch (e : Error) {
  685. errorThrown = e;
  686. }
  687. assertNotNull(errorThrown);
  688. assertTrue(errorThrown is AdapterNotRegisteredError);
  689. }
  690. [Test(async)]
  691. public function test_validateNow_inValidationThrowsError() : void {
  692. var s : DisplayObject = StageProxy.root.addChild(new TestDisplayObject("s"));
  693. var adapter : TestLifeCycleAdapter = new TestLifeCycleAdapter(_watcher);
  694. adapter.validateFunction = validate;
  695. _lifeCycle.registerDisplayObject(s, adapter);
  696. adapter.invalidate();
  697. var errorThrown : Error;
  698. setUpCompleteTimer(complete);
  699. function validate() : void {
  700. try {
  701. adapter.validateNow();
  702. } catch (e : Error) {
  703. errorThrown = e;
  704. }
  705. }
  706. function complete(event : Event, data : * = null) : void {
  707. assertNotNull(errorThrown);
  708. assertTrue(errorThrown is ValidateNowInRunningCycleError);
  709. }
  710. }
  711. [Test(async)]
  712. public function test_validateNow() : void {
  713. var s : DisplayObject = StageProxy.root.addChild(new TestDisplayObject("s"));
  714. var adapter : TestLifeCycleAdapter = new TestLifeCycleAdapter(_watcher);
  715. _lifeCycle.registerDisplayObject(s, adapter);
  716. adapter.invalidate();
  717. adapter.validateNow();
  718. assertTrue(ArrayUtils.arraysEqual([s, LifeCycle.PHASE_VALIDATE], _watcher.phasesLog));
  719. }
  720. [Test]
  721. public function test_validateNow_beforeRegistrationThrowsError() : void {
  722. var adapter : TestLifeCycleAdapter = new TestLifeCycleAdapter(_watcher);
  723. var errorThrown : Error;
  724. try {
  725. adapter.validateNow();
  726. } catch (e : Error) {
  727. errorThrown = e;
  728. }
  729. assertNotNull(errorThrown);
  730. assertTrue(errorThrown is AdapterNotRegisteredError);
  731. }
  732. [Test(async)]
  733. public function test_validateNow_withoutInvalidation() : void {
  734. var s : DisplayObject = StageProxy.root.addChild(new TestDisplayObject("s"));
  735. var adapter : TestLifeCycleAdapter = new TestLifeCycleAdapter(_watcher);
  736. _lifeCycle.registerDisplayObject(s, adapter);
  737. adapter.validateNow();
  738. assertTrue(ArrayUtils.arraysEqual([], _watcher.validateLog));
  739. }
  740. [Test(async)]
  741. public function test_validateNow_validatesOnlyTarget() : void {
  742. var s : DisplayObject = StageProxy.root.addChild(new TestDisplayObject("s"));
  743. var adapter : TestLifeCycleAdapter = new TestLifeCycleAdapter(_watcher);
  744. _lifeCycle.registerDisplayObject(s, adapter);
  745. var s2 : DisplayObject = StageProxy.root.addChild(new TestDisplayObject("s2"));
  746. var adapter2 : TestLifeCycleAdapter = new TestLifeCycleAdapter(_watcher);
  747. _lifeCycle.registerDisplayObject(s2, adapter2);
  748. adapter.invalidate();
  749. adapter2.invalidate();
  750. adapter.validateNow();
  751. assertTrue(ArrayUtils.arraysEqual([s, LifeCycle.PHASE_VALIDATE], _watcher.phasesLog));
  752. setUpCompleteTimer(complete);
  753. function complete(event : Event, data : * = null) : void {
  754. assertTrue(ArrayUtils.arraysEqual([s2, LifeCycle.PHASE_VALIDATE], _watcher.phasesLog));
  755. }
  756. }
  757. [Test(async)]
  758. public function test_validateNow_validatesOnlyTarget2() : void {
  759. var s : Sprite = StageProxy.root.addChild(new TestDisplayObject("s")) as Sprite;
  760. var adapter : TestLifeCycleAdapter = new TestLifeCycleAdapter(_watcher);
  761. _lifeCycle.registerDisplayObject(s, adapter);
  762. var s2 : Sprite = s.addChild(new TestDisplayObject("s2")) as Sprite;
  763. var adapter2 : TestLifeCycleAdapter = new TestLifeCycleAdapter(_watcher);
  764. _lifeCycle.registerDisplayObject(s2, adapter2);
  765. var s3 : Sprite = StageProxy.root.addChild(new TestDisplayObject("s3")) as Sprite;
  766. var adapter3 : TestLifeCycleAdapter = new TestLifeCycleAdapter(_watcher);
  767. _lifeCycle.registerDisplayObject(s3, adapter3);
  768. var s4 : Sprite = s3.addChild(new TestDisplayObject("s4")) as Sprite;
  769. var adapter4 : TestLifeCycleAdapter = new TestLifeCycleAdapter(_watcher);
  770. _lifeCycle.registerDisplayObject(s4, adapter4);
  771. adapter.invalidate();
  772. adapter2.invalidate();
  773. adapter3.invalidate();
  774. adapter4.invalidate();
  775. adapter.validateNow();
  776. assertTrue(ArrayUtils.arraysEqual([s, LifeCycle.PHASE_VALIDATE, s2, LifeCycle.PHASE_VALIDATE], _watcher.phasesLog));
  777. setUpCompleteTimer(complete);
  778. function complete(event : Event, data : * = null) : void {
  779. assertTrue(ArrayUtils.arraysEqual([s3, LifeCycle.PHASE_VALIDATE, s4, LifeCycle.PHASE_VALIDATE], _watcher.phasesLog));
  780. }
  781. }
  782. [Test]
  783. public function test_onAddedToStage_onRemovedFromStage() : void {
  784. var s : Sprite = new TestDisplayObject("s");
  785. var adapter : TestLifeCycleAdapter = new TestLifeCycleAdapter(_watcher);
  786. _lifeCycle.registerDisplayObject(s, adapter);
  787. assertEquals(0, adapter.addedToStageCount);
  788. assertEquals(0, adapter.removedFromStageCount);
  789. assertTrue(ArrayUtils.arraysEqual([], _watcher.addedLog));
  790. assertTrue(ArrayUtils.arraysEqual([], _watcher.removedLog));
  791. StageProxy.root.addChild(s);
  792. assertEquals(1, adapter.addedToStageCount);
  793. assertEquals(0, adapter.removedFromStageCount);
  794. assertTrue(ArrayUtils.arraysEqual([s], _watcher.addedLog));
  795. assertTrue(ArrayUtils.arraysEqual([], _watcher.removedLog));
  796. StageProxy.root.removeChild(s);
  797. assertEquals(1, adapter.addedToStageCount);
  798. assertEquals(1, adapter.removedFromStageCount);
  799. assertTrue(ArrayUtils.arraysEqual([], _watcher.addedLog));
  800. assertTrue(ArrayUtils.arraysEqual([s], _watcher.removedLog));
  801. StageProxy.root.addChild(s);
  802. assertEquals(2, adapter.addedToStageCount);
  803. assertEquals(1, adapter.removedFromStageCount);
  804. assertTrue(ArrayUtils.arraysEqual([s], _watcher.addedLog));
  805. assertTrue(ArrayUtils.arraysEqual([], _watcher.removedLog));
  806. }
  807. [Test]
  808. public function test_onAddedToStage_onRemovedFromStage2() : void {
  809. var s : Sprite = new TestDisplayObject("s");
  810. var adapter : TestLifeCycleAdapter = new TestLifeCycleAdapter(_watcher);
  811. _lifeCycle.registerDisplayObject(s, adapter);
  812. adapter.invalidate();
  813. var s2 : Sprite = s.addChild(new TestDisplayObject("s2")) as Sprite;
  814. var adapter2 : TestLifeCycleAdapter = new TestLifeCycleAdapter(_watcher);
  815. _lifeCycle.registerDisplayObject(s2, adapter2);
  816. assertEquals(0, adapter.addedToStageCount);
  817. assertEquals(0, adapter.removedFromStageCount);
  818. assertEquals(0, adapter2.addedToStageCount);
  819. assertEquals(0, adapter2.removedFromStageCount);
  820. assertTrue(ArrayUtils.arraysEqual([], _watcher.addedLog));
  821. assertTrue(ArrayUtils.arraysEqual([], _watcher.removedLog));
  822. StageProxy.root.addChild(s);
  823. assertEquals(1, adapter.addedToStageCount);
  824. assertEquals(0, adapter.removedFromStageCount);
  825. assertEquals(1, adapter2.addedToStageCount);
  826. assertEquals(0, adapter2.removedFromStageCount);
  827. assertTrue(ArrayUtils.arraysEqual([s, s2], _watcher.addedLog));
  828. assertTrue(ArrayUtils.arraysEqual([], _watcher.removedLog));
  829. StageProxy.root.removeChild(s);
  830. assertEquals(1, adapter.addedToStageCount);
  831. assertEquals(1, adapter.removedFromStageCount);
  832. assertEquals(1, adapter.addedToStageCount);
  833. assertEquals(1, adapter.removedFromStageCount);
  834. assertTrue(ArrayUtils.arraysEqual([], _watcher.addedLog));
  835. assertTrue(ArrayUtils.arraysEqual([s, s2], _watcher.removedLog));
  836. StageProxy.root.addChild(s);
  837. assertEquals(2, adapter.addedToStageCount);
  838. assertEquals(1, adapter.removedFromStageCount);
  839. assertEquals(2, adapter.addedToStageCount);
  840. assertEquals(1, adapter.removedFromStageCount);
  841. assertTrue(ArrayUtils.arraysEqual([s, s2], _watcher.addedLog));
  842. assertTrue(ArrayUtils.arraysEqual([], _watcher.removedLog));
  843. }
  844. [Test(async)]
  845. public function test_onRemovedFromStage_onAddedToStage_inValidation() : void {
  846. var s : Sprite = StageProxy.root.addChild(new TestDisplayObject("s")) as Sprite;
  847. var adapter : TestLifeCycleAdapter = new TestLifeCycleAdapter(_watcher);
  848. adapter.validateFunction = validate;
  849. _lifeCycle.registerDisplayObject(s, adapter);
  850. var s2 : Sprite = s.addChild(new TestDisplayObject("s2")) as Sprite;
  851. var adapter2 : TestLifeCycleAdapter = new TestLifeCycleAdapter(_watcher);
  852. _lifeCycle.registerDisplayObject(s2, adapter2);
  853. adapter.invalidate();
  854. assertEquals(0, adapter.addedToStageCount);
  855. assertEquals(0, adapter.removedFromStageCount);
  856. assertEquals(0, adapter2.addedToStageCount);
  857. assertEquals(0, adapter2.removedFromStageCount);
  858. assertTrue(ArrayUtils.arraysEqual([], _watcher.addedLog));
  859. assertTrue(ArrayUtils.arraysEqual([], _watcher.removedLog));
  860. setUpCompleteTimer(complete);
  861. function validate() : void {
  862. if (!adapter.validateCount) { // else, it will schedule a second run
  863. StageProxy.root.removeChild(s);
  864. StageProxy.root.addChild(s);
  865. }
  866. }
  867. function complete(event : Event, data : * = null) : void {
  868. assertEquals("1a", 1, adapter.addedToStageCount);
  869. assertEquals("1r", 1, adapter.removedFromStageCount);
  870. assertEquals("2a", 1, adapter2.addedToStageCount);
  871. assertEquals("2r", 1, adapter2.removedFromStageCount);
  872. assertTrue(ArrayUtils.arraysEqual([s, s2], _watcher.addedLog));
  873. assertTrue(ArrayUtils.arraysEqual([s, s2], _watcher.removedLog));
  874. }
  875. }
  876. [Test]
  877. public function test_onRemovedFromStage_onAddedToStage_afterCleanUp() : void {
  878. var s : Sprite = StageProxy.root.addChild(new TestDisplayObject("s")) as Sprite;
  879. var adapter : TestLifeCycleAdapter = new TestLifeCycleAdapter(_watcher);
  880. _lifeCycle.registerDisplayObject(s, adapter);
  881. var s2 : Sprite = s.addChild(new TestDisplayObject("s2")) as Sprite;
  882. var adapter2 : TestLifeCycleAdapter = new TestLifeCycleAdapter(_watcher);
  883. _lifeCycle.registerDisplayObject(s2, adapter2);
  884. assertEquals(0, adapter.addedToStageCount);
  885. assertEquals(0, adapter.removedFromStageCount);
  886. assertEquals(0, adapter2.addedToStageCount);
  887. assertEquals(0, adapter2.removedFromStageCount);
  888. assertTrue(ArrayUtils.arraysEqual([], _watcher.addedLog));
  889. assertTrue(ArrayUtils.arraysEqual([], _watcher.removedLog));
  890. _lifeCycle.cleanUp();
  891. StageProxy.root.removeChild(s);
  892. StageProxy.root.addChild(s);
  893. assertEquals(0, adapter.addedToStageCount);
  894. assertEquals(0, adapter.removedFromStageCount);
  895. assertEquals(0, adapter2.addedToStageCount);
  896. assertEquals(0, adapter2.removedFromStageCount);
  897. assertTrue(ArrayUtils.arraysEqual([], _watcher.addedLog));
  898. assertTrue(ArrayUtils.arraysEqual([], _watcher.removedLog));
  899. }
  900. [Test]
  901. public function test_onRemovedFromStage_onAddedToStage_afterUnregister() : void {
  902. var s : Sprite = StageProxy.root.addChild(new TestDisplayObject("s")) as Sprite;
  903. var adapter : TestLifeCycleAdapter = new TestLifeCycleAdapter(_watcher);
  904. _lifeCycle.registerDisplayObject(s, adapter);
  905. var s2 : Sprite = s.addChild(new TestDisplayObject("s2")) as Sprite;
  906. var adapter2 : TestLifeCycleAdapter = new TestLifeCycleAdapter(_watcher);
  907. _lifeCycle.registerDisplayObject(s2, adapter2);
  908. assertEquals(0, adapter.addedToStageCount);
  909. assertEquals(0, adapter.removedFromStageCount);
  910. assertEquals(0, adapter2.addedToStageCount);
  911. assertEquals(0, adapter2.removedFromStageCount);
  912. assertTrue(ArrayUtils.arraysEqual([], _watcher.addedLog));
  913. assertTrue(ArrayUtils.arraysEqual([], _watcher.removedLog));
  914. _lifeCycle.unregisterDisplayObject(s);
  915. StageProxy.root.removeChild(s);
  916. StageProxy.root.addChild(s);
  917. assertEquals(0, adapter.addedToStageCount);
  918. assertEquals(0, adapter.removedFromStageCount);
  919. assertEquals(1, adapter2.addedToStageCount);
  920. assertEquals(1, adapter2.removedFromStageCount);
  921. assertTrue(ArrayUtils.arraysEqual([s2], _watcher.addedLog));
  922. assertTrue(ArrayUtils.arraysEqual([s2], _watcher.removedLog));
  923. }
  924. [Test]
  925. public function test_nestLevel() : void {
  926. var s : Sprite = StageProxy.root.addChild(new TestDisplayObject("s")) as Sprite;
  927. var adapter : TestLifeCycleAdapter = new TestLifeCycleAdapter(_watcher);
  928. _lifeCycle.registerDisplayObject(s, adapter);
  929. var s2 : Sprite = s.addChild(new TestDisplayObject("s2")) as Sprite;
  930. var adapter2 : TestLifeCycleAdapter = new TestLifeCycleAdapter(_watcher);
  931. _lifeCycle.registerDisplayObject(s2, adapter2);
  932. assertEquals(2, adapter.nestLevel);
  933. assertEquals(3, adapter2.nestLevel);
  934. StageProxy.root.removeChild(s);
  935. assertEquals(-1, adapter.nestLevel);
  936. assertEquals(-1, adapter2.nestLevel);
  937. StageProxy.root.addChild(s2);
  938. s2.addChild(s);
  939. assertEquals(3, adapter.nestLevel);
  940. assertEquals(2, adapter2.nestLevel);
  941. }
  942. [Test]
  943. public function test_nestLevel_afterCleanUp() : void {
  944. var s : Sprite = StageProxy.root.addChild(new TestDisplayObject("s")) as Sprite;
  945. var adapter : TestLifeCycleAdapter = new TestLifeCycleAdapter(_watcher);
  946. _lifeCycle.registerDisplayObject(s, adapter);
  947. var s2 : Sprite = s.addChild(new TestDisplayObject("s2")) as Sprite;
  948. var adapter2 : TestLifeCycleAdapter = new TestLifeCycleAdapter(_watcher);
  949. _lifeCycle.registerDisplayObject(s2, adapter2);
  950. assertEquals(2, adapter.nestLevel);
  951. assertEquals(3, adapter2.nestLevel);
  952. _lifeCycle.cleanUp();
  953. assertEquals(-1, adapter.nestLevel);
  954. assertEquals(-1, adapter2.nestLevel);
  955. StageProxy.root.removeChild(s);
  956. assertEquals(-1, adapter.nestLevel);
  957. assertEquals(-1, adapter2.nestLevel);
  958. StageProxy.root.addChild(s);
  959. assertEquals(-1, adapter.nestLevel);
  960. assertEquals(-1, adapter2.nestLevel);
  961. }
  962. [Test]
  963. public function test_nestLevel_afterUnregister() : void {
  964. var s : Sprite = StageProxy.root.addChild(new TestDisplayObject("s")) as Sprite;
  965. var adapter : TestLifeCycleAdapter = new TestLifeCycleAdapter(_watcher);
  966. _lifeCycle.registerDisplayObject(s, adapter);
  967. var s2 : Sprite = s.addChild(new TestDisplayObject("s2")) as Sprite;
  968. var adapter2 : TestLifeCycleAdapter = new TestLifeCycleAdapter(_watcher);
  969. _lifeCycle.registerDisplayObject(s2, adapter2);
  970. assertEquals(2, adapter.nestLevel);
  971. assertEquals(3, adapter2.nestLevel);
  972. _lifeCycle.unregisterDisplayObject(s);
  973. assertEquals(-1, adapter.nestLevel);
  974. assertEquals(3, adapter2.nestLevel);
  975. StageProxy.root.removeChild(s);
  976. assertEquals(-1, adapter.nestLevel);
  977. assertEquals(-1, adapter2.nestLevel);
  978. StageProxy.root.addChild(s);
  979. assertEquals(-1, adapter.nestLevel);
  980. assertEquals(3, adapter2.nestLevel);
  981. }
  982. [Test(async)]
  983. public function test_invalidate_withinRenderThrowsError() : void {
  984. var s : Sprite = StageProxy.root.addChild(new TestDisplayObject("s")) as Sprite;
  985. var adapter : TestLifeCycleAdapter = new TestLifeCycleAdapter(_watcher);
  986. adapter.validateFunction = validate;
  987. adapter.renderFunction = render;
  988. _lifeCycle.registerDisplayObject(s, adapter);
  989. adapter.invalidate();
  990. var errorThrown : Error;
  991. setUpCompleteTimer(complete);
  992. function validate() : void {
  993. adapter.scheduleRendering();
  994. }
  995. function render() : void {
  996. try {
  997. adapter.invalidate();
  998. } catch (e : Error) {
  999. errorThrown = e;
  1000. }
  1001. }
  1002. function complete(event : Event, data : * = null) : void {
  1003. assertNotNull(errorThrown);
  1004. assertTrue(errorThrown is InvalidationNotAllowedHereError);
  1005. assertEquals(
  1006. InvalidationNotAllowedHereError.INVALIDATE_FROM_RENDER,
  1007. InvalidationNotAllowedHereError(errorThrown).messageKey
  1008. );
  1009. assertFalse(adapter.isInvalidForAnyPhase());
  1010. assertFalse(adapter.isInvalid());
  1011. assertEquals(1, adapter.validateCount);
  1012. assertTrue(ArrayUtils.arraysEqual([
  1013. s, LifeCycle.PHASE_VALIDATE,
  1014. s, LifeCycle.PHASE_RENDER
  1015. ], _watcher.phasesLog));
  1016. }
  1017. }
  1018. [Test(async)]
  1019. public function test_invalidate_othersWithinRenderThrowsError() : void {
  1020. var s : Sprite = StageProxy.root.addChild(new TestDisplayObject("s")) as Sprite;
  1021. var adapter : TestLifeCycleAdapter = new TestLifeCycleAdapter(_watcher);
  1022. adapter.validateFunction = validate;
  1023. adapter.renderFunction = render;
  1024. _lifeCycle.registerDisplayObject(s, adapter);
  1025. adapter.invalidate();
  1026. var s2 : Sprite = StageProxy.root.addChild(new TestDisplayObject("s")) as Sprite;
  1027. var adapter2 : TestLifeCycleAdapter = new TestLifeCycleAdapter(_watcher);
  1028. _lifeCycle.registerDisplayObject(s2, adapter2);
  1029. var errorThrown : Error;
  1030. setUpCompleteTimer(complete);
  1031. function validate() : void {
  1032. adapter.scheduleRendering();
  1033. }
  1034. function render() : void {
  1035. try {
  1036. adapter2.invalidate();
  1037. } catch (e : Error) {
  1038. errorThrown = e;
  1039. }
  1040. }
  1041. function complete(event : Event, data : * = null) : void {
  1042. assertNotNull(errorThrown);
  1043. assertTrue(errorThrown is InvalidationNotAllowedHereError);
  1044. assertEquals(
  1045. InvalidationNotAllowedHereError.INVALIDATE_FROM_RENDER,
  1046. InvalidationNotAllowedHereError(errorThrown).messageKey
  1047. );
  1048. assertFalse(adapter2.isInvalidForAnyPhase());
  1049. assertFalse(adapter2.isInvalid());
  1050. assertEquals(0, adapter2.validateCount);
  1051. assertTrue(ArrayUtils.arraysEqual([
  1052. s, LifeCycle.PHASE_VALIDATE,
  1053. s, LifeCycle.PHASE_RENDER
  1054. ], _watcher.phasesLog));
  1055. }
  1056. }
  1057. [Test(async)]
  1058. public function test_invalidateDefaults_outsideOfCycleThrowsError() : void {
  1059. var s : Sprite = StageProxy.root.addChild(new TestDisplayObject("s")) as Sprite;
  1060. var adapter : TestLifeCycleAdapter = new TestLifeCycleAdapter(_watcher);
  1061. _lifeCycle.registerDisplayObject(s, adapter);
  1062. var errorThrown : Error;
  1063. try {
  1064. adapter.invalidateDefaults();
  1065. } catch (e : Error) {
  1066. errorThrown = e;
  1067. }
  1068. assertNotNull(errorThrown);
  1069. assertTrue(errorThrown is InvalidationNotAllowedHereError);
  1070. assertEquals(
  1071. InvalidationNotAllowedHereError.INVALIDATE_FOR_SECONDARY_PHASE_OUTSIDE_OF_CYCLE,
  1072. InvalidationNotAllowedHereError(errorThrown).messageKey
  1073. );
  1074. assertFalse(adapter.isInvalidForAnyPhase());
  1075. assertFalse(adapter.defaultIsInvalid());
  1076. setUpCompleteTimer(complete);
  1077. function complete(event : Event, data : * = null) : void {
  1078. assertEquals(0, adapter.calculatedCount);
  1079. assertTrue(ArrayUtils.arraysEqual([], _watcher.phasesLog));
  1080. }
  1081. }
  1082. [Test(async)]
  1083. public function test_invalidateDefaults_withinCalculateDefaultsThrowsError() : void {
  1084. var s : Sprite = StageProxy.root.addChild(new TestDisplayObject("s")) as Sprite;
  1085. var adapter : TestLifeCycleAdapter = new TestLifeCycleAdapter(_watcher);
  1086. adapter.validateFunction = validate;
  1087. adapter.calculateFunction = calculate;
  1088. _lifeCycle.registerDisplayObject(s, adapter);
  1089. adapter.invalidate();
  1090. var errorThrown : Error;
  1091. setUpCompleteTimer(complete);
  1092. function validate() : void {
  1093. adapter.invalidateDefaults();
  1094. }
  1095. function calculate() : void {
  1096. try {
  1097. adapter.invalidateDefaults();
  1098. } catch (e : Error) {
  1099. errorThrown = e;
  1100. }
  1101. }
  1102. function complete(event : Event, data : * = null) : void {
  1103. assertNotNull(errorThrown);
  1104. assertTrue(errorThrown is InvalidationNotAllowedHereError);
  1105. assertEquals(
  1106. InvalidationNotAllowedHereError.INVALIDATE_DEFAULTS_FROM_CALCULATE_DEFAULTS,
  1107. InvalidationNotAllowedHereError(errorThrown).messageKey
  1108. );
  1109. assertFalse(adapter.isInvalidForAnyPhase());
  1110. assertFalse(adapter.defaultIsInvalid());
  1111. assertEquals(1, adapter.calculatedCount);
  1112. assertTrue(ArrayUtils.arraysEqual([
  1113. s, LifeCycle.PHASE_VALIDATE,
  1114. s, LifeCycle.PHASE_CALCULATE_DEFAULTS
  1115. ], _watcher.phasesLog));
  1116. }
  1117. }
  1118. [Test(async)]
  1119. public function test_invalidateDefaults_fromOtherComponentThrowsError() : void {
  1120. var s : Sprite = StageProxy.root.addChild(new TestDisplayObject("s")) as Sprite;
  1121. var adapter : TestLifeCycleAdapter = new TestLifeCycleAdapter(_watcher);
  1122. adapter.validateFunction = validate;
  1123. _lifeCycle.registerDisplayObject(s, adapter);
  1124. adapter.invalidate();
  1125. var s2 : Sprite = StageProxy.root.addChild(new TestDisplayObject("s")) as Sprite;
  1126. var adapter2 : TestLifeCycleAdapter = new TestLifeCycleAdapter(_watcher);
  1127. _lifeCycle.registerDisplayObject(s2, adapter2);
  1128. var errorThrown : Error;
  1129. setUpCompleteTimer(complete);
  1130. function validate() : void {
  1131. try {
  1132. adapter2.invalidateDefaults();
  1133. } catch (e : Error) {
  1134. errorThrown = e;
  1135. }
  1136. }
  1137. function complete(event : Event, data : * = null) : void {
  1138. assertNotNull(errorThrown);
  1139. assertTrue(errorThrown is InvalidationNotAllowedHereError);
  1140. assertEquals(
  1141. InvalidationNotAllowedHereError.INVALIDATE_FOR_SECONDARY_PHASE_NOT_FROM_CURRENT_OBJECT,
  1142. InvalidationNotAllowedHereError(errorThrown).messageKey
  1143. );
  1144. assertFalse(adapter2.isInvalidForAnyPhase());
  1145. assertFalse(adapter2.defaultIsInvalid());
  1146. assertEquals(0, adapter2.calculatedCount);
  1147. assertTrue(ArrayUtils.arraysEqual([
  1148. s, LifeCycle.PHASE_VALIDATE
  1149. ], _watcher.phasesLog));
  1150. }
  1151. }
  1152. [Test(async)]
  1153. public function test_invalidateDefaults_withinRenderThrowsError() : void {
  1154. var s : Sprite = StageProxy.root.addChild(new TestDisplayObject("s")) as Sprite;
  1155. var adapter : TestLifeCycleAdapter = new TestLifeCycleAdapter(_watcher);
  1156. adapter.validateFunction = validate;
  1157. adapter.renderFunction = render;
  1158. _lifeCycle.registerDisplayObject(s, adapter);
  1159. adapter.invalidate();
  1160. var errorThrown : Error;
  1161. setUpCompleteTimer(complete);
  1162. function validate() : void {
  1163. adapter.scheduleRendering();
  1164. }
  1165. function render() : void {
  1166. try {
  1167. adapter.invalidateDefaults();
  1168. } catch (e : Error) {
  1169. errorThrown = e;
  1170. }
  1171. }
  1172. function complete(event : Event, data : * = null) : void {
  1173. assertNotNull(errorThrown);
  1174. assertTrue(errorThrown is InvalidationNotAllowedHereError);
  1175. assertEquals(
  1176. InvalidationNotAllowedHereError.INVALIDATE_FROM_RENDER,
  1177. InvalidationNotAllowedHereError(errorThrown).messageKey
  1178. );
  1179. assertFalse(adapter.isInvalidForAnyPhase());
  1180. assertFalse(adapter.defaultIsInvalid());
  1181. assertEquals(0, adapter.calculatedCount);
  1182. assertTrue(ArrayUtils.arraysEqual([
  1183. s, LifeCycle.PHASE_VALIDATE,
  1184. s, LifeCycle.PHASE_RENDER
  1185. ], _watcher.phasesLog));
  1186. }
  1187. }
  1188. [Test(async)]
  1189. public function test_scheduleRendering_outsideOfCycleThrowsError() : void {
  1190. var s : Sprite = StageProxy.root.addChild(new TestDisplayObject("s")) as Sprite;
  1191. var adapter : TestLifeCycleAdapter = new TestLifeCycleAdapter(_watcher);
  1192. _lifeCycle.registerDisplayObject(s, adapter);
  1193. var errorThrown : Error;
  1194. try {
  1195. adapter.scheduleRendering();
  1196. } catch (e : Error) {
  1197. errorThrown = e;
  1198. }
  1199. assertNotNull(errorThrown);
  1200. assertTrue(errorThrown is InvalidationNotAllowedHereError);
  1201. assertEquals(
  1202. InvalidationNotAllowedHereError.INVALIDATE_FOR_SECONDARY_PHASE_OUTSIDE_OF_CYCLE,
  1203. InvalidationNotAllowedHereError(errorThrown).messageKey
  1204. );
  1205. assertFalse(adapter.isInvalidForAnyPhase());
  1206. assertFalse(adapter.shouldRender());
  1207. setUpCompleteTimer(complete);
  1208. function complete(event : Event, data : * = null) : void {
  1209. assertEquals(0, adapter.renderCount);
  1210. assertTrue(ArrayUtils.arraysEqual([], _watcher.phasesLog));
  1211. }
  1212. }
  1213. [Test(async)]
  1214. public function test_scheduleRendering_fromOtherComponentThrowsError() : void {
  1215. var s : Sprite = StageProxy.root.addChild(new TestDisplayObject("s")) as Sprite;
  1216. var adapter : TestLifeCycleAdapter = new TestLifeCycleAdapter(_watcher);
  1217. adapter.validateFunction = validate;
  1218. _lifeCycle.registerDisplayObject(s, adapter);
  1219. adapter.invalidate();
  1220. var s2 : Sprite = StageProxy.root.addChild(new TestDisplayObject("s")) as Sprite;
  1221. var adapter2 : TestLifeCycleAdapter = new TestLifeCycleAdapter(_watcher);
  1222. _lifeCycle.registerDisplayObject(s2, adapter2);
  1223. var errorThrown : Error;
  1224. setUpCompleteTimer(complete);
  1225. function validate() : void {
  1226. try {
  1227. adapter2.scheduleRendering();
  1228. } catch (e : Error) {
  1229. errorThrown = e;
  1230. }
  1231. }
  1232. function complete(event : Event, data : * = null) : void {
  1233. assertNotNull(errorThrown);
  1234. assertTrue(errorThrown is InvalidationNotAllowedHereError);
  1235. assertEquals(
  1236. InvalidationNotAllowedHereError.INVALIDATE_FOR_SECONDARY_PHASE_NOT_FROM_CURRENT_OBJECT,
  1237. InvalidationNotAllowedHereError(errorThrown).messageKey
  1238. );
  1239. assertFalse(adapter2.isInvalidForAnyPhase());
  1240. assertFalse(adapter2.shouldRender());
  1241. assertEquals(0, adapter2.renderCount);
  1242. assertTrue(ArrayUtils.arraysEqual([
  1243. s, LifeCycle.PHASE_VALIDATE
  1244. ], _watcher.phasesLog));
  1245. }
  1246. }
  1247. [Test(async)]
  1248. public function test_scheduleRendering_withinRenderThrowsError() : void {
  1249. var s : Sprite = StageProxy.root.addChild(new TestDisplayObject("s")) as Sprite;
  1250. var adapter : TestLifeCycleAdapter = new TestLifeCycleAdapter(_watcher);
  1251. adapter.validateFunction = validate;
  1252. adapter.renderFunction = render;
  1253. _lifeCycle.registerDisplayObject(s, adapter);
  1254. adapter.invalidate();
  1255. var errorThrown : Error;
  1256. setUpCompleteTimer(complete);
  1257. function validate() : void {
  1258. adapter.scheduleRendering();
  1259. }
  1260. function render() : void {
  1261. try {
  1262. adapter.scheduleRendering();
  1263. } catch (e : Error) {
  1264. errorThrown = e;
  1265. }
  1266. }
  1267. function complete(event : Event, data : * = null) : void {
  1268. assertNotNull(errorThrown);
  1269. assertTrue(errorThrown is InvalidationNotAllowedHereError);
  1270. assertEquals(
  1271. InvalidationNotAllowedHereError.INVALIDATE_FROM_RENDER,
  1272. InvalidationNotAllowedHereError(errorThrown).messageKey
  1273. );
  1274. assertFalse(adapter.isInvalidForAnyPhase());
  1275. assertFalse(adapter.shouldRender());
  1276. assertEquals(1, adapter.renderCount);
  1277. assertTrue(ArrayUtils.arraysEqual([
  1278. s, LifeCycle.PHASE_VALIDATE,
  1279. s, LifeCycle.PHASE_RENDER
  1280. ], _watcher.phasesLog));
  1281. }
  1282. }
  1283. }
  1284. }