Debian Codegraph @

closure-library/closure/goog/ui/menubuttonrenderer_test.js

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