| 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.editor.plugins.AbstractDialogPluginTest'); |
| 16 |
goog.setTestOnly('goog.editor.plugins.AbstractDialogPluginTest'); |
| 17 |
|
| 18 |
goog.require('goog.dom'); |
| 19 |
goog.require('goog.dom.SavedRange'); |
| 20 |
goog.require('goog.dom.TagName'); |
| 21 |
goog.require('goog.editor.Field'); |
| 22 |
goog.require('goog.editor.plugins.AbstractDialogPlugin'); |
| 23 |
goog.require('goog.events.Event'); |
| 24 |
goog.require('goog.events.EventHandler'); |
| 25 |
goog.require('goog.functions'); |
| 26 |
goog.require('goog.html.SafeHtml'); |
| 27 |
goog.require('goog.testing.MockClock'); |
| 28 |
goog.require('goog.testing.MockControl'); |
| 29 |
goog.require('goog.testing.PropertyReplacer'); |
| 30 |
goog.require('goog.testing.editor.FieldMock'); |
| 31 |
goog.require('goog.testing.editor.TestHelper'); |
| 32 |
goog.require('goog.testing.events'); |
| 33 |
goog.require('goog.testing.jsunit'); |
| 34 |
goog.require('goog.testing.mockmatchers.ArgumentMatcher'); |
| 35 |
goog.require('goog.ui.editor.AbstractDialog'); |
| 36 |
goog.require('goog.userAgent'); |
| 37 |
|
| 38 |
var plugin; |
| 39 |
var mockCtrl; |
| 40 |
var mockField; |
| 41 |
var mockSavedRange; |
| 42 |
var mockOpenedHandler; |
| 43 |
var mockClosedHandler; |
| 44 |
|
| 45 |
var COMMAND = 'myCommand'; |
| 46 |
var stubs = new goog.testing.PropertyReplacer(); |
| 47 |
|
| 48 |
var mockClock; |
| 49 |
var fieldObj; |
| 50 |
var fieldElem; |
| 51 |
var mockHandler; |
| 52 |
|
| 53 |
function setUp() { |
| 54 |
mockCtrl = new goog.testing.MockControl(); |
| 55 |
mockOpenedHandler = mockCtrl.createLooseMock(goog.events.EventHandler); |
| 56 |
mockClosedHandler = mockCtrl.createLooseMock(goog.events.EventHandler); |
| 57 |
|
| 58 |
mockField = new goog.testing.editor.FieldMock(undefined, undefined, {}); |
| 59 |
mockCtrl.addMock(mockField); |
| 60 |
mockField.focus(); |
| 61 |
|
| 62 |
plugin = createDialogPlugin(); |
| 63 |
} |
| 64 |
|
| 65 |
function setUpMockRange() { |
| 66 |
mockSavedRange = mockCtrl.createLooseMock(goog.dom.SavedRange); |
| 67 |
mockSavedRange.restore(); |
| 68 |
|
| 69 |
stubs.setPath( |
| 70 |
'goog.editor.range.saveUsingNormalizedCarets', |
| 71 |
goog.functions.constant(mockSavedRange)); |
| 72 |
} |
| 73 |
|
| 74 |
function tearDown() { |
| 75 |
stubs.reset(); |
| 76 |
tearDownRealEditableField(); |
| 77 |
if (mockClock) { |
| 78 |
// Crucial to letting time operations work normally in the rest of tests. |
| 79 |
mockClock.dispose(); |
| 80 |
} |
| 81 |
if (plugin) { |
| 82 |
mockField.$setIgnoreUnexpectedCalls(true); |
| 83 |
plugin.dispose(); |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
/** |
| 89 |
* Creates a concrete instance of goog.ui.editor.AbstractDialog by adding |
| 90 |
* a plain implementation of createDialogControl(). |
| 91 |
* @param {goog.dom.DomHelper} domHelper The dom helper to be used to |
| 92 |
* create the dialog. |
| 93 |
* @return {goog.ui.editor.AbstractDialog} The created dialog. |
| 94 |
*/ |
| 95 |
function createDialog(domHelper) { |
| 96 |
var dialog = new goog.ui.editor.AbstractDialog(domHelper); |
| 97 |
dialog.createDialogControl = function() { |
| 98 |
return new goog.ui.editor.AbstractDialog.Builder(dialog).build(); |
| 99 |
}; |
| 100 |
return dialog; |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
/** |
| 105 |
* Creates a concrete instance of the abstract class |
| 106 |
* goog.editor.plugins.AbstractDialogPlugin |
| 107 |
* and registers it with the mock editable field being used. |
| 108 |
* @return {goog.editor.plugins.AbstractDialogPlugin} The created plugin. |
| 109 |
*/ |
| 110 |
function createDialogPlugin() { |
| 111 |
var plugin = new goog.editor.plugins.AbstractDialogPlugin(COMMAND); |
| 112 |
plugin.createDialog = createDialog; |
| 113 |
plugin.returnControlToEditableField = plugin.restoreOriginalSelection; |
| 114 |
plugin.registerFieldObject(mockField); |
| 115 |
plugin.addEventListener( |
| 116 |
goog.editor.plugins.AbstractDialogPlugin.EventType.OPENED, |
| 117 |
mockOpenedHandler); |
| 118 |
plugin.addEventListener( |
| 119 |
goog.editor.plugins.AbstractDialogPlugin.EventType.CLOSED, |
| 120 |
mockClosedHandler); |
| 121 |
return plugin; |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
/** |
| 126 |
* Sets up the mock event handler to expect an OPENED event. |
| 127 |
*/ |
| 128 |
function expectOpened(/** number= */ opt_times) { |
| 129 |
mockOpenedHandler.handleEvent( |
| 130 |
new goog.testing.mockmatchers.ArgumentMatcher(function(arg) { |
| 131 |
return arg.type == |
| 132 |
goog.editor.plugins.AbstractDialogPlugin.EventType.OPENED; |
| 133 |
})); |
| 134 |
mockField.dispatchSelectionChangeEvent(); |
| 135 |
if (opt_times) { |
| 136 |
mockOpenedHandler.$times(opt_times); |
| 137 |
mockField.$times(opt_times); |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
/** |
| 143 |
* Sets up the mock event handler to expect a CLOSED event. |
| 144 |
*/ |
| 145 |
function expectClosed(/** number= */ opt_times) { |
| 146 |
mockClosedHandler.handleEvent( |
| 147 |
new goog.testing.mockmatchers.ArgumentMatcher(function(arg) { |
| 148 |
return arg.type == |
| 149 |
goog.editor.plugins.AbstractDialogPlugin.EventType.CLOSED; |
| 150 |
})); |
| 151 |
mockField.dispatchSelectionChangeEvent(); |
| 152 |
if (opt_times) { |
| 153 |
mockClosedHandler.$times(opt_times); |
| 154 |
mockField.$times(opt_times); |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
/** |
| 160 |
* Tests the simple flow of calling execCommand (which opens the |
| 161 |
* dialog) and immediately disposing of the plugin (which closes the dialog). |
| 162 |
* @param {boolean=} opt_reuse Whether to set the plugin to reuse its dialog. |
| 163 |
*/ |
| 164 |
function testExecAndDispose(opt_reuse) { |
| 165 |
setUpMockRange(); |
| 166 |
expectOpened(); |
| 167 |
expectClosed(); |
| 168 |
mockField.debounceEvent(goog.editor.Field.EventType.SELECTIONCHANGE); |
| 169 |
mockCtrl.$replayAll(); |
| 170 |
if (opt_reuse) { |
| 171 |
plugin.setReuseDialog(true); |
| 172 |
} |
| 173 |
assertFalse( |
| 174 |
'Dialog should not be open yet', |
| 175 |
!!plugin.getDialog() && plugin.getDialog().isOpen()); |
| 176 |
|
| 177 |
plugin.execCommand(COMMAND); |
| 178 |
assertTrue( |
| 179 |
'Dialog should be open now', |
| 180 |
!!plugin.getDialog() && plugin.getDialog().isOpen()); |
| 181 |
|
| 182 |
var tempDialog = plugin.getDialog(); |
| 183 |
plugin.dispose(); |
| 184 |
assertFalse( |
| 185 |
'Dialog should not still be open after disposal', tempDialog.isOpen()); |
| 186 |
mockCtrl.$verifyAll(); |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
/** |
| 191 |
* Tests execCommand and dispose while reusing the dialog. |
| 192 |
*/ |
| 193 |
function testExecAndDisposeReuse() { |
| 194 |
testExecAndDispose(true); |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
/** |
| 199 |
* Tests the flow of calling execCommand (which opens the dialog) and |
| 200 |
* then hiding it (simulating that a user did somthing to cause the dialog to |
| 201 |
* close). |
| 202 |
* @param {boolean=} opt_reuse Whether to set the plugin to reuse its dialog. |
| 203 |
*/ |
| 204 |
function testExecAndHide(opt_reuse) { |
| 205 |
setUpMockRange(); |
| 206 |
expectOpened(); |
| 207 |
expectClosed(); |
| 208 |
mockField.debounceEvent(goog.editor.Field.EventType.SELECTIONCHANGE); |
| 209 |
mockCtrl.$replayAll(); |
| 210 |
if (opt_reuse) { |
| 211 |
plugin.setReuseDialog(true); |
| 212 |
} |
| 213 |
assertFalse( |
| 214 |
'Dialog should not be open yet', |
| 215 |
!!plugin.getDialog() && plugin.getDialog().isOpen()); |
| 216 |
|
| 217 |
plugin.execCommand(COMMAND); |
| 218 |
assertTrue( |
| 219 |
'Dialog should be open now', |
| 220 |
!!plugin.getDialog() && plugin.getDialog().isOpen()); |
| 221 |
|
| 222 |
var tempDialog = plugin.getDialog(); |
| 223 |
plugin.getDialog().hide(); |
| 224 |
assertFalse( |
| 225 |
'Dialog should not still be open after hiding', tempDialog.isOpen()); |
| 226 |
if (opt_reuse) { |
| 227 |
assertFalse( |
| 228 |
'Dialog should not be disposed after hiding (will be reused)', |
| 229 |
tempDialog.isDisposed()); |
| 230 |
} else { |
| 231 |
assertTrue( |
| 232 |
'Dialog should be disposed after hiding', tempDialog.isDisposed()); |
| 233 |
} |
| 234 |
plugin.dispose(); |
| 235 |
mockCtrl.$verifyAll(); |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
/** |
| 240 |
* Tests execCommand and hide while reusing the dialog. |
| 241 |
*/ |
| 242 |
function testExecAndHideReuse() { |
| 243 |
testExecAndHide(true); |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
/** |
| 248 |
* Tests the flow of calling execCommand (which opens a dialog) and |
| 249 |
* then calling it again before the first dialog is closed. This is not |
| 250 |
* something anyone should be doing since dialogs are (usually?) modal so the |
| 251 |
* user can't do another execCommand before closing the first dialog. But |
| 252 |
* since the API makes it possible, I thought it would be good to guard |
| 253 |
* against and unit test. |
| 254 |
* @param {boolean=} opt_reuse Whether to set the plugin to reuse its dialog. |
| 255 |
*/ |
| 256 |
function testExecTwice(opt_reuse) { |
| 257 |
setUpMockRange(); |
| 258 |
if (opt_reuse) { |
| 259 |
expectOpened(2); // The second exec should cause a second OPENED event. |
| 260 |
// But the dialog was not closed between exec calls, so only one CLOSED is |
| 261 |
// expected. |
| 262 |
expectClosed(); |
| 263 |
plugin.setReuseDialog(true); |
| 264 |
mockField.debounceEvent(goog.editor.Field.EventType.SELECTIONCHANGE); |
| 265 |
} else { |
| 266 |
expectOpened(2); // The second exec should cause a second OPENED event. |
| 267 |
// The first dialog will be disposed so there should be two CLOSED events. |
| 268 |
expectClosed(2); |
| 269 |
mockSavedRange.restore(); // Expected 2x, once already recorded in setup. |
| 270 |
mockField.focus(); // Expected 2x, once already recorded in setup. |
| 271 |
mockField.debounceEvent(goog.editor.Field.EventType.SELECTIONCHANGE); |
| 272 |
mockField.$times(2); |
| 273 |
} |
| 274 |
mockCtrl.$replayAll(); |
| 275 |
|
| 276 |
assertFalse( |
| 277 |
'Dialog should not be open yet', |
| 278 |
!!plugin.getDialog() && plugin.getDialog().isOpen()); |
| 279 |
|
| 280 |
plugin.execCommand(COMMAND); |
| 281 |
assertTrue( |
| 282 |
'Dialog should be open now', |
| 283 |
!!plugin.getDialog() && plugin.getDialog().isOpen()); |
| 284 |
|
| 285 |
var tempDialog = plugin.getDialog(); |
| 286 |
plugin.execCommand(COMMAND); |
| 287 |
if (opt_reuse) { |
| 288 |
assertTrue( |
| 289 |
'Reused dialog should still be open after second exec', |
| 290 |
tempDialog.isOpen()); |
| 291 |
assertFalse( |
| 292 |
'Reused dialog should not be disposed after second exec', |
| 293 |
tempDialog.isDisposed()); |
| 294 |
} else { |
| 295 |
assertFalse( |
| 296 |
'First dialog should not still be open after opening second', |
| 297 |
tempDialog.isOpen()); |
| 298 |
assertTrue( |
| 299 |
'First dialog should be disposed after opening second', |
| 300 |
tempDialog.isDisposed()); |
| 301 |
} |
| 302 |
plugin.dispose(); |
| 303 |
mockCtrl.$verifyAll(); |
| 304 |
} |
| 305 |
|
| 306 |
|
| 307 |
/** |
| 308 |
* Tests execCommand twice while reusing the dialog. |
| 309 |
*/ |
| 310 |
function testExecTwiceReuse() { |
| 311 |
// Test is failing with an out-of-memory error in IE7. |
| 312 |
if (goog.userAgent.IE && !goog.userAgent.isVersionOrHigher('8')) { |
| 313 |
return; |
| 314 |
} |
| 315 |
|
| 316 |
testExecTwice(true); |
| 317 |
} |
| 318 |
|
| 319 |
|
| 320 |
/** |
| 321 |
* Tests that the selection is cleared when the dialog opens and is |
| 322 |
* correctly restored after it closes. |
| 323 |
*/ |
| 324 |
function testRestoreSelection() { |
| 325 |
setUpRealEditableField(); |
| 326 |
|
| 327 |
fieldObj.setSafeHtml(false, goog.html.SafeHtml.htmlEscape('12345')); |
| 328 |
var elem = fieldObj.getElement(); |
| 329 |
var helper = new goog.testing.editor.TestHelper(elem); |
| 330 |
helper.select('12345', 1, '12345', 4); // Selects '234'. |
| 331 |
|
| 332 |
assertEquals( |
| 333 |
'Incorrect text selected before dialog is opened', '234', |
| 334 |
fieldObj.getRange().getText()); |
| 335 |
plugin.execCommand(COMMAND); |
| 336 |
if (!goog.userAgent.IE && !goog.userAgent.OPERA) { |
| 337 |
// IE returns some bogus range when field doesn't have selection. |
| 338 |
// Opera can't remove the selection from a whitebox field. |
| 339 |
assertNull( |
| 340 |
'There should be no selection while dialog is open', |
| 341 |
fieldObj.getRange()); |
| 342 |
} |
| 343 |
plugin.getDialog().hide(); |
| 344 |
assertEquals( |
| 345 |
'Incorrect text selected after dialog is closed', '234', |
| 346 |
fieldObj.getRange().getText()); |
| 347 |
} |
| 348 |
|
| 349 |
|
| 350 |
/** |
| 351 |
* Setup a real editable field (instead of a mock) and register the plugin to |
| 352 |
* it. |
| 353 |
*/ |
| 354 |
function setUpRealEditableField() { |
| 355 |
fieldElem = goog.dom.createElement(goog.dom.TagName.DIV); |
| 356 |
fieldElem.id = 'myField'; |
| 357 |
document.body.appendChild(fieldElem); |
| 358 |
fieldObj = new goog.editor.Field('myField', document); |
| 359 |
fieldObj.makeEditable(); |
| 360 |
// Register the plugin to that field. |
| 361 |
plugin.getTrogClassId = goog.functions.constant('myClassId'); |
| 362 |
fieldObj.registerPlugin(plugin); |
| 363 |
} |
| 364 |
|
| 365 |
|
| 366 |
/** |
| 367 |
* Tear down the real editable field. |
| 368 |
*/ |
| 369 |
function tearDownRealEditableField() { |
| 370 |
if (fieldObj) { |
| 371 |
fieldObj.makeUneditable(); |
| 372 |
fieldObj.dispose(); |
| 373 |
fieldObj = null; |
| 374 |
} |
| 375 |
if (fieldElem && fieldElem.parentNode == document.body) { |
| 376 |
document.body.removeChild(fieldElem); |
| 377 |
} |
| 378 |
} |
| 379 |
|
| 380 |
|
| 381 |
/** |
| 382 |
* Tests that after the dialog is hidden via a keystroke, the editable field |
| 383 |
* doesn't fire an extra SELECTIONCHANGE event due to the keyup from that |
| 384 |
* keystroke. |
| 385 |
* There is also a robot test in dialog_robot.html to test debouncing the |
| 386 |
* SELECTIONCHANGE event when the dialog closes. |
| 387 |
*/ |
| 388 |
function testDebounceSelectionChange() { |
| 389 |
mockClock = new goog.testing.MockClock(true); |
| 390 |
// Initial time is 0 which evaluates to false in debouncing implementation. |
| 391 |
mockClock.tick(1); |
| 392 |
|
| 393 |
setUpRealEditableField(); |
| 394 |
|
| 395 |
// Set up a mock event handler to make sure selection change isn't fired |
| 396 |
// more than once on close and a second time on close. |
| 397 |
var count = 0; |
| 398 |
fieldObj.addEventListener( |
| 399 |
goog.editor.Field.EventType.SELECTIONCHANGE, function(e) { count++; }); |
| 400 |
|
| 401 |
assertEquals(0, count); |
| 402 |
plugin.execCommand(COMMAND); |
| 403 |
assertEquals(1, count); |
| 404 |
plugin.getDialog().hide(); |
| 405 |
assertEquals(2, count); |
| 406 |
|
| 407 |
// Fake the keyup event firing on the field after the dialog closes. |
| 408 |
var e = new goog.events.Event('keyup', plugin.fieldObject.getElement()); |
| 409 |
e.keyCode = 13; |
| 410 |
goog.testing.events.fireBrowserEvent(e); |
| 411 |
|
| 412 |
// Tick the mock clock so that selection change tries to fire. |
| 413 |
mockClock.tick(goog.editor.Field.SELECTION_CHANGE_FREQUENCY_ + 1); |
| 414 |
|
| 415 |
// Ensure the handler did not fire again. |
| 416 |
assertEquals(2, count); |
| 417 |
} |