/test/org/robotlegs/v2/view/impl/StageWatcher_OptimisationTests.as

https://github.com/ZackPierce/robotlegs-framework
ActionScript | 121 lines | 92 code | 14 blank | 15 comment | 0 complexity | d7e7458e699a92afbbc6c9b035f2c300 MD5 | raw file
  1. //------------------------------------------------------------------------------
  2. // Copyright (c) 2011 the original author or authors. All Rights Reserved.
  3. //
  4. // NOTICE: You are permitted to use, modify, and distribute this file
  5. // in accordance with the terms of the license agreement accompanying it.
  6. //------------------------------------------------------------------------------
  7. package org.robotlegs.v2.view.impl
  8. {
  9. import flash.display.DisplayObject;
  10. import flash.display.DisplayObjectContainer;
  11. import flash.display.Sprite;
  12. import mx.core.UIComponent;
  13. import org.fluint.uiImpersonation.UIImpersonator;
  14. import org.hamcrest.assertThat;
  15. import org.hamcrest.object.equalTo;
  16. import org.robotlegs.v2.view.api.IViewClassInfo;
  17. import org.robotlegs.v2.view.api.IViewWatcher;
  18. import org.robotlegs.v2.view.impl.support.ViewHandlerSupport;
  19. public class StageWatcher_OptimisationTests
  20. {
  21. /*============================================================================*/
  22. /* Protected Properties */
  23. /*============================================================================*/
  24. protected var container:DisplayObjectContainer;
  25. protected var group:UIComponent;
  26. protected var watcher:IViewWatcher;
  27. /*============================================================================*/
  28. /* Test Setup and Teardown */
  29. /*============================================================================*/
  30. [Before(ui)]
  31. public function setUp():void
  32. {
  33. group = new UIComponent()
  34. container = new Sprite();
  35. watcher = new StageWatcher();
  36. group.addChild(container)
  37. UIImpersonator.addChild(group);
  38. }
  39. [After]
  40. public function tearDown():void
  41. {
  42. watcher = null;
  43. UIImpersonator.removeAllChildren();
  44. }
  45. /*============================================================================*/
  46. /* Tests */
  47. /*============================================================================*/
  48. [Test]
  49. public function a_handler_that_doesnt_handle_a_view_SHOULD_be_reconsulted_after_invalidation():void
  50. {
  51. var addedCallCount:int;
  52. const handler:ViewHandlerSupport = new ViewHandlerSupport(
  53. 0x1,
  54. 0x0,
  55. false,
  56. function onAdded(view:DisplayObject, info:IViewClassInfo, response:uint):void
  57. {
  58. addedCallCount++;
  59. });
  60. watcher.addHandler(handler, container);
  61. container.addChild(new Sprite());
  62. container.addChild(new Sprite());
  63. handler.invalidate();
  64. container.addChild(new Sprite());
  65. container.addChild(new Sprite());
  66. assertThat(addedCallCount, equalTo(2));
  67. }
  68. [Test]
  69. public function a_handler_that_doesnt_handle_a_view_SHOULD_be_reconsulted_if_a_new_handler_is_added():void
  70. {
  71. var addedCallCount:int;
  72. const handler:ViewHandlerSupport = new ViewHandlerSupport(
  73. 0x1,
  74. 0x0,
  75. false,
  76. function onAdded(view:DisplayObject, info:IViewClassInfo, response:uint):void
  77. {
  78. addedCallCount++;
  79. });
  80. watcher.addHandler(handler, container);
  81. container.addChild(new Sprite());
  82. container.addChild(new Sprite());
  83. watcher.addHandler(new ViewHandlerSupport(0x1), container);
  84. container.addChild(new Sprite());
  85. container.addChild(new Sprite());
  86. assertThat(addedCallCount, equalTo(2));
  87. }
  88. [Test]
  89. public function a_handler_that_doesnt_handle_a_view_should_NOT_be_consulted_for_that_view_class_again():void
  90. {
  91. var addedCallCount:int;
  92. const handler:ViewHandlerSupport = new ViewHandlerSupport(
  93. 0x1,
  94. 0x0,
  95. false,
  96. function onAdded(view:DisplayObject, info:IViewClassInfo, response:uint):void
  97. {
  98. addedCallCount++;
  99. });
  100. watcher.addHandler(handler, container);
  101. container.addChild(new Sprite());
  102. container.addChild(new Sprite());
  103. container.addChild(new Sprite());
  104. assertThat(addedCallCount, equalTo(1));
  105. }
  106. }
  107. }