Debian Codegraph @

closure-library/closure/goog/editor/plugins/undoredomanager_test.js

raw
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.UndoRedoManagerTest');
16 goog.setTestOnly('goog.editor.plugins.UndoRedoManagerTest');
17
18 goog.require('goog.editor.plugins.UndoRedoManager');
19 goog.require('goog.editor.plugins.UndoRedoState');
20 goog.require('goog.events');
21 goog.require('goog.testing.StrictMock');
22 goog.require('goog.testing.jsunit');
23
24 var mockState1;
25 var mockState2;
26 var mockState3;
27 var states;
28 var manager;
29 var stateChangeCount;
30 var beforeUndoCount;
31 var beforeRedoCount;
32 var preventDefault;
33
34 function setUp() {
35 manager = new goog.editor.plugins.UndoRedoManager();
36 stateChangeCount = 0;
37 goog.events.listen(
38 manager, goog.editor.plugins.UndoRedoManager.EventType.STATE_CHANGE,
39 function() { stateChangeCount++; });
40
41 beforeUndoCount = 0;
42 preventDefault = false;
43 goog.events.listen(
44 manager, goog.editor.plugins.UndoRedoManager.EventType.BEFORE_UNDO,
45 function(e) {
46 beforeUndoCount++;
47 if (preventDefault) {
48 e.preventDefault();
49 }
50 });
51
52 beforeRedoCount = 0;
53 goog.events.listen(
54 manager, goog.editor.plugins.UndoRedoManager.EventType.BEFORE_REDO,
55 function(e) {
56 beforeRedoCount++;
57 if (preventDefault) {
58 e.preventDefault();
59 }
60 });
61
62 mockState1 = new goog.testing.StrictMock(goog.editor.plugins.UndoRedoState);
63 mockState2 = new goog.testing.StrictMock(goog.editor.plugins.UndoRedoState);
64 mockState3 = new goog.testing.StrictMock(goog.editor.plugins.UndoRedoState);
65 states = [mockState1, mockState2, mockState3];
66
67 mockState1.equals = mockState2.equals =
68 mockState3.equals = function(state) { return this == state; };
69
70 mockState1.isAsynchronous = mockState2.isAsynchronous =
71 mockState3.isAsynchronous = function() { return false; };
72 }
73
74
75 function tearDown() {
76 goog.events.removeAll(manager);
77 manager.dispose();
78 }
79
80
81 /**
82 * Adds all the mock states to the undo-redo manager.
83 */
84 function addStatesToManager() {
85 manager.addState(states[0]);
86
87 for (var i = 1; i < states.length; i++) {
88 var state = states[i];
89 manager.addState(state);
90 }
91
92 stateChangeCount = 0;
93 }
94
95
96 /**
97 * Resets all mock states so that they are ready for testing.
98 */
99 function resetStates() {
100 for (var i = 0; i < states.length; i++) {
101 states[i].$reset();
102 }
103 }
104
105
106 function testSetMaxUndoDepth() {
107 manager.setMaxUndoDepth(2);
108 addStatesToManager();
109 assertArrayEquals(
110 'Undo stack must contain only the two most recent states.',
111 [mockState2, mockState3], manager.undoStack_);
112 }
113
114
115 function testAddState() {
116 var stateAddedCount = 0;
117 goog.events.listen(
118 manager, goog.editor.plugins.UndoRedoManager.EventType.STATE_ADDED,
119 function() { stateAddedCount++; });
120
121 manager.addState(mockState1);
122 assertArrayEquals(
123 'Undo stack must contain added state.', [mockState1], manager.undoStack_);
124 assertEquals(
125 'Manager must dispatch one state change event on ' +
126 'undo stack 0->1 transition.',
127 1, stateChangeCount);
128 assertEquals('State added must have dispatched once.', 1, stateAddedCount);
129 mockState1.$reset();
130
131 // Test adding same state twice.
132 manager.addState(mockState1);
133 assertArrayEquals(
134 'Undo stack must not contain two equal, sequential states.', [mockState1],
135 manager.undoStack_);
136 assertEquals(
137 'Manager must not dispatch state change event when nothing is ' +
138 'added to the stack.',
139 1, stateChangeCount);
140 assertEquals('State added must have dispatched once.', 1, stateAddedCount);
141
142 // Test adding a second state.
143 manager.addState(mockState2);
144 assertArrayEquals(
145 'Undo stack must contain both states.', [mockState1, mockState2],
146 manager.undoStack_);
147 assertEquals(
148 'Manager must not dispatch state change event when second ' +
149 'state is added to the stack.',
150 1, stateChangeCount);
151 assertEquals('State added must have dispatched twice.', 2, stateAddedCount);
152
153 // Test adding a state when there is state on the redo stack.
154 manager.undo();
155 assertEquals(
156 'Manager must dispatch state change when redo stack goes to 1.', 2,
157 stateChangeCount);
158
159 manager.addState(mockState3);
160 assertArrayEquals(
161 'Undo stack must contain states 1 and 3.', [mockState1, mockState3],
162 manager.undoStack_);
163 assertEquals(
164 'Manager must dispatch state change event when redo stack ' +
165 'goes to zero.',
166 3, stateChangeCount);
167 assertEquals(
168 'State added must have dispatched three times.', 3, stateAddedCount);
169 }
170
171
172 function testHasState() {
173 assertFalse('New manager must have no undo state.', manager.hasUndoState());
174 assertFalse('New manager must have no redo state.', manager.hasRedoState());
175
176 manager.addState(mockState1);
177 assertTrue('Manager must have only undo state.', manager.hasUndoState());
178 assertFalse('Manager must have no redo state.', manager.hasRedoState());
179
180 manager.undo();
181 assertFalse('Manager must have no undo state.', manager.hasUndoState());
182 assertTrue('Manager must have only redo state.', manager.hasRedoState());
183 }
184
185
186 function testClearHistory() {
187 addStatesToManager();
188 manager.undo();
189 stateChangeCount = 0;
190
191 manager.clearHistory();
192 assertFalse('Undo stack must be empty.', manager.hasUndoState());
193 assertFalse('Redo stack must be empty.', manager.hasRedoState());
194 assertEquals(
195 'State change count must be 1 after clear history.', 1, stateChangeCount);
196
197 manager.clearHistory();
198 assertEquals(
199 'Repeated clearHistory must not change state change count.', 1,
200 stateChangeCount);
201 }
202
203
204 function testUndo() {
205 addStatesToManager();
206
207 mockState3.undo();
208 mockState3.$replay();
209 manager.undo();
210 assertEquals(
211 'Adding first item to redo stack must dispatch state change.', 1,
212 stateChangeCount);
213 assertEquals('Undo must cause before action to dispatch', 1, beforeUndoCount);
214 mockState3.$verify();
215
216 preventDefault = true;
217 mockState2.$replay();
218 manager.undo();
219 assertEquals(
220 'No stack transitions between 0 and 1, must not dispatch ' +
221 'state change.',
222 1, stateChangeCount);
223 assertEquals('Undo must cause before action to dispatch', 2, beforeUndoCount);
224 mockState2.$verify(); // Verify that undo was prevented.
225
226 preventDefault = false;
227 mockState1.undo();
228 mockState1.$replay();
229 manager.undo();
230 assertEquals(
231 'Doing last undo operation must dispatch state change.', 2,
232 stateChangeCount);
233 assertEquals('Undo must cause before action to dispatch', 3, beforeUndoCount);
234 mockState1.$verify();
235 }
236
237
238 function testUndo_Asynchronous() {
239 // Using a stub instead of a mock here so that the state can behave as an
240 // EventTarget and dispatch events.
241 var stubState = new goog.editor.plugins.UndoRedoState(true);
242 var undoCalled = false;
243 stubState.undo = function() { undoCalled = true; };
244 stubState.redo = goog.nullFunction;
245 stubState.equals = function() { return false; };
246
247 manager.addState(mockState2);
248 manager.addState(mockState1);
249 manager.addState(stubState);
250
251 manager.undo();
252 assertTrue('undoCalled must be true (undo must be called).', undoCalled);
253 assertEquals('Undo must cause before action to dispatch', 1, beforeUndoCount);
254
255 // Calling undo shouldn't actually undo since the first async undo hasn't
256 // fired an event yet.
257 mockState1.$replay();
258 manager.undo();
259 mockState1.$verify();
260 assertEquals(
261 'Before action must not dispatch for pending undo.', 1, beforeUndoCount);
262
263 // Dispatching undo completed on first undo, should cause the second pending
264 // undo to happen.
265 mockState1.$reset();
266 mockState1.undo();
267 mockState1.$replay();
268 mockState2.$replay(); // Nothing should happen to mockState2.
269 stubState.dispatchEvent(goog.editor.plugins.UndoRedoState.ACTION_COMPLETED);
270 mockState1.$verify();
271 mockState2.$verify();
272 assertEquals(
273 'Second undo must cause before action to dispatch', 2, beforeUndoCount);
274
275 // Test last undo.
276 mockState2.$reset();
277 mockState2.undo();
278 mockState2.$replay();
279 manager.undo();
280 mockState2.$verify();
281 assertEquals(
282 'Third undo must cause before action to dispatch', 3, beforeUndoCount);
283 }
284
285
286 function testRedo() {
287 addStatesToManager();
288 manager.undo();
289 manager.undo();
290 manager.undo();
291 resetStates();
292 stateChangeCount = 0;
293
294 mockState1.redo();
295 mockState1.$replay();
296 manager.redo();
297 assertEquals(
298 'Pushing first item onto undo stack during redo must dispatch ' +
299 'state change.',
300 1, stateChangeCount);
301 assertEquals(
302 'First redo must cause before action to dispatch', 1, beforeRedoCount);
303 mockState1.$verify();
304
305 preventDefault = true;
306 mockState2.$replay();
307 manager.redo();
308 assertEquals(
309 'No stack transitions between 0 and 1, must not dispatch ' +
310 'state change.',
311 1, stateChangeCount);
312 assertEquals(
313 'Second redo must cause before action to dispatch', 2, beforeRedoCount);
314 mockState2.$verify(); // Verify that redo was prevented.
315
316 preventDefault = false;
317 mockState3.redo();
318 mockState3.$replay();
319 manager.redo();
320 assertEquals(
321 'Removing last item from redo stack must dispatch state change.', 2,
322 stateChangeCount);
323 assertEquals(
324 'Third redo must cause before action to dispatch', 3, beforeRedoCount);
325 mockState3.$verify();
326 mockState3.$reset();
327
328 mockState3.undo();
329 mockState3.$replay();
330 manager.undo();
331 assertEquals(
332 'Putting item on redo stack must dispatch state change.', 3,
333 stateChangeCount);
334 assertEquals('Undo must cause before action to dispatch', 4, beforeUndoCount);
335 mockState3.$verify();
336 }
337
338
339 function testRedo_Asynchronous() {
340 var stubState = new goog.editor.plugins.UndoRedoState(true);
341 var redoCalled = false;
342 stubState.redo = function() { redoCalled = true; };
343 stubState.undo = goog.nullFunction;
344 stubState.equals = function() { return false; };
345
346 manager.addState(stubState);
347 manager.addState(mockState1);
348 manager.addState(mockState2);
349
350 manager.undo();
351 manager.undo();
352 manager.undo();
353 stubState.dispatchEvent(goog.editor.plugins.UndoRedoState.ACTION_COMPLETED);
354 resetStates();
355
356 manager.redo();
357 assertTrue('redoCalled must be true (redo must be called).', redoCalled);
358
359 // Calling redo shouldn't actually redo since the first async redo hasn't
360 // fired an event yet.
361 mockState1.$replay();
362 manager.redo();
363 mockState1.$verify();
364
365 // Dispatching redo completed on first redo, should cause the second pending
366 // redo to happen.
367 mockState1.$reset();
368 mockState1.redo();
369 mockState1.$replay();
370 mockState2.$replay(); // Nothing should happen to mockState1.
371 stubState.dispatchEvent(goog.editor.plugins.UndoRedoState.ACTION_COMPLETED);
372 mockState1.$verify();
373 mockState2.$verify();
374
375 // Test last redo.
376 mockState2.$reset();
377 mockState2.redo();
378 mockState2.$replay();
379 manager.redo();
380 mockState2.$verify();
381 }
382
383 function testUndoAndRedoPeek() {
384 addStatesToManager();
385 manager.undo();
386
387 assertEquals(
388 'redoPeek must return the top of the redo stack.',
389 manager.redoStack_[manager.redoStack_.length - 1], manager.redoPeek());
390 assertEquals(
391 'undoPeek must return the top of the undo stack.',
392 manager.undoStack_[manager.undoStack_.length - 1], manager.undoPeek());
393 }