| 1 |
// Copyright 2010 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.HoverCardTest'); |
| 16 |
goog.setTestOnly('goog.ui.HoverCardTest'); |
| 17 |
|
| 18 |
goog.require('goog.dom'); |
| 19 |
goog.require('goog.events'); |
| 20 |
goog.require('goog.math.Coordinate'); |
| 21 |
goog.require('goog.style'); |
| 22 |
goog.require('goog.testing.MockClock'); |
| 23 |
goog.require('goog.testing.events'); |
| 24 |
goog.require('goog.testing.events.Event'); |
| 25 |
goog.require('goog.testing.jsunit'); |
| 26 |
goog.require('goog.ui.HoverCard'); |
| 27 |
|
| 28 |
var timer = new goog.testing.MockClock(); |
| 29 |
var card; |
| 30 |
|
| 31 |
// Variables for mocks |
| 32 |
var triggeredElement; |
| 33 |
var cancelledElement; |
| 34 |
var showDelay; |
| 35 |
var shownCard; |
| 36 |
var hideDelay; |
| 37 |
|
| 38 |
// spans |
| 39 |
var john; |
| 40 |
var jane; |
| 41 |
var james; |
| 42 |
var bill; |
| 43 |
var child; |
| 44 |
|
| 45 |
// Inactive |
| 46 |
var elsewhere; |
| 47 |
var offAnchor; |
| 48 |
|
| 49 |
function setUpPage() { |
| 50 |
john = goog.dom.getElement('john'); |
| 51 |
jane = goog.dom.getElement('jane'); |
| 52 |
james = goog.dom.getElement('james'); |
| 53 |
bill = goog.dom.getElement('bill'); |
| 54 |
child = goog.dom.getElement('child'); |
| 55 |
} |
| 56 |
|
| 57 |
function setUp() { |
| 58 |
timer.install(); |
| 59 |
triggeredElement = null; |
| 60 |
cancelledElement = null; |
| 61 |
showDelay = null; |
| 62 |
shownCard = null; |
| 63 |
hideDelay = null; |
| 64 |
elsewhere = goog.dom.getElement('notpopup'); |
| 65 |
offAnchor = new goog.math.Coordinate(1, 1); |
| 66 |
} |
| 67 |
|
| 68 |
function initCard(opt_isAnchor, opt_checkChildren, opt_maxSearchSteps) { |
| 69 |
var isAnchor = opt_isAnchor || {SPAN: 'email'}; |
| 70 |
card = new goog.ui.HoverCard(isAnchor, opt_checkChildren); |
| 71 |
card.setText('Test hovercard'); |
| 72 |
|
| 73 |
if (opt_maxSearchSteps != null) { |
| 74 |
card.setMaxSearchSteps(opt_maxSearchSteps); |
| 75 |
} |
| 76 |
|
| 77 |
goog.events.listen(card, goog.ui.HoverCard.EventType.TRIGGER, onTrigger); |
| 78 |
goog.events.listen( |
| 79 |
card, goog.ui.HoverCard.EventType.CANCEL_TRIGGER, onCancel); |
| 80 |
goog.events.listen( |
| 81 |
card, goog.ui.HoverCard.EventType.BEFORE_SHOW, onBeforeShow); |
| 82 |
|
| 83 |
// This gets around the problem where AdvancedToolTip thinks it's |
| 84 |
// receiving a ghost event because cursor position hasn't moved off of |
| 85 |
// (0, 0). |
| 86 |
card.cursorPosition = new goog.math.Coordinate(1, 1); |
| 87 |
} |
| 88 |
|
| 89 |
// Event handlers |
| 90 |
function onTrigger(event) { |
| 91 |
triggeredElement = event.anchor; |
| 92 |
if (showDelay) { |
| 93 |
card.setShowDelayMs(showDelay); |
| 94 |
} |
| 95 |
return true; |
| 96 |
} |
| 97 |
|
| 98 |
function onCancel(event) { |
| 99 |
cancelledElement = event.anchor; |
| 100 |
} |
| 101 |
|
| 102 |
function onBeforeShow() { |
| 103 |
shownCard = card.getAnchorElement(); |
| 104 |
if (hideDelay) { |
| 105 |
card.setHideDelayMs(hideDelay); |
| 106 |
} |
| 107 |
return true; |
| 108 |
} |
| 109 |
|
| 110 |
function tearDown() { |
| 111 |
card.dispose(); |
| 112 |
timer.uninstall(); |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
/** |
| 117 |
* Verify that hovercard displays and goes away under normal circumstances. |
| 118 |
*/ |
| 119 |
function testTrigger() { |
| 120 |
initCard(); |
| 121 |
|
| 122 |
// Mouse over correct element fires trigger |
| 123 |
showDelay = 500; |
| 124 |
goog.testing.events.fireMouseOverEvent(john, elsewhere); |
| 125 |
assertEquals('Hovercard should have triggered', john, triggeredElement); |
| 126 |
|
| 127 |
// Show card after delay |
| 128 |
timer.tick(showDelay - 1); |
| 129 |
assertNull('Card should not have shown', shownCard); |
| 130 |
assertFalse(card.isVisible()); |
| 131 |
hideDelay = 5000; |
| 132 |
timer.tick(1); |
| 133 |
assertEquals('Card should have shown', john, shownCard); |
| 134 |
assertTrue(card.isVisible()); |
| 135 |
|
| 136 |
// Mouse out leads to hide delay |
| 137 |
goog.testing.events.fireMouseOutEvent(john, elsewhere); |
| 138 |
goog.testing.events.fireMouseMoveEvent(document, offAnchor); |
| 139 |
timer.tick(hideDelay - 1); |
| 140 |
assertTrue('Card should still be visible', card.isVisible()); |
| 141 |
timer.tick(10); |
| 142 |
assertFalse('Card should be hidden', card.isVisible()); |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
/** |
| 147 |
* Verify that CANCEL_TRIGGER event occurs when mouse goes out of |
| 148 |
* triggering element before hovercard is shown. |
| 149 |
*/ |
| 150 |
function testOnCancel() { |
| 151 |
initCard(); |
| 152 |
|
| 153 |
showDelay = 500; |
| 154 |
goog.testing.events.fireMouseOverEvent(john, elsewhere); |
| 155 |
timer.tick(showDelay - 1); |
| 156 |
goog.testing.events.fireMouseOutEvent(john, elsewhere); |
| 157 |
goog.testing.events.fireMouseMoveEvent(document, offAnchor); |
| 158 |
timer.tick(10); |
| 159 |
assertFalse('Card should be hidden', card.isVisible()); |
| 160 |
assertEquals('Should have cancelled trigger', john, cancelledElement); |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
/** |
| 165 |
* Verify that mousing over non-triggering elements don't interfere. |
| 166 |
*/ |
| 167 |
function testMouseOverNonTrigger() { |
| 168 |
initCard(); |
| 169 |
|
| 170 |
// Mouse over correct element fires trigger |
| 171 |
showDelay = 500; |
| 172 |
goog.testing.events.fireMouseOverEvent(john, elsewhere); |
| 173 |
timer.tick(showDelay); |
| 174 |
|
| 175 |
// Mouse over and out other element does nothing |
| 176 |
triggeredElement = null; |
| 177 |
goog.testing.events.fireMouseOverEvent(jane, elsewhere); |
| 178 |
timer.tick(showDelay + 1); |
| 179 |
assertNull(triggeredElement); |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
/** |
| 184 |
* Verify that a mouse over event with no target will not break |
| 185 |
* hover card. |
| 186 |
*/ |
| 187 |
function testMouseOverNoTarget() { |
| 188 |
initCard(); |
| 189 |
card.handleTriggerMouseOver_(new goog.testing.events.Event()); |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
/** |
| 194 |
* Verify that mousing over a second trigger before the first one shows |
| 195 |
* will correctly cancel the first and show the second. |
| 196 |
*/ |
| 197 |
function testMultipleTriggers() { |
| 198 |
initCard(); |
| 199 |
|
| 200 |
// Test second trigger when first one still pending |
| 201 |
showDelay = 500; |
| 202 |
hideDelay = 1000; |
| 203 |
goog.testing.events.fireMouseOverEvent(john, elsewhere); |
| 204 |
timer.tick(250); |
| 205 |
goog.testing.events.fireMouseOutEvent(john, james); |
| 206 |
goog.testing.events.fireMouseOverEvent(james, john); |
| 207 |
// First trigger should cancel because it isn't showing yet |
| 208 |
assertEquals('Should cancel first trigger', john, cancelledElement); |
| 209 |
timer.tick(300); |
| 210 |
assertFalse(card.isVisible()); |
| 211 |
timer.tick(250); |
| 212 |
assertEquals('Should show second card', james, shownCard); |
| 213 |
assertTrue(card.isVisible()); |
| 214 |
|
| 215 |
goog.testing.events.fireMouseOutEvent(james, john); |
| 216 |
goog.testing.events.fireMouseOverEvent(john, james); |
| 217 |
assertEquals('Should still show second card', james, card.getAnchorElement()); |
| 218 |
assertTrue(card.isVisible()); |
| 219 |
|
| 220 |
shownCard = null; |
| 221 |
timer.tick(501); |
| 222 |
assertEquals('Should show first card again', john, shownCard); |
| 223 |
assertTrue(card.isVisible()); |
| 224 |
|
| 225 |
// Test that cancelling while another is showing gives correct cancel |
| 226 |
// information |
| 227 |
cancelledElement = null; |
| 228 |
goog.testing.events.fireMouseOutEvent(john, james); |
| 229 |
goog.testing.events.fireMouseOverEvent(james, john); |
| 230 |
goog.testing.events.fireMouseOutEvent(james, elsewhere); |
| 231 |
assertEquals('Should cancel second card', james, cancelledElement); |
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
/** |
| 236 |
* Verify manual triggering. |
| 237 |
*/ |
| 238 |
function testManualTrigger() { |
| 239 |
initCard(); |
| 240 |
|
| 241 |
// Doesn't normally trigger for div tag |
| 242 |
showDelay = 500; |
| 243 |
goog.testing.events.fireMouseOverEvent(bill, elsewhere); |
| 244 |
timer.tick(showDelay); |
| 245 |
assertFalse(card.isVisible()); |
| 246 |
|
| 247 |
// Manually trigger element |
| 248 |
card.triggerForElement(bill); |
| 249 |
hideDelay = 600; |
| 250 |
timer.tick(showDelay); |
| 251 |
assertTrue(card.isVisible()); |
| 252 |
goog.testing.events.fireMouseOutEvent(bill, elsewhere); |
| 253 |
goog.testing.events.fireMouseMoveEvent(document, offAnchor); |
| 254 |
timer.tick(hideDelay); |
| 255 |
assertFalse(card.isVisible()); |
| 256 |
} |
| 257 |
|
| 258 |
|
| 259 |
/** |
| 260 |
* Verify creating with isAnchor function. |
| 261 |
*/ |
| 262 |
function testIsAnchor() { |
| 263 |
// Initialize card so only bill triggers it. |
| 264 |
initCard(function(element) { return element == bill; }); |
| 265 |
|
| 266 |
showDelay = 500; |
| 267 |
goog.testing.events.fireMouseOverEvent(bill, elsewhere); |
| 268 |
timer.tick(showDelay); |
| 269 |
assertTrue('Should trigger card', card.isVisible()); |
| 270 |
|
| 271 |
hideDelay = 300; |
| 272 |
goog.testing.events.fireMouseOutEvent(bill, elsewhere); |
| 273 |
goog.testing.events.fireMouseMoveEvent(document, offAnchor); |
| 274 |
timer.tick(hideDelay); |
| 275 |
assertFalse(card.isVisible()); |
| 276 |
|
| 277 |
goog.testing.events.fireMouseOverEvent(john, elsewhere); |
| 278 |
timer.tick(showDelay); |
| 279 |
assertFalse('Should not trigger card', card.isVisible()); |
| 280 |
} |
| 281 |
|
| 282 |
|
| 283 |
/** |
| 284 |
* Verify mouse over child of anchor triggers hovercard. |
| 285 |
*/ |
| 286 |
function testAnchorWithChildren() { |
| 287 |
initCard(); |
| 288 |
|
| 289 |
showDelay = 500; |
| 290 |
goog.testing.events.fireMouseOverEvent(james, elsewhere); |
| 291 |
timer.tick(250); |
| 292 |
|
| 293 |
// Moving from an anchor to a child of that anchor shouldn't cancel |
| 294 |
// or retrigger. |
| 295 |
var childBounds = goog.style.getBounds(child); |
| 296 |
var inChild = |
| 297 |
new goog.math.Coordinate(childBounds.left + 1, childBounds.top + 1); |
| 298 |
goog.testing.events.fireMouseOutEvent(james, child); |
| 299 |
goog.testing.events.fireMouseMoveEvent(child, inChild); |
| 300 |
assertNull("Shouldn't cancel trigger", cancelledElement); |
| 301 |
triggeredElement = null; |
| 302 |
goog.testing.events.fireMouseOverEvent(child, james); |
| 303 |
assertNull("Shouldn't retrigger card", triggeredElement); |
| 304 |
timer.tick(250); |
| 305 |
assertTrue('Card should show with original delay', card.isVisible()); |
| 306 |
|
| 307 |
hideDelay = 300; |
| 308 |
goog.testing.events.fireMouseOutEvent(child, elsewhere); |
| 309 |
goog.testing.events.fireMouseMoveEvent(child, offAnchor); |
| 310 |
timer.tick(hideDelay); |
| 311 |
assertFalse(card.isVisible()); |
| 312 |
|
| 313 |
goog.testing.events.fireMouseOverEvent(child, elsewhere); |
| 314 |
timer.tick(showDelay); |
| 315 |
assertTrue('Mouse over child should trigger card', card.isVisible()); |
| 316 |
} |
| 317 |
|
| 318 |
function testNoTriggerWithMaxSearchSteps() { |
| 319 |
initCard(undefined, true, 0); |
| 320 |
|
| 321 |
showDelay = 500; |
| 322 |
goog.testing.events.fireMouseOverEvent(child, elsewhere); |
| 323 |
timer.tick(showDelay); |
| 324 |
assertFalse('Should not trigger card', card.isVisible()); |
| 325 |
} |
| 326 |
|
| 327 |
function testTriggerWithMaxSearchSteps() { |
| 328 |
initCard(undefined, true, 2); |
| 329 |
|
| 330 |
showDelay = 500; |
| 331 |
goog.testing.events.fireMouseOverEvent(child, elsewhere); |
| 332 |
timer.tick(showDelay); |
| 333 |
assertTrue('Should trigger card', card.isVisible()); |
| 334 |
} |
| 335 |
|
| 336 |
function testPositionAfterSecondTriggerWithMaxSearchSteps() { |
| 337 |
initCard(undefined, true, 2); |
| 338 |
|
| 339 |
showDelay = 500; |
| 340 |
goog.testing.events.fireMouseOverEvent(john, elsewhere); |
| 341 |
timer.tick(showDelay); |
| 342 |
assertTrue('Should trigger card', card.isVisible()); |
| 343 |
assertEquals( |
| 344 |
'Card cursor x coordinate should be 1', card.position_.coordinate.x, 1); |
| 345 |
card.cursorPosition = new goog.math.Coordinate(2, 2); |
| 346 |
goog.testing.events.fireMouseOverEvent(child, elsewhere); |
| 347 |
timer.tick(showDelay); |
| 348 |
assertTrue('Should trigger card', card.isVisible()); |
| 349 |
assertEquals( |
| 350 |
'Card cursor x coordinate should be 2', card.position_.coordinate.x, 2); |
| 351 |
} |