| 1 |
// Copyright 2007 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.DialogTest'); |
| 16 |
goog.setTestOnly('goog.ui.DialogTest'); |
| 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.TagName'); |
| 23 |
goog.require('goog.dom.classlist'); |
| 24 |
goog.require('goog.events'); |
| 25 |
goog.require('goog.events.EventType'); |
| 26 |
goog.require('goog.events.KeyCodes'); |
| 27 |
goog.require('goog.fx.css3'); |
| 28 |
goog.require('goog.html.SafeHtml'); |
| 29 |
goog.require('goog.html.testing'); |
| 30 |
goog.require('goog.style'); |
| 31 |
goog.require('goog.testing.MockClock'); |
| 32 |
goog.require('goog.testing.events'); |
| 33 |
goog.require('goog.testing.jsunit'); |
| 34 |
goog.require('goog.testing.recordFunction'); |
| 35 |
goog.require('goog.ui.Dialog'); |
| 36 |
goog.require('goog.userAgent'); |
| 37 |
var bodyChildElement; |
| 38 |
var decorateTarget; |
| 39 |
var dialog; |
| 40 |
var mockClock; |
| 41 |
|
| 42 |
function setUp() { |
| 43 |
mockClock = new goog.testing.MockClock(true); |
| 44 |
bodyChildElement = goog.dom.createElement(goog.dom.TagName.DIV); |
| 45 |
document.body.appendChild(bodyChildElement); |
| 46 |
dialog = new goog.ui.Dialog(); |
| 47 |
var buttons = new goog.ui.Dialog.ButtonSet(); |
| 48 |
buttons.set(goog.ui.Dialog.DefaultButtonKeys.CANCEL, 'Foo!', false, true); |
| 49 |
buttons.set(goog.ui.Dialog.DefaultButtonKeys.OK, 'OK', true); |
| 50 |
dialog.setButtonSet(buttons); |
| 51 |
dialog.setVisible(true); |
| 52 |
|
| 53 |
decorateTarget = goog.dom.createDom(goog.dom.TagName.DIV); |
| 54 |
document.body.appendChild(decorateTarget); |
| 55 |
} |
| 56 |
|
| 57 |
function tearDown() { |
| 58 |
dialog.dispose(); |
| 59 |
goog.dom.removeNode(bodyChildElement); |
| 60 |
goog.dom.removeNode(decorateTarget); |
| 61 |
mockClock.dispose(); |
| 62 |
} |
| 63 |
|
| 64 |
function testCrossFrameFocus() { |
| 65 |
// Firefox (3.6, maybe future versions) fails this test when there are too |
| 66 |
// many other test files being run concurrently. |
| 67 |
if (goog.userAgent.IE || goog.userAgent.GECKO) { |
| 68 |
return; |
| 69 |
} |
| 70 |
dialog.setVisible(false); |
| 71 |
var iframeWindow = goog.dom.getElement('f').contentWindow; |
| 72 |
var iframeInput = goog.dom.getElementsByTagName( |
| 73 |
goog.dom.TagName.INPUT, iframeWindow.document)[0]; |
| 74 |
dialog.setButtonSet(goog.ui.Dialog.ButtonSet.OK); |
| 75 |
var dialogElement = dialog.getElement(); |
| 76 |
var focusCounter = 0; |
| 77 |
goog.events.listen(dialogElement, 'focus', function() { focusCounter++; }); |
| 78 |
iframeInput.focus(); |
| 79 |
dialog.setVisible(true); |
| 80 |
dialog.setVisible(false); |
| 81 |
iframeInput.focus(); |
| 82 |
dialog.setVisible(true); |
| 83 |
assertEquals(2, focusCounter); |
| 84 |
} |
| 85 |
|
| 86 |
function testNoDisabledButtonFocus() { |
| 87 |
dialog.setVisible(false); |
| 88 |
var buttonEl = |
| 89 |
dialog.getButtonSet().getButton(goog.ui.Dialog.DefaultButtonKeys.OK); |
| 90 |
buttonEl.disabled = true; |
| 91 |
var focused = false; |
| 92 |
buttonEl.focus = function() { focused = true; }; |
| 93 |
dialog.setVisible(true); |
| 94 |
assertFalse('Should not have called focus on disabled button', focused); |
| 95 |
} |
| 96 |
|
| 97 |
function testNoTitleClose() { |
| 98 |
assertTrue(goog.style.isElementShown(dialog.getTitleCloseElement())); |
| 99 |
dialog.setHasTitleCloseButton(false); |
| 100 |
assertFalse(goog.style.isElementShown(dialog.getTitleCloseElement())); |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
/** |
| 105 |
* Helper that clicks the first button in the dialog and checks if that |
| 106 |
* results in a goog.ui.Dialog.EventType.SELECT being dispatched. |
| 107 |
* @param {boolean} disableButton Whether to disable the button being |
| 108 |
* tested. |
| 109 |
* @return {boolean} Whether a goog.ui.Dialog.EventType.SELECT was dispatched. |
| 110 |
*/ |
| 111 |
function checkSelectDispatchedOnButtonClick(disableButton) { |
| 112 |
var aButton = goog.dom.getElementsByTagName( |
| 113 |
goog.dom.TagName.BUTTON, dialog.getButtonElement())[0]; |
| 114 |
assertNotEquals(aButton, null); |
| 115 |
aButton.disabled = disableButton; |
| 116 |
var wasCalled = false; |
| 117 |
var callRecorder = function() { wasCalled = true; }; |
| 118 |
goog.events.listen(dialog, goog.ui.Dialog.EventType.SELECT, callRecorder); |
| 119 |
goog.testing.events.fireClickSequence(aButton); |
| 120 |
return wasCalled; |
| 121 |
} |
| 122 |
|
| 123 |
function testButtonClicksDispatchSelectEvents() { |
| 124 |
assertTrue( |
| 125 |
'Select event should be dispatched' + |
| 126 |
' when clicking on an enabled button', |
| 127 |
checkSelectDispatchedOnButtonClick(false)); |
| 128 |
} |
| 129 |
|
| 130 |
function testDisabledButtonClicksDontDispatchSelectEvents() { |
| 131 |
assertFalse( |
| 132 |
'Select event should not be dispatched' + |
| 133 |
' when clicking on a disabled button', |
| 134 |
checkSelectDispatchedOnButtonClick(true)); |
| 135 |
} |
| 136 |
|
| 137 |
function testEnterKeyDispatchesDefaultSelectEvents() { |
| 138 |
var okButton = goog.dom.getElementsByTagName( |
| 139 |
goog.dom.TagName.BUTTON, dialog.getButtonElement())[1]; |
| 140 |
assertNotEquals(okButton, null); |
| 141 |
var wasCalled = false; |
| 142 |
var callRecorder = function() { wasCalled = true; }; |
| 143 |
goog.events.listen(dialog, goog.ui.Dialog.EventType.SELECT, callRecorder); |
| 144 |
// Test that event is not dispatched when default button is disabled. |
| 145 |
okButton.disabled = true; |
| 146 |
goog.testing.events.fireKeySequence( |
| 147 |
dialog.getElement(), goog.events.KeyCodes.ENTER); |
| 148 |
assertFalse(wasCalled); |
| 149 |
// Test that event is dispatched when default button is enabled. |
| 150 |
okButton.disabled = false; |
| 151 |
goog.testing.events.fireKeySequence( |
| 152 |
dialog.getElement(), goog.events.KeyCodes.ENTER); |
| 153 |
assertTrue(wasCalled); |
| 154 |
} |
| 155 |
|
| 156 |
function testEnterKeyOnDisabledDefaultButtonDoesNotDispatchSelectEvents() { |
| 157 |
var okButton = goog.dom.getElementsByTagName( |
| 158 |
goog.dom.TagName.BUTTON, dialog.getButtonElement())[1]; |
| 159 |
okButton.focus(); |
| 160 |
|
| 161 |
var callRecorder = goog.testing.recordFunction(); |
| 162 |
goog.events.listen(dialog, goog.ui.Dialog.EventType.SELECT, callRecorder); |
| 163 |
|
| 164 |
okButton.disabled = true; |
| 165 |
goog.testing.events.fireKeySequence(okButton, goog.events.KeyCodes.ENTER); |
| 166 |
assertEquals(0, callRecorder.getCallCount()); |
| 167 |
|
| 168 |
okButton.disabled = false; |
| 169 |
goog.testing.events.fireKeySequence(okButton, goog.events.KeyCodes.ENTER); |
| 170 |
assertEquals(1, callRecorder.getCallCount()); |
| 171 |
} |
| 172 |
|
| 173 |
function testEnterKeyDoesNothingOnSpecialFormElements() { |
| 174 |
checkEnterKeyDoesNothingOnSpecialFormElement( |
| 175 |
goog.html.SafeHtml.create('textarea', {}, 'Hello dialog'), |
| 176 |
'TEXTAREA'); |
| 177 |
|
| 178 |
checkEnterKeyDoesNothingOnSpecialFormElement( |
| 179 |
goog.html.SafeHtml.create('select', {}, 'Selection'), |
| 180 |
'SELECT'); |
| 181 |
|
| 182 |
checkEnterKeyDoesNothingOnSpecialFormElement( |
| 183 |
goog.html.SafeHtml.create('a', {'href': 'http://google.com'}, |
| 184 |
'Hello dialog'), |
| 185 |
'A'); |
| 186 |
} |
| 187 |
|
| 188 |
function checkEnterKeyDoesNothingOnSpecialFormElement(content, tagName) { |
| 189 |
dialog.setSafeHtmlContent(content); |
| 190 |
var formElement = dialog.getContentElement().getElementsByTagName(tagName)[0]; |
| 191 |
var wasCalled = false; |
| 192 |
var callRecorder = function() { wasCalled = true; }; |
| 193 |
goog.events.listen(dialog, goog.ui.Dialog.EventType.SELECT, callRecorder); |
| 194 |
|
| 195 |
// Enter does not fire on the enabled form element. |
| 196 |
goog.testing.events.fireKeySequence(formElement, goog.events.KeyCodes.ENTER); |
| 197 |
assertFalse(wasCalled); |
| 198 |
|
| 199 |
// Enter fires on the disabled form element. |
| 200 |
formElement.disabled = true; |
| 201 |
goog.testing.events.fireKeySequence(formElement, goog.events.KeyCodes.ENTER); |
| 202 |
assertTrue(wasCalled); |
| 203 |
} |
| 204 |
|
| 205 |
function testEscapeKeyDoesNothingOnSpecialFormElements() { |
| 206 |
dialog.setSafeHtmlContent(goog.html.SafeHtml.create('select', {}, [ |
| 207 |
goog.html.SafeHtml.create('option', {}, 'Hello'), |
| 208 |
goog.html.SafeHtml.create('option', {}, 'dialog') |
| 209 |
])); |
| 210 |
var select = dialog.getContentElement().getElementsByTagName('SELECT')[0]; |
| 211 |
var wasCalled = false; |
| 212 |
var callRecorder = function() { wasCalled = true; }; |
| 213 |
goog.events.listen(dialog, goog.ui.Dialog.EventType.SELECT, callRecorder); |
| 214 |
|
| 215 |
// Escape does not fire on the enabled select box. |
| 216 |
goog.testing.events.fireKeySequence(select, goog.events.KeyCodes.ESC); |
| 217 |
assertFalse(wasCalled); |
| 218 |
|
| 219 |
// Escape fires on the disabled select. |
| 220 |
select.disabled = true; |
| 221 |
goog.testing.events.fireKeySequence(select, goog.events.KeyCodes.ESC); |
| 222 |
assertTrue(wasCalled); |
| 223 |
} |
| 224 |
|
| 225 |
function testEscapeCloses() { |
| 226 |
// If escapeCloses is set to false, the dialog should ignore the escape key |
| 227 |
assertTrue(dialog.isEscapeToCancel()); |
| 228 |
dialog.setEscapeToCancel(false); |
| 229 |
assertFalse(dialog.isEscapeToCancel()); |
| 230 |
|
| 231 |
var buttons = new goog.ui.Dialog.ButtonSet(); |
| 232 |
buttons.set(goog.ui.Dialog.DefaultButtonKeys.OK, 'OK', true); |
| 233 |
dialog.setButtonSet(buttons); |
| 234 |
goog.testing.events.fireKeySequence( |
| 235 |
dialog.getContentElement(), goog.events.KeyCodes.ESC); |
| 236 |
assertTrue(dialog.isVisible()); |
| 237 |
|
| 238 |
// Having a cancel button should make no difference, escape should still not |
| 239 |
// work. |
| 240 |
buttons.set(goog.ui.Dialog.DefaultButtonKeys.CANCEL, 'Foo!', false, true); |
| 241 |
dialog.setButtonSet(buttons); |
| 242 |
goog.testing.events.fireKeySequence( |
| 243 |
dialog.getContentElement(), goog.events.KeyCodes.ESC); |
| 244 |
assertTrue(dialog.isVisible()); |
| 245 |
} |
| 246 |
|
| 247 |
function testKeydownClosesWithoutButtonSet() { |
| 248 |
// Clear button set |
| 249 |
dialog.setButtonSet(null); |
| 250 |
|
| 251 |
// Create a custom button. |
| 252 |
dialog.setSafeHtmlContent(goog.html.SafeHtml.create('button', |
| 253 |
{'id': 'button', 'name': 'ok'}, |
| 254 |
'OK')); |
| 255 |
var wasCalled = false; |
| 256 |
function called() { wasCalled = true; } |
| 257 |
var element = goog.dom.getElement('button'); |
| 258 |
goog.events.listen(element, goog.events.EventType.KEYPRESS, called); |
| 259 |
// Listen for 'Enter' on the button. |
| 260 |
// This tests using a dialog with no ButtonSet that has been set. Uses |
| 261 |
// a custom button. The callback should be called with no exception thrown. |
| 262 |
goog.testing.events.fireKeySequence(element, goog.events.KeyCodes.ENTER); |
| 263 |
assertTrue('Should have gotten event on the button.', wasCalled); |
| 264 |
} |
| 265 |
|
| 266 |
function testEnterKeyWithoutDefaultDoesNotPreventPropagation() { |
| 267 |
var buttons = new goog.ui.Dialog.ButtonSet(); |
| 268 |
buttons.set(goog.ui.Dialog.DefaultButtonKeys.CANCEL, 'Foo!', false); |
| 269 |
// Set a button set without a default selected button |
| 270 |
dialog.setButtonSet(buttons); |
| 271 |
dialog.setSafeHtmlContent(goog.html.SafeHtml.create('span', |
| 272 |
{'id': 'linkel', 'tabindex': '0'}, |
| 273 |
'Link Span')); |
| 274 |
|
| 275 |
var call = false; |
| 276 |
function called() { call = true; } |
| 277 |
var element = document.getElementById('linkel'); |
| 278 |
goog.events.listen(element, goog.events.EventType.KEYDOWN, called); |
| 279 |
goog.testing.events.fireKeySequence(element, goog.events.KeyCodes.ENTER); |
| 280 |
|
| 281 |
assertTrue('Should have gotten event on the link', call); |
| 282 |
} |
| 283 |
|
| 284 |
function testPreventDefaultedSelectCausesStopPropagation() { |
| 285 |
dialog.setButtonSet(goog.ui.Dialog.ButtonSet.OK_CANCEL); |
| 286 |
|
| 287 |
var callCount = 0; |
| 288 |
var keypressCount = 0; |
| 289 |
var keydownCount = 0; |
| 290 |
|
| 291 |
var preventDefaulter = function(e) { e.preventDefault(); }; |
| 292 |
|
| 293 |
goog.events.listen(dialog, goog.ui.Dialog.EventType.SELECT, preventDefaulter); |
| 294 |
goog.events.listen(document.body, goog.events.EventType.KEYPRESS, function() { |
| 295 |
keypressCount++; |
| 296 |
}); |
| 297 |
goog.events.listen(document.body, goog.events.EventType.KEYDOWN, function() { |
| 298 |
keydownCount++; |
| 299 |
}); |
| 300 |
|
| 301 |
// Ensure that if the SELECT event is prevented, all key events |
| 302 |
// are still stopped from propagating. |
| 303 |
goog.testing.events.fireKeySequence( |
| 304 |
dialog.getElement(), goog.events.KeyCodes.ENTER); |
| 305 |
assertEquals('The KEYPRESS should be stopped', 0, keypressCount); |
| 306 |
assertEquals('The KEYDOWN should not be stopped', 1, keydownCount); |
| 307 |
|
| 308 |
keypressCount = 0; |
| 309 |
keydownCount = 0; |
| 310 |
goog.testing.events.fireKeySequence( |
| 311 |
dialog.getElement(), goog.events.KeyCodes.ESC); |
| 312 |
assertEquals('The KEYDOWN should be stopped', 0, keydownCount); |
| 313 |
// Note: Some browsers don't yield keypresses on escape, so don't check. |
| 314 |
|
| 315 |
goog.events.unlisten( |
| 316 |
dialog, goog.ui.Dialog.EventType.SELECT, preventDefaulter); |
| 317 |
|
| 318 |
keypressCount = 0; |
| 319 |
keydownCount = 0; |
| 320 |
goog.testing.events.fireKeySequence( |
| 321 |
dialog.getElement(), goog.events.KeyCodes.ENTER); |
| 322 |
assertEquals('The KEYPRESS should be stopped', 0, keypressCount); |
| 323 |
assertEquals('The KEYDOWN should not be stopped', 1, keydownCount); |
| 324 |
} |
| 325 |
|
| 326 |
function testEnterKeyHandledInKeypress() { |
| 327 |
var inKeyPress = false; |
| 328 |
goog.events.listen(document.body, goog.events.EventType.KEYPRESS, function() { |
| 329 |
inKeyPress = true; |
| 330 |
}, true /* capture */); |
| 331 |
goog.events.listen(document.body, goog.events.EventType.KEYPRESS, function() { |
| 332 |
inKeyPress = false; |
| 333 |
}, false /* !capture */); |
| 334 |
var selectCalled = false; |
| 335 |
goog.events.listen(dialog, goog.ui.Dialog.EventType.SELECT, function() { |
| 336 |
selectCalled = true; |
| 337 |
assertTrue( |
| 338 |
'Select must be dispatched during keypress to allow popups', |
| 339 |
inKeyPress); |
| 340 |
}); |
| 341 |
|
| 342 |
goog.testing.events.fireKeySequence( |
| 343 |
dialog.getElement(), goog.events.KeyCodes.ENTER); |
| 344 |
assertTrue(selectCalled); |
| 345 |
} |
| 346 |
|
| 347 |
function testShiftTabAtTopSetsUpWrapAndDoesNotPreventPropagation() { |
| 348 |
dialog.setupBackwardTabWrap = goog.testing.recordFunction(); |
| 349 |
shiftTabRecorder = goog.testing.recordFunction(); |
| 350 |
|
| 351 |
goog.events.listen( |
| 352 |
dialog.getElement(), goog.events.EventType.KEYDOWN, shiftTabRecorder); |
| 353 |
var shiftProperties = {shiftKey: true}; |
| 354 |
goog.testing.events.fireKeySequence( |
| 355 |
dialog.getElement(), goog.events.KeyCodes.TAB, shiftProperties); |
| 356 |
|
| 357 |
assertNotNull( |
| 358 |
'Should have gotten event on Shift+TAB', shiftTabRecorder.getLastCall()); |
| 359 |
assertNotNull( |
| 360 |
'Backward tab wrap should have been set up', |
| 361 |
dialog.setupBackwardTabWrap.getLastCall()); |
| 362 |
} |
| 363 |
|
| 364 |
function testButtonsWithContentsDispatchSelectEvents() { |
| 365 |
var aButton = goog.dom.getElementsByTagName( |
| 366 |
goog.dom.TagName.BUTTON, dialog.getButtonElement())[0]; |
| 367 |
var aSpan = goog.dom.createElement(goog.dom.TagName.SPAN); |
| 368 |
aButton.appendChild(aSpan); |
| 369 |
var wasCalled = false; |
| 370 |
var callRecorder = function() { wasCalled = true; }; |
| 371 |
goog.events.listen(dialog, goog.ui.Dialog.EventType.SELECT, callRecorder); |
| 372 |
goog.testing.events.fireClickSequence(aSpan); |
| 373 |
assertTrue(wasCalled); |
| 374 |
} |
| 375 |
|
| 376 |
function testAfterHideEvent() { |
| 377 |
var wasCalled = false; |
| 378 |
var callRecorder = function() { wasCalled = true; }; |
| 379 |
goog.events.listen(dialog, goog.ui.Dialog.EventType.AFTER_HIDE, callRecorder); |
| 380 |
dialog.setVisible(false); |
| 381 |
assertTrue(wasCalled); |
| 382 |
} |
| 383 |
|
| 384 |
function testAfterShowEvent() { |
| 385 |
dialog.setVisible(false); |
| 386 |
var wasCalled = false; |
| 387 |
var callRecorder = function() { wasCalled = true; }; |
| 388 |
goog.events.listen(dialog, goog.ui.Dialog.EventType.AFTER_SHOW, callRecorder); |
| 389 |
dialog.setVisible(true); |
| 390 |
assertTrue(wasCalled); |
| 391 |
} |
| 392 |
|
| 393 |
function testCannedButtonSets() { |
| 394 |
dialog.setButtonSet(goog.ui.Dialog.ButtonSet.OK); |
| 395 |
assertButtons([goog.ui.Dialog.DefaultButtonKeys.OK]); |
| 396 |
|
| 397 |
dialog.setButtonSet(goog.ui.Dialog.ButtonSet.OK_CANCEL); |
| 398 |
assertButtons([ |
| 399 |
goog.ui.Dialog.DefaultButtonKeys.OK, goog.ui.Dialog.DefaultButtonKeys.CANCEL |
| 400 |
]); |
| 401 |
|
| 402 |
dialog.setButtonSet(goog.ui.Dialog.ButtonSet.YES_NO); |
| 403 |
assertButtons([ |
| 404 |
goog.ui.Dialog.DefaultButtonKeys.YES, goog.ui.Dialog.DefaultButtonKeys.NO |
| 405 |
]); |
| 406 |
|
| 407 |
dialog.setButtonSet(goog.ui.Dialog.ButtonSet.YES_NO_CANCEL); |
| 408 |
assertButtons([ |
| 409 |
goog.ui.Dialog.DefaultButtonKeys.YES, goog.ui.Dialog.DefaultButtonKeys.NO, |
| 410 |
goog.ui.Dialog.DefaultButtonKeys.CANCEL |
| 411 |
]); |
| 412 |
|
| 413 |
dialog.setButtonSet(goog.ui.Dialog.ButtonSet.CONTINUE_SAVE_CANCEL); |
| 414 |
assertButtons([ |
| 415 |
goog.ui.Dialog.DefaultButtonKeys.CONTINUE, |
| 416 |
goog.ui.Dialog.DefaultButtonKeys.SAVE, |
| 417 |
goog.ui.Dialog.DefaultButtonKeys.CANCEL |
| 418 |
]); |
| 419 |
} |
| 420 |
|
| 421 |
function testFactoryButtonSets() { |
| 422 |
dialog.setButtonSet(goog.ui.Dialog.ButtonSet.createOk()); |
| 423 |
assertButtons([goog.ui.Dialog.DefaultButtonKeys.OK]); |
| 424 |
|
| 425 |
dialog.setButtonSet(goog.ui.Dialog.ButtonSet.createOkCancel()); |
| 426 |
assertButtons([ |
| 427 |
goog.ui.Dialog.DefaultButtonKeys.OK, goog.ui.Dialog.DefaultButtonKeys.CANCEL |
| 428 |
]); |
| 429 |
|
| 430 |
dialog.setButtonSet(goog.ui.Dialog.ButtonSet.createYesNo()); |
| 431 |
assertButtons([ |
| 432 |
goog.ui.Dialog.DefaultButtonKeys.YES, goog.ui.Dialog.DefaultButtonKeys.NO |
| 433 |
]); |
| 434 |
|
| 435 |
dialog.setButtonSet(goog.ui.Dialog.ButtonSet.createYesNoCancel()); |
| 436 |
assertButtons([ |
| 437 |
goog.ui.Dialog.DefaultButtonKeys.YES, goog.ui.Dialog.DefaultButtonKeys.NO, |
| 438 |
goog.ui.Dialog.DefaultButtonKeys.CANCEL |
| 439 |
]); |
| 440 |
|
| 441 |
dialog.setButtonSet(goog.ui.Dialog.ButtonSet.createContinueSaveCancel()); |
| 442 |
assertButtons([ |
| 443 |
goog.ui.Dialog.DefaultButtonKeys.CONTINUE, |
| 444 |
goog.ui.Dialog.DefaultButtonKeys.SAVE, |
| 445 |
goog.ui.Dialog.DefaultButtonKeys.CANCEL |
| 446 |
]); |
| 447 |
} |
| 448 |
|
| 449 |
function testDefaultButtonClassName() { |
| 450 |
var key = 'someKey'; |
| 451 |
var msg = 'someMessage'; |
| 452 |
var isDefault = false; |
| 453 |
var buttonSetOne = new goog.ui.Dialog.ButtonSet().set(key, msg, isDefault); |
| 454 |
dialog.setButtonSet(buttonSetOne); |
| 455 |
var defaultClassName = goog.getCssName(buttonSetOne.class_, 'default'); |
| 456 |
var buttonOne = buttonSetOne.getButton(key); |
| 457 |
assertNotEquals(defaultClassName, buttonOne.className); |
| 458 |
var isDefault = true; |
| 459 |
var buttonSetTwo = new goog.ui.Dialog.ButtonSet().set(key, msg, isDefault); |
| 460 |
dialog.setButtonSet(buttonSetTwo); |
| 461 |
var buttonTwo = buttonSetTwo.getButton(key); |
| 462 |
assertEquals(defaultClassName, buttonTwo.className); |
| 463 |
} |
| 464 |
|
| 465 |
function testGetButton() { |
| 466 |
dialog.setButtonSet(goog.ui.Dialog.ButtonSet.OK); |
| 467 |
var buttons = document.getElementsByName(goog.ui.Dialog.DefaultButtonKeys.OK); |
| 468 |
assertEquals( |
| 469 |
buttons[0], |
| 470 |
dialog.getButtonSet().getButton(goog.ui.Dialog.DefaultButtonKeys.OK)); |
| 471 |
} |
| 472 |
|
| 473 |
function testGetAllButtons() { |
| 474 |
dialog.setButtonSet(goog.ui.Dialog.ButtonSet.YES_NO_CANCEL); |
| 475 |
var buttons = goog.dom.getElementsByTagName( |
| 476 |
goog.dom.TagName.BUTTON, dialog.getElement()); |
| 477 |
for (var i = 0; i < buttons.length; i++) { |
| 478 |
assertEquals(buttons[i], dialog.getButtonSet().getAllButtons()[i]); |
| 479 |
} |
| 480 |
} |
| 481 |
|
| 482 |
function testSetButtonEnabled() { |
| 483 |
var buttonSet = goog.ui.Dialog.ButtonSet.createYesNoCancel(); |
| 484 |
dialog.setButtonSet(buttonSet); |
| 485 |
assertFalse( |
| 486 |
buttonSet.getButton(goog.ui.Dialog.DefaultButtonKeys.NO).disabled); |
| 487 |
buttonSet.setButtonEnabled(goog.ui.Dialog.DefaultButtonKeys.NO, false); |
| 488 |
assertTrue(buttonSet.getButton(goog.ui.Dialog.DefaultButtonKeys.NO).disabled); |
| 489 |
buttonSet.setButtonEnabled(goog.ui.Dialog.DefaultButtonKeys.NO, true); |
| 490 |
assertFalse( |
| 491 |
buttonSet.getButton(goog.ui.Dialog.DefaultButtonKeys.NO).disabled); |
| 492 |
} |
| 493 |
|
| 494 |
function testSetAllButtonsEnabled() { |
| 495 |
var buttonSet = goog.ui.Dialog.ButtonSet.createContinueSaveCancel(); |
| 496 |
dialog.setButtonSet(buttonSet); |
| 497 |
var buttons = buttonSet.getAllButtons(); |
| 498 |
for (var i = 0; i < buttons.length; i++) { |
| 499 |
assertFalse(buttons[i].disabled); |
| 500 |
} |
| 501 |
|
| 502 |
buttonSet.setAllButtonsEnabled(false); |
| 503 |
for (var i = 0; i < buttons.length; i++) { |
| 504 |
assertTrue(buttons[i].disabled); |
| 505 |
} |
| 506 |
|
| 507 |
buttonSet.setAllButtonsEnabled(true); |
| 508 |
for (var i = 0; i < buttons.length; i++) { |
| 509 |
assertFalse(buttons[i].disabled); |
| 510 |
} |
| 511 |
} |
| 512 |
|
| 513 |
function testIframeMask() { |
| 514 |
var prevNumFrames = |
| 515 |
goog.dom.getElementsByTagNameAndClass(goog.dom.TagName.IFRAME).length; |
| 516 |
// generate a new dialog |
| 517 |
dialog.dispose(); |
| 518 |
dialog = new goog.ui.Dialog(null, true /* iframe mask */); |
| 519 |
dialog.setVisible(true); |
| 520 |
|
| 521 |
// Test that the dialog added one iframe to the document. |
| 522 |
// The absolute number of iframes should not be tested because, |
| 523 |
// in certain cases, the test runner itself can can add an iframe |
| 524 |
// to the document as part of a strategy not to block the UI for too long. |
| 525 |
// See goog.async.nextTick.getSetImmediateEmulator_. |
| 526 |
var curNumFrames = |
| 527 |
goog.dom.getElementsByTagNameAndClass(goog.dom.TagName.IFRAME).length; |
| 528 |
assertEquals('No iframe mask created', prevNumFrames + 1, curNumFrames); |
| 529 |
} |
| 530 |
|
| 531 |
function testNonModalDialog() { |
| 532 |
var prevNumFrames = |
| 533 |
goog.dom.getElementsByTagNameAndClass(goog.dom.TagName.IFRAME).length; |
| 534 |
// generate a new dialog |
| 535 |
dialog.dispose(); |
| 536 |
dialog = new goog.ui.Dialog(null, true /* iframe mask */); |
| 537 |
dialog.setModal(false); |
| 538 |
assertAriaHidden(false); |
| 539 |
dialog.setVisible(true); |
| 540 |
assertAriaHidden(true); |
| 541 |
|
| 542 |
// Test that the dialog did not change the number of iframes in the document. |
| 543 |
// The absolute number of iframes should not be tested because, |
| 544 |
// in certain cases, the test runner itself can can add an iframe |
| 545 |
// to the document as part of a strategy not to block the UI for too long. |
| 546 |
// See goog.async.nextTick.getSetImmediateEmulator_. |
| 547 |
var curNumFrames = |
| 548 |
goog.dom.getElementsByTagNameAndClass(goog.dom.TagName.IFRAME).length; |
| 549 |
assertEquals( |
| 550 |
'Iframe mask created for modal dialog', prevNumFrames, curNumFrames); |
| 551 |
} |
| 552 |
|
| 553 |
function testSwapModalForOpenDialog() { |
| 554 |
dialog.dispose(); |
| 555 |
dialog = new goog.ui.Dialog(null, true /* iframe mask */); |
| 556 |
assertAriaHidden(false); |
| 557 |
dialog.setVisible(true); |
| 558 |
assertAriaHidden(true); |
| 559 |
dialog.setModal(false); |
| 560 |
assertAriaHidden(false); |
| 561 |
assertFalse( |
| 562 |
'IFrame bg element should not be in dom', |
| 563 |
goog.dom.contains(document.body, dialog.getBackgroundIframe())); |
| 564 |
assertFalse( |
| 565 |
'bg element should not be in dom', |
| 566 |
goog.dom.contains(document.body, dialog.getBackgroundElement())); |
| 567 |
|
| 568 |
dialog.setModal(true); |
| 569 |
assertAriaHidden(true); |
| 570 |
assertTrue( |
| 571 |
'IFrame bg element should be in dom', |
| 572 |
goog.dom.contains(document.body, dialog.getBackgroundIframe())); |
| 573 |
assertTrue( |
| 574 |
'bg element should be in dom', |
| 575 |
goog.dom.contains(document.body, dialog.getBackgroundElement())); |
| 576 |
|
| 577 |
assertEquals( |
| 578 |
'IFrame bg element is a child of body', document.body, |
| 579 |
dialog.getBackgroundIframe().parentNode); |
| 580 |
assertEquals( |
| 581 |
'bg element is a child of body', document.body, |
| 582 |
dialog.getBackgroundElement().parentNode); |
| 583 |
|
| 584 |
assertTrue( |
| 585 |
'IFrame bg element should visible', |
| 586 |
goog.style.isElementShown(dialog.getBackgroundIframe())); |
| 587 |
assertTrue( |
| 588 |
'bg element should be visible', |
| 589 |
goog.style.isElementShown(dialog.getBackgroundElement())); |
| 590 |
} |
| 591 |
|
| 592 |
|
| 593 |
/** |
| 594 |
* Assert that the dialog has buttons with the given keys in the correct |
| 595 |
* order. |
| 596 |
* @param {Array<string>} keys An array of button keys. |
| 597 |
*/ |
| 598 |
function assertButtons(keys) { |
| 599 |
var buttons = goog.dom.getElementsByTagName( |
| 600 |
goog.dom.TagName.BUTTON, dialog.getElement()); |
| 601 |
var actualKeys = []; |
| 602 |
for (var i = 0; i < buttons.length; i++) { |
| 603 |
actualKeys[i] = buttons[i].name; |
| 604 |
} |
| 605 |
assertArrayEquals(keys, actualKeys); |
| 606 |
} |
| 607 |
|
| 608 |
function testButtonSetOkFiresDialogEventOnEscape() { |
| 609 |
dialog.setButtonSet(goog.ui.Dialog.ButtonSet.OK); |
| 610 |
var wasCalled = false; |
| 611 |
var callRecorder = function() { wasCalled = true; }; |
| 612 |
goog.events.listen(dialog, goog.ui.Dialog.EventType.SELECT, callRecorder); |
| 613 |
goog.testing.events.fireKeySequence( |
| 614 |
dialog.getElement(), goog.events.KeyCodes.ESC); |
| 615 |
assertTrue(wasCalled); |
| 616 |
} |
| 617 |
|
| 618 |
function testHideButtons_afterRender() { |
| 619 |
dialog.setButtonSet(goog.ui.Dialog.ButtonSet.OK); |
| 620 |
assertTrue(goog.style.isElementShown(dialog.buttonEl_)); |
| 621 |
dialog.setButtonSet(null); |
| 622 |
assertFalse(goog.style.isElementShown(dialog.buttonEl_)); |
| 623 |
dialog.setButtonSet(goog.ui.Dialog.ButtonSet.OK); |
| 624 |
assertTrue(goog.style.isElementShown(dialog.buttonEl_)); |
| 625 |
} |
| 626 |
|
| 627 |
function testHideButtons_beforeRender() { |
| 628 |
dialog.dispose(); |
| 629 |
|
| 630 |
dialog = new goog.ui.Dialog(); |
| 631 |
dialog.setButtonSet(null); |
| 632 |
dialog.setVisible(true); |
| 633 |
assertFalse(goog.style.isElementShown(dialog.buttonEl_)); |
| 634 |
dialog.setButtonSet(goog.ui.Dialog.ButtonSet.OK); |
| 635 |
assertTrue(goog.style.isElementShown(dialog.buttonEl_)); |
| 636 |
} |
| 637 |
|
| 638 |
function testHideButtons_beforeDecorate() { |
| 639 |
dialog.dispose(); |
| 640 |
|
| 641 |
dialog = new goog.ui.Dialog(); |
| 642 |
dialog.setButtonSet(null); |
| 643 |
dialog.decorate(decorateTarget); |
| 644 |
dialog.setVisible(true); |
| 645 |
assertFalse(goog.style.isElementShown(dialog.buttonEl_)); |
| 646 |
dialog.setButtonSet(goog.ui.Dialog.ButtonSet.OK); |
| 647 |
assertTrue(goog.style.isElementShown(dialog.buttonEl_)); |
| 648 |
} |
| 649 |
|
| 650 |
function testAriaLabelledBy_render() { |
| 651 |
dialog.dispose(); |
| 652 |
|
| 653 |
dialog = new goog.ui.Dialog(); |
| 654 |
dialog.render(); |
| 655 |
assertTrue(!!dialog.getTitleTextElement().id); |
| 656 |
assertNotNull(dialog.getElement()); |
| 657 |
assertEquals( |
| 658 |
dialog.getTitleTextElement().id, |
| 659 |
goog.a11y.aria.getState(dialog.getElement(), 'labelledby')); |
| 660 |
} |
| 661 |
|
| 662 |
function testAriaLabelledBy_decorate() { |
| 663 |
dialog.dispose(); |
| 664 |
|
| 665 |
dialog = new goog.ui.Dialog(); |
| 666 |
dialog.decorate(decorateTarget); |
| 667 |
dialog.setVisible(true); |
| 668 |
assertTrue(!!dialog.getTitleTextElement().id); |
| 669 |
assertNotNull(dialog.getElement()); |
| 670 |
assertEquals( |
| 671 |
dialog.getTitleTextElement().id, |
| 672 |
goog.a11y.aria.getState(dialog.getElement(), 'labelledby')); |
| 673 |
} |
| 674 |
|
| 675 |
|
| 676 |
function testPreferredAriaRole_renderDefault() { |
| 677 |
dialog.dispose(); |
| 678 |
|
| 679 |
dialog = new goog.ui.Dialog(); |
| 680 |
dialog.render(); |
| 681 |
assertNotNull(dialog.getElement()); |
| 682 |
assertEquals( |
| 683 |
dialog.getPreferredAriaRole(), |
| 684 |
goog.a11y.aria.getRole(dialog.getElement())); |
| 685 |
} |
| 686 |
|
| 687 |
function testPreferredAriaRole_decorateDefault() { |
| 688 |
dialog.dispose(); |
| 689 |
|
| 690 |
dialog = new goog.ui.Dialog(); |
| 691 |
dialog.decorate(decorateTarget); |
| 692 |
assertNotNull(dialog.getElement()); |
| 693 |
assertEquals( |
| 694 |
dialog.getPreferredAriaRole(), |
| 695 |
goog.a11y.aria.getRole(dialog.getElement())); |
| 696 |
} |
| 697 |
|
| 698 |
function testPreferredAriaRole_renderOverride() { |
| 699 |
dialog.dispose(); |
| 700 |
|
| 701 |
dialog = new goog.ui.Dialog(); |
| 702 |
dialog.setPreferredAriaRole(goog.a11y.aria.Role.ALERTDIALOG); |
| 703 |
dialog.render(); |
| 704 |
assertNotNull(dialog.getElement()); |
| 705 |
assertEquals( |
| 706 |
goog.a11y.aria.Role.ALERTDIALOG, |
| 707 |
goog.a11y.aria.getRole(dialog.getElement())); |
| 708 |
} |
| 709 |
|
| 710 |
function testPreferredAriaRole_decorateOverride() { |
| 711 |
dialog.dispose(); |
| 712 |
|
| 713 |
dialog = new goog.ui.Dialog(); |
| 714 |
dialog.setPreferredAriaRole(goog.a11y.aria.Role.ALERTDIALOG); |
| 715 |
dialog.decorate(decorateTarget); |
| 716 |
assertNotNull(dialog.getElement()); |
| 717 |
assertEquals( |
| 718 |
goog.a11y.aria.Role.ALERTDIALOG, |
| 719 |
goog.a11y.aria.getRole(dialog.getElement())); |
| 720 |
} |
| 721 |
|
| 722 |
function testDefaultOpacityIsAppliedOnRender() { |
| 723 |
dialog.dispose(); |
| 724 |
|
| 725 |
dialog = new goog.ui.Dialog(); |
| 726 |
dialog.render(); |
| 727 |
assertEquals(0.5, goog.style.getOpacity(dialog.getBackgroundElement())); |
| 728 |
} |
| 729 |
|
| 730 |
function testDefaultOpacityIsAppliedOnDecorate() { |
| 731 |
dialog.dispose(); |
| 732 |
|
| 733 |
dialog = new goog.ui.Dialog(); |
| 734 |
dialog.decorate(decorateTarget); |
| 735 |
assertEquals(0.5, goog.style.getOpacity(dialog.getBackgroundElement())); |
| 736 |
} |
| 737 |
|
| 738 |
function testDraggableStyle() { |
| 739 |
assertTrue( |
| 740 |
'draggable CSS class is set', |
| 741 |
goog.dom.classlist.contains( |
| 742 |
dialog.titleEl_, 'modal-dialog-title-draggable')); |
| 743 |
dialog.setDraggable(false); |
| 744 |
assertFalse( |
| 745 |
'draggable CSS class is removed', |
| 746 |
goog.dom.classlist.contains( |
| 747 |
dialog.titleEl_, 'modal-dialog-title-draggable')); |
| 748 |
} |
| 749 |
|
| 750 |
function testDraggingLifecycle() { |
| 751 |
dialog.dispose(); |
| 752 |
|
| 753 |
dialog = new goog.ui.Dialog(); |
| 754 |
dialog.setDraggerLimits_ = goog.testing.recordFunction(); |
| 755 |
dialog.createDom(); |
| 756 |
assertNull('dragger is not created in createDom', dialog.dragger_); |
| 757 |
|
| 758 |
dialog.setVisible(true); |
| 759 |
assertNotNull( |
| 760 |
'dragger is created when the dialog is rendered', dialog.dragger_); |
| 761 |
|
| 762 |
assertNull( |
| 763 |
'dragging limits are not set just before dragging', |
| 764 |
dialog.setDraggerLimits_.getLastCall()); |
| 765 |
goog.testing.events.fireMouseDownEvent(dialog.titleEl_); |
| 766 |
assertNotNull( |
| 767 |
'dragging limits are set', dialog.setDraggerLimits_.getLastCall()); |
| 768 |
|
| 769 |
dialog.exitDocument(); |
| 770 |
assertNull('dragger is cleaned up in exitDocument', dialog.dragger_); |
| 771 |
} |
| 772 |
|
| 773 |
function testDisposingVisibleDialogWithTransitionsDoesNotThrowException() { |
| 774 |
var transition = goog.fx.css3.fadeIn(dialog.getElement(), 0.1 /* duration */); |
| 775 |
|
| 776 |
dialog.setTransition(transition, transition, transition, transition); |
| 777 |
dialog.setVisible(true); |
| 778 |
dialog.dispose(); |
| 779 |
// Nothing to assert. We only want to ensure that there is no error. |
| 780 |
} |
| 781 |
|
| 782 |
function testEventsDuringAnimation() { |
| 783 |
dialog.dispose(); |
| 784 |
dialog = new goog.ui.Dialog(); |
| 785 |
dialog.render(); |
| 786 |
dialog.setTransition( |
| 787 |
goog.fx.css3.fadeIn(dialog.getElement(), 1), |
| 788 |
goog.fx.css3.fadeIn(dialog.getBackgroundElement(), 1), |
| 789 |
goog.fx.css3.fadeOut(dialog.getElement(), 1), |
| 790 |
goog.fx.css3.fadeOut(dialog.getBackgroundElement(), 1)); |
| 791 |
dialog.setVisible(true); |
| 792 |
assertTrue(dialog.isVisible()); |
| 793 |
|
| 794 |
var buttonSet = dialog.getButtonSet(); |
| 795 |
var button = buttonSet.getButton(buttonSet.getDefault()); |
| 796 |
|
| 797 |
// The button event fires while the animation is still going. |
| 798 |
goog.testing.events.fireClickSequence(button); |
| 799 |
mockClock.tick(2000); |
| 800 |
assertFalse(dialog.isVisible()); |
| 801 |
} |
| 802 |
|
| 803 |
function testHtmlContent() { |
| 804 |
dialog.setSafeHtmlContent( |
| 805 |
goog.html.testing.newSafeHtmlForTest( |
| 806 |
'<span class="theSpan">Hello</span>')); |
| 807 |
var spanEl = |
| 808 |
goog.dom.getElementByClass('theSpan', dialog.getContentElement()); |
| 809 |
assertEquals('Hello', goog.dom.getTextContent(spanEl)); |
| 810 |
assertEquals('<span class="theSpan">Hello</span>', dialog.getContent()); |
| 811 |
assertEquals( |
| 812 |
'<span class="theSpan">Hello</span>', |
| 813 |
goog.html.SafeHtml.unwrap(dialog.getSafeHtmlContent())); |
| 814 |
} |
| 815 |
|
| 816 |
function testSetTextContent() { |
| 817 |
dialog.setTextContent('Dinner <3\nTogether'); |
| 818 |
assertEquals('Dinner <3<br>Together', dialog.getContent()); |
| 819 |
} |
| 820 |
|
| 821 |
function testFocus() { |
| 822 |
// Focus should go to the dialog element. |
| 823 |
document.body.focus(); |
| 824 |
dialog.focus(); |
| 825 |
assertEquals(dialog.getElement(), document.activeElement); |
| 826 |
} |
| 827 |
|
| 828 |
// Asserts that a test element which is a child of the document body has the |
| 829 |
// aria property 'hidden' set on it, or not. |
| 830 |
function assertAriaHidden(expectedHidden) { |
| 831 |
var expectedString = expectedHidden ? 'true' : ''; |
| 832 |
assertEquals( |
| 833 |
expectedString, |
| 834 |
goog.a11y.aria.getState(bodyChildElement, goog.a11y.aria.State.HIDDEN)); |
| 835 |
} |