menubuttonrenderer_test.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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.MenuButtonRendererTest');
  15. goog.setTestOnly('goog.ui.MenuButtonRendererTest');
  16. goog.require('goog.a11y.aria');
  17. goog.require('goog.a11y.aria.State');
  18. goog.require('goog.dom');
  19. goog.require('goog.dom.TagName');
  20. goog.require('goog.dom.classlist');
  21. goog.require('goog.testing.jsunit');
  22. goog.require('goog.testing.ui.rendererasserts');
  23. goog.require('goog.ui.MenuButton');
  24. goog.require('goog.ui.MenuButtonRenderer');
  25. goog.require('goog.userAgent');
  26. var decoratedButton;
  27. var renderedButton;
  28. var savedRootTree;
  29. function setUp() {
  30. savedRootTree = goog.dom.getElement('root').cloneNode(true);
  31. decoratedButton = null;
  32. renderedButton = null;
  33. }
  34. function tearDown() {
  35. if (decoratedButton) {
  36. decoratedButton.dispose();
  37. }
  38. if (renderedButton) {
  39. renderedButton.dispose();
  40. }
  41. var root = goog.dom.getElement('root');
  42. root.parentNode.replaceChild(savedRootTree, root);
  43. }
  44. function testRendererWithTextContent() {
  45. renderedButton = new goog.ui.MenuButton('Foo');
  46. renderOnParent(renderedButton);
  47. checkButtonCaption(renderedButton);
  48. checkAriaState(renderedButton);
  49. decoratedButton = new goog.ui.MenuButton();
  50. decorateDemoButton(decoratedButton);
  51. checkButtonCaption(decoratedButton);
  52. checkAriaState(decoratedButton);
  53. assertButtonsEqual();
  54. }
  55. function testRendererWithNodeContent() {
  56. renderedButton = new goog.ui.MenuButton(
  57. goog.dom.createDom(goog.dom.TagName.DIV, null, 'Foo'));
  58. renderOnParent(renderedButton);
  59. var contentEl = renderedButton.getContentElement();
  60. if (goog.userAgent.IE || goog.userAgent.OPERA) {
  61. assertHTMLEquals('<div unselectable="on">Foo</div>', contentEl.innerHTML);
  62. } else {
  63. assertHTMLEquals('<div>Foo</div>', contentEl.innerHTML);
  64. }
  65. assertTrue(hasInlineBlock(contentEl));
  66. }
  67. function testSetContent() {
  68. renderedButton = new goog.ui.MenuButton();
  69. renderOnParent(renderedButton);
  70. var contentEl = renderedButton.getContentElement();
  71. assertHTMLEquals('', contentEl.innerHTML);
  72. renderedButton.setContent('Foo');
  73. contentEl = renderedButton.getContentElement();
  74. assertHTMLEquals('Foo', contentEl.innerHTML);
  75. assertTrue(hasInlineBlock(contentEl));
  76. renderedButton.setContent(
  77. goog.dom.createDom(goog.dom.TagName.DIV, null, 'Bar'));
  78. contentEl = renderedButton.getContentElement();
  79. assertHTMLEquals('<div>Bar</div>', contentEl.innerHTML);
  80. renderedButton.setContent('Foo');
  81. contentEl = renderedButton.getContentElement();
  82. assertHTMLEquals('Foo', contentEl.innerHTML);
  83. }
  84. function assertButtonsEqual() {
  85. assertHTMLEquals(
  86. 'Rendered button and decorated button produced different HTML!',
  87. renderedButton.getElement().innerHTML,
  88. decoratedButton.getElement().innerHTML);
  89. }
  90. /**
  91. * Render the given button as a child of 'parent'.
  92. * @param {goog.ui.Button} button A button with content 'Foo'.
  93. */
  94. function renderOnParent(button) {
  95. button.render(goog.dom.getElement('parent'));
  96. }
  97. /**
  98. * Decaorate the button with id 'button'.
  99. * @param {goog.ui.Button} button A button with no content.
  100. */
  101. function decorateDemoButton(button) {
  102. button.decorate(goog.dom.getElement('decoratedButton'));
  103. }
  104. /**
  105. * Verify that the button's caption is never the direct
  106. * child of an inline-block element.
  107. * @param {goog.ui.Button} button A button.
  108. */
  109. function checkButtonCaption(button) {
  110. var contentElement = button.getContentElement();
  111. assertEquals('Foo', contentElement.innerHTML);
  112. assertTrue(hasInlineBlock(contentElement));
  113. assert(hasInlineBlock(contentElement.parentNode));
  114. button.setContent('Bar');
  115. contentElement = button.getContentElement();
  116. assertEquals('Bar', contentElement.innerHTML);
  117. assertTrue(hasInlineBlock(contentElement));
  118. assert(hasInlineBlock(contentElement.parentNode));
  119. }
  120. /**
  121. * Verify that the menu button has the correct ARIA attributes
  122. * @param {goog.ui.Button} button A button.
  123. */
  124. function checkAriaState(button) {
  125. assertEquals(
  126. 'menu buttons should have default aria-expanded == false', 'false',
  127. goog.a11y.aria.getState(
  128. button.getElement(), goog.a11y.aria.State.EXPANDED));
  129. button.setOpen(true);
  130. assertEquals(
  131. 'menu buttons should not aria-expanded == true after ' +
  132. 'opening',
  133. 'true', goog.a11y.aria.getState(
  134. button.getElement(), goog.a11y.aria.State.EXPANDED));
  135. }
  136. function hasInlineBlock(el) {
  137. return goog.dom.classlist.contains(el, 'goog-inline-block');
  138. }
  139. function testDoesntCallGetCssClassInConstructor() {
  140. goog.testing.ui.rendererasserts.assertNoGetCssClassCallsInConstructor(
  141. goog.ui.MenuButtonRenderer);
  142. }