| 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.ControlTest'); |
| 16 |
goog.setTestOnly('goog.ui.ControlTest'); |
| 17 |
|
| 18 |
goog.require('goog.a11y.aria'); |
| 19 |
goog.require('goog.a11y.aria.State'); |
| 20 |
goog.require('goog.array'); |
| 21 |
goog.require('goog.dom'); |
| 22 |
goog.require('goog.dom.TagName'); |
| 23 |
goog.require('goog.dom.classlist'); |
| 24 |
goog.require('goog.events'); |
| 25 |
goog.require('goog.events.BrowserEvent'); |
| 26 |
goog.require('goog.events.KeyCodes'); |
| 27 |
goog.require('goog.html.testing'); |
| 28 |
goog.require('goog.object'); |
| 29 |
goog.require('goog.string'); |
| 30 |
goog.require('goog.style'); |
| 31 |
goog.require('goog.testing.ExpectedFailures'); |
| 32 |
goog.require('goog.testing.events'); |
| 33 |
goog.require('goog.testing.events.Event'); |
| 34 |
goog.require('goog.testing.jsunit'); |
| 35 |
goog.require('goog.ui.Component'); |
| 36 |
goog.require('goog.ui.Control'); |
| 37 |
goog.require('goog.ui.ControlRenderer'); |
| 38 |
goog.require('goog.ui.registry'); |
| 39 |
goog.require('goog.userAgent'); |
| 40 |
|
| 41 |
// Disabled due to problems on farm. |
| 42 |
var testFocus = false; |
| 43 |
|
| 44 |
var control; |
| 45 |
|
| 46 |
var ALL_EVENTS = goog.object.getValues(goog.ui.Component.EventType); |
| 47 |
var events = {}; |
| 48 |
var expectedFailures; |
| 49 |
var sandbox; |
| 50 |
var aria = goog.a11y.aria; |
| 51 |
var State = goog.a11y.aria.State; |
| 52 |
|
| 53 |
function setUpPage() { |
| 54 |
expectedFailures = new goog.testing.ExpectedFailures(); |
| 55 |
sandbox = document.getElementById('sandbox'); |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
/** |
| 61 |
* A dummy renderer, for testing purposes. |
| 62 |
* @constructor |
| 63 |
* @extends {goog.ui.ControlRenderer} |
| 64 |
*/ |
| 65 |
function TestRenderer() { |
| 66 |
goog.ui.ControlRenderer.call(this); |
| 67 |
} |
| 68 |
goog.inherits(TestRenderer, goog.ui.ControlRenderer); |
| 69 |
|
| 70 |
|
| 71 |
/** |
| 72 |
* Initializes the testcase prior to execution. |
| 73 |
*/ |
| 74 |
function setUp() { |
| 75 |
control = new goog.ui.Control('Hello'); |
| 76 |
control.setDispatchTransitionEvents(goog.ui.Component.State.ALL, true); |
| 77 |
goog.events.listen(control, ALL_EVENTS, countEvent); |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
/** |
| 82 |
* Cleans up after executing the testcase. |
| 83 |
*/ |
| 84 |
function tearDown() { |
| 85 |
control.dispose(); |
| 86 |
expectedFailures.handleTearDown(); |
| 87 |
goog.dom.removeChildren(sandbox); |
| 88 |
resetEventCount(); |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
/** |
| 93 |
* Resets the global counter for events dispatched by test objects. |
| 94 |
*/ |
| 95 |
function resetEventCount() { |
| 96 |
goog.object.clear(events); |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
/** |
| 101 |
* Increments the global counter for events of this type. |
| 102 |
* @param {goog.events.Event} e Event to count. |
| 103 |
*/ |
| 104 |
function countEvent(e) { |
| 105 |
var type = e.type; |
| 106 |
var target = e.target; |
| 107 |
|
| 108 |
if (!events[target]) { |
| 109 |
events[target] = {}; |
| 110 |
} |
| 111 |
|
| 112 |
if (events[target][type]) { |
| 113 |
events[target][type]++; |
| 114 |
} else { |
| 115 |
events[target][type] = 1; |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
/** |
| 121 |
* Returns the number of times test objects dispatched events of the given |
| 122 |
* type since the global counter was last reset. |
| 123 |
* @param {goog.ui.Control} target Event target. |
| 124 |
* @param {string} type Event type. |
| 125 |
* @return {number} Number of events of this type. |
| 126 |
*/ |
| 127 |
function getEventCount(target, type) { |
| 128 |
return events[target] && events[target][type] || 0; |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
/** |
| 133 |
* Returns true if no events were dispatched since the last reset. |
| 134 |
* @return {boolean} Whether no events have been dispatched since the last |
| 135 |
* reset. |
| 136 |
*/ |
| 137 |
function noEventsDispatched() { |
| 138 |
return !events || goog.object.isEmpty(events); |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
/** |
| 143 |
* Returns the number of event listeners created by the control. |
| 144 |
* @param {goog.ui.Control} control Control whose event listers are to be |
| 145 |
* counted. |
| 146 |
* @return {number} Number of event listeners. |
| 147 |
*/ |
| 148 |
function getListenerCount(control) { |
| 149 |
return control.googUiComponentHandler_ ? |
| 150 |
goog.object.getCount(control.getHandler().keys_) : |
| 151 |
0; |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
/** |
| 156 |
* Simulates a mousedown event on the given element, including focusing it. |
| 157 |
* @param {Element} element Element on which to simulate mousedown. |
| 158 |
* @param {goog.events.BrowserEvent.MouseButton=} opt_button Mouse button; |
| 159 |
* defaults to {@code goog.events.BrowserEvent.MouseButton.LEFT}. |
| 160 |
* @return {boolean} Whether the event was allowed to proceed. |
| 161 |
*/ |
| 162 |
function fireMouseDownAndFocus(element, opt_button) { |
| 163 |
var result = goog.testing.events.fireMouseDownEvent(element, opt_button); |
| 164 |
if (result) { |
| 165 |
// Browsers move focus for all buttons, not just the left button. |
| 166 |
element.focus(); |
| 167 |
} |
| 168 |
return result; |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
/** |
| 173 |
* @return {boolean} Whether we're on Mac Safari 3.x. |
| 174 |
*/ |
| 175 |
function isMacSafari3() { |
| 176 |
return goog.userAgent.WEBKIT && goog.userAgent.MAC && |
| 177 |
!goog.userAgent.isVersionOrHigher('527'); |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
/** |
| 182 |
* Tests the {@link goog.ui.Control} constructor. |
| 183 |
*/ |
| 184 |
function testConstructor() { |
| 185 |
assertNotNull('Constructed control must not be null', control); |
| 186 |
assertEquals( |
| 187 |
'Content must have expected value', 'Hello', control.getContent()); |
| 188 |
assertEquals( |
| 189 |
'Renderer must default to the registered renderer', |
| 190 |
goog.ui.registry.getDefaultRenderer(goog.ui.Control), |
| 191 |
control.getRenderer()); |
| 192 |
|
| 193 |
var content = goog.dom.createDom( |
| 194 |
goog.dom.TagName.DIV, null, 'Hello', |
| 195 |
goog.dom.createDom(goog.dom.TagName.B, null, 'World')); |
| 196 |
var testRenderer = new TestRenderer(); |
| 197 |
var fakeDomHelper = {}; |
| 198 |
var foo = new goog.ui.Control(content, testRenderer, fakeDomHelper); |
| 199 |
assertNotNull('Constructed object must not be null', foo); |
| 200 |
assertEquals('Content must have expected value', content, foo.getContent()); |
| 201 |
assertEquals( |
| 202 |
'Renderer must have expected value', testRenderer, foo.getRenderer()); |
| 203 |
assertEquals( |
| 204 |
'DOM helper must have expected value', fakeDomHelper, foo.getDomHelper()); |
| 205 |
foo.dispose(); |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
/** |
| 210 |
* Tests {@link goog.ui.Control#getHandler}. |
| 211 |
*/ |
| 212 |
function testGetHandler() { |
| 213 |
assertUndefined( |
| 214 |
'Event handler must be undefined before getHandler() ' + |
| 215 |
'is called', |
| 216 |
control.googUiComponentHandler_); |
| 217 |
var handler = control.getHandler(); |
| 218 |
assertNotNull('Event handler must not be null', handler); |
| 219 |
assertEquals( |
| 220 |
'getHandler() must return the same instance if called again', handler, |
| 221 |
control.getHandler()); |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
/** |
| 226 |
* Tests {@link goog.ui.Control#isHandleMouseEvents}. |
| 227 |
*/ |
| 228 |
function testIsHandleMouseEvents() { |
| 229 |
assertTrue( |
| 230 |
'Controls must handle their own mouse events by default', |
| 231 |
control.isHandleMouseEvents()); |
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
/** |
| 236 |
* Tests {@link goog.ui.Control#setHandleMouseEvents}. |
| 237 |
*/ |
| 238 |
function testSetHandleMouseEvents() { |
| 239 |
assertTrue( |
| 240 |
'Control must handle its own mouse events by default', |
| 241 |
control.isHandleMouseEvents()); |
| 242 |
control.setHandleMouseEvents(false); |
| 243 |
assertFalse( |
| 244 |
'Control must no longer handle its own mouse events', |
| 245 |
control.isHandleMouseEvents()); |
| 246 |
control.setHandleMouseEvents(true); |
| 247 |
assertTrue( |
| 248 |
'Control must once again handle its own mouse events', |
| 249 |
control.isHandleMouseEvents()); |
| 250 |
control.render(sandbox); |
| 251 |
assertTrue( |
| 252 |
'Rendered control must handle its own mouse events', |
| 253 |
control.isHandleMouseEvents()); |
| 254 |
control.setHandleMouseEvents(false); |
| 255 |
assertFalse( |
| 256 |
'Rendered control must no longer handle its own mouse events', |
| 257 |
control.isHandleMouseEvents()); |
| 258 |
control.setHandleMouseEvents(true); |
| 259 |
assertTrue( |
| 260 |
'Rendered control must once again handle its own mouse events', |
| 261 |
control.isHandleMouseEvents()); |
| 262 |
} |
| 263 |
|
| 264 |
|
| 265 |
/** |
| 266 |
* Tests {@link goog.ui.Control#getKeyEventTarget}. |
| 267 |
*/ |
| 268 |
function testGetKeyEventTarget() { |
| 269 |
assertNull( |
| 270 |
'Key event target of control without DOM must be null', |
| 271 |
control.getKeyEventTarget()); |
| 272 |
control.createDom(); |
| 273 |
assertEquals( |
| 274 |
'Key event target of control with DOM must be its element', |
| 275 |
control.getElement(), control.getKeyEventTarget()); |
| 276 |
} |
| 277 |
|
| 278 |
|
| 279 |
/** |
| 280 |
* Tests {@link goog.ui.Control#getKeyHandler}. |
| 281 |
*/ |
| 282 |
function testGetKeyHandler() { |
| 283 |
assertUndefined( |
| 284 |
'Key handler must be undefined before getKeyHandler() ' + |
| 285 |
'is called', |
| 286 |
control.keyHandler_); |
| 287 |
var keyHandler = control.getKeyHandler(); |
| 288 |
assertNotNull('Key handler must not be null', keyHandler); |
| 289 |
assertEquals( |
| 290 |
'getKeyHandler() must return the same instance if called ' + |
| 291 |
'again', |
| 292 |
keyHandler, control.getKeyHandler()); |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
/** |
| 297 |
* Tests {@link goog.ui.Control#getRenderer}. |
| 298 |
*/ |
| 299 |
function testGetRenderer() { |
| 300 |
assertEquals( |
| 301 |
'Renderer must be the default registered renderer', |
| 302 |
goog.ui.registry.getDefaultRenderer(goog.ui.Control), |
| 303 |
control.getRenderer()); |
| 304 |
} |
| 305 |
|
| 306 |
|
| 307 |
/** |
| 308 |
* Tests {@link goog.ui.Control#setRenderer}. |
| 309 |
*/ |
| 310 |
function testSetRenderer() { |
| 311 |
control.createDom(); |
| 312 |
assertNotNull('Control must have a DOM', control.getElement()); |
| 313 |
assertFalse('Control must not be in the document', control.isInDocument()); |
| 314 |
assertEquals( |
| 315 |
'Renderer must be the default registered renderer', |
| 316 |
goog.ui.registry.getDefaultRenderer(goog.ui.Control), |
| 317 |
control.getRenderer()); |
| 318 |
|
| 319 |
var testRenderer = new TestRenderer(); |
| 320 |
control.setRenderer(testRenderer); |
| 321 |
assertNull( |
| 322 |
'Control must not have a DOM after its renderer is reset', |
| 323 |
control.getElement()); |
| 324 |
assertFalse( |
| 325 |
'Control still must not be in the document', control.isInDocument()); |
| 326 |
assertEquals( |
| 327 |
'Renderer must have expected value', testRenderer, control.getRenderer()); |
| 328 |
|
| 329 |
control.render(sandbox); |
| 330 |
assertTrue('Control must be in the document', control.isInDocument()); |
| 331 |
|
| 332 |
assertThrows( |
| 333 |
'Resetting the renderer after the control has entered ' + |
| 334 |
'the document must throw error', |
| 335 |
function() { control.setRenderer({}); }); |
| 336 |
} |
| 337 |
|
| 338 |
|
| 339 |
/** |
| 340 |
* Tests {@link goog.ui.Control#getExtraClassNames}. |
| 341 |
*/ |
| 342 |
function testGetExtraClassNames() { |
| 343 |
assertNull( |
| 344 |
'Control must not have any extra class names by default', |
| 345 |
control.getExtraClassNames()); |
| 346 |
} |
| 347 |
|
| 348 |
|
| 349 |
/** |
| 350 |
* Tests {@link goog.ui.Control#addExtraClassName} and |
| 351 |
* {@link goog.ui.Control#removeExtraClassName}. |
| 352 |
*/ |
| 353 |
function testAddRemoveClassName() { |
| 354 |
assertNull( |
| 355 |
'Control must not have any extra class names by default', |
| 356 |
control.getExtraClassNames()); |
| 357 |
control.addClassName('foo'); |
| 358 |
assertArrayEquals( |
| 359 |
'Control must have expected extra class names', ['foo'], |
| 360 |
control.getExtraClassNames()); |
| 361 |
assertNull('Control must not have a DOM', control.getElement()); |
| 362 |
|
| 363 |
control.createDom(); |
| 364 |
assertSameElements( |
| 365 |
'Control\'s element must have expected class names', |
| 366 |
['goog-control', 'foo'], goog.dom.classlist.get(control.getElement())); |
| 367 |
|
| 368 |
control.addClassName('bar'); |
| 369 |
assertArrayEquals( |
| 370 |
'Control must have expected extra class names', ['foo', 'bar'], |
| 371 |
control.getExtraClassNames()); |
| 372 |
assertSameElements( |
| 373 |
'Control\'s element must have expected class names', |
| 374 |
['goog-control', 'foo', 'bar'], |
| 375 |
goog.dom.classlist.get(control.getElement())); |
| 376 |
|
| 377 |
control.addClassName('bar'); |
| 378 |
assertArrayEquals( |
| 379 |
'Adding the same class name again must be a no-op', ['foo', 'bar'], |
| 380 |
control.getExtraClassNames()); |
| 381 |
assertSameElements( |
| 382 |
'Adding the same class name again must be a no-op', |
| 383 |
['goog-control', 'foo', 'bar'], |
| 384 |
goog.dom.classlist.get(control.getElement())); |
| 385 |
|
| 386 |
control.addClassName(null); |
| 387 |
assertArrayEquals( |
| 388 |
'Adding null class name must be a no-op', ['foo', 'bar'], |
| 389 |
control.getExtraClassNames()); |
| 390 |
assertSameElements( |
| 391 |
'Adding null class name must be a no-op', ['goog-control', 'foo', 'bar'], |
| 392 |
goog.dom.classlist.get(control.getElement())); |
| 393 |
|
| 394 |
control.removeClassName(null); |
| 395 |
assertArrayEquals( |
| 396 |
'Removing null class name must be a no-op', ['foo', 'bar'], |
| 397 |
control.getExtraClassNames()); |
| 398 |
assertSameElements( |
| 399 |
'Removing null class name must be a no-op', |
| 400 |
['goog-control', 'foo', 'bar'], |
| 401 |
goog.dom.classlist.get(control.getElement())); |
| 402 |
|
| 403 |
control.removeClassName('foo'); |
| 404 |
assertArrayEquals( |
| 405 |
'Control must have expected extra class names', ['bar'], |
| 406 |
control.getExtraClassNames()); |
| 407 |
assertSameElements( |
| 408 |
'Control\'s element must have expected class names', |
| 409 |
['goog-control', 'bar'], goog.dom.classlist.get(control.getElement())); |
| 410 |
|
| 411 |
control.removeClassName('bar'); |
| 412 |
assertNull( |
| 413 |
'Control must not have any extra class names', |
| 414 |
control.getExtraClassNames()); |
| 415 |
assertSameElements( |
| 416 |
'Control\'s element must have expected class names', ['goog-control'], |
| 417 |
goog.dom.classlist.get(control.getElement())); |
| 418 |
} |
| 419 |
|
| 420 |
|
| 421 |
/** |
| 422 |
* Tests {@link goog.ui.Control#enableClassName}. |
| 423 |
*/ |
| 424 |
function testEnableClassName() { |
| 425 |
assertNull( |
| 426 |
'Control must not have any extra class names by default', |
| 427 |
control.getExtraClassNames()); |
| 428 |
|
| 429 |
control.enableClassName('foo', true); |
| 430 |
assertArrayEquals( |
| 431 |
'Control must have expected extra class names', ['foo'], |
| 432 |
control.getExtraClassNames()); |
| 433 |
|
| 434 |
control.enableClassName('bar', true); |
| 435 |
assertArrayEquals( |
| 436 |
'Control must have expected extra class names', ['foo', 'bar'], |
| 437 |
control.getExtraClassNames()); |
| 438 |
|
| 439 |
control.enableClassName('bar', true); |
| 440 |
assertArrayEquals( |
| 441 |
'Enabling the same class name again must be a no-op', ['foo', 'bar'], |
| 442 |
control.getExtraClassNames()); |
| 443 |
|
| 444 |
control.enableClassName(null); |
| 445 |
assertArrayEquals( |
| 446 |
'Enabling null class name must be a no-op', ['foo', 'bar'], |
| 447 |
control.getExtraClassNames()); |
| 448 |
|
| 449 |
control.enableClassName('foo', false); |
| 450 |
assertArrayEquals( |
| 451 |
'Control must have expected extra class names', ['bar'], |
| 452 |
control.getExtraClassNames()); |
| 453 |
|
| 454 |
control.enableClassName('bar', false); |
| 455 |
assertNull( |
| 456 |
'Control must not have any extra class names', |
| 457 |
control.getExtraClassNames()); |
| 458 |
} |
| 459 |
|
| 460 |
|
| 461 |
/** |
| 462 |
* Tests {@link goog.ui.Control#createDom}. |
| 463 |
*/ |
| 464 |
function testCreateDom() { |
| 465 |
assertNull('Control must not have a DOM by default', control.getElement()); |
| 466 |
assertFalse( |
| 467 |
'Control must not allow text selection by default', |
| 468 |
control.isAllowTextSelection()); |
| 469 |
assertTrue('Control must be visible by default', control.isVisible()); |
| 470 |
|
| 471 |
control.createDom(); |
| 472 |
assertNotNull('Control must have a DOM', control.getElement()); |
| 473 |
assertTrue( |
| 474 |
'Control\'s element must be unselectable', |
| 475 |
goog.style.isUnselectable(control.getElement())); |
| 476 |
assertTrue( |
| 477 |
'Control\'s element must be visible', |
| 478 |
control.getElement().style.display != 'none'); |
| 479 |
|
| 480 |
control.setAllowTextSelection(true); |
| 481 |
control.createDom(); |
| 482 |
assertFalse( |
| 483 |
'Control\'s element must be selectable', |
| 484 |
goog.style.isUnselectable(control.getElement())); |
| 485 |
|
| 486 |
control.setVisible(false); |
| 487 |
control.createDom(); |
| 488 |
assertTrue( |
| 489 |
'Control\'s element must be hidden', |
| 490 |
control.getElement().style.display == 'none'); |
| 491 |
} |
| 492 |
|
| 493 |
|
| 494 |
/** |
| 495 |
* Tests {@link goog.ui.Control#getContentElement}. |
| 496 |
*/ |
| 497 |
function testGetContentElement() { |
| 498 |
assertNull( |
| 499 |
'Unrendered control must not have a content element', |
| 500 |
control.getContentElement()); |
| 501 |
control.createDom(); |
| 502 |
assertEquals( |
| 503 |
'Control\'s content element must equal its root element', |
| 504 |
control.getElement(), control.getContentElement()); |
| 505 |
} |
| 506 |
|
| 507 |
|
| 508 |
/** |
| 509 |
* Tests {@link goog.ui.Control#canDecorate}. |
| 510 |
*/ |
| 511 |
function testCanDecorate() { |
| 512 |
assertTrue(control.canDecorate(goog.dom.createElement(goog.dom.TagName.DIV))); |
| 513 |
} |
| 514 |
|
| 515 |
|
| 516 |
/** |
| 517 |
* Tests {@link goog.ui.Control#decorateInternal}. |
| 518 |
*/ |
| 519 |
function testDecorateInternal() { |
| 520 |
sandbox.innerHTML = '<div id="foo">Hello, <b>World</b>!</div>'; |
| 521 |
var foo = goog.dom.getElement('foo'); |
| 522 |
control.decorate(foo); |
| 523 |
assertEquals( |
| 524 |
'Decorated control\'s element must have expected value', foo, |
| 525 |
control.getElement()); |
| 526 |
assertTrue( |
| 527 |
'Element must be unselectable', |
| 528 |
goog.style.isUnselectable(control.getElement())); |
| 529 |
assertTrue( |
| 530 |
'Element must be visible', control.getElement().style.display != 'none'); |
| 531 |
} |
| 532 |
|
| 533 |
|
| 534 |
/** |
| 535 |
* Tests {@link goog.ui.Control#decorateInternal} with a control that |
| 536 |
* allows text selection. |
| 537 |
*/ |
| 538 |
function testDecorateInternalForSelectableControl() { |
| 539 |
sandbox.innerHTML = '<div id="foo">Hello, <b>World</b>!</div>'; |
| 540 |
var foo = goog.dom.getElement('foo'); |
| 541 |
control.setAllowTextSelection(true); |
| 542 |
control.decorate(foo); |
| 543 |
assertEquals( |
| 544 |
'Decorated control\'s element must have expected value', foo, |
| 545 |
control.getElement()); |
| 546 |
assertFalse( |
| 547 |
'Element must be selectable', |
| 548 |
goog.style.isUnselectable(control.getElement())); |
| 549 |
assertTrue('Control must be visible', control.isVisible()); |
| 550 |
} |
| 551 |
|
| 552 |
|
| 553 |
/** |
| 554 |
* Tests {@link goog.ui.Control#decorateInternal} with a hidden element. |
| 555 |
*/ |
| 556 |
function testDecorateInternalForHiddenElement() { |
| 557 |
sandbox.innerHTML = '<div id="foo" style="display:none">Hello!</div>'; |
| 558 |
var foo = goog.dom.getElement('foo'); |
| 559 |
control.decorate(foo); |
| 560 |
assertEquals( |
| 561 |
'Decorated control\'s element must have expected value', foo, |
| 562 |
control.getElement()); |
| 563 |
assertTrue( |
| 564 |
'Element must be unselectable', |
| 565 |
goog.style.isUnselectable(control.getElement())); |
| 566 |
assertFalse('Control must be hidden', control.isVisible()); |
| 567 |
} |
| 568 |
|
| 569 |
|
| 570 |
/** |
| 571 |
* Tests {@link goog.ui.Control#enterDocument}. |
| 572 |
*/ |
| 573 |
function testEnterDocument() { |
| 574 |
control.render(sandbox); |
| 575 |
assertTrue('Control must be in the document', control.isInDocument()); |
| 576 |
if (goog.userAgent.IE && !goog.userAgent.isVersionOrHigher(9)) { |
| 577 |
assertEquals( |
| 578 |
'Control must have 5 mouse & 3 key event listeners on IE8', 8, |
| 579 |
getListenerCount(control)); |
| 580 |
} else { |
| 581 |
assertEquals( |
| 582 |
'Control must have 4 mouse and 3 key event listeners', 7, |
| 583 |
getListenerCount(control)); |
| 584 |
} |
| 585 |
assertEquals( |
| 586 |
'Control\'s key event handler must be attached to its ' + |
| 587 |
'key event target', |
| 588 |
control.getKeyEventTarget(), control.getKeyHandler().element_); |
| 589 |
} |
| 590 |
|
| 591 |
|
| 592 |
/** |
| 593 |
* Tests {@link goog.ui.Control#enterDocument} for a control that doesn't |
| 594 |
* handle mouse events. |
| 595 |
*/ |
| 596 |
function testEnterDocumentForControlWithoutMouseHandling() { |
| 597 |
control.setHandleMouseEvents(false); |
| 598 |
control.render(sandbox); |
| 599 |
assertTrue('Control must be in the document', control.isInDocument()); |
| 600 |
assertEquals( |
| 601 |
'Control must have 3 key event listeners', 3, getListenerCount(control)); |
| 602 |
assertEquals( |
| 603 |
'Control\'s key event handler must be attached to its ' + |
| 604 |
'key event target', |
| 605 |
control.getKeyEventTarget(), control.getKeyHandler().element_); |
| 606 |
} |
| 607 |
|
| 608 |
|
| 609 |
/** |
| 610 |
* Tests {@link goog.ui.Control#enterDocument} for a control that isn't |
| 611 |
* focusable. |
| 612 |
*/ |
| 613 |
function testEnterDocumentForNonFocusableControl() { |
| 614 |
control.setSupportedState(goog.ui.Component.State.FOCUSED, false); |
| 615 |
control.render(sandbox); |
| 616 |
assertTrue('Control must be in the document', control.isInDocument()); |
| 617 |
if (goog.userAgent.IE && !goog.userAgent.isVersionOrHigher(9)) { |
| 618 |
assertEquals( |
| 619 |
'Control must have 5 mouse event listeners on IE8', 5, |
| 620 |
getListenerCount(control)); |
| 621 |
} else { |
| 622 |
assertEquals( |
| 623 |
'Control must have 4 mouse event listeners', 4, |
| 624 |
getListenerCount(control)); |
| 625 |
} |
| 626 |
assertUndefined( |
| 627 |
'Control must not have a key event handler', control.keyHandler_); |
| 628 |
} |
| 629 |
|
| 630 |
|
| 631 |
/** |
| 632 |
* Tests {@link goog.ui.Control#enterDocument} for a control that doesn't |
| 633 |
* need to do any event handling. |
| 634 |
*/ |
| 635 |
function testEnterDocumentForControlWithoutEventHandlers() { |
| 636 |
control.setHandleMouseEvents(false); |
| 637 |
control.setSupportedState(goog.ui.Component.State.FOCUSED, false); |
| 638 |
control.render(sandbox); |
| 639 |
assertTrue('Control must be in the document', control.isInDocument()); |
| 640 |
assertEquals( |
| 641 |
'Control must have 0 event listeners', 0, getListenerCount(control)); |
| 642 |
assertUndefined( |
| 643 |
'Control must not have an event handler', |
| 644 |
control.googUiComponentHandler_); |
| 645 |
assertUndefined( |
| 646 |
'Control must not have a key event handler', control.keyHandler_); |
| 647 |
} |
| 648 |
|
| 649 |
|
| 650 |
/** |
| 651 |
* Tests {@link goog.ui.Control#exitDocument}. |
| 652 |
*/ |
| 653 |
function testExitDocument() { |
| 654 |
control.render(sandbox); |
| 655 |
assertTrue('Control must be in the document', control.isInDocument()); |
| 656 |
if (goog.userAgent.IE && !goog.userAgent.isVersionOrHigher(9)) { |
| 657 |
assertEquals( |
| 658 |
'Control must have 5 mouse & 3 key event listeners on IE8', 8, |
| 659 |
getListenerCount(control)); |
| 660 |
} else { |
| 661 |
assertEquals( |
| 662 |
'Control must have 4 mouse and 3 key event listeners', 7, |
| 663 |
getListenerCount(control)); |
| 664 |
} |
| 665 |
assertEquals( |
| 666 |
'Control\'s key event handler must be attached to its ' + |
| 667 |
'key event target', |
| 668 |
control.getKeyEventTarget(), control.getKeyHandler().element_); |
| 669 |
// Expected to fail on Mac Safari prior to version 527. |
| 670 |
expectedFailures.expectFailureFor(isMacSafari3()); |
| 671 |
try { |
| 672 |
assertTrue( |
| 673 |
'Control\'s element must support keyboard focus', |
| 674 |
goog.dom.isFocusableTabIndex(control.getKeyEventTarget())); |
| 675 |
} catch (e) { |
| 676 |
expectedFailures.handleException(e); |
| 677 |
} |
| 678 |
|
| 679 |
control.exitDocument(); |
| 680 |
assertFalse( |
| 681 |
'Control must no longer be in the document', control.isInDocument()); |
| 682 |
assertEquals( |
| 683 |
'Control must have no event listeners', 0, getListenerCount(control)); |
| 684 |
assertNull( |
| 685 |
'Control\'s key event handler must be unattached', |
| 686 |
control.getKeyHandler().element_); |
| 687 |
assertFalse( |
| 688 |
'Control\'s element must no longer support keyboard focus', |
| 689 |
goog.dom.isFocusableTabIndex(control.getKeyEventTarget())); |
| 690 |
} |
| 691 |
|
| 692 |
|
| 693 |
/** |
| 694 |
* Tests {@link goog.ui.Control#dispose}. |
| 695 |
*/ |
| 696 |
function testDispose() { |
| 697 |
control.render(sandbox); |
| 698 |
var handler = control.getHandler(); |
| 699 |
var keyHandler = control.getKeyHandler(); |
| 700 |
control.dispose(); |
| 701 |
assertFalse( |
| 702 |
'Control must no longer be in the document', control.isInDocument()); |
| 703 |
assertTrue('Control must have been disposed of', control.isDisposed()); |
| 704 |
assertUndefined('Renderer must have been deleted', control.getRenderer()); |
| 705 |
assertNull('Content must be nulled out', control.getContent()); |
| 706 |
assertTrue('Event handler must have been disposed of', handler.isDisposed()); |
| 707 |
assertUndefined( |
| 708 |
'Event handler must have been deleted', control.googUiComponentHandler_); |
| 709 |
assertTrue('Key handler must have been disposed of', keyHandler.isDisposed()); |
| 710 |
assertUndefined('Key handler must have been deleted', control.keyHandler_); |
| 711 |
assertNull( |
| 712 |
'Extra class names must have been nulled out', |
| 713 |
control.getExtraClassNames()); |
| 714 |
} |
| 715 |
|
| 716 |
|
| 717 |
/** |
| 718 |
* Tests {@link goog.ui.Control#getContent}. |
| 719 |
*/ |
| 720 |
function testGetContent() { |
| 721 |
assertNull( |
| 722 |
'Empty control must have null content', |
| 723 |
(new goog.ui.Control(null)).getContent()); |
| 724 |
assertEquals( |
| 725 |
'Control must have expected content', 'Hello', control.getContent()); |
| 726 |
control.render(sandbox); |
| 727 |
assertEquals( |
| 728 |
'Control must have expected content after rendering', 'Hello', |
| 729 |
control.getContent()); |
| 730 |
} |
| 731 |
|
| 732 |
|
| 733 |
/** |
| 734 |
* Tests {@link goog.ui.Control#getContent}. |
| 735 |
*/ |
| 736 |
function testGetContentForDecoratedControl() { |
| 737 |
sandbox.innerHTML = '<div id="empty"></div>\n' + |
| 738 |
'<div id="text">Hello, world!</div>\n' + |
| 739 |
'<div id="element"><span>Foo</span></div>\n' + |
| 740 |
'<div id="nodelist">Hello, <b>world</b>!</div>\n'; |
| 741 |
|
| 742 |
var empty = new goog.ui.Control(null); |
| 743 |
empty.decorate(goog.dom.getElement('empty')); |
| 744 |
assertNull( |
| 745 |
'Content of control decorating empty DIV must be null', |
| 746 |
empty.getContent()); |
| 747 |
empty.dispose(); |
| 748 |
|
| 749 |
var text = new goog.ui.Control(null); |
| 750 |
text.decorate(goog.dom.getElement('text')); |
| 751 |
assertEquals( |
| 752 |
'Content of control decorating DIV with text contents ' + |
| 753 |
'must be as expected', |
| 754 |
'Hello, world!', text.getContent().nodeValue); |
| 755 |
text.dispose(); |
| 756 |
|
| 757 |
var element = new goog.ui.Control(null); |
| 758 |
element.decorate(goog.dom.getElement('element')); |
| 759 |
assertEquals( |
| 760 |
'Content of control decorating DIV with element child ' + |
| 761 |
'must be as expected', |
| 762 |
goog.dom.getElement('element').firstChild, element.getContent()); |
| 763 |
element.dispose(); |
| 764 |
|
| 765 |
var nodelist = new goog.ui.Control(null); |
| 766 |
nodelist.decorate(goog.dom.getElement('nodelist')); |
| 767 |
assertSameElements( |
| 768 |
'Content of control decorating DIV with mixed ' + |
| 769 |
'contents must be as expected', |
| 770 |
goog.dom.getElement('nodelist').childNodes, nodelist.getContent()); |
| 771 |
nodelist.dispose(); |
| 772 |
} |
| 773 |
|
| 774 |
|
| 775 |
/** |
| 776 |
* Tests {@link goog.ui.Control#setAriaLabel}. |
| 777 |
*/ |
| 778 |
function testSetAriaLabel_render() { |
| 779 |
assertNull( |
| 780 |
'Controls must not have any aria label by default', |
| 781 |
control.getAriaLabel()); |
| 782 |
|
| 783 |
control.setAriaLabel('label'); |
| 784 |
assertEquals('Control must have aria label', 'label', control.getAriaLabel()); |
| 785 |
|
| 786 |
control.render(sandbox); |
| 787 |
|
| 788 |
var elem = control.getElementStrict(); |
| 789 |
assertEquals( |
| 790 |
'Element must have control\'s aria label after rendering', 'label', |
| 791 |
goog.a11y.aria.getLabel(elem)); |
| 792 |
|
| 793 |
control.setAriaLabel('new label'); |
| 794 |
assertEquals( |
| 795 |
'Element must have the new aria label', 'new label', |
| 796 |
goog.a11y.aria.getLabel(elem)); |
| 797 |
} |
| 798 |
|
| 799 |
|
| 800 |
/** |
| 801 |
* Tests {@link goog.ui.Control#setAriaLabel}. |
| 802 |
*/ |
| 803 |
function testSetAriaLabel_decorate() { |
| 804 |
assertNull( |
| 805 |
'Controls must not have any aria label by default', |
| 806 |
control.getAriaLabel()); |
| 807 |
|
| 808 |
control.setAriaLabel('label'); |
| 809 |
assertEquals('Control must have aria label', 'label', control.getAriaLabel()); |
| 810 |
|
| 811 |
sandbox.innerHTML = '<div id="nodelist" role="button">' + |
| 812 |
'Hello, <b>world</b>!</div>'; |
| 813 |
control.decorate(goog.dom.getElement('nodelist')); |
| 814 |
|
| 815 |
var elem = control.getElementStrict(); |
| 816 |
assertEquals( |
| 817 |
'Element must have control\'s aria label after rendering', 'label', |
| 818 |
goog.a11y.aria.getLabel(elem)); |
| 819 |
assertEquals( |
| 820 |
'Element must have the correct role', 'button', |
| 821 |
elem.getAttribute('role')); |
| 822 |
|
| 823 |
control.setAriaLabel('new label'); |
| 824 |
assertEquals( |
| 825 |
'Element must have the new aria label', 'new label', |
| 826 |
goog.a11y.aria.getLabel(elem)); |
| 827 |
} |
| 828 |
|
| 829 |
|
| 830 |
/** |
| 831 |
* Tests {@link goog.ui.Control#setContent}. |
| 832 |
*/ |
| 833 |
function testSetContent() { |
| 834 |
control.setContent('Bye'); |
| 835 |
assertEquals( |
| 836 |
'Unrendered control control must have expected contents', 'Bye', |
| 837 |
control.getContent()); |
| 838 |
assertNull('No DOM must be created by setContent', control.getElement()); |
| 839 |
|
| 840 |
control.createDom(); |
| 841 |
assertEquals( |
| 842 |
'Rendered control\'s DOM must have expected contents', 'Bye', |
| 843 |
control.getElement().innerHTML); |
| 844 |
|
| 845 |
control.setContent(null); |
| 846 |
assertNull( |
| 847 |
'Rendered control must have expected contents', control.getContent()); |
| 848 |
assertEquals( |
| 849 |
'Rendered control\'s DOM must have expected contents', '', |
| 850 |
control.getElement().innerHTML); |
| 851 |
|
| 852 |
control.setContent([ |
| 853 |
goog.dom.createDom( |
| 854 |
goog.dom.TagName.DIV, null, |
| 855 |
goog.dom.createDom(goog.dom.TagName.SPAN, null, 'Hello')), |
| 856 |
'World' |
| 857 |
]); |
| 858 |
assertHTMLEquals( |
| 859 |
'Control\'s DOM must be updated', '<div><span>Hello</span></div>World', |
| 860 |
control.getElement().innerHTML); |
| 861 |
} |
| 862 |
|
| 863 |
|
| 864 |
/** |
| 865 |
* Tests {@link goog.ui.Control#setContentInternal}. |
| 866 |
*/ |
| 867 |
function testSetContentInternal() { |
| 868 |
control.render(sandbox); |
| 869 |
assertEquals( |
| 870 |
'Control must have expected content after rendering', 'Hello', |
| 871 |
control.getContent()); |
| 872 |
control.setContentInternal('Bye'); |
| 873 |
assertEquals( |
| 874 |
'Control must have expected contents', 'Bye', control.getContent()); |
| 875 |
assertEquals( |
| 876 |
'Control\'s DOM must be unchanged', 'Hello', |
| 877 |
control.getElement().innerHTML); |
| 878 |
} |
| 879 |
|
| 880 |
|
| 881 |
/** |
| 882 |
* Tests {@link goog.ui.Control#getCaption}. |
| 883 |
*/ |
| 884 |
function testGetCaption() { |
| 885 |
assertEquals( |
| 886 |
'Empty control\'s caption must be empty string', '', |
| 887 |
(new goog.ui.Control(null)).getCaption()); |
| 888 |
|
| 889 |
assertEquals( |
| 890 |
'Caption must have expected value', 'Hello', control.getCaption()); |
| 891 |
|
| 892 |
sandbox.innerHTML = '<div id="nodelist">Hello, <b>world</b>!</div>'; |
| 893 |
control.decorate(goog.dom.getElement('nodelist')); |
| 894 |
assertEquals( |
| 895 |
'Caption must have expected value', 'Hello, world!', |
| 896 |
control.getCaption()); |
| 897 |
|
| 898 |
var arrayContent = goog.array.clone( |
| 899 |
goog.dom.safeHtmlToNode( |
| 900 |
goog.html.testing.newSafeHtmlForTest( |
| 901 |
' <b> foo</b><i> bar</i> ')).childNodes); |
| 902 |
control.setContent(arrayContent); |
| 903 |
assertEquals( |
| 904 |
'whitespaces must be normalized in the caption', 'foo bar', |
| 905 |
control.getCaption()); |
| 906 |
|
| 907 |
control.setContent('\xa0foo'); |
| 908 |
assertEquals( |
| 909 |
'indenting spaces must be kept', '\xa0foo', control.getCaption()); |
| 910 |
} |
| 911 |
|
| 912 |
|
| 913 |
/** |
| 914 |
* Tests {@link goog.ui.Control#setCaption}. |
| 915 |
*/ |
| 916 |
function testSetCaption() { |
| 917 |
control.setCaption('Hello, world!'); |
| 918 |
assertEquals( |
| 919 |
'Control must have a string caption "Hello, world!"', 'Hello, world!', |
| 920 |
control.getCaption()); |
| 921 |
} |
| 922 |
|
| 923 |
|
| 924 |
/** |
| 925 |
* Tests {@link goog.ui.Control#setRightToLeft}. |
| 926 |
*/ |
| 927 |
function testSetRightToLeft() { |
| 928 |
control.createDom(); |
| 929 |
assertFalse( |
| 930 |
'Control\'s element must not have right-to-left class', |
| 931 |
goog.dom.classlist.contains(control.getElement(), 'goog-control-rtl')); |
| 932 |
control.setRightToLeft(true); |
| 933 |
assertTrue( |
| 934 |
'Control\'s element must have right-to-left class', |
| 935 |
goog.dom.classlist.contains(control.getElement(), 'goog-control-rtl')); |
| 936 |
control.render(sandbox); |
| 937 |
assertThrows( |
| 938 |
'Changing the render direction of a control already in ' + |
| 939 |
'the document is an error', |
| 940 |
function() { control.setRightToLeft(false); }); |
| 941 |
} |
| 942 |
|
| 943 |
|
| 944 |
/** |
| 945 |
* Tests {@link goog.ui.Control#isAllowTextSelection}. |
| 946 |
*/ |
| 947 |
function testIsAllowTextSelection() { |
| 948 |
assertFalse( |
| 949 |
'Controls must not allow text selection by default', |
| 950 |
control.isAllowTextSelection()); |
| 951 |
} |
| 952 |
|
| 953 |
|
| 954 |
/** |
| 955 |
* Tests {@link goog.ui.Control#setAllowTextSelection}. |
| 956 |
*/ |
| 957 |
function testSetAllowTextSelection() { |
| 958 |
assertFalse( |
| 959 |
'Controls must not allow text selection by default', |
| 960 |
control.isAllowTextSelection()); |
| 961 |
|
| 962 |
control.setAllowTextSelection(true); |
| 963 |
assertTrue( |
| 964 |
'Control must allow text selection', control.isAllowTextSelection()); |
| 965 |
|
| 966 |
control.setAllowTextSelection(false); |
| 967 |
assertFalse( |
| 968 |
'Control must no longer allow text selection', |
| 969 |
control.isAllowTextSelection()); |
| 970 |
|
| 971 |
control.render(sandbox); |
| 972 |
|
| 973 |
assertFalse( |
| 974 |
'Control must not allow text selection even after rendered', |
| 975 |
control.isAllowTextSelection()); |
| 976 |
|
| 977 |
control.setAllowTextSelection(true); |
| 978 |
assertTrue( |
| 979 |
'Control must once again allow text selection', |
| 980 |
control.isAllowTextSelection()); |
| 981 |
} |
| 982 |
|
| 983 |
|
| 984 |
/** |
| 985 |
* Tests {@link goog.ui.Control#isVisible}. |
| 986 |
*/ |
| 987 |
function testIsVisible() { |
| 988 |
assertTrue('Controls must be visible by default', control.isVisible()); |
| 989 |
} |
| 990 |
|
| 991 |
|
| 992 |
/** |
| 993 |
* Tests {@link goog.ui.Control#setVisible} before it is rendered. |
| 994 |
*/ |
| 995 |
function testSetVisible() { |
| 996 |
assertFalse( |
| 997 |
'setVisible(true) must return false if already visible', |
| 998 |
control.setVisible(true)); |
| 999 |
assertTrue('No events must have been dispatched', noEventsDispatched()); |
| 1000 |
|
| 1001 |
assertTrue( |
| 1002 |
'setVisible(false) must return true if previously visible', |
| 1003 |
control.setVisible(false)); |
| 1004 |
assertEquals( |
| 1005 |
'One HIDE event must have been dispatched', 1, |
| 1006 |
getEventCount(control, goog.ui.Component.EventType.HIDE)); |
| 1007 |
assertFalse('Control must no longer be visible', control.isVisible()); |
| 1008 |
|
| 1009 |
assertTrue( |
| 1010 |
'setVisible(true) must return true if previously hidden', |
| 1011 |
control.setVisible(true)); |
| 1012 |
assertEquals( |
| 1013 |
'One SHOW event must have been dispatched', 1, |
| 1014 |
getEventCount(control, goog.ui.Component.EventType.SHOW)); |
| 1015 |
assertTrue('Control must be visible', control.isVisible()); |
| 1016 |
} |
| 1017 |
|
| 1018 |
|
| 1019 |
/** |
| 1020 |
* Tests {@link goog.ui.Control#setVisible} after it is rendered. |
| 1021 |
*/ |
| 1022 |
function testSetVisibleForRenderedControl() { |
| 1023 |
control.render(sandbox); |
| 1024 |
assertTrue( |
| 1025 |
'No events must have been dispatched during rendering', |
| 1026 |
noEventsDispatched()); |
| 1027 |
|
| 1028 |
assertFalse( |
| 1029 |
'setVisible(true) must return false if already visible', |
| 1030 |
control.setVisible(true)); |
| 1031 |
assertTrue('No events must have been dispatched', noEventsDispatched()); |
| 1032 |
assertTrue( |
| 1033 |
'Control\'s element must be visible', |
| 1034 |
control.getElement().style.display != 'none'); |
| 1035 |
|
| 1036 |
assertTrue( |
| 1037 |
'setVisible(false) must return true if previously visible', |
| 1038 |
control.setVisible(false)); |
| 1039 |
assertEquals( |
| 1040 |
'One HIDE event must have been dispatched', 1, |
| 1041 |
getEventCount(control, goog.ui.Component.EventType.HIDE)); |
| 1042 |
assertFalse('Control must no longer be visible', control.isVisible()); |
| 1043 |
assertTrue( |
| 1044 |
'Control\'s element must be hidden', |
| 1045 |
control.getElement().style.display == 'none'); |
| 1046 |
|
| 1047 |
assertTrue( |
| 1048 |
'setVisible(true) must return true if previously hidden', |
| 1049 |
control.setVisible(true)); |
| 1050 |
assertEquals( |
| 1051 |
'One SHOW event must have been dispatched', 1, |
| 1052 |
getEventCount(control, goog.ui.Component.EventType.SHOW)); |
| 1053 |
assertTrue('Control must be visible', control.isVisible()); |
| 1054 |
assertTrue( |
| 1055 |
'Control\'s element must be visible', |
| 1056 |
control.getElement().style.display != 'none'); |
| 1057 |
} |
| 1058 |
|
| 1059 |
|
| 1060 |
/** |
| 1061 |
* Tests {@link goog.ui.Control#setVisible} for disabled non-focusable |
| 1062 |
* controls. |
| 1063 |
*/ |
| 1064 |
function testSetVisibleForDisabledNonFocusableControl() { |
| 1065 |
// Hidden, disabled, non-focusable control becoming visible. |
| 1066 |
control.setEnabled(false); |
| 1067 |
control.setSupportedState(goog.ui.Component.State.FOCUSED, false); |
| 1068 |
control.render(sandbox); |
| 1069 |
assertTrue('Control must be visible', control.isVisible()); |
| 1070 |
assertFalse( |
| 1071 |
'Control must not have a tab index', |
| 1072 |
goog.dom.isFocusableTabIndex(control.getKeyEventTarget())); |
| 1073 |
|
| 1074 |
// Visible, disabled, non-focusable control becoming hidden. |
| 1075 |
control.getKeyEventTarget().focus(); |
| 1076 |
assertEquals( |
| 1077 |
'Control must not have dispatched FOCUS', 0, |
| 1078 |
getEventCount(control, goog.ui.Component.EventType.FOCUS)); |
| 1079 |
assertFalse('Control must not have keyboard focus', control.isFocused()); |
| 1080 |
control.setVisible(false); |
| 1081 |
assertFalse('Control must be hidden', control.isVisible()); |
| 1082 |
assertFalse( |
| 1083 |
'Control must not have a tab index', |
| 1084 |
goog.dom.isFocusableTabIndex(control.getKeyEventTarget())); |
| 1085 |
assertEquals( |
| 1086 |
'Control must have dispatched HIDE', 1, |
| 1087 |
getEventCount(control, goog.ui.Component.EventType.HIDE)); |
| 1088 |
assertEquals( |
| 1089 |
'Control must not have dispatched BLUR', 0, |
| 1090 |
getEventCount(control, goog.ui.Component.EventType.BLUR)); |
| 1091 |
} |
| 1092 |
|
| 1093 |
|
| 1094 |
/** |
| 1095 |
* Tests {@link goog.ui.Control#setVisible} for disabled focusable controls. |
| 1096 |
*/ |
| 1097 |
function testSetVisibleForDisabledFocusableControl() { |
| 1098 |
// Hidden, disabled, focusable control becoming visible. |
| 1099 |
control.setEnabled(false); |
| 1100 |
control.setSupportedState(goog.ui.Component.State.FOCUSED, true); |
| 1101 |
control.render(sandbox); |
| 1102 |
assertTrue('Control must be visible', control.isVisible()); |
| 1103 |
assertFalse( |
| 1104 |
'Control must not have a tab index', |
| 1105 |
goog.dom.isFocusableTabIndex(control.getKeyEventTarget())); |
| 1106 |
|
| 1107 |
// Visible, disabled, focusable control becoming hidden. |
| 1108 |
control.getKeyEventTarget().focus(); |
| 1109 |
assertEquals( |
| 1110 |
'Control must not have dispatched FOCUS', 0, |
| 1111 |
getEventCount(control, goog.ui.Component.EventType.FOCUS)); |
| 1112 |
assertFalse('Control must not have keyboard focus', control.isFocused()); |
| 1113 |
control.setVisible(false); |
| 1114 |
assertFalse('Control must be hidden', control.isVisible()); |
| 1115 |
assertFalse( |
| 1116 |
'Control must not have a tab index', |
| 1117 |
goog.dom.isFocusableTabIndex(control.getKeyEventTarget())); |
| 1118 |
assertEquals( |
| 1119 |
'Control must have dispatched HIDE', 1, |
| 1120 |
getEventCount(control, goog.ui.Component.EventType.HIDE)); |
| 1121 |
assertEquals( |
| 1122 |
'Control must not have dispatched BLUR', 0, |
| 1123 |
getEventCount(control, goog.ui.Component.EventType.BLUR)); |
| 1124 |
} |
| 1125 |
|
| 1126 |
|
| 1127 |
/** |
| 1128 |
* Tests {@link goog.ui.Control#setVisible} for enabled non-focusable |
| 1129 |
* controls. |
| 1130 |
*/ |
| 1131 |
function testSetVisibleForEnabledNonFocusableControl() { |
| 1132 |
// Hidden, enabled, non-focusable control becoming visible. |
| 1133 |
control.setEnabled(true); |
| 1134 |
control.setSupportedState(goog.ui.Component.State.FOCUSED, false); |
| 1135 |
control.render(sandbox); |
| 1136 |
assertTrue('Control must be visible', control.isVisible()); |
| 1137 |
assertFalse( |
| 1138 |
'Control must not have a tab index', |
| 1139 |
goog.dom.isFocusableTabIndex(control.getKeyEventTarget())); |
| 1140 |
|
| 1141 |
if (testFocus) { |
| 1142 |
// Visible, enabled, non-focusable control becoming hidden. |
| 1143 |
control.getKeyEventTarget().focus(); |
| 1144 |
assertEquals( |
| 1145 |
'Control must not have dispatched FOCUS', 0, |
| 1146 |
getEventCount(control, goog.ui.Component.EventType.FOCUS)); |
| 1147 |
assertFalse('Control must not have keyboard focus', control.isFocused()); |
| 1148 |
control.setVisible(false); |
| 1149 |
assertFalse('Control must be hidden', control.isVisible()); |
| 1150 |
assertFalse( |
| 1151 |
'Control must not have a tab index', |
| 1152 |
goog.dom.isFocusableTabIndex(control.getKeyEventTarget())); |
| 1153 |
assertEquals( |
| 1154 |
'Control must have dispatched HIDE', 1, |
| 1155 |
getEventCount(control, goog.ui.Component.EventType.HIDE)); |
| 1156 |
assertEquals( |
| 1157 |
'Control must not have dispatched BLUR', 0, |
| 1158 |
getEventCount(control, goog.ui.Component.EventType.BLUR)); |
| 1159 |
} |
| 1160 |
} |
| 1161 |
|
| 1162 |
|
| 1163 |
/** |
| 1164 |
* Tests {@link goog.ui.Control#setVisible} for enabled focusable controls. |
| 1165 |
*/ |
| 1166 |
function testSetVisibleForEnabledFocusableControl() { |
| 1167 |
// Hidden, enabled, focusable control becoming visible. |
| 1168 |
control.setEnabled(true); |
| 1169 |
control.setSupportedState(goog.ui.Component.State.FOCUSED, true); |
| 1170 |
control.render(sandbox); |
| 1171 |
assertTrue('Control must be visible', control.isVisible()); |
| 1172 |
|
| 1173 |
if (testFocus) { |
| 1174 |
// Expected to fail on Mac Safari prior to version 527. |
| 1175 |
expectedFailures.expectFailureFor(isMacSafari3()); |
| 1176 |
try { |
| 1177 |
// Mac Safari currently doesn't support tabIndex on arbitrary |
| 1178 |
// elements. |
| 1179 |
assertTrue( |
| 1180 |
'Control must have a tab index', |
| 1181 |
goog.dom.isFocusableTabIndex(control.getKeyEventTarget())); |
| 1182 |
} catch (e) { |
| 1183 |
expectedFailures.handleException(e); |
| 1184 |
} |
| 1185 |
|
| 1186 |
// Visible, enabled, focusable control becoming hidden. |
| 1187 |
control.getKeyEventTarget().focus(); |
| 1188 |
|
| 1189 |
// Expected to fail on IE. |
| 1190 |
expectedFailures.expectFailureFor(goog.userAgent.IE); |
| 1191 |
try { |
| 1192 |
// IE dispatches focus and blur events asynchronously! |
| 1193 |
assertEquals( |
| 1194 |
'Control must have dispatched FOCUS', 1, |
| 1195 |
getEventCount(control, goog.ui.Component.EventType.FOCUS)); |
| 1196 |
assertTrue('Control must have keyboard focus', control.isFocused()); |
| 1197 |
} catch (e) { |
| 1198 |
expectedFailures.handleException(e); |
| 1199 |
} |
| 1200 |
|
| 1201 |
control.setVisible(false); |
| 1202 |
assertFalse('Control must be hidden', control.isVisible()); |
| 1203 |
assertFalse( |
| 1204 |
'Control must not have a tab index', |
| 1205 |
goog.dom.isFocusableTabIndex(control.getKeyEventTarget())); |
| 1206 |
assertEquals( |
| 1207 |
'Control must have dispatched HIDE', 1, |
| 1208 |
getEventCount(control, goog.ui.Component.EventType.HIDE)); |
| 1209 |
|
| 1210 |
// Expected to fail on IE. |
| 1211 |
expectedFailures.expectFailureFor(goog.userAgent.IE); |
| 1212 |
try { |
| 1213 |
// IE dispatches focus and blur events asynchronously! |
| 1214 |
assertEquals( |
| 1215 |
'Control must have dispatched BLUR', 1, |
| 1216 |
getEventCount(control, goog.ui.Component.EventType.BLUR)); |
| 1217 |
assertFalse( |
| 1218 |
'Control must no longer have keyboard focus', control.isFocused()); |
| 1219 |
} catch (e) { |
| 1220 |
expectedFailures.handleException(e); |
| 1221 |
} |
| 1222 |
} |
| 1223 |
} |
| 1224 |
|
| 1225 |
|
| 1226 |
/** |
| 1227 |
* Tests {@link goog.ui.Control#isEnabled}. |
| 1228 |
*/ |
| 1229 |
function testIsEnabled() { |
| 1230 |
assertTrue('Controls must be enabled by default', control.isEnabled()); |
| 1231 |
} |
| 1232 |
|
| 1233 |
|
| 1234 |
/** |
| 1235 |
* Tests {@link goog.ui.Control#setEnabled}. |
| 1236 |
*/ |
| 1237 |
function testSetEnabled() { |
| 1238 |
control.render(sandbox); |
| 1239 |
control.setHighlighted(true); |
| 1240 |
control.setActive(true); |
| 1241 |
control.getKeyEventTarget().focus(); |
| 1242 |
|
| 1243 |
resetEventCount(); |
| 1244 |
|
| 1245 |
control.setEnabled(true); |
| 1246 |
assertTrue('No events must have been dispatched', noEventsDispatched()); |
| 1247 |
assertTrue('Control must be enabled', control.isEnabled()); |
| 1248 |
assertTrue('Control must be highlighted', control.isHighlighted()); |
| 1249 |
assertTrue('Control must be active', control.isActive()); |
| 1250 |
var elem = control.getElementStrict(); |
| 1251 |
assertTrue( |
| 1252 |
'Control element must not have aria-disabled', |
| 1253 |
goog.string.isEmptyOrWhitespace(aria.getState(elem, State.DISABLED))); |
| 1254 |
assertEquals( |
| 1255 |
'Control element must have a tabIndex of 0', 0, |
| 1256 |
goog.string.toNumber(elem.getAttribute('tabIndex') || '')); |
| 1257 |
|
| 1258 |
if (testFocus) { |
| 1259 |
// Expected to fail on IE and Mac Safari 3. IE calls focus handlers |
| 1260 |
// asynchronously, and Mac Safari 3 doesn't support keyboard focus. |
| 1261 |
expectedFailures.expectFailureFor(goog.userAgent.IE); |
| 1262 |
expectedFailures.expectFailureFor(isMacSafari3()); |
| 1263 |
try { |
| 1264 |
assertTrue('Control must be focused', control.isFocused()); |
| 1265 |
} catch (e) { |
| 1266 |
expectedFailures.handleException(e); |
| 1267 |
} |
| 1268 |
} |
| 1269 |
|
| 1270 |
resetEventCount(); |
| 1271 |
|
| 1272 |
control.setEnabled(false); |
| 1273 |
assertEquals( |
| 1274 |
'One DISABLE event must have been dispatched', 1, |
| 1275 |
getEventCount(control, goog.ui.Component.EventType.DISABLE)); |
| 1276 |
assertFalse('Control must be disabled', control.isEnabled()); |
| 1277 |
assertFalse('Control must not be highlighted', control.isHighlighted()); |
| 1278 |
assertFalse('Control must not be active', control.isActive()); |
| 1279 |
assertFalse('Control must not be focused', control.isFocused()); |
| 1280 |
assertEquals( |
| 1281 |
'Control element must have aria-disabled true', 'true', |
| 1282 |
aria.getState(control.getElementStrict(), State.DISABLED)); |
| 1283 |
assertNull( |
| 1284 |
'Control element must not have a tabIndex', |
| 1285 |
control.getElement().getAttribute('tabIndex')); |
| 1286 |
|
| 1287 |
control.setEnabled(true); |
| 1288 |
control.exitDocument(); |
| 1289 |
var cssClass = goog.getCssName(goog.ui.ControlRenderer.CSS_CLASS, 'disabled'); |
| 1290 |
var element = goog.dom.createDom(goog.dom.TagName.DIV, {tabIndex: 0}); |
| 1291 |
element.className = cssClass; |
| 1292 |
goog.dom.appendChild(sandbox, element); |
| 1293 |
control.decorate(element); |
| 1294 |
assertEquals( |
| 1295 |
'Control element must have aria-disabled true', 'true', |
| 1296 |
aria.getState(control.getElementStrict(), State.DISABLED)); |
| 1297 |
assertNull( |
| 1298 |
'Control element must not have a tabIndex', |
| 1299 |
control.getElement().getAttribute('tabIndex')); |
| 1300 |
control.setEnabled(true); |
| 1301 |
elem = control.getElementStrict(); |
| 1302 |
assertEquals( |
| 1303 |
'Control element must have aria-disabled false', 'false', |
| 1304 |
aria.getState(elem, State.DISABLED)); |
| 1305 |
assertEquals( |
| 1306 |
'Control element must have tabIndex 0', 0, |
| 1307 |
goog.string.toNumber(elem.getAttribute('tabIndex') || '')); |
| 1308 |
} |
| 1309 |
|
| 1310 |
|
| 1311 |
/** |
| 1312 |
* Tests {@link goog.ui.Control#setState} when using |
| 1313 |
* goog.ui.Component.State.DISABLED. |
| 1314 |
*/ |
| 1315 |
function testSetStateWithDisabled() { |
| 1316 |
control.render(sandbox); |
| 1317 |
control.setHighlighted(true); |
| 1318 |
control.setActive(true); |
| 1319 |
control.getKeyEventTarget().focus(); |
| 1320 |
|
| 1321 |
resetEventCount(); |
| 1322 |
|
| 1323 |
control.setState(goog.ui.Component.State.DISABLED, false); |
| 1324 |
assertTrue('No events must have been dispatched', noEventsDispatched()); |
| 1325 |
assertTrue('Control must be enabled', control.isEnabled()); |
| 1326 |
assertTrue('Control must be highlighted', control.isHighlighted()); |
| 1327 |
assertTrue('Control must be active', control.isActive()); |
| 1328 |
assertTrue( |
| 1329 |
'Control element must not have aria-disabled', |
| 1330 |
goog.string.isEmptyOrWhitespace( |
| 1331 |
aria.getState(control.getElementStrict(), State.DISABLED))); |
| 1332 |
assertEquals( |
| 1333 |
'Control element must have a tabIndex of 0', 0, |
| 1334 |
goog.string.toNumber( |
| 1335 |
control.getElement().getAttribute('tabIndex') || '')); |
| 1336 |
|
| 1337 |
if (testFocus) { |
| 1338 |
// Expected to fail on IE and Mac Safari 3. IE calls focus handlers |
| 1339 |
// asynchronously, and Mac Safari 3 doesn't support keyboard focus. |
| 1340 |
expectedFailures.expectFailureFor(goog.userAgent.IE); |
| 1341 |
expectedFailures.expectFailureFor(isMacSafari3()); |
| 1342 |
try { |
| 1343 |
assertTrue('Control must be focused', control.isFocused()); |
| 1344 |
} catch (e) { |
| 1345 |
expectedFailures.handleException(e); |
| 1346 |
} |
| 1347 |
} |
| 1348 |
|
| 1349 |
resetEventCount(); |
| 1350 |
|
| 1351 |
control.setState(goog.ui.Component.State.DISABLED, true); |
| 1352 |
assertEquals( |
| 1353 |
'One DISABLE event must have been dispatched', 1, |
| 1354 |
getEventCount(control, goog.ui.Component.EventType.DISABLE)); |
| 1355 |
assertFalse('Control must be disabled', control.isEnabled()); |
| 1356 |
assertFalse('Control must not be highlighted', control.isHighlighted()); |
| 1357 |
assertFalse('Control must not be active', control.isActive()); |
| 1358 |
assertFalse('Control must not be focused', control.isFocused()); |
| 1359 |
assertEquals( |
| 1360 |
'Control element must have aria-disabled true', 'true', |
| 1361 |
aria.getState(control.getElementStrict(), State.DISABLED)); |
| 1362 |
assertNull( |
| 1363 |
'Control element must not have a tabIndex', |
| 1364 |
control.getElement().getAttribute('tabIndex')); |
| 1365 |
|
| 1366 |
control.setState(goog.ui.Component.State.DISABLED, false); |
| 1367 |
control.exitDocument(); |
| 1368 |
var cssClass = goog.getCssName(goog.ui.ControlRenderer.CSS_CLASS, 'disabled'); |
| 1369 |
var element = goog.dom.createDom(goog.dom.TagName.DIV, {tabIndex: 0}); |
| 1370 |
element.className = cssClass; |
| 1371 |
goog.dom.appendChild(sandbox, element); |
| 1372 |
control.decorate(element); |
| 1373 |
assertEquals( |
| 1374 |
'Control element must have aria-disabled true', 'true', |
| 1375 |
aria.getState(control.getElementStrict(), State.DISABLED)); |
| 1376 |
assertNull( |
| 1377 |
'Control element must not have a tabIndex', |
| 1378 |
control.getElement().getAttribute('tabIndex')); |
| 1379 |
control.setState(goog.ui.Component.State.DISABLED, false); |
| 1380 |
elem = control.getElementStrict(); |
| 1381 |
assertEquals( |
| 1382 |
'Control element must have aria-disabled false', 'false', |
| 1383 |
aria.getState(elem, State.DISABLED)); |
| 1384 |
assertEquals( |
| 1385 |
'Control element must have tabIndex 0', 0, |
| 1386 |
goog.string.toNumber(elem.getAttribute('tabIndex') || '')); |
| 1387 |
} |
| 1388 |
|
| 1389 |
|
| 1390 |
/** |
| 1391 |
* Tests {@link goog.ui.Control#setEnabled} when the control has a parent. |
| 1392 |
*/ |
| 1393 |
function testSetEnabledWithParent() { |
| 1394 |
var child = new goog.ui.Control(null); |
| 1395 |
child.setDispatchTransitionEvents(goog.ui.Component.State.ALL, true); |
| 1396 |
control.addChild(child, true /* opt_render */); |
| 1397 |
control.setEnabled(false); |
| 1398 |
|
| 1399 |
resetEventCount(); |
| 1400 |
|
| 1401 |
assertFalse('Parent must be disabled', control.isEnabled()); |
| 1402 |
assertTrue('Child must be enabled', child.isEnabled()); |
| 1403 |
|
| 1404 |
child.setEnabled(false); |
| 1405 |
assertTrue( |
| 1406 |
'No events must have been dispatched when child is disabled', |
| 1407 |
noEventsDispatched()); |
| 1408 |
assertTrue('Child must still be enabled', child.isEnabled()); |
| 1409 |
|
| 1410 |
resetEventCount(); |
| 1411 |
|
| 1412 |
control.setEnabled(true); |
| 1413 |
assertEquals( |
| 1414 |
'One ENABLE event must have been dispatched by the parent', 1, |
| 1415 |
getEventCount(control, goog.ui.Component.EventType.ENABLE)); |
| 1416 |
assertTrue('Parent must be enabled', control.isEnabled()); |
| 1417 |
assertTrue('Child must still be enabled', child.isEnabled()); |
| 1418 |
|
| 1419 |
resetEventCount(); |
| 1420 |
|
| 1421 |
child.setEnabled(false); |
| 1422 |
assertEquals( |
| 1423 |
'One DISABLE event must have been dispatched by the child', 1, |
| 1424 |
getEventCount(child, goog.ui.Component.EventType.DISABLE)); |
| 1425 |
assertTrue('Parent must still be enabled', control.isEnabled()); |
| 1426 |
assertFalse('Child must now be disabled', child.isEnabled()); |
| 1427 |
|
| 1428 |
resetEventCount(); |
| 1429 |
|
| 1430 |
control.setEnabled(false); |
| 1431 |
assertEquals( |
| 1432 |
'One DISABLE event must have been dispatched by the parent', 1, |
| 1433 |
getEventCount(control, goog.ui.Component.EventType.DISABLE)); |
| 1434 |
assertFalse('Parent must now be disabled', control.isEnabled()); |
| 1435 |
assertFalse('Child must still be disabled', child.isEnabled()); |
| 1436 |
|
| 1437 |
child.dispose(); |
| 1438 |
} |
| 1439 |
|
| 1440 |
|
| 1441 |
/** |
| 1442 |
* Tests {@link goog.ui.Control#isHighlighted}. |
| 1443 |
*/ |
| 1444 |
function testIsHighlighted() { |
| 1445 |
assertFalse( |
| 1446 |
'Controls must not be highlighted by default', control.isHighlighted()); |
| 1447 |
} |
| 1448 |
|
| 1449 |
|
| 1450 |
/** |
| 1451 |
* Tests {@link goog.ui.Control#setHighlighted}. |
| 1452 |
*/ |
| 1453 |
function testSetHighlighted() { |
| 1454 |
control.setSupportedState(goog.ui.Component.State.HOVER, false); |
| 1455 |
|
| 1456 |
control.setHighlighted(true); |
| 1457 |
assertFalse( |
| 1458 |
'Control must not be highlighted, because it isn\'t ' + |
| 1459 |
'highlightable', |
| 1460 |
control.isHighlighted()); |
| 1461 |
assertTrue( |
| 1462 |
'Control must not have dispatched any events', noEventsDispatched()); |
| 1463 |
|
| 1464 |
control.setSupportedState(goog.ui.Component.State.HOVER, true); |
| 1465 |
|
| 1466 |
control.setHighlighted(true); |
| 1467 |
assertTrue('Control must be highlighted', control.isHighlighted()); |
| 1468 |
assertEquals( |
| 1469 |
'Control must have dispatched a HIGHLIGHT event', 1, |
| 1470 |
getEventCount(control, goog.ui.Component.EventType.HIGHLIGHT)); |
| 1471 |
|
| 1472 |
control.setHighlighted(true); |
| 1473 |
assertTrue('Control must still be highlighted', control.isHighlighted()); |
| 1474 |
assertEquals( |
| 1475 |
'Control must not dispatch more HIGHLIGHT events', 1, |
| 1476 |
getEventCount(control, goog.ui.Component.EventType.HIGHLIGHT)); |
| 1477 |
|
| 1478 |
control.setHighlighted(false); |
| 1479 |
assertFalse('Control must not be highlighted', control.isHighlighted()); |
| 1480 |
assertEquals( |
| 1481 |
'Control must have dispatched an UNHIGHLIGHT event', 1, |
| 1482 |
getEventCount(control, goog.ui.Component.EventType.UNHIGHLIGHT)); |
| 1483 |
control.setEnabled(false); |
| 1484 |
assertFalse('Control must be disabled', control.isEnabled()); |
| 1485 |
|
| 1486 |
control.setHighlighted(true); |
| 1487 |
assertTrue( |
| 1488 |
'Control must be highlighted, even when disabled', |
| 1489 |
control.isHighlighted()); |
| 1490 |
assertEquals( |
| 1491 |
'Control must have dispatched another HIGHLIGHT event', 2, |
| 1492 |
getEventCount(control, goog.ui.Component.EventType.HIGHLIGHT)); |
| 1493 |
} |
| 1494 |
|
| 1495 |
|
| 1496 |
/** |
| 1497 |
* Tests {@link goog.ui.Control#isActive}. |
| 1498 |
*/ |
| 1499 |
function testIsActive() { |
| 1500 |
assertFalse('Controls must not be active by default', control.isActive()); |
| 1501 |
} |
| 1502 |
|
| 1503 |
|
| 1504 |
/** |
| 1505 |
* Tests {@link goog.ui.Control#setActive}. |
| 1506 |
*/ |
| 1507 |
function testSetActive() { |
| 1508 |
control.setSupportedState(goog.ui.Component.State.ACTIVE, false); |
| 1509 |
|
| 1510 |
control.setActive(true); |
| 1511 |
assertFalse( |
| 1512 |
'Control must not be active, because it isn\'t activateable', |
| 1513 |
control.isActive()); |
| 1514 |
assertTrue( |
| 1515 |
'Control must not have dispatched any events', noEventsDispatched()); |
| 1516 |
|
| 1517 |
control.setSupportedState(goog.ui.Component.State.ACTIVE, true); |
| 1518 |
|
| 1519 |
control.setActive(true); |
| 1520 |
assertTrue('Control must be active', control.isActive()); |
| 1521 |
assertEquals( |
| 1522 |
'Control must have dispatched an ACTIVATE event', 1, |
| 1523 |
getEventCount(control, goog.ui.Component.EventType.ACTIVATE)); |
| 1524 |
|
| 1525 |
control.setActive(true); |
| 1526 |
assertTrue('Control must still be active', control.isActive()); |
| 1527 |
assertEquals( |
| 1528 |
'Control must not dispatch more ACTIVATE events', 1, |
| 1529 |
getEventCount(control, goog.ui.Component.EventType.ACTIVATE)); |
| 1530 |
|
| 1531 |
control.setEnabled(false); |
| 1532 |
assertFalse('Control must be disabled', control.isEnabled()); |
| 1533 |
assertFalse('Control must not be active', control.isActive()); |
| 1534 |
assertEquals( |
| 1535 |
'Control must have dispatched a DEACTIVATE event', 1, |
| 1536 |
getEventCount(control, goog.ui.Component.EventType.DEACTIVATE)); |
| 1537 |
} |
| 1538 |
|
| 1539 |
|
| 1540 |
/** |
| 1541 |
* Tests disposing the control from an action event handler. |
| 1542 |
*/ |
| 1543 |
function testDisposeOnAction() { |
| 1544 |
goog.events.listen(control, goog.ui.Component.EventType.ACTION, function(e) { |
| 1545 |
control.dispose(); |
| 1546 |
}); |
| 1547 |
|
| 1548 |
// Control must not throw an exception if disposed of in an ACTION event |
| 1549 |
// handler. |
| 1550 |
control.performActionInternal(); |
| 1551 |
control.setActive(true); |
| 1552 |
assertTrue('Control should have been disposed of', control.isDisposed()); |
| 1553 |
} |
| 1554 |
|
| 1555 |
|
| 1556 |
/** |
| 1557 |
* Tests {@link goog.ui.Control#isSelected}. |
| 1558 |
*/ |
| 1559 |
function testIsSelected() { |
| 1560 |
assertFalse('Controls must not be selected by default', control.isSelected()); |
| 1561 |
} |
| 1562 |
|
| 1563 |
|
| 1564 |
/** |
| 1565 |
* Tests {@link goog.ui.Control#setSelected}. |
| 1566 |
*/ |
| 1567 |
function testSetSelected() { |
| 1568 |
control.setSupportedState(goog.ui.Component.State.SELECTED, false); |
| 1569 |
|
| 1570 |
control.setSelected(true); |
| 1571 |
assertFalse( |
| 1572 |
'Control must not be selected, because it isn\'t selectable', |
| 1573 |
control.isSelected()); |
| 1574 |
assertTrue( |
| 1575 |
'Control must not have dispatched any events', noEventsDispatched()); |
| 1576 |
|
| 1577 |
control.setSupportedState(goog.ui.Component.State.SELECTED, true); |
| 1578 |
|
| 1579 |
control.setSelected(true); |
| 1580 |
assertTrue('Control must be selected', control.isSelected()); |
| 1581 |
assertEquals( |
| 1582 |
'Control must have dispatched a SELECT event', 1, |
| 1583 |
getEventCount(control, goog.ui.Component.EventType.SELECT)); |
| 1584 |
|
| 1585 |
control.setSelected(true); |
| 1586 |
assertTrue('Control must still be selected', control.isSelected()); |
| 1587 |
assertEquals( |
| 1588 |
'Control must not dispatch more SELECT events', 1, |
| 1589 |
getEventCount(control, goog.ui.Component.EventType.SELECT)); |
| 1590 |
|
| 1591 |
control.setSelected(false); |
| 1592 |
assertFalse('Control must not be selected', control.isSelected()); |
| 1593 |
assertEquals( |
| 1594 |
'Control must have dispatched an UNSELECT event', 1, |
| 1595 |
getEventCount(control, goog.ui.Component.EventType.UNSELECT)); |
| 1596 |
control.setEnabled(false); |
| 1597 |
assertFalse('Control must be disabled', control.isEnabled()); |
| 1598 |
|
| 1599 |
control.setSelected(true); |
| 1600 |
assertTrue( |
| 1601 |
'Control must be selected, even when disabled', control.isSelected()); |
| 1602 |
assertEquals( |
| 1603 |
'Control must have dispatched another SELECT event', 2, |
| 1604 |
getEventCount(control, goog.ui.Component.EventType.SELECT)); |
| 1605 |
} |
| 1606 |
|
| 1607 |
|
| 1608 |
/** |
| 1609 |
* Tests {@link goog.ui.Control#isChecked}. |
| 1610 |
*/ |
| 1611 |
function testIsChecked() { |
| 1612 |
assertFalse('Controls must not be checked by default', control.isChecked()); |
| 1613 |
} |
| 1614 |
|
| 1615 |
|
| 1616 |
/** |
| 1617 |
* Tests {@link goog.ui.Control#setChecked}. |
| 1618 |
*/ |
| 1619 |
function testSetChecked() { |
| 1620 |
control.setSupportedState(goog.ui.Component.State.CHECKED, false); |
| 1621 |
|
| 1622 |
control.setChecked(true); |
| 1623 |
assertFalse( |
| 1624 |
'Control must not be checked, because it isn\'t checkable', |
| 1625 |
control.isChecked()); |
| 1626 |
assertTrue( |
| 1627 |
'Control must not have dispatched any events', noEventsDispatched()); |
| 1628 |
|
| 1629 |
control.setSupportedState(goog.ui.Component.State.CHECKED, true); |
| 1630 |
|
| 1631 |
control.setChecked(true); |
| 1632 |
assertTrue('Control must be checked', control.isChecked()); |
| 1633 |
assertEquals( |
| 1634 |
'Control must have dispatched a CHECK event', 1, |
| 1635 |
getEventCount(control, goog.ui.Component.EventType.CHECK)); |
| 1636 |
|
| 1637 |
control.setChecked(true); |
| 1638 |
assertTrue('Control must still be checked', control.isChecked()); |
| 1639 |
assertEquals( |
| 1640 |
'Control must not dispatch more CHECK events', 1, |
| 1641 |
getEventCount(control, goog.ui.Component.EventType.CHECK)); |
| 1642 |
|
| 1643 |
control.setChecked(false); |
| 1644 |
assertFalse('Control must not be checked', control.isChecked()); |
| 1645 |
assertEquals( |
| 1646 |
'Control must have dispatched an UNCHECK event', 1, |
| 1647 |
getEventCount(control, goog.ui.Component.EventType.UNCHECK)); |
| 1648 |
control.setEnabled(false); |
| 1649 |
assertFalse('Control must be disabled', control.isEnabled()); |
| 1650 |
|
| 1651 |
control.setChecked(true); |
| 1652 |
assertTrue( |
| 1653 |
'Control must be checked, even when disabled', control.isChecked()); |
| 1654 |
assertEquals( |
| 1655 |
'Control must have dispatched another CHECK event', 2, |
| 1656 |
getEventCount(control, goog.ui.Component.EventType.CHECK)); |
| 1657 |
} |
| 1658 |
|
| 1659 |
|
| 1660 |
/** |
| 1661 |
* Tests {@link goog.ui.Control#isFocused}. |
| 1662 |
*/ |
| 1663 |
function testIsFocused() { |
| 1664 |
assertFalse('Controls must not be focused by default', control.isFocused()); |
| 1665 |
} |
| 1666 |
|
| 1667 |
|
| 1668 |
/** |
| 1669 |
* Tests {@link goog.ui.Control#setFocused}. |
| 1670 |
*/ |
| 1671 |
function testSetFocused() { |
| 1672 |
control.setSupportedState(goog.ui.Component.State.FOCUSED, false); |
| 1673 |
|
| 1674 |
control.setFocused(true); |
| 1675 |
assertFalse( |
| 1676 |
'Control must not be focused, because it isn\'t focusable', |
| 1677 |
control.isFocused()); |
| 1678 |
assertTrue( |
| 1679 |
'Control must not have dispatched any events', noEventsDispatched()); |
| 1680 |
|
| 1681 |
control.setSupportedState(goog.ui.Component.State.FOCUSED, true); |
| 1682 |
|
| 1683 |
control.setFocused(true); |
| 1684 |
assertTrue('Control must be focused', control.isFocused()); |
| 1685 |
assertEquals( |
| 1686 |
'Control must have dispatched a FOCUS event', 1, |
| 1687 |
getEventCount(control, goog.ui.Component.EventType.FOCUS)); |
| 1688 |
|
| 1689 |
control.setFocused(true); |
| 1690 |
assertTrue('Control must still be focused', control.isFocused()); |
| 1691 |
assertEquals( |
| 1692 |
'Control must not dispatch more FOCUS events', 1, |
| 1693 |
getEventCount(control, goog.ui.Component.EventType.FOCUS)); |
| 1694 |
|
| 1695 |
control.setFocused(false); |
| 1696 |
assertFalse('Control must not be focused', control.isFocused()); |
| 1697 |
assertEquals( |
| 1698 |
'Control must have dispatched an BLUR event', 1, |
| 1699 |
getEventCount(control, goog.ui.Component.EventType.BLUR)); |
| 1700 |
control.setEnabled(false); |
| 1701 |
assertFalse('Control must be disabled', control.isEnabled()); |
| 1702 |
|
| 1703 |
control.setFocused(true); |
| 1704 |
assertTrue( |
| 1705 |
'Control must be focused, even when disabled', control.isFocused()); |
| 1706 |
assertEquals( |
| 1707 |
'Control must have dispatched another FOCUS event', 2, |
| 1708 |
getEventCount(control, goog.ui.Component.EventType.FOCUS)); |
| 1709 |
} |
| 1710 |
|
| 1711 |
|
| 1712 |
/** |
| 1713 |
* Tests {@link goog.ui.Control#isOpen}. |
| 1714 |
*/ |
| 1715 |
function testIsOpen() { |
| 1716 |
assertFalse('Controls must not be open by default', control.isOpen()); |
| 1717 |
} |
| 1718 |
|
| 1719 |
|
| 1720 |
/** |
| 1721 |
* Tests {@link goog.ui.Control#setOpen}. |
| 1722 |
*/ |
| 1723 |
function testSetOpen() { |
| 1724 |
control.setSupportedState(goog.ui.Component.State.OPENED, false); |
| 1725 |
|
| 1726 |
control.setOpen(true); |
| 1727 |
assertFalse( |
| 1728 |
'Control must not be opened, because it isn\'t openable', |
| 1729 |
control.isOpen()); |
| 1730 |
assertTrue( |
| 1731 |
'Control must not have dispatched any events', noEventsDispatched()); |
| 1732 |
|
| 1733 |
control.setSupportedState(goog.ui.Component.State.OPENED, true); |
| 1734 |
|
| 1735 |
control.setOpen(true); |
| 1736 |
assertTrue('Control must be opened', control.isOpen()); |
| 1737 |
assertEquals( |
| 1738 |
'Control must have dispatched a OPEN event', 1, |
| 1739 |
getEventCount(control, goog.ui.Component.EventType.OPEN)); |
| 1740 |
|
| 1741 |
control.setOpen(true); |
| 1742 |
assertTrue('Control must still be opened', control.isOpen()); |
| 1743 |
assertEquals( |
| 1744 |
'Control must not dispatch more OPEN events', 1, |
| 1745 |
getEventCount(control, goog.ui.Component.EventType.OPEN)); |
| 1746 |
|
| 1747 |
control.setOpen(false); |
| 1748 |
assertFalse('Control must not be opened', control.isOpen()); |
| 1749 |
assertEquals( |
| 1750 |
'Control must have dispatched an CLOSE event', 1, |
| 1751 |
getEventCount(control, goog.ui.Component.EventType.CLOSE)); |
| 1752 |
control.setEnabled(false); |
| 1753 |
assertFalse('Control must be disabled', control.isEnabled()); |
| 1754 |
|
| 1755 |
control.setOpen(true); |
| 1756 |
assertTrue('Control must be opened, even when disabled', control.isOpen()); |
| 1757 |
assertEquals( |
| 1758 |
'Control must have dispatched another OPEN event', 2, |
| 1759 |
getEventCount(control, goog.ui.Component.EventType.OPEN)); |
| 1760 |
} |
| 1761 |
|
| 1762 |
|
| 1763 |
/** |
| 1764 |
* Tests {@link goog.ui.Control#getState}. |
| 1765 |
*/ |
| 1766 |
function testGetState() { |
| 1767 |
assertEquals( |
| 1768 |
'Controls must be in the default state', 0x00, control.getState()); |
| 1769 |
} |
| 1770 |
|
| 1771 |
|
| 1772 |
/** |
| 1773 |
* Tests {@link goog.ui.Control#hasState}. |
| 1774 |
*/ |
| 1775 |
function testHasState() { |
| 1776 |
assertFalse( |
| 1777 |
'Control must not be disabled', |
| 1778 |
control.hasState(goog.ui.Component.State.DISABLED)); |
| 1779 |
assertFalse( |
| 1780 |
'Control must not be in the HOVER state', |
| 1781 |
control.hasState(goog.ui.Component.State.HOVER)); |
| 1782 |
assertFalse( |
| 1783 |
'Control must not be active', |
| 1784 |
control.hasState(goog.ui.Component.State.ACTIVE)); |
| 1785 |
assertFalse( |
| 1786 |
'Control must not be selected', |
| 1787 |
control.hasState(goog.ui.Component.State.SELECTED)); |
| 1788 |
assertFalse( |
| 1789 |
'Control must not be checked', |
| 1790 |
control.hasState(goog.ui.Component.State.CHECKED)); |
| 1791 |
assertFalse( |
| 1792 |
'Control must not be focused', |
| 1793 |
control.hasState(goog.ui.Component.State.FOCUSED)); |
| 1794 |
assertFalse( |
| 1795 |
'Control must not be open', |
| 1796 |
control.hasState(goog.ui.Component.State.OPEN)); |
| 1797 |
} |
| 1798 |
|
| 1799 |
|
| 1800 |
/** |
| 1801 |
* Tests {@link goog.ui.Control#setState}. |
| 1802 |
*/ |
| 1803 |
function testSetState() { |
| 1804 |
control.createDom(); |
| 1805 |
control.setSupportedState(goog.ui.Component.State.ACTIVE, false); |
| 1806 |
|
| 1807 |
assertFalse( |
| 1808 |
'Control must not be active', |
| 1809 |
control.hasState(goog.ui.Component.State.ACTIVE)); |
| 1810 |
control.setState(goog.ui.Component.State.ACTIVE, true); |
| 1811 |
assertFalse( |
| 1812 |
'Control must still be inactive (because it doesn\'t ' + |
| 1813 |
'support the ACTIVE state)', |
| 1814 |
control.hasState(goog.ui.Component.State.ACTIVE)); |
| 1815 |
|
| 1816 |
control.setSupportedState(goog.ui.Component.State.ACTIVE, true); |
| 1817 |
|
| 1818 |
control.setState(goog.ui.Component.State.ACTIVE, true); |
| 1819 |
assertTrue( |
| 1820 |
'Control must be active', |
| 1821 |
control.hasState(goog.ui.Component.State.ACTIVE)); |
| 1822 |
assertTrue( |
| 1823 |
'Control must have the active CSS style', |
| 1824 |
goog.dom.classlist.contains(control.getElement(), 'goog-control-active')); |
| 1825 |
|
| 1826 |
control.setState(goog.ui.Component.State.ACTIVE, true); |
| 1827 |
assertTrue( |
| 1828 |
'Control must still be active', |
| 1829 |
control.hasState(goog.ui.Component.State.ACTIVE)); |
| 1830 |
assertTrue( |
| 1831 |
'Control must still have the active CSS style', |
| 1832 |
goog.dom.classlist.contains(control.getElement(), 'goog-control-active')); |
| 1833 |
|
| 1834 |
assertTrue('No events must have been dispatched', noEventsDispatched()); |
| 1835 |
} |
| 1836 |
|
| 1837 |
|
| 1838 |
/** |
| 1839 |
* Tests {@link goog.ui.Control#setStateInternal}. |
| 1840 |
*/ |
| 1841 |
function testSetStateInternal() { |
| 1842 |
control.setStateInternal(0x00); |
| 1843 |
assertEquals('State should be 0x00', 0x00, control.getState()); |
| 1844 |
control.setStateInternal(0x17); |
| 1845 |
assertEquals('State should be 0x17', 0x17, control.getState()); |
| 1846 |
} |
| 1847 |
|
| 1848 |
|
| 1849 |
/** |
| 1850 |
* Tests {@link goog.ui.Control#isSupportedState}. |
| 1851 |
*/ |
| 1852 |
function testIsSupportedState() { |
| 1853 |
assertTrue( |
| 1854 |
'Control must support DISABLED', |
| 1855 |
control.isSupportedState(goog.ui.Component.State.DISABLED)); |
| 1856 |
assertTrue( |
| 1857 |
'Control must support HOVER', |
| 1858 |
control.isSupportedState(goog.ui.Component.State.HOVER)); |
| 1859 |
assertTrue( |
| 1860 |
'Control must support ACTIVE', |
| 1861 |
control.isSupportedState(goog.ui.Component.State.ACTIVE)); |
| 1862 |
assertTrue( |
| 1863 |
'Control must support FOCUSED', |
| 1864 |
control.isSupportedState(goog.ui.Component.State.FOCUSED)); |
| 1865 |
assertFalse( |
| 1866 |
'Control must no support SELECTED', |
| 1867 |
control.isSupportedState(goog.ui.Component.State.SELECTED)); |
| 1868 |
assertFalse( |
| 1869 |
'Control must no support CHECKED', |
| 1870 |
control.isSupportedState(goog.ui.Component.State.CHECKED)); |
| 1871 |
assertFalse( |
| 1872 |
'Control must no support OPENED', |
| 1873 |
control.isSupportedState(goog.ui.Component.State.OPENED)); |
| 1874 |
} |
| 1875 |
|
| 1876 |
|
| 1877 |
/** |
| 1878 |
* Tests {@link goog.ui.Control#setSupportedState}. |
| 1879 |
*/ |
| 1880 |
function testSetSupportedState() { |
| 1881 |
control.setSupportedState(goog.ui.Component.State.HOVER, true); |
| 1882 |
assertTrue( |
| 1883 |
'Control must still support HOVER', |
| 1884 |
control.isSupportedState(goog.ui.Component.State.HOVER)); |
| 1885 |
|
| 1886 |
control.setSupportedState(goog.ui.Component.State.HOVER, false); |
| 1887 |
assertFalse( |
| 1888 |
'Control must no longer support HOVER', |
| 1889 |
control.isSupportedState(goog.ui.Component.State.HOVER)); |
| 1890 |
|
| 1891 |
control.setState(goog.ui.Component.State.ACTIVE, true); |
| 1892 |
control.setSupportedState(goog.ui.Component.State.ACTIVE, false); |
| 1893 |
assertFalse( |
| 1894 |
'Control must no longer support ACTIVE', |
| 1895 |
control.isSupportedState(goog.ui.Component.State.ACTIVE)); |
| 1896 |
assertFalse( |
| 1897 |
'Control must no longer be in the ACTIVE state', |
| 1898 |
control.hasState(goog.ui.Component.State.ACTIVE)); |
| 1899 |
|
| 1900 |
control.render(sandbox); |
| 1901 |
|
| 1902 |
control.setSupportedState(goog.ui.Component.State.FOCUSED, true); |
| 1903 |
control.setState(goog.ui.Component.State.FOCUSED, true); |
| 1904 |
|
| 1905 |
assertThrows( |
| 1906 |
'Must not be able to disable support for the FOCUSED ' + |
| 1907 |
"state for a control that's already in the document and focused", |
| 1908 |
function() { |
| 1909 |
control.setSupportedState(goog.ui.Component.State.FOCUSED, false); |
| 1910 |
}); |
| 1911 |
|
| 1912 |
assertTrue('No events must have been dispatched', noEventsDispatched()); |
| 1913 |
} |
| 1914 |
|
| 1915 |
|
| 1916 |
/** |
| 1917 |
* Tests {@link goog.ui.Control#isAutoState}. |
| 1918 |
*/ |
| 1919 |
function testIsAutoState() { |
| 1920 |
assertTrue( |
| 1921 |
'Control must have DISABLED as an auto-state', |
| 1922 |
control.isAutoState(goog.ui.Component.State.DISABLED)); |
| 1923 |
assertTrue( |
| 1924 |
'Control must have HOVER as an auto-state', |
| 1925 |
control.isAutoState(goog.ui.Component.State.HOVER)); |
| 1926 |
assertTrue( |
| 1927 |
'Control must have ACTIVE as an auto-state', |
| 1928 |
control.isAutoState(goog.ui.Component.State.ACTIVE)); |
| 1929 |
assertTrue( |
| 1930 |
'Control must have FOCUSED as an auto-state', |
| 1931 |
control.isAutoState(goog.ui.Component.State.FOCUSED)); |
| 1932 |
|
| 1933 |
assertFalse( |
| 1934 |
'Control must not have SELECTED as an auto-state', |
| 1935 |
control.isAutoState(goog.ui.Component.State.SELECTED)); |
| 1936 |
assertFalse( |
| 1937 |
'Control must not have CHECKED as an auto-state', |
| 1938 |
control.isAutoState(goog.ui.Component.State.CHECKED)); |
| 1939 |
assertFalse( |
| 1940 |
'Control must not have OPENED as an auto-state', |
| 1941 |
control.isAutoState(goog.ui.Component.State.OPENED)); |
| 1942 |
|
| 1943 |
assertTrue('No events must have been dispatched', noEventsDispatched()); |
| 1944 |
} |
| 1945 |
|
| 1946 |
|
| 1947 |
/** |
| 1948 |
* Tests {@link goog.ui.Control#setAutoStates}. |
| 1949 |
*/ |
| 1950 |
function testSetAutoStates() { |
| 1951 |
control.setAutoStates(goog.ui.Component.State.HOVER, false); |
| 1952 |
assertFalse( |
| 1953 |
'Control must not have HOVER as an auto-state', |
| 1954 |
control.isAutoState(goog.ui.Component.State.HOVER)); |
| 1955 |
|
| 1956 |
control.setAutoStates( |
| 1957 |
goog.ui.Component.State.ACTIVE | goog.ui.Component.State.FOCUSED, false); |
| 1958 |
assertFalse( |
| 1959 |
'Control must not have ACTIVE as an auto-state', |
| 1960 |
control.isAutoState(goog.ui.Component.State.ACTIVE)); |
| 1961 |
assertFalse( |
| 1962 |
'Control must not have FOCUSED as an auto-state', |
| 1963 |
control.isAutoState(goog.ui.Component.State.FOCUSED)); |
| 1964 |
|
| 1965 |
control.setSupportedState(goog.ui.Component.State.FOCUSED, false); |
| 1966 |
control.setAutoStates(goog.ui.Component.State.FOCUSED, true); |
| 1967 |
assertFalse( |
| 1968 |
'Control must not have FOCUSED as an auto-state if it no ' + |
| 1969 |
'longer supports FOCUSED', |
| 1970 |
control.isAutoState(goog.ui.Component.State.FOCUSED)); |
| 1971 |
|
| 1972 |
assertTrue('No events must have been dispatched', noEventsDispatched()); |
| 1973 |
} |
| 1974 |
|
| 1975 |
|
| 1976 |
/** |
| 1977 |
* Tests {@link goog.ui.Control#isDispatchTransitionEvents}. |
| 1978 |
*/ |
| 1979 |
function testIsDispatchTransitionEvents() { |
| 1980 |
assertTrue( |
| 1981 |
'Control must dispatch DISABLED transition events', |
| 1982 |
control.isDispatchTransitionEvents(goog.ui.Component.State.DISABLED)); |
| 1983 |
assertTrue( |
| 1984 |
'Control must dispatch HOVER transition events', |
| 1985 |
control.isDispatchTransitionEvents(goog.ui.Component.State.HOVER)); |
| 1986 |
assertTrue( |
| 1987 |
'Control must dispatch ACTIVE transition events', |
| 1988 |
control.isDispatchTransitionEvents(goog.ui.Component.State.ACTIVE)); |
| 1989 |
assertTrue( |
| 1990 |
'Control must dispatch FOCUSED transition events', |
| 1991 |
control.isDispatchTransitionEvents(goog.ui.Component.State.FOCUSED)); |
| 1992 |
|
| 1993 |
assertFalse( |
| 1994 |
'Control must not dispatch SELECTED transition events', |
| 1995 |
control.isDispatchTransitionEvents(goog.ui.Component.State.SELECTED)); |
| 1996 |
assertFalse( |
| 1997 |
'Control must not dispatch CHECKED transition events', |
| 1998 |
control.isDispatchTransitionEvents(goog.ui.Component.State.CHECKED)); |
| 1999 |
assertFalse( |
| 2000 |
'Control must not dispatch OPENED transition events', |
| 2001 |
control.isDispatchTransitionEvents(goog.ui.Component.State.OPENED)); |
| 2002 |
|
| 2003 |
assertTrue('No events must have been dispatched', noEventsDispatched()); |
| 2004 |
} |
| 2005 |
|
| 2006 |
|
| 2007 |
/** |
| 2008 |
* Tests {@link goog.ui.Control#setDispatchTransitionEvents}. |
| 2009 |
*/ |
| 2010 |
function testSetDispatchTransitionEvents() { |
| 2011 |
control.setDispatchTransitionEvents(goog.ui.Component.State.HOVER, false); |
| 2012 |
assertFalse( |
| 2013 |
'Control must not dispatch HOVER transition events', |
| 2014 |
control.isDispatchTransitionEvents(goog.ui.Component.State.HOVER)); |
| 2015 |
|
| 2016 |
control.setSupportedState(goog.ui.Component.State.SELECTED, true); |
| 2017 |
control.setDispatchTransitionEvents(goog.ui.Component.State.SELECTED, true); |
| 2018 |
assertTrue( |
| 2019 |
'Control must dispatch SELECTED transition events', |
| 2020 |
control.isDispatchTransitionEvents(goog.ui.Component.State.SELECTED)); |
| 2021 |
|
| 2022 |
assertTrue('No events must have been dispatched', noEventsDispatched()); |
| 2023 |
} |
| 2024 |
|
| 2025 |
|
| 2026 |
/** |
| 2027 |
* Tests {@link goog.ui.Control#isTransitionAllowed}. |
| 2028 |
*/ |
| 2029 |
function testIsTransitionAllowed() { |
| 2030 |
assertTrue( |
| 2031 |
'Control must support the HOVER state', |
| 2032 |
control.isSupportedState(goog.ui.Component.State.HOVER)); |
| 2033 |
assertFalse( |
| 2034 |
'Control must not be in the HOVER state', |
| 2035 |
control.hasState(goog.ui.Component.State.HOVER)); |
| 2036 |
assertTrue( |
| 2037 |
'Control must dispatch HOVER transition events', |
| 2038 |
control.isDispatchTransitionEvents(goog.ui.Component.State.HOVER)); |
| 2039 |
|
| 2040 |
assertTrue( |
| 2041 |
'Control must be allowed to transition to the HOVER state', |
| 2042 |
control.isTransitionAllowed(goog.ui.Component.State.HOVER, true)); |
| 2043 |
assertEquals( |
| 2044 |
'Control must have dispatched one HIGHLIGHT event', 1, |
| 2045 |
getEventCount(control, goog.ui.Component.EventType.HIGHLIGHT)); |
| 2046 |
assertFalse( |
| 2047 |
'Control must not be highlighted', |
| 2048 |
control.hasState(goog.ui.Component.State.HOVER)); |
| 2049 |
|
| 2050 |
control.setState(goog.ui.Component.State.HOVER, true); |
| 2051 |
control.setDispatchTransitionEvents(goog.ui.Component.State.HOVER, false); |
| 2052 |
|
| 2053 |
assertTrue( |
| 2054 |
'Control must be allowed to transition from the HOVER state', |
| 2055 |
control.isTransitionAllowed(goog.ui.Component.State.HOVER, false)); |
| 2056 |
assertEquals( |
| 2057 |
'Control must not have dispatched any UNHIGHLIGHT events', 0, |
| 2058 |
getEventCount(control, goog.ui.Component.EventType.UNHIGHLIGHT)); |
| 2059 |
assertTrue( |
| 2060 |
'Control must still be highlighted', |
| 2061 |
control.hasState(goog.ui.Component.State.HOVER)); |
| 2062 |
|
| 2063 |
control.setSupportedState(goog.ui.Component.State.FOCUSED, false); |
| 2064 |
resetEventCount(); |
| 2065 |
|
| 2066 |
assertFalse( |
| 2067 |
'Control doesn\'t support the FOCUSED state', |
| 2068 |
control.isSupportedState(goog.ui.Component.State.FOCUSED)); |
| 2069 |
assertFalse( |
| 2070 |
'Control must not be FOCUSED', |
| 2071 |
control.hasState(goog.ui.Component.State.FOCUSED)); |
| 2072 |
assertFalse( |
| 2073 |
'Control must not be allowed to transition to the FOCUSED ' + |
| 2074 |
'state', |
| 2075 |
control.isTransitionAllowed(goog.ui.Component.State.FOCUSED, true)); |
| 2076 |
assertEquals( |
| 2077 |
'Control must not have dispatched any FOCUS events', 0, |
| 2078 |
getEventCount(control, goog.ui.Component.EventType.FOCUS)); |
| 2079 |
|
| 2080 |
control.setEnabled(false); |
| 2081 |
resetEventCount(); |
| 2082 |
|
| 2083 |
assertTrue( |
| 2084 |
'Control must support the DISABLED state', |
| 2085 |
control.isSupportedState(goog.ui.Component.State.DISABLED)); |
| 2086 |
assertTrue( |
| 2087 |
'Control must be DISABLED', |
| 2088 |
control.hasState(goog.ui.Component.State.DISABLED)); |
| 2089 |
assertFalse( |
| 2090 |
'Control must not be allowed to transition to the DISABLED ' + |
| 2091 |
'state, because it is already there', |
| 2092 |
control.isTransitionAllowed(goog.ui.Component.State.DISABLED, true)); |
| 2093 |
assertEquals( |
| 2094 |
'Control must not have dispatched any ENABLE events', 0, |
| 2095 |
getEventCount(control, goog.ui.Component.EventType.ENABLE)); |
| 2096 |
} |
| 2097 |
|
| 2098 |
|
| 2099 |
/** |
| 2100 |
* Tests {@link goog.ui.Control#handleKeyEvent}. |
| 2101 |
*/ |
| 2102 |
function testHandleKeyEvent() { |
| 2103 |
control.render(); |
| 2104 |
control.isVisible = control.isEnabled = function() { return true; }; |
| 2105 |
|
| 2106 |
|
| 2107 |
goog.testing.events.fireKeySequence( |
| 2108 |
control.getKeyEventTarget(), goog.events.KeyCodes.A); |
| 2109 |
|
| 2110 |
assertEquals( |
| 2111 |
'Control must not have dispatched an ACTION event', 0, |
| 2112 |
getEventCount(control, goog.ui.Component.EventType.ACTION)); |
| 2113 |
|
| 2114 |
goog.testing.events.fireKeySequence( |
| 2115 |
control.getKeyEventTarget(), goog.events.KeyCodes.ENTER); |
| 2116 |
assertEquals( |
| 2117 |
'Control must have dispatched an ACTION event', 1, |
| 2118 |
getEventCount(control, goog.ui.Component.EventType.ACTION)); |
| 2119 |
} |
| 2120 |
|
| 2121 |
|
| 2122 |
/** |
| 2123 |
* Tests {@link goog.ui.Control#performActionInternal}. |
| 2124 |
*/ |
| 2125 |
function testPerformActionInternal() { |
| 2126 |
assertFalse('Control must not be checked', control.isChecked()); |
| 2127 |
assertFalse('Control must not be selected', control.isSelected()); |
| 2128 |
assertFalse('Control must not be open', control.isOpen()); |
| 2129 |
|
| 2130 |
control.performActionInternal(); |
| 2131 |
|
| 2132 |
assertFalse('Control must not be checked', control.isChecked()); |
| 2133 |
assertFalse('Control must not be selected', control.isSelected()); |
| 2134 |
assertFalse('Control must not be open', control.isOpen()); |
| 2135 |
assertEquals( |
| 2136 |
'Control must have dispatched an ACTION event', 1, |
| 2137 |
getEventCount(control, goog.ui.Component.EventType.ACTION)); |
| 2138 |
|
| 2139 |
control.setSupportedState(goog.ui.Component.State.CHECKED, true); |
| 2140 |
control.setSupportedState(goog.ui.Component.State.SELECTED, true); |
| 2141 |
control.setSupportedState(goog.ui.Component.State.OPENED, true); |
| 2142 |
|
| 2143 |
control.performActionInternal(); |
| 2144 |
|
| 2145 |
assertTrue('Control must be checked', control.isChecked()); |
| 2146 |
assertTrue('Control must be selected', control.isSelected()); |
| 2147 |
assertTrue('Control must be open', control.isOpen()); |
| 2148 |
assertEquals( |
| 2149 |
'Control must have dispatched a CHECK event', 1, |
| 2150 |
getEventCount(control, goog.ui.Component.EventType.CHECK)); |
| 2151 |
assertEquals( |
| 2152 |
'Control must have dispatched a SELECT event', 1, |
| 2153 |
getEventCount(control, goog.ui.Component.EventType.SELECT)); |
| 2154 |
assertEquals( |
| 2155 |
'Control must have dispatched a OPEN event', 1, |
| 2156 |
getEventCount(control, goog.ui.Component.EventType.OPEN)); |
| 2157 |
assertEquals( |
| 2158 |
'Control must have dispatched another ACTION event', 2, |
| 2159 |
getEventCount(control, goog.ui.Component.EventType.ACTION)); |
| 2160 |
|
| 2161 |
control.performActionInternal(); |
| 2162 |
|
| 2163 |
assertFalse('Control must not be checked', control.isChecked()); |
| 2164 |
assertTrue('Control must be selected', control.isSelected()); |
| 2165 |
assertFalse('Control must not be open', control.isOpen()); |
| 2166 |
assertEquals( |
| 2167 |
'Control must have dispatched an UNCHECK event', 1, |
| 2168 |
getEventCount(control, goog.ui.Component.EventType.UNCHECK)); |
| 2169 |
assertEquals( |
| 2170 |
'Control must not have dispatched an UNSELECT event', 0, |
| 2171 |
getEventCount(control, goog.ui.Component.EventType.UNSELECT)); |
| 2172 |
assertEquals( |
| 2173 |
'Control must have dispatched a CLOSE event', 1, |
| 2174 |
getEventCount(control, goog.ui.Component.EventType.CLOSE)); |
| 2175 |
assertEquals( |
| 2176 |
'Control must have dispatched another ACTION event', 3, |
| 2177 |
getEventCount(control, goog.ui.Component.EventType.ACTION)); |
| 2178 |
} |
| 2179 |
|
| 2180 |
|
| 2181 |
/** |
| 2182 |
* Tests {@link goog.ui.Control#handleMouseOver}. |
| 2183 |
*/ |
| 2184 |
function testHandleMouseOver() { |
| 2185 |
control.setContent( |
| 2186 |
goog.dom.createDom(goog.dom.TagName.SPAN, {id: 'caption'}, 'Hello')); |
| 2187 |
control.render(sandbox); |
| 2188 |
|
| 2189 |
var element = control.getElement(); |
| 2190 |
var caption = goog.dom.getElement('caption'); |
| 2191 |
|
| 2192 |
// Verify baseline assumptions. |
| 2193 |
assertTrue( |
| 2194 |
'Caption must be contained within the control', |
| 2195 |
goog.dom.contains(element, caption)); |
| 2196 |
assertTrue('Control must be enabled', control.isEnabled()); |
| 2197 |
assertTrue( |
| 2198 |
'HOVER must be an auto-state', |
| 2199 |
control.isAutoState(goog.ui.Component.State.HOVER)); |
| 2200 |
assertFalse( |
| 2201 |
'Control must not start out highlighted', control.isHighlighted()); |
| 2202 |
|
| 2203 |
// Scenario 1: relatedTarget is contained within the control's DOM. |
| 2204 |
goog.testing.events.fireMouseOverEvent(element, caption); |
| 2205 |
assertTrue( |
| 2206 |
'No events must have been dispatched for internal mouse move', |
| 2207 |
noEventsDispatched()); |
| 2208 |
assertFalse( |
| 2209 |
'Control must not be highlighted for internal mouse move', |
| 2210 |
control.isHighlighted()); |
| 2211 |
resetEventCount(); |
| 2212 |
|
| 2213 |
// Scenario 2: preventDefault() is called on the ENTER event. |
| 2214 |
var key = goog.events.listen( |
| 2215 |
control, goog.ui.Component.EventType.ENTER, |
| 2216 |
function(e) { e.preventDefault(); }); |
| 2217 |
goog.testing.events.fireMouseOverEvent(element, sandbox); |
| 2218 |
assertEquals( |
| 2219 |
'Control must have dispatched 1 ENTER event', 1, |
| 2220 |
getEventCount(control, goog.ui.Component.EventType.ENTER)); |
| 2221 |
assertFalse( |
| 2222 |
'Control must not be highlighted if ENTER is canceled', |
| 2223 |
control.isHighlighted()); |
| 2224 |
goog.events.unlistenByKey(key); |
| 2225 |
resetEventCount(); |
| 2226 |
|
| 2227 |
// Scenario 3: Control is disabled. |
| 2228 |
control.setEnabled(false); |
| 2229 |
goog.testing.events.fireMouseOverEvent(element, sandbox); |
| 2230 |
assertEquals( |
| 2231 |
'Control must dispatch ENTER event on mouseover even if ' + |
| 2232 |
'disabled', |
| 2233 |
1, getEventCount(control, goog.ui.Component.EventType.ENTER)); |
| 2234 |
assertFalse( |
| 2235 |
'Control must not be highlighted if it is disabled', |
| 2236 |
control.isHighlighted()); |
| 2237 |
control.setEnabled(true); |
| 2238 |
resetEventCount(); |
| 2239 |
|
| 2240 |
// Scenario 4: HOVER is not an auto-state. |
| 2241 |
control.setAutoStates(goog.ui.Component.State.HOVER, false); |
| 2242 |
goog.testing.events.fireMouseOverEvent(element, sandbox); |
| 2243 |
assertEquals( |
| 2244 |
'Control must dispatch ENTER event on mouseover even if ' + |
| 2245 |
'HOVER is not an auto-state', |
| 2246 |
1, getEventCount(control, goog.ui.Component.EventType.ENTER)); |
| 2247 |
assertFalse( |
| 2248 |
'Control must not be highlighted if HOVER isn\'t an auto-' + |
| 2249 |
'state', |
| 2250 |
control.isHighlighted()); |
| 2251 |
control.setAutoStates(goog.ui.Component.State.HOVER, true); |
| 2252 |
resetEventCount(); |
| 2253 |
|
| 2254 |
// Scenario 5: All is well. |
| 2255 |
goog.testing.events.fireMouseOverEvent(element, sandbox); |
| 2256 |
assertEquals( |
| 2257 |
'Control must dispatch ENTER event on mouseover', 1, |
| 2258 |
getEventCount(control, goog.ui.Component.EventType.ENTER)); |
| 2259 |
assertEquals( |
| 2260 |
'Control must dispatch HIGHLIGHT event on mouseover', 1, |
| 2261 |
getEventCount(control, goog.ui.Component.EventType.HIGHLIGHT)); |
| 2262 |
assertTrue('Control must be highlighted', control.isHighlighted()); |
| 2263 |
resetEventCount(); |
| 2264 |
|
| 2265 |
// Scenario 6: relatedTarget is null |
| 2266 |
control.setHighlighted(false); |
| 2267 |
goog.testing.events.fireMouseOverEvent(element, null); |
| 2268 |
assertEquals( |
| 2269 |
'Control must dispatch ENTER event on mouseover', 1, |
| 2270 |
getEventCount(control, goog.ui.Component.EventType.ENTER)); |
| 2271 |
assertEquals( |
| 2272 |
'Control must dispatch HIGHLIGHT event on mouseover', 1, |
| 2273 |
getEventCount(control, goog.ui.Component.EventType.HIGHLIGHT)); |
| 2274 |
assertTrue('Control must be highlighted', control.isHighlighted()); |
| 2275 |
resetEventCount(); |
| 2276 |
} |
| 2277 |
|
| 2278 |
|
| 2279 |
/** |
| 2280 |
* Tests {@link goog.ui.Control#handleMouseOut}. |
| 2281 |
*/ |
| 2282 |
function testHandleMouseOut() { |
| 2283 |
control.setContent( |
| 2284 |
goog.dom.createDom(goog.dom.TagName.SPAN, {id: 'caption'}, 'Hello')); |
| 2285 |
control.setHighlighted(true); |
| 2286 |
control.setActive(true); |
| 2287 |
|
| 2288 |
resetEventCount(); |
| 2289 |
|
| 2290 |
control.render(sandbox); |
| 2291 |
|
| 2292 |
var element = control.getElement(); |
| 2293 |
var caption = goog.dom.getElement('caption'); |
| 2294 |
|
| 2295 |
// Verify baseline assumptions. |
| 2296 |
assertTrue( |
| 2297 |
'Caption must be contained within the control', |
| 2298 |
goog.dom.contains(element, caption)); |
| 2299 |
assertTrue('Control must be enabled', control.isEnabled()); |
| 2300 |
assertTrue( |
| 2301 |
'HOVER must be an auto-state', |
| 2302 |
control.isAutoState(goog.ui.Component.State.HOVER)); |
| 2303 |
assertTrue( |
| 2304 |
'ACTIVE must be an auto-state', |
| 2305 |
control.isAutoState(goog.ui.Component.State.ACTIVE)); |
| 2306 |
assertTrue('Control must start out highlighted', control.isHighlighted()); |
| 2307 |
assertTrue('Control must start out active', control.isActive()); |
| 2308 |
|
| 2309 |
// Scenario 1: relatedTarget is contained within the control's DOM. |
| 2310 |
goog.testing.events.fireMouseOutEvent(element, caption); |
| 2311 |
assertTrue( |
| 2312 |
'No events must have been dispatched for internal mouse move', |
| 2313 |
noEventsDispatched()); |
| 2314 |
assertTrue( |
| 2315 |
'Control must not be un-highlighted for internal mouse move', |
| 2316 |
control.isHighlighted()); |
| 2317 |
assertTrue( |
| 2318 |
'Control must not be deactivated for internal mouse move', |
| 2319 |
control.isActive()); |
| 2320 |
resetEventCount(); |
| 2321 |
|
| 2322 |
// Scenario 2: preventDefault() is called on the LEAVE event. |
| 2323 |
var key = goog.events.listen( |
| 2324 |
control, goog.ui.Component.EventType.LEAVE, |
| 2325 |
function(e) { e.preventDefault(); }); |
| 2326 |
goog.testing.events.fireMouseOutEvent(element, sandbox); |
| 2327 |
assertEquals( |
| 2328 |
'Control must have dispatched 1 LEAVE event', 1, |
| 2329 |
getEventCount(control, goog.ui.Component.EventType.LEAVE)); |
| 2330 |
assertTrue( |
| 2331 |
'Control must not be un-highlighted if LEAVE is canceled', |
| 2332 |
control.isHighlighted()); |
| 2333 |
assertTrue( |
| 2334 |
'Control must not be deactivated if LEAVE is canceled', |
| 2335 |
control.isActive()); |
| 2336 |
goog.events.unlistenByKey(key); |
| 2337 |
resetEventCount(); |
| 2338 |
|
| 2339 |
// Scenario 3: ACTIVE is not an auto-state. |
| 2340 |
control.setAutoStates(goog.ui.Component.State.ACTIVE, false); |
| 2341 |
goog.testing.events.fireMouseOutEvent(element, sandbox); |
| 2342 |
assertEquals( |
| 2343 |
'Control must dispatch LEAVE event on mouseout even if ' + |
| 2344 |
'ACTIVE is not an auto-state', |
| 2345 |
1, getEventCount(control, goog.ui.Component.EventType.LEAVE)); |
| 2346 |
assertTrue( |
| 2347 |
'Control must not be deactivated if ACTIVE isn\'t an auto-' + |
| 2348 |
'state', |
| 2349 |
control.isActive()); |
| 2350 |
assertFalse( |
| 2351 |
'Control must be un-highlighted even if ACTIVE isn\'t an ' + |
| 2352 |
'auto-state', |
| 2353 |
control.isHighlighted()); |
| 2354 |
control.setAutoStates(goog.ui.Component.State.ACTIVE, true); |
| 2355 |
control.setHighlighted(true); |
| 2356 |
resetEventCount(); |
| 2357 |
|
| 2358 |
// Scenario 4: HOVER is not an auto-state. |
| 2359 |
control.setAutoStates(goog.ui.Component.State.HOVER, false); |
| 2360 |
goog.testing.events.fireMouseOutEvent(element, sandbox); |
| 2361 |
assertEquals( |
| 2362 |
'Control must dispatch LEAVE event on mouseout even if ' + |
| 2363 |
'HOVER is not an auto-state', |
| 2364 |
1, getEventCount(control, goog.ui.Component.EventType.LEAVE)); |
| 2365 |
assertFalse( |
| 2366 |
'Control must be deactivated even if HOVER isn\'t an auto-' + |
| 2367 |
'state', |
| 2368 |
control.isActive()); |
| 2369 |
assertTrue( |
| 2370 |
'Control must not be un-highlighted if HOVER isn\'t an auto-' + |
| 2371 |
'state', |
| 2372 |
control.isHighlighted()); |
| 2373 |
control.setAutoStates(goog.ui.Component.State.HOVER, true); |
| 2374 |
control.setActive(true); |
| 2375 |
resetEventCount(); |
| 2376 |
|
| 2377 |
// Scenario 5: All is well. |
| 2378 |
goog.testing.events.fireMouseOutEvent(element, sandbox); |
| 2379 |
assertEquals( |
| 2380 |
'Control must dispatch LEAVE event on mouseout', 1, |
| 2381 |
getEventCount(control, goog.ui.Component.EventType.LEAVE)); |
| 2382 |
assertEquals( |
| 2383 |
'Control must dispatch DEACTIVATE event on mouseout', 1, |
| 2384 |
getEventCount(control, goog.ui.Component.EventType.DEACTIVATE)); |
| 2385 |
assertEquals( |
| 2386 |
'Control must dispatch UNHIGHLIGHT event on mouseout', 1, |
| 2387 |
getEventCount(control, goog.ui.Component.EventType.UNHIGHLIGHT)); |
| 2388 |
assertFalse('Control must be deactivated', control.isActive()); |
| 2389 |
assertFalse('Control must be unhighlighted', control.isHighlighted()); |
| 2390 |
resetEventCount(); |
| 2391 |
|
| 2392 |
// Scenario 6: relatedTarget is null |
| 2393 |
control.setActive(true); |
| 2394 |
control.setHighlighted(true); |
| 2395 |
goog.testing.events.fireMouseOutEvent(element, null); |
| 2396 |
assertEquals( |
| 2397 |
'Control must dispatch LEAVE event on mouseout', 1, |
| 2398 |
getEventCount(control, goog.ui.Component.EventType.LEAVE)); |
| 2399 |
assertEquals( |
| 2400 |
'Control must dispatch DEACTIVATE event on mouseout', 1, |
| 2401 |
getEventCount(control, goog.ui.Component.EventType.DEACTIVATE)); |
| 2402 |
assertEquals( |
| 2403 |
'Control must dispatch UNHIGHLIGHT event on mouseout', 1, |
| 2404 |
getEventCount(control, goog.ui.Component.EventType.UNHIGHLIGHT)); |
| 2405 |
assertFalse('Control must be deactivated', control.isActive()); |
| 2406 |
assertFalse('Control must be unhighlighted', control.isHighlighted()); |
| 2407 |
resetEventCount(); |
| 2408 |
} |
| 2409 |
|
| 2410 |
function testIsMouseEventWithinElement() { |
| 2411 |
var child = goog.dom.createElement(goog.dom.TagName.DIV); |
| 2412 |
var parent = goog.dom.createDom(goog.dom.TagName.DIV, null, child); |
| 2413 |
var notChild = goog.dom.createElement(goog.dom.TagName.DIV); |
| 2414 |
|
| 2415 |
var event = new goog.testing.events.Event('mouseout'); |
| 2416 |
event.relatedTarget = child; |
| 2417 |
assertTrue( |
| 2418 |
'Event is within element', |
| 2419 |
goog.ui.Control.isMouseEventWithinElement_(event, parent)); |
| 2420 |
|
| 2421 |
var event = new goog.testing.events.Event('mouseout'); |
| 2422 |
event.relatedTarget = notChild; |
| 2423 |
assertFalse( |
| 2424 |
'Event is not within element', |
| 2425 |
goog.ui.Control.isMouseEventWithinElement_(event, parent)); |
| 2426 |
} |
| 2427 |
|
| 2428 |
function testHandleMouseDown() { |
| 2429 |
control.render(sandbox); |
| 2430 |
assertFalse( |
| 2431 |
'preventDefault() must have been called for control that ' + |
| 2432 |
'doesn\'t support text selection', |
| 2433 |
fireMouseDownAndFocus(control.getElement())); |
| 2434 |
assertTrue('Control must be highlighted', control.isHighlighted()); |
| 2435 |
assertTrue('Control must be active', control.isActive()); |
| 2436 |
|
| 2437 |
if (testFocus) { |
| 2438 |
// Expected to fail on IE and Mac Safari 3. IE calls focus handlers |
| 2439 |
// asynchronously, and Mac Safari 3 doesn't support keyboard focus. |
| 2440 |
expectedFailures.expectFailureFor(goog.userAgent.IE); |
| 2441 |
expectedFailures.expectFailureFor(isMacSafari3()); |
| 2442 |
try { |
| 2443 |
assertTrue('Control must be focused', control.isFocused()); |
| 2444 |
} catch (e) { |
| 2445 |
expectedFailures.handleException(e); |
| 2446 |
} |
| 2447 |
} |
| 2448 |
} |
| 2449 |
|
| 2450 |
function testHandleMouseDownForDisabledControl() { |
| 2451 |
control.setEnabled(false); |
| 2452 |
control.render(sandbox); |
| 2453 |
assertFalse( |
| 2454 |
'preventDefault() must have been called for control that ' + |
| 2455 |
'doesn\'t support text selection', |
| 2456 |
fireMouseDownAndFocus(control.getElement())); |
| 2457 |
assertFalse('Control must not be highlighted', control.isHighlighted()); |
| 2458 |
assertFalse('Control must not be active', control.isActive()); |
| 2459 |
if (testFocus) { |
| 2460 |
assertFalse('Control must not be focused', control.isFocused()); |
| 2461 |
} |
| 2462 |
} |
| 2463 |
|
| 2464 |
function testHandleMouseDownForNoHoverAutoState() { |
| 2465 |
control.setAutoStates(goog.ui.Component.State.HOVER, false); |
| 2466 |
control.render(sandbox); |
| 2467 |
assertFalse( |
| 2468 |
'preventDefault() must have been called for control that ' + |
| 2469 |
'doesn\'t support text selection', |
| 2470 |
fireMouseDownAndFocus(control.getElement())); |
| 2471 |
assertFalse('Control must not be highlighted', control.isHighlighted()); |
| 2472 |
assertTrue('Control must be active', control.isActive()); |
| 2473 |
|
| 2474 |
if (testFocus) { |
| 2475 |
// Expected to fail on IE and Mac Safari 3. IE calls focus handlers |
| 2476 |
// asynchronously, and Mac Safari 3 doesn't support keyboard focus. |
| 2477 |
expectedFailures.expectFailureFor(goog.userAgent.IE); |
| 2478 |
expectedFailures.expectFailureFor(isMacSafari3()); |
| 2479 |
try { |
| 2480 |
assertTrue('Control must be focused', control.isFocused()); |
| 2481 |
} catch (e) { |
| 2482 |
expectedFailures.handleException(e); |
| 2483 |
} |
| 2484 |
} |
| 2485 |
} |
| 2486 |
|
| 2487 |
function testHandleMouseDownForRightMouseButton() { |
| 2488 |
control.render(sandbox); |
| 2489 |
assertTrue( |
| 2490 |
'preventDefault() must not have been called for right ' + |
| 2491 |
'mouse button', |
| 2492 |
fireMouseDownAndFocus( |
| 2493 |
control.getElement(), goog.events.BrowserEvent.MouseButton.RIGHT)); |
| 2494 |
assertTrue('Control must be highlighted', control.isHighlighted()); |
| 2495 |
assertFalse('Control must not be active', control.isActive()); |
| 2496 |
|
| 2497 |
if (testFocus) { |
| 2498 |
// Expected to fail on IE and Mac Safari 3. IE calls focus handlers |
| 2499 |
// asynchronously, and Mac Safari 3 doesn't support keyboard focus. |
| 2500 |
expectedFailures.expectFailureFor(goog.userAgent.IE); |
| 2501 |
expectedFailures.expectFailureFor(isMacSafari3()); |
| 2502 |
try { |
| 2503 |
assertTrue('Control must be focused', control.isFocused()); |
| 2504 |
} catch (e) { |
| 2505 |
expectedFailures.handleException(e); |
| 2506 |
} |
| 2507 |
} |
| 2508 |
} |
| 2509 |
|
| 2510 |
function testHandleMouseDownForNoActiveAutoState() { |
| 2511 |
control.setAutoStates(goog.ui.Component.State.ACTIVE, false); |
| 2512 |
control.render(sandbox); |
| 2513 |
assertFalse( |
| 2514 |
'preventDefault() must have been called for control that ' + |
| 2515 |
'doesn\'t support text selection', |
| 2516 |
fireMouseDownAndFocus(control.getElement())); |
| 2517 |
assertTrue('Control must be highlighted', control.isHighlighted()); |
| 2518 |
assertFalse('Control must not be active', control.isActive()); |
| 2519 |
|
| 2520 |
if (testFocus) { |
| 2521 |
// Expected to fail on IE and Mac Safari 3. IE calls focus handlers |
| 2522 |
// asynchronously, and Mac Safari 3 doesn't support keyboard focus. |
| 2523 |
expectedFailures.expectFailureFor(goog.userAgent.IE); |
| 2524 |
expectedFailures.expectFailureFor(isMacSafari3()); |
| 2525 |
try { |
| 2526 |
assertTrue('Control must be focused', control.isFocused()); |
| 2527 |
} catch (e) { |
| 2528 |
expectedFailures.handleException(e); |
| 2529 |
} |
| 2530 |
} |
| 2531 |
} |
| 2532 |
|
| 2533 |
function testHandleMouseDownForNonFocusableControl() { |
| 2534 |
control.setSupportedState(goog.ui.Component.State.FOCUSED, false); |
| 2535 |
control.render(sandbox); |
| 2536 |
assertFalse( |
| 2537 |
'preventDefault() must have been called for control that ' + |
| 2538 |
'doesn\'t support text selection', |
| 2539 |
fireMouseDownAndFocus(control.getElement())); |
| 2540 |
assertTrue('Control must be highlighted', control.isHighlighted()); |
| 2541 |
assertTrue('Control must be active', control.isActive()); |
| 2542 |
assertFalse('Control must not be focused', control.isFocused()); |
| 2543 |
} |
| 2544 |
|
| 2545 |
// TODO(attila): Find out why this is flaky on FF2/Linux and FF1.5/Win. |
| 2546 |
// function testHandleMouseDownForSelectableControl() { |
| 2547 |
// control.setAllowTextSelection(true); |
| 2548 |
// control.render(sandbox); |
| 2549 |
// assertTrue('preventDefault() must not have been called for control ' + |
| 2550 |
// 'that supports text selection', |
| 2551 |
// fireMouseDownAndFocus(control.getElement())); |
| 2552 |
// assertTrue('Control must be highlighted', control.isHighlighted()); |
| 2553 |
// assertTrue('Control must be active', control.isActive()); |
| 2554 |
// // Expected to fail on IE and Mac Safari 3. IE calls focus handlers |
| 2555 |
// // asynchronously, and Mac Safari 3 doesn't support keyboard focus. |
| 2556 |
// expectedFailures.expectFailureFor(goog.userAgent.IE); |
| 2557 |
// expectedFailures.expectFailureFor(isMacSafari3()); |
| 2558 |
// try { |
| 2559 |
// assertTrue('Control must be focused', control.isFocused()); |
| 2560 |
// } catch (e) { |
| 2561 |
// expectedFailures.handleException(e); |
| 2562 |
// } |
| 2563 |
//} |
| 2564 |
|
| 2565 |
|
| 2566 |
/** |
| 2567 |
* Tests {@link goog.ui.Control#handleMouseUp}. |
| 2568 |
*/ |
| 2569 |
function testHandleMouseUp() { |
| 2570 |
control.setActive(true); |
| 2571 |
|
| 2572 |
// Override performActionInternal() for testing purposes. |
| 2573 |
var actionPerformed = false; |
| 2574 |
control.performActionInternal = function() { |
| 2575 |
actionPerformed = true; |
| 2576 |
return true; |
| 2577 |
}; |
| 2578 |
|
| 2579 |
resetEventCount(); |
| 2580 |
|
| 2581 |
control.render(sandbox); |
| 2582 |
var element = control.getElement(); |
| 2583 |
|
| 2584 |
// Verify baseline assumptions. |
| 2585 |
assertTrue('Control must be enabled', control.isEnabled()); |
| 2586 |
assertTrue( |
| 2587 |
'HOVER must be an auto-state', |
| 2588 |
control.isAutoState(goog.ui.Component.State.HOVER)); |
| 2589 |
assertTrue( |
| 2590 |
'ACTIVE must be an auto-state', |
| 2591 |
control.isAutoState(goog.ui.Component.State.ACTIVE)); |
| 2592 |
assertFalse( |
| 2593 |
'Control must not start out highlighted', control.isHighlighted()); |
| 2594 |
assertTrue('Control must start out active', control.isActive()); |
| 2595 |
|
| 2596 |
// Scenario 1: Control is disabled. |
| 2597 |
control.setEnabled(false); |
| 2598 |
goog.testing.events.fireMouseUpEvent(element); |
| 2599 |
assertFalse( |
| 2600 |
'Disabled control must not highlight on mouseup', |
| 2601 |
control.isHighlighted()); |
| 2602 |
assertFalse('No action must have been performed', actionPerformed); |
| 2603 |
control.setActive(true); |
| 2604 |
control.setEnabled(true); |
| 2605 |
|
| 2606 |
// Scenario 2: HOVER is not an auto-state. |
| 2607 |
control.setAutoStates(goog.ui.Component.State.HOVER, false); |
| 2608 |
goog.testing.events.fireMouseUpEvent(element); |
| 2609 |
assertFalse( |
| 2610 |
'Control must not highlight on mouseup if HOVER isn\'t an ' + |
| 2611 |
'auto-state', |
| 2612 |
control.isHighlighted()); |
| 2613 |
assertTrue( |
| 2614 |
'Action must have been performed even if HOVER isn\'t an ' + |
| 2615 |
'auto-state', |
| 2616 |
actionPerformed); |
| 2617 |
assertFalse( |
| 2618 |
'Control must have been deactivated on mouseup even if ' + |
| 2619 |
'HOVER isn\'t an auto-state', |
| 2620 |
control.isActive()); |
| 2621 |
actionPerformed = false; |
| 2622 |
control.setActive(true); |
| 2623 |
control.setAutoStates(goog.ui.Component.State.HOVER, true); |
| 2624 |
|
| 2625 |
// Scenario 3: Control is not active. |
| 2626 |
control.setActive(false); |
| 2627 |
goog.testing.events.fireMouseUpEvent(element); |
| 2628 |
assertTrue( |
| 2629 |
'Control must highlight on mouseup, even if inactive', |
| 2630 |
control.isHighlighted()); |
| 2631 |
assertFalse( |
| 2632 |
'No action must have been performed if control is inactive', |
| 2633 |
actionPerformed); |
| 2634 |
assertFalse( |
| 2635 |
'Inactive control must remain inactive after mouseup', |
| 2636 |
control.isActive()); |
| 2637 |
control.setHighlighted(false); |
| 2638 |
control.setActive(true); |
| 2639 |
|
| 2640 |
// Scenario 4: performActionInternal() returns false. |
| 2641 |
control.performActionInternal = function() { |
| 2642 |
actionPerformed = true; |
| 2643 |
return false; |
| 2644 |
}; |
| 2645 |
goog.testing.events.fireMouseUpEvent(element); |
| 2646 |
assertTrue( |
| 2647 |
'Control must highlight on mouseup, even if no action is ' + |
| 2648 |
'performed', |
| 2649 |
control.isHighlighted()); |
| 2650 |
assertTrue('performActionInternal must have been called', actionPerformed); |
| 2651 |
assertTrue( |
| 2652 |
'Control must not deactivate if performActionInternal ' + |
| 2653 |
'returns false', |
| 2654 |
control.isActive()); |
| 2655 |
control.setHighlighted(false); |
| 2656 |
actionPerformed = false; |
| 2657 |
control.performActionInternal = function() { |
| 2658 |
actionPerformed = true; |
| 2659 |
return true; |
| 2660 |
}; |
| 2661 |
|
| 2662 |
// Scenario 5: ACTIVE is not an auto-state. |
| 2663 |
control.setAutoStates(goog.ui.Component.State.ACTIVE, false); |
| 2664 |
goog.testing.events.fireMouseUpEvent(element); |
| 2665 |
assertTrue( |
| 2666 |
'Control must highlight on mouseup even if ACTIVE isn\'t an ' + |
| 2667 |
'auto-state', |
| 2668 |
control.isHighlighted()); |
| 2669 |
assertTrue( |
| 2670 |
'Action must have been performed even if ACTIVE isn\'t an ' + |
| 2671 |
'auto-state', |
| 2672 |
actionPerformed); |
| 2673 |
assertTrue( |
| 2674 |
'Control must not have been deactivated on mouseup if ' + |
| 2675 |
'ACTIVE isn\'t an auto-state', |
| 2676 |
control.isActive()); |
| 2677 |
actionPerformed = false; |
| 2678 |
control.setHighlighted(false); |
| 2679 |
control.setAutoStates(goog.ui.Component.State.ACTIVE, true); |
| 2680 |
|
| 2681 |
// Scenario 6: All is well. |
| 2682 |
goog.testing.events.fireMouseUpEvent(element); |
| 2683 |
assertTrue('Control must highlight on mouseup', control.isHighlighted()); |
| 2684 |
assertTrue('Action must have been performed', actionPerformed); |
| 2685 |
assertFalse('Control must have been deactivated', control.isActive()); |
| 2686 |
} |
| 2687 |
|
| 2688 |
function testDefaultConstructor() { |
| 2689 |
var control = new goog.ui.Control(); |
| 2690 |
assertNull(control.getContent()); |
| 2691 |
} |
| 2692 |
|
| 2693 |
|
| 2694 |
function assertClickSequenceFires(msg) { |
| 2695 |
var actionCount = getEventCount(control, goog.ui.Component.EventType.ACTION); |
| 2696 |
goog.testing.events.fireClickSequence(control.getKeyEventTarget()); |
| 2697 |
assertEquals( |
| 2698 |
msg, actionCount + 1, |
| 2699 |
getEventCount(control, goog.ui.Component.EventType.ACTION)); |
| 2700 |
} |
| 2701 |
|
| 2702 |
|
| 2703 |
function assertIsolatedClickFires(msg) { |
| 2704 |
var actionCount = getEventCount(control, goog.ui.Component.EventType.ACTION); |
| 2705 |
goog.testing.events.fireClickEvent(control.getKeyEventTarget()); |
| 2706 |
assertEquals( |
| 2707 |
msg, actionCount + 1, |
| 2708 |
getEventCount(control, goog.ui.Component.EventType.ACTION)); |
| 2709 |
} |
| 2710 |
|
| 2711 |
|
| 2712 |
function assertIsolatedClickDoesNotFire(msg) { |
| 2713 |
var actionCount = getEventCount(control, goog.ui.Component.EventType.ACTION); |
| 2714 |
goog.testing.events.fireClickEvent(control.getKeyEventTarget()); |
| 2715 |
assertEquals( |
| 2716 |
msg, actionCount, |
| 2717 |
getEventCount(control, goog.ui.Component.EventType.ACTION)); |
| 2718 |
} |
| 2719 |
|
| 2720 |
|
| 2721 |
function testIeMouseEventSequenceSimulator() { |
| 2722 |
control.render(sandbox); |
| 2723 |
|
| 2724 |
// Click sequences and isolated clicks must be handled correctly in any order. |
| 2725 |
assertClickSequenceFires('ACTION event expected after a click sequence'); |
| 2726 |
assertClickSequenceFires( |
| 2727 |
'ACTION event expected after a second consecutive click sequence'); |
| 2728 |
if (goog.userAgent.IE) { |
| 2729 |
// For some reason in IE8 and perhaps earlier, isolated clicks do not result |
| 2730 |
// a detectable dispatch of an ACTION event, so we'll only assert the |
| 2731 |
// desired handling of isolated clicks in IE9 and higher. |
| 2732 |
if (goog.userAgent.isVersionOrHigher(9)) { |
| 2733 |
assertIsolatedClickFires( |
| 2734 |
'ACTION event expected after an isolated click immediately ' + |
| 2735 |
'following a click sequence'); |
| 2736 |
assertIsolatedClickFires( |
| 2737 |
'ACTION event expected after second consecutive isolated click'); |
| 2738 |
} else { |
| 2739 |
// For IE8-and-lower, fire an isolated click event in preparation for our |
| 2740 |
// final assertion. |
| 2741 |
goog.testing.events.fireClickEvent(control.getKeyEventTarget()); |
| 2742 |
} |
| 2743 |
} else { |
| 2744 |
assertIsolatedClickDoesNotFire( |
| 2745 |
'No ACTION event expected after an isolated click immediately ' + |
| 2746 |
'following a click sequence'); |
| 2747 |
assertIsolatedClickDoesNotFire( |
| 2748 |
'No ACTION event expected after second consecutive isolated click'); |
| 2749 |
} |
| 2750 |
assertClickSequenceFires( |
| 2751 |
'ACTION event expected after click sequence immediately following ' + |
| 2752 |
'an isolated click '); |
| 2753 |
} |
| 2754 |
|
| 2755 |
function testIeMouseEventSequenceSimulatorStrictMode() { |
| 2756 |
if (!document.createEvent) { |
| 2757 |
return; |
| 2758 |
} |
| 2759 |
|
| 2760 |
control.render(sandbox); |
| 2761 |
|
| 2762 |
var actionCount = getEventCount(control, goog.ui.Component.EventType.ACTION); |
| 2763 |
var e = document.createEvent('MouseEvents'); |
| 2764 |
e.initMouseEvent( |
| 2765 |
'click', true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, |
| 2766 |
null); |
| 2767 |
control.getElementStrict().dispatchEvent(e); |
| 2768 |
if (goog.userAgent.IE) { |
| 2769 |
assertEquals( |
| 2770 |
'ACTION event expected after an isolated click', actionCount + 1, |
| 2771 |
getEventCount(control, goog.ui.Component.EventType.ACTION)); |
| 2772 |
} else { |
| 2773 |
assertEquals( |
| 2774 |
'No ACTION event expected after an isolated click', actionCount, |
| 2775 |
getEventCount(control, goog.ui.Component.EventType.ACTION)); |
| 2776 |
} |
| 2777 |
} |