aboutsummaryrefslogtreecommitdiff
path: root/contexts/data/lib/closure-library/closure/goog/ui/menuitem_test.html
blob: fb8103a3d59ba515be3e5415f2f808f36c358ed0 (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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
<!DOCTYPE html>
<html>
<!--
Copyright 2008 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.
-->
<!--
Author: attila@google.com (Attila Bodis)
-->
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title>Closure Unit Tests - goog.ui.MenuItem</title>
  <script src="../base.js"></script>
  <script>
    goog.require('goog.array');
    goog.require('goog.dom');
    goog.require('goog.dom.NodeType');
    goog.require('goog.dom.classes');
    goog.require('goog.events.KeyCodes');
    goog.require('goog.testing.events');
    goog.require('goog.testing.jsunit');
    goog.require('goog.testing.recordFunction');
    goog.require('goog.ui.Component.State');
    goog.require('goog.ui.MenuItem');
    goog.require('goog.ui.MenuItemRenderer');
  </script>
</head>
<body>
  <div id="sandbox"></div>
  <div id="parentComponent"></div>
  <script>
    var sandbox = goog.dom.getElement('sandbox');
    var item;

    function setUp() {
      item = new goog.ui.MenuItem('Item');
    }

    function tearDown() {
      item.dispose();
      goog.dom.removeChildren(sandbox);
    }

    function testMenuItem() {
      assertNotNull('Instance must not be null', item);
      assertEquals('Renderer must default to MenuItemRenderer singleton',
          goog.ui.MenuItemRenderer.getInstance(), item.getRenderer());
      assertEquals('Content must have expected value', 'Item',
          item.getContent());
      assertEquals('Caption must default to the content', item.getContent(),
          item.getCaption());
      assertEquals('Value must default to the caption', item.getCaption(),
          item.getValue());
    }

    function testMenuItemConstructor() {
      var model = 'Hello';
      var fakeDom = {};
      var fakeRenderer = {};

      var menuItem = new goog.ui.MenuItem('Item', model, fakeDom, fakeRenderer);
      assertEquals('Content must have expected value', 'Item',
          menuItem.getContent());
      assertEquals('Caption must default to the content', menuItem.getContent(),
          menuItem.getCaption());
      assertEquals('Model must be set', model, menuItem.getModel());
      assertNotEquals('Value must not equal the caption', menuItem.getCaption(),
          menuItem.getValue());
      assertEquals('Value must equal the model', model, menuItem.getValue());
      assertEquals('DomHelper must be set', fakeDom, menuItem.getDomHelper());
      assertEquals('Renderer must be set', fakeRenderer,
          menuItem.getRenderer());
    }

    function testGetValue() {
      assertUndefined('Model must be undefined by default', item.getModel());
      assertEquals('Without a model, value must default to the caption',
          item.getCaption(), item.getValue());
      item.setModel('Foo');
      assertEquals('With a model, value must default to the model',
          item.getModel(), item.getValue());
    }

    function testSetValue() {
      assertUndefined('Model must be undefined by default', item.getModel());
      assertEquals('Without a model, value must default to the caption',
          item.getCaption(), item.getValue());
      item.setValue(17);
      assertEquals('Value must be set', 17, item.getValue());
      assertEquals('Value and model must be the same', item.getValue(),
          item.getModel());
    }

    function testGetSetContent() {
      assertEquals('Content must have expected value', 'Item',
          item.getContent());
      item.setContent(goog.dom.createDom('div', 'foo', 'Foo'));
      assertEquals('Content must be an element', goog.dom.NodeType.ELEMENT,
          item.getContent().nodeType);
      assertHTMLEquals('Content must be the expected element',
          '<div class="foo">Foo</div>',
          goog.dom.getOuterHtml(item.getContent()));
    }

    function testGetSetCaption() {
      assertEquals('Caption must have expected value', 'Item',
          item.getCaption());
      item.setCaption('Hello, world!');
      assertTrue('Caption must be a string', goog.isString(item.getCaption()));
      assertEquals('Caption must have expected value', 'Hello, world!',
          item.getCaption());
      item.setContent(goog.dom.createDom('div', 'foo', 'Foo'));
      assertTrue('Caption must be a string', goog.isString(item.getCaption()));
      assertEquals('Caption must have expected value', 'Foo',
          item.getCaption());
    }

    function testGetSetContentAfterCreateDom() {
      item.createDom();
      assertEquals('Content must have expected value', 'Item',
          item.getContent());
      item.setContent(goog.dom.createDom('div', 'foo', 'Foo'));
      assertEquals('Content must be an element', goog.dom.NodeType.ELEMENT,
          item.getContent().nodeType);
      assertHTMLEquals('Content must be the expected element',
          '<div class="foo">Foo</div>',
          goog.dom.getOuterHtml(item.getContent()));
    }

    function testGetSetCaptionAfterCreateDom() {
      item.createDom();
      assertEquals('Caption must have expected value', 'Item',
          item.getCaption());
      item.setCaption('Hello, world!');
      assertTrue('Caption must be a string', goog.isString(item.getCaption()));
      assertEquals('Caption must have expected value', 'Hello, world!',
          item.getCaption());
      item.setContent(goog.dom.createDom('div', 'foo', 'Foo'));
      assertTrue('Caption must be a string', goog.isString(item.getCaption()));
      assertEquals('Caption must have expected value', 'Foo',
          item.getCaption());

      var arrayContent = goog.array.clone(goog.dom.htmlToDocumentFragment(
          ' <b> \xa0foo</b><i>  bar</i> ').childNodes);
      item.setContent(arrayContent);
      assertEquals('whitespaces must be normalized in the caption',
          '\xa0foo bar', item.getCaption());
    }

    function testSetSelectable() {
      assertFalse('Item must not be selectable by default',
          item.isSupportedState(goog.ui.Component.State.SELECTED));
      item.setSelectable(true);
      assertTrue('Item must be selectable',
          item.isSupportedState(goog.ui.Component.State.SELECTED));
      item.setSelected(true);
      assertTrue('Item must be selected', item.isSelected());
      assertFalse('Item must not be checked', item.isChecked());
      item.setSelectable(false);
      assertFalse('Item must not no longer be selectable',
          item.isSupportedState(goog.ui.Component.State.SELECTED));
      assertFalse('Item must no longer be selected', item.isSelected());
      assertFalse('Item must not be checked', item.isChecked());
    }

    function testSetCheckable() {
      assertFalse('Item must not be checkable by default',
          item.isSupportedState(goog.ui.Component.State.CHECKED));
      item.setCheckable(true);
      assertTrue('Item must be checkable',
          item.isSupportedState(goog.ui.Component.State.CHECKED));
      item.setChecked(true);
      assertTrue('Item must be checked', item.isChecked());
      assertFalse('Item must not be selected', item.isSelected());
      item.setCheckable(false);
      assertFalse('Item must not no longer be checkable',
          item.isSupportedState(goog.ui.Component.State.CHECKED));
      assertFalse('Item must no longer be checked', item.isChecked());
      assertFalse('Item must not be selected', item.isSelected());
    }

    function testSetSelectableBeforeCreateDom() {
      item.setSelectable(true);
      item.createDom();
      assertTrue('Item must have checkbox structure',
          item.getRenderer().hasCheckBoxStructure(item.getElement()));
      item.setSelectable(false);
      assertFalse('Item must no longer have checkbox structure',
          item.getRenderer().hasCheckBoxStructure(item.getElement()));
    }

    function testSetCheckableBeforeCreateDom() {
      item.setCheckable(true);
      item.createDom();
      assertTrue('Item must have checkbox structure',
          item.getRenderer().hasCheckBoxStructure(item.getElement()));
      item.setCheckable(false);
      assertFalse('Item must no longer have checkbox structure',
          item.getRenderer().hasCheckBoxStructure(item.getElement()));
    }

    function testSetSelectableAfterCreateDom() {
      item.createDom();
      item.setSelectable(true);
      assertTrue('Item must have checkbox structure',
          item.getRenderer().hasCheckBoxStructure(item.getElement()));
      item.setSelectable(false);
      assertFalse('Item must no longer have checkbox structure',
          item.getRenderer().hasCheckBoxStructure(item.getElement()));
    }

    function testSetCheckableAfterCreateDom() {
      item.createDom();
      item.setCheckable(true);
      assertTrue('Item must have checkbox structure',
          item.getRenderer().hasCheckBoxStructure(item.getElement()));
      item.setCheckable(false);
      assertFalse('Item must no longer have checkbox structure',
          item.getRenderer().hasCheckBoxStructure(item.getElement()));
    }

    function testSelectableBehavior() {
      item.setSelectable(true);
      item.render(sandbox);
      assertFalse('Item must not be selected by default', item.isSelected());
      item.performActionInternal();
      assertTrue('Item must be selected', item.isSelected());
      item.performActionInternal();
      assertTrue('Item must still be selected', item.isSelected());
    }

    function testCheckableBehavior() {
      item.setCheckable(true);
      item.render(sandbox);
      assertFalse('Item must not be checked by default', item.isChecked());
      item.performActionInternal();
      assertTrue('Item must be checked', item.isChecked());
      item.performActionInternal();
      assertFalse('Item must no longer be checked', item.isChecked());
    }

    function testGetSetContentForItemWithCheckBox() {
      item.setSelectable(true);
      item.createDom();

      assertTrue('Item must have checkbox structure',
          item.getRenderer().hasCheckBoxStructure(item.getElement()));
      assertEquals('getContent() must not return the checkbox structure',
          'Item', item.getContent());

      item.setContent('Hello');
      assertEquals('getContent() must not return the checkbox structure',
          'Hello', item.getContent());
      assertTrue('Item must still have checkbox structure',
          item.getRenderer().hasCheckBoxStructure(item.getElement()));

      item.setContent(goog.dom.createDom('span', 'foo', 'Foo'));
      assertEquals('getContent() must return element',
          goog.dom.NodeType.ELEMENT, item.getContent().nodeType);
      assertTrue('Item must still have checkbox structure',
          item.getRenderer().hasCheckBoxStructure(item.getElement()));

      item.setContent(null);
      assertNull('getContent() must return null', item.getContent());
      assertTrue('Item must still have checkbox structure',
          item.getRenderer().hasCheckBoxStructure(item.getElement()));
    }

    function testGetSetCaptionForItemWithCheckBox() {
      item.setCheckable(true);
      item.createDom();

      assertTrue('Item must have checkbox structure',
          item.getRenderer().hasCheckBoxStructure(item.getElement()));
      assertEquals('getCaption() must not return the checkbox structure',
          'Item', item.getCaption());

      item.setCaption('Hello');
      assertEquals('getCaption() must not return the checkbox structure',
          'Hello', item.getCaption());
      assertTrue('Item must still have checkbox structure',
          item.getRenderer().hasCheckBoxStructure(item.getElement()));

      item.setContent(goog.dom.createDom('span', 'foo', 'Foo'));
      assertEquals('getCaption() must return text content', 'Foo',
          item.getCaption());
      assertTrue('Item must still have checkbox structure',
          item.getRenderer().hasCheckBoxStructure(item.getElement()));

      item.setCaption('');
      assertEquals('getCaption() must return empty string', '',
          item.getCaption());
      assertTrue('Item must still have checkbox structure',
          item.getRenderer().hasCheckBoxStructure(item.getElement()));
    }

    function testGetSetCaptionForItemWithAccelerators() {
      var contentArr = [];
      contentArr.push(goog.dom.createDom('span',
          goog.getCssName('goog-menuitem-accel'), 'Ctrl+1'));
      contentArr.push(goog.dom.createTextNode('Hello'));
      item.setCaption(contentArr);
      assertEquals('getCaption() must not return the accelerator', 'Hello',
          item.getCaption());

      item.setCaption('');
      assertEquals('getCaption() must return empty string', '',
          item.getCaption());
    }

    function testGetSetCaptionForItemWithMnemonics() {
      var contentArr = [];
      contentArr.push(goog.dom.createDom('span',
          goog.getCssName('goog-menuitem-mnemonic-hint'), 'H'));
      contentArr.push(goog.dom.createTextNode('ello'));
      item.setCaption(contentArr);
      assertEquals('getCaption() must not return hint markup', 'Hello',
          item.getCaption());

      contentArr = [];
      contentArr.push(goog.dom.createTextNode('Hello'));
      contentArr.push(goog.dom.createDom('span',
          goog.getCssName('goog-menuitem-mnemonic-separator'), '(',
          goog.dom.createDom('span',
              goog.getCssName('goog-menuitem-mnemonic-hint'), 'J'), ')'));
      item.setCaption(contentArr);
      assertEquals('getCaption() must not return the paranethetical mnemonic',
          'Hello', item.getCaption());

      item.setCaption('');
      assertEquals('getCaption() must return the empty string', '',
      item.getCaption());
    }

    function testHandleKeyEventInternalWithMnemonic() {
      item.performActionInternal =
          goog.testing.recordFunction(item.performActionInternal);
      item.setMnemonic(goog.events.KeyCodes.F);
      item.handleKeyEventInternal({'keyCode': goog.events.KeyCodes.F});
      assertEquals('performActionInternal must be called', 1,
          item.performActionInternal.getCallCount());
    }

    function testHandleKeyEventInternalWithoutMnemonic() {
      item.performActionInternal = goog.testing.recordFunction(
          item.performActionInternal);
      item.handleKeyEventInternal({'keyCode': goog.events.KeyCodes.F});
      assertEquals('performActionInternal must not be called without a' +
          ' mnemonic', 0, item.performActionInternal.getCallCount());
    }

    function testRender() {
      item.render(sandbox);
      var contentElement = item.getContentElement();
      assertNotNull('Content element must exist', contentElement);
      assertTrue('Content element must have expected class name',
          goog.dom.classes.has(contentElement,
              item.getRenderer().getStructuralCssClass() + '-content'));
      assertHTMLEquals('Content element must have expected structure',
          'Item', contentElement.innerHTML);
    }

    function testRenderSelectableItem() {
      item.setSelectable(true);
      item.render(sandbox);
      assertTrue('Item must have checkbox structure',
          item.getRenderer().hasCheckBoxStructure(item.getElement()));
      assertEquals('getCaption() return expected value', 'Item',
          item.getCaption());
    }

    function testRenderSelectedItem() {
      item.setSelectable(true);
      item.setSelected(true);
      item.render(sandbox);
      assertTrue('Item must have checkbox structure',
          item.getRenderer().hasCheckBoxStructure(item.getElement()));
      assertTrue('Item must have selected style',
          goog.dom.classes.has(item.getElement(),
              item.getRenderer().getClassForState(
                  goog.ui.Component.State.SELECTED)));
    }

    function testRenderCheckableItem() {
      item.setCheckable(true);
      item.render(sandbox);
      assertTrue('Item must have checkbox structure',
          item.getRenderer().hasCheckBoxStructure(item.getElement()));
      assertEquals('getCaption() return expected value', 'Item',
          item.getCaption());
    }

    function testRenderCheckedItem() {
      item.setCheckable(true);
      item.setChecked(true);
      item.render(sandbox);
      assertTrue('Item must have checkbox structure',
          item.getRenderer().hasCheckBoxStructure(item.getElement()));
      assertTrue('Item must have checked style',
          goog.dom.classes.has(item.getElement(),
              item.getRenderer().getClassForState(
                  goog.ui.Component.State.CHECKED)));
    }

    function testDecorate() {
      sandbox.innerHTML = '<div id="foo">Foo</div>';
      var foo = goog.dom.getElement('foo');
      item.decorate(foo);
      assertEquals('Decorated element must be as expected', foo,
          item.getElement());
      assertTrue('Decorated element must have expected class name',
          goog.dom.classes.has(item.getElement(),
              item.getRenderer().getCssClass()));
      assertEquals('Content element must be the decorated element\'s child',
          item.getContentElement(), item.getElement().firstChild);
      assertHTMLEquals('Content must have expected structure', 'Foo',
          item.getContentElement().innerHTML);
    }

    function testDecorateCheckableItem() {
      sandbox.innerHTML = '<div id="foo" class="goog-option">Foo</div>';
      var foo = goog.dom.getElement('foo');
      item.decorate(foo);
      assertEquals('Decorated element must be as expected', foo,
          item.getElement());
      assertTrue('Decorated element must have expected class name',
          goog.dom.classes.has(item.getElement(),
              item.getRenderer().getCssClass()));
      assertEquals('Content element must be the decorated element\'s child',
          item.getContentElement(), item.getElement().firstChild);
      assertTrue('Item must have checkbox structure',
          item.getRenderer().hasCheckBoxStructure(item.getElement()));
      assertFalse('Item must not be checked', item.isChecked());
    }

    function testDecorateCheckedItem() {
      sandbox.innerHTML =
          '<div id="foo" class="goog-option goog-option-selected">Foo</div>';
      var foo = goog.dom.getElement('foo');
      item.decorate(foo);
      assertEquals('Decorated element must be as expected', foo,
          item.getElement());
      assertSameElements('Decorated element must have expected class names',
          ['goog-menuitem', 'goog-option', 'goog-option-selected'],
          goog.dom.classes.get(item.getElement()));
      assertEquals('Content element must be the decorated element\'s child',
          item.getContentElement(), item.getElement().firstChild);
      assertTrue('Item must have checkbox structure',
          item.getRenderer().hasCheckBoxStructure(item.getElement()));
      assertTrue('Item must be checked', item.isChecked());
    }

    function testDecorateTemplate() {
      sandbox.innerHTML = '<div id="foo" class="goog-menuitem">' +
          '<div class="goog-menuitem-content">Foo</div></div>';
      var foo = goog.dom.getElement('foo');
      item.decorate(foo);
      assertEquals('Decorated element must be as expected', foo,
          item.getElement());
      assertTrue('Decorated element must have expected class name',
          goog.dom.classes.has(item.getElement(),
              item.getRenderer().getCssClass()));
      assertEquals('Content element must be the decorated element\'s child',
          item.getContentElement(), item.getElement().firstChild);
      assertHTMLEquals('Content must have expected structure', 'Foo',
          item.getContentElement().innerHTML);
    }

    function testDecorateCheckableItemTemplate() {
      sandbox.innerHTML = '<div id="foo" class="goog-menuitem goog-option">' +
          '<div class="goog-menuitem-content">' +
          '<div class="goog-menuitem-checkbox"></div>' +
          'Foo</div></div>';
      var foo = goog.dom.getElement('foo');
      item.decorate(foo);
      assertEquals('Decorated element must be as expected', foo,
          item.getElement());
      assertTrue('Decorated element must have expected class name',
          goog.dom.classes.has(item.getElement(),
              item.getRenderer().getCssClass()));
      assertEquals('Content element must be the decorated element\'s child',
          item.getContentElement(), item.getElement().firstChild);
      assertTrue('Item must have checkbox structure',
          item.getRenderer().hasCheckBoxStructure(item.getElement()));
      assertEquals('Item must have exactly one checkbox structure', 1,
          goog.dom.getElementsByTagNameAndClass('div', 'goog-menuitem-checkbox',
              item.getElement()).length);
      assertFalse('Item must not be checked', item.isChecked());
    }

    function testDecorateCheckedItemTemplate() {
      sandbox.innerHTML = '<div id="foo" ' +
          'class="goog-menuitem goog-option goog-option-selected">' +
          '<div class="goog-menuitem-content">' +
          '<div class="goog-menuitem-checkbox"></div>' +
          'Foo</div></div>';
      var foo = goog.dom.getElement('foo');
      item.decorate(foo);
      assertEquals('Decorated element must be as expected', foo,
          item.getElement());
      assertSameElements('Decorated element must have expected class names',
          ['goog-menuitem', 'goog-option', 'goog-option-selected'],
          goog.dom.classes.get(item.getElement()));
      assertEquals('Content element must be the decorated element\'s child',
          item.getContentElement(), item.getElement().firstChild);
      assertTrue('Item must have checkbox structure',
          item.getRenderer().hasCheckBoxStructure(item.getElement()));
      assertEquals('Item must have exactly one checkbox structure', 1,
          goog.dom.getElementsByTagNameAndClass('div', 'goog-menuitem-checkbox',
              item.getElement()).length);
      assertTrue('Item must be checked', item.isChecked());
    }

    /** @bug 1463524 */
    function testHandleMouseUp() {
      var COORDS_1 = new goog.math.Coordinate(1, 1);
      var COORDS_2 = new goog.math.Coordinate(2, 2);
      item.setActive(true);
      // Override performActionInternal() for testing purposes.
      var actionPerformed;
      item.performActionInternal = function() {
        actionPerformed = true;
        return true;
      };
      item.render(sandbox);

      // Scenario 1: item has no parent.
      actionPerformed = false;
      item.setActive(true);
      goog.testing.events.fireMouseUpEvent(item.getElement());
      assertTrue('Action should be performed on mouseup', actionPerformed);

      // Scenario 2: item has a parent.
      actionPerformed = false;
      item.setActive(true);
      var parent = new goog.ui.Component();
      var parentElem = goog.dom.getElement('parentComponent');
      parent.render(parentElem);
      parent.addChild(item);
      parent.openingCoords = COORDS_1;
      goog.testing.events.fireMouseUpEvent(
          item.getElement(), undefined, COORDS_2);
      assertTrue('Action should be performed on mouseup', actionPerformed);

      // Scenario 3: item has a parent which was opened during mousedown, and
      // item, and now the mouseup fires at the same coords.
      actionPerformed = false;
      item.setActive(true);
      parent.openingCoords = COORDS_2;
      goog.testing.events.fireMouseUpEvent(
          item.getElement(), undefined, COORDS_2);
      assertFalse('Action should not be performed on mouseup', actionPerformed);
    }

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