| 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.MenuItemTest'); |
| 16 |
goog.setTestOnly('goog.ui.MenuItemTest'); |
| 17 |
|
| 18 |
goog.require('goog.a11y.aria'); |
| 19 |
goog.require('goog.a11y.aria.Role'); |
| 20 |
goog.require('goog.array'); |
| 21 |
goog.require('goog.dom'); |
| 22 |
goog.require('goog.dom.NodeType'); |
| 23 |
goog.require('goog.dom.TagName'); |
| 24 |
goog.require('goog.dom.classlist'); |
| 25 |
goog.require('goog.events.KeyCodes'); |
| 26 |
goog.require('goog.html.testing'); |
| 27 |
goog.require('goog.math.Coordinate'); |
| 28 |
goog.require('goog.testing.events'); |
| 29 |
goog.require('goog.testing.jsunit'); |
| 30 |
goog.require('goog.testing.recordFunction'); |
| 31 |
goog.require('goog.ui.Component'); |
| 32 |
goog.require('goog.ui.MenuItem'); |
| 33 |
goog.require('goog.ui.MenuItemRenderer'); |
| 34 |
|
| 35 |
var sandbox; |
| 36 |
var item; |
| 37 |
|
| 38 |
function setUp() { |
| 39 |
sandbox = goog.dom.getElement('sandbox'); |
| 40 |
item = new goog.ui.MenuItem('Item'); |
| 41 |
} |
| 42 |
|
| 43 |
function tearDown() { |
| 44 |
item.dispose(); |
| 45 |
goog.dom.removeChildren(sandbox); |
| 46 |
} |
| 47 |
|
| 48 |
function testMenuItem() { |
| 49 |
assertNotNull('Instance must not be null', item); |
| 50 |
assertEquals( |
| 51 |
'Renderer must default to MenuItemRenderer singleton', |
| 52 |
goog.ui.MenuItemRenderer.getInstance(), item.getRenderer()); |
| 53 |
assertEquals('Content must have expected value', 'Item', item.getContent()); |
| 54 |
assertEquals( |
| 55 |
'Caption must default to the content', item.getContent(), |
| 56 |
item.getCaption()); |
| 57 |
assertEquals( |
| 58 |
'Value must default to the caption', item.getCaption(), item.getValue()); |
| 59 |
} |
| 60 |
|
| 61 |
function testMenuItemConstructor() { |
| 62 |
var model = 'Hello'; |
| 63 |
var fakeDom = {}; |
| 64 |
var fakeRenderer = {}; |
| 65 |
|
| 66 |
var menuItem = new goog.ui.MenuItem('Item', model, fakeDom, fakeRenderer); |
| 67 |
assertEquals( |
| 68 |
'Content must have expected value', 'Item', menuItem.getContent()); |
| 69 |
assertEquals( |
| 70 |
'Caption must default to the content', menuItem.getContent(), |
| 71 |
menuItem.getCaption()); |
| 72 |
assertEquals('Model must be set', model, menuItem.getModel()); |
| 73 |
assertNotEquals( |
| 74 |
'Value must not equal the caption', menuItem.getCaption(), |
| 75 |
menuItem.getValue()); |
| 76 |
assertEquals('Value must equal the model', model, menuItem.getValue()); |
| 77 |
assertEquals('DomHelper must be set', fakeDom, menuItem.getDomHelper()); |
| 78 |
assertEquals('Renderer must be set', fakeRenderer, menuItem.getRenderer()); |
| 79 |
} |
| 80 |
|
| 81 |
function testGetValue() { |
| 82 |
assertUndefined('Model must be undefined by default', item.getModel()); |
| 83 |
assertEquals( |
| 84 |
'Without a model, value must default to the caption', item.getCaption(), |
| 85 |
item.getValue()); |
| 86 |
item.setModel('Foo'); |
| 87 |
assertEquals( |
| 88 |
'With a model, value must default to the model', item.getModel(), |
| 89 |
item.getValue()); |
| 90 |
} |
| 91 |
|
| 92 |
function testSetValue() { |
| 93 |
assertUndefined('Model must be undefined by default', item.getModel()); |
| 94 |
assertEquals( |
| 95 |
'Without a model, value must default to the caption', item.getCaption(), |
| 96 |
item.getValue()); |
| 97 |
item.setValue(17); |
| 98 |
assertEquals('Value must be set', 17, item.getValue()); |
| 99 |
assertEquals( |
| 100 |
'Value and model must be the same', item.getValue(), item.getModel()); |
| 101 |
} |
| 102 |
|
| 103 |
function testGetSetContent() { |
| 104 |
assertEquals('Content must have expected value', 'Item', item.getContent()); |
| 105 |
item.setContent(goog.dom.createDom(goog.dom.TagName.DIV, 'foo', 'Foo')); |
| 106 |
assertEquals( |
| 107 |
'Content must be an element', goog.dom.NodeType.ELEMENT, |
| 108 |
item.getContent().nodeType); |
| 109 |
assertHTMLEquals( |
| 110 |
'Content must be the expected element', '<div class="foo">Foo</div>', |
| 111 |
goog.dom.getOuterHtml(item.getContent())); |
| 112 |
} |
| 113 |
|
| 114 |
function testGetSetCaption() { |
| 115 |
assertEquals('Caption must have expected value', 'Item', item.getCaption()); |
| 116 |
item.setCaption('Hello, world!'); |
| 117 |
assertTrue('Caption must be a string', goog.isString(item.getCaption())); |
| 118 |
assertEquals( |
| 119 |
'Caption must have expected value', 'Hello, world!', item.getCaption()); |
| 120 |
item.setContent(goog.dom.createDom(goog.dom.TagName.DIV, 'foo', 'Foo')); |
| 121 |
assertTrue('Caption must be a string', goog.isString(item.getCaption())); |
| 122 |
assertEquals('Caption must have expected value', 'Foo', item.getCaption()); |
| 123 |
} |
| 124 |
|
| 125 |
function testGetSetContentAfterCreateDom() { |
| 126 |
item.createDom(); |
| 127 |
assertEquals('Content must have expected value', 'Item', item.getContent()); |
| 128 |
item.setContent(goog.dom.createDom(goog.dom.TagName.DIV, 'foo', 'Foo')); |
| 129 |
assertEquals( |
| 130 |
'Content must be an element', goog.dom.NodeType.ELEMENT, |
| 131 |
item.getContent().nodeType); |
| 132 |
assertHTMLEquals( |
| 133 |
'Content must be the expected element', '<div class="foo">Foo</div>', |
| 134 |
goog.dom.getOuterHtml(item.getContent())); |
| 135 |
} |
| 136 |
|
| 137 |
function testGetSetCaptionAfterCreateDom() { |
| 138 |
item.createDom(); |
| 139 |
assertEquals('Caption must have expected value', 'Item', item.getCaption()); |
| 140 |
item.setCaption('Hello, world!'); |
| 141 |
assertTrue('Caption must be a string', goog.isString(item.getCaption())); |
| 142 |
assertEquals( |
| 143 |
'Caption must have expected value', 'Hello, world!', item.getCaption()); |
| 144 |
item.setContent(goog.dom.createDom(goog.dom.TagName.DIV, 'foo', 'Foo')); |
| 145 |
assertTrue('Caption must be a string', goog.isString(item.getCaption())); |
| 146 |
assertEquals('Caption must have expected value', 'Foo', item.getCaption()); |
| 147 |
|
| 148 |
var arrayContent = goog.array.clone( |
| 149 |
goog.dom.safeHtmlToNode(goog.html.testing.newSafeHtmlForTest( |
| 150 |
' <b> \xa0foo</b><i> bar</i> ')).childNodes); |
| 151 |
item.setContent(arrayContent); |
| 152 |
assertEquals( |
| 153 |
'whitespaces must be normalized in the caption', '\xa0foo bar', |
| 154 |
item.getCaption()); |
| 155 |
} |
| 156 |
|
| 157 |
function testSetSelectable() { |
| 158 |
assertFalse( |
| 159 |
'Item must not be selectable by default', |
| 160 |
item.isSupportedState(goog.ui.Component.State.SELECTED)); |
| 161 |
item.setSelectable(true); |
| 162 |
assertTrue( |
| 163 |
'Item must be selectable', |
| 164 |
item.isSupportedState(goog.ui.Component.State.SELECTED)); |
| 165 |
item.setSelected(true); |
| 166 |
assertTrue('Item must be selected', item.isSelected()); |
| 167 |
assertFalse('Item must not be checked', item.isChecked()); |
| 168 |
item.setSelectable(false); |
| 169 |
assertFalse( |
| 170 |
'Item must not no longer be selectable', |
| 171 |
item.isSupportedState(goog.ui.Component.State.SELECTED)); |
| 172 |
assertFalse('Item must no longer be selected', item.isSelected()); |
| 173 |
assertFalse('Item must not be checked', item.isChecked()); |
| 174 |
} |
| 175 |
|
| 176 |
function testSetCheckable() { |
| 177 |
assertFalse( |
| 178 |
'Item must not be checkable by default', |
| 179 |
item.isSupportedState(goog.ui.Component.State.CHECKED)); |
| 180 |
item.setCheckable(true); |
| 181 |
assertTrue( |
| 182 |
'Item must be checkable', |
| 183 |
item.isSupportedState(goog.ui.Component.State.CHECKED)); |
| 184 |
item.setChecked(true); |
| 185 |
assertTrue('Item must be checked', item.isChecked()); |
| 186 |
assertFalse('Item must not be selected', item.isSelected()); |
| 187 |
item.setCheckable(false); |
| 188 |
assertFalse( |
| 189 |
'Item must not no longer be checkable', |
| 190 |
item.isSupportedState(goog.ui.Component.State.CHECKED)); |
| 191 |
assertFalse('Item must no longer be checked', item.isChecked()); |
| 192 |
assertFalse('Item must not be selected', item.isSelected()); |
| 193 |
} |
| 194 |
|
| 195 |
function testSetSelectableBeforeCreateDom() { |
| 196 |
item.setSelectable(true); |
| 197 |
item.createDom(); |
| 198 |
assertTrue( |
| 199 |
'Item must have checkbox structure', |
| 200 |
item.getRenderer().hasCheckBoxStructure(item.getElement())); |
| 201 |
item.setSelectable(false); |
| 202 |
assertFalse( |
| 203 |
'Item must no longer have checkbox structure', |
| 204 |
item.getRenderer().hasCheckBoxStructure(item.getElement())); |
| 205 |
} |
| 206 |
|
| 207 |
function testSetCheckableBeforeCreateDom() { |
| 208 |
item.setCheckable(true); |
| 209 |
item.createDom(); |
| 210 |
assertTrue( |
| 211 |
'Item must have checkbox structure', |
| 212 |
item.getRenderer().hasCheckBoxStructure(item.getElement())); |
| 213 |
assertEquals( |
| 214 |
'Element must have ARIA role menuitemcheckbox', |
| 215 |
goog.a11y.aria.Role.MENU_ITEM_CHECKBOX, |
| 216 |
goog.a11y.aria.getRole(item.getElement())); |
| 217 |
item.setCheckable(false); |
| 218 |
assertFalse( |
| 219 |
'Item must no longer have checkbox structure', |
| 220 |
item.getRenderer().hasCheckBoxStructure(item.getElement())); |
| 221 |
} |
| 222 |
|
| 223 |
function testSetSelectableAfterCreateDom() { |
| 224 |
item.createDom(); |
| 225 |
item.setSelectable(true); |
| 226 |
assertTrue( |
| 227 |
'Item must have checkbox structure', |
| 228 |
item.getRenderer().hasCheckBoxStructure(item.getElement())); |
| 229 |
assertEquals( |
| 230 |
'Element must have ARIA role menuitemradio', |
| 231 |
goog.a11y.aria.Role.MENU_ITEM_RADIO, |
| 232 |
goog.a11y.aria.getRole(item.getElement())); |
| 233 |
item.setSelectable(false); |
| 234 |
assertFalse( |
| 235 |
'Item must no longer have checkbox structure', |
| 236 |
item.getRenderer().hasCheckBoxStructure(item.getElement())); |
| 237 |
} |
| 238 |
|
| 239 |
function testSetCheckableAfterCreateDom() { |
| 240 |
item.createDom(); |
| 241 |
item.setCheckable(true); |
| 242 |
assertTrue( |
| 243 |
'Item must have checkbox structure', |
| 244 |
item.getRenderer().hasCheckBoxStructure(item.getElement())); |
| 245 |
item.setCheckable(false); |
| 246 |
assertFalse( |
| 247 |
'Item must no longer have checkbox structure', |
| 248 |
item.getRenderer().hasCheckBoxStructure(item.getElement())); |
| 249 |
} |
| 250 |
|
| 251 |
function testSelectableBehavior() { |
| 252 |
item.setSelectable(true); |
| 253 |
item.render(sandbox); |
| 254 |
assertFalse('Item must not be selected by default', item.isSelected()); |
| 255 |
item.performActionInternal(); |
| 256 |
assertTrue('Item must be selected', item.isSelected()); |
| 257 |
item.performActionInternal(); |
| 258 |
assertTrue('Item must still be selected', item.isSelected()); |
| 259 |
} |
| 260 |
|
| 261 |
function testCheckableBehavior() { |
| 262 |
item.setCheckable(true); |
| 263 |
item.render(sandbox); |
| 264 |
assertFalse('Item must not be checked by default', item.isChecked()); |
| 265 |
item.performActionInternal(); |
| 266 |
assertTrue('Item must be checked', item.isChecked()); |
| 267 |
item.performActionInternal(); |
| 268 |
assertFalse('Item must no longer be checked', item.isChecked()); |
| 269 |
} |
| 270 |
|
| 271 |
function testGetSetContentForItemWithCheckBox() { |
| 272 |
item.setSelectable(true); |
| 273 |
item.createDom(); |
| 274 |
|
| 275 |
assertTrue( |
| 276 |
'Item must have checkbox structure', |
| 277 |
item.getRenderer().hasCheckBoxStructure(item.getElement())); |
| 278 |
assertEquals( |
| 279 |
'getContent() must not return the checkbox structure', 'Item', |
| 280 |
item.getContent()); |
| 281 |
|
| 282 |
item.setContent('Hello'); |
| 283 |
assertEquals( |
| 284 |
'getContent() must not return the checkbox structure', 'Hello', |
| 285 |
item.getContent()); |
| 286 |
assertTrue( |
| 287 |
'Item must still have checkbox structure', |
| 288 |
item.getRenderer().hasCheckBoxStructure(item.getElement())); |
| 289 |
|
| 290 |
item.setContent(goog.dom.createDom(goog.dom.TagName.SPAN, 'foo', 'Foo')); |
| 291 |
assertEquals( |
| 292 |
'getContent() must return element', goog.dom.NodeType.ELEMENT, |
| 293 |
item.getContent().nodeType); |
| 294 |
assertTrue( |
| 295 |
'Item must still have checkbox structure', |
| 296 |
item.getRenderer().hasCheckBoxStructure(item.getElement())); |
| 297 |
|
| 298 |
item.setContent(null); |
| 299 |
assertNull('getContent() must return null', item.getContent()); |
| 300 |
assertTrue( |
| 301 |
'Item must still have checkbox structure', |
| 302 |
item.getRenderer().hasCheckBoxStructure(item.getElement())); |
| 303 |
} |
| 304 |
|
| 305 |
function testGetSetCaptionForItemWithCheckBox() { |
| 306 |
item.setCheckable(true); |
| 307 |
item.createDom(); |
| 308 |
|
| 309 |
assertTrue( |
| 310 |
'Item must have checkbox structure', |
| 311 |
item.getRenderer().hasCheckBoxStructure(item.getElement())); |
| 312 |
assertEquals( |
| 313 |
'getCaption() must not return the checkbox structure', 'Item', |
| 314 |
item.getCaption()); |
| 315 |
|
| 316 |
item.setCaption('Hello'); |
| 317 |
assertEquals( |
| 318 |
'getCaption() must not return the checkbox structure', 'Hello', |
| 319 |
item.getCaption()); |
| 320 |
assertTrue( |
| 321 |
'Item must still have checkbox structure', |
| 322 |
item.getRenderer().hasCheckBoxStructure(item.getElement())); |
| 323 |
|
| 324 |
item.setContent(goog.dom.createDom(goog.dom.TagName.SPAN, 'foo', 'Foo')); |
| 325 |
assertEquals( |
| 326 |
'getCaption() must return text content', 'Foo', item.getCaption()); |
| 327 |
assertTrue( |
| 328 |
'Item must still have checkbox structure', |
| 329 |
item.getRenderer().hasCheckBoxStructure(item.getElement())); |
| 330 |
|
| 331 |
item.setCaption(''); |
| 332 |
assertEquals('getCaption() must return empty string', '', item.getCaption()); |
| 333 |
assertTrue( |
| 334 |
'Item must still have checkbox structure', |
| 335 |
item.getRenderer().hasCheckBoxStructure(item.getElement())); |
| 336 |
} |
| 337 |
|
| 338 |
function testGetSetCaptionForItemWithAccelerators() { |
| 339 |
var contentArr = []; |
| 340 |
contentArr.push( |
| 341 |
goog.dom.createDom( |
| 342 |
goog.dom.TagName.SPAN, goog.getCssName('goog-menuitem-accel'), |
| 343 |
'Ctrl+1')); |
| 344 |
contentArr.push(goog.dom.createTextNode('Hello')); |
| 345 |
item.setCaption(contentArr); |
| 346 |
assertEquals( |
| 347 |
'getCaption() must not return the accelerator', 'Hello', |
| 348 |
item.getCaption()); |
| 349 |
|
| 350 |
item.setCaption([goog.dom.createDom( |
| 351 |
goog.dom.TagName.SPAN, goog.getCssName('goog-menuitem-accel'), |
| 352 |
'Ctrl+1')]); |
| 353 |
assertEquals('getCaption() must return empty string', '', item.getCaption()); |
| 354 |
|
| 355 |
assertEquals( |
| 356 |
'getAccelerator() should return the accelerator', 'Ctrl+1', |
| 357 |
item.getAccelerator()); |
| 358 |
} |
| 359 |
|
| 360 |
function testGetSetCaptionForItemWithMnemonics() { |
| 361 |
var contentArr = []; |
| 362 |
contentArr.push( |
| 363 |
goog.dom.createDom( |
| 364 |
goog.dom.TagName.SPAN, goog.getCssName('goog-menuitem-mnemonic-hint'), |
| 365 |
'H')); |
| 366 |
contentArr.push(goog.dom.createTextNode('ello')); |
| 367 |
item.setCaption(contentArr); |
| 368 |
assertEquals( |
| 369 |
'getCaption() must not return hint markup', 'Hello', item.getCaption()); |
| 370 |
|
| 371 |
contentArr = []; |
| 372 |
contentArr.push(goog.dom.createTextNode('Hello')); |
| 373 |
contentArr.push( |
| 374 |
goog.dom.createDom( |
| 375 |
goog.dom.TagName.SPAN, |
| 376 |
goog.getCssName('goog-menuitem-mnemonic-separator'), '(', |
| 377 |
goog.dom.createDom( |
| 378 |
goog.dom.TagName.SPAN, |
| 379 |
goog.getCssName('goog-menuitem-mnemonic-hint'), 'J'), |
| 380 |
')')); |
| 381 |
item.setCaption(contentArr); |
| 382 |
assertEquals( |
| 383 |
'getCaption() must not return the paranethetical mnemonic', 'Hello', |
| 384 |
item.getCaption()); |
| 385 |
|
| 386 |
item.setCaption(''); |
| 387 |
assertEquals( |
| 388 |
'getCaption() must return the empty string', '', item.getCaption()); |
| 389 |
} |
| 390 |
|
| 391 |
function testHandleKeyEventInternalWithMnemonic() { |
| 392 |
item.performActionInternal = |
| 393 |
goog.testing.recordFunction(item.performActionInternal); |
| 394 |
item.setMnemonic(goog.events.KeyCodes.F); |
| 395 |
item.handleKeyEventInternal({'keyCode': goog.events.KeyCodes.F}); |
| 396 |
assertEquals( |
| 397 |
'performActionInternal must be called', 1, |
| 398 |
item.performActionInternal.getCallCount()); |
| 399 |
} |
| 400 |
|
| 401 |
function testHandleKeyEventInternalWithoutMnemonic() { |
| 402 |
item.performActionInternal = |
| 403 |
goog.testing.recordFunction(item.performActionInternal); |
| 404 |
item.handleKeyEventInternal({'keyCode': goog.events.KeyCodes.F}); |
| 405 |
assertEquals( |
| 406 |
'performActionInternal must not be called without a' + |
| 407 |
' mnemonic', |
| 408 |
0, item.performActionInternal.getCallCount()); |
| 409 |
} |
| 410 |
|
| 411 |
function testRender() { |
| 412 |
item.render(sandbox); |
| 413 |
var contentElement = item.getContentElement(); |
| 414 |
assertNotNull('Content element must exist', contentElement); |
| 415 |
assertTrue( |
| 416 |
'Content element must have expected class name', |
| 417 |
goog.dom.classlist.contains( |
| 418 |
contentElement, |
| 419 |
item.getRenderer().getStructuralCssClass() + '-content')); |
| 420 |
assertHTMLEquals( |
| 421 |
'Content element must have expected structure', 'Item', |
| 422 |
contentElement.innerHTML); |
| 423 |
} |
| 424 |
|
| 425 |
function testRenderSelectableItem() { |
| 426 |
item.setSelectable(true); |
| 427 |
item.render(sandbox); |
| 428 |
assertTrue( |
| 429 |
'Item must have checkbox structure', |
| 430 |
item.getRenderer().hasCheckBoxStructure(item.getElement())); |
| 431 |
assertEquals('getCaption() return expected value', 'Item', item.getCaption()); |
| 432 |
} |
| 433 |
|
| 434 |
function testRenderSelectedItem() { |
| 435 |
item.setSelectable(true); |
| 436 |
item.setSelected(true); |
| 437 |
item.render(sandbox); |
| 438 |
assertTrue( |
| 439 |
'Item must have checkbox structure', |
| 440 |
item.getRenderer().hasCheckBoxStructure(item.getElement())); |
| 441 |
assertTrue( |
| 442 |
'Item must have selected style', |
| 443 |
goog.dom.classlist.contains( |
| 444 |
item.getElement(), item.getRenderer().getClassForState( |
| 445 |
goog.ui.Component.State.SELECTED))); |
| 446 |
} |
| 447 |
|
| 448 |
function testRenderCheckableItem() { |
| 449 |
item.setCheckable(true); |
| 450 |
item.render(sandbox); |
| 451 |
assertTrue( |
| 452 |
'Item must have checkbox structure', |
| 453 |
item.getRenderer().hasCheckBoxStructure(item.getElement())); |
| 454 |
assertEquals('getCaption() return expected value', 'Item', item.getCaption()); |
| 455 |
} |
| 456 |
|
| 457 |
function testRenderCheckedItem() { |
| 458 |
item.setCheckable(true); |
| 459 |
item.setChecked(true); |
| 460 |
item.render(sandbox); |
| 461 |
assertTrue( |
| 462 |
'Item must have checkbox structure', |
| 463 |
item.getRenderer().hasCheckBoxStructure(item.getElement())); |
| 464 |
assertTrue( |
| 465 |
'Item must have checked style', |
| 466 |
goog.dom.classlist.contains( |
| 467 |
item.getElement(), item.getRenderer().getClassForState( |
| 468 |
goog.ui.Component.State.CHECKED))); |
| 469 |
} |
| 470 |
|
| 471 |
function testDecorate() { |
| 472 |
sandbox.innerHTML = '<div id="foo">Foo</div>'; |
| 473 |
var foo = goog.dom.getElement('foo'); |
| 474 |
item.decorate(foo); |
| 475 |
assertEquals('Decorated element must be as expected', foo, item.getElement()); |
| 476 |
assertTrue( |
| 477 |
'Decorated element must have expected class name', |
| 478 |
goog.dom.classlist.contains( |
| 479 |
item.getElement(), item.getRenderer().getCssClass())); |
| 480 |
assertEquals( |
| 481 |
'Content element must be the decorated element\'s child', |
| 482 |
item.getContentElement(), item.getElement().firstChild); |
| 483 |
assertHTMLEquals( |
| 484 |
'Content must have expected structure', 'Foo', |
| 485 |
item.getContentElement().innerHTML); |
| 486 |
} |
| 487 |
|
| 488 |
function testDecorateCheckableItem() { |
| 489 |
sandbox.innerHTML = '<div id="foo" class="goog-option">Foo</div>'; |
| 490 |
var foo = goog.dom.getElement('foo'); |
| 491 |
item.decorate(foo); |
| 492 |
assertEquals('Decorated element must be as expected', foo, item.getElement()); |
| 493 |
assertTrue( |
| 494 |
'Decorated element must have expected class name', |
| 495 |
goog.dom.classlist.contains( |
| 496 |
item.getElement(), item.getRenderer().getCssClass())); |
| 497 |
assertEquals( |
| 498 |
'Content element must be the decorated element\'s child', |
| 499 |
item.getContentElement(), item.getElement().firstChild); |
| 500 |
assertTrue( |
| 501 |
'Item must have checkbox structure', |
| 502 |
item.getRenderer().hasCheckBoxStructure(item.getElement())); |
| 503 |
assertFalse('Item must not be checked', item.isChecked()); |
| 504 |
} |
| 505 |
|
| 506 |
function testDecorateCheckedItem() { |
| 507 |
sandbox.innerHTML = |
| 508 |
'<div id="foo" class="goog-option goog-option-selected">Foo</div>'; |
| 509 |
var foo = goog.dom.getElement('foo'); |
| 510 |
item.decorate(foo); |
| 511 |
assertEquals('Decorated element must be as expected', foo, item.getElement()); |
| 512 |
assertSameElements( |
| 513 |
'Decorated element must have expected class names', |
| 514 |
['goog-menuitem', 'goog-option', 'goog-option-selected'], |
| 515 |
goog.dom.classlist.get(item.getElement())); |
| 516 |
assertEquals( |
| 517 |
'Content element must be the decorated element\'s child', |
| 518 |
item.getContentElement(), item.getElement().firstChild); |
| 519 |
assertTrue( |
| 520 |
'Item must have checkbox structure', |
| 521 |
item.getRenderer().hasCheckBoxStructure(item.getElement())); |
| 522 |
assertTrue('Item must be checked', item.isChecked()); |
| 523 |
} |
| 524 |
|
| 525 |
function testDecorateTemplate() { |
| 526 |
sandbox.innerHTML = '<div id="foo" class="goog-menuitem">' + |
| 527 |
'<div class="goog-menuitem-content">Foo</div></div>'; |
| 528 |
var foo = goog.dom.getElement('foo'); |
| 529 |
item.decorate(foo); |
| 530 |
assertEquals('Decorated element must be as expected', foo, item.getElement()); |
| 531 |
assertTrue( |
| 532 |
'Decorated element must have expected class name', |
| 533 |
goog.dom.classlist.contains( |
| 534 |
item.getElement(), item.getRenderer().getCssClass())); |
| 535 |
assertEquals( |
| 536 |
'Content element must be the decorated element\'s child', |
| 537 |
item.getContentElement(), item.getElement().firstChild); |
| 538 |
assertHTMLEquals( |
| 539 |
'Content must have expected structure', 'Foo', |
| 540 |
item.getContentElement().innerHTML); |
| 541 |
} |
| 542 |
|
| 543 |
function testDecorateCheckableItemTemplate() { |
| 544 |
sandbox.innerHTML = '<div id="foo" class="goog-menuitem goog-option">' + |
| 545 |
'<div class="goog-menuitem-content">' + |
| 546 |
'<div class="goog-menuitem-checkbox"></div>' + |
| 547 |
'Foo</div></div>'; |
| 548 |
var foo = goog.dom.getElement('foo'); |
| 549 |
item.decorate(foo); |
| 550 |
assertEquals('Decorated element must be as expected', foo, item.getElement()); |
| 551 |
assertTrue( |
| 552 |
'Decorated element must have expected class name', |
| 553 |
goog.dom.classlist.contains( |
| 554 |
item.getElement(), item.getRenderer().getCssClass())); |
| 555 |
assertEquals( |
| 556 |
'Content element must be the decorated element\'s child', |
| 557 |
item.getContentElement(), item.getElement().firstChild); |
| 558 |
assertTrue( |
| 559 |
'Item must have checkbox structure', |
| 560 |
item.getRenderer().hasCheckBoxStructure(item.getElement())); |
| 561 |
assertEquals( |
| 562 |
'Item must have exactly one checkbox structure', 1, |
| 563 |
goog.dom |
| 564 |
.getElementsByTagNameAndClass( |
| 565 |
goog.dom.TagName.DIV, 'goog-menuitem-checkbox', item.getElement()) |
| 566 |
.length); |
| 567 |
assertFalse('Item must not be checked', item.isChecked()); |
| 568 |
} |
| 569 |
|
| 570 |
function testDecorateCheckedItemTemplate() { |
| 571 |
sandbox.innerHTML = '<div id="foo" ' + |
| 572 |
'class="goog-menuitem goog-option goog-option-selected">' + |
| 573 |
'<div class="goog-menuitem-content">' + |
| 574 |
'<div class="goog-menuitem-checkbox"></div>' + |
| 575 |
'Foo</div></div>'; |
| 576 |
var foo = goog.dom.getElement('foo'); |
| 577 |
item.decorate(foo); |
| 578 |
assertEquals('Decorated element must be as expected', foo, item.getElement()); |
| 579 |
assertSameElements( |
| 580 |
'Decorated element must have expected class names', |
| 581 |
['goog-menuitem', 'goog-option', 'goog-option-selected'], |
| 582 |
goog.dom.classlist.get(item.getElement())); |
| 583 |
assertEquals( |
| 584 |
'Content element must be the decorated element\'s child', |
| 585 |
item.getContentElement(), item.getElement().firstChild); |
| 586 |
assertTrue( |
| 587 |
'Item must have checkbox structure', |
| 588 |
item.getRenderer().hasCheckBoxStructure(item.getElement())); |
| 589 |
assertEquals( |
| 590 |
'Item must have exactly one checkbox structure', 1, |
| 591 |
goog.dom |
| 592 |
.getElementsByTagNameAndClass( |
| 593 |
goog.dom.TagName.DIV, 'goog-menuitem-checkbox', item.getElement()) |
| 594 |
.length); |
| 595 |
assertTrue('Item must be checked', item.isChecked()); |
| 596 |
} |
| 597 |
|
| 598 |
|
| 599 |
/** @bug 1463524 */ |
| 600 |
function testHandleMouseUp() { |
| 601 |
var COORDS_1 = new goog.math.Coordinate(1, 1); |
| 602 |
var COORDS_2 = new goog.math.Coordinate(2, 2); |
| 603 |
item.setActive(true); |
| 604 |
// Override performActionInternal() for testing purposes. |
| 605 |
var actionPerformed; |
| 606 |
item.performActionInternal = function() { |
| 607 |
actionPerformed = true; |
| 608 |
return true; |
| 609 |
}; |
| 610 |
item.render(sandbox); |
| 611 |
|
| 612 |
// Scenario 1: item has no parent. |
| 613 |
actionPerformed = false; |
| 614 |
item.setActive(true); |
| 615 |
goog.testing.events.fireMouseUpEvent(item.getElement()); |
| 616 |
assertTrue('Action should be performed on mouseup', actionPerformed); |
| 617 |
|
| 618 |
// Scenario 2: item has a parent. |
| 619 |
actionPerformed = false; |
| 620 |
item.setActive(true); |
| 621 |
var parent = new goog.ui.Component(); |
| 622 |
var parentElem = goog.dom.getElement('parentComponent'); |
| 623 |
parent.render(parentElem); |
| 624 |
parent.addChild(item); |
| 625 |
parent.openingCoords = COORDS_1; |
| 626 |
goog.testing.events.fireMouseUpEvent(item.getElement(), undefined, COORDS_2); |
| 627 |
assertTrue('Action should be performed on mouseup', actionPerformed); |
| 628 |
|
| 629 |
// Scenario 3: item has a parent which was opened during mousedown, and |
| 630 |
// item, and now the mouseup fires at the same coords. |
| 631 |
actionPerformed = false; |
| 632 |
item.setActive(true); |
| 633 |
parent.openingCoords = COORDS_2; |
| 634 |
goog.testing.events.fireMouseUpEvent(item.getElement(), undefined, COORDS_2); |
| 635 |
assertFalse('Action should not be performed on mouseup', actionPerformed); |
| 636 |
} |
| 637 |
|
| 638 |
function testSetAriaLabel() { |
| 639 |
assertNull('Item must not have aria label by default', item.getAriaLabel()); |
| 640 |
item.setAriaLabel('Item 1'); |
| 641 |
item.render(sandbox); |
| 642 |
var el = item.getElementStrict(); |
| 643 |
assertEquals( |
| 644 |
'Item element must have expected aria-label', 'Item 1', |
| 645 |
el.getAttribute('aria-label')); |
| 646 |
assertEquals( |
| 647 |
'Item element must have expected aria-role', 'menuitem', |
| 648 |
el.getAttribute('role')); |
| 649 |
item.setAriaLabel('Item 2'); |
| 650 |
assertEquals( |
| 651 |
'Item element must have updated aria-label', 'Item 2', |
| 652 |
el.getAttribute('aria-label')); |
| 653 |
assertEquals( |
| 654 |
'Item element must have expected aria-role', 'menuitem', |
| 655 |
el.getAttribute('role')); |
| 656 |
} |