| 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.UndoRedoTest'); |
| 16 |
goog.setTestOnly('goog.editor.plugins.UndoRedoTest'); |
| 17 |
|
| 18 |
goog.require('goog.array'); |
| 19 |
goog.require('goog.dom'); |
| 20 |
goog.require('goog.dom.browserrange'); |
| 21 |
goog.require('goog.editor.Field'); |
| 22 |
goog.require('goog.editor.plugins.LoremIpsum'); |
| 23 |
goog.require('goog.editor.plugins.UndoRedo'); |
| 24 |
goog.require('goog.events'); |
| 25 |
goog.require('goog.functions'); |
| 26 |
goog.require('goog.html.SafeHtml'); |
| 27 |
goog.require('goog.testing.MockClock'); |
| 28 |
goog.require('goog.testing.PropertyReplacer'); |
| 29 |
goog.require('goog.testing.StrictMock'); |
| 30 |
goog.require('goog.testing.jsunit'); |
| 31 |
|
| 32 |
var mockEditableField; |
| 33 |
var editableField; |
| 34 |
var fieldHashCode; |
| 35 |
var undoPlugin; |
| 36 |
var state; |
| 37 |
var mockState; |
| 38 |
var commands; |
| 39 |
var clock; |
| 40 |
var stubs = new goog.testing.PropertyReplacer(); |
| 41 |
|
| 42 |
|
| 43 |
function setUp() { |
| 44 |
mockEditableField = new goog.testing.StrictMock(goog.editor.Field); |
| 45 |
|
| 46 |
// Update the arg list verifier for dispatchCommandValueChange to |
| 47 |
// correctly compare arguments that are arrays (or other complex objects). |
| 48 |
mockEditableField.$registerArgumentListVerifier( |
| 49 |
'dispatchEvent', function(expected, args) { |
| 50 |
return goog.array.equals(expected, args, function(a, b) { |
| 51 |
assertObjectEquals(a, b); |
| 52 |
return true; |
| 53 |
}); |
| 54 |
}); |
| 55 |
mockEditableField.getHashCode = function() { return 'fieldId'; }; |
| 56 |
|
| 57 |
undoPlugin = new goog.editor.plugins.UndoRedo(); |
| 58 |
undoPlugin.registerFieldObject(mockEditableField); |
| 59 |
mockState = |
| 60 |
new goog.testing.StrictMock(goog.editor.plugins.UndoRedo.UndoState_); |
| 61 |
mockState.fieldHashCode = 'fieldId'; |
| 62 |
mockState.isAsynchronous = function() { return false; }; |
| 63 |
// Don't bother mocking the inherited event target pieces of the state. |
| 64 |
// If we don't do this, then mocked asynchronous undos are a lot harder and |
| 65 |
// that behavior is tested as part of the UndoRedoManager tests. |
| 66 |
mockState.addEventListener = goog.nullFunction; |
| 67 |
|
| 68 |
commands = [ |
| 69 |
goog.editor.plugins.UndoRedo.COMMAND.REDO, |
| 70 |
goog.editor.plugins.UndoRedo.COMMAND.UNDO |
| 71 |
]; |
| 72 |
state = new goog.editor.plugins.UndoRedo.UndoState_( |
| 73 |
'1', '', null, goog.nullFunction); |
| 74 |
|
| 75 |
clock = new goog.testing.MockClock(true); |
| 76 |
|
| 77 |
editableField = new goog.editor.Field('testField'); |
| 78 |
fieldHashCode = editableField.getHashCode(); |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
function tearDown() { |
| 83 |
// Reset field so any attempted access during disposes don't cause errors. |
| 84 |
mockEditableField.$reset(); |
| 85 |
clock.dispose(); |
| 86 |
undoPlugin.dispose(); |
| 87 |
|
| 88 |
// NOTE(nicksantos): I think IE is blowing up on this call because |
| 89 |
// it is lame. It manifests its lameness by throwing an exception. |
| 90 |
// Kudos to XT for helping me to figure this out. |
| 91 |
try { |
| 92 |
} catch (e) { |
| 93 |
} |
| 94 |
|
| 95 |
if (!editableField.isUneditable()) { |
| 96 |
editableField.makeUneditable(); |
| 97 |
} |
| 98 |
editableField.dispose(); |
| 99 |
goog.dom.removeChildren(goog.dom.getElement('testField')); |
| 100 |
stubs.reset(); |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
// undo-redo plugin tests |
| 105 |
|
| 106 |
|
| 107 |
function testQueryCommandValue() { |
| 108 |
assertFalse( |
| 109 |
'Must return false for empty undo stack.', |
| 110 |
undoPlugin.queryCommandValue(goog.editor.plugins.UndoRedo.COMMAND.UNDO)); |
| 111 |
|
| 112 |
assertFalse( |
| 113 |
'Must return false for empty redo stack.', |
| 114 |
undoPlugin.queryCommandValue(goog.editor.plugins.UndoRedo.COMMAND.REDO)); |
| 115 |
|
| 116 |
undoPlugin.undoManager_.addState(mockState); |
| 117 |
|
| 118 |
assertTrue( |
| 119 |
'Must return true for a non-empty undo stack.', |
| 120 |
undoPlugin.queryCommandValue(goog.editor.plugins.UndoRedo.COMMAND.UNDO)); |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
function testExecCommand() { |
| 125 |
undoPlugin.undoManager_.addState(mockState); |
| 126 |
|
| 127 |
mockState.undo(); |
| 128 |
mockState.$replay(); |
| 129 |
|
| 130 |
undoPlugin.execCommand(goog.editor.plugins.UndoRedo.COMMAND.UNDO); |
| 131 |
// Second undo should do nothing since only one item on stack. |
| 132 |
undoPlugin.execCommand(goog.editor.plugins.UndoRedo.COMMAND.UNDO); |
| 133 |
mockState.$verify(); |
| 134 |
|
| 135 |
mockState.$reset(); |
| 136 |
mockState.redo(); |
| 137 |
mockState.$replay(); |
| 138 |
undoPlugin.execCommand(goog.editor.plugins.UndoRedo.COMMAND.REDO); |
| 139 |
// Second redo should do nothing since only one item on stack. |
| 140 |
undoPlugin.execCommand(goog.editor.plugins.UndoRedo.COMMAND.REDO); |
| 141 |
mockState.$verify(); |
| 142 |
} |
| 143 |
|
| 144 |
function testHandleKeyboardShortcut_TrogStates() { |
| 145 |
undoPlugin.undoManager_.addState(mockState); |
| 146 |
undoPlugin.undoManager_.addState(state); |
| 147 |
undoPlugin.undoManager_.undo(); |
| 148 |
mockEditableField.$reset(); |
| 149 |
|
| 150 |
var stubUndoEvent = {ctrlKey: true, altKey: false, shiftKey: false}; |
| 151 |
var stubRedoEvent = {ctrlKey: true, altKey: false, shiftKey: true}; |
| 152 |
var stubRedoEvent2 = {ctrlKey: true, altKey: false, shiftKey: false}; |
| 153 |
var result; |
| 154 |
|
| 155 |
// Test handling Trogedit undos. Should always call EditableField's |
| 156 |
// execCommand. Since EditableField is mocked, this will not result in a call |
| 157 |
// to the mockState's undo and redo methods. |
| 158 |
mockEditableField.execCommand(goog.editor.plugins.UndoRedo.COMMAND.UNDO); |
| 159 |
mockEditableField.$replay(); |
| 160 |
result = undoPlugin.handleKeyboardShortcut(stubUndoEvent, 'z', true); |
| 161 |
assertTrue('Plugin must return true when it handles shortcut.', result); |
| 162 |
mockEditableField.$verify(); |
| 163 |
mockEditableField.$reset(); |
| 164 |
|
| 165 |
mockEditableField.execCommand(goog.editor.plugins.UndoRedo.COMMAND.REDO); |
| 166 |
mockEditableField.$replay(); |
| 167 |
result = undoPlugin.handleKeyboardShortcut(stubRedoEvent, 'z', true); |
| 168 |
assertTrue('Plugin must return true when it handles shortcut.', result); |
| 169 |
mockEditableField.$verify(); |
| 170 |
mockEditableField.$reset(); |
| 171 |
|
| 172 |
mockEditableField.execCommand(goog.editor.plugins.UndoRedo.COMMAND.REDO); |
| 173 |
mockEditableField.$replay(); |
| 174 |
result = undoPlugin.handleKeyboardShortcut(stubRedoEvent2, 'y', true); |
| 175 |
assertTrue('Plugin must return true when it handles shortcut.', result); |
| 176 |
mockEditableField.$verify(); |
| 177 |
mockEditableField.$reset(); |
| 178 |
|
| 179 |
mockEditableField.$replay(); |
| 180 |
result = undoPlugin.handleKeyboardShortcut(stubRedoEvent2, 'y', false); |
| 181 |
assertFalse('Plugin must return false when modifier is not pressed.', result); |
| 182 |
mockEditableField.$verify(); |
| 183 |
mockEditableField.$reset(); |
| 184 |
|
| 185 |
mockEditableField.$replay(); |
| 186 |
result = undoPlugin.handleKeyboardShortcut(stubUndoEvent, 'f', true); |
| 187 |
assertFalse( |
| 188 |
'Plugin must return false when it doesn\'t handle shortcut.', result); |
| 189 |
mockEditableField.$verify(); |
| 190 |
} |
| 191 |
|
| 192 |
function testHandleKeyboardShortcut_NotTrogStates() { |
| 193 |
var stubUndoEvent = {ctrlKey: true, altKey: false, shiftKey: false}; |
| 194 |
|
| 195 |
// Trogedit undo states all have a fieldHashCode, nulling that out makes this |
| 196 |
// state be treated as a non-Trogedit undo-redo state. |
| 197 |
state.fieldHashCode = null; |
| 198 |
undoPlugin.undoManager_.addState(state); |
| 199 |
mockEditableField.$reset(); |
| 200 |
|
| 201 |
// Non-trog state shouldn't go through EditableField.execCommand, however, |
| 202 |
// we still exect command value change dispatch since undo-redo plugin |
| 203 |
// redispatches those anytime manager's state changes. |
| 204 |
mockEditableField.dispatchEvent({ |
| 205 |
type: goog.editor.Field.EventType.COMMAND_VALUE_CHANGE, |
| 206 |
commands: commands |
| 207 |
}); |
| 208 |
mockEditableField.$replay(); |
| 209 |
var result = undoPlugin.handleKeyboardShortcut(stubUndoEvent, 'z', true); |
| 210 |
assertTrue('Plugin must return true when it handles shortcut.', result); |
| 211 |
mockEditableField.$verify(); |
| 212 |
} |
| 213 |
|
| 214 |
function testEnable() { |
| 215 |
assertFalse( |
| 216 |
'Plugin must start disabled.', undoPlugin.isEnabled(editableField)); |
| 217 |
|
| 218 |
editableField.makeEditable(); |
| 219 |
editableField.setSafeHtml(false, goog.html.SafeHtml.create('div', {}, 'a')); |
| 220 |
undoPlugin.enable(editableField); |
| 221 |
|
| 222 |
assertTrue(undoPlugin.isEnabled(editableField)); |
| 223 |
assertNotNull( |
| 224 |
'Must have an event handler for enabled field.', |
| 225 |
undoPlugin.eventHandlers_[fieldHashCode]); |
| 226 |
|
| 227 |
var currentState = undoPlugin.currentStates_[fieldHashCode]; |
| 228 |
assertNotNull('Enabled plugin must have a current state.', currentState); |
| 229 |
assertEquals( |
| 230 |
'After enable, undo content must match the field content.', |
| 231 |
editableField.getElement().innerHTML, currentState.undoContent_); |
| 232 |
|
| 233 |
assertTrue( |
| 234 |
'After enable, undo cursorPosition must match the field cursor' + |
| 235 |
'position.', |
| 236 |
cursorPositionsEqual( |
| 237 |
getCurrentCursorPosition(), currentState.undoCursorPosition_)); |
| 238 |
|
| 239 |
assertUndefined( |
| 240 |
'Current state must never have redo content.', currentState.redoContent_); |
| 241 |
assertUndefined( |
| 242 |
'Current state must never have redo cursor position.', |
| 243 |
currentState.redoCursorPosition_); |
| 244 |
} |
| 245 |
|
| 246 |
function testDisable() { |
| 247 |
editableField.makeEditable(); |
| 248 |
undoPlugin.enable(editableField); |
| 249 |
assertTrue( |
| 250 |
'Plugin must be enabled so we can test disabling.', |
| 251 |
undoPlugin.isEnabled(editableField)); |
| 252 |
|
| 253 |
var delayedChangeFired = false; |
| 254 |
goog.events.listenOnce( |
| 255 |
editableField, goog.editor.Field.EventType.DELAYEDCHANGE, |
| 256 |
function(e) { delayedChangeFired = true; }); |
| 257 |
editableField.setSafeHtml(false, goog.html.SafeHtml.htmlEscape('foo')); |
| 258 |
|
| 259 |
undoPlugin.disable(editableField); |
| 260 |
assertTrue('disable must fire pending delayed changes.', delayedChangeFired); |
| 261 |
assertEquals( |
| 262 |
'disable must add undo state from pending change.', 1, |
| 263 |
undoPlugin.undoManager_.undoStack_.length); |
| 264 |
|
| 265 |
assertFalse(undoPlugin.isEnabled(editableField)); |
| 266 |
assertUndefined( |
| 267 |
'Disabled plugin must not have current state.', |
| 268 |
undoPlugin.eventHandlers_[fieldHashCode]); |
| 269 |
assertUndefined( |
| 270 |
'Disabled plugin must not have event handlers.', |
| 271 |
undoPlugin.eventHandlers_[fieldHashCode]); |
| 272 |
} |
| 273 |
|
| 274 |
function testUpdateCurrentState_() { |
| 275 |
editableField.registerPlugin(new goog.editor.plugins.LoremIpsum('LOREM')); |
| 276 |
editableField.makeEditable(); |
| 277 |
editableField.getPluginByClassId('LoremIpsum').usingLorem_ = true; |
| 278 |
undoPlugin.updateCurrentState_(editableField); |
| 279 |
var currentState = undoPlugin.currentStates_[fieldHashCode]; |
| 280 |
assertNotUndefined( |
| 281 |
'Must create empty states for field using lorem ipsum.', |
| 282 |
undoPlugin.currentStates_[fieldHashCode]); |
| 283 |
assertEquals('', currentState.undoContent_); |
| 284 |
assertNull(currentState.undoCursorPosition_); |
| 285 |
|
| 286 |
editableField.getPluginByClassId('LoremIpsum').usingLorem_ = false; |
| 287 |
|
| 288 |
// Pretend foo is the default contents to test '' == default contents |
| 289 |
// behavior. |
| 290 |
editableField.getInjectableContents = function(contents, styles) { |
| 291 |
return contents == '' ? 'foo' : contents; |
| 292 |
}; |
| 293 |
editableField.setSafeHtml(false, goog.html.SafeHtml.htmlEscape('foo')); |
| 294 |
undoPlugin.updateCurrentState_(editableField); |
| 295 |
assertEquals(currentState, undoPlugin.currentStates_[fieldHashCode]); |
| 296 |
|
| 297 |
// NOTE(user): Because there is already a current state, this setSafeHtml will |
| 298 |
// add a state to the undo stack. |
| 299 |
editableField.setSafeHtml(false, goog.html.SafeHtml.create('div', {}, 'a')); |
| 300 |
// Select some text so we have a valid selection that gets saved in the |
| 301 |
// UndoState. |
| 302 |
goog.dom.browserrange.createRangeFromNodeContents(editableField.getElement()) |
| 303 |
.select(); |
| 304 |
|
| 305 |
undoPlugin.updateCurrentState_(editableField); |
| 306 |
currentState = undoPlugin.currentStates_[fieldHashCode]; |
| 307 |
assertNotNull( |
| 308 |
'Must create state for field not using lorem ipsum', currentState); |
| 309 |
assertEquals(fieldHashCode, currentState.fieldHashCode); |
| 310 |
var content = editableField.getElement().innerHTML; |
| 311 |
var cursorPosition = getCurrentCursorPosition(); |
| 312 |
assertEquals(content, currentState.undoContent_); |
| 313 |
assertTrue( |
| 314 |
cursorPositionsEqual(cursorPosition, currentState.undoCursorPosition_)); |
| 315 |
assertUndefined(currentState.redoContent_); |
| 316 |
assertUndefined(currentState.redoCursorPosition_); |
| 317 |
|
| 318 |
undoPlugin.updateCurrentState_(editableField); |
| 319 |
assertEquals( |
| 320 |
'Updating state when state has not changed must not add undo ' + |
| 321 |
'state to stack.', |
| 322 |
1, undoPlugin.undoManager_.undoStack_.length); |
| 323 |
assertEquals( |
| 324 |
'Updating state when state has not changed must not create ' + |
| 325 |
'a new state.', |
| 326 |
currentState, undoPlugin.currentStates_[fieldHashCode]); |
| 327 |
assertUndefined( |
| 328 |
'Updating state when state has not changed must not add ' + |
| 329 |
'redo content.', |
| 330 |
currentState.redoContent_); |
| 331 |
assertUndefined( |
| 332 |
'Updating state when state has not changed must not add ' + |
| 333 |
'redo cursor position.', |
| 334 |
currentState.redoCursorPosition_); |
| 335 |
|
| 336 |
editableField.setSafeHtml(false, goog.html.SafeHtml.create('div', {}, 'b')); |
| 337 |
undoPlugin.updateCurrentState_(editableField); |
| 338 |
currentState = undoPlugin.currentStates_[fieldHashCode]; |
| 339 |
assertNotNull( |
| 340 |
'Must create state for field not using lorem ipsum', currentState); |
| 341 |
assertEquals(fieldHashCode, currentState.fieldHashCode); |
| 342 |
var newContent = editableField.getElement().innerHTML; |
| 343 |
var newCursorPosition = getCurrentCursorPosition(); |
| 344 |
assertEquals(newContent, currentState.undoContent_); |
| 345 |
assertTrue( |
| 346 |
cursorPositionsEqual( |
| 347 |
newCursorPosition, currentState.undoCursorPosition_)); |
| 348 |
assertUndefined(currentState.redoContent_); |
| 349 |
assertUndefined(currentState.redoCursorPosition_); |
| 350 |
|
| 351 |
var undoState = goog.array.peek(undoPlugin.undoManager_.undoStack_); |
| 352 |
assertNotNull( |
| 353 |
'Must create state for field not using lorem ipsum', currentState); |
| 354 |
assertEquals(fieldHashCode, currentState.fieldHashCode); |
| 355 |
assertEquals(content, undoState.undoContent_); |
| 356 |
assertTrue( |
| 357 |
cursorPositionsEqual(cursorPosition, undoState.undoCursorPosition_)); |
| 358 |
assertEquals(newContent, undoState.redoContent_); |
| 359 |
assertTrue( |
| 360 |
cursorPositionsEqual(newCursorPosition, undoState.redoCursorPosition_)); |
| 361 |
} |
| 362 |
|
| 363 |
|
| 364 |
/** |
| 365 |
* Tests that change events get restarted properly after an undo call despite |
| 366 |
* an exception being thrown in the process (see bug/1991234). |
| 367 |
*/ |
| 368 |
function testUndoRestartsChangeEvents() { |
| 369 |
undoPlugin.registerFieldObject(editableField); |
| 370 |
editableField.makeEditable(); |
| 371 |
editableField.setSafeHtml(false, goog.html.SafeHtml.create('div', {}, 'a')); |
| 372 |
clock.tick(1000); |
| 373 |
undoPlugin.enable(editableField); |
| 374 |
|
| 375 |
// Change content so we can undo it. |
| 376 |
editableField.setSafeHtml(false, goog.html.SafeHtml.create('div', {}, 'b')); |
| 377 |
clock.tick(1000); |
| 378 |
|
| 379 |
var currentState = undoPlugin.currentStates_[fieldHashCode]; |
| 380 |
stubs.set( |
| 381 |
editableField, 'setCursorPosition', |
| 382 |
goog.functions.error('Faking exception during setCursorPosition()')); |
| 383 |
try { |
| 384 |
currentState.undo(); |
| 385 |
} catch (e) { |
| 386 |
fail('Exception should not have been thrown during undo()'); |
| 387 |
} |
| 388 |
assertEquals( |
| 389 |
'Change events should be on', 0, |
| 390 |
editableField.stoppedEvents_[goog.editor.Field.EventType.CHANGE]); |
| 391 |
assertEquals( |
| 392 |
'Delayed change events should be on', 0, |
| 393 |
editableField.stoppedEvents_[goog.editor.Field.EventType.DELAYEDCHANGE]); |
| 394 |
} |
| 395 |
|
| 396 |
function testRefreshCurrentState() { |
| 397 |
editableField.makeEditable(); |
| 398 |
editableField.setSafeHtml(false, goog.html.SafeHtml.create('div', {}, 'a')); |
| 399 |
clock.tick(1000); |
| 400 |
undoPlugin.enable(editableField); |
| 401 |
|
| 402 |
// Create current state and verify it. |
| 403 |
var currentState = undoPlugin.currentStates_[fieldHashCode]; |
| 404 |
assertEquals(fieldHashCode, currentState.fieldHashCode); |
| 405 |
var content = editableField.getElement().innerHTML; |
| 406 |
var cursorPosition = getCurrentCursorPosition(); |
| 407 |
assertEquals(content, currentState.undoContent_); |
| 408 |
assertTrue( |
| 409 |
cursorPositionsEqual(cursorPosition, currentState.undoCursorPosition_)); |
| 410 |
|
| 411 |
// Update the field w/o dispatching delayed change, and verify that the |
| 412 |
// current state hasn't changed to reflect new values. |
| 413 |
editableField.setSafeHtml( |
| 414 |
false, goog.html.SafeHtml.create('div', {}, 'b'), true); |
| 415 |
clock.tick(1000); |
| 416 |
currentState = undoPlugin.currentStates_[fieldHashCode]; |
| 417 |
assertEquals( |
| 418 |
'Content must match old state.', content, currentState.undoContent_); |
| 419 |
assertTrue( |
| 420 |
'Cursor position must match old state.', |
| 421 |
cursorPositionsEqual(cursorPosition, currentState.undoCursorPosition_)); |
| 422 |
|
| 423 |
undoPlugin.refreshCurrentState(editableField); |
| 424 |
assertFalse( |
| 425 |
'Refresh must not cause states to go on the undo-redo stack.', |
| 426 |
undoPlugin.undoManager_.hasUndoState()); |
| 427 |
currentState = undoPlugin.currentStates_[fieldHashCode]; |
| 428 |
content = editableField.getElement().innerHTML; |
| 429 |
cursorPosition = getCurrentCursorPosition(); |
| 430 |
assertEquals( |
| 431 |
'Content must match current field state.', content, |
| 432 |
currentState.undoContent_); |
| 433 |
assertTrue( |
| 434 |
'Cursor position must match current field state.', |
| 435 |
cursorPositionsEqual(cursorPosition, currentState.undoCursorPosition_)); |
| 436 |
|
| 437 |
undoPlugin.disable(editableField); |
| 438 |
assertUndefined(undoPlugin.currentStates_[fieldHashCode]); |
| 439 |
undoPlugin.refreshCurrentState(editableField); |
| 440 |
assertUndefined( |
| 441 |
'Must not refresh current state of fields that do not have ' + |
| 442 |
'undo-redo enabled.', |
| 443 |
undoPlugin.currentStates_[fieldHashCode]); |
| 444 |
} |
| 445 |
|
| 446 |
|
| 447 |
/** |
| 448 |
* Returns the CursorPosition for the selection currently in the Field. |
| 449 |
* @return {goog.editor.plugins.UndoRedo.CursorPosition_} |
| 450 |
*/ |
| 451 |
function getCurrentCursorPosition() { |
| 452 |
return undoPlugin.getCursorPosition_(editableField); |
| 453 |
} |
| 454 |
|
| 455 |
|
| 456 |
/** |
| 457 |
* Compares two cursor positions and returns whether they are equal. |
| 458 |
* @param {goog.editor.plugins.UndoRedo.CursorPosition_} a |
| 459 |
* A cursor position. |
| 460 |
* @param {goog.editor.plugins.UndoRedo.CursorPosition_} b |
| 461 |
* A cursor position. |
| 462 |
* @return {boolean} Whether the positions are equal. |
| 463 |
*/ |
| 464 |
function cursorPositionsEqual(a, b) { |
| 465 |
if (!a && !b) { |
| 466 |
return true; |
| 467 |
} else if (a && b) { |
| 468 |
return a.toString() == b.toString(); |
| 469 |
} |
| 470 |
// Only one cursor position is an object, can't be equal. |
| 471 |
return false; |
| 472 |
} |
| 473 |
// Undo state tests |
| 474 |
|
| 475 |
|
| 476 |
function testSetUndoState() { |
| 477 |
state.setUndoState('content', 'position'); |
| 478 |
assertEquals('Undo content incorrectly set', 'content', state.undoContent_); |
| 479 |
assertEquals( |
| 480 |
'Undo cursor position incorrectly set', 'position', |
| 481 |
state.undoCursorPosition_); |
| 482 |
} |
| 483 |
|
| 484 |
function testSetRedoState() { |
| 485 |
state.setRedoState('content', 'position'); |
| 486 |
assertEquals('Redo content incorrectly set', 'content', state.redoContent_); |
| 487 |
assertEquals( |
| 488 |
'Redo cursor position incorrectly set', 'position', |
| 489 |
state.redoCursorPosition_); |
| 490 |
} |
| 491 |
|
| 492 |
function testEquals() { |
| 493 |
assertTrue('A state must equal itself', state.equals(state)); |
| 494 |
|
| 495 |
var state2 = new goog.editor.plugins.UndoRedo.UndoState_('1', '', null); |
| 496 |
assertTrue( |
| 497 |
'A state must equal a state with the same hash code and content.', |
| 498 |
state.equals(state2)); |
| 499 |
|
| 500 |
state2 = new goog.editor.plugins.UndoRedo.UndoState_('1', '', 'foo'); |
| 501 |
assertTrue( |
| 502 |
'States with different cursor positions must be equal', |
| 503 |
state.equals(state2)); |
| 504 |
|
| 505 |
state2.setRedoState('bar', null); |
| 506 |
assertFalse( |
| 507 |
'States with different redo content must not be equal', |
| 508 |
state.equals(state2)); |
| 509 |
|
| 510 |
state2 = new goog.editor.plugins.UndoRedo.UndoState_('3', '', null); |
| 511 |
assertFalse( |
| 512 |
'States with different field hash codes must not be equal', |
| 513 |
state.equals(state2)); |
| 514 |
|
| 515 |
state2 = new goog.editor.plugins.UndoRedo.UndoState_('1', 'baz', null); |
| 516 |
assertFalse( |
| 517 |
'States with different undoContent must not be equal', |
| 518 |
state.equals(state2)); |
| 519 |
} |
| 520 |
|
| 521 |
|
| 522 |
/** @bug 1359214 */ |
| 523 |
function testClearUndoHistory() { |
| 524 |
var undoRedoPlugin = new goog.editor.plugins.UndoRedo(); |
| 525 |
editableField.registerPlugin(undoRedoPlugin); |
| 526 |
editableField.makeEditable(); |
| 527 |
|
| 528 |
editableField.dispatchChange(); |
| 529 |
clock.tick(10000); |
| 530 |
|
| 531 |
goog.dom.setTextContent(editableField.getElement(), 'y'); |
| 532 |
editableField.dispatchChange(); |
| 533 |
assertFalse(undoRedoPlugin.undoManager_.hasUndoState()); |
| 534 |
|
| 535 |
clock.tick(10000); |
| 536 |
assertTrue(undoRedoPlugin.undoManager_.hasUndoState()); |
| 537 |
|
| 538 |
goog.dom.setTextContent(editableField.getElement(), 'z'); |
| 539 |
editableField.dispatchChange(); |
| 540 |
|
| 541 |
var numCalls = 0; |
| 542 |
goog.events.listen( |
| 543 |
editableField, goog.editor.Field.EventType.DELAYEDCHANGE, |
| 544 |
function() { numCalls++; }); |
| 545 |
undoRedoPlugin.clearHistory(); |
| 546 |
// 1 call from stopChangeEvents(). 0 calls from startChangeEvents(). |
| 547 |
assertEquals( |
| 548 |
'clearHistory must not cause delayed change when none pending', 1, |
| 549 |
numCalls); |
| 550 |
|
| 551 |
clock.tick(10000); |
| 552 |
assertFalse(undoRedoPlugin.undoManager_.hasUndoState()); |
| 553 |
} |