| 1 |
// Copyright 2008 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.ContainerRendererTest'); |
| 16 |
goog.setTestOnly('goog.ui.ContainerRendererTest'); |
| 17 |
|
| 18 |
goog.require('goog.dom'); |
| 19 |
goog.require('goog.dom.TagName'); |
| 20 |
goog.require('goog.style'); |
| 21 |
goog.require('goog.testing.ExpectedFailures'); |
| 22 |
goog.require('goog.testing.PropertyReplacer'); |
| 23 |
goog.require('goog.testing.jsunit'); |
| 24 |
goog.require('goog.testing.ui.rendererasserts'); |
| 25 |
goog.require('goog.ui.Container'); |
| 26 |
goog.require('goog.ui.ContainerRenderer'); |
| 27 |
goog.require('goog.userAgent'); |
| 28 |
|
| 29 |
var renderer; |
| 30 |
var expectedFailures; |
| 31 |
var stubs = new goog.testing.PropertyReplacer(); |
| 32 |
|
| 33 |
function setUpPage() { |
| 34 |
expectedFailures = new goog.testing.ExpectedFailures(); |
| 35 |
} |
| 36 |
|
| 37 |
function setUp() { |
| 38 |
var sandbox = goog.dom.getElement('sandbox'); |
| 39 |
|
| 40 |
sandbox.appendChild( |
| 41 |
goog.dom.createDom(goog.dom.TagName.SPAN, {id: 'noTabIndex'}, 'Test')); |
| 42 |
sandbox.appendChild( |
| 43 |
goog.dom.createDom( |
| 44 |
goog.dom.TagName.DIV, |
| 45 |
{id: 'container', 'class': 'goog-container-horizontal'}, |
| 46 |
goog.dom.createDom( |
| 47 |
goog.dom.TagName.DIV, {id: 'control', 'class': 'goog-control'}, |
| 48 |
'Hello, world!'))); |
| 49 |
|
| 50 |
renderer = goog.ui.ContainerRenderer.getInstance(); |
| 51 |
} |
| 52 |
|
| 53 |
function tearDown() { |
| 54 |
goog.dom.removeChildren(goog.dom.getElement('sandbox')); |
| 55 |
stubs.reset(); |
| 56 |
expectedFailures.handleTearDown(); |
| 57 |
} |
| 58 |
|
| 59 |
function testGetInstance() { |
| 60 |
assertTrue( |
| 61 |
'getInstance() must return a ContainerRenderer', |
| 62 |
renderer instanceof goog.ui.ContainerRenderer); |
| 63 |
assertEquals( |
| 64 |
'getInstance() must return the same object each time', renderer, |
| 65 |
goog.ui.ContainerRenderer.getInstance()); |
| 66 |
} |
| 67 |
|
| 68 |
function testGetCustomRenderer() { |
| 69 |
var cssClass = 'special-css-class'; |
| 70 |
var containerRenderer = goog.ui.ContainerRenderer.getCustomRenderer( |
| 71 |
goog.ui.ContainerRenderer, cssClass); |
| 72 |
assertEquals( |
| 73 |
'Renderer should have returned the custom CSS class.', cssClass, |
| 74 |
containerRenderer.getCssClass()); |
| 75 |
} |
| 76 |
|
| 77 |
function testGetAriaRole() { |
| 78 |
assertUndefined('ARIA role must be undefined', renderer.getAriaRole()); |
| 79 |
} |
| 80 |
|
| 81 |
function testEnableTabIndex() { |
| 82 |
var container = goog.dom.getElement('container'); |
| 83 |
assertFalse( |
| 84 |
'Container must not have any tab index', |
| 85 |
goog.dom.isFocusableTabIndex(container)); |
| 86 |
|
| 87 |
// WebKit on Mac doesn't support tabIndex for arbitrary DOM elements |
| 88 |
// until version 527 or later. |
| 89 |
expectedFailures.expectFailureFor( |
| 90 |
goog.userAgent.WEBKIT && goog.userAgent.MAC && |
| 91 |
!goog.userAgent.isVersionOrHigher('527')); |
| 92 |
try { |
| 93 |
renderer.enableTabIndex(container, true); |
| 94 |
assertTrue( |
| 95 |
'Container must have a tab index', |
| 96 |
goog.dom.isFocusableTabIndex(container)); |
| 97 |
assertEquals('Container\'s tab index must be 0', 0, container.tabIndex); |
| 98 |
|
| 99 |
renderer.enableTabIndex(container, false); |
| 100 |
assertFalse( |
| 101 |
'Container must not have a tab index', |
| 102 |
goog.dom.isFocusableTabIndex(container)); |
| 103 |
assertEquals('Container\'s tab index must be -1', -1, container.tabIndex); |
| 104 |
} catch (e) { |
| 105 |
expectedFailures.handleException(e); |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
function testCreateDom() { |
| 110 |
var horizontal = |
| 111 |
new goog.ui.Container(goog.ui.Container.Orientation.HORIZONTAL); |
| 112 |
var element1 = renderer.createDom(horizontal); |
| 113 |
assertEquals('Element must be a DIV', 'DIV', element1.tagName); |
| 114 |
assertEquals( |
| 115 |
'Element must have the expected class name', |
| 116 |
'goog-container goog-container-horizontal', element1.className); |
| 117 |
|
| 118 |
var vertical = new goog.ui.Container(goog.ui.Container.Orientation.VERTICAL); |
| 119 |
var element2 = renderer.createDom(vertical); |
| 120 |
assertEquals('Element must be a DIV', 'DIV', element2.tagName); |
| 121 |
assertEquals( |
| 122 |
'Element must have the expected class name', |
| 123 |
'goog-container goog-container-vertical', element2.className); |
| 124 |
} |
| 125 |
|
| 126 |
function testGetContentElement() { |
| 127 |
assertNull( |
| 128 |
'getContentElement() must return null if element is null', |
| 129 |
renderer.getContentElement(null)); |
| 130 |
var element = goog.dom.getElement('container'); |
| 131 |
assertEquals( |
| 132 |
'getContentElement() must return its argument', element, |
| 133 |
renderer.getContentElement(element)); |
| 134 |
} |
| 135 |
|
| 136 |
function testCanDecorate() { |
| 137 |
assertFalse( |
| 138 |
'canDecorate() must return false for a SPAN', |
| 139 |
renderer.canDecorate(goog.dom.getElement('noTabIndex'))); |
| 140 |
assertTrue( |
| 141 |
'canDecorate() must return true for a DIV', |
| 142 |
renderer.canDecorate(goog.dom.getElement('container'))); |
| 143 |
} |
| 144 |
|
| 145 |
function testDecorate() { |
| 146 |
var container = new goog.ui.Container(); |
| 147 |
var element = goog.dom.getElement('container'); |
| 148 |
|
| 149 |
assertFalse( |
| 150 |
'Container must not be in the document', container.isInDocument()); |
| 151 |
container.decorate(element); |
| 152 |
assertTrue('Container must be in the document', container.isInDocument()); |
| 153 |
|
| 154 |
assertEquals( |
| 155 |
'Container\'s ID must match the decorated element\'s ID', element.id, |
| 156 |
container.getId()); |
| 157 |
assertEquals( |
| 158 |
'Element must have the expected class name', |
| 159 |
'goog-container-horizontal goog-container', element.className); |
| 160 |
assertEquals('Container must have one child', 1, container.getChildCount()); |
| 161 |
assertEquals( |
| 162 |
'Child component\'s ID must be as expected', 'control', |
| 163 |
container.getChildAt(0).getId()); |
| 164 |
|
| 165 |
assertThrows('Redecorating must throw error', function() { |
| 166 |
container.decorate(element); |
| 167 |
}); |
| 168 |
} |
| 169 |
|
| 170 |
function testDecorateWithCustomContainerElement() { |
| 171 |
var element = goog.dom.getElement('container'); |
| 172 |
var alternateContainerElement = goog.dom.createElement(goog.dom.TagName.DIV); |
| 173 |
element.appendChild(alternateContainerElement); |
| 174 |
|
| 175 |
var container = new goog.ui.Container(); |
| 176 |
stubs.set(renderer, 'getContentElement', function() { |
| 177 |
return alternateContainerElement; |
| 178 |
}); |
| 179 |
|
| 180 |
assertFalse( |
| 181 |
'Container must not be in the document', container.isInDocument()); |
| 182 |
container.decorate(element); |
| 183 |
assertTrue('Container must be in the document', container.isInDocument()); |
| 184 |
|
| 185 |
assertEquals( |
| 186 |
'Container\'s ID must match the decorated element\'s ID', element.id, |
| 187 |
container.getId()); |
| 188 |
assertEquals( |
| 189 |
'Element must have the expected class name', |
| 190 |
'goog-container-horizontal goog-container', element.className); |
| 191 |
assertEquals('Container must have 0 children', 0, container.getChildCount()); |
| 192 |
|
| 193 |
assertThrows('Redecorating must throw error', function() { |
| 194 |
container.decorate(element); |
| 195 |
}); |
| 196 |
} |
| 197 |
|
| 198 |
function testSetStateFromClassName() { |
| 199 |
var container = new goog.ui.Container(); |
| 200 |
|
| 201 |
assertEquals( |
| 202 |
'Container must be vertical', goog.ui.Container.Orientation.VERTICAL, |
| 203 |
container.getOrientation()); |
| 204 |
renderer.setStateFromClassName( |
| 205 |
container, 'goog-container-horizontal', 'goog-container'); |
| 206 |
assertEquals( |
| 207 |
'Container must be horizontal', goog.ui.Container.Orientation.HORIZONTAL, |
| 208 |
container.getOrientation()); |
| 209 |
renderer.setStateFromClassName( |
| 210 |
container, 'goog-container-vertical', 'goog-container'); |
| 211 |
assertEquals( |
| 212 |
'Container must be vertical', goog.ui.Container.Orientation.VERTICAL, |
| 213 |
container.getOrientation()); |
| 214 |
|
| 215 |
assertTrue('Container must be enabled', container.isEnabled()); |
| 216 |
renderer.setStateFromClassName( |
| 217 |
container, 'goog-container-disabled', 'goog-container'); |
| 218 |
assertFalse('Container must be disabled', container.isEnabled()); |
| 219 |
} |
| 220 |
|
| 221 |
function testInitializeDom() { |
| 222 |
var container = new goog.ui.Container(); |
| 223 |
var element = goog.dom.getElement('container'); |
| 224 |
container.decorate(element); |
| 225 |
|
| 226 |
assertTrue( |
| 227 |
'Container\'s root element must be unselectable', |
| 228 |
goog.style.isUnselectable(container.getElement())); |
| 229 |
|
| 230 |
assertEquals( |
| 231 |
'On IE, container\'s root element must have hideFocus=true', |
| 232 |
goog.userAgent.IE, !!container.getElement().hideFocus); |
| 233 |
} |
| 234 |
|
| 235 |
function testDoesntCallGetCssClassInConstructor() { |
| 236 |
goog.testing.ui.rendererasserts.assertNoGetCssClassCallsInConstructor( |
| 237 |
goog.ui.ContainerRenderer); |
| 238 |
} |