| 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.MenuItemRendererTest'); |
| 16 |
goog.setTestOnly('goog.ui.MenuItemRendererTest'); |
| 17 |
|
| 18 |
goog.require('goog.a11y.aria'); |
| 19 |
goog.require('goog.a11y.aria.Role'); |
| 20 |
goog.require('goog.a11y.aria.State'); |
| 21 |
goog.require('goog.dom'); |
| 22 |
goog.require('goog.dom.classlist'); |
| 23 |
goog.require('goog.testing.jsunit'); |
| 24 |
goog.require('goog.testing.ui.rendererasserts'); |
| 25 |
goog.require('goog.ui.Component'); |
| 26 |
goog.require('goog.ui.MenuItem'); |
| 27 |
goog.require('goog.ui.MenuItemRenderer'); |
| 28 |
|
| 29 |
var sandbox; |
| 30 |
var item, renderer; |
| 31 |
|
| 32 |
function setUp() { |
| 33 |
sandbox = goog.dom.getElement('sandbox'); |
| 34 |
item = new goog.ui.MenuItem('Hello'); |
| 35 |
renderer = goog.ui.MenuItemRenderer.getInstance(); |
| 36 |
} |
| 37 |
|
| 38 |
function tearDown() { |
| 39 |
item.dispose(); |
| 40 |
goog.dom.removeChildren(sandbox); |
| 41 |
} |
| 42 |
|
| 43 |
function testMenuItemRenderer() { |
| 44 |
assertNotNull('Instance must not be null', renderer); |
| 45 |
assertEquals( |
| 46 |
'Singleton getter must always return same instance', renderer, |
| 47 |
goog.ui.MenuItemRenderer.getInstance()); |
| 48 |
} |
| 49 |
|
| 50 |
function testCreateDom() { |
| 51 |
var element = renderer.createDom(item); |
| 52 |
assertNotNull('Element must not be null', element); |
| 53 |
assertSameElements( |
| 54 |
'Element must have the expected class names', ['goog-menuitem'], |
| 55 |
goog.dom.classlist.get(element)); |
| 56 |
assertEquals( |
| 57 |
'Element must have exactly one child element', 1, |
| 58 |
element.childNodes.length); |
| 59 |
assertHTMLEquals( |
| 60 |
'Child element must have the expected structure', |
| 61 |
'<div class="goog-menuitem-content">Hello</div>', element.innerHTML); |
| 62 |
} |
| 63 |
|
| 64 |
function testCreateDomWithHoverState() { |
| 65 |
item.setHighlighted(true); |
| 66 |
var element = renderer.createDom(item); |
| 67 |
assertSameElements( |
| 68 |
'Element must have the expected class names', |
| 69 |
['goog-menuitem', 'goog-menuitem-highlight'], |
| 70 |
goog.dom.classlist.get(element)); |
| 71 |
} |
| 72 |
|
| 73 |
function testCreateDomForCheckableItem() { |
| 74 |
item.setSupportedState(goog.ui.Component.State.CHECKED, true); |
| 75 |
item.render(); |
| 76 |
var element = item.getElement(); |
| 77 |
assertNotNull(element); |
| 78 |
assertSameElements( |
| 79 |
'Element must have the expected class names', |
| 80 |
['goog-menuitem', 'goog-option'], goog.dom.classlist.get(element)); |
| 81 |
assertEquals( |
| 82 |
'Element must have ARIA role menuitemcheckbox', |
| 83 |
goog.a11y.aria.Role.MENU_ITEM_CHECKBOX, goog.a11y.aria.getRole(element)); |
| 84 |
|
| 85 |
item.setChecked(true); |
| 86 |
assertTrue('Item must be checked', item.isChecked()); |
| 87 |
assertSameElements( |
| 88 |
'Checked item must have the expected class names', |
| 89 |
['goog-menuitem', 'goog-option', 'goog-option-selected'], |
| 90 |
goog.dom.classlist.get(element)); |
| 91 |
assertEquals( |
| 92 |
'Item must have checked ARIA state', 'true', |
| 93 |
goog.a11y.aria.getState(element, goog.a11y.aria.State.CHECKED)); |
| 94 |
} |
| 95 |
|
| 96 |
function testCreateUpdateDomForCheckableItem() { |
| 97 |
// Render the item first, then update its supported states to include CHECKED. |
| 98 |
item.render(); |
| 99 |
var element = item.getElement(); |
| 100 |
item.setSupportedState(goog.ui.Component.State.CHECKED, true); |
| 101 |
assertNotNull(element); |
| 102 |
assertSameElements( |
| 103 |
'Element must have the expected class names', |
| 104 |
['goog-menuitem', 'goog-option'], goog.dom.classlist.get(element)); |
| 105 |
assertEquals( |
| 106 |
'Element must have ARIA role menuitemcheckbox', |
| 107 |
goog.a11y.aria.Role.MENU_ITEM_CHECKBOX, goog.a11y.aria.getRole(element)); |
| 108 |
|
| 109 |
// Now actually check the item. |
| 110 |
item.setChecked(true); |
| 111 |
assertTrue('Item must be checked', item.isChecked()); |
| 112 |
assertSameElements( |
| 113 |
'Checked item must have the expected class names', |
| 114 |
['goog-menuitem', 'goog-option', 'goog-option-selected'], |
| 115 |
goog.dom.classlist.get(element)); |
| 116 |
assertEquals( |
| 117 |
'Item must have checked ARIA state', 'true', |
| 118 |
goog.a11y.aria.getState(element, goog.a11y.aria.State.CHECKED)); |
| 119 |
} |
| 120 |
|
| 121 |
function testCreateDomForSelectableItem() { |
| 122 |
item.setSupportedState(goog.ui.Component.State.SELECTED, true); |
| 123 |
item.render(); |
| 124 |
var element = item.getElement(); |
| 125 |
assertNotNull(element); |
| 126 |
assertSameElements( |
| 127 |
'Element must have the expected class names', |
| 128 |
['goog-menuitem', 'goog-option'], goog.dom.classlist.get(element)); |
| 129 |
assertEquals( |
| 130 |
'Element must have ARIA role menuitemradio', |
| 131 |
goog.a11y.aria.Role.MENU_ITEM_RADIO, goog.a11y.aria.getRole(element)); |
| 132 |
|
| 133 |
item.setSelected(true); |
| 134 |
assertTrue('Item must be selected', item.isSelected()); |
| 135 |
assertSameElements( |
| 136 |
'Selected item must have the expected class names', |
| 137 |
['goog-menuitem', 'goog-option', 'goog-option-selected'], |
| 138 |
goog.dom.classlist.get(element)); |
| 139 |
assertEquals( |
| 140 |
'Item must have selected ARIA state', 'true', |
| 141 |
goog.a11y.aria.getState(element, goog.a11y.aria.State.CHECKED)); |
| 142 |
} |
| 143 |
|
| 144 |
function testCreateUpdateDomForSelectableItem() { |
| 145 |
// Render the item first, then update its supported states to include |
| 146 |
// SELECTED. |
| 147 |
item.render(); |
| 148 |
var element = item.getElement(); |
| 149 |
item.setSupportedState(goog.ui.Component.State.SELECTED, true); |
| 150 |
assertNotNull(element); |
| 151 |
assertSameElements( |
| 152 |
'Element must have the expected class names', |
| 153 |
['goog-menuitem', 'goog-option'], goog.dom.classlist.get(element)); |
| 154 |
assertEquals( |
| 155 |
'Element must have ARIA role menuitemradio', |
| 156 |
goog.a11y.aria.Role.MENU_ITEM_RADIO, goog.a11y.aria.getRole(element)); |
| 157 |
|
| 158 |
// Now actually select the item. |
| 159 |
item.setSelected(true); |
| 160 |
assertTrue('Item must be selected', item.isSelected()); |
| 161 |
assertSameElements( |
| 162 |
'Selected item must have the expected class names', |
| 163 |
['goog-menuitem', 'goog-option', 'goog-option-selected'], |
| 164 |
goog.dom.classlist.get(element)); |
| 165 |
assertEquals( |
| 166 |
'Item must have selected ARIA state', 'true', |
| 167 |
goog.a11y.aria.getState(element, goog.a11y.aria.State.CHECKED)); |
| 168 |
} |
| 169 |
|
| 170 |
function testGetContentElement() { |
| 171 |
assertNull( |
| 172 |
'Content element must be the null initially', item.getContentElement()); |
| 173 |
item.createDom(); |
| 174 |
assertEquals( |
| 175 |
'Content element must be the element\'s first child', |
| 176 |
item.getElement().firstChild, item.getContentElement()); |
| 177 |
} |
| 178 |
|
| 179 |
function testDecorate() { |
| 180 |
sandbox.innerHTML = '<div id="foo">Hello</div>'; |
| 181 |
var foo = goog.dom.getElement('foo'); |
| 182 |
|
| 183 |
var element = renderer.decorate(item, foo); |
| 184 |
assertSameElements( |
| 185 |
'Element must have the expected class names', ['goog-menuitem'], |
| 186 |
goog.dom.classlist.get(element)); |
| 187 |
assertEquals( |
| 188 |
'Element must have exactly one child element', 1, |
| 189 |
element.childNodes.length); |
| 190 |
assertHTMLEquals( |
| 191 |
'Child element must have the expected structure', |
| 192 |
'<div class="goog-menuitem-content">Hello</div>', element.innerHTML); |
| 193 |
} |
| 194 |
|
| 195 |
function testDecorateWithContentStructure() { |
| 196 |
sandbox.innerHTML = |
| 197 |
'<div id="foo"><div class="goog-menuitem-content">Hello</div></div>'; |
| 198 |
var foo = goog.dom.getElement('foo'); |
| 199 |
|
| 200 |
var element = renderer.decorate(item, foo); |
| 201 |
assertSameElements( |
| 202 |
'Element must have the expected class names', ['goog-menuitem'], |
| 203 |
goog.dom.classlist.get(element)); |
| 204 |
assertEquals( |
| 205 |
'Element must have exactly one child element', 1, |
| 206 |
element.childNodes.length); |
| 207 |
assertHTMLEquals( |
| 208 |
'Child element must have the expected structure', |
| 209 |
'<div class="goog-menuitem-content">Hello</div>', element.innerHTML); |
| 210 |
} |
| 211 |
|
| 212 |
function testDecorateWithHoverState() { |
| 213 |
sandbox.innerHTML = |
| 214 |
'<div id="foo" class="goog-menuitem-highlight">Hello</div>'; |
| 215 |
var foo = goog.dom.getElement('foo'); |
| 216 |
|
| 217 |
assertFalse('Item must not be highlighted', item.isHighlighted()); |
| 218 |
var element = renderer.decorate(item, foo); |
| 219 |
assertSameElements( |
| 220 |
'Element must have the expected class names', |
| 221 |
['goog-menuitem', 'goog-menuitem-highlight'], |
| 222 |
goog.dom.classlist.get(element)); |
| 223 |
assertTrue('Item must be highlighted', item.isHighlighted()); |
| 224 |
} |
| 225 |
|
| 226 |
function testDecorateCheckableItem() { |
| 227 |
sandbox.innerHTML = '<div id="foo" class="goog-option">Hello</div>'; |
| 228 |
var foo = goog.dom.getElement('foo'); |
| 229 |
|
| 230 |
assertFalse( |
| 231 |
'Item must not be checkable', |
| 232 |
item.isSupportedState(goog.ui.Component.State.CHECKED)); |
| 233 |
var element = renderer.decorate(item, foo); |
| 234 |
assertSameElements( |
| 235 |
'Element must have the expected class names', |
| 236 |
['goog-menuitem', 'goog-option'], goog.dom.classlist.get(element)); |
| 237 |
assertTrue( |
| 238 |
'Item must be checkable', |
| 239 |
item.isSupportedState(goog.ui.Component.State.CHECKED)); |
| 240 |
assertHTMLEquals( |
| 241 |
'Child element must have the expected structure', |
| 242 |
'<div class="goog-menuitem-content">' + |
| 243 |
'<div class="goog-menuitem-checkbox"></div>Hello</div>', |
| 244 |
element.innerHTML); |
| 245 |
} |
| 246 |
|
| 247 |
function testSetContent() { |
| 248 |
item.setSupportedState(goog.ui.Component.State.CHECKED, true); |
| 249 |
var element = renderer.createDom(item); |
| 250 |
assertHTMLEquals( |
| 251 |
'Child element must have the expected structure', |
| 252 |
'<div class="goog-menuitem-content">' + |
| 253 |
'<div class="goog-menuitem-checkbox"></div>Hello</div>', |
| 254 |
element.innerHTML); |
| 255 |
|
| 256 |
renderer.setContent(element, 'Goodbye'); |
| 257 |
assertHTMLEquals( |
| 258 |
'Child element must have the expected structure', |
| 259 |
'<div class="goog-menuitem-content">' + |
| 260 |
'<div class="goog-menuitem-checkbox"></div>Goodbye</div>', |
| 261 |
element.innerHTML); |
| 262 |
} |
| 263 |
|
| 264 |
function testDoesntCallGetCssClassInConstructor() { |
| 265 |
goog.testing.ui.rendererasserts.assertNoGetCssClassCallsInConstructor( |
| 266 |
goog.ui.MenuItemRenderer); |
| 267 |
} |