| 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.NativeButtonRendererTest'); |
| 16 |
goog.setTestOnly('goog.ui.NativeButtonRendererTest'); |
| 17 |
|
| 18 |
goog.require('goog.dom'); |
| 19 |
goog.require('goog.dom.TagName'); |
| 20 |
goog.require('goog.dom.classlist'); |
| 21 |
goog.require('goog.events'); |
| 22 |
goog.require('goog.testing.ExpectedFailures'); |
| 23 |
goog.require('goog.testing.events'); |
| 24 |
goog.require('goog.testing.jsunit'); |
| 25 |
goog.require('goog.testing.ui.rendererasserts'); |
| 26 |
goog.require('goog.ui.Button'); |
| 27 |
goog.require('goog.ui.Component'); |
| 28 |
goog.require('goog.ui.NativeButtonRenderer'); |
| 29 |
goog.require('goog.userAgent'); |
| 30 |
|
| 31 |
var sandbox; |
| 32 |
var renderer; |
| 33 |
var expectedFailures; |
| 34 |
var button; |
| 35 |
|
| 36 |
function setUpPage() { |
| 37 |
sandbox = goog.dom.getElement('sandbox'); |
| 38 |
renderer = goog.ui.NativeButtonRenderer.getInstance(); |
| 39 |
expectedFailures = new goog.testing.ExpectedFailures(); |
| 40 |
} |
| 41 |
|
| 42 |
function setUp() { |
| 43 |
button = new goog.ui.Button('Hello', renderer); |
| 44 |
} |
| 45 |
|
| 46 |
function tearDown() { |
| 47 |
button.dispose(); |
| 48 |
goog.dom.removeChildren(sandbox); |
| 49 |
expectedFailures.handleTearDown(); |
| 50 |
} |
| 51 |
|
| 52 |
function testConstructor() { |
| 53 |
assertNotNull('Renderer must not be null', renderer); |
| 54 |
} |
| 55 |
|
| 56 |
function testGetAriaRole() { |
| 57 |
assertUndefined('ARIA role must be undefined', renderer.getAriaRole()); |
| 58 |
} |
| 59 |
|
| 60 |
function testCreateDom() { |
| 61 |
button.setTooltip('Hello, world!'); |
| 62 |
button.setValue('foo'); |
| 63 |
var element = renderer.createDom(button); |
| 64 |
assertNotNull('Element must not be null', element); |
| 65 |
assertEquals('Element must be a button', |
| 66 |
String(goog.dom.TagName.BUTTON), element.tagName); |
| 67 |
assertSameElements( |
| 68 |
'Button element must have expected class name', ['goog-button'], |
| 69 |
goog.dom.classlist.get(element)); |
| 70 |
assertFalse('Button element must be enabled', element.disabled); |
| 71 |
assertEquals( |
| 72 |
'Button element must have expected title', 'Hello, world!', |
| 73 |
element.title); |
| 74 |
// Expected to fail on IE. |
| 75 |
expectedFailures.expectFailureFor(goog.userAgent.IE); |
| 76 |
try { |
| 77 |
// See http://www.fourmilab.ch/fourmilog/archives/2007-03/000824.html |
| 78 |
// for a description of the problem. |
| 79 |
assertEquals( |
| 80 |
'Button element must have expected value', 'foo', element.value); |
| 81 |
assertEquals( |
| 82 |
'Button element must have expected contents', 'Hello', |
| 83 |
element.innerHTML); |
| 84 |
} catch (e) { |
| 85 |
expectedFailures.handleException(e); |
| 86 |
} |
| 87 |
assertFalse( |
| 88 |
'Button must not handle its own mouse events', |
| 89 |
button.isHandleMouseEvents()); |
| 90 |
assertFalse( |
| 91 |
'Button must not support the custom FOCUSED state', |
| 92 |
button.isSupportedState(goog.ui.Component.State.FOCUSED)); |
| 93 |
} |
| 94 |
|
| 95 |
function testCanDecorate() { |
| 96 |
sandbox.innerHTML = '<button id="buttonElement">Button</button>\n' + |
| 97 |
'<input id="inputButton" type="button" value="Input Button">\n' + |
| 98 |
'<input id="inputSubmit" type="submit" value="Input Submit">\n' + |
| 99 |
'<input id="inputReset" type="reset" value="Input Reset">\n' + |
| 100 |
'<input id="inputText" type="text" size="10">\n' + |
| 101 |
'<div id="divButton" class="goog-button">Hello</div>'; |
| 102 |
|
| 103 |
assertTrue( |
| 104 |
'Must be able to decorate <button>', |
| 105 |
renderer.canDecorate(goog.dom.getElement('buttonElement'))); |
| 106 |
assertTrue( |
| 107 |
'Must be able to decorate <input type="button">', |
| 108 |
renderer.canDecorate(goog.dom.getElement('inputButton'))); |
| 109 |
assertTrue( |
| 110 |
'Must be able to decorate <input type="submit">', |
| 111 |
renderer.canDecorate(goog.dom.getElement('inputSubmit'))); |
| 112 |
assertTrue( |
| 113 |
'Must be able to decorate <input type="reset">', |
| 114 |
renderer.canDecorate(goog.dom.getElement('inputReset'))); |
| 115 |
assertFalse( |
| 116 |
'Must not be able to decorate <input type="text">', |
| 117 |
renderer.canDecorate(goog.dom.getElement('inputText'))); |
| 118 |
assertFalse( |
| 119 |
'Must not be able to decorate <div class="goog-button">', |
| 120 |
renderer.canDecorate(goog.dom.getElement('divButton'))); |
| 121 |
} |
| 122 |
|
| 123 |
function testDecorate() { |
| 124 |
sandbox.innerHTML = |
| 125 |
'<button id="foo" title="Hello!" value="bar">Foo Button</button>\n' + |
| 126 |
'<button id="disabledButton" value="bah" disabled="disabled">Disabled' + |
| 127 |
'</button>'; |
| 128 |
|
| 129 |
var element = renderer.decorate(button, goog.dom.getElement('foo')); |
| 130 |
assertEquals( |
| 131 |
'Decorated element must be as expected', goog.dom.getElement('foo'), |
| 132 |
element); |
| 133 |
assertEquals( |
| 134 |
'Decorated button title must have expected value', 'Hello!', |
| 135 |
button.getTooltip()); |
| 136 |
// Expected to fail on IE. |
| 137 |
expectedFailures.expectFailureFor(goog.userAgent.IE); |
| 138 |
try { |
| 139 |
// See http://www.fourmilab.ch/fourmilog/archives/2007-03/000824.html |
| 140 |
// for a description of the problem. |
| 141 |
assertEquals( |
| 142 |
'Decorated button value must have expected value', 'bar', |
| 143 |
button.getValue()); |
| 144 |
} catch (e) { |
| 145 |
expectedFailures.handleException(e); |
| 146 |
} |
| 147 |
assertFalse( |
| 148 |
'Button must not handle its own mouse events', |
| 149 |
button.isHandleMouseEvents()); |
| 150 |
assertFalse( |
| 151 |
'Button must not support the custom FOCUSED state', |
| 152 |
button.isSupportedState(goog.ui.Component.State.FOCUSED)); |
| 153 |
|
| 154 |
element = renderer.decorate(button, goog.dom.getElement('disabledButton')); |
| 155 |
assertFalse('Decorated button must be disabled', button.isEnabled()); |
| 156 |
assertSameElements( |
| 157 |
'Decorated button must have expected class names', |
| 158 |
['goog-button', 'goog-button-disabled'], goog.dom.classlist.get(element)); |
| 159 |
// Expected to fail on IE. |
| 160 |
expectedFailures.expectFailureFor(goog.userAgent.IE); |
| 161 |
try { |
| 162 |
// See http://www.fourmilab.ch/fourmilog/archives/2007-03/000824.html |
| 163 |
// for a description of the problem. |
| 164 |
assertEquals( |
| 165 |
'Decorated button value must have expected value', 'bah', |
| 166 |
button.getValue()); |
| 167 |
} catch (e) { |
| 168 |
expectedFailures.handleException(e); |
| 169 |
} |
| 170 |
assertFalse( |
| 171 |
'Button must not handle its own mouse events', |
| 172 |
button.isHandleMouseEvents()); |
| 173 |
assertFalse( |
| 174 |
'Button must not support the custom FOCUSED state', |
| 175 |
button.isSupportedState(goog.ui.Component.State.FOCUSED)); |
| 176 |
} |
| 177 |
|
| 178 |
function testInitializeDom() { |
| 179 |
var dispatchedActionCount = 0; |
| 180 |
var handleAction = function() { dispatchedActionCount++; }; |
| 181 |
goog.events.listen(button, goog.ui.Component.EventType.ACTION, handleAction); |
| 182 |
|
| 183 |
button.render(sandbox); |
| 184 |
goog.testing.events.fireClickSequence(button.getElement()); |
| 185 |
assertEquals( |
| 186 |
'Button must have dispatched ACTION on click', 1, dispatchedActionCount); |
| 187 |
|
| 188 |
goog.events.unlisten( |
| 189 |
button, goog.ui.Component.EventType.ACTION, handleAction); |
| 190 |
} |
| 191 |
|
| 192 |
function testIsFocusable() { |
| 193 |
assertTrue('Enabled button must be focusable', renderer.isFocusable(button)); |
| 194 |
button.setEnabled(false); |
| 195 |
assertFalse( |
| 196 |
'Disabled button must not be focusable', renderer.isFocusable(button)); |
| 197 |
} |
| 198 |
|
| 199 |
function testSetState() { |
| 200 |
button.render(sandbox); |
| 201 |
assertFalse( |
| 202 |
'Button element must not be disabled', button.getElement().disabled); |
| 203 |
renderer.setState(button, goog.ui.Component.State.DISABLED, true); |
| 204 |
assertTrue('Button element must be disabled', button.getElement().disabled); |
| 205 |
} |
| 206 |
|
| 207 |
function testGetValue() { |
| 208 |
sandbox.innerHTML = '<button id="foo" value="blah">Hello</button>'; |
| 209 |
// Expected to fail on IE. |
| 210 |
expectedFailures.expectFailureFor(goog.userAgent.IE); |
| 211 |
try { |
| 212 |
// See http://www.fourmilab.ch/fourmilog/archives/2007-03/000824.html |
| 213 |
// for a description of the problem. |
| 214 |
assertEquals( |
| 215 |
'Value must be as expected', 'blah', |
| 216 |
renderer.getValue(goog.dom.getElement('foo'))); |
| 217 |
} catch (e) { |
| 218 |
expectedFailures.handleException(e); |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
function testSetValue() { |
| 223 |
button.render(sandbox); |
| 224 |
renderer.setValue(button.getElement(), 'What?'); |
| 225 |
assertEquals( |
| 226 |
'Button must have expected value', 'What?', |
| 227 |
renderer.getValue(button.getElement())); |
| 228 |
} |
| 229 |
|
| 230 |
function testDoesntCallGetCssClassInConstructor() { |
| 231 |
goog.testing.ui.rendererasserts.assertNoGetCssClassCallsInConstructor( |
| 232 |
goog.ui.NativeButtonRenderer); |
| 233 |
} |