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