Debian Codegraph @

closure-library/closure/goog/ui/style/app/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.style.app.MenuButtonRendererTest');
16 goog.setTestOnly('goog.ui.style.app.MenuButtonRendererTest');
17
18 goog.require('goog.dom');
19 goog.require('goog.testing.jsunit');
20 goog.require('goog.testing.ui.style');
21 goog.require('goog.ui.Component');
22 goog.require('goog.ui.MenuButton');
23 goog.require('goog.ui.style.app.MenuButtonRenderer');
24 var renderer = goog.ui.style.app.MenuButtonRenderer.getInstance();
25 var button;
26
27 // Write iFrame tag to load reference FastUI markup. Then, our tests will
28 // compare the generated markup to the reference markup.
29 var refPath = '../../../../../webutil/css/fastui/app/menubutton_spec.html';
30 goog.testing.ui.style.writeReferenceFrame(refPath);
31
32 function shouldRunTests() {
33 // Disable tests when being run as a part of open-source repo as the button
34 // specs are not included in closure-library.
35 return !(/closure\/goog\/ui/.test(location.pathname));
36 }
37
38 function setUp() {
39 button = new goog.ui.MenuButton('Hello Generated', null, renderer);
40 button.setTooltip('Click for Generated');
41 }
42
43 function tearDown() {
44 if (button) {
45 button.dispose();
46 }
47 goog.dom.removeChildren(goog.dom.getElement('sandbox'));
48 }
49
50 function testGeneratedButton() {
51 button.render(goog.dom.getElement('sandbox'));
52 goog.testing.ui.style.assertStructureMatchesReference(
53 button.getElement(), 'normal-resting');
54 assertEquals(
55 'Hello Generated', button.getContentElement().firstChild.nodeValue);
56 assertEquals(
57 'Click for Generated', button.getElement().getAttribute('title'));
58 }
59
60 function testButtonStates() {
61 button.render(goog.dom.getElement('sandbox'));
62 goog.testing.ui.style.assertStructureMatchesReference(
63 button.getElement(), 'normal-resting');
64 button.setState(goog.ui.Component.State.HOVER, true);
65 goog.testing.ui.style.assertStructureMatchesReference(
66 button.getElement(), 'normal-hover');
67 button.setState(goog.ui.Component.State.HOVER, false);
68 button.setState(goog.ui.Component.State.FOCUSED, true);
69 goog.testing.ui.style.assertStructureMatchesReference(
70 button.getElement(), 'normal-focused');
71 button.setState(goog.ui.Component.State.FOCUSED, false);
72 button.setState(goog.ui.Component.State.ACTIVE, true);
73 goog.testing.ui.style.assertStructureMatchesReference(
74 button.getElement(), 'normal-active');
75 button.setState(goog.ui.Component.State.ACTIVE, false);
76 button.setState(goog.ui.Component.State.DISABLED, true);
77 goog.testing.ui.style.assertStructureMatchesReference(
78 button.getElement(), 'normal-disabled');
79 }
80
81 function testDecoratedButton() {
82 button.decorate(goog.dom.getElement('button'));
83 goog.testing.ui.style.assertStructureMatchesReference(
84 button.getElement(), 'normal-resting');
85 assertEquals(
86 'Hello Decorated', button.getContentElement().firstChild.nodeValue);
87 assertEquals(
88 'Click for Decorated', button.getElement().getAttribute('title'));
89 }
90
91 function testDecoratedButtonBox() {
92 button.decorate(goog.dom.getElement('button-box'));
93 goog.testing.ui.style.assertStructureMatchesReference(
94 button.getElement(), 'normal-resting');
95 assertEquals(
96 'Hello Decorated Box', button.getContentElement().firstChild.nodeValue);
97 assertEquals(
98 'Click for Decorated Box', button.getElement().getAttribute('title'));
99 }
100
101 function testExistingContentIsUsed() {
102 button.decorate(goog.dom.getElement('button-with-dom-content'));
103 goog.testing.ui.style.assertStructureMatchesReference(
104 button.getElement(), 'normal-resting');
105 // Safari 3 adds style="-webkit-user-select" to the strong tag, so we
106 // can't simply look at the HTML.
107 var content = button.getContentElement();
108 assertEquals(
109 'Unexpected number of child nodes; expected existing number ' +
110 'plus one for the dropdown element',
111 4, content.childNodes.length);
112 assertEquals('Unexpected tag', 'STRONG', content.childNodes[0].tagName);
113 assertEquals(
114 'Unexpected text content', 'Hello Strong',
115 content.childNodes[0].innerHTML);
116 assertEquals('Unexpected tag', 'EM', content.childNodes[2].tagName);
117 assertEquals(
118 'Unexpected text content', 'Box', content.childNodes[2].innerHTML);
119 }
120
121 function testDecoratedButtonWithMenu() {
122 button.decorate(goog.dom.getElement('button-with-menu'));
123 assertEquals('Unexpected number of menu items', 2, button.getItemCount());
124 goog.testing.ui.style.assertStructureMatchesReference(
125 button.getElement(), 'normal-resting');
126 assertFalse(
127 'Expected menu element to not be contained by button',
128 goog.dom.contains(button.getElement(), button.getMenu().getElement()));
129 }
130
131 function testDropDownExistsAfterButtonRename() {
132 button.decorate(goog.dom.getElement('button-2'));
133 button.setContent('New title');
134 goog.testing.ui.style.assertStructureMatchesReference(
135 button.getElement(), 'normal-resting');
136 assertEquals(
137 'Unexpected number of child nodes; expected text element ' +
138 'and the dropdown element',
139 2, button.getContentElement().childNodes.length);
140 assertEquals('New title', button.getContentElement().firstChild.nodeValue);
141 assertEquals(
142 'Click for Decorated', button.getElement().getAttribute('title'));
143 }