scrollfloater_test.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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.ScrollFloaterTest');
  15. goog.setTestOnly('goog.ui.ScrollFloaterTest');
  16. goog.require('goog.dom');
  17. goog.require('goog.events');
  18. goog.require('goog.style');
  19. goog.require('goog.testing.PropertyReplacer');
  20. goog.require('goog.testing.jsunit');
  21. goog.require('goog.ui.ScrollFloater');
  22. function testScrollFloater() {
  23. var scrollFloater = new goog.ui.ScrollFloater();
  24. var floater = goog.dom.getElement('floater');
  25. scrollFloater.decorate(floater);
  26. assertTrue('Default state is enabled', scrollFloater.isScrollingEnabled());
  27. assertFalse(
  28. 'On unscrolled page should not be floating', scrollFloater.isFloating());
  29. scrollFloater.setScrollingEnabled(false);
  30. assertFalse('We can disable the floater', scrollFloater.isScrollingEnabled());
  31. scrollFloater.dispose();
  32. }
  33. function testScrollFloaterEvents() {
  34. var scrollFloater = new goog.ui.ScrollFloater();
  35. var floater = goog.dom.getElement('floater');
  36. scrollFloater.setContainerElement(goog.dom.getElement('container'));
  37. scrollFloater.decorate(floater);
  38. var floatWasCalled = false;
  39. var callRecorder = function() { floatWasCalled = true; };
  40. goog.events.listen(
  41. scrollFloater, goog.ui.ScrollFloater.EventType.FLOAT, callRecorder);
  42. scrollFloater.float_(goog.ui.ScrollFloater.FloatMode_.TOP);
  43. assertTrue('FLOAT event was called', floatWasCalled);
  44. assertTrue('Should be floating', scrollFloater.isFloating());
  45. assertFalse('Should not be pinned', scrollFloater.isPinned());
  46. var dockWasCalled = false;
  47. callRecorder = function() { dockWasCalled = true; };
  48. goog.events.listen(
  49. scrollFloater, goog.ui.ScrollFloater.EventType.DOCK, callRecorder);
  50. scrollFloater.dock_();
  51. assertTrue('DOCK event was called', dockWasCalled);
  52. assertFalse('Should not be floating', scrollFloater.isFloating());
  53. assertFalse('Should not be pinned', scrollFloater.isPinned());
  54. var pinWasCalled = false;
  55. callRecorder = function() { pinWasCalled = true; };
  56. goog.events.listen(
  57. scrollFloater, goog.ui.ScrollFloater.EventType.PIN, callRecorder);
  58. scrollFloater.pin_();
  59. assertTrue('PIN event was called', pinWasCalled);
  60. assertFalse('Should not be floating', scrollFloater.isFloating());
  61. assertTrue('Should be pinned', scrollFloater.isPinned());
  62. scrollFloater.dispose();
  63. }
  64. function testScrollFloaterEventCancellation() {
  65. var scrollFloater = new goog.ui.ScrollFloater();
  66. var floater = goog.dom.getElement('floater');
  67. scrollFloater.decorate(floater);
  68. // Event handler that returns false to cancel the event.
  69. var eventCanceller = function() { return false; };
  70. // Have eventCanceller handle the FLOAT event and verify cancellation.
  71. goog.events.listen(
  72. scrollFloater, goog.ui.ScrollFloater.EventType.FLOAT, eventCanceller);
  73. scrollFloater.float_(goog.ui.ScrollFloater.FloatMode_.TOP);
  74. assertFalse('Should not be floating', scrollFloater.isFloating());
  75. // Have eventCanceller handle the PIN event and verify cancellation.
  76. goog.events.listen(
  77. scrollFloater, goog.ui.ScrollFloater.EventType.PIN, eventCanceller);
  78. scrollFloater.dock_();
  79. assertFalse('Should not be pinned', scrollFloater.isPinned());
  80. // Detach eventCanceller and enable floating.
  81. goog.events.unlisten(
  82. scrollFloater, goog.ui.ScrollFloater.EventType.FLOAT, eventCanceller);
  83. scrollFloater.float_(goog.ui.ScrollFloater.FloatMode_.TOP);
  84. // Have eventCanceller handle the DOCK event and verify cancellation.
  85. goog.events.listen(
  86. scrollFloater, goog.ui.ScrollFloater.EventType.DOCK, eventCanceller);
  87. scrollFloater.dock_();
  88. assertTrue('Should still be floating', scrollFloater.isFloating());
  89. scrollFloater.dispose();
  90. }
  91. function testScrollFloaterUpdateStyleOnFloatEvent() {
  92. var scrollFloater = new goog.ui.ScrollFloater();
  93. var floater = goog.dom.getElement('floater');
  94. scrollFloater.decorate(floater);
  95. // Event handler that sets the font size of the scrollfloater to 20px.
  96. var updateStyle = function(e) {
  97. goog.style.setStyle(e.target.getElement(), 'font-size', '20px');
  98. };
  99. // Set the current font size to 10px.
  100. goog.style.setStyle(scrollFloater.getElement(), 'font-size', '10px');
  101. goog.events.listen(
  102. scrollFloater, goog.ui.ScrollFloater.EventType.FLOAT, updateStyle);
  103. scrollFloater.float_(goog.ui.ScrollFloater.FloatMode_.BOTTOM);
  104. // Ensure event handler got called and updated the font size.
  105. assertEquals(
  106. 'Font size should be 20px', '20px',
  107. goog.style.getStyle(scrollFloater.getElement(), 'font-size'));
  108. assertEquals(
  109. 'Top should be auto', 'auto',
  110. goog.style.getStyle(scrollFloater.getElement(), 'top'));
  111. assertEquals(
  112. 'Bottom should be 0px', 0,
  113. parseInt(goog.style.getStyle(scrollFloater.getElement(), 'bottom')));
  114. scrollFloater.dispose();
  115. }
  116. function testScrollFloaterHandlesHorizontalScrolling() {
  117. var scrollFloater = new goog.ui.ScrollFloater();
  118. var floater = goog.dom.getElement('floater');
  119. scrollFloater.decorate(floater);
  120. scrollFloater.float_(goog.ui.ScrollFloater.FloatMode_.TOP);
  121. // For some reason the default position of the tested SF is 16px left.
  122. assertEquals(
  123. 'Element should be left aligned', '16px',
  124. goog.style.getStyle(scrollFloater.getElement(), 'left'));
  125. var propReplacer = new goog.testing.PropertyReplacer();
  126. propReplacer.set(
  127. goog.dom, 'getDocumentScroll', function() { return {'x': 20}; });
  128. scrollFloater.float_(goog.ui.ScrollFloater.FloatMode_.TOP);
  129. assertEquals(
  130. 'Element should be scrolled to the left', '-4px',
  131. goog.style.getStyle(scrollFloater.getElement(), 'left'));
  132. propReplacer.reset();
  133. scrollFloater.dispose();
  134. }