| 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.TabBarTest'); |
| 16 |
goog.setTestOnly('goog.ui.TabBarTest'); |
| 17 |
|
| 18 |
goog.require('goog.dom'); |
| 19 |
goog.require('goog.events'); |
| 20 |
goog.require('goog.events.Event'); |
| 21 |
goog.require('goog.events.EventType'); |
| 22 |
goog.require('goog.events.KeyCodes'); |
| 23 |
goog.require('goog.testing.jsunit'); |
| 24 |
goog.require('goog.ui.Component'); |
| 25 |
goog.require('goog.ui.Container'); |
| 26 |
goog.require('goog.ui.Tab'); |
| 27 |
goog.require('goog.ui.TabBar'); |
| 28 |
goog.require('goog.ui.TabBarRenderer'); |
| 29 |
|
| 30 |
var sandbox; |
| 31 |
var tabBar; |
| 32 |
|
| 33 |
// Fake keyboard event object. |
| 34 |
function FakeKeyEvent(keyCode) { |
| 35 |
this.keyCode = keyCode; |
| 36 |
this.defaultPrevented = false; |
| 37 |
this.propagationStopped = false; |
| 38 |
} |
| 39 |
FakeKeyEvent.prototype.preventDefault = function() { |
| 40 |
this.defaultPrevented = true; |
| 41 |
}; |
| 42 |
FakeKeyEvent.prototype.stopPropagation = function() { |
| 43 |
this.propagationStopped = true; |
| 44 |
}; |
| 45 |
|
| 46 |
function setUp() { |
| 47 |
sandbox = goog.dom.getElement('sandbox'); |
| 48 |
tabBar = new goog.ui.TabBar(); |
| 49 |
} |
| 50 |
|
| 51 |
function tearDown() { |
| 52 |
tabBar.dispose(); |
| 53 |
goog.dom.removeChildren(sandbox); |
| 54 |
} |
| 55 |
|
| 56 |
function testConstructor() { |
| 57 |
assertNotNull('Tab bar must not be null', tabBar); |
| 58 |
assertEquals( |
| 59 |
'Tab bar renderer must default to expected value', |
| 60 |
goog.ui.TabBarRenderer.getInstance(), tabBar.getRenderer()); |
| 61 |
assertEquals( |
| 62 |
'Tab bar location must default to expected value', |
| 63 |
goog.ui.TabBar.Location.TOP, tabBar.getLocation()); |
| 64 |
assertEquals( |
| 65 |
'Tab bar orientation must default to expected value', |
| 66 |
goog.ui.Container.Orientation.HORIZONTAL, tabBar.getOrientation()); |
| 67 |
|
| 68 |
var fakeRenderer = {}; |
| 69 |
var fakeDomHelper = {}; |
| 70 |
var bar = new goog.ui.TabBar( |
| 71 |
goog.ui.TabBar.Location.START, fakeRenderer, fakeDomHelper); |
| 72 |
assertNotNull('Tab bar must not be null', bar); |
| 73 |
assertEquals( |
| 74 |
'Tab bar renderer must have expected value', fakeRenderer, |
| 75 |
bar.getRenderer()); |
| 76 |
assertEquals( |
| 77 |
'Tab bar DOM helper must have expected value', fakeDomHelper, |
| 78 |
bar.getDomHelper()); |
| 79 |
assertEquals( |
| 80 |
'Tab bar location must have expected value', |
| 81 |
goog.ui.TabBar.Location.START, bar.getLocation()); |
| 82 |
assertEquals( |
| 83 |
'Tab bar orientation must have expected value', |
| 84 |
goog.ui.Container.Orientation.VERTICAL, bar.getOrientation()); |
| 85 |
bar.dispose(); |
| 86 |
} |
| 87 |
|
| 88 |
function testDispose() { |
| 89 |
// Set tabBar.selectedTab_ to something non-null, just to test dispose(). |
| 90 |
tabBar.selectedTab_ = {}; |
| 91 |
assertNotNull('Selected tab must be non-null', tabBar.getSelectedTab()); |
| 92 |
assertFalse('Tab bar must not have been disposed of', tabBar.isDisposed()); |
| 93 |
tabBar.dispose(); |
| 94 |
assertNull('Selected tab must be null', tabBar.getSelectedTab()); |
| 95 |
assertTrue('Tab bar must have been disposed of', tabBar.isDisposed()); |
| 96 |
} |
| 97 |
|
| 98 |
function testAddRemoveChild() { |
| 99 |
assertNull('No tab must be selected', tabBar.getSelectedTab()); |
| 100 |
|
| 101 |
var first = new goog.ui.Tab('First'); |
| 102 |
tabBar.addChild(first); |
| 103 |
assertEquals( |
| 104 |
'First tab must have been added at the expected index', 0, |
| 105 |
tabBar.indexOfChild(first)); |
| 106 |
first.setSelected(true); |
| 107 |
assertEquals('First tab must be selected', 0, tabBar.getSelectedTabIndex()); |
| 108 |
|
| 109 |
var second = new goog.ui.Tab('Second'); |
| 110 |
tabBar.addChild(second); |
| 111 |
assertEquals( |
| 112 |
'Second tab must have been added at the expected index', 1, |
| 113 |
tabBar.indexOfChild(second)); |
| 114 |
assertEquals( |
| 115 |
'First tab must remain selected', 0, tabBar.getSelectedTabIndex()); |
| 116 |
|
| 117 |
var firstRemoved = tabBar.removeChild(first); |
| 118 |
assertEquals( |
| 119 |
'removeChild() must return the removed tab', first, firstRemoved); |
| 120 |
assertEquals( |
| 121 |
'First tab must no longer be in the tab bar', -1, |
| 122 |
tabBar.indexOfChild(first)); |
| 123 |
assertEquals( |
| 124 |
'Second tab must be at the expected index', 0, |
| 125 |
tabBar.indexOfChild(second)); |
| 126 |
assertFalse('First tab must no longer be selected', first.isSelected()); |
| 127 |
assertTrue('Remaining tab must be selected', second.isSelected()); |
| 128 |
|
| 129 |
var secondRemoved = tabBar.removeChild(second); |
| 130 |
assertEquals( |
| 131 |
'removeChild() must return the removed tab', second, secondRemoved); |
| 132 |
assertFalse('Tab must no longer be selected', second.isSelected()); |
| 133 |
assertNull('No tab must be selected', tabBar.getSelectedTab()); |
| 134 |
} |
| 135 |
|
| 136 |
function testGetSetLocation() { |
| 137 |
assertEquals( |
| 138 |
'Location must default to TOP', goog.ui.TabBar.Location.TOP, |
| 139 |
tabBar.getLocation()); |
| 140 |
tabBar.setLocation(goog.ui.TabBar.Location.START); |
| 141 |
assertEquals( |
| 142 |
'Location must have expected value', goog.ui.TabBar.Location.START, |
| 143 |
tabBar.getLocation()); |
| 144 |
tabBar.createDom(); |
| 145 |
assertThrows( |
| 146 |
'Attempting to change the location after the tab bar has ' + |
| 147 |
'been rendered must throw error', |
| 148 |
function() { tabBar.setLocation(goog.ui.TabBar.Location.BOTTOM); }); |
| 149 |
} |
| 150 |
|
| 151 |
function testIsSetAutoSelectTabs() { |
| 152 |
assertTrue( |
| 153 |
'Tab bar must auto-select tabs by default', tabBar.isAutoSelectTabs()); |
| 154 |
tabBar.setAutoSelectTabs(false); |
| 155 |
assertFalse( |
| 156 |
'Tab bar must no longer auto-select tabs by default', |
| 157 |
tabBar.isAutoSelectTabs()); |
| 158 |
tabBar.render(sandbox); |
| 159 |
assertFalse( |
| 160 |
'Rendering must not change auto-select setting', |
| 161 |
tabBar.isAutoSelectTabs()); |
| 162 |
tabBar.setAutoSelectTabs(true); |
| 163 |
assertTrue( |
| 164 |
'Tab bar must once again auto-select tabs', tabBar.isAutoSelectTabs()); |
| 165 |
} |
| 166 |
|
| 167 |
function setHighlightedIndexFromKeyEvent() { |
| 168 |
var foo, bar, baz; |
| 169 |
|
| 170 |
// Create a tab bar with some tabs. |
| 171 |
tabBar.addChild(foo = new goog.ui.Tab('foo')); |
| 172 |
tabBar.addChild(bar = new goog.ui.Tab('bar')); |
| 173 |
tabBar.addChild(baz = new goog.ui.Tab('baz')); |
| 174 |
|
| 175 |
// Verify baseline assumptions. |
| 176 |
assertNull('No tab must be highlighted', tabBar.getHighlighted()); |
| 177 |
assertNull('No tab must be selected', tabBar.getSelectedTab()); |
| 178 |
assertTrue( |
| 179 |
'Tab bar must auto-select tabs on keyboard highlight', |
| 180 |
tabBar.isAutoSelectTabs()); |
| 181 |
|
| 182 |
// Highlight and selection must move together. |
| 183 |
tabBar.setHighlightedIndexFromKeyEvent(0); |
| 184 |
assertTrue('Foo must be highlighted', foo.isHighlighted()); |
| 185 |
assertTrue('Foo must be selected', foo.isSelected()); |
| 186 |
|
| 187 |
// Highlight and selection must move together. |
| 188 |
tabBar.setHighlightedIndexFromKeyEvent(1); |
| 189 |
assertFalse('Foo must no longer be highlighted', foo.isHighlighted()); |
| 190 |
assertFalse('Foo must no longer be selected', foo.isSelected()); |
| 191 |
assertTrue('Bar must be highlighted', bar.isHighlighted()); |
| 192 |
assertTrue('Bar must be selected', bar.isSelected()); |
| 193 |
|
| 194 |
// Turn off auto-select-on-keyboard-highlight. |
| 195 |
tabBar.setAutoSelectTabs(false); |
| 196 |
|
| 197 |
// Selection must not change; only highlight should move. |
| 198 |
tabBar.setHighlightedIndexFromKeyEvent(2); |
| 199 |
assertFalse('Bar must no longer be highlighted', bar.isHighlighted()); |
| 200 |
assertTrue('Bar must remain selected', bar.isSelected()); |
| 201 |
assertTrue('Baz must be highlighted', baz.isHighlighted()); |
| 202 |
assertFalse('Baz must not be selected', baz.isSelected()); |
| 203 |
} |
| 204 |
|
| 205 |
function testGetSetSelectedTab() { |
| 206 |
var foo, bar, baz; |
| 207 |
|
| 208 |
// Create a tab bar with some tabs. |
| 209 |
tabBar.addChild(foo = new goog.ui.Tab('foo')); |
| 210 |
tabBar.addChild(bar = new goog.ui.Tab('bar')); |
| 211 |
tabBar.addChild(baz = new goog.ui.Tab('baz')); |
| 212 |
|
| 213 |
assertNull('No tab must be selected', tabBar.getSelectedTab()); |
| 214 |
|
| 215 |
tabBar.setSelectedTab(baz); |
| 216 |
assertTrue('Baz must be selected', baz.isSelected()); |
| 217 |
assertEquals('Baz must be the selected tab', baz, tabBar.getSelectedTab()); |
| 218 |
|
| 219 |
tabBar.setSelectedTab(foo); |
| 220 |
assertFalse('Baz must no longer be selected', baz.isSelected()); |
| 221 |
assertTrue('Foo must be selected', foo.isSelected()); |
| 222 |
assertEquals('Foo must be the selected tab', foo, tabBar.getSelectedTab()); |
| 223 |
|
| 224 |
tabBar.setSelectedTab(foo); |
| 225 |
assertTrue('Foo must remain selected', foo.isSelected()); |
| 226 |
assertEquals( |
| 227 |
'Foo must remain the selected tab', foo, tabBar.getSelectedTab()); |
| 228 |
|
| 229 |
tabBar.setSelectedTab(null); |
| 230 |
assertFalse('Foo must no longer be selected', foo.isSelected()); |
| 231 |
assertNull('No tab must be selected', tabBar.getSelectedTab()); |
| 232 |
} |
| 233 |
|
| 234 |
function testGetSetSelectedTabIndex() { |
| 235 |
var foo, bar, baz; |
| 236 |
|
| 237 |
// Create a tab bar with some tabs. |
| 238 |
tabBar.addChildAt(foo = new goog.ui.Tab('foo'), 0); |
| 239 |
tabBar.addChildAt(bar = new goog.ui.Tab('bar'), 1); |
| 240 |
tabBar.addChildAt(baz = new goog.ui.Tab('baz'), 2); |
| 241 |
|
| 242 |
assertEquals('No tab must be selected', -1, tabBar.getSelectedTabIndex()); |
| 243 |
|
| 244 |
tabBar.setSelectedTabIndex(2); |
| 245 |
assertTrue('Baz must be selected', baz.isSelected()); |
| 246 |
assertEquals('Baz must be the selected tab', 2, tabBar.getSelectedTabIndex()); |
| 247 |
|
| 248 |
tabBar.setSelectedTabIndex(0); |
| 249 |
assertFalse('Baz must no longer be selected', baz.isSelected()); |
| 250 |
assertTrue('Foo must be selected', foo.isSelected()); |
| 251 |
assertEquals('Foo must be the selected tab', 0, tabBar.getSelectedTabIndex()); |
| 252 |
|
| 253 |
tabBar.setSelectedTabIndex(0); |
| 254 |
assertTrue('Foo must remain selected', foo.isSelected()); |
| 255 |
assertEquals( |
| 256 |
'Foo must remain the selected tab', 0, tabBar.getSelectedTabIndex()); |
| 257 |
|
| 258 |
tabBar.setSelectedTabIndex(-1); |
| 259 |
assertFalse('Foo must no longer be selected', foo.isSelected()); |
| 260 |
assertEquals('No tab must be selected', -1, tabBar.getSelectedTabIndex()); |
| 261 |
} |
| 262 |
|
| 263 |
function testDeselectIfSelected() { |
| 264 |
var foo, bar, baz; |
| 265 |
|
| 266 |
// Create a tab bar with some tabs. |
| 267 |
tabBar.addChild(foo = new goog.ui.Tab('foo')); |
| 268 |
tabBar.addChild(bar = new goog.ui.Tab('bar')); |
| 269 |
tabBar.addChild(baz = new goog.ui.Tab('baz')); |
| 270 |
|
| 271 |
// Start with the middle tab selected. |
| 272 |
bar.setSelected(true); |
| 273 |
assertTrue('Bar must be selected', bar.isSelected()); |
| 274 |
assertEquals('Bar must be the selected tab', bar, tabBar.getSelectedTab()); |
| 275 |
|
| 276 |
// Should be a no-op. |
| 277 |
tabBar.deselectIfSelected(null); |
| 278 |
assertTrue('Bar must remain selected', bar.isSelected()); |
| 279 |
assertEquals( |
| 280 |
'Bar must remain the selected tab', bar, tabBar.getSelectedTab()); |
| 281 |
|
| 282 |
// Should be a no-op. |
| 283 |
tabBar.deselectIfSelected(foo); |
| 284 |
assertTrue('Bar must remain selected', bar.isSelected()); |
| 285 |
assertEquals( |
| 286 |
'Bar must remain the selected tab', bar, tabBar.getSelectedTab()); |
| 287 |
|
| 288 |
// Should deselect bar and select the previous tab (foo). |
| 289 |
tabBar.deselectIfSelected(bar); |
| 290 |
assertFalse('Bar must no longer be selected', bar.isSelected()); |
| 291 |
assertTrue('Foo must be selected', foo.isSelected()); |
| 292 |
assertEquals('Foo must be the selected tab', foo, tabBar.getSelectedTab()); |
| 293 |
|
| 294 |
// Should deselect foo and select the next tab (bar). |
| 295 |
tabBar.deselectIfSelected(foo); |
| 296 |
assertFalse('Foo must no longer be selected', foo.isSelected()); |
| 297 |
assertTrue('Bar must be selected', bar.isSelected()); |
| 298 |
assertEquals('Bar must be the selected tab', bar, tabBar.getSelectedTab()); |
| 299 |
} |
| 300 |
|
| 301 |
function testHandleTabSelect() { |
| 302 |
var foo, bar, baz; |
| 303 |
|
| 304 |
// Create a tab bar with some tabs. |
| 305 |
tabBar.addChild(foo = new goog.ui.Tab('foo')); |
| 306 |
tabBar.addChild(bar = new goog.ui.Tab('bar')); |
| 307 |
tabBar.addChild(baz = new goog.ui.Tab('baz')); |
| 308 |
|
| 309 |
assertNull('No tab must be selected', tabBar.getSelectedTab()); |
| 310 |
|
| 311 |
tabBar.handleTabSelect( |
| 312 |
new goog.events.Event(goog.ui.Component.EventType.SELECT, bar)); |
| 313 |
assertEquals('Bar must be the selected tab', bar, tabBar.getSelectedTab()); |
| 314 |
|
| 315 |
tabBar.handleTabSelect( |
| 316 |
new goog.events.Event(goog.ui.Component.EventType.SELECT, bar)); |
| 317 |
assertEquals('Bar must remain selected tab', bar, tabBar.getSelectedTab()); |
| 318 |
|
| 319 |
tabBar.handleTabSelect( |
| 320 |
new goog.events.Event(goog.ui.Component.EventType.SELECT, foo)); |
| 321 |
assertEquals( |
| 322 |
'Foo must now be the selected tab', foo, tabBar.getSelectedTab()); |
| 323 |
} |
| 324 |
|
| 325 |
function testHandleTabUnselect() { |
| 326 |
var foo, bar, baz; |
| 327 |
|
| 328 |
// Create a tab bar with some tabs. |
| 329 |
tabBar.addChild(foo = new goog.ui.Tab('foo')); |
| 330 |
tabBar.addChild(bar = new goog.ui.Tab('bar')); |
| 331 |
tabBar.addChild(baz = new goog.ui.Tab('baz')); |
| 332 |
|
| 333 |
bar.setSelected(true); |
| 334 |
assertEquals('Bar must be the selected tab', bar, tabBar.getSelectedTab()); |
| 335 |
|
| 336 |
tabBar.handleTabUnselect( |
| 337 |
new goog.events.Event(goog.ui.Component.EventType.UNSELECT, foo)); |
| 338 |
assertEquals( |
| 339 |
'Bar must remain the selected tab', bar, tabBar.getSelectedTab()); |
| 340 |
|
| 341 |
tabBar.handleTabUnselect( |
| 342 |
new goog.events.Event(goog.ui.Component.EventType.SELECT, bar)); |
| 343 |
assertNull('No tab must be selected', tabBar.getSelectedTab()); |
| 344 |
} |
| 345 |
|
| 346 |
function testHandleTabDisable() { |
| 347 |
var foo, bar, baz; |
| 348 |
|
| 349 |
// Create a tab bar with some tabs. |
| 350 |
tabBar.addChild(foo = new goog.ui.Tab('foo')); |
| 351 |
tabBar.addChild(bar = new goog.ui.Tab('bar')); |
| 352 |
tabBar.addChild(baz = new goog.ui.Tab('baz')); |
| 353 |
|
| 354 |
// Start with the middle tab selected. |
| 355 |
bar.setSelected(true); |
| 356 |
assertTrue('Bar must be selected', bar.isSelected()); |
| 357 |
assertEquals('Bar must be the selected tab', bar, tabBar.getSelectedTab()); |
| 358 |
|
| 359 |
// Should deselect bar and select the previous enabled, visible tab (foo). |
| 360 |
bar.setEnabled(false); |
| 361 |
assertFalse('Bar must no longer be selected', bar.isSelected()); |
| 362 |
assertTrue('Foo must be selected', foo.isSelected()); |
| 363 |
assertEquals('Foo must be the selected tab', foo, tabBar.getSelectedTab()); |
| 364 |
|
| 365 |
// Should deselect foo and select the next enabled, visible tab (baz). |
| 366 |
foo.setEnabled(false); |
| 367 |
assertFalse('Foo must no longer be selected', foo.isSelected()); |
| 368 |
assertTrue('Baz must be selected', baz.isSelected()); |
| 369 |
assertEquals('Baz must be the selected tab', baz, tabBar.getSelectedTab()); |
| 370 |
|
| 371 |
// Should deselect baz. Since there are no enabled, visible tabs left, |
| 372 |
// the tab bar should have no selected tab. |
| 373 |
baz.setEnabled(false); |
| 374 |
assertFalse('Baz must no longer be selected', baz.isSelected()); |
| 375 |
assertNull('No tab must be selected', tabBar.getSelectedTab()); |
| 376 |
} |
| 377 |
|
| 378 |
function testHandleTabHide() { |
| 379 |
var foo, bar, baz; |
| 380 |
|
| 381 |
// Create a tab bar with some tabs. |
| 382 |
tabBar.addChild(foo = new goog.ui.Tab('foo')); |
| 383 |
tabBar.addChild(bar = new goog.ui.Tab('bar')); |
| 384 |
tabBar.addChild(baz = new goog.ui.Tab('baz')); |
| 385 |
|
| 386 |
// Start with the middle tab selected. |
| 387 |
bar.setSelected(true); |
| 388 |
assertTrue('Bar must be selected', bar.isSelected()); |
| 389 |
assertEquals('Bar must be the selected tab', bar, tabBar.getSelectedTab()); |
| 390 |
|
| 391 |
// Should deselect bar and select the previous enabled, visible tab (foo). |
| 392 |
bar.setVisible(false); |
| 393 |
assertFalse('Bar must no longer be selected', bar.isSelected()); |
| 394 |
assertTrue('Foo must be selected', foo.isSelected()); |
| 395 |
assertEquals('Foo must be the selected tab', foo, tabBar.getSelectedTab()); |
| 396 |
|
| 397 |
// Should deselect foo and select the next enabled, visible tab (baz). |
| 398 |
foo.setVisible(false); |
| 399 |
assertFalse('Foo must no longer be selected', foo.isSelected()); |
| 400 |
assertTrue('Baz must be selected', baz.isSelected()); |
| 401 |
assertEquals('Baz must be the selected tab', baz, tabBar.getSelectedTab()); |
| 402 |
|
| 403 |
// Should deselect baz. Since there are no enabled, visible tabs left, |
| 404 |
// the tab bar should have no selected tab. |
| 405 |
baz.setVisible(false); |
| 406 |
assertFalse('Baz must no longer be selected', baz.isSelected()); |
| 407 |
assertNull('No tab must be selected', tabBar.getSelectedTab()); |
| 408 |
} |
| 409 |
|
| 410 |
function testHandleFocus() { |
| 411 |
var foo, bar, baz; |
| 412 |
|
| 413 |
// Create a tab bar with some tabs. |
| 414 |
tabBar.addChild(foo = new goog.ui.Tab('foo'), true); |
| 415 |
tabBar.addChild(bar = new goog.ui.Tab('bar'), true); |
| 416 |
tabBar.addChild(baz = new goog.ui.Tab('baz'), true); |
| 417 |
|
| 418 |
// Render the tab bar into the document, so highlight handling works as |
| 419 |
// expected. |
| 420 |
tabBar.render(sandbox); |
| 421 |
|
| 422 |
// Start with the middle tab selected. |
| 423 |
bar.setSelected(true); |
| 424 |
assertTrue('Bar must be selected', bar.isSelected()); |
| 425 |
assertEquals('Bar must be the selected tab', bar, tabBar.getSelectedTab()); |
| 426 |
|
| 427 |
assertNull('No tab must be highlighted', tabBar.getHighlighted()); |
| 428 |
tabBar.handleFocus( |
| 429 |
new goog.events.Event(goog.events.EventType.FOCUS, tabBar.getElement())); |
| 430 |
assertTrue('Bar must be highlighted', bar.isHighlighted()); |
| 431 |
assertEquals('Bar must be the highlighted tab', bar, tabBar.getHighlighted()); |
| 432 |
} |
| 433 |
|
| 434 |
function testHandleFocusWithoutSelectedTab() { |
| 435 |
var foo, bar, baz; |
| 436 |
|
| 437 |
// Create a tab bar with some tabs. |
| 438 |
tabBar.addChild(foo = new goog.ui.Tab('foo'), true); |
| 439 |
tabBar.addChild(bar = new goog.ui.Tab('bar'), true); |
| 440 |
tabBar.addChild(baz = new goog.ui.Tab('baz'), true); |
| 441 |
|
| 442 |
// Render the tab bar into the document, so highlight handling works as |
| 443 |
// expected. |
| 444 |
tabBar.render(sandbox); |
| 445 |
|
| 446 |
// Start with no tab selected. |
| 447 |
assertNull('No tab must be selected', tabBar.getSelectedTab()); |
| 448 |
|
| 449 |
assertNull('No tab must be highlighted', tabBar.getHighlighted()); |
| 450 |
tabBar.handleFocus( |
| 451 |
new goog.events.Event(goog.events.EventType.FOCUS, tabBar.getElement())); |
| 452 |
assertTrue('Foo must be highlighted', foo.isHighlighted()); |
| 453 |
assertEquals('Foo must be the highlighted tab', foo, tabBar.getHighlighted()); |
| 454 |
} |
| 455 |
|
| 456 |
function testGetOrientationFromLocation() { |
| 457 |
assertEquals( |
| 458 |
goog.ui.Container.Orientation.HORIZONTAL, |
| 459 |
goog.ui.TabBar.getOrientationFromLocation(goog.ui.TabBar.Location.TOP)); |
| 460 |
assertEquals( |
| 461 |
goog.ui.Container.Orientation.HORIZONTAL, |
| 462 |
goog.ui.TabBar.getOrientationFromLocation( |
| 463 |
goog.ui.TabBar.Location.BOTTOM)); |
| 464 |
assertEquals( |
| 465 |
goog.ui.Container.Orientation.VERTICAL, |
| 466 |
goog.ui.TabBar.getOrientationFromLocation(goog.ui.TabBar.Location.START)); |
| 467 |
assertEquals( |
| 468 |
goog.ui.Container.Orientation.VERTICAL, |
| 469 |
goog.ui.TabBar.getOrientationFromLocation(goog.ui.TabBar.Location.END)); |
| 470 |
} |
| 471 |
|
| 472 |
function testKeyboardNavigation() { |
| 473 |
var foo, bar, baz; |
| 474 |
|
| 475 |
// Create a tab bar with some tabs. |
| 476 |
tabBar.addChild(foo = new goog.ui.Tab('foo'), true); |
| 477 |
tabBar.addChild(bar = new goog.ui.Tab('bar'), true); |
| 478 |
tabBar.addChild(baz = new goog.ui.Tab('baz'), true); |
| 479 |
tabBar.render(sandbox); |
| 480 |
|
| 481 |
// Highlight the selected tab (this happens automatically when the tab |
| 482 |
// bar receives keyboard focus). |
| 483 |
tabBar.setSelectedTabIndex(0); |
| 484 |
tabBar.getSelectedTab().setHighlighted(true); |
| 485 |
|
| 486 |
// Count events dispatched by each tab. |
| 487 |
var eventCount = { |
| 488 |
'foo': {'select': 0, 'unselect': 0}, |
| 489 |
'bar': {'select': 0, 'unselect': 0}, |
| 490 |
'baz': {'select': 0, 'unselect': 0} |
| 491 |
}; |
| 492 |
|
| 493 |
function countEvent(e) { |
| 494 |
var tabId = e.target.getContent(); |
| 495 |
var type = e.type; |
| 496 |
eventCount[tabId][type]++; |
| 497 |
} |
| 498 |
|
| 499 |
function getEventCount(tabId, type) { return eventCount[tabId][type]; } |
| 500 |
|
| 501 |
// Listen for SELECT and UNSELECT events on the tab bar. |
| 502 |
goog.events.listen( |
| 503 |
tabBar, |
| 504 |
[ |
| 505 |
goog.ui.Component.EventType.SELECT, goog.ui.Component.EventType.UNSELECT |
| 506 |
], |
| 507 |
countEvent); |
| 508 |
|
| 509 |
// Verify baseline assumptions. |
| 510 |
assertTrue('Tab bar must auto-select tabs', tabBar.isAutoSelectTabs()); |
| 511 |
assertEquals('First tab must be selected', 0, tabBar.getSelectedTabIndex()); |
| 512 |
|
| 513 |
// Simulate a right arrow key event. |
| 514 |
var rightEvent = new FakeKeyEvent(goog.events.KeyCodes.RIGHT); |
| 515 |
assertTrue( |
| 516 |
'Key event must have beeen handled', tabBar.handleKeyEvent(rightEvent)); |
| 517 |
assertTrue( |
| 518 |
'Key event propagation must have been stopped', |
| 519 |
rightEvent.propagationStopped); |
| 520 |
assertTrue( |
| 521 |
'Default key event must have been prevented', |
| 522 |
rightEvent.defaultPrevented); |
| 523 |
assertEquals( |
| 524 |
'Foo must have dispatched UNSELECT', 1, |
| 525 |
getEventCount('foo', goog.ui.Component.EventType.UNSELECT)); |
| 526 |
assertEquals( |
| 527 |
'Bar must have dispatched SELECT', 1, |
| 528 |
getEventCount('bar', goog.ui.Component.EventType.SELECT)); |
| 529 |
assertEquals('Bar must have been selected', bar, tabBar.getSelectedTab()); |
| 530 |
|
| 531 |
// Simulate a left arrow key event. |
| 532 |
var leftEvent = new FakeKeyEvent(goog.events.KeyCodes.LEFT); |
| 533 |
assertTrue( |
| 534 |
'Key event must have beeen handled', tabBar.handleKeyEvent(leftEvent)); |
| 535 |
assertTrue( |
| 536 |
'Key event propagation must have been stopped', |
| 537 |
leftEvent.propagationStopped); |
| 538 |
assertTrue( |
| 539 |
'Default key event must have been prevented', leftEvent.defaultPrevented); |
| 540 |
assertEquals( |
| 541 |
'Bar must have dispatched UNSELECT', 1, |
| 542 |
getEventCount('bar', goog.ui.Component.EventType.UNSELECT)); |
| 543 |
assertEquals( |
| 544 |
'Foo must have dispatched SELECT', 1, |
| 545 |
getEventCount('foo', goog.ui.Component.EventType.SELECT)); |
| 546 |
assertEquals('Foo must have been selected', foo, tabBar.getSelectedTab()); |
| 547 |
|
| 548 |
// Disable tab auto-selection. |
| 549 |
tabBar.setAutoSelectTabs(false); |
| 550 |
|
| 551 |
// Simulate another left arrow key event. |
| 552 |
var anotherLeftEvent = new FakeKeyEvent(goog.events.KeyCodes.LEFT); |
| 553 |
assertTrue( |
| 554 |
'Key event must have beeen handled', |
| 555 |
tabBar.handleKeyEvent(anotherLeftEvent)); |
| 556 |
assertTrue( |
| 557 |
'Key event propagation must have been stopped', |
| 558 |
anotherLeftEvent.propagationStopped); |
| 559 |
assertTrue( |
| 560 |
'Default key event must have been prevented', |
| 561 |
anotherLeftEvent.defaultPrevented); |
| 562 |
assertEquals('Foo must remain selected', foo, tabBar.getSelectedTab()); |
| 563 |
assertEquals( |
| 564 |
'Foo must not have dispatched another UNSELECT event', 1, |
| 565 |
getEventCount('foo', goog.ui.Component.EventType.UNSELECT)); |
| 566 |
assertEquals( |
| 567 |
'Baz must not have dispatched a SELECT event', 0, |
| 568 |
getEventCount('baz', goog.ui.Component.EventType.SELECT)); |
| 569 |
assertFalse('Baz must not be selected', baz.isSelected()); |
| 570 |
assertTrue('Baz must be highlighted', baz.isHighlighted()); |
| 571 |
|
| 572 |
// Simulate 'g' key event. |
| 573 |
var gEvent = new FakeKeyEvent(goog.events.KeyCodes.G); |
| 574 |
assertFalse( |
| 575 |
'Key event must not have beeen handled', tabBar.handleKeyEvent(gEvent)); |
| 576 |
assertFalse( |
| 577 |
'Key event propagation must not have been stopped', |
| 578 |
gEvent.propagationStopped); |
| 579 |
assertFalse( |
| 580 |
'Default key event must not have been prevented', |
| 581 |
gEvent.defaultPrevented); |
| 582 |
assertEquals('Foo must remain selected', foo, tabBar.getSelectedTab()); |
| 583 |
|
| 584 |
// Clean up. |
| 585 |
goog.events.unlisten( |
| 586 |
tabBar, |
| 587 |
[ |
| 588 |
goog.ui.Component.EventType.SELECT, goog.ui.Component.EventType.UNSELECT |
| 589 |
], |
| 590 |
countEvent); |
| 591 |
} |
| 592 |
|
| 593 |
function testExitAndEnterDocument() { |
| 594 |
var component = new goog.ui.Component(); |
| 595 |
component.render(sandbox); |
| 596 |
|
| 597 |
var tab1 = new goog.ui.Tab('tab1'); |
| 598 |
var tab2 = new goog.ui.Tab('tab2'); |
| 599 |
var tab3 = new goog.ui.Tab('tab3'); |
| 600 |
tabBar.addChild(tab1, true); |
| 601 |
tabBar.addChild(tab2, true); |
| 602 |
tabBar.addChild(tab3, true); |
| 603 |
|
| 604 |
component.addChild(tabBar, true); |
| 605 |
tab2.setSelected(true); |
| 606 |
assertEquals(tabBar.getSelectedTab(), tab2); |
| 607 |
|
| 608 |
component.removeChild(tabBar, true); |
| 609 |
tab1.setSelected(true); |
| 610 |
assertEquals(tabBar.getSelectedTab(), tab2); |
| 611 |
|
| 612 |
component.addChild(tabBar, true); |
| 613 |
tab3.setSelected(true); |
| 614 |
assertEquals(tabBar.getSelectedTab(), tab3); |
| 615 |
} |