aboutsummaryrefslogtreecommitdiff
path: root/contexts/data/lib/closure-library/closure/goog/editor/plugins/undoredomanager_test.html
blob: 9b7f215c34775aace022b8744145fbca3d632501 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
<!DOCTYPE html>
<!--
  All Rights Reserved.

  @author ajp@google.com (Andy Perelson)
-->
<html>
<!--
Copyright 2008 The Closure Library Authors. All Rights Reserved.

Use of this source code is governed by the Apache License, Version 2.0.
See the COPYING file for details.
-->
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Trogedit Unit Tests - goog.editor.plugins.UndoRedoManager</title>
<script src="../../base.js"></script>
<script>
  goog.require('goog.testing.StrictMock');
  goog.require('goog.testing.LooseMock');
  goog.require('goog.testing.jsunit');
  goog.require('goog.editor.plugins.UndoRedo');
</script>
</head>
<body>

<script>

var mockState1;
var mockState2;
var mockState3;
var states;
var manager;
var stateChangeCount;
var beforeUndoCount;
var beforeRedoCount;
var preventDefault;

function setUp() {
  manager = new goog.editor.plugins.UndoRedoManager();
  stateChangeCount = 0;  
  goog.events.listen(manager,
      goog.editor.plugins.UndoRedoManager.EventType.STATE_CHANGE,
      function() {
        stateChangeCount++;
      });

  beforeUndoCount = 0;
  preventDefault = false;
  goog.events.listen(manager,
      goog.editor.plugins.UndoRedoManager.EventType.BEFORE_UNDO,
      function(e) {
        beforeUndoCount++;
        if (preventDefault) {
          e.preventDefault();
        }
      });

  beforeRedoCount = 0;
  goog.events.listen(manager,
      goog.editor.plugins.UndoRedoManager.EventType.BEFORE_REDO,
      function(e) {
        beforeRedoCount++;
        if (preventDefault) {
          e.preventDefault();
        }
      });

  mockState1 = new goog.testing.StrictMock(goog.editor.plugins.UndoRedoState);
  mockState2 = new goog.testing.StrictMock(goog.editor.plugins.UndoRedoState);
  mockState3 = new goog.testing.StrictMock(goog.editor.plugins.UndoRedoState);
  states = [mockState1, mockState2, mockState3];

  mockState1.equals = mockState2.equals = mockState3.equals = function(state) {
    return this == state;
  };

  mockState1.isAsynchronous = mockState2.isAsynchronous =
      mockState3.isAsynchronous = function() {
    return false;
  };
}


function tearDown() {
  goog.events.removeAll(manager);
  manager.dispose();
}


/**
 * Adds all the mock states to the undo-redo manager.
 */
function addStatesToManager() {
  manager.addState(states[0]);

  for (var i = 1; i < states.length; i++) {
    var state = states[i];
    manager.addState(state);
  }

  stateChangeCount = 0;
}

/**
 * Resets all mock states so that they are ready for testing.
 */
function resetStates() {
  for (var i = 0; i < states.length; i++) {
    states[i].$reset();
  }
}


function testSetMaxUndoDepth() {
  manager.setMaxUndoDepth(2);
  addStatesToManager();
  assertArrayEquals('Undo stack must contain only the two most recent states.',
      [mockState2, mockState3], manager.undoStack_);
}


