aboutsummaryrefslogtreecommitdiff
path: root/contexts/data/lib/closure-library/closure/goog/ui/hovercard_test.html
blob: 7bc00c6205f07a80720c874e230d7f879720106a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
<html>
<!--
Copyright 2010 The Closure Library Authors. All Rights Reserved.

Use of this source code is governed by the Apache License, Version 2.0.
See the COPYING file for details.
-->

<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
 <title>Closure Unit Tests - goog.ui.HoverCard</title>

  <style type="text/css">
  .goog-tooltip {
    background: infobackground;
    color: infotext;
    border: 1px solid infotext;
    padding: 1px;
    font:menu;
  }
  </style>

  <script type="text/javascript" src="../base.js"></script>
  <script type="text/javascript">
    goog.require('goog.ui.HoverCard');
    goog.require('goog.dom');
    goog.require('goog.testing.MockClock');
    goog.require('goog.testing.events');
    goog.require('goog.testing.jsunit');
  </script>

  </head>
  <body>
  <p id="notpopup">Content</p>
  <span id="john" email="john@gmail.com">Span for John that can trigger a
    hovercard.</span><br>
  <span id="jane">Span for Jane that doesn't trigger a hovercard (no email
    attribute)</span><br>
  <span id="james" email="james@gmail.com">Span for James that can trigger a
    hovercard<span id="child">Child of james</span></span><br>
  <div id="bill" email="bill@gmail.com">Doesn't trigger for Bill because
    it's a div</div>
  <script type="text/javascript">

    var timer = new goog.testing.MockClock();
    var card;

    // Variables for mocks
    var triggeredElement;
    var cancelledElement;
    var showDelay;
    var shownCard;
    var hideDelay;

    // spans
    var john = goog.dom.getElement('john');
    var jane = goog.dom.getElement('jane');
    var james = goog.dom.getElement('james');
    var bill = goog.dom.getElement('bill');
    var child = goog.dom.getElement('child');

    // Inactive
    var elsewhere;
    var offAnchor;

    function setUp() {
      timer.install();
      triggeredElement = null;
      cancelledElement = null;
      showDelay = null;
      shownCard = null;
      hideDelay = null;
      elsewhere = goog.dom.getElement('notpopup');
      offAnchor = new goog.math.Coordinate(1, 1);
    }

    function initCard(opt_isAnchor, opt_checkChildren, opt_maxSearchSteps) {
      var isAnchor = opt_isAnchor || {SPAN: 'email'};
      card = new goog.ui.HoverCard(isAnchor, opt_checkChildren);
      card.setText('Test hovercard');

      if (opt_maxSearchSteps != null) {
        card.setMaxSearchSteps(opt_maxSearchSteps);
      }

      goog.events.listen(card, goog.ui.HoverCard.EventType.TRIGGER, onTrigger);
      goog.events.listen(card, goog.ui.HoverCard.EventType.CANCEL_TRIGGER,
                         onCancel);
      goog.events.listen(card, goog.ui.HoverCard.EventType.BEFORE_SHOW,
                         onBeforeShow);

      // This gets around the problem where AdvancedToolTip thinks it's
      // receiving a ghost event because cursor position hasn't moved off of
      // (0, 0).
      card.cursorPosition = new goog.math.Coordinate(1, 1);
    }

    // Event handlers
    function onTrigger(event) {
      triggeredElement = event.anchor;
      if (showDelay) {
        card.setShowDelayMs(showDelay);
      }
      return true;
    }

    function onCancel(event) {
      cancelledElement = event.anchor;
    }

    function onBeforeShow() {
      shownCard = card.getAnchorElement();
      if (hideDelay) {
        card.setHideDelayMs(hideDelay);
      }
      return true;
    }

    function tearDown() {
      card.dispose();
      timer.uninstall();
    }

    /**
     * Verify that hovercard displays and goes away under normal circumstances.
     */
    function testTrigger() {
      initCard();

      // Mouse over correct element fires trigger
      showDelay = 500;
      goog.testing.events.fireMouseOverEvent(john, elsewhere);
      assertEquals('Hovercard should have triggered', john,
                   triggeredElement);

      // Show card after delay
      timer.tick(showDelay - 1);
      assertNull('Card should not have shown', shownCard);
      assertFalse(card.isVisible());
      hideDelay = 5000;
      timer.tick(1);
      assertEquals('Card should have shown', john, shownCard);
      assertTrue(card.isVisible());

      // Mouse out leads to hide delay
      goog.testing.events.fireMouseOutEvent(john, elsewhere);
      goog.testing.events.fireMouseMoveEvent(document, offAnchor);
      timer.tick(hideDelay - 1);
      assertTrue('Card should still be visible', card.isVisible());
      timer.tick(10);
      assertFalse('Card should be hidden', card.isVisible());
    }

    /**
     * Verify that CANCEL_TRIGGER event occurs when mouse goes out of
     * triggering element before hovercard is shown.
     */
    function testOnCancel() {
      initCard();

      showDelay = 500;
      goog.testing.events.fireMouseOverEvent(john, elsewhere);
      timer.tick(showDelay - 1);
      goog.testing.events.fireMouseOutEvent(john, elsewhere);
      goog.testing.events.fireMouseMoveEvent(document, offAnchor);
      timer.tick(10);
      assertFalse('Card should be hidden', card.isVisible());
      assertEquals('Should have cancelled trigger', john, cancelledElement);
    }

    /**
     * Verify that mousing over non-triggering elements don't interfere.
     */
    function testMouseOverNonTrigger() {
      initCard();

      // Mouse over correct element fires trigger
      showDelay = 500;
      goog.testing.events.fireMouseOverEvent(john, elsewhere);
      timer.tick(showDelay);

      // Mouse over and out other element does nothing
      triggeredElement = null;
      goog.testing.events.fireMouseOverEvent(jane, elsewhere);
      timer.tick(showDelay + 1);
      assertNull(triggeredElement);
    }

    /**
     * Verify that a mouse over event with no target will not break
     * hover card.
     */
    function testMouseOverNoTarget() {
      initCard();
      card.handleTriggerMouseOver_(new goog.testing.events.Event());
    }

    /**
     * Verify that mousing over a second trigger before the first one shows
     * will correctly cancel the first and show the second.
     */
    function testMultipleTriggers() {
      initCard();

      // Test second trigger when first one still pending
      showDelay = 500;
      hideDelay = 1000;
      goog.testing.events.fireMouseOverEvent(john, elsewhere);
      timer.tick(250);
      goog.testing.events.fireMouseOutEvent(john, james);
      goog.testing.events.fireMouseOverEvent(james, john);
      // First trigger should cancel because it isn't showing yet
      assertEquals('Should cancel first trigger', john, cancelledElement);
      timer.tick(300);
      assertFalse(card.isVisible());
      timer.tick(250);
      assertEquals('Should show second card', james, shownCard);
      assertTrue(card.isVisible());

      goog.testing.events.fireMouseOutEvent(james, john);
      goog.testing.events.fireMouseOverEvent(john, james);
      assertEquals('Should still show second card', james,
                   card.getAnchorElement());
      assertTrue(card.isVisible());

      shownCard = null;
      timer.tick(501);
      assertEquals('Should show first card again', john, shownCard);
      assertTrue(card.isVisible());

      // Test that cancelling while another is showing gives correct cancel
      // information
      cancelledElement = null;
      goog.testing.events.fireMouseOutEvent(john, james);
      goog.testing.events.fireMouseOverEvent(james, john);
      goog.testing.events.fireMouseOutEvent(james, elsewhere);
      assertEquals('Should cancel second card', james, cancelledElement);
    }

    /**
     * Verify manual triggering.
     */
    function testManualTrigger() {
      initCard();

      // Doesn't normally trigger for div tag
      showDelay = 500;
      goog.testing.events.fireMouseOverEvent(bill, elsewhere);
      timer.tick(showDelay);
      assertFalse(card.isVisible());

      // Manually trigger element
      card.triggerForElement(bill);
      hideDelay = 600;
      timer.tick(showDelay);
      assertTrue(card.isVisible());
      goog.testing.events.fireMouseOutEvent(bill, elsewhere);
      goog.testing.events.fireMouseMoveEvent(document, offAnchor);
      timer.tick(hideDelay);
      assertFalse(card.isVisible());
    }

    /**
     * Verify creating with isAnchor function.
     */
    function testIsAnchor() {
      // Initialize card so only bill triggers it.
      initCard(function(element) {
        return element == bill;
      });

      showDelay = 500;
      goog.testing.events.fireMouseOverEvent(bill, elsewhere);
      timer.tick(showDelay);
      assertTrue('Should trigger card', card.isVisible());

      hideDelay = 300;
      goog.testing.events.fireMouseOutEvent(bill, elsewhere);
      goog.testing.events.fireMouseMoveEvent(document, offAnchor);
      timer.tick(hideDelay);
      assertFalse(card.isVisible());

      goog.testing.events.fireMouseOverEvent(john, elsewhere);
      timer.tick(showDelay);
      assertFalse('Should not trigger card', card.isVisible());
    }

    /**
     * Verify mouse over child of anchor triggers hovercard.
     */
    function testAnchorWithChildren() {
      initCard();

      showDelay = 500;
      goog.testing.events.fireMouseOverEvent(james, elsewhere);
      timer.tick(250);

      // Moving from an anchor to a child of that anchor shouldn't cancel
      // or retrigger.
      var childBounds = goog.style.getBounds(child);
      var inChild = new goog.math.Coordinate(childBounds.left + 1,
                                             childBounds.top + 1);
      goog.testing.events.fireMouseOutEvent(james, child);
      goog.testing.events.fireMouseMoveEvent(child, inChild);
      assertNull("Shouldn't cancel trigger", cancelledElement);
      triggeredElement = null;
      goog.testing.events.fireMouseOverEvent(child, james);
      assertNull("Shouldn't retrigger card", triggeredElement);
      timer.tick(250);
      assertTrue('Card should show with original delay', card.isVisible());

      hideDelay = 300;
      goog.testing.events.fireMouseOutEvent(child, elsewhere);
      goog.testing.events.fireMouseMoveEvent(child, offAnchor);
      timer.tick(hideDelay);
      assertFalse(card.isVisible());

      goog.testing.events.fireMouseOverEvent(child, elsewhere);
      timer.tick(showDelay);
      assertTrue('Mouse over child should trigger card', card.isVisible());
    }

    function testNoTriggerWithMaxSearchSteps() {
      initCard(undefined, true, 0);

      showDelay = 500;
      goog.testing.events.fireMouseOverEvent(child, elsewhere);
      timer.tick(showDelay);
      assertFalse('Should not trigger card', card.isVisible());
    }

    function testTriggerWithMaxSearchSteps() {
      initCard(undefined, true, 2);

      showDelay = 500;
      goog.testing.events.fireMouseOverEvent(child, elsewhere);
      timer.tick(showDelay);
      assertTrue('Should trigger card', card.isVisible());
    }

    function testPositionAfterSecondTriggerWithMaxSearchSteps() {
      initCard(undefined, true, 2);

      showDelay = 500;
      goog.testing.events.fireMouseOverEvent(john, elsewhere);
      timer.tick(showDelay);
      assertTrue('Should trigger card', card.isVisible());
      assertEquals('Card cursor x coordinate should be 1',
          card.position_.coordinate.x, 1);
      card.cursorPosition = new goog.math.Coordinate(2, 2);
      goog.testing.events.fireMouseOverEvent(child, elsewhere);
      timer.tick(showDelay);
      assertTrue('Should trigger card', card.isVisible());
      assertEquals('Card cursor x coordinate should be 2',
          card.position_.coordinate.x, 2);
    }

  </script>
  </body>
</html>