tabbar_test.js 22 KB

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