function testAddState() {
  var stateAddedCount = 0;
  goog.events.listen(manager,
      goog.editor.plugins.UndoRedoManager.EventType.STATE_ADDED,
      function() {
        stateAddedCount++;
      });

  manager.addState(mockState1);
  assertArrayEquals('Undo stack must contain added state.',
      [mockState1], manager.undoStack_);
  assertEquals('Manager must dispatch one state change event on ' +
      'undo stack 0->1 transition.', 1, stateChangeCount);
  assertEquals('State added must have dispatched once.', 1, stateAddedCount);
  mockState1.$reset();

  // Test adding same state twice.
  manager.addState(mockState1);
  assertArrayEquals('Undo stack must not contain two equal, sequential states.',
      [mockState1], manager.undoStack_);
  assertEquals('Manager must not dispatch state change event when nothing is ' +
      'added to the stack.', 1, stateChangeCount);
  assertEquals('State added must have dispatched once.', 1, stateAddedCount);

  // Test adding a second state.
  manager.addState(mockState2);
  assertArrayEquals('Undo stack must contain both states.',
      [mockState1, mockState2], manager.undoStack_);
  assertEquals('Manager must not dispatch state change event when second ' +
      'state is added to the stack.', 1, stateChangeCount);
  assertEquals('State added must have dispatched twice.', 2, stateAddedCount);

  // Test adding a state when there is state on the redo stack.
  manager.undo();
  assertEquals('Manager must dispatch state change when redo stack goes to 1.',
      2, stateChangeCount);

  manager.addState(mockState3);
  assertArrayEquals('Undo stack must contain states 1 and 3.',
      [mockState1, mockState3], manager.undoStack_);
  assertEquals('Manager must dispatch state change event when redo stack ' +
      'goes to zero.', 3, stateChangeCount);
  assertEquals('State added must have dispatched three times.',
      3, stateAddedCount);
}


function testHasState() {
  assertFalse('New manager must have no undo state.', manager.hasUndoState());
  assertFalse('New manager must have no redo state.', manager.hasRedoState());

  manager.addState(mockState1);
  assertTrue('Manager must have only undo state.', manager.hasUndoState());
  assertFalse('Manager must have no redo state.', manager.hasRedoState());

  manager.undo();
  assertFalse('Manager must have no undo state.', manager.hasUndoState());
  assertTrue('Manager must have only redo state.', manager.hasRedoState());
}


function testClearHistory() {
  addStatesToManager();
  manager.undo();
  stateChangeCount = 0;
  
  manager.clearHistory();
  assertFalse('Undo stack must be empty.', manager.hasUndoState());
  assertFalse('Redo stack must be empty.', manager.hasRedoState());
  assertEquals('State change count must be 1 after clear history.',
      1, stateChangeCount);

  manager.clearHistory();
  assertEquals('Repeated clearHistory must not change state change count.', 
      1, stateChangeCount);
}


function testUndo() {
  addStatesToManager();

  mockState3.undo();
  mockState3.$replay();
  manager.undo();
  assertEquals('Adding first item to redo stack must dispatch state change.',
      1, stateChangeCount);
  assertEquals('Undo must cause before action to dispatch',
      1, beforeUndoCount);
  mockState3.$verify();

  preventDefault = true;
  mockState2.$replay();
  manager.undo();
  assertEquals('No stack transitions between 0 and 1, must not dispatch ' +
      'state change.', 1, stateChangeCount);
  assertEquals('Undo must cause before action to dispatch',
      2, beforeUndoCount);
  mockState2.$verify(); // Verify that undo was prevented.

  preventDefault = false;
  mockState1.undo();
  mockState1.$replay();
  manager.undo();
  assertEquals('Doing last undo operation must dispatch state change.',
      2, stateChangeCount);
  assertEquals('Undo must cause before action to dispatch',
      3, beforeUndoCount);
  mockState1.$verify();
}


function testUndo_Asynchronous() {
  // Using a stub instead of a mock here so that the state can behave as an
  // EventTarget and dispatch events.
  var stubState = new goog.editor.plugins.UndoRedoState(true);
  var undoCalled = false;
  stubState.undo = function() {
    undoCalled = true;
  };
  stubState.redo = goog.nullFunction;
  stubState.equals = function() {
    return false;
  };

  manager.addState(mockState2);
  manager.addState(mockState1);
  manager.addState(stubState);

  manager.undo();
  assertTrue('undoCalled must be true (undo must be called).', undoCalled);
  assertEquals('Undo must cause before action to dispatch',
      1, beforeUndoCount);

  // Calling undo shouldn't actually undo since the first async undo hasn't
  // fired an event yet.
  mockState1.$replay();
  manager.undo();
  mockState1.$verify();
  assertEquals('Before action must not dispatch for pending undo.',
      1, beforeUndoCount);

  // Dispatching undo completed on first undo, should cause the second pending
  // undo to happen.
  mockState1.$reset();
  mockState1.undo();
  mockState1.$replay();
  mockState2.$replay(); // Nothing should happen to mockState2.
  stubState.dispatchEvent(goog.editor.plugins.UndoRedoState.ACTION_COMPLETED);
  mockState1.$verify();
  mockState2.$verify();
  assertEquals('Second undo must cause before action to dispatch',
      2, beforeUndoCount);

  // Test last undo.
  mockState2.$reset();
  mockState2.undo();
  mockState2.$replay();
  manager.undo();
  mockState2.$verify();
  assertEquals('Third undo must cause before action to dispatch',
      3, beforeUndoCount);
}


