animatedzippy_test.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. // Copyright 2011 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.ui.AnimatedZippyTest');
  15. goog.setTestOnly('goog.ui.AnimatedZippyTest');
  16. goog.require('goog.a11y.aria');
  17. goog.require('goog.a11y.aria.State');
  18. goog.require('goog.asserts');
  19. goog.require('goog.dom');
  20. goog.require('goog.events');
  21. goog.require('goog.functions');
  22. goog.require('goog.fx.Animation');
  23. goog.require('goog.fx.Transition');
  24. goog.require('goog.testing.PropertyReplacer');
  25. goog.require('goog.testing.asserts');
  26. goog.require('goog.testing.jsunit');
  27. goog.require('goog.ui.AnimatedZippy');
  28. goog.require('goog.ui.Zippy');
  29. var animatedZippy;
  30. var animatedZippyHeaderEl;
  31. var propertyReplacer;
  32. function setUp() {
  33. animatedZippyHeaderEl = goog.dom.getElement('t1');
  34. goog.asserts.assert(animatedZippyHeaderEl);
  35. animatedZippy = new goog.ui.AnimatedZippy(
  36. animatedZippyHeaderEl, goog.dom.getElement('c1'));
  37. propertyReplacer = new goog.testing.PropertyReplacer();
  38. }
  39. function tearDown() {
  40. propertyReplacer.reset();
  41. animatedZippy.dispose();
  42. }
  43. function testConstructor() {
  44. assertNotNull('must not be null', animatedZippy);
  45. }
  46. function testExpandCollapse() {
  47. var animationsPlayed = 0;
  48. var toggleEventsFired = 0;
  49. propertyReplacer.replace(goog.fx.Animation.prototype, 'play', function() {
  50. animationsPlayed++;
  51. this.dispatchAnimationEvent(goog.fx.Transition.EventType.END);
  52. });
  53. propertyReplacer.replace(
  54. goog.ui.AnimatedZippy.prototype, 'onAnimate_', goog.functions.NULL);
  55. goog.events.listenOnce(
  56. animatedZippy, goog.ui.Zippy.Events.TOGGLE, function(e) {
  57. toggleEventsFired++;
  58. assertTrue('TOGGLE event must be for expansion', e.expanded);
  59. assertEquals('expanded must be true', true, animatedZippy.isExpanded());
  60. assertEquals(
  61. 'aria-expanded must be true', 'true',
  62. goog.a11y.aria.getState(
  63. animatedZippyHeaderEl, goog.a11y.aria.State.EXPANDED));
  64. });
  65. animatedZippy.expand();
  66. goog.events.listenOnce(
  67. animatedZippy, goog.ui.Zippy.Events.TOGGLE, function(e) {
  68. toggleEventsFired++;
  69. assertFalse('TOGGLE event must be for collapse', e.expanded);
  70. assertEquals(
  71. 'expanded must be false', false, animatedZippy.isExpanded());
  72. assertEquals(
  73. 'aria-expanded must be false', 'false',
  74. goog.a11y.aria.getState(
  75. animatedZippyHeaderEl, goog.a11y.aria.State.EXPANDED));
  76. });
  77. animatedZippy.collapse();
  78. assertEquals('animations must play', 2, animationsPlayed);
  79. assertEquals('TOGGLE events must fire', 2, toggleEventsFired);
  80. }
  81. /** Tests the TOGGLE_ANIMATION_BEGIN event. */
  82. function testToggleBegin() {
  83. var animationsPlayed = 0;
  84. var toggleEventsFired = 0;
  85. propertyReplacer.replace(goog.fx.Animation.prototype, 'play', function() {
  86. animationsPlayed++;
  87. this.dispatchAnimationEvent(goog.fx.Transition.EventType.BEGIN);
  88. this.dispatchAnimationEvent(goog.fx.Transition.EventType.END);
  89. });
  90. propertyReplacer.replace(
  91. goog.ui.AnimatedZippy.prototype, 'onAnimate_', goog.functions.NULL);
  92. goog.events.listenOnce(
  93. animatedZippy, goog.ui.AnimatedZippy.Events.TOGGLE_ANIMATION_BEGIN,
  94. function(e) {
  95. toggleEventsFired++;
  96. assertTrue(
  97. 'TOGGLE_ANIMATION_BEGIN event must be for expansion', e.expanded);
  98. assertEquals(
  99. 'expanded must be false', false, animatedZippy.isExpanded());
  100. assertEquals(
  101. 'aria-expanded must be true', 'true',
  102. goog.a11y.aria.getState(
  103. animatedZippyHeaderEl, goog.a11y.aria.State.EXPANDED));
  104. });
  105. animatedZippy.expand();
  106. goog.events.listenOnce(
  107. animatedZippy, goog.ui.AnimatedZippy.Events.TOGGLE_ANIMATION_BEGIN,
  108. function(e) {
  109. toggleEventsFired++;
  110. assertFalse(
  111. 'TOGGLE_ANIMATION_BEGIN event must be for collapse', e.expanded);
  112. assertEquals('expanded must be true', true, animatedZippy.isExpanded());
  113. assertEquals(
  114. 'aria-expanded must be false', 'false',
  115. goog.a11y.aria.getState(
  116. animatedZippyHeaderEl, goog.a11y.aria.State.EXPANDED));
  117. });
  118. animatedZippy.collapse();
  119. assertEquals('animations must play', 2, animationsPlayed);
  120. assertEquals('TOGGLE_ANIMATION_BEGIN events must fire', 2, toggleEventsFired);
  121. }
  122. /** Tests the TOGGLE_ANIMATION_END event. */
  123. function testToggleEnd() {
  124. var animationsPlayed = 0;
  125. var toggleEventsFired = 0;
  126. propertyReplacer.replace(goog.fx.Animation.prototype, 'play', function() {
  127. animationsPlayed++;
  128. this.dispatchAnimationEvent(goog.fx.Transition.EventType.END);
  129. });
  130. propertyReplacer.replace(
  131. goog.ui.AnimatedZippy.prototype, 'onAnimate_', goog.functions.NULL);
  132. goog.events.listenOnce(
  133. animatedZippy, goog.ui.AnimatedZippy.Events.TOGGLE_ANIMATION_END,
  134. function(e) {
  135. toggleEventsFired++;
  136. assertTrue(
  137. 'TOGGLE_ANIMATION_END event must be for expansion', e.expanded);
  138. assertEquals('expanded must be true', true, animatedZippy.isExpanded());
  139. assertEquals(
  140. 'aria-expanded must be true', 'true',
  141. goog.a11y.aria.getState(
  142. animatedZippyHeaderEl, goog.a11y.aria.State.EXPANDED));
  143. });
  144. animatedZippy.expand();
  145. goog.events.listenOnce(
  146. animatedZippy, goog.ui.AnimatedZippy.Events.TOGGLE_ANIMATION_END,
  147. function(e) {
  148. toggleEventsFired++;
  149. assertFalse(
  150. 'TOGGLE_ANIMATION_END event must be for collapse', e.expanded);
  151. assertEquals(
  152. 'expanded must be false', false, animatedZippy.isExpanded());
  153. assertEquals(
  154. 'aria-expanded must be false', 'false',
  155. goog.a11y.aria.getState(
  156. animatedZippyHeaderEl, goog.a11y.aria.State.EXPANDED));
  157. });
  158. animatedZippy.collapse();
  159. assertEquals('animations must play', 2, animationsPlayed);
  160. assertEquals('TOGGLE_ANIMATION_END events must fire', 2, toggleEventsFired);
  161. }