menuitem_test.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  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.MenuItemTest');
  15. goog.setTestOnly('goog.ui.MenuItemTest');
  16. goog.require('goog.a11y.aria');
  17. goog.require('goog.a11y.aria.Role');
  18. goog.require('goog.array');
  19. goog.require('goog.dom');
  20. goog.require('goog.dom.NodeType');
  21. goog.require('goog.dom.TagName');
  22. goog.require('goog.dom.classlist');
  23. goog.require('goog.events.KeyCodes');
  24. goog.require('goog.html.testing');
  25. goog.require('goog.math.Coordinate');
  26. goog.require('goog.testing.events');
  27. goog.require('goog.testing.jsunit');
  28. goog.require('goog.testing.recordFunction');
  29. goog.require('goog.ui.Component');
  30. goog.require('goog.ui.MenuItem');
  31. goog.require('goog.ui.MenuItemRenderer');
  32. var sandbox;
  33. var item;
  34. function setUp() {
  35. sandbox = goog.dom.getElement('sandbox');
  36. item = new goog.ui.MenuItem('Item');
  37. }
  38. function tearDown() {
  39. item.dispose();
  40. goog.dom.removeChildren(sandbox);
  41. }
  42. function testMenuItem() {
  43. assertNotNull('Instance must not be null', item);
  44. assertEquals(
  45. 'Renderer must default to MenuItemRenderer singleton',
  46. goog.ui.MenuItemRenderer.getInstance(), item.getRenderer());
  47. assertEquals('Content must have expected value', 'Item', item.getContent());
  48. assertEquals(
  49. 'Caption must default to the content', item.getContent(),
  50. item.getCaption());
  51. assertEquals(
  52. 'Value must default to the caption', item.getCaption(), item.getValue());
  53. }
  54. function testMenuItemConstructor() {
  55. var model = 'Hello';
  56. var fakeDom = {};
  57. var fakeRenderer = {};
  58. var menuItem = new goog.ui.MenuItem('Item', model, fakeDom, fakeRenderer);
  59. assertEquals(
  60. 'Content must have expected value', 'Item', menuItem.getContent());
  61. assertEquals(
  62. 'Caption must default to the content', menuItem.getContent(),
  63. menuItem.getCaption());
  64. assertEquals('Model must be set', model, menuItem.getModel());
  65. assertNotEquals(
  66. 'Value must not equal the caption', menuItem.getCaption(),
  67. menuItem.getValue());
  68. assertEquals('Value must equal the model', model, menuItem.getValue());
  69. assertEquals('DomHelper must be set', fakeDom, menuItem.getDomHelper());
  70. assertEquals('Renderer must be set', fakeRenderer, menuItem.getRenderer());
  71. }
  72. function testGetValue() {
  73. assertUndefined('Model must be undefined by default', item.getModel());
  74. assertEquals(
  75. 'Without a model, value must default to the caption', item.getCaption(),
  76. item.getValue());
  77. item.setModel('Foo');
  78. assertEquals(
  79. 'With a model, value must default to the model', item.getModel(),
  80. item.getValue());
  81. }
  82. function testSetValue() {
  83. assertUndefined('Model must be undefined by default', item.getModel());
  84. assertEquals(
  85. 'Without a model, value must default to the caption', item.getCaption(),
  86. item.getValue());
  87. item.setValue(17);
  88. assertEquals('Value must be set', 17, item.getValue());
  89. assertEquals(
  90. 'Value and model must be the same', item.getValue(), item.getModel());
  91. }
  92. function testGetSetContent() {
  93. assertEquals('Content must have expected value', 'Item', item.getContent());
  94. item.setContent(goog.dom.createDom(goog.dom.TagName.DIV, 'foo', 'Foo'));
  95. assertEquals(
  96. 'Content must be an element', goog.dom.NodeType.ELEMENT,
  97. item.getContent().nodeType);
  98. assertHTMLEquals(
  99. 'Content must be the expected element', '<div class="foo">Foo</div>',
  100. goog.dom.getOuterHtml(item.getContent()));
  101. }
  102. function testGetSetCaption() {
  103. assertEquals('Caption must have expected value', 'Item', item.getCaption());
  104. item.setCaption('Hello, world!');
  105. assertTrue('Caption must be a string', goog.isString(item.getCaption()));
  106. assertEquals(
  107. 'Caption must have expected value', 'Hello, world!', item.getCaption());
  108. item.setContent(goog.dom.createDom(goog.dom.TagName.DIV, 'foo', 'Foo'));
  109. assertTrue('Caption must be a string', goog.isString(item.getCaption()));
  110. assertEquals('Caption must have expected value', 'Foo', item.getCaption());
  111. }
  112. function testGetSetContentAfterCreateDom() {
  113. item.createDom();
  114. assertEquals('Content must have expected value', 'Item', item.getContent());
  115. item.setContent(goog.dom.createDom(goog.dom.TagName.DIV, 'foo', 'Foo'));
  116. assertEquals(
  117. 'Content must be an element', goog.dom.NodeType.ELEMENT,
  118. item.getContent().nodeType);
  119. assertHTMLEquals(
  120. 'Content must be the expected element', '<div class="foo">Foo</div>',
  121. goog.dom.getOuterHtml(item.getContent()));
  122. }
  123. function testGetSetCaptionAfterCreateDom() {
  124. item.createDom();
  125. assertEquals('Caption must have expected value', 'Item', item.getCaption());
  126. item.setCaption('Hello, world!');
  127. assertTrue('Caption must be a string', goog.isString(item.getCaption()));
  128. assertEquals(
  129. 'Caption must have expected value', 'Hello, world!', item.getCaption());
  130. item.setContent(goog.dom.createDom(goog.dom.TagName.DIV, 'foo', 'Foo'));
  131. assertTrue('Caption must be a string', goog.isString(item.getCaption()));
  132. assertEquals('Caption must have expected value', 'Foo', item.getCaption());
  133. var arrayContent = goog.array.clone(
  134. goog.dom.safeHtmlToNode(goog.html.testing.newSafeHtmlForTest(
  135. ' <b> \xa0foo</b><i> bar</i> ')).childNodes);
  136. item.setContent(arrayContent);
  137. assertEquals(
  138. 'whitespaces must be normalized in the caption', '\xa0foo bar',
  139. item.getCaption());
  140. }
  141. function testSetSelectable() {
  142. assertFalse(
  143. 'Item must not be selectable by default',
  144. item.isSupportedState(goog.ui.Component.State.SELECTED));
  145. item.setSelectable(true);
  146. assertTrue(
  147. 'Item must be selectable',
  148. item.isSupportedState(goog.ui.Component.State.SELECTED));
  149. item.setSelected(true);
  150. assertTrue('Item must be selected', item.isSelected());
  151. assertFalse('Item must not be checked', item.isChecked());
  152. item.setSelectable(false);
  153. assertFalse(
  154. 'Item must not no longer be selectable',
  155. item.isSupportedState(goog.ui.Component.State.SELECTED));
  156. assertFalse('Item must no longer be selected', item.isSelected());
  157. assertFalse('Item must not be checked', item.isChecked());
  158. }
  159. function testSetCheckable() {
  160. assertFalse(
  161. 'Item must not be checkable by default',
  162. item.isSupportedState(goog.ui.Component.State.CHECKED));
  163. item.setCheckable(true);
  164. assertTrue(
  165. 'Item must be checkable',
  166. item.isSupportedState(goog.ui.Component.State.CHECKED));
  167. item.setChecked(true);
  168. assertTrue('Item must be checked', item.isChecked());
  169. assertFalse('Item must not be selected', item.isSelected());
  170. item.setCheckable(false);
  171. assertFalse(
  172. 'Item must not no longer be checkable',
  173. item.isSupportedState(goog.ui.Component.State.CHECKED));
  174. assertFalse('Item must no longer be checked', item.isChecked());
  175. assertFalse('Item must not be selected', item.isSelected());
  176. }
  177. function testSetSelectableBeforeCreateDom() {
  178. item.setSelectable(true);
  179. item.createDom();
  180. assertTrue(
  181. 'Item must have checkbox structure',
  182. item.getRenderer().hasCheckBoxStructure(item.getElement()));
  183. item.setSelectable(false);
  184. assertFalse(
  185. 'Item must no longer have checkbox structure',
  186. item.getRenderer().hasCheckBoxStructure(item.getElement()));
  187. }
  188. function testSetCheckableBeforeCreateDom() {
  189. item.setCheckable(true);
  190. item.createDom();
  191. assertTrue(
  192. 'Item must have checkbox structure',
  193. item.getRenderer().hasCheckBoxStructure(item.getElement()));
  194. assertEquals(
  195. 'Element must have ARIA role menuitemcheckbox',
  196. goog.a11y.aria.Role.MENU_ITEM_CHECKBOX,
  197. goog.a11y.aria.getRole(item.getElement()));
  198. item.setCheckable(false);
  199. assertFalse(
  200. 'Item must no longer have checkbox structure',
  201. item.getRenderer().hasCheckBoxStructure(item.getElement()));
  202. }
  203. function testSetSelectableAfterCreateDom() {
  204. item.createDom();
  205. item.setSelectable(true);
  206. assertTrue(
  207. 'Item must have checkbox structure',
  208. item.getRenderer().hasCheckBoxStructure(item.getElement()));
  209. assertEquals(
  210. 'Element must have ARIA role menuitemradio',
  211. goog.a11y.aria.Role.MENU_ITEM_RADIO,
  212. goog.a11y.aria.getRole(item.getElement()));
  213. item.setSelectable(false);
  214. assertFalse(
  215. 'Item must no longer have checkbox structure',
  216. item.getRenderer().hasCheckBoxStructure(item.getElement()));
  217. }
  218. function testSetCheckableAfterCreateDom() {
  219. item.createDom();
  220. item.setCheckable(true);
  221. assertTrue(
  222. 'Item must have checkbox structure',
  223. item.getRenderer().hasCheckBoxStructure(item.getElement()));
  224. item.setCheckable(false);
  225. assertFalse(
  226. 'Item must no longer have checkbox structure',
  227. item.getRenderer().hasCheckBoxStructure(item.getElement()));
  228. }
  229. function testSelectableBehavior() {
  230. item.setSelectable(true);
  231. item.render(sandbox);
  232. assertFalse('Item must not be selected by default', item.isSelected());
  233. item.performActionInternal();
  234. assertTrue('Item must be selected', item.isSelected());
  235. item.performActionInternal();
  236. assertTrue('Item must still be selected', item.isSelected());
  237. }
  238. function testCheckableBehavior() {
  239. item.setCheckable(true);
  240. item.render(sandbox);
  241. assertFalse('Item must not be checked by default', item.isChecked());
  242. item.performActionInternal();
  243. assertTrue('Item must be checked', item.isChecked());
  244. item.performActionInternal();
  245. assertFalse('Item must no longer be checked', item.isChecked());
  246. }
  247. function testGetSetContentForItemWithCheckBox() {
  248. item.setSelectable(true);
  249. item.createDom();
  250. assertTrue(
  251. 'Item must have checkbox structure',
  252. item.getRenderer().hasCheckBoxStructure(item.getElement()));
  253. assertEquals(
  254. 'getContent() must not return the checkbox structure', 'Item',
  255. item.getContent());
  256. item.setContent('Hello');
  257. assertEquals(
  258. 'getContent() must not return the checkbox structure', 'Hello',
  259. item.getContent());
  260. assertTrue(
  261. 'Item must still have checkbox structure',
  262. item.getRenderer().hasCheckBoxStructure(item.getElement()));
  263. item.setContent(goog.dom.createDom(goog.dom.TagName.SPAN, 'foo', 'Foo'));
  264. assertEquals(
  265. 'getContent() must return element', goog.dom.NodeType.ELEMENT,
  266. item.getContent().nodeType);
  267. assertTrue(
  268. 'Item must still have checkbox structure',
  269. item.getRenderer().hasCheckBoxStructure(item.getElement()));
  270. item.setContent(null);
  271. assertNull('getContent() must return null', item.getContent());
  272. assertTrue(
  273. 'Item must still have checkbox structure',
  274. item.getRenderer().hasCheckBoxStructure(item.getElement()));
  275. }
  276. function testGetSetCaptionForItemWithCheckBox() {
  277. item.setCheckable(true);
  278. item.createDom();
  279. assertTrue(
  280. 'Item must have checkbox structure',
  281. item.getRenderer().hasCheckBoxStructure(item.getElement()));
  282. assertEquals(
  283. 'getCaption() must not return the checkbox structure', 'Item',
  284. item.getCaption());
  285. item.setCaption('Hello');
  286. assertEquals(
  287. 'getCaption() must not return the checkbox structure', 'Hello',
  288. item.getCaption());
  289. assertTrue(
  290. 'Item must still have checkbox structure',
  291. item.getRenderer().hasCheckBoxStructure(item.getElement()));
  292. item.setContent(goog.dom.createDom(goog.dom.TagName.SPAN, 'foo', 'Foo'));
  293. assertEquals(
  294. 'getCaption() must return text content', 'Foo', item.getCaption());
  295. assertTrue(
  296. 'Item must still have checkbox structure',
  297. item.getRenderer().hasCheckBoxStructure(item.getElement()));
  298. item.setCaption('');
  299. assertEquals('getCaption() must return empty string', '', item.getCaption());
  300. assertTrue(
  301. 'Item must still have checkbox structure',
  302. item.getRenderer().hasCheckBoxStructure(item.getElement()));
  303. }
  304. function testGetSetCaptionForItemWithAccelerators() {
  305. var contentArr = [];
  306. contentArr.push(
  307. goog.dom.createDom(
  308. goog.dom.TagName.SPAN, goog.getCssName('goog-menuitem-accel'),
  309. 'Ctrl+1'));
  310. contentArr.push(goog.dom.createTextNode('Hello'));
  311. item.setCaption(contentArr);
  312. assertEquals(
  313. 'getCaption() must not return the accelerator', 'Hello',
  314. item.getCaption());
  315. item.setCaption([goog.dom.createDom(
  316. goog.dom.TagName.SPAN, goog.getCssName('goog-menuitem-accel'),
  317. 'Ctrl+1')]);
  318. assertEquals('getCaption() must return empty string', '', item.getCaption());
  319. assertEquals(
  320. 'getAccelerator() should return the accelerator', 'Ctrl+1',
  321. item.getAccelerator());
  322. }
  323. function testGetSetCaptionForItemWithMnemonics() {
  324. var contentArr = [];
  325. contentArr.push(
  326. goog.dom.createDom(
  327. goog.dom.TagName.SPAN, goog.getCssName('goog-menuitem-mnemonic-hint'),
  328. 'H'));
  329. contentArr.push(goog.dom.createTextNode('ello'));
  330. item.setCaption(contentArr);
  331. assertEquals(
  332. 'getCaption() must not return hint markup', 'Hello', item.getCaption());
  333. contentArr = [];
  334. contentArr.push(goog.dom.createTextNode('Hello'));
  335. contentArr.push(
  336. goog.dom.createDom(
  337. goog.dom.TagName.SPAN,
  338. goog.getCssName('goog-menuitem-mnemonic-separator'), '(',
  339. goog.dom.createDom(
  340. goog.dom.TagName.SPAN,
  341. goog.getCssName('goog-menuitem-mnemonic-hint'), 'J'),
  342. ')'));
  343. item.setCaption(contentArr);
  344. assertEquals(
  345. 'getCaption() must not return the paranethetical mnemonic', 'Hello',
  346. item.getCaption());
  347. item.setCaption('');
  348. assertEquals(
  349. 'getCaption() must return the empty string', '', item.getCaption());
  350. }
  351. function testHandleKeyEventInternalWithMnemonic() {
  352. item.performActionInternal =
  353. goog.testing.recordFunction(item.performActionInternal);
  354. item.setMnemonic(goog.events.KeyCodes.F);
  355. item.handleKeyEventInternal({'keyCode': goog.events.KeyCodes.F});
  356. assertEquals(
  357. 'performActionInternal must be called', 1,
  358. item.performActionInternal.getCallCount());
  359. }
  360. function testHandleKeyEventInternalWithoutMnemonic() {
  361. item.performActionInternal =
  362. goog.testing.recordFunction(item.performActionInternal);
  363. item.handleKeyEventInternal({'keyCode': goog.events.KeyCodes.F});
  364. assertEquals(
  365. 'performActionInternal must not be called without a' +
  366. ' mnemonic',
  367. 0, item.performActionInternal.getCallCount());
  368. }
  369. function testRender() {
  370. item.render(sandbox);
  371. var contentElement = item.getContentElement();
  372. assertNotNull('Content element must exist', contentElement);
  373. assertTrue(
  374. 'Content element must have expected class name',
  375. goog.dom.classlist.contains(
  376. contentElement,
  377. item.getRenderer().getStructuralCssClass() + '-content'));
  378. assertHTMLEquals(
  379. 'Content element must have expected structure', 'Item',
  380. contentElement.innerHTML);
  381. }
  382. function testRenderSelectableItem() {
  383. item.setSelectable(true);
  384. item.render(sandbox);
  385. assertTrue(
  386. 'Item must have checkbox structure',
  387. item.getRenderer().hasCheckBoxStructure(item.getElement()));
  388. assertEquals('getCaption() return expected value', 'Item', item.getCaption());
  389. }
  390. function testRenderSelectedItem() {
  391. item.setSelectable(true);
  392. item.setSelected(true);
  393. item.render(sandbox);
  394. assertTrue(
  395. 'Item must have checkbox structure',
  396. item.getRenderer().hasCheckBoxStructure(item.getElement()));
  397. assertTrue(
  398. 'Item must have selected style',
  399. goog.dom.classlist.contains(
  400. item.getElement(), item.getRenderer().getClassForState(
  401. goog.ui.Component.State.SELECTED)));
  402. }
  403. function testRenderCheckableItem() {
  404. item.setCheckable(true);
  405. item.render(sandbox);
  406. assertTrue(
  407. 'Item must have checkbox structure',
  408. item.getRenderer().hasCheckBoxStructure(item.getElement()));
  409. assertEquals('getCaption() return expected value', 'Item', item.getCaption());
  410. }
  411. function testRenderCheckedItem() {
  412. item.setCheckable(true);
  413. item.setChecked(true);
  414. item.render(sandbox);
  415. assertTrue(
  416. 'Item must have checkbox structure',
  417. item.getRenderer().hasCheckBoxStructure(item.getElement()));
  418. assertTrue(
  419. 'Item must have checked style',
  420. goog.dom.classlist.contains(
  421. item.getElement(), item.getRenderer().getClassForState(
  422. goog.ui.Component.State.CHECKED)));
  423. }
  424. function testDecorate() {
  425. sandbox.innerHTML = '<div id="foo">Foo</div>';
  426. var foo = goog.dom.getElement('foo');
  427. item.decorate(foo);
  428. assertEquals('Decorated element must be as expected', foo, item.getElement());
  429. assertTrue(
  430. 'Decorated element must have expected class name',
  431. goog.dom.classlist.contains(
  432. item.getElement(), item.getRenderer().getCssClass()));
  433. assertEquals(
  434. 'Content element must be the decorated element\'s child',
  435. item.getContentElement(), item.getElement().firstChild);
  436. assertHTMLEquals(
  437. 'Content must have expected structure', 'Foo',
  438. item.getContentElement().innerHTML);
  439. }
  440. function testDecorateCheckableItem() {
  441. sandbox.innerHTML = '<div id="foo" class="goog-option">Foo</div>';
  442. var foo = goog.dom.getElement('foo');
  443. item.decorate(foo);
  444. assertEquals('Decorated element must be as expected', foo, item.getElement());
  445. assertTrue(
  446. 'Decorated element must have expected class name',
  447. goog.dom.classlist.contains(
  448. item.getElement(), item.getRenderer().getCssClass()));
  449. assertEquals(
  450. 'Content element must be the decorated element\'s child',
  451. item.getContentElement(), item.getElement().firstChild);
  452. assertTrue(
  453. 'Item must have checkbox structure',
  454. item.getRenderer().hasCheckBoxStructure(item.getElement()));
  455. assertFalse('Item must not be checked', item.isChecked());
  456. }
  457. function testDecorateCheckedItem() {
  458. sandbox.innerHTML =
  459. '<div id="foo" class="goog-option goog-option-selected">Foo</div>';
  460. var foo = goog.dom.getElement('foo');
  461. item.decorate(foo);
  462. assertEquals('Decorated element must be as expected', foo, item.getElement());
  463. assertSameElements(
  464. 'Decorated element must have expected class names',
  465. ['goog-menuitem', 'goog-option', 'goog-option-selected'],
  466. goog.dom.classlist.get(item.getElement()));
  467. assertEquals(
  468. 'Content element must be the decorated element\'s child',
  469. item.getContentElement(), item.getElement().firstChild);
  470. assertTrue(
  471. 'Item must have checkbox structure',
  472. item.getRenderer().hasCheckBoxStructure(item.getElement()));
  473. assertTrue('Item must be checked', item.isChecked());
  474. }
  475. function testDecorateTemplate() {
  476. sandbox.innerHTML = '<div id="foo" class="goog-menuitem">' +
  477. '<div class="goog-menuitem-content">Foo</div></div>';
  478. var foo = goog.dom.getElement('foo');
  479. item.decorate(foo);
  480. assertEquals('Decorated element must be as expected', foo, item.getElement());
  481. assertTrue(
  482. 'Decorated element must have expected class name',
  483. goog.dom.classlist.contains(
  484. item.getElement(), item.getRenderer().getCssClass()));
  485. assertEquals(
  486. 'Content element must be the decorated element\'s child',
  487. item.getContentElement(), item.getElement().firstChild);
  488. assertHTMLEquals(
  489. 'Content must have expected structure', 'Foo',
  490. item.getContentElement().innerHTML);
  491. }
  492. function testDecorateCheckableItemTemplate() {
  493. sandbox.innerHTML = '<div id="foo" class="goog-menuitem goog-option">' +
  494. '<div class="goog-menuitem-content">' +
  495. '<div class="goog-menuitem-checkbox"></div>' +
  496. 'Foo</div></div>';
  497. var foo = goog.dom.getElement('foo');
  498. item.decorate(foo);
  499. assertEquals('Decorated element must be as expected', foo, item.getElement());
  500. assertTrue(
  501. 'Decorated element must have expected class name',
  502. goog.dom.classlist.contains(
  503. item.getElement(), item.getRenderer().getCssClass()));
  504. assertEquals(
  505. 'Content element must be the decorated element\'s child',
  506. item.getContentElement(), item.getElement().firstChild);
  507. assertTrue(
  508. 'Item must have checkbox structure',
  509. item.getRenderer().hasCheckBoxStructure(item.getElement()));
  510. assertEquals(
  511. 'Item must have exactly one checkbox structure', 1,
  512. goog.dom
  513. .getElementsByTagNameAndClass(
  514. goog.dom.TagName.DIV, 'goog-menuitem-checkbox', item.getElement())
  515. .length);
  516. assertFalse('Item must not be checked', item.isChecked());
  517. }
  518. function testDecorateCheckedItemTemplate() {
  519. sandbox.innerHTML = '<div id="foo" ' +
  520. 'class="goog-menuitem goog-option goog-option-selected">' +
  521. '<div class="goog-menuitem-content">' +
  522. '<div class="goog-menuitem-checkbox"></div>' +
  523. 'Foo</div></div>';
  524. var foo = goog.dom.getElement('foo');
  525. item.decorate(foo);
  526. assertEquals('Decorated element must be as expected', foo, item.getElement());
  527. assertSameElements(
  528. 'Decorated element must have expected class names',
  529. ['goog-menuitem', 'goog-option', 'goog-option-selected'],
  530. goog.dom.classlist.get(item.getElement()));
  531. assertEquals(
  532. 'Content element must be the decorated element\'s child',
  533. item.getContentElement(), item.getElement().firstChild);
  534. assertTrue(
  535. 'Item must have checkbox structure',
  536. item.getRenderer().hasCheckBoxStructure(item.getElement()));
  537. assertEquals(
  538. 'Item must have exactly one checkbox structure', 1,
  539. goog.dom
  540. .getElementsByTagNameAndClass(
  541. goog.dom.TagName.DIV, 'goog-menuitem-checkbox', item.getElement())
  542. .length);
  543. assertTrue('Item must be checked', item.isChecked());
  544. }
  545. /** @bug 1463524 */
  546. function testHandleMouseUp() {
  547. var COORDS_1 = new goog.math.Coordinate(1, 1);
  548. var COORDS_2 = new goog.math.Coordinate(2, 2);
  549. item.setActive(true);
  550. // Override performActionInternal() for testing purposes.
  551. var actionPerformed;
  552. item.performActionInternal = function() {
  553. actionPerformed = true;
  554. return true;
  555. };
  556. item.render(sandbox);
  557. // Scenario 1: item has no parent.
  558. actionPerformed = false;
  559. item.setActive(true);
  560. goog.testing.events.fireMouseUpEvent(item.getElement());
  561. assertTrue('Action should be performed on mouseup', actionPerformed);
  562. // Scenario 2: item has a parent.
  563. actionPerformed = false;
  564. item.setActive(true);
  565. var parent = new goog.ui.Component();
  566. var parentElem = goog.dom.getElement('parentComponent');
  567. parent.render(parentElem);
  568. parent.addChild(item);
  569. parent.openingCoords = COORDS_1;
  570. goog.testing.events.fireMouseUpEvent(item.getElement(), undefined, COORDS_2);
  571. assertTrue('Action should be performed on mouseup', actionPerformed);
  572. // Scenario 3: item has a parent which was opened during mousedown, and
  573. // item, and now the mouseup fires at the same coords.
  574. actionPerformed = false;
  575. item.setActive(true);
  576. parent.openingCoords = COORDS_2;
  577. goog.testing.events.fireMouseUpEvent(item.getElement(), undefined, COORDS_2);
  578. assertFalse('Action should not be performed on mouseup', actionPerformed);
  579. }
  580. function testSetAriaLabel() {
  581. assertNull('Item must not have aria label by default', item.getAriaLabel());
  582. item.setAriaLabel('Item 1');
  583. item.render(sandbox);
  584. var el = item.getElementStrict();
  585. assertEquals(
  586. 'Item element must have expected aria-label', 'Item 1',
  587. el.getAttribute('aria-label'));
  588. assertEquals(
  589. 'Item element must have expected aria-role', 'menuitem',
  590. el.getAttribute('role'));
  591. item.setAriaLabel('Item 2');
  592. assertEquals(
  593. 'Item element must have updated aria-label', 'Item 2',
  594. el.getAttribute('aria-label'));
  595. assertEquals(
  596. 'Item element must have expected aria-role', 'menuitem',
  597. el.getAttribute('role'));
  598. }