| 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.ControlRendererTest'); |
| 16 |
goog.setTestOnly('goog.ui.ControlRendererTest'); |
| 17 |
|
| 18 |
goog.require('goog.a11y.aria'); |
| 19 |
goog.require('goog.a11y.aria.Role'); |
| 20 |
goog.require('goog.a11y.aria.State'); |
| 21 |
goog.require('goog.dom'); |
| 22 |
goog.require('goog.dom.NodeType'); |
| 23 |
goog.require('goog.dom.TagName'); |
| 24 |
goog.require('goog.dom.classlist'); |
| 25 |
goog.require('goog.object'); |
| 26 |
goog.require('goog.style'); |
| 27 |
goog.require('goog.testing.ExpectedFailures'); |
| 28 |
goog.require('goog.testing.PropertyReplacer'); |
| 29 |
goog.require('goog.testing.jsunit'); |
| 30 |
goog.require('goog.ui.Component'); |
| 31 |
goog.require('goog.ui.Control'); |
| 32 |
goog.require('goog.ui.ControlRenderer'); |
| 33 |
goog.require('goog.userAgent'); |
| 34 |
|
| 35 |
var control, controlRenderer, testRenderer, propertyReplacer; |
| 36 |
var sandbox; |
| 37 |
var expectedFailures; |
| 38 |
|
| 39 |
function setUpPage() { |
| 40 |
sandbox = goog.dom.getElement('sandbox'); |
| 41 |
expectedFailures = new goog.testing.ExpectedFailures(); |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
/** |
| 47 |
* A subclass of ControlRenderer that overrides {@code getAriaRole} and |
| 48 |
* {@code getStructuralCssClass} for testing purposes. |
| 49 |
* @constructor |
| 50 |
* @extends {goog.ui.ControlRenderer} |
| 51 |
*/ |
| 52 |
function TestRenderer() { |
| 53 |
goog.ui.ControlRenderer.call(this); |
| 54 |
} |
| 55 |
goog.inherits(TestRenderer, goog.ui.ControlRenderer); |
| 56 |
goog.addSingletonGetter(TestRenderer); |
| 57 |
|
| 58 |
TestRenderer.CSS_CLASS = 'goog-button'; |
| 59 |
|
| 60 |
TestRenderer.IE6_CLASS_COMBINATIONS = [ |
| 61 |
['combined', 'goog-base-hover', 'goog-button'], |
| 62 |
['combined', 'goog-base-disabled', 'goog-button'], |
| 63 |
['combined', 'combined2', 'goog-base-hover', 'goog-base-rtl', 'goog-button'] |
| 64 |
]; |
| 65 |
|
| 66 |
|
| 67 |
/** @override */ |
| 68 |
TestRenderer.prototype.getAriaRole = function() { |
| 69 |
return goog.a11y.aria.Role.BUTTON; |
| 70 |
}; |
| 71 |
|
| 72 |
|
| 73 |
/** @override */ |
| 74 |
TestRenderer.prototype.getCssClass = function() { |
| 75 |
return TestRenderer.CSS_CLASS; |
| 76 |
}; |
| 77 |
|
| 78 |
|
| 79 |
/** @override */ |
| 80 |
TestRenderer.prototype.getStructuralCssClass = function() { |
| 81 |
return 'goog-base'; |
| 82 |
}; |
| 83 |
|
| 84 |
|
| 85 |
/** @override */ |
| 86 |
TestRenderer.prototype.getIe6ClassCombinations = function() { |
| 87 |
return TestRenderer.IE6_CLASS_COMBINATIONS; |
| 88 |
}; |
| 89 |
|
| 90 |
|
| 91 |
/** |
| 92 |
* @return {boolean} Whether we're on Mac Safari 3.x. |
| 93 |
*/ |
| 94 |
function isMacSafari3() { |
| 95 |
return goog.userAgent.WEBKIT && goog.userAgent.MAC && |
| 96 |
!goog.userAgent.isVersionOrHigher('527'); |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
/** |
| 101 |
* @return {boolean} Whether we're on IE6 or lower. |
| 102 |
*/ |
| 103 |
function isIe6() { |
| 104 |
return goog.userAgent.IE && !goog.userAgent.isVersionOrHigher('7'); |
| 105 |
} |
| 106 |
|
| 107 |
function setUp() { |
| 108 |
control = new goog.ui.Control('Hello'); |
| 109 |
controlRenderer = goog.ui.ControlRenderer.getInstance(); |
| 110 |
testRenderer = TestRenderer.getInstance(); |
| 111 |
propertyReplacer = new goog.testing.PropertyReplacer(); |
| 112 |
} |
| 113 |
|
| 114 |
function tearDown() { |
| 115 |
propertyReplacer.reset(); |
| 116 |
control.dispose(); |
| 117 |
expectedFailures.handleTearDown(); |
| 118 |
control = null; |
| 119 |
controlRenderer = null; |
| 120 |
testRenderer = null; |
| 121 |
goog.dom.removeChildren(sandbox); |
| 122 |
} |
| 123 |
|
| 124 |
function testConstructor() { |
| 125 |
assertNotNull( |
| 126 |
'ControlRenderer singleton instance must not be null', controlRenderer); |
| 127 |
assertNotNull( |
| 128 |
'TestRenderer singleton instance must not be null', testRenderer); |
| 129 |
} |
| 130 |
|
| 131 |
function testGetCustomRenderer() { |
| 132 |
var cssClass = 'special-css-class'; |
| 133 |
var renderer = goog.ui.ControlRenderer.getCustomRenderer( |
| 134 |
goog.ui.ControlRenderer, cssClass); |
| 135 |
assertEquals( |
| 136 |
'Renderer should have returned the custom CSS class.', cssClass, |
| 137 |
renderer.getCssClass()); |
| 138 |
} |
| 139 |
|
| 140 |
function testGetAriaRole() { |
| 141 |
assertUndefined( |
| 142 |
'ControlRenderer\'s ARIA role must be undefined', |
| 143 |
controlRenderer.getAriaRole()); |
| 144 |
assertEquals( |
| 145 |
'TestRenderer\'s ARIA role must have expected value', |
| 146 |
goog.a11y.aria.Role.BUTTON, testRenderer.getAriaRole()); |
| 147 |
} |
| 148 |
|
| 149 |
function testCreateDom() { |
| 150 |
assertHTMLEquals( |
| 151 |
'ControlRenderer must create correct DOM', |
| 152 |
'<div class="goog-control">Hello</div>', |
| 153 |
goog.dom.getOuterHtml(controlRenderer.createDom(control))); |
| 154 |
assertHTMLEquals( |
| 155 |
'TestRenderer must create correct DOM', |
| 156 |
'<div class="goog-button goog-base">Hello</div>', |
| 157 |
goog.dom.getOuterHtml(testRenderer.createDom(control))); |
| 158 |
} |
| 159 |
|
| 160 |
function testGetContentElement() { |
| 161 |
assertEquals( |
| 162 |
'getContentElement() must return its argument', sandbox, |
| 163 |
controlRenderer.getContentElement(sandbox)); |
| 164 |
} |
| 165 |
|
| 166 |
function testEnableExtraClassName() { |
| 167 |
// enableExtraClassName() must be a no-op if control has no DOM. |
| 168 |
controlRenderer.enableExtraClassName(control, 'foo', true); |
| 169 |
|
| 170 |
control.createDom(); |
| 171 |
var element = control.getElement(); |
| 172 |
|
| 173 |
controlRenderer.enableExtraClassName(control, 'foo', true); |
| 174 |
assertSameElements( |
| 175 |
'Extra class name must have been added', ['goog-control', 'foo'], |
| 176 |
goog.dom.classlist.get(element)); |
| 177 |
|
| 178 |
controlRenderer.enableExtraClassName(control, 'foo', true); |
| 179 |
assertSameElements( |
| 180 |
'Enabling existing extra class name must be a no-op', |
| 181 |
['goog-control', 'foo'], goog.dom.classlist.get(element)); |
| 182 |
|
| 183 |
controlRenderer.enableExtraClassName(control, 'bar', false); |
| 184 |
assertSameElements( |
| 185 |
'Disabling nonexistent class name must be a no-op', |
| 186 |
['goog-control', 'foo'], goog.dom.classlist.get(element)); |
| 187 |
|
| 188 |
controlRenderer.enableExtraClassName(control, 'foo', false); |
| 189 |
assertSameElements( |
| 190 |
'Extra class name must have been removed', ['goog-control'], |
| 191 |
goog.dom.classlist.get(element)); |
| 192 |
} |
| 193 |
|
| 194 |
function testCanDecorate() { |
| 195 |
assertTrue('canDecorate() must return true', controlRenderer.canDecorate()); |
| 196 |
} |
| 197 |
|
| 198 |
function testDecorate() { |
| 199 |
sandbox.innerHTML = '<div id="foo">Hello, world!</div>'; |
| 200 |
var foo = goog.dom.getElement('foo'); |
| 201 |
var element = controlRenderer.decorate(control, foo); |
| 202 |
|
| 203 |
assertEquals('decorate() must return its argument', foo, element); |
| 204 |
assertEquals('Decorated control\'s ID must be set', 'foo', control.getId()); |
| 205 |
assertTrue( |
| 206 |
'Decorated control\'s content must be a text node', |
| 207 |
control.getContent().nodeType == goog.dom.NodeType.TEXT); |
| 208 |
assertEquals( |
| 209 |
'Decorated control\'s content must have expected value', 'Hello, world!', |
| 210 |
control.getContent().nodeValue); |
| 211 |
assertEquals( |
| 212 |
'Decorated control\'s state must be as expected', 0x00, |
| 213 |
control.getState()); |
| 214 |
assertSameElements( |
| 215 |
'Decorated element\'s classes must be as expected', ['goog-control'], |
| 216 |
goog.dom.classlist.get(element)); |
| 217 |
} |
| 218 |
|
| 219 |
function testDecorateComplexDom() { |
| 220 |
sandbox.innerHTML = '<div id="foo"><i>Hello</i>,<b>world</b>!</div>'; |
| 221 |
var foo = goog.dom.getElement('foo'); |
| 222 |
var element = controlRenderer.decorate(control, foo); |
| 223 |
|
| 224 |
assertEquals('decorate() must return its argument', foo, element); |
| 225 |
assertEquals('Decorated control\'s ID must be set', 'foo', control.getId()); |
| 226 |
assertTrue( |
| 227 |
'Decorated control\'s content must be an array', |
| 228 |
goog.isArray(control.getContent())); |
| 229 |
assertEquals( |
| 230 |
'Decorated control\'s content must have expected length', 4, |
| 231 |
control.getContent().length); |
| 232 |
assertEquals( |
| 233 |
'Decorated control\'s state must be as expected', 0x00, |
| 234 |
control.getState()); |
| 235 |
assertSameElements( |
| 236 |
'Decorated element\'s classes must be as expected', ['goog-control'], |
| 237 |
goog.dom.classlist.get(element)); |
| 238 |
} |
| 239 |
|
| 240 |
function testDecorateWithClasses() { |
| 241 |
sandbox.innerHTML = |
| 242 |
'<div id="foo" class="app goog-base-disabled goog-base-hover"></div>'; |
| 243 |
var foo = goog.dom.getElement('foo'); |
| 244 |
|
| 245 |
control.addClassName('extra'); |
| 246 |
var element = testRenderer.decorate(control, foo); |
| 247 |
|
| 248 |
assertEquals('decorate() must return its argument', foo, element); |
| 249 |
assertEquals('Decorated control\'s ID must be set', 'foo', control.getId()); |
| 250 |
assertNull('Decorated control\'s content must be null', control.getContent()); |
| 251 |
assertEquals( |
| 252 |
'Decorated control\'s state must be as expected', |
| 253 |
goog.ui.Component.State.DISABLED | goog.ui.Component.State.HOVER, |
| 254 |
control.getState()); |
| 255 |
assertSameElements( |
| 256 |
'Decorated element\'s classes must be as expected', |
| 257 |
[ |
| 258 |
'app', 'extra', 'goog-base', 'goog-base-disabled', 'goog-base-hover', |
| 259 |
'goog-button' |
| 260 |
], |
| 261 |
goog.dom.classlist.get(element)); |
| 262 |
} |
| 263 |
|
| 264 |
function testDecorateOptimization() { |
| 265 |
// Temporarily replace goog.dom.classlist.set(). |
| 266 |
propertyReplacer.set(goog.dom.classlist, 'set', function() { |
| 267 |
fail('goog.dom.classlist.set() must not be called'); |
| 268 |
}); |
| 269 |
|
| 270 |
// Since foo has all required classes, goog.dom.classlist.set() must not be |
| 271 |
// called at all. |
| 272 |
sandbox.innerHTML = '<div id="foo" class="goog-control">Foo</div>'; |
| 273 |
controlRenderer.decorate(control, goog.dom.getElement('foo')); |
| 274 |
|
| 275 |
// Since bar has all required classes, goog.dom.classlist.set() must not be |
| 276 |
// called at all. |
| 277 |
sandbox.innerHTML = '<div id="bar" class="goog-base goog-button">Bar' + |
| 278 |
'</div>'; |
| 279 |
testRenderer.decorate(control, goog.dom.getElement('bar')); |
| 280 |
|
| 281 |
// Since baz has all required classes, goog.dom.classlist.set() must not be |
| 282 |
// called at all. |
| 283 |
sandbox.innerHTML = '<div id="baz" class="goog-base goog-button ' + |
| 284 |
'goog-button-disabled">Baz</div>'; |
| 285 |
testRenderer.decorate(control, goog.dom.getElement('baz')); |
| 286 |
} |
| 287 |
|
| 288 |
function testInitializeDom() { |
| 289 |
var renderer = new goog.ui.ControlRenderer(); |
| 290 |
|
| 291 |
// Replace setRightToLeft(). |
| 292 |
renderer.setRightToLeft = function() { |
| 293 |
fail('setRightToLeft() must not be called'); |
| 294 |
}; |
| 295 |
|
| 296 |
// When a control with default render direction enters the document, |
| 297 |
// setRightToLeft() must not be called. |
| 298 |
control.setRenderer(renderer); |
| 299 |
control.render(sandbox); |
| 300 |
|
| 301 |
// When a control in the default state (enabled, visible, focusable) |
| 302 |
// enters the document, it must get a tab index. |
| 303 |
// Expected to fail on Mac Safari 3, because it doesn't support tab index. |
| 304 |
expectedFailures.expectFailureFor(isMacSafari3()); |
| 305 |
try { |
| 306 |
assertTrue( |
| 307 |
'Enabled, visible, focusable control must have tab index', |
| 308 |
goog.dom.isFocusableTabIndex(control.getElement())); |
| 309 |
} catch (e) { |
| 310 |
expectedFailures.handleException(e); |
| 311 |
} |
| 312 |
} |
| 313 |
|
| 314 |
function testInitializeDomDecorated() { |
| 315 |
var renderer = new goog.ui.ControlRenderer(); |
| 316 |
|
| 317 |
// Replace setRightToLeft(). |
| 318 |
renderer.setRightToLeft = function() { |
| 319 |
fail('setRightToLeft() must not be called'); |
| 320 |
}; |
| 321 |
|
| 322 |
sandbox.innerHTML = '<div id="foo" class="goog-control">Foo</div>'; |
| 323 |
|
| 324 |
// When a control with default render direction enters the document, |
| 325 |
// setRightToLeft() must not be called. |
| 326 |
control.setRenderer(renderer); |
| 327 |
control.decorate(goog.dom.getElement('foo')); |
| 328 |
|
| 329 |
// When a control in the default state (enabled, visible, focusable) |
| 330 |
// enters the document, it must get a tab index. |
| 331 |
// Expected to fail on Mac Safari 3, because it doesn't support tab index. |
| 332 |
expectedFailures.expectFailureFor(isMacSafari3()); |
| 333 |
try { |
| 334 |
assertTrue( |
| 335 |
'Enabled, visible, focusable control must have tab index', |
| 336 |
goog.dom.isFocusableTabIndex(control.getElement())); |
| 337 |
} catch (e) { |
| 338 |
expectedFailures.handleException(e); |
| 339 |
} |
| 340 |
} |
| 341 |
|
| 342 |
function testInitializeDomDisabledBiDi() { |
| 343 |
var renderer = new goog.ui.ControlRenderer(); |
| 344 |
|
| 345 |
// Replace setFocusable(). |
| 346 |
renderer.setFocusable = function() { |
| 347 |
fail('setFocusable() must not be called'); |
| 348 |
}; |
| 349 |
|
| 350 |
// When a disabled control enters the document, setFocusable() must not |
| 351 |
// be called. |
| 352 |
control.setEnabled(false); |
| 353 |
control.setRightToLeft(true); |
| 354 |
control.setRenderer(renderer); |
| 355 |
control.render(sandbox); |
| 356 |
|
| 357 |
// When a right-to-left control enters the document, special stying must |
| 358 |
// be applied. |
| 359 |
assertSameElements( |
| 360 |
'BiDi control must have right-to-left class', |
| 361 |
['goog-control', 'goog-control-disabled', 'goog-control-rtl'], |
| 362 |
goog.dom.classlist.get(control.getElement())); |
| 363 |
} |
| 364 |
|
| 365 |
function testInitializeDomDisabledBiDiDecorated() { |
| 366 |
var renderer = new goog.ui.ControlRenderer(); |
| 367 |
|
| 368 |
// Replace setFocusable(). |
| 369 |
renderer.setFocusable = function() { |
| 370 |
fail('setFocusable() must not be called'); |
| 371 |
}; |
| 372 |
|
| 373 |
sandbox.innerHTML = '<div dir="rtl">\n' + |
| 374 |
' <div id="foo" class="goog-control-disabled">Foo</div>\n' + |
| 375 |
'</div>\n'; |
| 376 |
|
| 377 |
// When a disabled control enters the document, setFocusable() must not |
| 378 |
// be called. |
| 379 |
control.setRenderer(renderer); |
| 380 |
control.decorate(goog.dom.getElement('foo')); |
| 381 |
|
| 382 |
// When a right-to-left control enters the document, special stying must |
| 383 |
// be applied. |
| 384 |
assertSameElements( |
| 385 |
'BiDi control must have right-to-left class', |
| 386 |
['goog-control', 'goog-control-disabled', 'goog-control-rtl'], |
| 387 |
goog.dom.classlist.get(control.getElement())); |
| 388 |
} |
| 389 |
|
| 390 |
function testSetAriaRole() { |
| 391 |
sandbox.innerHTML = '<div id="foo">Foo</div><div id="bar">Bar</div>'; |
| 392 |
|
| 393 |
var foo = goog.dom.getElement('foo'); |
| 394 |
assertNotNull(foo); |
| 395 |
controlRenderer.setAriaRole(foo); |
| 396 |
assertEvaluatesToFalse( |
| 397 |
'The role should be empty.', goog.a11y.aria.getRole(foo)); |
| 398 |
var bar = goog.dom.getElement('bar'); |
| 399 |
assertNotNull(bar); |
| 400 |
testRenderer.setAriaRole(bar); |
| 401 |
assertEquals( |
| 402 |
'Element must have expected ARIA role', goog.a11y.aria.Role.BUTTON, |
| 403 |
goog.a11y.aria.getRole(bar)); |
| 404 |
} |
| 405 |
|
| 406 |
function testSetAriaStatesHidden() { |
| 407 |
sandbox.innerHTML = '<div id="foo">Foo</div><div id="bar">Bar</div>'; |
| 408 |
var foo = goog.dom.getElement('foo'); |
| 409 |
|
| 410 |
control.setVisible(true); |
| 411 |
controlRenderer.setAriaStates(control, foo); |
| 412 |
|
| 413 |
assertEquals( |
| 414 |
'ControlRenderer did not set aria-hidden.', '', |
| 415 |
goog.a11y.aria.getState(foo, goog.a11y.aria.State.HIDDEN)); |
| 416 |
|
| 417 |
control.setVisible(false); |
| 418 |
controlRenderer.setAriaStates(control, foo); |
| 419 |
|
| 420 |
assertEquals( |
| 421 |
'ControlRenderer did not set aria-hidden.', 'true', |
| 422 |
goog.a11y.aria.getState(foo, goog.a11y.aria.State.HIDDEN)); |
| 423 |
} |
| 424 |
|
| 425 |
function testSetAriaStatesDisabled() { |
| 426 |
sandbox.innerHTML = '<div id="foo">Foo</div><div id="bar">Bar</div>'; |
| 427 |
var foo = goog.dom.getElement('foo'); |
| 428 |
|
| 429 |
control.setEnabled(true); |
| 430 |
controlRenderer.setAriaStates(control, foo); |
| 431 |
|
| 432 |
assertEquals( |
| 433 |
'ControlRenderer did not set aria-disabled.', '', |
| 434 |
goog.a11y.aria.getState(foo, goog.a11y.aria.State.DISABLED)); |
| 435 |
|
| 436 |
control.setEnabled(false); |
| 437 |
controlRenderer.setAriaStates(control, foo); |
| 438 |
|
| 439 |
assertEquals( |
| 440 |
'ControlRenderer did not set aria-disabled.', 'true', |
| 441 |
goog.a11y.aria.getState(foo, goog.a11y.aria.State.DISABLED)); |
| 442 |
} |
| 443 |
|
| 444 |
function testSetAriaStatesSelected() { |
| 445 |
sandbox.innerHTML = '<div id="foo">Foo</div><div id="bar">Bar</div>'; |
| 446 |
var foo = goog.dom.getElement('foo'); |
| 447 |
control.setSupportedState(goog.ui.Component.State.SELECTED, true); |
| 448 |
|
| 449 |
control.setSelected(true); |
| 450 |
controlRenderer.setAriaStates(control, foo); |
| 451 |
|
| 452 |
assertEquals( |
| 453 |
'ControlRenderer did not set aria-selected.', 'true', |
| 454 |
goog.a11y.aria.getState(foo, goog.a11y.aria.State.SELECTED)); |
| 455 |
|
| 456 |
control.setSelected(false); |
| 457 |
controlRenderer.setAriaStates(control, foo); |
| 458 |
|
| 459 |
assertEquals( |
| 460 |
'ControlRenderer did not set aria-selected.', 'false', |
| 461 |
goog.a11y.aria.getState(foo, goog.a11y.aria.State.SELECTED)); |
| 462 |
} |
| 463 |
|
| 464 |
function testSetAriaStatesChecked() { |
| 465 |
sandbox.innerHTML = '<div id="foo">Foo</div><div id="bar">Bar</div>'; |
| 466 |
var foo = goog.dom.getElement('foo'); |
| 467 |
control.setSupportedState(goog.ui.Component.State.CHECKED, true); |
| 468 |
|
| 469 |
control.setChecked(true); |
| 470 |
controlRenderer.setAriaStates(control, foo); |
| 471 |
|
| 472 |
assertEquals( |
| 473 |
'ControlRenderer did not set aria-checked.', 'true', |
| 474 |
goog.a11y.aria.getState(foo, goog.a11y.aria.State.CHECKED)); |
| 475 |
|
| 476 |
control.setChecked(false); |
| 477 |
controlRenderer.setAriaStates(control, foo); |
| 478 |
|
| 479 |
assertEquals( |
| 480 |
'ControlRenderer did not set aria-checked.', 'false', |
| 481 |
goog.a11y.aria.getState(foo, goog.a11y.aria.State.CHECKED)); |
| 482 |
} |
| 483 |
|
| 484 |
function testSetAriaStatesExpanded() { |
| 485 |
sandbox.innerHTML = '<div id="foo">Foo</div><div id="bar">Bar</div>'; |
| 486 |
var foo = goog.dom.getElement('foo'); |
| 487 |
control.setSupportedState(goog.ui.Component.State.OPENED, true); |
| 488 |
|
| 489 |
control.setOpen(true); |
| 490 |
controlRenderer.setAriaStates(control, foo); |
| 491 |
|
| 492 |
assertEquals( |
| 493 |
'ControlRenderer did not set aria-expanded.', 'true', |
| 494 |
goog.a11y.aria.getState(foo, goog.a11y.aria.State.EXPANDED)); |
| 495 |
|
| 496 |
control.setOpen(false); |
| 497 |
controlRenderer.setAriaStates(control, foo); |
| 498 |
|
| 499 |
assertEquals( |
| 500 |
'ControlRenderer did not set aria-expanded.', 'false', |
| 501 |
goog.a11y.aria.getState(foo, goog.a11y.aria.State.EXPANDED)); |
| 502 |
} |
| 503 |
|
| 504 |
function testSetAllowTextSelection() { |
| 505 |
sandbox.innerHTML = '<div id="foo"><span>Foo</span></div>'; |
| 506 |
var foo = goog.dom.getElement('foo'); |
| 507 |
|
| 508 |
controlRenderer.setAllowTextSelection(foo, false); |
| 509 |
assertTrue( |
| 510 |
'Parent element must be unselectable on all browsers', |
| 511 |
goog.style.isUnselectable(foo)); |
| 512 |
if (goog.userAgent.IE || goog.userAgent.OPERA) { |
| 513 |
assertTrue( |
| 514 |
'On IE and Opera, child element must also be unselectable', |
| 515 |
goog.style.isUnselectable(foo.firstChild)); |
| 516 |
} else { |
| 517 |
assertFalse( |
| 518 |
'On browsers other than IE and Opera, the child element ' + |
| 519 |
'must not be unselectable', |
| 520 |
goog.style.isUnselectable(foo.firstChild)); |
| 521 |
} |
| 522 |
|
| 523 |
controlRenderer.setAllowTextSelection(foo, true); |
| 524 |
assertFalse( |
| 525 |
'Parent element must be selectable', goog.style.isUnselectable(foo)); |
| 526 |
assertFalse( |
| 527 |
'Child element must be unselectable', |
| 528 |
goog.style.isUnselectable(foo.firstChild)); |
| 529 |
} |
| 530 |
|
| 531 |
function testSetRightToLeft() { |
| 532 |
sandbox.innerHTML = '<div id="foo">Foo</div><div id="bar">Bar</div>'; |
| 533 |
|
| 534 |
var foo = goog.dom.getElement('foo'); |
| 535 |
controlRenderer.setRightToLeft(foo, true); |
| 536 |
assertSameElements( |
| 537 |
'Element must have right-to-left class applied', ['goog-control-rtl'], |
| 538 |
goog.dom.classlist.get(foo)); |
| 539 |
controlRenderer.setRightToLeft(foo, false); |
| 540 |
assertSameElements( |
| 541 |
'Element must not have right-to-left class applied', [], |
| 542 |
goog.dom.classlist.get(foo)); |
| 543 |
|
| 544 |
var bar = goog.dom.getElement('bar'); |
| 545 |
testRenderer.setRightToLeft(bar, true); |
| 546 |
assertSameElements( |
| 547 |
'Element must have right-to-left class applied', ['goog-base-rtl'], |
| 548 |
goog.dom.classlist.get(bar)); |
| 549 |
testRenderer.setRightToLeft(bar, false); |
| 550 |
assertSameElements( |
| 551 |
'Element must not have right-to-left class applied', [], |
| 552 |
goog.dom.classlist.get(bar)); |
| 553 |
} |
| 554 |
|
| 555 |
function testIsFocusable() { |
| 556 |
control.render(sandbox); |
| 557 |
// Expected to fail on Mac Safari 3, because it doesn't support tab index. |
| 558 |
expectedFailures.expectFailureFor(isMacSafari3()); |
| 559 |
try { |
| 560 |
assertTrue( |
| 561 |
'Control\'s key event target must be focusable', |
| 562 |
controlRenderer.isFocusable(control)); |
| 563 |
} catch (e) { |
| 564 |
expectedFailures.handleException(e); |
| 565 |
} |
| 566 |
} |
| 567 |
|
| 568 |
function testIsFocusableForNonFocusableControl() { |
| 569 |
control.setSupportedState(goog.ui.Component.State.FOCUSED, false); |
| 570 |
control.render(sandbox); |
| 571 |
assertFalse( |
| 572 |
'Non-focusable control\'s key event target must not be ' + |
| 573 |
'focusable', |
| 574 |
controlRenderer.isFocusable(control)); |
| 575 |
} |
| 576 |
|
| 577 |
function testIsFocusableForControlWithoutKeyEventTarget() { |
| 578 |
// Unrendered control has no key event target. |
| 579 |
assertNull( |
| 580 |
'Unrendered control must not have key event target', |
| 581 |
control.getKeyEventTarget()); |
| 582 |
assertFalse( |
| 583 |
'isFocusable() must return null if no key event target', |
| 584 |
controlRenderer.isFocusable(control)); |
| 585 |
} |
| 586 |
|
| 587 |
function testSetFocusable() { |
| 588 |
control.render(sandbox); |
| 589 |
controlRenderer.setFocusable(control, false); |
| 590 |
assertFalse( |
| 591 |
'Control\'s key event target must not have tab index', |
| 592 |
goog.dom.isFocusableTabIndex(control.getKeyEventTarget())); |
| 593 |
controlRenderer.setFocusable(control, true); |
| 594 |
// Expected to fail on Mac Safari 3, because it doesn't support tab index. |
| 595 |
expectedFailures.expectFailureFor(isMacSafari3()); |
| 596 |
try { |
| 597 |
assertTrue( |
| 598 |
'Control\'s key event target must have focusable tab index', |
| 599 |
goog.dom.isFocusableTabIndex(control.getKeyEventTarget())); |
| 600 |
} catch (e) { |
| 601 |
expectedFailures.handleException(e); |
| 602 |
} |
| 603 |
} |
| 604 |
|
| 605 |
function testSetFocusableForNonFocusableControl() { |
| 606 |
control.setSupportedState(goog.ui.Component.State.FOCUSED, false); |
| 607 |
control.render(sandbox); |
| 608 |
assertFalse( |
| 609 |
'Non-focusable control\'s key event target must not be ' + |
| 610 |
'focusable', |
| 611 |
goog.dom.isFocusableTabIndex(control.getKeyEventTarget())); |
| 612 |
controlRenderer.setFocusable(control, true); |
| 613 |
assertFalse( |
| 614 |
'Non-focusable control\'s key event target must not be ' + |
| 615 |
'focusable, even after calling setFocusable(true)', |
| 616 |
goog.dom.isFocusableTabIndex(control.getKeyEventTarget())); |
| 617 |
} |
| 618 |
|
| 619 |
function testSetVisible() { |
| 620 |
sandbox.innerHTML = '<div id="foo">Foo</div>'; |
| 621 |
var foo = goog.dom.getElement('foo'); |
| 622 |
assertTrue('Element must be visible', foo.style.display != 'none'); |
| 623 |
controlRenderer.setVisible(foo, true); |
| 624 |
assertEquals( |
| 625 |
'ControlRenderer did not set aria-hidden.', 'false', |
| 626 |
goog.a11y.aria.getState(foo, goog.a11y.aria.State.HIDDEN)); |
| 627 |
assertTrue('Element must still be visible', foo.style.display != 'none'); |
| 628 |
controlRenderer.setVisible(foo, false); |
| 629 |
assertEquals( |
| 630 |
'ControlRenderer did not set aria-hidden.', 'true', |
| 631 |
goog.a11y.aria.getState(foo, goog.a11y.aria.State.HIDDEN)); |
| 632 |
assertTrue('Element must be hidden', foo.style.display == 'none'); |
| 633 |
} |
| 634 |
|
| 635 |
function testSetState() { |
| 636 |
control.setRenderer(testRenderer); |
| 637 |
control.createDom(); |
| 638 |
var element = control.getElement(); |
| 639 |
assertNotNull(element); |
| 640 |
assertSameElements( |
| 641 |
'Control must have expected class names', ['goog-button', 'goog-base'], |
| 642 |
goog.dom.classlist.get(element)); |
| 643 |
assertEquals( |
| 644 |
'Control must not have disabled ARIA state', '', |
| 645 |
goog.a11y.aria.getState(element, goog.a11y.aria.State.DISABLED)); |
| 646 |
|
| 647 |
testRenderer.setState(control, goog.ui.Component.State.DISABLED, true); |
| 648 |
assertSameElements( |
| 649 |
'Control must have disabled class name', |
| 650 |
['goog-button', 'goog-base', 'goog-base-disabled'], |
| 651 |
goog.dom.classlist.get(element)); |
| 652 |
assertEquals( |
| 653 |
'Control must have disabled ARIA state', 'true', |
| 654 |
goog.a11y.aria.getState(element, goog.a11y.aria.State.DISABLED)); |
| 655 |
|
| 656 |
testRenderer.setState(control, goog.ui.Component.State.DISABLED, false); |
| 657 |
assertSameElements( |
| 658 |
'Control must no longer have disabled class name', |
| 659 |
['goog-button', 'goog-base'], goog.dom.classlist.get(element)); |
| 660 |
assertEquals( |
| 661 |
'Control must not have disabled ARIA state', 'false', |
| 662 |
goog.a11y.aria.getState(element, goog.a11y.aria.State.DISABLED)); |
| 663 |
|
| 664 |
testRenderer.setState(control, 0xFFFFFF, true); |
| 665 |
assertSameElements( |
| 666 |
'Class names must be unchanged for invalid state', |
| 667 |
['goog-button', 'goog-base'], goog.dom.classlist.get(element)); |
| 668 |
} |
| 669 |
|
| 670 |
function testUpdateAriaStateDisabled() { |
| 671 |
control.createDom(); |
| 672 |
var element = control.getElement(); |
| 673 |
assertNotNull(element); |
| 674 |
controlRenderer.updateAriaState( |
| 675 |
element, goog.ui.Component.State.DISABLED, true); |
| 676 |
assertEquals( |
| 677 |
'Control must have disabled ARIA state', 'true', |
| 678 |
goog.a11y.aria.getState(element, goog.a11y.aria.State.DISABLED)); |
| 679 |
|
| 680 |
controlRenderer.updateAriaState( |
| 681 |
element, goog.ui.Component.State.DISABLED, false); |
| 682 |
assertEquals( |
| 683 |
'Control must no longer have disabled ARIA state', 'false', |
| 684 |
goog.a11y.aria.getState(element, goog.a11y.aria.State.DISABLED)); |
| 685 |
} |
| 686 |
|
| 687 |
function testSetAriaStatesRender_ariaStateDisabled() { |
| 688 |
control.setEnabled(false); |
| 689 |
var renderer = new goog.ui.ControlRenderer(); |
| 690 |
control.setRenderer(renderer); |
| 691 |
control.render(sandbox); |
| 692 |
var element = control.getElement(); |
| 693 |
assertNotNull(element); |
| 694 |
assertFalse('Control must be disabled', control.isEnabled()); |
| 695 |
assertEquals( |
| 696 |
'Control must have disabled ARIA state', 'true', |
| 697 |
goog.a11y.aria.getState(element, goog.a11y.aria.State.DISABLED)); |
| 698 |
} |
| 699 |
|
| 700 |
function testSetAriaStatesDecorate_ariaStateDisabled() { |
| 701 |
sandbox.innerHTML = '<div id="foo" class="app goog-base-disabled"></div>'; |
| 702 |
var element = goog.dom.getElement('foo'); |
| 703 |
|
| 704 |
control.setRenderer(testRenderer); |
| 705 |
control.decorate(element); |
| 706 |
assertNotNull(element); |
| 707 |
assertFalse('Control must be disabled', control.isEnabled()); |
| 708 |
assertEquals( |
| 709 |
'Control must have disabled ARIA state', 'true', |
| 710 |
goog.a11y.aria.getState(element, goog.a11y.aria.State.DISABLED)); |
| 711 |
} |
| 712 |
|
| 713 |
function testUpdateAriaStateSelected() { |
| 714 |
control.createDom(); |
| 715 |
var element = control.getElement(); |
| 716 |
assertNotNull(element); |
| 717 |
controlRenderer.updateAriaState( |
| 718 |
element, goog.ui.Component.State.SELECTED, true); |
| 719 |
assertEquals( |
| 720 |
'Control must have selected ARIA state', 'true', |
| 721 |
goog.a11y.aria.getState(element, goog.a11y.aria.State.SELECTED)); |
| 722 |
|
| 723 |
controlRenderer.updateAriaState( |
| 724 |
element, goog.ui.Component.State.SELECTED, false); |
| 725 |
assertEquals( |
| 726 |
'Control must no longer have selected ARIA state', 'false', |
| 727 |
goog.a11y.aria.getState(element, goog.a11y.aria.State.SELECTED)); |
| 728 |
} |
| 729 |
|
| 730 |
function testSetAriaStatesRender_ariaStateSelected() { |
| 731 |
control.setSupportedState(goog.ui.Component.State.SELECTED, true); |
| 732 |
control.setSelected(true); |
| 733 |
|
| 734 |
var renderer = new goog.ui.ControlRenderer(); |
| 735 |
control.setRenderer(renderer); |
| 736 |
control.render(sandbox); |
| 737 |
var element = control.getElement(); |
| 738 |
assertNotNull(element); |
| 739 |
assertTrue('Control must be selected', control.isSelected()); |
| 740 |
assertEquals( |
| 741 |
'Control must have selected ARIA state', 'true', |
| 742 |
goog.a11y.aria.getState(element, goog.a11y.aria.State.SELECTED)); |
| 743 |
} |
| 744 |
|
| 745 |
function testSetAriaStatesRender_ariaStateNotSelected() { |
| 746 |
control.setSupportedState(goog.ui.Component.State.SELECTED, true); |
| 747 |
|
| 748 |
var renderer = new goog.ui.ControlRenderer(); |
| 749 |
control.setRenderer(renderer); |
| 750 |
control.render(sandbox); |
| 751 |
var element = control.getElement(); |
| 752 |
assertNotNull(element); |
| 753 |
assertFalse('Control must not be selected', control.isSelected()); |
| 754 |
assertEquals( |
| 755 |
'Control must have not-selected ARIA state', 'false', |
| 756 |
goog.a11y.aria.getState(element, goog.a11y.aria.State.SELECTED)); |
| 757 |
} |
| 758 |
|
| 759 |
function testSetAriaStatesDecorate_ariaStateSelected() { |
| 760 |
control.setSupportedState(goog.ui.Component.State.SELECTED, true); |
| 761 |
|
| 762 |
sandbox.innerHTML = '<div id="foo" class="app goog-control-selected"></div>'; |
| 763 |
var element = goog.dom.getElement('foo'); |
| 764 |
|
| 765 |
control.setRenderer(controlRenderer); |
| 766 |
control.decorate(element); |
| 767 |
assertNotNull(element); |
| 768 |
assertTrue('Control must be selected', control.isSelected()); |
| 769 |
assertEquals( |
| 770 |
'Control must have selected ARIA state', 'true', |
| 771 |
goog.a11y.aria.getState(element, goog.a11y.aria.State.SELECTED)); |
| 772 |
} |
| 773 |
|
| 774 |
function testUpdateAriaStateChecked() { |
| 775 |
control.createDom(); |
| 776 |
var element = control.getElement(); |
| 777 |
assertNotNull(element); |
| 778 |
controlRenderer.updateAriaState( |
| 779 |
element, goog.ui.Component.State.CHECKED, true); |
| 780 |
assertEquals( |
| 781 |
'Control must have checked ARIA state', 'true', |
| 782 |
goog.a11y.aria.getState(element, goog.a11y.aria.State.CHECKED)); |
| 783 |
|
| 784 |
controlRenderer.updateAriaState( |
| 785 |
element, goog.ui.Component.State.CHECKED, false); |
| 786 |
assertEquals( |
| 787 |
'Control must no longer have checked ARIA state', 'false', |
| 788 |
goog.a11y.aria.getState(element, goog.a11y.aria.State.CHECKED)); |
| 789 |
} |
| 790 |
|
| 791 |
function testSetAriaStatesRender_ariaStateChecked() { |
| 792 |
control.setSupportedState(goog.ui.Component.State.CHECKED, true); |
| 793 |
control.setChecked(true); |
| 794 |
|
| 795 |
var renderer = new goog.ui.ControlRenderer(); |
| 796 |
control.setRenderer(renderer); |
| 797 |
control.render(sandbox); |
| 798 |
var element = control.getElement(); |
| 799 |
assertNotNull(element); |
| 800 |
assertTrue('Control must be checked', control.isChecked()); |
| 801 |
assertEquals( |
| 802 |
'Control must have checked ARIA state', 'true', |
| 803 |
goog.a11y.aria.getState(element, goog.a11y.aria.State.CHECKED)); |
| 804 |
} |
| 805 |
|
| 806 |
function testSetAriaStatesDecorate_ariaStateChecked() { |
| 807 |
sandbox.innerHTML = '<div id="foo" class="app goog-control-checked"></div>'; |
| 808 |
var element = goog.dom.getElement('foo'); |
| 809 |
|
| 810 |
control.setSupportedState(goog.ui.Component.State.CHECKED, true); |
| 811 |
control.decorate(element); |
| 812 |
assertNotNull(element); |
| 813 |
assertTrue('Control must be checked', control.isChecked()); |
| 814 |
assertEquals( |
| 815 |
'Control must have checked ARIA state', 'true', |
| 816 |
goog.a11y.aria.getState(element, goog.a11y.aria.State.CHECKED)); |
| 817 |
} |
| 818 |
|
| 819 |
function testUpdateAriaStateOpened() { |
| 820 |
control.createDom(); |
| 821 |
var element = control.getElement(); |
| 822 |
assertNotNull(element); |
| 823 |
controlRenderer.updateAriaState( |
| 824 |
element, goog.ui.Component.State.OPENED, true); |
| 825 |
assertEquals( |
| 826 |
'Control must have expanded ARIA state', 'true', |
| 827 |
goog.a11y.aria.getState(element, goog.a11y.aria.State.EXPANDED)); |
| 828 |
|
| 829 |
controlRenderer.updateAriaState( |
| 830 |
element, goog.ui.Component.State.OPENED, false); |
| 831 |
assertEquals( |
| 832 |
'Control must no longer have expanded ARIA state', 'false', |
| 833 |
goog.a11y.aria.getState(element, goog.a11y.aria.State.EXPANDED)); |
| 834 |
} |
| 835 |
|
| 836 |
function testSetAriaStatesRender_ariaStateOpened() { |
| 837 |
control.setSupportedState(goog.ui.Component.State.OPENED, true); |
| 838 |
control.setOpen(true); |
| 839 |
|
| 840 |
var renderer = new goog.ui.ControlRenderer(); |
| 841 |
control.setRenderer(renderer); |
| 842 |
control.render(sandbox); |
| 843 |
var element = control.getElement(); |
| 844 |
assertNotNull(element); |
| 845 |
assertTrue('Control must be opened', control.isOpen()); |
| 846 |
assertEquals( |
| 847 |
'Control must have expanded ARIA state', 'true', |
| 848 |
goog.a11y.aria.getState(element, goog.a11y.aria.State.EXPANDED)); |
| 849 |
} |
| 850 |
|
| 851 |
function testSetAriaStatesDecorate_ariaStateOpened() { |
| 852 |
sandbox.innerHTML = '<div id="foo" class="app goog-base-open"></div>'; |
| 853 |
var element = goog.dom.getElement('foo'); |
| 854 |
|
| 855 |
control.setSupportedState(goog.ui.Component.State.OPENED, true); |
| 856 |
control.setRenderer(testRenderer); |
| 857 |
control.decorate(element); |
| 858 |
assertNotNull(element); |
| 859 |
assertTrue('Control must be opened', control.isOpen()); |
| 860 |
assertEquals( |
| 861 |
'Control must have expanded ARIA state', 'true', |
| 862 |
goog.a11y.aria.getState(element, goog.a11y.aria.State.EXPANDED)); |
| 863 |
} |
| 864 |
|
| 865 |
function testSetAriaStateRoleNotInMap() { |
| 866 |
sandbox.innerHTML = '<div id="foo" role="option">Hello, world!</div>'; |
| 867 |
control.setRenderer(controlRenderer); |
| 868 |
control.setSupportedState(goog.ui.Component.State.CHECKED, true); |
| 869 |
var element = goog.dom.getElement('foo'); |
| 870 |
control.decorate(element); |
| 871 |
assertEquals( |
| 872 |
'Element should have ARIA role option.', goog.a11y.aria.Role.OPTION, |
| 873 |
goog.a11y.aria.getRole(element)); |
| 874 |
control.setStateInternal(goog.ui.Component.State.DISABLED, true); |
| 875 |
controlRenderer.setAriaStates(control, element); |
| 876 |
assertEquals( |
| 877 |
'Element should have aria-disabled true', 'true', |
| 878 |
goog.a11y.aria.getState(element, goog.a11y.aria.State.DISABLED)); |
| 879 |
control.setStateInternal(goog.ui.Component.State.CHECKED, true); |
| 880 |
controlRenderer.setAriaStates(control, element); |
| 881 |
assertEquals( |
| 882 |
'Element should have aria-checked true', 'true', |
| 883 |
goog.a11y.aria.getState(element, goog.a11y.aria.State.CHECKED)); |
| 884 |
} |
| 885 |
|
| 886 |
function testSetAriaStateRoleInMapMatches() { |
| 887 |
sandbox.innerHTML = '<div id="foo" role="checkbox">Hello, world!</div>'; |
| 888 |
control.setRenderer(controlRenderer); |
| 889 |
control.setSupportedState(goog.ui.Component.State.CHECKED, true); |
| 890 |
var element = goog.dom.getElement('foo'); |
| 891 |
control.decorate(element); |
| 892 |
assertEquals( |
| 893 |
'Element should have ARIA role checkbox.', goog.a11y.aria.Role.CHECKBOX, |
| 894 |
goog.a11y.aria.getRole(element)); |
| 895 |
control.setStateInternal(goog.ui.Component.State.DISABLED, true); |
| 896 |
controlRenderer.setAriaStates(control, element); |
| 897 |
assertEquals( |
| 898 |
'Element should have aria-disabled true', 'true', |
| 899 |
goog.a11y.aria.getState(element, goog.a11y.aria.State.DISABLED)); |
| 900 |
control.setStateInternal(goog.ui.Component.State.CHECKED, true); |
| 901 |
controlRenderer.setAriaStates(control, element); |
| 902 |
assertEquals( |
| 903 |
'Element should have aria-checked true', 'true', |
| 904 |
goog.a11y.aria.getState(element, goog.a11y.aria.State.CHECKED)); |
| 905 |
} |
| 906 |
|
| 907 |
function testSetAriaStateRoleInMapNotMatches() { |
| 908 |
sandbox.innerHTML = '<div id="foo" role="button">Hello, world!</div>'; |
| 909 |
control.setRenderer(controlRenderer); |
| 910 |
control.setSupportedState(goog.ui.Component.State.CHECKED, true); |
| 911 |
var element = goog.dom.getElement('foo'); |
| 912 |
control.decorate(element); |
| 913 |
assertEquals( |
| 914 |
'Element should have ARIA role button.', goog.a11y.aria.Role.BUTTON, |
| 915 |
goog.a11y.aria.getRole(element)); |
| 916 |
control.setStateInternal(goog.ui.Component.State.DISABLED, true); |
| 917 |
controlRenderer.setAriaStates(control, element); |
| 918 |
assertEquals( |
| 919 |
'Element should have aria-disabled true', 'true', |
| 920 |
goog.a11y.aria.getState(element, goog.a11y.aria.State.DISABLED)); |
| 921 |
control.setStateInternal(goog.ui.Component.State.CHECKED, true); |
| 922 |
controlRenderer.setAriaStates(control, element); |
| 923 |
assertEquals( |
| 924 |
'Element should have aria-pressed true', 'true', |
| 925 |
goog.a11y.aria.getState(element, goog.a11y.aria.State.PRESSED)); |
| 926 |
assertEquals( |
| 927 |
'Element should not have aria-checked', '', |
| 928 |
goog.a11y.aria.getState(element, goog.a11y.aria.State.CHECKED)); |
| 929 |
} |
| 930 |
|
| 931 |
function testToggleAriaStateMap() { |
| 932 |
var map = goog.object.create( |
| 933 |
goog.a11y.aria.Role.BUTTON, goog.a11y.aria.State.PRESSED, |
| 934 |
goog.a11y.aria.Role.CHECKBOX, goog.a11y.aria.State.CHECKED, |
| 935 |
goog.a11y.aria.Role.MENU_ITEM, goog.a11y.aria.State.SELECTED, |
| 936 |
goog.a11y.aria.Role.MENU_ITEM_CHECKBOX, goog.a11y.aria.State.CHECKED, |
| 937 |
goog.a11y.aria.Role.MENU_ITEM_RADIO, goog.a11y.aria.State.CHECKED, |
| 938 |
goog.a11y.aria.Role.RADIO, goog.a11y.aria.State.CHECKED, |
| 939 |
goog.a11y.aria.Role.TAB, goog.a11y.aria.State.SELECTED, |
| 940 |
goog.a11y.aria.Role.TREEITEM, goog.a11y.aria.State.SELECTED); |
| 941 |
for (var key in map) { |
| 942 |
assertTrue( |
| 943 |
'Toggle ARIA state map incorrect.', |
| 944 |
key in goog.ui.ControlRenderer.TOGGLE_ARIA_STATE_MAP_); |
| 945 |
assertEquals( |
| 946 |
'Toggle ARIA state map incorrect.', map[key], |
| 947 |
goog.ui.ControlRenderer.TOGGLE_ARIA_STATE_MAP_[key]); |
| 948 |
} |
| 949 |
} |
| 950 |
function testSetContent() { |
| 951 |
goog.dom.setTextContent(sandbox, 'Hello, world!'); |
| 952 |
controlRenderer.setContent(sandbox, 'Not so fast!'); |
| 953 |
assertEquals( |
| 954 |
'Element must contain expected text value', 'Not so fast!', |
| 955 |
goog.dom.getTextContent(sandbox)); |
| 956 |
} |
| 957 |
|
| 958 |
function testSetContentNull() { |
| 959 |
goog.dom.setTextContent(sandbox, 'Hello, world!'); |
| 960 |
controlRenderer.setContent(sandbox, null); |
| 961 |
assertEquals( |
| 962 |
'Element must have no child nodes', 0, sandbox.childNodes.length); |
| 963 |
assertEquals( |
| 964 |
'Element must contain expected text value', '', |
| 965 |
goog.dom.getTextContent(sandbox)); |
| 966 |
} |
| 967 |
|
| 968 |
function testSetContentEmpty() { |
| 969 |
goog.dom.setTextContent(sandbox, 'Hello, world!'); |
| 970 |
controlRenderer.setContent(sandbox, ''); |
| 971 |
assertEquals('Element must not have children', 0, sandbox.childNodes.length); |
| 972 |
assertEquals( |
| 973 |
'Element must contain expected text value', '', |
| 974 |
goog.dom.getTextContent(sandbox)); |
| 975 |
} |
| 976 |
|
| 977 |
function testSetContentWhitespace() { |
| 978 |
goog.dom.setTextContent(sandbox, 'Hello, world!'); |
| 979 |
controlRenderer.setContent(sandbox, ' '); |
| 980 |
assertEquals('Element must have one child', 1, sandbox.childNodes.length); |
| 981 |
assertEquals( |
| 982 |
'Child must be a text node', goog.dom.NodeType.TEXT, |
| 983 |
sandbox.firstChild.nodeType); |
| 984 |
assertEquals( |
| 985 |
'Element must contain expected text value', ' ', |
| 986 |
goog.dom.getTextContent(sandbox)); |
| 987 |
} |
| 988 |
|
| 989 |
function testSetContentTextNode() { |
| 990 |
goog.dom.setTextContent(sandbox, 'Hello, world!'); |
| 991 |
controlRenderer.setContent(sandbox, document.createTextNode('Text')); |
| 992 |
assertEquals('Element must have one child', 1, sandbox.childNodes.length); |
| 993 |
assertEquals( |
| 994 |
'Child must be a text node', goog.dom.NodeType.TEXT, |
| 995 |
sandbox.firstChild.nodeType); |
| 996 |
assertEquals( |
| 997 |
'Element must contain expected text value', 'Text', |
| 998 |
goog.dom.getTextContent(sandbox)); |
| 999 |
} |
| 1000 |
|
| 1001 |
function testSetContentElementNode() { |
| 1002 |
goog.dom.setTextContent(sandbox, 'Hello, world!'); |
| 1003 |
controlRenderer.setContent( |
| 1004 |
sandbox, goog.dom.createDom(goog.dom.TagName.DIV, {id: 'foo'}, 'Foo')); |
| 1005 |
assertEquals('Element must have one child', 1, sandbox.childNodes.length); |
| 1006 |
assertEquals( |
| 1007 |
'Child must be an element node', goog.dom.NodeType.ELEMENT, |
| 1008 |
sandbox.firstChild.nodeType); |
| 1009 |
assertHTMLEquals( |
| 1010 |
'Element must contain expected HTML', '<div id="foo">Foo</div>', |
| 1011 |
sandbox.innerHTML); |
| 1012 |
} |
| 1013 |
|
| 1014 |
function testSetContentArray() { |
| 1015 |
goog.dom.setTextContent(sandbox, 'Hello, world!'); |
| 1016 |
controlRenderer.setContent( |
| 1017 |
sandbox, |
| 1018 |
['Hello, ', goog.dom.createDom(goog.dom.TagName.B, null, 'world'), '!']); |
| 1019 |
assertEquals( |
| 1020 |
'Element must have three children', 3, sandbox.childNodes.length); |
| 1021 |
assertEquals( |
| 1022 |
'1st child must be a text node', goog.dom.NodeType.TEXT, |
| 1023 |
sandbox.childNodes[0].nodeType); |
| 1024 |
assertEquals( |
| 1025 |
'2nd child must be an element', goog.dom.NodeType.ELEMENT, |
| 1026 |
sandbox.childNodes[1].nodeType); |
| 1027 |
assertEquals( |
| 1028 |
'3rd child must be a text node', goog.dom.NodeType.TEXT, |
| 1029 |
sandbox.childNodes[2].nodeType); |
| 1030 |
assertHTMLEquals( |
| 1031 |
'Element must contain expected HTML', 'Hello, <b>world</b>!', |
| 1032 |
sandbox.innerHTML); |
| 1033 |
} |
| 1034 |
|
| 1035 |
function testSetContentNodeList() { |
| 1036 |
goog.dom.setTextContent(sandbox, 'Hello, world!'); |
| 1037 |
var div = goog.dom.createDom( |
| 1038 |
goog.dom.TagName.DIV, null, 'Hello, ', |
| 1039 |
goog.dom.createDom(goog.dom.TagName.B, null, 'world'), '!'); |
| 1040 |
controlRenderer.setContent(sandbox, div.childNodes); |
| 1041 |
assertEquals( |
| 1042 |
'Element must have three children', 3, sandbox.childNodes.length); |
| 1043 |
assertEquals( |
| 1044 |
'1st child must be a text node', goog.dom.NodeType.TEXT, |
| 1045 |
sandbox.childNodes[0].nodeType); |
| 1046 |
assertEquals( |
| 1047 |
'2nd child must be an element', goog.dom.NodeType.ELEMENT, |
| 1048 |
sandbox.childNodes[1].nodeType); |
| 1049 |
assertEquals( |
| 1050 |
'3rd child must be a text node', goog.dom.NodeType.TEXT, |
| 1051 |
sandbox.childNodes[2].nodeType); |
| 1052 |
assertHTMLEquals( |
| 1053 |
'Element must contain expected HTML', 'Hello, <b>world</b>!', |
| 1054 |
sandbox.innerHTML); |
| 1055 |
} |
| 1056 |
|
| 1057 |
function testGetKeyEventTarget() { |
| 1058 |
assertNull( |
| 1059 |
'Key event target for unrendered control must be null', |
| 1060 |
controlRenderer.getKeyEventTarget(control)); |
| 1061 |
control.createDom(); |
| 1062 |
assertEquals( |
| 1063 |
'Key event target for rendered control must be its element', |
| 1064 |
control.getElement(), controlRenderer.getKeyEventTarget(control)); |
| 1065 |
} |
| 1066 |
|
| 1067 |
function testGetCssClass() { |
| 1068 |
assertEquals( |
| 1069 |
'ControlRenderer\'s CSS class must have expected value', |
| 1070 |
goog.ui.ControlRenderer.CSS_CLASS, controlRenderer.getCssClass()); |
| 1071 |
assertEquals( |
| 1072 |
'TestRenderer\'s CSS class must have expected value', |
| 1073 |
TestRenderer.CSS_CLASS, testRenderer.getCssClass()); |
| 1074 |
} |
| 1075 |
|
| 1076 |
function testGetStructuralCssClass() { |
| 1077 |
assertEquals( |
| 1078 |
'ControlRenderer\'s structural class must be its CSS class', |
| 1079 |
controlRenderer.getCssClass(), controlRenderer.getStructuralCssClass()); |
| 1080 |
assertEquals( |
| 1081 |
'TestRenderer\'s structural class must have expected value', 'goog-base', |
| 1082 |
testRenderer.getStructuralCssClass()); |
| 1083 |
} |
| 1084 |
|
| 1085 |
function testGetClassNames() { |
| 1086 |
// These tests use assertArrayEquals, because the order is significant. |
| 1087 |
assertArrayEquals( |
| 1088 |
'ControlRenderer must return expected class names ' + |
| 1089 |
'in the expected order', |
| 1090 |
['goog-control'], controlRenderer.getClassNames(control)); |
| 1091 |
assertArrayEquals( |
| 1092 |
'TestRenderer must return expected class names ' + |
| 1093 |
'in the expected order', |
| 1094 |
['goog-button', 'goog-base'], testRenderer.getClassNames(control)); |
| 1095 |
} |
| 1096 |
|
| 1097 |
function testGetClassNamesForControlWithState() { |
| 1098 |
control.setStateInternal( |
| 1099 |
goog.ui.Component.State.HOVER | goog.ui.Component.State.ACTIVE); |
| 1100 |
|
| 1101 |
// These tests use assertArrayEquals, because the order is significant. |
| 1102 |
assertArrayEquals( |
| 1103 |
'ControlRenderer must return expected class names ' + |
| 1104 |
'in the expected order', |
| 1105 |
['goog-control', 'goog-control-hover', 'goog-control-active'], |
| 1106 |
controlRenderer.getClassNames(control)); |
| 1107 |
assertArrayEquals( |
| 1108 |
'TestRenderer must return expected class names ' + |
| 1109 |
'in the expected order', |
| 1110 |
['goog-button', 'goog-base', 'goog-base-hover', 'goog-base-active'], |
| 1111 |
testRenderer.getClassNames(control)); |
| 1112 |
} |
| 1113 |
|
| 1114 |
function testGetClassNamesForControlWithExtraClassNames() { |
| 1115 |
control.addClassName('foo'); |
| 1116 |
control.addClassName('bar'); |
| 1117 |
|
| 1118 |
// These tests use assertArrayEquals, because the order is significant. |
| 1119 |
assertArrayEquals( |
| 1120 |
'ControlRenderer must return expected class names ' + |
| 1121 |
'in the expected order', |
| 1122 |
['goog-control', 'foo', 'bar'], controlRenderer.getClassNames(control)); |
| 1123 |
assertArrayEquals( |
| 1124 |
'TestRenderer must return expected class names ' + |
| 1125 |
'in the expected order', |
| 1126 |
['goog-button', 'goog-base', 'foo', 'bar'], |
| 1127 |
testRenderer.getClassNames(control)); |
| 1128 |
} |
| 1129 |
|
| 1130 |
function testGetClassNamesForControlWithStateAndExtraClassNames() { |
| 1131 |
control.setStateInternal( |
| 1132 |
goog.ui.Component.State.HOVER | goog.ui.Component.State.ACTIVE); |
| 1133 |
control.addClassName('foo'); |
| 1134 |
control.addClassName('bar'); |
| 1135 |
|
| 1136 |
// These tests use assertArrayEquals, because the order is significant. |
| 1137 |
assertArrayEquals( |
| 1138 |
'ControlRenderer must return expected class names ' + |
| 1139 |
'in the expected order', |
| 1140 |
[ |
| 1141 |
'goog-control', 'goog-control-hover', 'goog-control-active', 'foo', |
| 1142 |
'bar' |
| 1143 |
], |
| 1144 |
controlRenderer.getClassNames(control)); |
| 1145 |
assertArrayEquals( |
| 1146 |
'TestRenderer must return expected class names ' + |
| 1147 |
'in the expected order', |
| 1148 |
[ |
| 1149 |
'goog-button', 'goog-base', 'goog-base-hover', 'goog-base-active', |
| 1150 |
'foo', 'bar' |
| 1151 |
], |
| 1152 |
testRenderer.getClassNames(control)); |
| 1153 |
} |
| 1154 |
|
| 1155 |
function testGetClassNamesForState() { |
| 1156 |
// These tests use assertArrayEquals, because the order is significant. |
| 1157 |
assertArrayEquals( |
| 1158 |
'ControlRenderer must return expected class names ' + |
| 1159 |
'in the expected order', |
| 1160 |
['goog-control-hover', 'goog-control-checked'], |
| 1161 |
controlRenderer.getClassNamesForState( |
| 1162 |
goog.ui.Component.State.HOVER | goog.ui.Component.State.CHECKED)); |
| 1163 |
assertArrayEquals( |
| 1164 |
'TestRenderer must return expected class names ' + |
| 1165 |
'in the expected order', |
| 1166 |
['goog-base-hover', 'goog-base-checked'], |
| 1167 |
testRenderer.getClassNamesForState( |
| 1168 |
goog.ui.Component.State.HOVER | goog.ui.Component.State.CHECKED)); |
| 1169 |
} |
| 1170 |
|
| 1171 |
function testGetClassForState() { |
| 1172 |
var renderer = new goog.ui.ControlRenderer(); |
| 1173 |
assertUndefined( |
| 1174 |
'State-to-class map must not exist until first use', |
| 1175 |
renderer.classByState_); |
| 1176 |
assertEquals( |
| 1177 |
'Renderer must return expected class name for SELECTED', |
| 1178 |
'goog-control-selected', |
| 1179 |
renderer.getClassForState(goog.ui.Component.State.SELECTED)); |
| 1180 |
assertUndefined( |
| 1181 |
'Renderer must return undefined for invalid state', |
| 1182 |
renderer.getClassForState('foo')); |
| 1183 |
} |
| 1184 |
|
| 1185 |
function testGetStateFromClass() { |
| 1186 |
var renderer = new goog.ui.ControlRenderer(); |
| 1187 |
assertUndefined( |
| 1188 |
'Class-to-state map must not exist until first use', |
| 1189 |
renderer.stateByClass_); |
| 1190 |
assertEquals( |
| 1191 |
'Renderer must return expected state', goog.ui.Component.State.SELECTED, |
| 1192 |
renderer.getStateFromClass('goog-control-selected')); |
| 1193 |
assertEquals( |
| 1194 |
'Renderer must return 0x00 for unknown class', 0x00, |
| 1195 |
renderer.getStateFromClass('goog-control-annoyed')); |
| 1196 |
} |
| 1197 |
|
| 1198 |
function testIe6ClassCombinationsCreateDom() { |
| 1199 |
control.setRenderer(testRenderer); |
| 1200 |
|
| 1201 |
control.enableClassName('combined', true); |
| 1202 |
|
| 1203 |
control.createDom(); |
| 1204 |
var element = control.getElement(); |
| 1205 |
|
| 1206 |
testRenderer.setState(control, goog.ui.Component.State.DISABLED, true); |
| 1207 |
var expectedClasses = |
| 1208 |
['combined', 'goog-base', 'goog-base-disabled', 'goog-button']; |
| 1209 |
if (isIe6()) { |
| 1210 |
assertSameElements( |
| 1211 |
'IE6 and lower should have one combined class', |
| 1212 |
expectedClasses.concat(['combined_goog-base-disabled_goog-button']), |
| 1213 |
goog.dom.classlist.get(element)); |
| 1214 |
} else { |
| 1215 |
assertSameElements( |
| 1216 |
'Non IE6 browsers should not have a combined class', expectedClasses, |
| 1217 |
goog.dom.classlist.get(element)); |
| 1218 |
} |
| 1219 |
|
| 1220 |
testRenderer.setState(control, goog.ui.Component.State.DISABLED, false); |
| 1221 |
testRenderer.setState(control, goog.ui.Component.State.HOVER, true); |
| 1222 |
var expectedClasses = |
| 1223 |
['combined', 'goog-base', 'goog-base-hover', 'goog-button']; |
| 1224 |
if (isIe6()) { |
| 1225 |
assertSameElements( |
| 1226 |
'IE6 and lower should have one combined class', |
| 1227 |
expectedClasses.concat(['combined_goog-base-hover_goog-button']), |
| 1228 |
goog.dom.classlist.get(element)); |
| 1229 |
} else { |
| 1230 |
assertSameElements( |
| 1231 |
'Non IE6 browsers should not have a combined class', expectedClasses, |
| 1232 |
goog.dom.classlist.get(element)); |
| 1233 |
} |
| 1234 |
|
| 1235 |
testRenderer.setRightToLeft(element, true); |
| 1236 |
testRenderer.enableExtraClassName(control, 'combined2', true); |
| 1237 |
var expectedClasses = [ |
| 1238 |
'combined', 'combined2', 'goog-base', 'goog-base-hover', 'goog-base-rtl', |
| 1239 |
'goog-button' |
| 1240 |
]; |
| 1241 |
if (isIe6()) { |
| 1242 |
assertSameElements( |
| 1243 |
'IE6 and lower should have two combined class', expectedClasses.concat([ |
| 1244 |
'combined_goog-base-hover_goog-button', |
| 1245 |
'combined_combined2_goog-base-hover_goog-base-rtl_goog-button' |
| 1246 |
]), |
| 1247 |
goog.dom.classlist.get(element)); |
| 1248 |
} else { |
| 1249 |
assertSameElements( |
| 1250 |
'Non IE6 browsers should not have a combined class', expectedClasses, |
| 1251 |
goog.dom.classlist.get(element)); |
| 1252 |
} |
| 1253 |
} |
| 1254 |
|
| 1255 |
function testIe6ClassCombinationsDecorate() { |
| 1256 |
sandbox.innerHTML = '<div id="foo" class="combined goog-base-hover"></div>'; |
| 1257 |
var foo = goog.dom.getElement('foo'); |
| 1258 |
|
| 1259 |
var element = testRenderer.decorate(control, foo); |
| 1260 |
|
| 1261 |
var expectedClasses = |
| 1262 |
['combined', 'goog-base', 'goog-base-hover', 'goog-button']; |
| 1263 |
if (isIe6()) { |
| 1264 |
assertSameElements( |
| 1265 |
'IE6 and lower should have one combined class', |
| 1266 |
expectedClasses.concat(['combined_goog-base-hover_goog-button']), |
| 1267 |
goog.dom.classlist.get(element)); |
| 1268 |
} else { |
| 1269 |
assertSameElements( |
| 1270 |
'Non IE6 browsers should not have a combined class', expectedClasses, |
| 1271 |
goog.dom.classlist.get(element)); |
| 1272 |
} |
| 1273 |
} |