aboutsummaryrefslogtreecommitdiff
path: root/contexts/data/lib/closure-library/closure/goog/ui/controlrenderer_test.html
blob: 3cf44e7b3698ded3b16b22da11014286161920c8 (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
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
<!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.ControlRenderer</title>
  <script src="../base.js"></script>
  <script>
    goog.require('goog.dom.NodeType');
    goog.require('goog.dom.a11y');
    goog.require('goog.dom.a11y.Role');
    goog.require('goog.dom.a11y.State');
    goog.require('goog.dom.classes');
    goog.require('goog.style');
    goog.require('goog.testing.ExpectedFailures');
    goog.require('goog.testing.jsunit');
    goog.require('goog.ui.Component.State');
    goog.require('goog.ui.Control');
    goog.require('goog.ui.ControlRenderer');
    goog.require('goog.userAgent');
  </script>
</head>
<body>
  <div id="sandbox"></div>
  <script>
    var control, controlRenderer, testRenderer;
    var sandbox = goog.dom.getElement('sandbox');
    var expectedFailures = new goog.testing.ExpectedFailures();

    /**
     * A subclass of ControlRenderer that overrides {@code getAriaRole} and
     * {@code getStructuralCssClass} for testing purposes.
     * @constructor
     * @extends {goog.ui.ControlRenderer}
     */
    function TestRenderer() {
      goog.ui.ControlRenderer.call(this);
    };
    goog.inherits(TestRenderer, goog.ui.ControlRenderer);
    goog.addSingletonGetter(TestRenderer);

    TestRenderer.CSS_CLASS = 'goog-button';

    TestRenderer.IE6_CLASS_COMBINATIONS = [
      ['combined', 'goog-base-hover', 'goog-button'],
      ['combined', 'goog-base-disabled', 'goog-button'],
      ['combined', 'combined2', 'goog-base-hover', 'goog-base-rtl',
       'goog-button']
    ];

    /** {@override} */
    TestRenderer.prototype.getAriaRole = function() {
      return goog.dom.a11y.Role.BUTTON;
    };

    /** {@override} */
    TestRenderer.prototype.getCssClass = function() {
      return TestRenderer.CSS_CLASS;
    };

    /** {@override} */
    TestRenderer.prototype.getStructuralCssClass = function() {
      return 'goog-base';
    };

    /** {@override} */
    TestRenderer.prototype.getIe6ClassCombinations = function() {
      return TestRenderer.IE6_CLASS_COMBINATIONS;
    };

    /**
     * @return {boolean} Whether we're on Mac Safari 3.x.
     */
    function isMacSafari3() {
      return goog.userAgent.WEBKIT && goog.userAgent.MAC &&
          !goog.userAgent.isVersion('527');
    }

    /**
     * @return {boolean} Whether we're on IE6 or lower.
     */
    function isIe6() {
      return goog.userAgent.IE && !goog.userAgent.isVersion('7');
    }

    function setUp() {
      control = new goog.ui.Control('Hello');
      controlRenderer = goog.ui.ControlRenderer.getInstance();
      testRenderer = TestRenderer.getInstance();
    }

    function tearDown() {
      control.dispose();
      expectedFailures.handleTearDown();
      control = null;
      controlRenderer = null;
      testRenderer = null;
      goog.dom.removeChildren(sandbox);
    }

    function testConstructor() {
      assertNotNull('ControlRenderer singleton instance must not be null',
          controlRenderer);
      assertNotNull('TestRenderer singleton instance must not be null',
          testRenderer);
    }

    function testGetCustomRenderer() {
      var cssClass = 'special-css-class';
      var renderer = goog.ui.ControlRenderer.getCustomRenderer(
          goog.ui.ControlRenderer, cssClass);
      assertEquals(
          'Renderer should have returned the custom CSS class.',
          cssClass,
          renderer.getCssClass());
    }

    function testGetAriaRole() {
      assertUndefined('ControlRenderer\'s ARIA role must be undefined',
          controlRenderer.getAriaRole());
      assertEquals('TestRenderer\'s ARIA role must have expected value',
          goog.dom.a11y.Role.BUTTON, testRenderer.getAriaRole());
    }

    function testCreateDom() {
      assertHTMLEquals('ControlRenderer must create correct DOM',
          '<div class="goog-control">Hello</div>',
          goog.dom.getOuterHtml(controlRenderer.createDom(control)));
      assertHTMLEquals('TestRenderer must create correct DOM',
          '<div class="goog-button goog-base">Hello</div>',
          goog.dom.getOuterHtml(testRenderer.createDom(control)));
    }

    function testGetContentElement() {
      assertEquals('getContentElement() must return its argument', sandbox,
          controlRenderer.getContentElement(sandbox));
    }

    function testEnableExtraClassName() {
      // enableExtraClassName() must be a no-op if control has no DOM.
      controlRenderer.enableExtraClassName(control, 'foo', true);

      control.createDom();
      var element = control.getElement();

      controlRenderer.enableExtraClassName(control, 'foo', true);
      assertSameElements('Extra class name must have been added',
          ['goog-control', 'foo'], goog.dom.classes.get(element));

      controlRenderer.enableExtraClassName(control, 'foo', true);
      assertSameElements('Enabling existing extra class name must be a no-op',
          ['goog-control', 'foo'], goog.dom.classes.get(element));

      controlRenderer.enableExtraClassName(control, 'bar', false);
      assertSameElements('Disabling nonexistent class name must be a no-op',
          ['goog-control', 'foo'], goog.dom.classes.get(element));

      controlRenderer.enableExtraClassName(control, 'foo', false);
      assertSameElements('Extra class name must have been removed',
          ['goog-control'], goog.dom.classes.get(element));
    }

    function testCanDecorate() {
      assertTrue('canDecorate() must return true',
          controlRenderer.canDecorate());
    }

    function testDecorate() {
      sandbox.innerHTML = '<div id="foo">Hello, world!</div>';
      var foo = goog.dom.getElement('foo');
      var element = controlRenderer.decorate(control, foo);

      assertEquals('decorate() must return its argument', foo, element);
      assertEquals('Decorated control\'s ID must be set', 'foo',
          control.getId());
      assertTrue('Decorated control\'s content must be a text node',
          control.getContent().nodeType == goog.dom.NodeType.TEXT);
      assertEquals('Decorated control\'s content must have expected value',
          'Hello, world!', control.getContent().nodeValue);
      assertEquals('Decorated control\'s state must be as expected', 0x00,
          control.getState());
      assertSameElements('Decorated element\'s classes must be as expected',
          ['goog-control'], goog.dom.classes.get(element));
    }

    function testDecorateComplexDom() {
      sandbox.innerHTML = '<div id="foo"><i>Hello</i>,<b>world</b>!</div>';
      var foo = goog.dom.getElement('foo');
      var element = controlRenderer.decorate(control, foo);

      assertEquals('decorate() must return its argument', foo, element);
      assertEquals('Decorated control\'s ID must be set', 'foo',
          control.getId());
      assertTrue('Decorated control\'s content must be an array',
          goog.isArray(control.getContent()));
      assertEquals('Decorated control\'s content must have expected length', 4,
          control.getContent().length);
      assertEquals('Decorated control\'s state must be as expected', 0x00,
          control.getState());
      assertSameElements('Decorated element\'s classes must be as expected',
          ['goog-control'], goog.dom.classes.get(element));
    }

    function testDecorateWithClasses() {
      sandbox.innerHTML =
          '<div id="foo" class="app goog-base-disabled goog-base-hover"></div>';
      var foo = goog.dom.getElement('foo');

      control.addClassName('extra');
      var element = testRenderer.decorate(control, foo);

      assertEquals('decorate() must return its argument', foo, element);
      assertEquals('Decorated control\'s ID must be set', 'foo',
          control.getId());
      assertNull('Decorated control\'s content must be null',
          control.getContent());
      assertEquals('Decorated control\'s state must be as expected',
          goog.ui.Component.State.DISABLED | goog.ui.Component.State.HOVER,
          control.getState());
      assertSameElements('Decorated element\'s classes must be as expected', [
        'app',
        'extra',
        'goog-base',
        'goog-base-disabled',
        'goog-base-hover',
        'goog-button'
      ], goog.dom.classes.get(element));
    }

    function testDecorateOptimization() {
      // Temporarily replace goog.dom.classes.set().
      var goog$dom$classes$set = goog.dom.classes.set;
      goog.dom.classes.set = function() {
        fail('goog.dom.classes.set() must not be called');
      };

      // Since foo has all required classes, goog.dom.classes.set() must not be
      // called at all.
      sandbox.innerHTML = '<div id="foo" class="goog-control">Foo</div>';
      controlRenderer.decorate(control, goog.dom.getElement('foo'));

      // Since bar has all required classes, goog.dom.classes.set() must not be
      // called at all.
      sandbox.innerHTML = '<div id="bar" class="goog-base goog-button">Bar' +
          '</div>';
      testRenderer.decorate(control, goog.dom.getElement('bar'));

      // Since baz has all required classes, goog.dom.classes.set() must not be
      // called at all.
      sandbox.innerHTML = '<div id="baz" class="goog-base goog-button ' +
          'goog-button-disabled">Baz</div>';
      testRenderer.decorate(control, goog.dom.getElement('baz'));

      // Restore goog.dom.classes.set().
      goog.dom.classes.set = goog$dom$classes$set;
    }

    function testInitializeDom() {
      var renderer = new goog.ui.ControlRenderer();

      // Replace setRightToLeft().
      renderer.setRightToLeft = function() {
        fail('setRightToLeft() must not be called');
      };

      // When a control with default render direction enters the document,
      // setRightToLeft() must not be called.
      control.setRenderer(renderer);
      control.render(sandbox);

      // When a control in the default state (enabled, visible, focusable)
      // enters the document, it must get a tab index.
      // Expected to fail on Mac Safari 3, because it doesn't support tab index.
      expectedFailures.expectFailureFor(isMacSafari3());
      try {
        assertTrue('Enabled, visible, focusable control must have tab index',
            goog.dom.isFocusableTabIndex(control.getElement()));
      } catch (e) {
        expectedFailures.handleException(e);
      }
    }

    function testInitializeDomDecorated() {
      var renderer = new goog.ui.ControlRenderer();

      // Replace setRightToLeft().
      renderer.setRightToLeft = function() {
        fail('setRightToLeft() must not be called');
      };

      sandbox.innerHTML = '<div id="foo" class="goog-control">Foo</div>';

      // When a control with default render direction enters the document,
      // setRightToLeft() must not be called.
      control.setRenderer(renderer);
      control.decorate(goog.dom.getElement('foo'));

      // When a control in the default state (enabled, visible, focusable)
      // enters the document, it must get a tab index.
      // Expected to fail on Mac Safari 3, because it doesn't support tab index.
      expectedFailures.expectFailureFor(isMacSafari3());
      try {
        assertTrue('Enabled, visible, focusable control must have tab index',
            goog.dom.isFocusableTabIndex(control.getElement()));
      } catch (e) {
        expectedFailures.handleException(e);
      }
    }

    function testInitializeDomDisabledBiDi() {
      var renderer = new goog.ui.ControlRenderer();

      // Replace setFocusable().
      renderer.setFocusable = function() {
        fail('setFocusable() must not be called');
      };

      // When a disabled control enters the document, setFocusable() must not
      // be called.
      control.setEnabled(false);
      control.setRightToLeft(true);
      control.setRenderer(renderer);
      control.render(sandbox);

      // When a right-to-left control enters the document, special stying must
      // be applied.
      assertSameElements('BiDi control must have right-to-left class',
          ['goog-control', 'goog-control-disabled', 'goog-control-rtl'],
          goog.dom.classes.get(control.getElement()));
    }

    function testInitializeDomDisabledBiDiDecorated() {
      var renderer = new goog.ui.ControlRenderer();

      // Replace setFocusable().
      renderer.setFocusable = function() {
        fail('setFocusable() must not be called');
      };

      sandbox.innerHTML =
          '<div dir="rtl">\n' +
          '  <div id="foo" class="goog-control-disabled">Foo</div>\n' +
          '</div>\n';

      // When a disabled control enters the document, setFocusable() must not
      // be called.
      control.setRenderer(renderer);
      control.decorate(goog.dom.getElement('foo'));

      // When a right-to-left control enters the document, special stying must
      // be applied.
      assertSameElements('BiDi control must have right-to-left class',
          ['goog-control', 'goog-control-disabled', 'goog-control-rtl'],
          goog.dom.classes.get(control.getElement()));
    }

    function testSetAriaRole() {
      sandbox.innerHTML = '<div id="foo">Foo</div><div id="bar">Bar</div>';

      var foo = goog.dom.getElement('foo');
      controlRenderer.setAriaRole(foo);
      assertEquals('ControlRenderer must not set ARIA role', '',
          goog.dom.a11y.getRole(foo));

      var bar = goog.dom.getElement('bar');
      testRenderer.setAriaRole(bar);
      assertEquals('Element must have expected ARIA role',
          goog.dom.a11y.Role.BUTTON, goog.dom.a11y.getRole(bar));
    }

    function testSetAllowTextSelection() {
      sandbox.innerHTML = '<div id="foo"><span>Foo</span></div>';
      var foo = goog.dom.getElement('foo');

      controlRenderer.setAllowTextSelection(foo, false);
      assertTrue('Parent element must be unselectable on all browsers',
          goog.style.isUnselectable(foo));
      if (goog.userAgent.IE || goog.userAgent.OPERA) {
        assertTrue('On IE and Opera, child element must also be unselectable',
            goog.style.isUnselectable(foo.firstChild));
      } else {
        assertFalse('On browsers other than IE and Opera, the child element ' +
            'must not be unselectable',
            goog.style.isUnselectable(foo.firstChild));
      }

      controlRenderer.setAllowTextSelection(foo, true);
      assertFalse('Parent element must be selectable',
          goog.style.isUnselectable(foo));
      assertFalse('Child element must be unselectable',
          goog.style.isUnselectable(foo.firstChild));
    }

    function testSetRightToLeft() {
      sandbox.innerHTML = '<div id="foo">Foo</div><div id="bar">Bar</div>';

      var foo = goog.dom.getElement('foo');
      controlRenderer.setRightToLeft(foo, true);
      assertSameElements('Element must have right-to-left class applied',
          ['goog-control-rtl'], goog.dom.classes.get(foo));
      controlRenderer.setRightToLeft(foo, false);
      assertSameElements('Element must not have right-to-left class applied',
          [], goog.dom.classes.get(foo));

      var bar = goog.dom.getElement('bar');
      testRenderer.setRightToLeft(bar, true);
      assertSameElements('Element must have right-to-left class applied',
          ['goog-base-rtl'], goog.dom.classes.get(bar));
      testRenderer.setRightToLeft(bar, false);
      assertSameElements('Element must not have right-to-left class applied',
          [], goog.dom.classes.get(bar));
    }

    function testIsFocusable() {
      control.render(sandbox);
      // Expected to fail on Mac Safari 3, because it doesn't support tab index.
      expectedFailures.expectFailureFor(isMacSafari3());
      try {
        assertTrue('Control\'s key event target must be focusable',
            controlRenderer.isFocusable(control));
      } catch (e) {
        expectedFailures.handleException(e);
      }
    }

    function testIsFocusableForNonFocusableControl() {
      control.setSupportedState(goog.ui.Component.State.FOCUSED, false);
      control.render(sandbox);
      assertFalse('Non-focusable control\'s key event target must not be ' +
          'focusable', controlRenderer.isFocusable(control));
    }

    function testIsFocusableForControlWithoutKeyEventTarget() {
      // Unrendered control has no key event target.
      assertNull('Unrendered control must not have key event target',
          control.getKeyEventTarget());
      assertFalse('isFocusable() must return null if no key event target',
          controlRenderer.isFocusable(control));
    }

    function testSetFocusable() {
      control.render(sandbox);
      controlRenderer.setFocusable(control, false);
      assertFalse('Control\'s key event target must not have tab index',
          goog.dom.isFocusableTabIndex(control.getKeyEventTarget()));
      controlRenderer.setFocusable(control, true);
      // Expected to fail on Mac Safari 3, because it doesn't support tab index.
      expectedFailures.expectFailureFor(isMacSafari3());
      try {
        assertTrue('Control\'s key event target must have focusable tab index',
            goog.dom.isFocusableTabIndex(control.getKeyEventTarget()));
      } catch (e) {
        expectedFailures.handleException(e);
      }
    }

    function testSetFocusableForNonFocusableControl() {
      control.setSupportedState(goog.ui.Component.State.FOCUSED, false);
      control.render(sandbox);
      assertFalse('Non-focusable control\'s key event target must not be ' +
          'focusable',
          goog.dom.isFocusableTabIndex(control.getKeyEventTarget()));
      controlRenderer.setFocusable(control, true);
      assertFalse('Non-focusable control\'s key event target must not be ' +
          'focusable, even after calling setFocusable(true)',
          goog.dom.isFocusableTabIndex(control.getKeyEventTarget()));
    }

    function testSetVisible() {
      sandbox.innerHTML = '<div id="foo">Foo</div>';
      var foo = goog.dom.getElement('foo');
      assertTrue('Element must be visible', foo.style.display != 'none');
      controlRenderer.setVisible(foo, true);
      assertTrue('Element must still be visible', foo.style.display != 'none');
      controlRenderer.setVisible(foo, false);
      assertTrue('Element must be hidden', foo.style.display == 'none');
    }

    function testSetState() {
      control.setRenderer(testRenderer);
      control.createDom();
      var element = control.getElement();

      assertSameElements('Control must have expected class names',
          ['goog-button', 'goog-base'], goog.dom.classes.get(element));
      assertEquals('Control must not have disabled ARIA state', '',
          goog.dom.a11y.getState(element, goog.dom.a11y.State.DISABLED));

      testRenderer.setState(control, goog.ui.Component.State.DISABLED, true);
      assertSameElements('Control must have disabled class name',
          ['goog-button', 'goog-base', 'goog-base-disabled'],
          goog.dom.classes.get(element));
      assertEquals('Control must have disabled ARIA state', 'true',
          goog.dom.a11y.getState(element, goog.dom.a11y.State.DISABLED));

      testRenderer.setState(control, goog.ui.Component.State.DISABLED, false);
      assertSameElements('Control must no longer have disabled class name',
          ['goog-button', 'goog-base'], goog.dom.classes.get(element));
      assertEquals('Control must not have disabled ARIA state',
          'false',
          goog.dom.a11y.getState(element, goog.dom.a11y.State.DISABLED));

      testRenderer.setState(control, 0xFFFFFF, true);
      assertSameElements('Class names must be unchanged for invalid state',
          ['goog-button', 'goog-base'], goog.dom.classes.get(element));
    }

    function testUpdateAriaStateDisabled() {
      control.createDom();
      var element = control.getElement();

      controlRenderer.updateAriaState(element, goog.ui.Component.State.DISABLED,
          true);
      assertEquals('Control must have disabled ARIA state', 'true',
          goog.dom.a11y.getState(element, goog.dom.a11y.State.DISABLED));

      controlRenderer.updateAriaState(element, goog.ui.Component.State.DISABLED,
          false);
      assertEquals('Control must no longer have disabled ARIA state',
          'false',
          goog.dom.a11y.getState(element, goog.dom.a11y.State.DISABLED));
  }

    function testCreateDomAriaStateDisabled() {
      control.setEnabled(false);
      control.createDom();
      var element = control.getElement();

      assertFalse('Control must be disabled', control.isEnabled());
      assertEquals('Control must have disabled ARIA state', 'true',
        goog.dom.a11y.getState(element, goog.dom.a11y.State.DISABLED));
    }

    function testDecorateAriaStateDisabled() {
      sandbox.innerHTML =
          '<div id="foo" class="app goog-base-disabled"></div>';
      var foo = goog.dom.getElement('foo');

      var element = testRenderer.decorate(control, foo);

      assertFalse('Control must be disabled', control.isEnabled());
      assertEquals('Control must have disabled ARIA state', 'true',
        goog.dom.a11y.getState(element, goog.dom.a11y.State.DISABLED));
    }

    function testUpdateAriaStateSelected() {
      control.createDom();
      var element = control.getElement();

      controlRenderer.updateAriaState(element, goog.ui.Component.State.SELECTED,
          true);
      assertEquals('Control must have selected ARIA state', 'true',
          goog.dom.a11y.getState(element, goog.dom.a11y.State.SELECTED));

      controlRenderer.updateAriaState(element, goog.ui.Component.State.SELECTED,
          false);
      assertEquals('Control must no longer have selected ARIA state',
          'false',
          goog.dom.a11y.getState(element, goog.dom.a11y.State.SELECTED));
  }

    function testCreateDomAriaStateSelected() {
      control.setSupportedState(goog.ui.Component.State.SELECTED, true);
      control.setSelected(true);

      control.createDom();
      var element = control.getElement();

      assertTrue('Control must be selected', control.isSelected());
      assertEquals('Control must have selected ARIA state', 'true',
        goog.dom.a11y.getState(element, goog.dom.a11y.State.SELECTED));
    }

    function testDecorateAriaStateSelected() {
      sandbox.innerHTML =
          '<div id="foo" class="app goog-base-selected"></div>';
      var foo = goog.dom.getElement('foo');

      var element = testRenderer.decorate(control, foo);

      assertTrue('Control must be selected', control.isSelected());
      assertEquals('Control must have selected ARIA state', 'true',
        goog.dom.a11y.getState(element, goog.dom.a11y.State.SELECTED));
    }

    function testUpdateAriaStateChecked() {
      control.createDom();
      var element = control.getElement();

      controlRenderer.updateAriaState(element, goog.ui.Component.State.CHECKED,
          true);
      assertEquals('Control must have checked ARIA state', 'true',
          goog.dom.a11y.getState(element, goog.dom.a11y.State.CHECKED));

      controlRenderer.updateAriaState(element, goog.ui.Component.State.CHECKED,
          false);
      assertEquals('Control must no longer have checked ARIA state',
          'false',
          goog.dom.a11y.getState(element, goog.dom.a11y.State.CHECKED));
  }

    function testCreateDomAriaStateChecked() {
      control.setSupportedState(goog.ui.Component.State.CHECKED, true);
      control.setChecked(true);

      control.createDom();
      var element = control.getElement();

      assertTrue('Control must be checked', control.isChecked());
      assertEquals('Control must have checked ARIA state', 'true',
        goog.dom.a11y.getState(element, goog.dom.a11y.State.CHECKED));
  }

    function testDecorateAriaStateChecked() {
      sandbox.innerHTML =
          '<div id="foo" class="app goog-base-checked"></div>';
      var foo = goog.dom.getElement('foo');

      control.setSupportedState(goog.ui.Component.State.CHECKED, true);
      var element = testRenderer.decorate(control, foo);

      assertTrue('Control must be checked', control.isChecked());
      assertEquals('Control must have checked ARIA state', 'true',
        goog.dom.a11y.getState(element, goog.dom.a11y.State.CHECKED));
  }

    function testUpdateAriaStateOpened() {
      control.createDom();
      var element = control.getElement();

      controlRenderer.updateAriaState(element, goog.ui.Component.State.OPENED,
          true);
      assertEquals('Control must have expanded ARIA state', 'true',
          goog.dom.a11y.getState(element, goog.dom.a11y.State.EXPANDED));

      controlRenderer.updateAriaState(element, goog.ui.Component.State.OPENED,
          false);
      assertEquals('Control must no longer have expanded ARIA state',
          'false',
          goog.dom.a11y.getState(element, goog.dom.a11y.State.EXPANDED));
  }

    function testCreateDomAriaStateOpened() {
      control.setSupportedState(goog.ui.Component.State.OPENED, true);
      control.setOpen(true);

      control.createDom();
      var element = control.getElement();

      assertTrue('Control must be opened', control.isOpen());
      assertEquals('Control must have expanded ARIA state', 'true',
        goog.dom.a11y.getState(element, goog.dom.a11y.State.EXPANDED));
  }

    function testDecorateAriaStateOpened() {
      sandbox.innerHTML =
          '<div id="foo" class="app goog-base-open"></div>';
      var foo = goog.dom.getElement('foo');

      control.setSupportedState(goog.ui.Component.State.OPENED, true);
      var element = testRenderer.decorate(control, foo);

      assertTrue('Control must be opened', control.isOpen());
      assertEquals('Control must have expanded ARIA state', 'true',
        goog.dom.a11y.getState(element, goog.dom.a11y.State.EXPANDED));
  }

    function testSetContent() {
      sandbox.innerHTML = 'Hello, world!';
      controlRenderer.setContent(sandbox, 'Not so fast!');
      assertEquals('Element must contain expected text value', 'Not so fast!',
          goog.dom.getTextContent(sandbox));
    }

    function testSetContentNull() {
      sandbox.innerHTML = 'Hello, world!';
      controlRenderer.setContent(sandbox, null);
      assertEquals('Element must have no child nodes', 0,
          sandbox.childNodes.length);
      assertEquals('Element must contain expected text value', '',
          goog.dom.getTextContent(sandbox));
    }

    function testSetContentEmpty() {
      sandbox.innerHTML = 'Hello, world!';
      controlRenderer.setContent(sandbox, '');
      assertEquals('Element must not have children', 0,
          sandbox.childNodes.length);
      assertEquals('Element must contain expected text value', '',
          goog.dom.getTextContent(sandbox));
    }

    function testSetContentWhitespace() {
      sandbox.innerHTML = 'Hello, world!';
      controlRenderer.setContent(sandbox, ' ');
      assertEquals('Element must have one child', 1,
          sandbox.childNodes.length);
      assertEquals('Child must be a text node', goog.dom.NodeType.TEXT,
          sandbox.firstChild.nodeType);
      assertEquals('Element must contain expected text value', ' ',
          goog.dom.getTextContent(sandbox));
    }

    function testSetContentTextNode() {
      sandbox.innerHTML = 'Hello, world!';
      controlRenderer.setContent(sandbox, document.createTextNode('Text'));
      assertEquals('Element must have one child', 1,
          sandbox.childNodes.length);
      assertEquals('Child must be a text node', goog.dom.NodeType.TEXT,
          sandbox.firstChild.nodeType);
      assertEquals('Element must contain expected text value', 'Text',
          goog.dom.getTextContent(sandbox));
    }

    function testSetContentElementNode() {
      sandbox.innerHTML = 'Hello, world!';
      controlRenderer.setContent(sandbox,
          goog.dom.createDom('div', {id: 'foo'}, 'Foo'));
      assertEquals('Element must have one child', 1,
          sandbox.childNodes.length);
      assertEquals('Child must be an element node', goog.dom.NodeType.ELEMENT,
          sandbox.firstChild.nodeType);
      assertHTMLEquals('Element must contain expected HTML',
          '<div id="foo">Foo</div>', sandbox.innerHTML);
    }

    function testSetContentArray() {
      sandbox.innerHTML = 'Hello, world!';
      controlRenderer.setContent(sandbox,
          ['Hello, ', goog.dom.createDom('b', null, 'world'), '!']);
      assertEquals('Element must have three children', 3,
          sandbox.childNodes.length);
      assertEquals('1st child must be a text node', goog.dom.NodeType.TEXT,
          sandbox.childNodes[0].nodeType);
      assertEquals('2nd child must be an element', goog.dom.NodeType.ELEMENT,
          sandbox.childNodes[1].nodeType);
      assertEquals('3rd child must be a text node', goog.dom.NodeType.TEXT,
          sandbox.childNodes[2].nodeType);
      assertHTMLEquals('Element must contain expected HTML',
          'Hello, <b>world</b>!', sandbox.innerHTML);
    }

    function testSetContentNodeList() {
      sandbox.innerHTML = 'Hello, world!';
      var div = goog.dom.createDom('div', null, 'Hello, ',
          goog.dom.createDom('b', null, 'world'), '!');
      controlRenderer.setContent(sandbox, div.childNodes);
      assertEquals('Element must have three children', 3,
          sandbox.childNodes.length);
      assertEquals('1st child must be a text node', goog.dom.NodeType.TEXT,
          sandbox.childNodes[0].nodeType);
      assertEquals('2nd child must be an element', goog.dom.NodeType.ELEMENT,
          sandbox.childNodes[1].nodeType);
      assertEquals('3rd child must be a text node', goog.dom.NodeType.TEXT,
          sandbox.childNodes[2].nodeType);
      assertHTMLEquals('Element must contain expected HTML',
          'Hello, <b>world</b>!', sandbox.innerHTML);
    }

    function testGetKeyEventTarget() {
      assertNull('Key event target for unrendered control must be null',
          controlRenderer.getKeyEventTarget(control));
      control.createDom();
      assertEquals('Key event target for rendered control must be its element',
          control.getElement(), controlRenderer.getKeyEventTarget(control));
    }

    function testGetCssClass() {
      assertEquals('ControlRenderer\'s CSS class must have expected value',
          goog.ui.ControlRenderer.CSS_CLASS, controlRenderer.getCssClass());
      assertEquals('TestRenderer\'s CSS class must have expected value',
          TestRenderer.CSS_CLASS, testRenderer.getCssClass());
    }

    function testGetStructuralCssClass() {
      assertEquals('ControlRenderer\'s structural class must be its CSS class',
          controlRenderer.getCssClass(),
          controlRenderer.getStructuralCssClass());
      assertEquals('TestRenderer\'s structural class must have expected value',
          'goog-base', testRenderer.getStructuralCssClass());
    }

    function testGetClassNames() {
      // These tests use assertArrayEquals, because the order is significant.
      assertArrayEquals('ControlRenderer must return expected class names ' +
          'in the expected order',
          ['goog-control'],
          controlRenderer.getClassNames(control));
      assertArrayEquals('TestRenderer must return expected class names ' +
          'in the expected order',
          ['goog-button', 'goog-base'],
          testRenderer.getClassNames(control));
    }

    function testGetClassNamesForControlWithState() {
      control.setStateInternal(goog.ui.Component.State.HOVER |
          goog.ui.Component.State.ACTIVE);

      // These tests use assertArrayEquals, because the order is significant.
      assertArrayEquals('ControlRenderer must return expected class names ' +
          'in the expected order',
          ['goog-control', 'goog-control-hover', 'goog-control-active'],
          controlRenderer.getClassNames(control));
      assertArrayEquals('TestRenderer must return expected class names ' +
          'in the expected order',
          ['goog-button', 'goog-base', 'goog-base-hover', 'goog-base-active'],
          testRenderer.getClassNames(control));
    }

    function testGetClassNamesForControlWithExtraClassNames() {
      control.addClassName('foo');
      control.addClassName('bar');

      // These tests use assertArrayEquals, because the order is significant.
      assertArrayEquals('ControlRenderer must return expected class names ' +
          'in the expected order',
          ['goog-control', 'foo', 'bar'],
          controlRenderer.getClassNames(control));
      assertArrayEquals('TestRenderer must return expected class names ' +
          'in the expected order',
          ['goog-button', 'goog-base', 'foo', 'bar'],
          testRenderer.getClassNames(control));
    }

    function testGetClassNamesForControlWithStateAndExtraClassNames() {
      control.setStateInternal(goog.ui.Component.State.HOVER |
          goog.ui.Component.State.ACTIVE);
      control.addClassName('foo');
      control.addClassName('bar');

      // These tests use assertArrayEquals, because the order is significant.
      assertArrayEquals('ControlRenderer must return expected class names ' +
          'in the expected order', [
            'goog-control',
            'goog-control-hover',
            'goog-control-active',
            'foo',
            'bar'
          ], controlRenderer.getClassNames(control));
      assertArrayEquals('TestRenderer must return expected class names ' +
          'in the expected order', [
            'goog-button',
            'goog-base',
            'goog-base-hover',
            'goog-base-active',
            'foo',
            'bar'
          ], testRenderer.getClassNames(control));
    }

    function testGetClassNamesForState() {
      // These tests use assertArrayEquals, because the order is significant.
      assertArrayEquals('ControlRenderer must return expected class names ' +
          'in the expected order',
          ['goog-control-hover', 'goog-control-checked'],
          controlRenderer.getClassNamesForState(goog.ui.Component.State.HOVER |
              goog.ui.Component.State.CHECKED));
      assertArrayEquals('TestRenderer must return expected class names ' +
          'in the expected order',
          ['goog-base-hover', 'goog-base-checked'],
          testRenderer.getClassNamesForState(goog.ui.Component.State.HOVER |
              goog.ui.Component.State.CHECKED));
    }

    function testGetClassForState() {
      var renderer = new goog.ui.ControlRenderer();
      assertUndefined('State-to-class map must not exist until first use',
          renderer.classByState_);
      assertEquals('Renderer must return expected class name for SELECTED',
          'goog-control-selected',
          renderer.getClassForState(goog.ui.Component.State.SELECTED));
      assertUndefined('Renderer must return undefined for invalid state',
          renderer.getClassForState('foo'));
    }

    function testGetStateFromClass() {
      var renderer = new goog.ui.ControlRenderer();
      assertUndefined('Class-to-state map must not exist until first use',
          renderer.stateByClass_);
      assertEquals('Renderer must return expected state',
          goog.ui.Component.State.SELECTED,
          renderer.getStateFromClass('goog-control-selected'));
      assertEquals('Renderer must return 0x00 for unknown class',
          0x00,
          renderer.getStateFromClass('goog-control-annoyed'));
    }

    function testIe6ClassCombinationsCreateDom() {
      control.setRenderer(testRenderer);

      control.enableClassName('combined', true);

      control.createDom();
      var element = control.getElement();

      testRenderer.setState(control, goog.ui.Component.State.DISABLED, true);
      var expectedClasses = [
        'combined',
        'goog-base',
        'goog-base-disabled',
        'goog-button'
      ];
      if (isIe6()) {
        assertSameElements('IE6 and lower should have one combined class',
            expectedClasses.concat(['combined_goog-base-disabled_goog-button']),
            goog.dom.classes.get(element));
      } else {
        assertSameElements('Non IE6 browsers should not have a combined class',
            expectedClasses, goog.dom.classes.get(element));
      }

      testRenderer.setState(control, goog.ui.Component.State.DISABLED, false);
      testRenderer.setState(control, goog.ui.Component.State.HOVER, true);
      var expectedClasses = [
        'combined',
        'goog-base',
        'goog-base-hover',
        'goog-button'
      ];
      if (isIe6()) {
        assertSameElements('IE6 and lower should have one combined class',
            expectedClasses.concat(['combined_goog-base-hover_goog-button']),
            goog.dom.classes.get(element));
      } else {
        assertSameElements('Non IE6 browsers should not have a combined class',
            expectedClasses, goog.dom.classes.get(element));
      }

      testRenderer.setRightToLeft(element, true);
      testRenderer.enableExtraClassName(control, 'combined2', true);
      var expectedClasses = [
        'combined',
        'combined2',
        'goog-base',
        'goog-base-hover',
        'goog-base-rtl',
        'goog-button'
      ];
      if (isIe6()) {
        assertSameElements('IE6 and lower should have two combined class',
            expectedClasses.concat([
              'combined_goog-base-hover_goog-button',
              'combined_combined2_goog-base-hover_goog-base-rtl_goog-button'
            ]), goog.dom.classes.get(element));
      } else {
        assertSameElements('Non IE6 browsers should not have a combined class',
            expectedClasses, goog.dom.classes.get(element));
      }

    }

    function testIe6ClassCombinationsDecorate() {
      sandbox.innerHTML =
          '<div id="foo" class="combined goog-base-hover"></div>';
      var foo = goog.dom.getElement('foo');

      var element = testRenderer.decorate(control, foo);

      var expectedClasses = [
        'combined',
        'goog-base',
        'goog-base-hover',
        'goog-button'
      ];
      if (isIe6()) {
        assertSameElements('IE6 and lower should have one combined class',
            expectedClasses.concat(['combined_goog-base-hover_goog-button']),
            goog.dom.classes.get(element));
      } else {
        assertSameElements('Non IE6 browsers should not have a combined class',
            expectedClasses, goog.dom.classes.get(element));
      }

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