submenu_test.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  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.SubMenuTest');
  15. goog.setTestOnly('goog.ui.SubMenuTest');
  16. goog.require('goog.a11y.aria');
  17. goog.require('goog.a11y.aria.State');
  18. goog.require('goog.dom');
  19. goog.require('goog.dom.classlist');
  20. goog.require('goog.events');
  21. goog.require('goog.events.Event');
  22. goog.require('goog.events.KeyCodes');
  23. goog.require('goog.events.KeyHandler');
  24. goog.require('goog.functions');
  25. goog.require('goog.positioning');
  26. goog.require('goog.positioning.Overflow');
  27. goog.require('goog.style');
  28. goog.require('goog.testing.MockClock');
  29. goog.require('goog.testing.events');
  30. goog.require('goog.testing.jsunit');
  31. goog.require('goog.ui.Component');
  32. goog.require('goog.ui.Menu');
  33. goog.require('goog.ui.MenuItem');
  34. goog.require('goog.ui.SubMenu');
  35. goog.require('goog.ui.SubMenuRenderer');
  36. var menu;
  37. var clonedMenuDom;
  38. var mockClock;
  39. // mock out goog.positioning.positionAtCoordinate so that
  40. // the menu always fits. (we don't care about testing the
  41. // dynamic menu positioning if the menu doesn't fit in the window.)
  42. var oldPositionFn = goog.positioning.positionAtCoordinate;
  43. goog.positioning.positionAtCoordinate = function(
  44. absolutePos, movableElement, movableElementCorner, opt_margin,
  45. opt_overflow) {
  46. return oldPositionFn.call(
  47. null, absolutePos, movableElement, movableElementCorner, opt_margin,
  48. goog.positioning.Overflow.IGNORE);
  49. };
  50. function setUp() {
  51. clonedMenuDom = goog.dom.getElement('demoMenu').cloneNode(true);
  52. menu = new goog.ui.Menu();
  53. }
  54. function tearDown() {
  55. document.body.style.direction = 'ltr';
  56. menu.dispose();
  57. var element = goog.dom.getElement('demoMenu');
  58. element.parentNode.replaceChild(clonedMenuDom, element);
  59. goog.dom.removeChildren(goog.dom.getElement('sandbox'));
  60. if (mockClock) {
  61. mockClock.uninstall();
  62. mockClock = null;
  63. }
  64. }
  65. function assertKeyHandlingIsCorrect(keyToOpenSubMenu, keyToCloseSubMenu) {
  66. menu.setFocusable(true);
  67. menu.decorate(goog.dom.getElement('demoMenu'));
  68. var KeyCodes = goog.events.KeyCodes;
  69. var plainItem = menu.getChildAt(0);
  70. plainItem.setMnemonic(KeyCodes.F);
  71. var subMenuItem1 = menu.getChildAt(1);
  72. subMenuItem1.setMnemonic(KeyCodes.S);
  73. var subMenuItem1Menu = subMenuItem1.getMenu();
  74. menu.setHighlighted(plainItem);
  75. var fireKeySequence = goog.testing.events.fireKeySequence;
  76. assertTrue(
  77. 'Expected OpenSubMenu key to not be handled',
  78. fireKeySequence(plainItem.getElement(), keyToOpenSubMenu));
  79. assertFalse(subMenuItem1Menu.isVisible());
  80. assertFalse(
  81. 'Expected F key to be handled',
  82. fireKeySequence(plainItem.getElement(), KeyCodes.F));
  83. assertFalse(
  84. 'Expected DOWN key to be handled',
  85. fireKeySequence(plainItem.getElement(), KeyCodes.DOWN));
  86. assertEquals(subMenuItem1, menu.getChildAt(1));
  87. assertFalse(
  88. 'Expected OpenSubMenu key to be handled',
  89. fireKeySequence(subMenuItem1.getElement(), keyToOpenSubMenu));
  90. assertTrue(subMenuItem1Menu.isVisible());
  91. assertFalse(
  92. 'Expected CloseSubMenu key to be handled',
  93. fireKeySequence(subMenuItem1.getElement(), keyToCloseSubMenu));
  94. assertFalse(subMenuItem1Menu.isVisible());
  95. assertFalse(
  96. 'Expected UP key to be handled',
  97. fireKeySequence(subMenuItem1.getElement(), KeyCodes.UP));
  98. assertFalse(
  99. 'Expected S key to be handled',
  100. fireKeySequence(plainItem.getElement(), KeyCodes.S));
  101. assertTrue(subMenuItem1Menu.isVisible());
  102. }
  103. function testKeyHandling_ltr() {
  104. assertKeyHandlingIsCorrect(
  105. goog.events.KeyCodes.RIGHT, goog.events.KeyCodes.LEFT);
  106. }
  107. function testKeyHandling_rtl() {
  108. document.body.style.direction = 'rtl';
  109. assertKeyHandlingIsCorrect(
  110. goog.events.KeyCodes.LEFT, goog.events.KeyCodes.RIGHT);
  111. }
  112. function testNormalLtrSubMenu() {
  113. menu.decorate(goog.dom.getElement('demoMenu'));
  114. var subMenu = menu.getChildAt(1);
  115. assertArrowDirection(subMenu, false);
  116. assertRenderDirection(subMenu, false);
  117. assertArrowPosition(subMenu, false);
  118. }
  119. function testNormalRtlSubMenu() {
  120. document.body.style.direction = 'rtl';
  121. menu.decorate(goog.dom.getElement('demoMenu'));
  122. var subMenu = menu.getChildAt(1);
  123. assertArrowDirection(subMenu, true);
  124. assertRenderDirection(subMenu, true);
  125. assertArrowPosition(subMenu, true);
  126. }
  127. function testLtrSubMenuAlignedToStart() {
  128. menu.decorate(goog.dom.getElement('demoMenu'));
  129. var subMenu = menu.getChildAt(1);
  130. subMenu.setAlignToEnd(false);
  131. assertArrowDirection(subMenu, true);
  132. assertRenderDirection(subMenu, true);
  133. assertArrowPosition(subMenu, false);
  134. }
  135. function testNullContentElement() {
  136. var subMenu = new goog.ui.SubMenu();
  137. subMenu.setContent('demo');
  138. }
  139. function testRtlSubMenuAlignedToStart() {
  140. document.body.style.direction = 'rtl';
  141. menu.decorate(goog.dom.getElement('demoMenu'));
  142. var subMenu = menu.getChildAt(1);
  143. subMenu.setAlignToEnd(false);
  144. assertArrowDirection(subMenu, false);
  145. assertRenderDirection(subMenu, false);
  146. assertArrowPosition(subMenu, true);
  147. }
  148. function testSetContentKeepsArrow_ltr() {
  149. document.body.style.direction = 'ltr';
  150. menu.decorate(goog.dom.getElement('demoMenu'));
  151. var subMenu = menu.getChildAt(1);
  152. subMenu.setAlignToEnd(false);
  153. subMenu.setContent('test');
  154. assertArrowDirection(subMenu, true);
  155. assertRenderDirection(subMenu, true);
  156. assertArrowPosition(subMenu, false);
  157. }
  158. function testSetContentKeepsArrow_rtl() {
  159. document.body.style.direction = 'rtl';
  160. menu.decorate(goog.dom.getElement('demoMenu'));
  161. var subMenu = menu.getChildAt(1);
  162. subMenu.setAlignToEnd(false);
  163. subMenu.setContent('test');
  164. assertArrowDirection(subMenu, false);
  165. assertRenderDirection(subMenu, false);
  166. assertArrowPosition(subMenu, true);
  167. }
  168. function testExitDocument() {
  169. menu.decorate(goog.dom.getElement('demoMenu'));
  170. var subMenu = menu.getChildAt(1);
  171. var innerMenu = subMenu.getMenu();
  172. assertTrue('Top-level menu was not in document', menu.isInDocument());
  173. assertTrue('Submenu was not in document', subMenu.isInDocument());
  174. assertTrue('Inner menu was not in document', innerMenu.isInDocument());
  175. menu.exitDocument();
  176. assertFalse('Top-level menu was in document', menu.isInDocument());
  177. assertFalse('Submenu was in document', subMenu.isInDocument());
  178. assertFalse('Inner menu was in document', innerMenu.isInDocument());
  179. }
  180. function testDisposal() {
  181. menu.decorate(goog.dom.getElement('demoMenu'));
  182. var subMenu = menu.getChildAt(1);
  183. var innerMenu = subMenu.getMenu();
  184. menu.dispose();
  185. assert('Top-level menu was not disposed', menu.getDisposed());
  186. assert('Submenu was not disposed', subMenu.getDisposed());
  187. assert('Inner menu was not disposed', innerMenu.getDisposed());
  188. }
  189. function testShowAndDismissSubMenu() {
  190. var openEventDispatched = false;
  191. var closeEventDispatched = false;
  192. function handleEvent(e) {
  193. switch (e.type) {
  194. case goog.ui.Component.EventType.OPEN:
  195. openEventDispatched = true;
  196. break;
  197. case goog.ui.Component.EventType.CLOSE:
  198. closeEventDispatched = true;
  199. break;
  200. default:
  201. fail('Invalid event type: ' + e.type);
  202. }
  203. }
  204. menu.decorate(goog.dom.getElement('demoMenu'));
  205. var subMenu = menu.getChildAt(1);
  206. subMenu.setHighlighted(true);
  207. goog.events.listen(
  208. subMenu,
  209. [goog.ui.Component.EventType.OPEN, goog.ui.Component.EventType.CLOSE],
  210. handleEvent);
  211. assertFalse(
  212. 'Submenu must not have "-open" CSS class',
  213. goog.dom.classlist.contains(subMenu.getElement(), 'goog-submenu-open'));
  214. assertFalse('Popup menu must not be visible', subMenu.getMenu().isVisible());
  215. assertFalse('No OPEN event must have been dispatched', openEventDispatched);
  216. assertFalse('No CLOSE event must have been dispatched', closeEventDispatched);
  217. subMenu.showSubMenu();
  218. assertTrue(
  219. 'Submenu must have "-open" CSS class',
  220. goog.dom.classlist.contains(subMenu.getElement(), 'goog-submenu-open'));
  221. assertTrue('Popup menu must be visible', subMenu.getMenu().isVisible());
  222. assertTrue('OPEN event must have been dispatched', openEventDispatched);
  223. assertFalse('No CLOSE event must have been dispatched', closeEventDispatched);
  224. subMenu.dismissSubMenu();
  225. assertFalse(
  226. 'Submenu must not have "-open" CSS class',
  227. goog.dom.classlist.contains(subMenu.getElement(), 'goog-submenu-open'));
  228. assertFalse('Popup menu must not be visible', subMenu.getMenu().isVisible());
  229. assertTrue('CLOSE event must have been dispatched', closeEventDispatched);
  230. goog.events.unlisten(
  231. subMenu,
  232. [goog.ui.Component.EventType.OPEN, goog.ui.Component.EventType.CLOSE],
  233. handleEvent);
  234. }
  235. function testDismissWhenSubMenuNotVisible() {
  236. var openEventDispatched = false;
  237. var closeEventDispatched = false;
  238. function handleEvent(e) {
  239. switch (e.type) {
  240. case goog.ui.Component.EventType.OPEN:
  241. openEventDispatched = true;
  242. break;
  243. case goog.ui.Component.EventType.CLOSE:
  244. closeEventDispatched = true;
  245. break;
  246. default:
  247. fail('Invalid event type: ' + e.type);
  248. }
  249. }
  250. menu.decorate(goog.dom.getElement('demoMenu'));
  251. var subMenu = menu.getChildAt(1);
  252. subMenu.setHighlighted(true);
  253. goog.events.listen(
  254. subMenu,
  255. [goog.ui.Component.EventType.OPEN, goog.ui.Component.EventType.CLOSE],
  256. handleEvent);
  257. assertFalse(
  258. 'Submenu must not have "-open" CSS class',
  259. goog.dom.classlist.contains(subMenu.getElement(), 'goog-submenu-open'));
  260. assertFalse('Popup menu must not be visible', subMenu.getMenu().isVisible());
  261. assertFalse('No OPEN event must have been dispatched', openEventDispatched);
  262. assertFalse('No CLOSE event must have been dispatched', closeEventDispatched);
  263. subMenu.showSubMenu();
  264. subMenu.getMenu().setVisible(false);
  265. subMenu.dismissSubMenu();
  266. assertFalse(
  267. 'Submenu must not have "-open" CSS class',
  268. goog.dom.classlist.contains(subMenu.getElement(), 'goog-submenu-open'));
  269. assertFalse(subMenu.menuIsVisible_);
  270. assertFalse('Popup menu must not be visible', subMenu.getMenu().isVisible());
  271. assertTrue('CLOSE event must have been dispatched', closeEventDispatched);
  272. goog.events.unlisten(
  273. subMenu,
  274. [goog.ui.Component.EventType.OPEN, goog.ui.Component.EventType.CLOSE],
  275. handleEvent);
  276. }
  277. function testCloseSubMenuBehavior() {
  278. menu.decorate(goog.dom.getElement('demoMenu'));
  279. var subMenu = menu.getChildAt(1);
  280. subMenu.getElement().id = 'subMenu';
  281. var innerMenu = subMenu.getMenu();
  282. innerMenu.getChildAt(0).getElement().id = 'child1';
  283. subMenu.setHighlighted(true);
  284. subMenu.showSubMenu();
  285. function MyFakeEvent(keyCode, opt_eventType) {
  286. this.type = opt_eventType || goog.events.KeyHandler.EventType.KEY;
  287. this.keyCode = keyCode;
  288. this.propagationStopped = false;
  289. this.preventDefault = goog.nullFunction;
  290. this.stopPropagation = function() { this.propagationStopped = true; };
  291. }
  292. // Focus on the first item in the submenu and verify the activedescendant is
  293. // set correctly.
  294. subMenu.handleKeyEvent(new MyFakeEvent(goog.events.KeyCodes.DOWN));
  295. assertEquals(
  296. 'First item in submenu must be the aria-activedescendant', 'child1',
  297. goog.a11y.aria.getState(
  298. menu.getElement(), goog.a11y.aria.State.ACTIVEDESCENDANT));
  299. // Dismiss the submenu and verify the activedescendant is updated correctly.
  300. subMenu.handleKeyEvent(new MyFakeEvent(goog.events.KeyCodes.LEFT));
  301. assertEquals(
  302. 'Submenu must be the aria-activedescendant', 'subMenu',
  303. goog.a11y.aria.getState(
  304. menu.getElement(), goog.a11y.aria.State.ACTIVEDESCENDANT));
  305. }
  306. function testLazyInstantiateSubMenu() {
  307. menu.decorate(goog.dom.getElement('demoMenu'));
  308. var subMenu = menu.getChildAt(1);
  309. subMenu.setHighlighted(true);
  310. var lazyMenu;
  311. var key = goog.events.listen(
  312. subMenu, goog.ui.Component.EventType.OPEN, function(e) {
  313. lazyMenu = new goog.ui.Menu();
  314. lazyMenu.addItem(new goog.ui.MenuItem('foo'));
  315. lazyMenu.addItem(new goog.ui.MenuItem('bar'));
  316. subMenu.setMenu(lazyMenu, /* opt_internal */ false);
  317. });
  318. subMenu.showSubMenu();
  319. assertNotNull('Popup menu must have been created', lazyMenu);
  320. assertEquals(
  321. 'Popup menu must be a child of the submenu', subMenu,
  322. lazyMenu.getParent());
  323. assertTrue('Popup menu must have been rendered', lazyMenu.isInDocument());
  324. assertTrue('Popup menu must be visible', lazyMenu.isVisible());
  325. menu.dispose();
  326. assertTrue('Submenu must have been disposed of', subMenu.isDisposed());
  327. assertFalse(
  328. 'Popup menu must not have been disposed of', lazyMenu.isDisposed());
  329. lazyMenu.dispose();
  330. goog.events.unlistenByKey(key);
  331. }
  332. function testReusableMenu() {
  333. var subMenuOne = new goog.ui.SubMenu('SubMenu One');
  334. var subMenuTwo = new goog.ui.SubMenu('SubMenu Two');
  335. menu.addItem(subMenuOne);
  336. menu.addItem(subMenuTwo);
  337. menu.render(goog.dom.getElement('sandbox'));
  338. // It is possible for the same popup menu to be shared between different
  339. // submenus.
  340. var sharedMenu = new goog.ui.Menu();
  341. sharedMenu.addItem(new goog.ui.MenuItem('Hello'));
  342. sharedMenu.addItem(new goog.ui.MenuItem('World'));
  343. assertNull('Shared menu must not have a parent', sharedMenu.getParent());
  344. subMenuOne.setMenu(sharedMenu);
  345. assertEquals(
  346. 'SubMenuOne must point to the shared menu', sharedMenu,
  347. subMenuOne.getMenu());
  348. assertEquals(
  349. 'SubMenuOne must be the shared menu\'s parent', subMenuOne,
  350. sharedMenu.getParent());
  351. subMenuTwo.setMenu(sharedMenu);
  352. assertEquals(
  353. 'SubMenuTwo must point to the shared menu', sharedMenu,
  354. subMenuTwo.getMenu());
  355. assertEquals(
  356. 'SubMenuTwo must be the shared menu\'s parent', subMenuTwo,
  357. sharedMenu.getParent());
  358. assertEquals(
  359. 'SubMenuOne must still point to the shared menu', sharedMenu,
  360. subMenuOne.getMenu());
  361. menu.setHighlighted(subMenuOne);
  362. subMenuOne.showSubMenu();
  363. assertEquals(
  364. 'SubMenuOne must point to the shared menu', sharedMenu,
  365. subMenuOne.getMenu());
  366. assertEquals(
  367. 'SubMenuOne must be the shared menu\'s parent', subMenuOne,
  368. sharedMenu.getParent());
  369. assertEquals(
  370. 'SubMenuTwo must still point to the shared menu', sharedMenu,
  371. subMenuTwo.getMenu());
  372. assertTrue('Shared menu must be visible', sharedMenu.isVisible());
  373. menu.setHighlighted(subMenuTwo);
  374. subMenuTwo.showSubMenu();
  375. assertEquals(
  376. 'SubMenuTwo must point to the shared menu', sharedMenu,
  377. subMenuTwo.getMenu());
  378. assertEquals(
  379. 'SubMenuTwo must be the shared menu\'s parent', subMenuTwo,
  380. sharedMenu.getParent());
  381. assertEquals(
  382. 'SubMenuOne must still point to the shared menu', sharedMenu,
  383. subMenuOne.getMenu());
  384. assertTrue('Shared menu must be visible', sharedMenu.isVisible());
  385. }
  386. /**
  387. * If you remove a submenu in the interval between when a mouseover event
  388. * is fired on it, and showSubMenu() is called, showSubMenu causes a null
  389. * value to be dereferenced. This test validates that the fix for this works.
  390. * (See bug 1823144).
  391. */
  392. function testDeleteItemDuringSubmenuDisplayInterval() {
  393. mockClock = new goog.testing.MockClock(true);
  394. var submenu = new goog.ui.SubMenu('submenu');
  395. submenu.addItem(new goog.ui.MenuItem('submenu item 1'));
  396. menu.addItem(submenu);
  397. // Trigger mouseover, and remove item before showSubMenu can be called.
  398. var e = new goog.events.Event();
  399. submenu.handleMouseOver(e);
  400. menu.removeItem(submenu);
  401. mockClock.tick(goog.ui.SubMenu.MENU_DELAY_MS);
  402. // (No JS error should occur.)
  403. }
  404. function testShowSubMenuAfterRemoval() {
  405. var submenu = new goog.ui.SubMenu('submenu');
  406. menu.addItem(submenu);
  407. menu.removeItem(submenu);
  408. submenu.showSubMenu();
  409. // (No JS error should occur.)
  410. }
  411. /**
  412. * Tests that if a sub menu is selectable, then it can handle actions.
  413. */
  414. function testSubmenuSelectable() {
  415. var submenu = new goog.ui.SubMenu('submenu');
  416. submenu.addItem(new goog.ui.MenuItem('submenu item 1'));
  417. menu.addItem(submenu);
  418. submenu.setSelectable(true);
  419. var numClicks = 0;
  420. var menuClickedFn = function(e) { numClicks++; };
  421. goog.events.listen(
  422. submenu, goog.ui.Component.EventType.ACTION, menuClickedFn);
  423. submenu.performActionInternal(null);
  424. submenu.performActionInternal(null);
  425. assertEquals('The submenu should have fired an event', 2, numClicks);
  426. submenu.setSelectable(false);
  427. submenu.performActionInternal(null);
  428. assertEquals(
  429. 'The submenu should not have fired any further events', 2, numClicks);
  430. }
  431. /**
  432. * Tests that if a sub menu is checkable, then it can handle actions.
  433. */
  434. function testSubmenuCheckable() {
  435. var submenu = new goog.ui.SubMenu('submenu');
  436. submenu.addItem(new goog.ui.MenuItem('submenu item 1'));
  437. menu.addItem(submenu);
  438. submenu.setCheckable(true);
  439. var numClicks = 0;
  440. var menuClickedFn = function(e) { numClicks++; };
  441. goog.events.listen(
  442. submenu, goog.ui.Component.EventType.ACTION, menuClickedFn);
  443. submenu.performActionInternal(null);
  444. submenu.performActionInternal(null);
  445. assertEquals('The submenu should have fired an event', 2, numClicks);
  446. submenu.setCheckable(false);
  447. submenu.performActionInternal(null);
  448. assertEquals(
  449. 'The submenu should not have fired any further events', 2, numClicks);
  450. }
  451. /**
  452. * Tests that entering a child menu cancels the dismiss timer for the submenu.
  453. */
  454. function testEnteringChildCancelsDismiss() {
  455. var submenu = new goog.ui.SubMenu('submenu');
  456. submenu.isInDocument = goog.functions.TRUE;
  457. submenu.addItem(new goog.ui.MenuItem('submenu item 1'));
  458. menu.addItem(submenu);
  459. mockClock = new goog.testing.MockClock(true);
  460. submenu.getMenu().setVisible(true);
  461. // This starts the dismiss timer.
  462. submenu.setHighlighted(false);
  463. // This should cancel the dismiss timer.
  464. submenu.getMenu().dispatchEvent(goog.ui.Component.EventType.ENTER);
  465. // Tick the length of the dismiss timer.
  466. mockClock.tick(goog.ui.SubMenu.MENU_DELAY_MS);
  467. // Check that the menu is now highlighted and still visible.
  468. assertTrue(submenu.getMenu().isVisible());
  469. assertTrue(submenu.isHighlighted());
  470. }
  471. /**
  472. * Asserts that this sub menu renders in the right direction relative to
  473. * the parent menu.
  474. * @param {goog.ui.SubMenu} subMenu The sub menu.
  475. * @param {boolean} left True for left-pointing, false for right-pointing.
  476. */
  477. function assertRenderDirection(subMenu, left) {
  478. subMenu.getParent().setHighlighted(subMenu);
  479. subMenu.showSubMenu();
  480. var menuItemPosition = goog.style.getPageOffset(subMenu.getElement());
  481. var menuPosition = goog.style.getPageOffset(subMenu.getMenu().getElement());
  482. assert(Math.abs(menuItemPosition.y - menuPosition.y) < 5);
  483. assertEquals(
  484. 'Menu at: ' + menuPosition.x + ', submenu item at: ' + menuItemPosition.x,
  485. left, menuPosition.x < menuItemPosition.x);
  486. }
  487. /**
  488. * Asserts that this sub menu has a properly-oriented arrow.
  489. * @param {goog.ui.SubMenu} subMenu The sub menu.
  490. * @param {boolean} left True for left-pointing, false for right-pointing.
  491. */
  492. function assertArrowDirection(subMenu, left) {
  493. assertEquals(
  494. left ? goog.ui.SubMenuRenderer.LEFT_ARROW_ :
  495. goog.ui.SubMenuRenderer.RIGHT_ARROW_,
  496. getArrowElement(subMenu).innerHTML);
  497. }
  498. /**
  499. * Asserts that the arrow position is correct.
  500. * @param {goog.ui.SubMenu} subMenu The sub menu.
  501. * @param {boolean} leftAlign True for left-aligned, false for right-aligned.
  502. */
  503. function assertArrowPosition(subMenu, left) {
  504. var arrow = getArrowElement(subMenu);
  505. var expectedLeft =
  506. left ? 0 : arrow.offsetParent.offsetWidth - arrow.offsetWidth;
  507. var actualLeft = arrow.offsetLeft;
  508. assertTrue(
  509. 'Expected left offset: ' + expectedLeft + '\n' +
  510. 'Actual left offset: ' + actualLeft + '\n',
  511. Math.abs(expectedLeft - actualLeft) < 5);
  512. }
  513. /**
  514. * Gets the arrow element of a sub menu.
  515. * @param {goog.ui.SubMenu} subMenu The sub menu.
  516. * @return {Element} The arrow.
  517. */
  518. function getArrowElement(subMenu) {
  519. return subMenu.getContentElement().lastChild;
  520. }