Debian Codegraph @

closure-library/closure/goog/ui/style/app/buttonrenderer_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.ButtonRendererTest');
16 goog.setTestOnly('goog.ui.style.app.ButtonRendererTest');
17
18 goog.require('goog.dom');
19 goog.require('goog.testing.jsunit');
20 goog.require('goog.testing.ui.style');
21 goog.require('goog.ui.Button');
22 goog.require('goog.ui.Component');
23 goog.require('goog.ui.style.app.ButtonRenderer');
24 goog.require('goog.userAgent');
25 var renderer = goog.ui.style.app.ButtonRenderer.getInstance();
26 var button;
27
28 // Write iFrame tag to load reference FastUI markup. Then, our tests will
29 // compare the generated markup to the reference markup.
30 var refPath = '../../../../../webutil/css/fastui/app/button_spec.html';
31 goog.testing.ui.style.writeReferenceFrame(refPath);
32
33 function shouldRunTests() {
34 // Disable tests when being run as a part of open-source repo as the button
35 // specs are not included in closure-library.
36 return !(/closure\/goog\/ui/.test(location.pathname));
37 }
38
39 function setUp() {
40 button = new goog.ui.Button('Hello Generated', renderer);
41 button.setTooltip('Click for Generated');
42 }
43
44 function tearDown() {
45 if (button) {
46 button.dispose();
47 }
48 goog.dom.removeChildren(goog.dom.getElement('sandbox'));
49 }
50
51 function testGeneratedButton() {
52 button.render(goog.dom.getElement('sandbox'));
53 goog.testing.ui.style.assertStructureMatchesReference(
54 button.getElement(), 'normal-resting');
55 assertEquals('Hello Generated', button.getContentElement().innerHTML);
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('Hello Decorated', button.getContentElement().innerHTML);
86 assertEquals(
87 'Click for Decorated', button.getElement().getAttribute('title'));
88 }
89
90 function testDecoratedButtonBox() {
91 button.decorate(goog.dom.getElement('button-box'));
92 goog.testing.ui.style.assertStructureMatchesReference(
93 button.getElement(), 'normal-resting');
94 assertEquals('Hello Decorated Box', button.getContentElement().innerHTML);
95 assertEquals(
96 'Click for Decorated Box', button.getElement().getAttribute('title'));
97 }
98
99 /*
100 * This test demonstrates what happens when you put whitespace in a
101 * decorated button's content, and the decorated element 'hasBoxStructure'.
102 * Note that this behavior is different than when the element does not
103 * have box structure. Should this be fixed?
104 */
105 function testDecoratedButtonBoxWithSpaceInContent() {
106 button.decorate(goog.dom.getElement('button-box-with-space-in-content'));
107 goog.testing.ui.style.assertStructureMatchesReference(
108 button.getElement(), 'normal-resting');
109 if (goog.userAgent.IE && !goog.userAgent.isDocumentModeOrHigher(9)) {
110 assertEquals(
111 'Hello Decorated Box with space ',
112 button.getContentElement().innerHTML);
113 } else {
114 assertEquals(
115 '\n Hello Decorated Box with space\n ',
116 button.getContentElement().innerHTML);
117 }
118 }
119
120 function testExistingContentIsUsed() {
121 button.decorate(goog.dom.getElement('button-box-with-dom-content'));
122 goog.testing.ui.style.assertStructureMatchesReference(
123 button.getElement(), 'normal-resting');
124 // Safari 3 adds style="-webkit-user-select" to the strong tag, so we
125 // can't simply look at the HTML.
126 var content = button.getContentElement();
127 assertEquals(
128 'Unexpected number of child nodes', 3, content.childNodes.length);
129 assertEquals('Unexpected tag', 'STRONG', content.childNodes[0].tagName);
130 assertEquals(
131 'Unexpected text content', 'Hello', content.childNodes[0].innerHTML);
132 assertEquals('Unexpected tag', 'EM', content.childNodes[2].tagName);
133 assertEquals(
134 'Unexpected text content', 'Box', content.childNodes[2].innerHTML);
135 }