function testRedo() {
  addStatesToManager();
  manager.undo();
  manager.undo();
  manager.undo();
  resetStates();
  stateChangeCount = 0;

  mockState1.redo();
  mockState1.$replay();
  manager.redo();
  assertEquals('Pushing first item onto undo stack during redo must dispatch ' +
               'state change.', 1, stateChangeCount);
  assertEquals('First redo must cause before action to dispatch',
      1, beforeRedoCount);
  mockState1.$verify();

  preventDefault = true;
  mockState2.$replay();
  manager.redo();
  assertEquals('No stack transitions between 0 and 1, must not dispatch ' +
      'state change.', 1, stateChangeCount);
  assertEquals('Second redo must cause before action to dispatch',
      2, beforeRedoCount);
  mockState2.$verify(); // Verify that redo was prevented.

  preventDefault = false;
  mockState3.redo();
  mockState3.$replay();
  manager.redo();
  assertEquals('Removing last item from redo stack must dispatch state change.',
      2, stateChangeCount);
  assertEquals('Third redo must cause before action to dispatch',
      3, beforeRedoCount);
  mockState3.$verify();
  mockState3.$reset();

  mockState3.undo();
  mockState3.$replay();
  manager.undo();
  assertEquals('Putting item on redo stack must dispatch state change.',
      3, stateChangeCount);
  assertEquals('Undo must cause before action to dispatch',
      4, beforeUndoCount);
  mockState3.$verify();
}


function testRedo_Asynchronous() {
  var stubState = new goog.editor.plugins.UndoRedoState(true);
  var redoCalled = false;
  stubState.redo = function() {
    redoCalled = true;
  };
  stubState.undo = goog.nullFunction;
  stubState.equals = function() {
    return false;
  };

  manager.addState(stubState);
  manager.addState(mockState1);
  manager.addState(mockState2);

  manager.undo();
  manager.undo();
  manager.undo();
  stubState.dispatchEvent(goog.editor.plugins.UndoRedoState.ACTION_COMPLETED);
  resetStates();

  manager.redo();
  assertTrue('redoCalled must be true (redo must be called).', redoCalled);

  // Calling redo shouldn't actually redo since the first async redo hasn't
  // fired an event yet.
  mockState1.$replay();
  manager.redo();
  mockState1.$verify();

  // Dispatching redo completed on first redo, should cause the second pending
  // redo to happen.
  mockState1.$reset();
  mockState1.redo();
  mockState1.$replay();
  mockState2.$replay(); // Nothing should happen to mockState1.
  stubState.dispatchEvent(goog.editor.plugins.UndoRedoState.ACTION_COMPLETED);
  mockState1.$verify();
  mockState2.$verify();

  // Test last redo.
  mockState2.$reset();
  mockState2.redo();
  mockState2.$replay();
  manager.redo();
  mockState2.$verify();
}

function testUndoAndRedoPeek() {
  addStatesToManager();
  manager.undo();

  assertEquals('redoPeek must return the top of the redo stack.',
      manager.redoStack_[manager.redoStack_.length - 1], manager.redoPeek());
  assertEquals('undoPeek must return the top of the undo stack.',
      manager.undoStack_[manager.undoStack_.length - 1], manager.undoPeek());
}
</script>
</body>
</html>