| 1 |
// Copyright 2012 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 |
/** |
| 16 |
* @fileoverview Shared tests for goog.editor.Field and ContentEditableField. |
| 17 |
* Since ContentEditableField switches many of the internal code paths in Field |
| 18 |
* (such as via usesIframe()) it's important to re-run a lot of the same tests. |
| 19 |
* |
| 20 |
* @author [email protected] (Nick Santos) |
| 21 |
* @author [email protected] (Garrett Boyer) |
| 22 |
*/ |
| 23 |
|
| 24 |
/** @suppress {extraProvide} */ |
| 25 |
goog.provide('goog.editor.field_test'); |
| 26 |
|
| 27 |
goog.require('goog.array'); |
| 28 |
goog.require('goog.dom'); |
| 29 |
goog.require('goog.dom.Range'); |
| 30 |
goog.require('goog.dom.TagName'); |
| 31 |
goog.require('goog.dom.classlist'); |
| 32 |
goog.require('goog.editor.BrowserFeature'); |
| 33 |
goog.require('goog.editor.Field'); |
| 34 |
goog.require('goog.editor.Plugin'); |
| 35 |
goog.require('goog.editor.range'); |
| 36 |
goog.require('goog.events'); |
| 37 |
goog.require('goog.events.BrowserEvent'); |
| 38 |
goog.require('goog.events.EventType'); |
| 39 |
goog.require('goog.events.KeyCodes'); |
| 40 |
goog.require('goog.functions'); |
| 41 |
goog.require('goog.html.SafeHtml'); |
| 42 |
goog.require('goog.testing.LooseMock'); |
| 43 |
goog.require('goog.testing.MockClock'); |
| 44 |
goog.require('goog.testing.dom'); |
| 45 |
goog.require('goog.testing.events'); |
| 46 |
goog.require('goog.testing.events.Event'); |
| 47 |
goog.require('goog.testing.jsunit'); |
| 48 |
goog.require('goog.testing.recordFunction'); |
| 49 |
goog.require('goog.userAgent'); |
| 50 |
goog.setTestOnly('Tests for goog.editor.*Field'); |
| 51 |
|
| 52 |
/** Hard-coded HTML for the tests. */ |
| 53 |
var HTML = '<div id="testField">I am text.</div>'; |
| 54 |
|
| 55 |
|
| 56 |
function setUp() { |
| 57 |
goog.dom.getElement('parent').innerHTML = HTML; |
| 58 |
assertTrue( |
| 59 |
'FieldConstructor should be set by the test HTML file', |
| 60 |
goog.isFunction(FieldConstructor)); |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
function tearDown() { |
| 65 |
// NOTE(nicksantos): I think IE is blowing up on this call because |
| 66 |
// it is lame. It manifests its lameness by throwing an exception. |
| 67 |
// Kudos to XT for helping me to figure this out. |
| 68 |
try { |
| 69 |
} catch (e) { |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
// Tests for the plugin interface. |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
/** |
| 78 |
* Dummy plugin for test usage. |
| 79 |
* @constructor |
| 80 |
* @extends {goog.editor.Plugin} |
| 81 |
* @final |
| 82 |
*/ |
| 83 |
function TestPlugin() { |
| 84 |
TestPlugin.base(this, 'constructor'); |
| 85 |
|
| 86 |
this.getTrogClassId = function() { return 'TestPlugin'; }; |
| 87 |
|
| 88 |
this.handleKeyDown = goog.nullFunction; |
| 89 |
this.handleKeyPress = goog.nullFunction; |
| 90 |
this.handleKeyUp = goog.nullFunction; |
| 91 |
this.handleKeyboardShortcut = goog.nullFunction; |
| 92 |
this.isSupportedCommand = goog.nullFunction; |
| 93 |
this.execCommandInternal = goog.nullFunction; |
| 94 |
this.queryCommandValue = goog.nullFunction; |
| 95 |
this.activeOnUneditableFields = goog.nullFunction; |
| 96 |
this.handleSelectionChange = goog.nullFunction; |
| 97 |
} |
| 98 |
goog.inherits(TestPlugin, goog.editor.Plugin); |
| 99 |
|
| 100 |
|
| 101 |
/** |
| 102 |
* Tests that calling registerPlugin will add the plugin to the |
| 103 |
* plugin map. |
| 104 |
*/ |
| 105 |
function testRegisterPlugin() { |
| 106 |
var editableField = new FieldConstructor('testField'); |
| 107 |
var plugin = new TestPlugin(); |
| 108 |
|
| 109 |
editableField.registerPlugin(plugin); |
| 110 |
|
| 111 |
assertEquals( |
| 112 |
'Registered plugin must be in protected plugin map.', plugin, |
| 113 |
editableField.plugins_[plugin.getTrogClassId()]); |
| 114 |
assertEquals( |
| 115 |
'Plugin has a keydown handler, should be in keydown map', plugin, |
| 116 |
editableField.indexedPlugins_[goog.editor.Plugin.Op.KEYDOWN][0]); |
| 117 |
assertEquals( |
| 118 |
'Plugin has a keypress handler, should be in keypress map', plugin, |
| 119 |
editableField.indexedPlugins_[goog.editor.Plugin.Op.KEYPRESS][0]); |
| 120 |
assertEquals( |
| 121 |
'Plugin has a keyup handler, should be in keuup map', plugin, |
| 122 |
editableField.indexedPlugins_[goog.editor.Plugin.Op.KEYUP][0]); |
| 123 |
assertEquals( |
| 124 |
'Plugin has a selectionchange handler, should be in selectionchange map', |
| 125 |
plugin, |
| 126 |
editableField.indexedPlugins_[goog.editor.Plugin.Op.SELECTION][0]); |
| 127 |
assertEquals( |
| 128 |
'Plugin has a shortcut handler, should be in shortcut map', plugin, |
| 129 |
editableField.indexedPlugins_[goog.editor.Plugin.Op.SHORTCUT][0]); |
| 130 |
assertEquals( |
| 131 |
'Plugin has a execCommand, should be in execCommand map', plugin, |
| 132 |
editableField.indexedPlugins_[goog.editor.Plugin.Op.EXEC_COMMAND][0]); |
| 133 |
assertEquals( |
| 134 |
'Plugin has a queryCommand, should be in queryCommand map', plugin, |
| 135 |
editableField.indexedPlugins_[goog.editor.Plugin.Op.QUERY_COMMAND][0]); |
| 136 |
assertEquals( |
| 137 |
'Plugin does not have a prepareContentsHtml,' + |
| 138 |
'should not be in prepareContentsHtml map', |
| 139 |
undefined, |
| 140 |
editableField.indexedPlugins_[goog.editor.Plugin.Op.PREPARE_CONTENTS_HTML] |
| 141 |
[0]); |
| 142 |
assertEquals( |
| 143 |
'Plugin does not have a cleanContentsDom,' + |
| 144 |
'should not be in cleanContentsDom map', |
| 145 |
undefined, |
| 146 |
editableField.indexedPlugins_[goog.editor.Plugin.Op.CLEAN_CONTENTS_DOM] |
| 147 |
[0]); |
| 148 |
assertEquals( |
| 149 |
'Plugin does not have a cleanContentsHtml,' + |
| 150 |
'should not be in cleanContentsHtml map', |
| 151 |
undefined, |
| 152 |
editableField.indexedPlugins_[goog.editor.Plugin.Op.CLEAN_CONTENTS_HTML] |
| 153 |
[0]); |
| 154 |
|
| 155 |
editableField.dispose(); |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
/** |
| 160 |
* Tests that calling unregisterPlugin will remove the plugin from |
| 161 |
* the map. |
| 162 |
*/ |
| 163 |
function testUnregisterPlugin() { |
| 164 |
var editableField = new FieldConstructor('testField'); |
| 165 |
var plugin = new TestPlugin(); |
| 166 |
|
| 167 |
editableField.registerPlugin(plugin); |
| 168 |
editableField.unregisterPlugin(plugin); |
| 169 |
|
| 170 |
assertUndefined( |
| 171 |
'Unregistered plugin must not be in protected plugin map.', |
| 172 |
editableField.plugins_[plugin.getTrogClassId()]); |
| 173 |
|
| 174 |
editableField.dispose(); |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
/** |
| 179 |
* Tests that registered plugins can be fetched by their id. |
| 180 |
*/ |
| 181 |
function testGetPluginByClassId() { |
| 182 |
var editableField = new FieldConstructor('testField'); |
| 183 |
var plugin = new TestPlugin(); |
| 184 |
|
| 185 |
assertUndefined( |
| 186 |
'Must not be able to get unregistered plugins by class id.', |
| 187 |
editableField.getPluginByClassId(plugin.getTrogClassId())); |
| 188 |
|
| 189 |
editableField.registerPlugin(plugin); |
| 190 |
assertEquals( |
| 191 |
'Must be able to get registered plugins by class id.', plugin, |
| 192 |
editableField.getPluginByClassId(plugin.getTrogClassId())); |
| 193 |
editableField.dispose(); |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
/** |
| 198 |
* Tests that plugins get auto disposed by default when the field is disposed. |
| 199 |
* Tests that plugins with setAutoDispose(false) do not get disposed when the |
| 200 |
* field is disposed. |
| 201 |
*/ |
| 202 |
function testDisposed_PluginAutoDispose() { |
| 203 |
var editableField = new FieldConstructor('testField'); |
| 204 |
var plugin = new TestPlugin(); |
| 205 |
|
| 206 |
var noDisposePlugin = new goog.editor.Plugin(); |
| 207 |
noDisposePlugin.getTrogClassId = function() { return 'noDisposeId'; }; |
| 208 |
noDisposePlugin.setAutoDispose(false); |
| 209 |
|
| 210 |
editableField.registerPlugin(plugin); |
| 211 |
editableField.registerPlugin(noDisposePlugin); |
| 212 |
editableField.dispose(); |
| 213 |
assert(editableField.isDisposed()); |
| 214 |
assertTrue(plugin.isDisposed()); |
| 215 |
assertFalse(noDisposePlugin.isDisposed()); |
| 216 |
} |
| 217 |
|
| 218 |
var STRING_KEY = String.fromCharCode(goog.events.KeyCodes.A).toLowerCase(); |
| 219 |
|
| 220 |
|
| 221 |
/** |
| 222 |
* @return {goog.events.Event} Returns an event for a keyboard shortcut |
| 223 |
* for the letter 'a'. |
| 224 |
*/ |
| 225 |
function getBrowserEvent() { |
| 226 |
var e = new goog.events.BrowserEvent(); |
| 227 |
e.ctrlKey = true; |
| 228 |
e.metaKey = true; |
| 229 |
e.charCode = goog.events.KeyCodes.A; |
| 230 |
return e; |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
/** |
| 235 |
* @param {boolean} followLinkInNewWindow Whether activating a hyperlink |
| 236 |
* in the editable field will open a new window or not. |
| 237 |
* @return {!goog.editor.Field} Returns an editable field after its load phase. |
| 238 |
*/ |
| 239 |
function createEditableFieldWithListeners(followLinkInNewWindow) { |
| 240 |
var editableField = new FieldConstructor('testField'); |
| 241 |
editableField.setFollowLinkInNewWindow(followLinkInNewWindow); |
| 242 |
|
| 243 |
var originalElement = editableField.getOriginalElement(); |
| 244 |
editableField.setupFieldObject(originalElement); |
| 245 |
editableField.handleFieldLoad(); |
| 246 |
|
| 247 |
return editableField; |
| 248 |
} |
| 249 |
|
| 250 |
function getListenerTarget(editableField) { |
| 251 |
var elt = editableField.getElement(); |
| 252 |
var listenerTarget = goog.editor.BrowserFeature.USE_DOCUMENT_FOR_KEY_EVENTS && |
| 253 |
editableField.usesIframe() ? |
| 254 |
elt.ownerDocument : |
| 255 |
elt; |
| 256 |
return listenerTarget; |
| 257 |
} |
| 258 |
|
| 259 |
function assertClickDefaultActionIsCanceled(editableField) { |
| 260 |
var cancelClickDefaultActionListener = goog.events.getListener( |
| 261 |
getListenerTarget(editableField), goog.events.EventType.CLICK, |
| 262 |
goog.editor.Field.cancelLinkClick_, undefined, editableField); |
| 263 |
|
| 264 |
assertNotNull(cancelClickDefaultActionListener); |
| 265 |
} |
| 266 |
|
| 267 |
function assertClickDefaultActionIsNotCanceled(editableField) { |
| 268 |
var cancelClickDefaultActionListener = goog.events.getListener( |
| 269 |
getListenerTarget(editableField), goog.events.EventType.CLICK, |
| 270 |
goog.editor.Field.cancelLinkClick_, undefined, editableField); |
| 271 |
|
| 272 |
assertNull(cancelClickDefaultActionListener); |
| 273 |
} |
| 274 |
|
| 275 |
|
| 276 |
/** |
| 277 |
* Tests that plugins are disabled when the field is made uneditable. |
| 278 |
*/ |
| 279 |
|
| 280 |
function testMakeUneditableDisablesPlugins() { |
| 281 |
var editableField = new FieldConstructor('testField'); |
| 282 |
var plugin = new TestPlugin(); |
| 283 |
|
| 284 |
var calls = 0; |
| 285 |
plugin.disable = function(field) { |
| 286 |
assertEquals(editableField, field); |
| 287 |
assertTrue(field.isUneditable()); |
| 288 |
calls++; |
| 289 |
}; |
| 290 |
|
| 291 |
editableField.registerPlugin(plugin); |
| 292 |
editableField.makeEditable(); |
| 293 |
|
| 294 |
assertEquals(0, calls); |
| 295 |
editableField.makeUneditable(); |
| 296 |
|
| 297 |
assertEquals(1, calls); |
| 298 |
|
| 299 |
editableField.dispose(); |
| 300 |
} |
| 301 |
|
| 302 |
|
| 303 |
/** |
| 304 |
* Test that if a browser open a new page when clicking a link in a content |
| 305 |
* editable element, a click listener is set to cancel this default action. |
| 306 |
*/ |
| 307 |
function testClickDefaultActionIsCanceledWhenBrowserFollowsClick() { |
| 308 |
// Simulate a browser that will open a new page when activating a link in a |
| 309 |
// content editable element. |
| 310 |
var editableField = |
| 311 |
createEditableFieldWithListeners(true /* followLinkInNewWindow */); |
| 312 |
assertClickDefaultActionIsCanceled(editableField); |
| 313 |
|
| 314 |
editableField.dispose(); |
| 315 |
} |
| 316 |
|
| 317 |
|
| 318 |
/** |
| 319 |
* Test that if a browser does not open a new page when clicking a link in a |
| 320 |
* content editable element, the click default action is not canceled. |
| 321 |
*/ |
| 322 |
function testClickDefaultActionIsNotCanceledWhenBrowserDontFollowsClick() { |
| 323 |
// Simulate a browser that will NOT open a new page when activating a link in |
| 324 |
// a content editable element. |
| 325 |
var editableField = |
| 326 |
createEditableFieldWithListeners(false /* followLinkInNewWindow */); |
| 327 |
assertClickDefaultActionIsNotCanceled(editableField); |
| 328 |
|
| 329 |
editableField.dispose(); |
| 330 |
} |
| 331 |
|
| 332 |
|
| 333 |
/** |
| 334 |
* Test that if a plugin registers keyup, it gets called. |
| 335 |
*/ |
| 336 |
function testPluginKeyUp() { |
| 337 |
var editableField = new FieldConstructor('testField'); |
| 338 |
var plugin = new TestPlugin(); |
| 339 |
var e = getBrowserEvent(); |
| 340 |
|
| 341 |
var mockPlugin = new goog.testing.LooseMock(plugin); |
| 342 |
mockPlugin.getTrogClassId().$returns('mockPlugin'); |
| 343 |
mockPlugin.registerFieldObject(editableField); |
| 344 |
mockPlugin.isEnabled(editableField).$anyTimes().$returns(true); |
| 345 |
mockPlugin.handleKeyUp(e); |
| 346 |
mockPlugin.$replay(); |
| 347 |
|
| 348 |
editableField.registerPlugin(mockPlugin); |
| 349 |
|
| 350 |
editableField.handleKeyUp_(e); |
| 351 |
|
| 352 |
mockPlugin.$verify(); |
| 353 |
} |
| 354 |
|
| 355 |
|
| 356 |
/** |
| 357 |
* Test that if a plugin registers keydown, it gets called. |
| 358 |
*/ |
| 359 |
function testPluginKeyDown() { |
| 360 |
var editableField = new FieldConstructor('testField'); |
| 361 |
var plugin = new TestPlugin(); |
| 362 |
var e = getBrowserEvent(); |
| 363 |
|
| 364 |
var mockPlugin = new goog.testing.LooseMock(plugin); |
| 365 |
mockPlugin.getTrogClassId().$returns('mockPlugin'); |
| 366 |
mockPlugin.registerFieldObject(editableField); |
| 367 |
mockPlugin.isEnabled(editableField).$anyTimes().$returns(true); |
| 368 |
mockPlugin.handleKeyDown(e).$returns(true); |
| 369 |
mockPlugin.$replay(); |
| 370 |
|
| 371 |
editableField.registerPlugin(mockPlugin); |
| 372 |
|
| 373 |
editableField.handleKeyDown_(e); |
| 374 |
|
| 375 |
mockPlugin.$verify(); |
| 376 |
} |
| 377 |
|
| 378 |
|
| 379 |
/** |
| 380 |
* Test that if a plugin registers keypress, it gets called. |
| 381 |
*/ |
| 382 |
function testPluginKeyPress() { |
| 383 |
var editableField = new FieldConstructor('testField'); |
| 384 |
var plugin = new TestPlugin(); |
| 385 |
var e = getBrowserEvent(); |
| 386 |
|
| 387 |
var mockPlugin = new goog.testing.LooseMock(plugin); |
| 388 |
mockPlugin.getTrogClassId().$returns('mockPlugin'); |
| 389 |
mockPlugin.registerFieldObject(editableField); |
| 390 |
mockPlugin.isEnabled(editableField).$anyTimes().$returns(true); |
| 391 |
mockPlugin.handleKeyPress(e).$returns(true); |
| 392 |
mockPlugin.$replay(); |
| 393 |
|
| 394 |
editableField.registerPlugin(mockPlugin); |
| 395 |
|
| 396 |
editableField.handleKeyPress_(e); |
| 397 |
|
| 398 |
mockPlugin.$verify(); |
| 399 |
} |
| 400 |
|
| 401 |
|
| 402 |
/** |
| 403 |
* If one plugin handles a key event, the rest of the plugins do not get their |
| 404 |
* key handlers invoked. |
| 405 |
*/ |
| 406 |
function testHandledKeyEvent() { |
| 407 |
var editableField = new FieldConstructor('testField'); |
| 408 |
var plugin = new TestPlugin(); |
| 409 |
var e = getBrowserEvent(); |
| 410 |
|
| 411 |
var mockPlugin1 = new goog.testing.LooseMock(plugin); |
| 412 |
mockPlugin1.getTrogClassId().$returns('mockPlugin1'); |
| 413 |
mockPlugin1.registerFieldObject(editableField); |
| 414 |
mockPlugin1.isEnabled(editableField).$anyTimes().$returns(true); |
| 415 |
if (goog.editor.BrowserFeature.USES_KEYDOWN) { |
| 416 |
mockPlugin1.handleKeyDown(e).$returns(true); |
| 417 |
} else { |
| 418 |
mockPlugin1.handleKeyPress(e).$returns(true); |
| 419 |
} |
| 420 |
mockPlugin1.handleKeyUp(e).$returns(true); |
| 421 |
mockPlugin1.$replay(); |
| 422 |
|
| 423 |
var mockPlugin2 = new goog.testing.LooseMock(plugin); |
| 424 |
mockPlugin2.getTrogClassId().$returns('mockPlugin2'); |
| 425 |
mockPlugin2.registerFieldObject(editableField); |
| 426 |
mockPlugin2.isEnabled(editableField).$anyTimes().$returns(true); |
| 427 |
mockPlugin2.$replay(); |
| 428 |
|
| 429 |
editableField.registerPlugin(mockPlugin1); |
| 430 |
editableField.registerPlugin(mockPlugin2); |
| 431 |
|
| 432 |
editableField.handleKeyUp_(e); |
| 433 |
if (goog.editor.BrowserFeature.USES_KEYDOWN) { |
| 434 |
editableField.handleKeyDown_(e); |
| 435 |
} else { |
| 436 |
editableField.handleKeyPress_(e); |
| 437 |
} |
| 438 |
|
| 439 |
mockPlugin1.$verify(); |
| 440 |
mockPlugin2.$verify(); |
| 441 |
} |
| 442 |
|
| 443 |
|
| 444 |
/** |
| 445 |
* Tests to make sure the cut and paste events are not dispatched immediately. |
| 446 |
*/ |
| 447 |
function testHandleCutAndPasteEvents() { |
| 448 |
if (goog.editor.BrowserFeature.USE_MUTATION_EVENTS) { |
| 449 |
// Cut and paste events do not raise events at all in Mozilla. |
| 450 |
return; |
| 451 |
} |
| 452 |
var editableField = new FieldConstructor('testField'); |
| 453 |
var clock = new goog.testing.MockClock(true); |
| 454 |
var delayedChanges = goog.testing.recordFunction(); |
| 455 |
goog.events.listen( |
| 456 |
editableField, goog.editor.Field.EventType.DELAYEDCHANGE, delayedChanges); |
| 457 |
|
| 458 |
editableField.makeEditable(); |
| 459 |
|
| 460 |
goog.testing.events.fireBrowserEvent( |
| 461 |
new goog.testing.events.Event('cut', editableField.getElement())); |
| 462 |
assertEquals( |
| 463 |
'Cut event should be on a timer', 0, delayedChanges.getCallCount()); |
| 464 |
clock.tick(1000); |
| 465 |
assertEquals( |
| 466 |
'delayed change event should fire within 1s after cut', 1, |
| 467 |
delayedChanges.getCallCount()); |
| 468 |
|
| 469 |
goog.testing.events.fireBrowserEvent( |
| 470 |
new goog.testing.events.Event('paste', editableField.getElement())); |
| 471 |
assertEquals( |
| 472 |
'Paste event should be on a timer', 1, delayedChanges.getCallCount()); |
| 473 |
clock.tick(1000); |
| 474 |
assertEquals( |
| 475 |
'delayed change event should fire within 1s after paste', 2, |
| 476 |
delayedChanges.getCallCount()); |
| 477 |
|
| 478 |
clock.dispose(); |
| 479 |
editableField.dispose(); |
| 480 |
} |
| 481 |
|
| 482 |
|
| 483 |
/** |
| 484 |
* If the first plugin does not handle the key event, the next plugin gets |
| 485 |
* a chance to handle it. |
| 486 |
*/ |
| 487 |
function testNotHandledKeyEvent() { |
| 488 |
var editableField = new FieldConstructor('testField'); |
| 489 |
var plugin = new TestPlugin(); |
| 490 |
var e = getBrowserEvent(); |
| 491 |
|
| 492 |
var mockPlugin1 = new goog.testing.LooseMock(plugin); |
| 493 |
mockPlugin1.getTrogClassId().$returns('mockPlugin1'); |
| 494 |
mockPlugin1.registerFieldObject(editableField); |
| 495 |
mockPlugin1.isEnabled(editableField).$anyTimes().$returns(true); |
| 496 |
if (goog.editor.BrowserFeature.USES_KEYDOWN) { |
| 497 |
mockPlugin1.handleKeyDown(e).$returns(false); |
| 498 |
} else { |
| 499 |
mockPlugin1.handleKeyPress(e).$returns(false); |
| 500 |
} |
| 501 |
mockPlugin1.handleKeyUp(e).$returns(false); |
| 502 |
mockPlugin1.$replay(); |
| 503 |
|
| 504 |
var mockPlugin2 = new goog.testing.LooseMock(plugin); |
| 505 |
mockPlugin2.getTrogClassId().$returns('mockPlugin2'); |
| 506 |
mockPlugin2.registerFieldObject(editableField); |
| 507 |
mockPlugin2.isEnabled(editableField).$anyTimes().$returns(true); |
| 508 |
if (goog.editor.BrowserFeature.USES_KEYDOWN) { |
| 509 |
mockPlugin2.handleKeyDown(e).$returns(true); |
| 510 |
} else { |
| 511 |
mockPlugin2.handleKeyPress(e).$returns(true); |
| 512 |
} |
| 513 |
mockPlugin2.handleKeyUp(e).$returns(true); |
| 514 |
mockPlugin2.$replay(); |
| 515 |
|
| 516 |
editableField.registerPlugin(mockPlugin1); |
| 517 |
editableField.registerPlugin(mockPlugin2); |
| 518 |
|
| 519 |
editableField.handleKeyUp_(e); |
| 520 |
if (goog.editor.BrowserFeature.USES_KEYDOWN) { |
| 521 |
editableField.handleKeyDown_(e); |
| 522 |
} else { |
| 523 |
editableField.handleKeyPress_(e); |
| 524 |
} |
| 525 |
|
| 526 |
mockPlugin1.$verify(); |
| 527 |
mockPlugin2.$verify(); |
| 528 |
} |
| 529 |
|
| 530 |
|
| 531 |
/** |
| 532 |
* Make sure that handleKeyboardShortcut is called if other key handlers |
| 533 |
* return false. |
| 534 |
*/ |
| 535 |
function testKeyboardShortcutCalled() { |
| 536 |
var editableField = new FieldConstructor('testField'); |
| 537 |
var plugin = new TestPlugin(); |
| 538 |
var e = getBrowserEvent(); |
| 539 |
|
| 540 |
var mockPlugin = new goog.testing.LooseMock(plugin); |
| 541 |
mockPlugin.getTrogClassId().$returns('mockPlugin'); |
| 542 |
mockPlugin.registerFieldObject(editableField); |
| 543 |
mockPlugin.isEnabled(editableField).$anyTimes().$returns(true); |
| 544 |
if (goog.editor.BrowserFeature.USES_KEYDOWN) { |
| 545 |
mockPlugin.handleKeyDown(e).$returns(false); |
| 546 |
} else { |
| 547 |
mockPlugin.handleKeyPress(e).$returns(false); |
| 548 |
} |
| 549 |
mockPlugin.handleKeyboardShortcut(e, STRING_KEY, true).$returns(false); |
| 550 |
mockPlugin.$replay(); |
| 551 |
|
| 552 |
editableField.registerPlugin(mockPlugin); |
| 553 |
|
| 554 |
if (goog.editor.BrowserFeature.USES_KEYDOWN) { |
| 555 |
editableField.handleKeyDown_(e); |
| 556 |
} else { |
| 557 |
editableField.handleKeyPress_(e); |
| 558 |
} |
| 559 |
|
| 560 |
mockPlugin.$verify(); |
| 561 |
} |
| 562 |
|
| 563 |
|
| 564 |
/** |
| 565 |
* Make sure that handleKeyboardShortcut is not called if other key handlers |
| 566 |
* return true. |
| 567 |
*/ |
| 568 |
function testKeyboardShortcutNotCalled() { |
| 569 |
var editableField = new FieldConstructor('testField'); |
| 570 |
var plugin = new TestPlugin(); |
| 571 |
var e = getBrowserEvent(); |
| 572 |
|
| 573 |
var mockPlugin = new goog.testing.LooseMock(plugin); |
| 574 |
mockPlugin.getTrogClassId().$returns('mockPlugin'); |
| 575 |
mockPlugin.registerFieldObject(editableField); |
| 576 |
mockPlugin.isEnabled(editableField).$anyTimes().$returns(true); |
| 577 |
if (goog.editor.BrowserFeature.USES_KEYDOWN) { |
| 578 |
mockPlugin.handleKeyDown(e).$returns(true); |
| 579 |
} else { |
| 580 |
mockPlugin.handleKeyPress(e).$returns(true); |
| 581 |
} |
| 582 |
mockPlugin.$replay(); |
| 583 |
|
| 584 |
editableField.registerPlugin(mockPlugin); |
| 585 |
|
| 586 |
if (goog.editor.BrowserFeature.USES_KEYDOWN) { |
| 587 |
editableField.handleKeyDown_(e); |
| 588 |
} else { |
| 589 |
editableField.handleKeyPress_(e); |
| 590 |
} |
| 591 |
|
| 592 |
mockPlugin.$verify(); |
| 593 |
} |
| 594 |
|
| 595 |
|
| 596 |
/** |
| 597 |
* Make sure that handleKeyboardShortcut is not called if alt is pressed. |
| 598 |
* @bug 1363959 |
| 599 |
*/ |
| 600 |
function testKeyHandlingAlt() { |
| 601 |
var editableField = new FieldConstructor('testField'); |
| 602 |
var plugin = new TestPlugin(); |
| 603 |
var e = getBrowserEvent(); |
| 604 |
e.altKey = true; |
| 605 |
|
| 606 |
var mockPlugin = new goog.testing.LooseMock(plugin); |
| 607 |
mockPlugin.getTrogClassId().$returns('mockPlugin'); |
| 608 |
mockPlugin.registerFieldObject(editableField); |
| 609 |
mockPlugin.isEnabled(editableField).$anyTimes().$returns(true); |
| 610 |
if (goog.editor.BrowserFeature.USES_KEYDOWN) { |
| 611 |
mockPlugin.handleKeyDown(e).$returns(false); |
| 612 |
} else { |
| 613 |
mockPlugin.handleKeyPress(e).$returns(false); |
| 614 |
} |
| 615 |
mockPlugin.$replay(); |
| 616 |
|
| 617 |
editableField.registerPlugin(mockPlugin); |
| 618 |
|
| 619 |
if (goog.editor.BrowserFeature.USES_KEYDOWN) { |
| 620 |
editableField.handleKeyDown_(e); |
| 621 |
} else { |
| 622 |
editableField.handleKeyPress_(e); |
| 623 |
} |
| 624 |
|
| 625 |
mockPlugin.$verify(); |
| 626 |
} |
| 627 |
|
| 628 |
|
| 629 |
/** |
| 630 |
* Test that if a plugin has an execCommand function, it gets called |
| 631 |
* but only for supported commands. |
| 632 |
*/ |
| 633 |
function testPluginExecCommand() { |
| 634 |
var plugin = new TestPlugin(); |
| 635 |
var passedCommand, passedArg; |
| 636 |
plugin.execCommand = function(command, arg) { |
| 637 |
passedCommand = command; |
| 638 |
passedArg = arg; |
| 639 |
}; |
| 640 |
|
| 641 |
var editableField = new FieldConstructor('testField'); |
| 642 |
editableField.registerPlugin(plugin); |
| 643 |
plugin.enable(editableField); |
| 644 |
plugin.isSupportedCommand = goog.functions.constant(true); |
| 645 |
|
| 646 |
editableField.execCommand('+indent', true); |
| 647 |
// Verify that the plugin's execCommand was called with the correct |
| 648 |
// args. |
| 649 |
assertEquals('+indent', passedCommand); |
| 650 |
assertTrue(passedArg); |
| 651 |
|
| 652 |
passedCommand = null; |
| 653 |
passedArg = null; |
| 654 |
plugin.isSupportedCommand = goog.functions.constant(false); |
| 655 |
|
| 656 |
editableField.execCommand('+outdent', false); |
| 657 |
// Verify that a plugin's execCommand is not called if it isn't a supported |
| 658 |
// command. |
| 659 |
assertNull(passedCommand); |
| 660 |
assertNull(passedArg); |
| 661 |
|
| 662 |
editableField.dispose(); |
| 663 |
plugin.dispose(); |
| 664 |
} |
| 665 |
|
| 666 |
|
| 667 |
/** |
| 668 |
* Test that if one plugin supports execCommand, no other plugins |
| 669 |
* get a chance to handle the execComand. |
| 670 |
*/ |
| 671 |
function testSupportedExecCommand() { |
| 672 |
var editableField = new FieldConstructor('testField'); |
| 673 |
var plugin = new TestPlugin(); |
| 674 |
|
| 675 |
var mockPlugin1 = new goog.testing.LooseMock(plugin); |
| 676 |
mockPlugin1.getTrogClassId().$returns('mockPlugin1'); |
| 677 |
mockPlugin1.registerFieldObject(editableField); |
| 678 |
mockPlugin1.isEnabled(editableField).$anyTimes().$returns(true); |
| 679 |
mockPlugin1.isSupportedCommand('+indent').$returns(true); |
| 680 |
mockPlugin1.execCommandInternal('+indent').$returns(true); |
| 681 |
mockPlugin1.execCommand('+indent').$does(function() { |
| 682 |
mockPlugin1.execCommandInternal('+indent'); |
| 683 |
}); |
| 684 |
mockPlugin1.$replay(); |
| 685 |
|
| 686 |
var mockPlugin2 = new goog.testing.LooseMock(plugin); |
| 687 |
mockPlugin2.getTrogClassId().$returns('mockPlugin2'); |
| 688 |
mockPlugin2.registerFieldObject(editableField); |
| 689 |
mockPlugin2.isEnabled(editableField).$anyTimes().$returns(true); |
| 690 |
mockPlugin2.$replay(); |
| 691 |
|
| 692 |
editableField.registerPlugin(mockPlugin1); |
| 693 |
editableField.registerPlugin(mockPlugin2); |
| 694 |
|
| 695 |
editableField.execCommand('+indent'); |
| 696 |
|
| 697 |
mockPlugin1.$verify(); |
| 698 |
mockPlugin2.$verify(); |
| 699 |
} |
| 700 |
|
| 701 |
|
| 702 |
/** |
| 703 |
* Test that if the first plugin does not support execCommand, the other |
| 704 |
* plugins get a chance to handle the execCommand. |
| 705 |
*/ |
| 706 |
function testNotSupportedExecCommand() { |
| 707 |
var editableField = new FieldConstructor('testField'); |
| 708 |
var plugin = new TestPlugin(); |
| 709 |
|
| 710 |
var mockPlugin1 = new goog.testing.LooseMock(plugin); |
| 711 |
mockPlugin1.getTrogClassId().$returns('mockPlugin1'); |
| 712 |
mockPlugin1.registerFieldObject(editableField); |
| 713 |
mockPlugin1.isEnabled(editableField).$anyTimes().$returns(true); |
| 714 |
mockPlugin1.isSupportedCommand('+indent').$returns(false); |
| 715 |
mockPlugin1.$replay(); |
| 716 |
|
| 717 |
var mockPlugin2 = new goog.testing.LooseMock(plugin); |
| 718 |
mockPlugin2.getTrogClassId().$returns('mockPlugin2'); |
| 719 |
mockPlugin2.registerFieldObject(editableField); |
| 720 |
mockPlugin2.isEnabled(editableField).$anyTimes().$returns(true); |
| 721 |
mockPlugin2.isSupportedCommand('+indent').$returns(true); |
| 722 |
mockPlugin2.execCommandInternal('+indent').$returns(true); |
| 723 |
mockPlugin2.execCommand('+indent').$does(function() { |
| 724 |
mockPlugin2.execCommandInternal('+indent'); |
| 725 |
}); |
| 726 |
mockPlugin2.$replay(); |
| 727 |
|
| 728 |
editableField.registerPlugin(mockPlugin1); |
| 729 |
editableField.registerPlugin(mockPlugin2); |
| 730 |
|
| 731 |
editableField.execCommand('+indent'); |
| 732 |
|
| 733 |
mockPlugin1.$verify(); |
| 734 |
mockPlugin2.$verify(); |
| 735 |
} |
| 736 |
|
| 737 |
|
| 738 |
/** |
| 739 |
* Tests that if a plugin supports a command that its queryCommandValue |
| 740 |
* gets called and no further plugins can handle the queryCommandValue. |
| 741 |
*/ |
| 742 |
function testSupportedQueryCommand() { |
| 743 |
var editableField = new FieldConstructor('testField'); |
| 744 |
var plugin = new TestPlugin(); |
| 745 |
|
| 746 |
var mockPlugin1 = new goog.testing.LooseMock(plugin); |
| 747 |
mockPlugin1.getTrogClassId().$returns('mockPlugin1'); |
| 748 |
mockPlugin1.registerFieldObject(editableField); |
| 749 |
mockPlugin1.isEnabled(editableField).$anyTimes().$returns(true); |
| 750 |
mockPlugin1.isSupportedCommand('+indent').$returns(true); |
| 751 |
mockPlugin1.queryCommandValue('+indent').$returns(true); |
| 752 |
mockPlugin1.activeOnUneditableFields().$returns(true); |
| 753 |
mockPlugin1.$replay(); |
| 754 |
|
| 755 |
var mockPlugin2 = new goog.testing.LooseMock(plugin); |
| 756 |
mockPlugin2.getTrogClassId().$returns('mockPlugin2'); |
| 757 |
mockPlugin2.registerFieldObject(editableField); |
| 758 |
mockPlugin2.isEnabled(editableField).$anyTimes().$returns(true); |
| 759 |
mockPlugin2.$replay(); |
| 760 |
|
| 761 |
editableField.registerPlugin(mockPlugin1); |
| 762 |
editableField.registerPlugin(mockPlugin2); |
| 763 |
|
| 764 |
editableField.queryCommandValue('+indent'); |
| 765 |
|
| 766 |
mockPlugin1.$verify(); |
| 767 |
mockPlugin2.$verify(); |
| 768 |
} |
| 769 |
|
| 770 |
|
| 771 |
/** |
| 772 |
* Tests that if the first plugin does not support a command that its |
| 773 |
* queryCommandValue do not get called and the next plugin can handle the |
| 774 |
* queryCommandValue. |
| 775 |
*/ |
| 776 |
function testNotSupportedQueryCommand() { |
| 777 |
var editableField = new FieldConstructor('testField'); |
| 778 |
var plugin = new TestPlugin(); |
| 779 |
|
| 780 |
var mockPlugin1 = new goog.testing.LooseMock(plugin); |
| 781 |
mockPlugin1.getTrogClassId().$returns('mockPlugin1'); |
| 782 |
mockPlugin1.registerFieldObject(editableField); |
| 783 |
mockPlugin1.isEnabled(editableField).$anyTimes().$returns(true); |
| 784 |
mockPlugin1.isSupportedCommand('+indent').$returns(false); |
| 785 |
mockPlugin1.$replay(); |
| 786 |
|
| 787 |
var mockPlugin2 = new goog.testing.LooseMock(plugin); |
| 788 |
mockPlugin2.getTrogClassId().$returns('mockPlugin2'); |
| 789 |
mockPlugin2.registerFieldObject(editableField); |
| 790 |
mockPlugin2.isEnabled(editableField).$anyTimes().$returns(true); |
| 791 |
mockPlugin2.isSupportedCommand('+indent').$returns(true); |
| 792 |
mockPlugin2.queryCommandValue('+indent').$returns(true); |
| 793 |
mockPlugin2.activeOnUneditableFields().$returns(true); |
| 794 |
mockPlugin2.$replay(); |
| 795 |
|
| 796 |
editableField.registerPlugin(mockPlugin1); |
| 797 |
editableField.registerPlugin(mockPlugin2); |
| 798 |
|
| 799 |
editableField.queryCommandValue('+indent'); |
| 800 |
|
| 801 |
mockPlugin1.$verify(); |
| 802 |
mockPlugin2.$verify(); |
| 803 |
} |
| 804 |
|
| 805 |
|
| 806 |
/** |
| 807 |
* Tests that if a plugin handles selectionChange that it gets called and |
| 808 |
* no further plugins can handle the selectionChange. |
| 809 |
*/ |
| 810 |
function testHandledSelectionChange() { |
| 811 |
var editableField = new FieldConstructor('testField'); |
| 812 |
var plugin = new TestPlugin(); |
| 813 |
var e = getBrowserEvent(); |
| 814 |
|
| 815 |
var mockPlugin1 = new goog.testing.LooseMock(plugin); |
| 816 |
mockPlugin1.getTrogClassId().$returns('mockPlugin1'); |
| 817 |
mockPlugin1.registerFieldObject(editableField); |
| 818 |
mockPlugin1.isEnabled(editableField).$anyTimes().$returns(true); |
| 819 |
mockPlugin1.handleSelectionChange(e, undefined).$returns(true); |
| 820 |
mockPlugin1.$replay(); |
| 821 |
|
| 822 |
var mockPlugin2 = new goog.testing.LooseMock(plugin); |
| 823 |
mockPlugin2.getTrogClassId().$returns('mockPlugin2'); |
| 824 |
mockPlugin2.registerFieldObject(editableField); |
| 825 |
mockPlugin2.isEnabled(editableField).$anyTimes().$returns(true); |
| 826 |
mockPlugin2.$replay(); |
| 827 |
|
| 828 |
editableField.registerPlugin(mockPlugin1); |
| 829 |
editableField.registerPlugin(mockPlugin2); |
| 830 |
|
| 831 |
editableField.dispatchSelectionChangeEvent(e); |
| 832 |
|
| 833 |
mockPlugin1.$verify(); |
| 834 |
mockPlugin2.$verify(); |
| 835 |
} |
| 836 |
|
| 837 |
|
| 838 |
/** |
| 839 |
* Tests that if the first plugin does not handle selectionChange that |
| 840 |
* the next plugin gets a chance to handle it. |
| 841 |
*/ |
| 842 |
function testNotHandledSelectionChange() { |
| 843 |
var editableField = new FieldConstructor('testField'); |
| 844 |
var plugin = new TestPlugin(); |
| 845 |
var e = getBrowserEvent(); |
| 846 |
|
| 847 |
var mockPlugin1 = new goog.testing.LooseMock(plugin); |
| 848 |
mockPlugin1.getTrogClassId().$returns('mockPlugin1'); |
| 849 |
mockPlugin1.registerFieldObject(editableField); |
| 850 |
mockPlugin1.isEnabled(editableField).$anyTimes().$returns(true); |
| 851 |
mockPlugin1.handleSelectionChange(e, undefined).$returns(false); |
| 852 |
mockPlugin1.$replay(); |
| 853 |
|
| 854 |
var mockPlugin2 = new goog.testing.LooseMock(plugin); |
| 855 |
mockPlugin2.getTrogClassId().$returns('mockPlugin2'); |
| 856 |
mockPlugin2.registerFieldObject(editableField); |
| 857 |
mockPlugin2.isEnabled(editableField).$anyTimes().$returns(true); |
| 858 |
mockPlugin2.handleSelectionChange(e, undefined).$returns(true); |
| 859 |
mockPlugin2.$replay(); |
| 860 |
|
| 861 |
editableField.registerPlugin(mockPlugin1); |
| 862 |
editableField.registerPlugin(mockPlugin2); |
| 863 |
|
| 864 |
editableField.dispatchSelectionChangeEvent(e); |
| 865 |
|
| 866 |
mockPlugin1.$verify(); |
| 867 |
mockPlugin2.$verify(); |
| 868 |
} |
| 869 |
|
| 870 |
|
| 871 |
// Tests for goog.editor.Field internals. |
| 872 |
|
| 873 |
function testSelectionChange() { |
| 874 |
var editableField = new FieldConstructor('testField', document); |
| 875 |
var clock = new goog.testing.MockClock(true); |
| 876 |
var beforeSelectionChanges = goog.testing.recordFunction(); |
| 877 |
goog.events.listen( |
| 878 |
editableField, goog.editor.Field.EventType.BEFORESELECTIONCHANGE, |
| 879 |
beforeSelectionChanges); |
| 880 |
var selectionChanges = goog.testing.recordFunction(); |
| 881 |
goog.events.listen( |
| 882 |
editableField, goog.editor.Field.EventType.SELECTIONCHANGE, |
| 883 |
selectionChanges); |
| 884 |
|
| 885 |
editableField.makeEditable(); |
| 886 |
|
| 887 |
// Emulate pressing left arrow key, this should result in a |
| 888 |
// BEFORESELECTIONCHANGE event immediately, and a SELECTIONCHANGE event after |
| 889 |
// a short timeout. |
| 890 |
editableField.handleKeyUp_({keyCode: goog.events.KeyCodes.LEFT}); |
| 891 |
assertEquals( |
| 892 |
'Before selection change should fire immediately', 1, |
| 893 |
beforeSelectionChanges.getCallCount()); |
| 894 |
assertEquals( |
| 895 |
'Selection change should be on a timer', 0, |
| 896 |
selectionChanges.getCallCount()); |
| 897 |
clock.tick(1000); |
| 898 |
assertEquals( |
| 899 |
'Selection change should fire within 1s', 1, |
| 900 |
selectionChanges.getCallCount()); |
| 901 |
|
| 902 |
// Programically place cursor at start. SELECTIONCHANGE event should be fired. |
| 903 |
editableField.placeCursorAtStart(); |
| 904 |
assertEquals( |
| 905 |
'Selection change should fire', 2, selectionChanges.getCallCount()); |
| 906 |
|
| 907 |
clock.dispose(); |
| 908 |
editableField.dispose(); |
| 909 |
} |
| 910 |
|
| 911 |
|
| 912 |
function testSelectionChangeOnMouseUp() { |
| 913 |
var fakeEvent = |
| 914 |
new goog.events.BrowserEvent({type: 'mouseup', target: 'fakeTarget'}); |
| 915 |
var editableField = new FieldConstructor('testField', document); |
| 916 |
var clock = new goog.testing.MockClock(true); |
| 917 |
var beforeSelectionChanges = goog.testing.recordFunction(); |
| 918 |
goog.events.listen( |
| 919 |
editableField, goog.editor.Field.EventType.BEFORESELECTIONCHANGE, |
| 920 |
beforeSelectionChanges); |
| 921 |
var selectionChanges = goog.testing.recordFunction(); |
| 922 |
goog.events.listen( |
| 923 |
editableField, goog.editor.Field.EventType.SELECTIONCHANGE, |
| 924 |
selectionChanges); |
| 925 |
|
| 926 |
var plugin = new TestPlugin(); |
| 927 |
plugin.handleSelectionChange = goog.testing.recordFunction(); |
| 928 |
editableField.registerPlugin(plugin); |
| 929 |
|
| 930 |
editableField.makeEditable(); |
| 931 |
|
| 932 |
// Emulate a mouseup event, this should result in immediate |
| 933 |
// BEFORESELECTIONCHANGE and SELECTIONCHANGE, plus a second SELECTIONCHANGE in |
| 934 |
// IE after a short timeout. |
| 935 |
editableField.handleMouseUp_(fakeEvent); |
| 936 |
assertEquals( |
| 937 |
'Before selection change should fire immediately', 1, |
| 938 |
beforeSelectionChanges.getCallCount()); |
| 939 |
assertEquals( |
| 940 |
'Selection change should fire immediately', 1, |
| 941 |
selectionChanges.getCallCount()); |
| 942 |
assertEquals( |
| 943 |
'Plugin should have handled selection change immediately', 1, |
| 944 |
plugin.handleSelectionChange.getCallCount()); |
| 945 |
assertEquals( |
| 946 |
'Plugin should have received original browser event to handle', fakeEvent, |
| 947 |
plugin.handleSelectionChange.getLastCall().getArguments()[0]); |
| 948 |
|
| 949 |
// Pretend another plugin fired a SELECTIONCHANGE in the meantime. |
| 950 |
editableField.dispatchSelectionChangeEvent(); |
| 951 |
assertEquals( |
| 952 |
'Second selection change should fire immediately', 2, |
| 953 |
selectionChanges.getCallCount()); |
| 954 |
assertEquals( |
| 955 |
'Plugin should have handled second selection change immediately', 2, |
| 956 |
plugin.handleSelectionChange.getCallCount()); |
| 957 |
var args = plugin.handleSelectionChange.getLastCall().getArguments(); |
| 958 |
assertTrue( |
| 959 |
'Plugin should not have received data from extra firing', |
| 960 |
args.length == 0 || !args[0] && (args.length == 1 || !args[1])); |
| 961 |
|
| 962 |
// Now check for the extra call in IE. |
| 963 |
clock.tick(1000); |
| 964 |
if (goog.userAgent.IE) { |
| 965 |
assertEquals( |
| 966 |
'Additional selection change should fire within 1s', 3, |
| 967 |
selectionChanges.getCallCount()); |
| 968 |
assertEquals( |
| 969 |
'Plugin should have handled selection change within 1s', 3, |
| 970 |
plugin.handleSelectionChange.getCallCount()); |
| 971 |
assertEquals( |
| 972 |
'Plugin should have received target of original browser event', |
| 973 |
fakeEvent.target, |
| 974 |
plugin.handleSelectionChange.getLastCall().getArguments().pop()); |
| 975 |
} else { |
| 976 |
assertEquals( |
| 977 |
'No additional selection change should fire', 2, |
| 978 |
selectionChanges.getCallCount()); |
| 979 |
assertEquals( |
| 980 |
'Plugin should not have handled selection change again', 2, |
| 981 |
plugin.handleSelectionChange.getCallCount()); |
| 982 |
} |
| 983 |
|
| 984 |
clock.dispose(); |
| 985 |
editableField.dispose(); |
| 986 |
} |
| 987 |
|
| 988 |
|
| 989 |
function testSelectionChangeBeforeUneditable() { |
| 990 |
var editableField = new FieldConstructor('testField', document); |
| 991 |
var clock = new goog.testing.MockClock(true); |
| 992 |
var selectionChanges = goog.testing.recordFunction(); |
| 993 |
goog.events.listen( |
| 994 |
editableField, goog.editor.Field.EventType.SELECTIONCHANGE, |
| 995 |
selectionChanges); |
| 996 |
|
| 997 |
editableField.makeEditable(); |
| 998 |
editableField.handleKeyUp_({keyCode: goog.events.KeyCodes.LEFT}); |
| 999 |
assertEquals( |
| 1000 |
'Selection change should be on a timer', 0, |
| 1001 |
selectionChanges.getCallCount()); |
| 1002 |
editableField.makeUneditable(); |
| 1003 |
assertEquals( |
| 1004 |
'Selection change should fire during make uneditable', 1, |
| 1005 |
selectionChanges.getCallCount()); |
| 1006 |
clock.tick(1000); |
| 1007 |
assertEquals( |
| 1008 |
'No additional selection change should fire', 1, |
| 1009 |
selectionChanges.getCallCount()); |
| 1010 |
|
| 1011 |
clock.dispose(); |
| 1012 |
editableField.dispose(); |
| 1013 |
} |
| 1014 |
|
| 1015 |
|
| 1016 |
function testGetEditableDomHelper() { |
| 1017 |
var editableField = new FieldConstructor('testField', document); |
| 1018 |
assertNull( |
| 1019 |
'Before being made editable, we do not know the dom helper', |
| 1020 |
editableField.getEditableDomHelper()); |
| 1021 |
editableField.makeEditable(); |
| 1022 |
assertNotNull( |
| 1023 |
'After being made editable, we know the dom helper', |
| 1024 |
editableField.getEditableDomHelper()); |
| 1025 |
assertEquals( |
| 1026 |
'Document from domHelper should be the editable elements doc', |
| 1027 |
goog.dom.getOwnerDocument(editableField.getElement()), |
| 1028 |
editableField.getEditableDomHelper().getDocument()); |
| 1029 |
editableField.dispose(); |
| 1030 |
} |
| 1031 |
|
| 1032 |
|
| 1033 |
function testQueryCommandValue() { |
| 1034 |
var editableField = new FieldConstructor('testField', document); |
| 1035 |
assertFalse(editableField.queryCommandValue('boo')); |
| 1036 |
assertObjectEquals( |
| 1037 |
{'boo': false, 'aieee': false}, |
| 1038 |
editableField.queryCommandValue(['boo', 'aieee'])); |
| 1039 |
|
| 1040 |
editableField.makeEditable(); |
| 1041 |
assertFalse(editableField.queryCommandValue('boo')); |
| 1042 |
|
| 1043 |
focusFieldSync(editableField); |
| 1044 |
assertNull(editableField.queryCommandValue('boo')); |
| 1045 |
assertObjectEquals( |
| 1046 |
{'boo': null, 'aieee': null}, |
| 1047 |
editableField.queryCommandValue(['boo', 'aieee'])); |
| 1048 |
editableField.dispose(); |
| 1049 |
} |
| 1050 |
|
| 1051 |
function focusFieldSync(field) { |
| 1052 |
field.focus(); |
| 1053 |
|
| 1054 |
// IE fires focus events async, so create a fake focus event |
| 1055 |
// synchronously. |
| 1056 |
goog.testing.events.fireFocusEvent(field.getElement()); |
| 1057 |
} |
| 1058 |
|
| 1059 |
|
| 1060 |
function testSetSafeHtml() { |
| 1061 |
var editableField = new FieldConstructor('testField', document); |
| 1062 |
var clock = new goog.testing.MockClock(true); |
| 1063 |
|
| 1064 |
try { |
| 1065 |
var delayedChangeCalled = false; |
| 1066 |
goog.events.listen( |
| 1067 |
editableField, goog.editor.Field.EventType.DELAYEDCHANGE, |
| 1068 |
function() { delayedChangeCalled = true; }); |
| 1069 |
|
| 1070 |
editableField.makeEditable(); |
| 1071 |
clock.tick(1000); |
| 1072 |
assertFalse( |
| 1073 |
'Make editable must not fire delayed change.', delayedChangeCalled); |
| 1074 |
|
| 1075 |
editableField.setSafeHtml( |
| 1076 |
false, goog.html.SafeHtml.htmlEscape('bar'), |
| 1077 |
true /* Don't fire delayed change */); |
| 1078 |
goog.testing.dom.assertHtmlContentsMatch('bar', editableField.getElement()); |
| 1079 |
clock.tick(1000); |
| 1080 |
assertFalse( |
| 1081 |
'setSafeHtml must not fire delayed change if so configured.', |
| 1082 |
delayedChangeCalled); |
| 1083 |
|
| 1084 |
editableField.setSafeHtml( |
| 1085 |
false, goog.html.SafeHtml.htmlEscape('foo'), |
| 1086 |
false /* Fire delayed change */); |
| 1087 |
goog.testing.dom.assertHtmlContentsMatch('foo', editableField.getElement()); |
| 1088 |
clock.tick(1000); |
| 1089 |
assertTrue( |
| 1090 |
'setSafeHtml must fire delayed change by default', delayedChangeCalled); |
| 1091 |
} finally { |
| 1092 |
clock.dispose(); |
| 1093 |
editableField.dispose(); |
| 1094 |
} |
| 1095 |
} |
| 1096 |
|
| 1097 |
|
| 1098 |
/** |
| 1099 |
* Helper to test that the cursor is placed at the beginning of the editable |
| 1100 |
* field's contents. |
| 1101 |
* @param {string=} opt_html Html to replace the test file default field |
| 1102 |
* contents with. |
| 1103 |
* @param {string=} opt_parentId Id of the parent of the node where the cursor |
| 1104 |
* is expected to be placed. If omitted, will expect cursor to be placed in |
| 1105 |
* the first child of the field element (or, if the field has no content, in |
| 1106 |
* the field element itself). |
| 1107 |
*/ |
| 1108 |
function doTestPlaceCursorAtStart(opt_html, opt_parentId) { |
| 1109 |
var editableField = new FieldConstructor('testField', document); |
| 1110 |
editableField.makeEditable(); |
| 1111 |
// Initially place selection not at the start of the editable field. |
| 1112 |
var textNode = editableField.getElement().firstChild; |
| 1113 |
goog.dom.Range.createFromNodes(textNode, 1, textNode, 2).select(); |
| 1114 |
if (opt_html != null) { |
| 1115 |
editableField.getElement().innerHTML = opt_html; |
| 1116 |
} |
| 1117 |
|
| 1118 |
editableField.placeCursorAtStart(); |
| 1119 |
var range = editableField.getRange(); |
| 1120 |
assertNotNull( |
| 1121 |
'Placing the cursor should result in a range object being available', |
| 1122 |
range); |
| 1123 |
assertTrue('The range should be collapsed', range.isCollapsed()); |
| 1124 |
textNode = editableField.getElement().firstChild; |
| 1125 |
|
| 1126 |
// We check whether getAttribute exist because textNode may be an actual |
| 1127 |
// TextNode, which does not have getAttribute. |
| 1128 |
if (textNode && textNode.getAttribute && |
| 1129 |
textNode.getAttribute('_moz_editor_bogus_node')) { |
| 1130 |
// At least in FF >= 6, assigning '' to innerHTML of a contentEditable |
| 1131 |
// element will results in textNode being modified into: |
| 1132 |
// <br _moz_editor_bogus_node="TRUE" _moz_dirty=""> instead of nulling |
| 1133 |
// it. So we should null it ourself. |
| 1134 |
textNode = null; |
| 1135 |
} |
| 1136 |
|
| 1137 |
var startNode = opt_parentId ? |
| 1138 |
editableField.getEditableDomHelper().getElement(opt_parentId).firstChild : |
| 1139 |
textNode ? textNode : editableField.getElement(); |
| 1140 |
if (goog.userAgent.WEBKIT && !goog.userAgent.isVersionOrHigher('528')) { |
| 1141 |
// Safari 3 seems to normalize the selection to the shallowest endpoint (in |
| 1142 |
// this case the editable element) in all cases tested below. This is OK |
| 1143 |
// because when you start typing it magically inserts the text at the |
| 1144 |
// deepest endpoint, and even behaves as desired in the case tested by |
| 1145 |
// testPlaceCursorAtStartNonImportantTextNode. |
| 1146 |
startNode = editableField.getElement(); |
| 1147 |
} |
| 1148 |
assertEquals( |
| 1149 |
'The range should start at the specified expected node', startNode, |
| 1150 |
range.getStartNode()); |
| 1151 |
assertEquals( |
| 1152 |
'The range should start at the beginning of the node', 0, |
| 1153 |
range.getStartOffset()); |
| 1154 |
} |
| 1155 |
|
| 1156 |
|
| 1157 |
/** |
| 1158 |
* Verify that restoreSavedRange() restores the range and sets the focus. |
| 1159 |
*/ |
| 1160 |
function testRestoreSavedRange() { |
| 1161 |
var editableField = new FieldConstructor('testField', document); |
| 1162 |
editableField.makeEditable(); |
| 1163 |
|
| 1164 |
|
| 1165 |
// Create another node to take the focus later. |
| 1166 |
var doc = goog.dom.getOwnerDocument(editableField.getElement()); |
| 1167 |
var dom = goog.dom.getDomHelper(editableField.getElement()); |
| 1168 |
var otherElem = dom.createElement(goog.dom.TagName.DIV); |
| 1169 |
otherElem.tabIndex = 1; // Make it focusable. |
| 1170 |
editableField.getElement().parentNode.appendChild(otherElem); |
| 1171 |
|
| 1172 |
// Initially place selection not at the start of the editable field. |
| 1173 |
var textNode = editableField.getElement().firstChild; |
| 1174 |
var range = goog.dom.Range.createFromNodes(textNode, 1, textNode, 2); |
| 1175 |
range.select(); |
| 1176 |
var savedRange = goog.editor.range.saveUsingNormalizedCarets(range); |
| 1177 |
|
| 1178 |
// Change range to be a simple cursor at start, and move focus away. |
| 1179 |
editableField.placeCursorAtStart(); |
| 1180 |
otherElem.focus(); |
| 1181 |
|
| 1182 |
editableField.restoreSavedRange(savedRange); |
| 1183 |
|
| 1184 |
// Verify that we have focus and the range is restored. |
| 1185 |
assertEquals( |
| 1186 |
'Field should be focused', editableField.getElement(), |
| 1187 |
goog.dom.getActiveElement(doc)); |
| 1188 |
var newRange = editableField.getRange(); |
| 1189 |
assertEquals('Range startNode', textNode, newRange.getStartNode()); |
| 1190 |
assertEquals('Range startOffset', 1, newRange.getStartOffset()); |
| 1191 |
assertEquals('Range endNode', textNode, newRange.getEndNode()); |
| 1192 |
assertEquals('Range endOffset', 2, newRange.getEndOffset()); |
| 1193 |
} |
| 1194 |
|
| 1195 |
|
| 1196 |
function testPlaceCursorAtStart() { |
| 1197 |
doTestPlaceCursorAtStart(); |
| 1198 |
} |
| 1199 |
|
| 1200 |
|
| 1201 |
function testPlaceCursorAtStartEmptyField() { |
| 1202 |
doTestPlaceCursorAtStart(''); |
| 1203 |
} |
| 1204 |
|
| 1205 |
|
| 1206 |
function testPlaceCursorAtStartNonImportantTextNode() { |
| 1207 |
doTestPlaceCursorAtStart( |
| 1208 |
'\n<span id="important">important text</span>', 'important'); |
| 1209 |
} |
| 1210 |
|
| 1211 |
|
| 1212 |
/** |
| 1213 |
* Helper to test that the cursor is placed at the beginning of the editable |
| 1214 |
* field's contents. |
| 1215 |
* @param {string=} opt_html Html to replace the test file default field |
| 1216 |
* contents with. |
| 1217 |
* @param {string=} opt_parentId Id of the parent of the node where the cursor |
| 1218 |
* is expected to be placed. If omitted, will expect cursor to be placed in |
| 1219 |
* the first child of the field element (or, if the field has no content, in |
| 1220 |
* the field element itself). |
| 1221 |
* @param {number=} opt_offset The offset to expect for the end position. |
| 1222 |
*/ |
| 1223 |
function doTestPlaceCursorAtEnd(opt_html, opt_parentId, opt_offset) { |
| 1224 |
var editableField = new FieldConstructor('testField', document); |
| 1225 |
editableField.makeEditable(); |
| 1226 |
|
| 1227 |
// Initially place selection not at the end of the editable field. |
| 1228 |
var textNode = editableField.getElement().firstChild; |
| 1229 |
goog.dom.Range.createFromNodes(textNode, 0, textNode, 1).select(); |
| 1230 |
if (opt_html != null) { |
| 1231 |
editableField.getElement().innerHTML = opt_html; |
| 1232 |
} |
| 1233 |
|
| 1234 |
editableField.placeCursorAtEnd(); |
| 1235 |
var range = editableField.getRange(); |
| 1236 |
assertNotNull( |
| 1237 |
'Placing the cursor should result in a range object being available', |
| 1238 |
range); |
| 1239 |
assertTrue('The range should be collapsed', range.isCollapsed()); |
| 1240 |
textNode = editableField.getElement().firstChild; |
| 1241 |
|
| 1242 |
// We check whether getAttribute exist because textNode may be an actual |
| 1243 |
// TextNode, which does not have getAttribute. |
| 1244 |
|
| 1245 |
var hasBogusNode = textNode && textNode.getAttribute && |
| 1246 |
textNode.getAttribute('_moz_editor_bogus_node'); |
| 1247 |
if (hasBogusNode) { |
| 1248 |
// At least in FF >= 6, assigning '' to innerHTML of a contentEditable |
| 1249 |
// element will results in textNode being modified into: |
| 1250 |
// <br _moz_editor_bogus_node="TRUE" _moz_dirty=""> instead of nulling |
| 1251 |
// it. So we should null it ourself. |
| 1252 |
textNode = null; |
| 1253 |
} |
| 1254 |
|
| 1255 |
var endNode = opt_parentId ? |
| 1256 |
editableField.getEditableDomHelper().getElement(opt_parentId).lastChild : |
| 1257 |
textNode ? textNode : editableField.getElement(); |
| 1258 |
assertEquals( |
| 1259 |
'The range should end at the specified expected node', endNode, |
| 1260 |
range.getEndNode()); |
| 1261 |
var offset = goog.isDefAndNotNull(opt_offset) ? opt_offset : textNode ? |
| 1262 |
endNode.nodeValue.length : |
| 1263 |
endNode.childNodes.length - 1; |
| 1264 |
if (hasBogusNode) { |
| 1265 |
assertEquals( |
| 1266 |
'The range should end at the ending of the bogus node ' + |
| 1267 |
'added by FF', |
| 1268 |
offset + 1, range.getEndOffset()); |
| 1269 |
} else { |
| 1270 |
assertEquals( |
| 1271 |
'The range should end at the ending of the node', offset, |
| 1272 |
range.getEndOffset()); |
| 1273 |
} |
| 1274 |
} |
| 1275 |
|
| 1276 |
|
| 1277 |
function testPlaceCursorAtEnd() { |
| 1278 |
doTestPlaceCursorAtEnd(); |
| 1279 |
} |
| 1280 |
|
| 1281 |
|
| 1282 |
function testPlaceCursorAtEndEmptyField() { |
| 1283 |
doTestPlaceCursorAtEnd('', null, 0); |
| 1284 |
} |
| 1285 |
|
| 1286 |
|
| 1287 |
function testPlaceCursorAtEndNonImportantTextNode() { |
| 1288 |
doTestPlaceCursorAtStart( |
| 1289 |
'\n<span id="important">important text</span>', 'important'); |
| 1290 |
} |
| 1291 |
|
| 1292 |
|
| 1293 |
// Tests related to change/delayed change events. |
| 1294 |
|
| 1295 |
|
| 1296 |
function testClearDelayedChange() { |
| 1297 |
var clock = new goog.testing.MockClock(true); |
| 1298 |
var editableField = new FieldConstructor('testField', document); |
| 1299 |
editableField.makeEditable(); |
| 1300 |
|
| 1301 |
var delayedChangeCalled = false; |
| 1302 |
goog.events.listen( |
| 1303 |
editableField, goog.editor.Field.EventType.DELAYEDCHANGE, |
| 1304 |
function() { delayedChangeCalled = true; }); |
| 1305 |
|
| 1306 |
// Clears delayed change timer. |
| 1307 |
editableField.delayedChangeTimer_.start(); |
| 1308 |
editableField.clearDelayedChange(); |
| 1309 |
assertTrue(delayedChangeCalled); |
| 1310 |
if (editableField.changeTimerGecko_) { |
| 1311 |
assertFalse(editableField.changeTimerGecko_.isActive()); |
| 1312 |
} |
| 1313 |
assertFalse(editableField.delayedChangeTimer_.isActive()); |
| 1314 |
|
| 1315 |
// Clears delayed changes caused by changeTimerGecko_ |
| 1316 |
if (editableField.changeTimerGecko_) { |
| 1317 |
delayedChangeCalled = false; |
| 1318 |
editableField.changeTimerGecko_.start(); |
| 1319 |
editableField.clearDelayedChange(); |
| 1320 |
assertTrue(delayedChangeCalled); |
| 1321 |
if (editableField.changeTimerGecko_) { |
| 1322 |
assertFalse(editableField.changeTimerGecko_.isActive()); |
| 1323 |
} |
| 1324 |
assertFalse(editableField.delayedChangeTimer_.isActive()); |
| 1325 |
} |
| 1326 |
clock.dispose(); |
| 1327 |
} |
| 1328 |
|
| 1329 |
|
| 1330 |
function testHandleChange() { |
| 1331 |
if (goog.editor.BrowserFeature.USE_MUTATION_EVENTS) { |
| 1332 |
var editableField = new FieldConstructor('testField', document); |
| 1333 |
editableField.makeEditable(); |
| 1334 |
|
| 1335 |
editableField.changeTimerGecko_.start(); |
| 1336 |
editableField.handleChange(); |
| 1337 |
assertFalse(editableField.changeTimerGecko_.isActive()); |
| 1338 |
} |
| 1339 |
} |
| 1340 |
|
| 1341 |
|
| 1342 |
function testDispatchDelayedChange() { |
| 1343 |
var editableField = new FieldConstructor('testField', document); |
| 1344 |
editableField.makeEditable(); |
| 1345 |
|
| 1346 |
editableField.delayedChangeTimer_.start(); |
| 1347 |
editableField.dispatchDelayedChange_(); |
| 1348 |
assertFalse(editableField.delayedChangeTimer_.isActive()); |
| 1349 |
} |
| 1350 |
|
| 1351 |
|
| 1352 |
function testHandleWindowLevelMouseUp() { |
| 1353 |
var editableField = new FieldConstructor('testField', document); |
| 1354 |
if (editableField.usesIframe()) { |
| 1355 |
// Only run this test if the editor does not use an iframe. |
| 1356 |
return; |
| 1357 |
} |
| 1358 |
editableField.setUseWindowMouseUp(true); |
| 1359 |
editableField.makeEditable(); |
| 1360 |
var selectionHasFired = false; |
| 1361 |
goog.events.listenOnce( |
| 1362 |
editableField, goog.editor.Field.EventType.SELECTIONCHANGE, |
| 1363 |
function(e) { selectionHasFired = true; }); |
| 1364 |
var editableElement = editableField.getElement(); |
| 1365 |
var otherElement = goog.dom.createDom(goog.dom.TagName.DIV); |
| 1366 |
goog.dom.insertSiblingAfter(otherElement, document.body.lastChild); |
| 1367 |
|
| 1368 |
goog.testing.events.fireMouseDownEvent(editableElement); |
| 1369 |
assertFalse(selectionHasFired); |
| 1370 |
goog.testing.events.fireMouseUpEvent(otherElement); |
| 1371 |
assertTrue(selectionHasFired); |
| 1372 |
} |
| 1373 |
|
| 1374 |
function testNoHandleWindowLevelMouseUp() { |
| 1375 |
var editableField = new FieldConstructor('testField', document); |
| 1376 |
editableField.setUseWindowMouseUp(false); |
| 1377 |
editableField.makeEditable(); |
| 1378 |
var selectionHasFired = false; |
| 1379 |
goog.events.listenOnce( |
| 1380 |
editableField, goog.editor.Field.EventType.SELECTIONCHANGE, |
| 1381 |
function(e) { selectionHasFired = true; }); |
| 1382 |
var editableElement = editableField.getElement(); |
| 1383 |
var otherElement = goog.dom.createDom(goog.dom.TagName.DIV); |
| 1384 |
goog.dom.insertSiblingAfter(otherElement, document.body.lastChild); |
| 1385 |
|
| 1386 |
goog.testing.events.fireMouseDownEvent(editableElement); |
| 1387 |
assertFalse(selectionHasFired); |
| 1388 |
goog.testing.events.fireMouseUpEvent(otherElement); |
| 1389 |
assertFalse(selectionHasFired); |
| 1390 |
} |
| 1391 |
|
| 1392 |
function testIsGeneratingKey() { |
| 1393 |
var regularKeyEvent = new goog.events.BrowserEvent(); |
| 1394 |
regularKeyEvent.charCode = goog.events.KeyCodes.A; |
| 1395 |
|
| 1396 |
var ctrlKeyEvent = new goog.events.BrowserEvent(); |
| 1397 |
ctrlKeyEvent.ctrlKey = true; |
| 1398 |
ctrlKeyEvent.metaKey = true; |
| 1399 |
ctrlKeyEvent.charCode = goog.events.KeyCodes.A; |
| 1400 |
|
| 1401 |
var imeKeyEvent = new goog.events.BrowserEvent(); |
| 1402 |
imeKeyEvent.keyCode = |
| 1403 |
229; // indicates from an IME - see KEYS_CAUSING_CHANGES |
| 1404 |
|
| 1405 |
assertTrue(goog.editor.Field.isGeneratingKey_(regularKeyEvent, true)); |
| 1406 |
assertFalse(goog.editor.Field.isGeneratingKey_(ctrlKeyEvent, true)); |
| 1407 |
if (goog.userAgent.WINDOWS && !goog.userAgent.GECKO) { |
| 1408 |
assertTrue(goog.editor.Field.isGeneratingKey_(imeKeyEvent, false)); |
| 1409 |
} else { |
| 1410 |
assertFalse(goog.editor.Field.isGeneratingKey_(imeKeyEvent, false)); |
| 1411 |
} |
| 1412 |
} |
| 1413 |
|
| 1414 |
function testSetEditableClassName() { |
| 1415 |
var element = goog.dom.getElement('testField'); |
| 1416 |
var editableField = new FieldConstructor('testField'); |
| 1417 |
|
| 1418 |
assertFalse(goog.dom.classlist.contains(element, 'editable')); |
| 1419 |
editableField.makeEditable(); |
| 1420 |
assertTrue(goog.dom.classlist.contains(element, 'editable')); |
| 1421 |
assertEquals( |
| 1422 |
1, |
| 1423 |
goog.array.count( |
| 1424 |
goog.dom.classlist.get(element), goog.functions.equalTo('editable'))); |
| 1425 |
|
| 1426 |
// Skip restore won't reset the original element's CSS classes. |
| 1427 |
editableField.makeUneditable(true /* opt_skipRestore */); |
| 1428 |
|
| 1429 |
editableField.makeEditable(); |
| 1430 |
assertTrue(goog.dom.classlist.contains(element, 'editable')); |
| 1431 |
assertEquals( |
| 1432 |
1, |
| 1433 |
goog.array.count( |
| 1434 |
goog.dom.classlist.get(element), goog.functions.equalTo('editable'))); |
| 1435 |
} |