aboutsummaryrefslogtreecommitdiff
path: root/contexts/data/lib/closure-library/closure/goog/ui/containerrenderer_test.html
blob: 6aa1ab2d083cfdc3f9b7cdab4f43d483eeb058fc (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
<!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.ContainerRenderer</title>
  <script src="../base.js"></script>
  <script>
    goog.require('goog.dom');
    goog.require('goog.testing.ExpectedFailures');
    goog.require('goog.testing.PropertyReplacer');
    goog.require('goog.testing.jsunit');
    goog.require('goog.testing.ui.rendererasserts');
    goog.require('goog.ui.Container');
    goog.require('goog.ui.ContainerRenderer');
    goog.require('goog.ui.Control');
    goog.require('goog.ui.ControlRenderer');
  </script>
</head>
<body>
  <div id="sandbox"></div>

  <script>
    var renderer;
    var expectedFailures = new goog.testing.ExpectedFailures();
    var stubs = new goog.testing.PropertyReplacer();

    function setUp() {
      var sandbox = goog.dom.getElement('sandbox');

      sandbox.appendChild(goog.dom.createDom('span', {
        id: 'noTabIndex'
      }, 'Test'));
      sandbox.appendChild(goog.dom.createDom('div', {
        id: 'container',
        'class': 'goog-container-horizontal'
      }, goog.dom.createDom('div', {
        id: 'control',
        'class': 'goog-control'
      }, 'Hello, world!')));

      renderer = goog.ui.ContainerRenderer.getInstance();
    }

    function tearDown() {
      goog.dom.getElement('sandbox').innerHTML = '';
      stubs.reset();
      expectedFailures.handleTearDown();
    }

    function testGetInstance() {
      assertTrue('getInstance() must return a ContainerRenderer',
          renderer instanceof goog.ui.ContainerRenderer);
      assertEquals('getInstance() must return the same object each time',
          renderer, goog.ui.ContainerRenderer.getInstance());
    }

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

    function testGetAriaRole() {
      assertUndefined('ARIA role must be undefined', renderer.getAriaRole());
    }

    function testEnableTabIndex() {
      var container = goog.dom.getElement('container');
      assertFalse('Container must not have any tab index',
          goog.dom.isFocusableTabIndex(container));

      // WebKit on Mac doesn't support tabIndex for arbitrary DOM elements
      // until version 527 or later.
      expectedFailures.expectFailureFor(goog.userAgent.WEBKIT &&
          goog.userAgent.MAC && !goog.userAgent.isVersion('527'));
      try {
        renderer.enableTabIndex(container, true);
        assertTrue('Container must have a tab index',
            goog.dom.isFocusableTabIndex(container));
        assertEquals('Container\'s tab index must be 0', 0, container.tabIndex);

        renderer.enableTabIndex(container, false);
        assertFalse('Container must not have a tab index',
            goog.dom.isFocusableTabIndex(container));
        assertEquals('Container\'s tab index must be -1', -1,
            container.tabIndex);
      } catch (e) {
        expectedFailures.handleException(e);
      }
    }

    function testCreateDom() {
      var horizontal = new goog.ui.Container(
          goog.ui.Container.Orientation.HORIZONTAL);
      var element1 = renderer.createDom(horizontal);
      assertEquals('Element must be a DIV', 'DIV', element1.tagName);
      assertEquals('Element must have the expected class name',
          'goog-container goog-container-horizontal',
          element1.className);

      var vertical = new goog.ui.Container(
          goog.ui.Container.Orientation.VERTICAL);
      var element2 = renderer.createDom(vertical);
      assertEquals('Element must be a DIV', 'DIV', element2.tagName);
      assertEquals('Element must have the expected class name',
          'goog-container goog-container-vertical',
          element2.className);
    }

    function testGetContentElement() {
      assertNull('getContentElement() must return null if element is null',
          renderer.getContentElement(null));
      var element = goog.dom.getElement('container');
      assertEquals('getContentElement() must return its argument',
          element, renderer.getContentElement(element));
    }

    function testCanDecorate() {
      assertFalse('canDecorate() must return false for a SPAN',
          renderer.canDecorate(goog.dom.getElement('noTabIndex')));
      assertTrue('canDecorate() must return true for a DIV',
          renderer.canDecorate(goog.dom.getElement('container')));
    }

    function testDecorate() {
      var container = new goog.ui.Container();
      var element = goog.dom.getElement('container');

      assertFalse('Container must not be in the document',
          container.isInDocument());
      container.decorate(element);
      assertTrue('Container must be in the document',
          container.isInDocument());

      assertEquals('Container\'s ID must match the decorated element\'s ID',
          element.id, container.getId());
      assertEquals('Element must have the expected class name',
          'goog-container-horizontal goog-container', element.className);
      assertEquals('Container must have one child', 1,
          container.getChildCount());
      assertEquals('Child component\'s ID must be as expected', 'control',
          container.getChildAt(0).getId());

      assertThrows('Redecorating must throw error', function() {
        container.decorate(element);
      });
    }
    
    function testDecorateWithCustomContainerElement() {
      var element = goog.dom.getElement('container');
      var alternateContainerElement = goog.dom.createElement('div');
      element.appendChild(alternateContainerElement);
      
      var container = new goog.ui.Container();
      stubs.set(renderer, 'getContentElement', function() {
        return alternateContainerElement;
      });

      assertFalse('Container must not be in the document',
          container.isInDocument());
      container.decorate(element);
      assertTrue('Container must be in the document',
          container.isInDocument());

      assertEquals('Container\'s ID must match the decorated element\'s ID',
          element.id, container.getId());
      assertEquals('Element must have the expected class name',
          'goog-container-horizontal goog-container', element.className);
      assertEquals('Container must have 0 children', 0,
          container.getChildCount());

      assertThrows('Redecorating must throw error', function() {
        container.decorate(element);
      });
    }

    function testSetStateFromClassName() {
      var container = new goog.ui.Container();

      assertEquals('Container must be vertical',
          goog.ui.Container.Orientation.VERTICAL, container.getOrientation());
      renderer.setStateFromClassName(container, 'goog-container-horizontal',
          'goog-container');
      assertEquals('Container must be horizontal',
          goog.ui.Container.Orientation.HORIZONTAL, container.getOrientation());
      renderer.setStateFromClassName(container, 'goog-container-vertical',
          'goog-container');
      assertEquals('Container must be vertical',
          goog.ui.Container.Orientation.VERTICAL, container.getOrientation());

      assertTrue('Container must be enabled', container.isEnabled());
      renderer.setStateFromClassName(container, 'goog-container-disabled',
          'goog-container');
      assertFalse('Container must be disabled', container.isEnabled());
    }

    function testInitializeDom() {
      var container = new goog.ui.Container();
      var element = goog.dom.getElement('container');
      container.decorate(element);

      assertTrue('Container\'s root element must be unselectable',
          goog.style.isUnselectable(container.getElement()));

      assertEquals('On IE, container\'s root element must have hideFocus=true',
          goog.userAgent.IE, !!container.getElement().hideFocus);
    }

    function testDoesntCallGetCssClassInConstructor() {
      goog.testing.ui.rendererasserts.
          assertNoGetCssClassCallsInConstructor(goog.ui.ContainerRenderer);
    }
  </script>
</body>
</html>