containerscroller_test.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. // Copyright 2008 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.ContainerScrollerTest');
  15. goog.setTestOnly('goog.ui.ContainerScrollerTest');
  16. goog.require('goog.dom');
  17. goog.require('goog.testing.MockClock');
  18. goog.require('goog.testing.events');
  19. goog.require('goog.testing.jsunit');
  20. goog.require('goog.ui.Container');
  21. goog.require('goog.ui.ContainerScroller');
  22. var sandbox;
  23. var sandboxHtml;
  24. var container;
  25. var mockClock;
  26. var scroller;
  27. function setUpPage() {
  28. sandbox = goog.dom.getElement('sandbox');
  29. sandboxHtml = sandbox.innerHTML;
  30. }
  31. function setUp() {
  32. container = new goog.ui.Container();
  33. container.decorate(sandbox);
  34. container.getElement().scrollTop = 0;
  35. mockClock = new goog.testing.MockClock(true);
  36. scroller = null;
  37. }
  38. function tearDown() {
  39. container.dispose();
  40. if (scroller) {
  41. scroller.dispose();
  42. }
  43. // Tick one second to clear all the extra registered events.
  44. mockClock.tick(1000);
  45. mockClock.uninstall();
  46. sandbox.innerHTML = sandboxHtml;
  47. }
  48. function testHighlightFirstStaysAtTop() {
  49. scroller = new goog.ui.ContainerScroller(container);
  50. container.getChildAt(0).setHighlighted(true);
  51. assertEquals(0, container.getElement().scrollTop);
  52. }
  53. function testHighlightSecondStaysAtTop() {
  54. scroller = new goog.ui.ContainerScroller(container);
  55. container.getChildAt(1).setHighlighted(true);
  56. assertEquals(0, container.getElement().scrollTop);
  57. }
  58. function testHighlightSecondLastScrollsNearTheBottom() {
  59. scroller = new goog.ui.ContainerScroller(container);
  60. container.getChildAt(8).setHighlighted(true);
  61. assertEquals(
  62. 'Since scrolling is lazy, when highlighting the second' +
  63. ' last, the item should be the last visible one.',
  64. 80, container.getElement().scrollTop);
  65. }
  66. function testHighlightLastScrollsToBottom() {
  67. scroller = new goog.ui.ContainerScroller(container);
  68. container.getChildAt(9).setHighlighted(true);
  69. assertEquals(100, container.getElement().scrollTop);
  70. }
  71. function testScrollRestoreIfStillVisible() {
  72. scroller = new goog.ui.ContainerScroller(container);
  73. container.getChildAt(9).setHighlighted(true);
  74. var scrollTop = container.getElement().scrollTop;
  75. container.setVisible(false);
  76. container.setVisible(true);
  77. assertEquals(
  78. 'Scroll position should be the same after restore, if it ' +
  79. 'still makes highlighted item visible',
  80. scrollTop, container.getElement().scrollTop);
  81. }
  82. function testNoScrollRestoreIfNotVisible() {
  83. scroller = new goog.ui.ContainerScroller(container);
  84. container.getElement().scrollTop = 100;
  85. container.setVisible(false);
  86. container.getChildAt(0).setHighlighted(true);
  87. container.setVisible(true);
  88. assertNotEquals(
  89. 'Scroll position should not be the same after restore, if ' +
  90. 'the scroll position when the menu was hidden no longer ' +
  91. 'makes the highlighted item visible when the container is ' +
  92. 'shown again',
  93. 100, container.getElement().scrollTop);
  94. }
  95. function testCenterOnHighlightedOnFirstOpen() {
  96. container.setVisible(false);
  97. scroller = new goog.ui.ContainerScroller(container);
  98. container.getChildAt(4).setHighlighted(true);
  99. container.setVisible(true);
  100. // #2 should be at the top when 4 is centered, meaning a scroll top
  101. // of 40 pixels.
  102. assertEquals(
  103. 'On the very first display of the scroller, the item should be ' +
  104. 'centered, rather than just assured in view.',
  105. 40, container.getElement().scrollTop);
  106. }
  107. function testHighlightsAreIgnoredInResponseToScrolling() {
  108. scroller = new goog.ui.ContainerScroller(container);
  109. container.getChildAt(9).setHighlighted(true);
  110. goog.testing.events.fireMouseOverEvent(
  111. goog.dom.getElement('control-5'), goog.dom.getElement('control-9'));
  112. assertEquals(
  113. 'Mouseovers due to scrolls should be ignored', 9,
  114. container.getHighlightedIndex());
  115. }
  116. function testHighlightsAreNotIgnoredWhenNotScrolling() {
  117. scroller = new goog.ui.ContainerScroller(container);
  118. container.getChildAt(5).setHighlighted(true);
  119. mockClock.tick(1000);
  120. goog.testing.events.fireMouseOutEvent(
  121. goog.dom.getElement('control-5'), goog.dom.getElement('control-6'));
  122. goog.testing.events.fireMouseOverEvent(
  123. goog.dom.getElement('control-6'), goog.dom.getElement('control-5'));
  124. assertEquals(
  125. 'Mousovers not due to scrolls should not be ignored', 6,
  126. container.getHighlightedIndex());
  127. }
  128. function testFastSynchronousHighlightsNotIgnored() {
  129. scroller = new goog.ui.ContainerScroller(container);
  130. // Whereas subsequent highlights from mouseovers due to a scroll, should
  131. // be ignored, they should not ignored if they are made synchronusly
  132. // from the code and not from a mouseover. Imagine how bad it would be
  133. // if you could only set the highligted index a certain number of
  134. // times in the same execution context.
  135. container.getChildAt(9).setHighlighted(true);
  136. container.getChildAt(1).setHighlighted(true);
  137. assertEquals(
  138. 'Synchronous highlights should NOT be ignored.', 1,
  139. container.getHighlightedIndex());
  140. container.getChildAt(8).setHighlighted(true);
  141. assertEquals(
  142. 'Synchronous highlights should NOT be ignored.', 8,
  143. container.getHighlightedIndex());
  144. }
  145. function testInitialItemIsCentered() {
  146. container.getChildAt(4).setHighlighted(true);
  147. scroller = new goog.ui.ContainerScroller(container);
  148. // #2 should be at the top when 4 is centered, meaning a scroll top
  149. // of 40 pixels.
  150. assertEquals(
  151. 'On the very first attachment of the scroller, the item should be ' +
  152. 'centered, rather than just assured in view.',
  153. 40, container.getElement().scrollTop);
  154. }
  155. function testInitialItemIsCenteredTopItem() {
  156. container.getChildAt(0).setHighlighted(true);
  157. scroller = new goog.ui.ContainerScroller(container);
  158. assertEquals(0, container.getElement().scrollTop);
  159. }
  160. function testHidingMenuItemsDoesntAffectContainerScroller() {
  161. scroller = new goog.ui.ContainerScroller(container);
  162. container.getElement = function() {
  163. fail(
  164. 'getElement() must not be called when a control in the container is ' +
  165. 'being hidden');
  166. };
  167. container.getChildAt(0).setVisible(false);
  168. }