source: other-projects/nz-flag-design/trunk/design-2d/Original editor.method.ac/test/history_test.html@ 29468

Last change on this file since 29468 was 29468, checked in by sjs49, 9 years ago

Initial commit for editor.method.ac for flag design

  • Property svn:executable set to *
File size: 15.9 KB
Line 
1<!DOCTYPE html>
2<html>
3<head>
4 <link rel='stylesheet' href='qunit/qunit.css' type='text/css'/>
5 <script src='../editor/lib/jquery.js'></script>
6 <script type='text/javascript' src='../editor/src/history.js'></script>
7 <script type='text/javascript' src='qunit/qunit.js'></script>
8 <script type='text/javascript'>
9 $(function() {
10 // TODO(codedread): Write tests for handling history events.
11
12 // Mocked out methods.
13 svgedit.transformlist = {};
14 svgedit.transformlist.removeElementFromListMap = function(elem) {};
15 svgedit.utilities = {};
16 svgedit.utilities.getHref = function(elem) { return '#foo'; };
17 svgedit.utilities.setHref = function(elem, val) {};
18 svgedit.utilities.getRotationAngle = function(elem) { return 0; };
19
20 // log function
21 QUnit.log = function(result, message) {
22 if (window.console && window.console.log) {
23 window.console.log(result +' :: '+ message);
24 }
25 };
26
27 var svgns = 'http://www.w3.org/2000/svg';
28 var svg = document.createElementNS(svgns, 'svg');
29 var undoMgr = null;
30 var divparent = document.getElementById('divparent');
31 var div1 = document.getElementById('div1');
32 var div2 = document.getElementById('div2');
33 var div3 = document.getElementById('div3');
34 var div4 = document.getElementById('div4');
35 var div5 = document.getElementById('div5');
36
37 module('svgedit.history');
38
39 var MockCommand = function(opt_text) { this.text_ = opt_text; };
40 MockCommand.prototype.apply = function() {};
41 MockCommand.prototype.unapply = function() {};
42 MockCommand.prototype.getText = function() { return this.text_; };
43 MockCommand.prototype.elements = function() { return []; };
44
45 var MockHistoryEventHandler = function() {};
46 MockHistoryEventHandler.prototype.handleHistoryEvent = function(eventType, command) {};
47
48 function setUp() {
49 undoMgr = new svgedit.history.UndoManager();
50 }
51 function tearDown() {
52 undoMgr = null;
53 }
54
55 test('Test svgedit.history package', function() {
56 expect(13);
57
58 ok(svgedit.history);
59 ok(svgedit.history.MoveElementCommand);
60 ok(svgedit.history.InsertElementCommand);
61 ok(svgedit.history.ChangeElementCommand);
62 ok(svgedit.history.RemoveElementCommand);
63 ok(svgedit.history.BatchCommand);
64 ok(svgedit.history.UndoManager);
65 equals(typeof svgedit.history.MoveElementCommand, typeof function(){});
66 equals(typeof svgedit.history.InsertElementCommand, typeof function(){});
67 equals(typeof svgedit.history.ChangeElementCommand, typeof function(){});
68 equals(typeof svgedit.history.RemoveElementCommand, typeof function(){});
69 equals(typeof svgedit.history.BatchCommand, typeof function(){});
70 equals(typeof svgedit.history.UndoManager, typeof function(){});
71 });
72
73 test('Test UndoManager methods', function() {
74 expect(14);
75 setUp();
76
77 ok(undoMgr);
78 ok(undoMgr.addCommandToHistory);
79 ok(undoMgr.getUndoStackSize);
80 ok(undoMgr.getRedoStackSize);
81 ok(undoMgr.resetUndoStack);
82 ok(undoMgr.getNextUndoCommandText);
83 ok(undoMgr.getNextRedoCommandText);
84
85 equals(typeof undoMgr, typeof {});
86 equals(typeof undoMgr.addCommandToHistory, typeof function(){});
87 equals(typeof undoMgr.getUndoStackSize, typeof function(){});
88 equals(typeof undoMgr.getRedoStackSize, typeof function(){});
89 equals(typeof undoMgr.resetUndoStack, typeof function(){});
90 equals(typeof undoMgr.getNextUndoCommandText, typeof function(){});
91 equals(typeof undoMgr.getNextRedoCommandText, typeof function(){});
92
93 tearDown();
94 });
95
96 test('Test UndoManager.addCommandToHistory() function', function() {
97 expect(3);
98
99 setUp();
100
101 equals(undoMgr.getUndoStackSize(), 0);
102 undoMgr.addCommandToHistory(new MockCommand());
103 equals(undoMgr.getUndoStackSize(), 1);
104 undoMgr.addCommandToHistory(new MockCommand());
105 equals(undoMgr.getUndoStackSize(), 2);
106
107 tearDown();
108 });
109
110 test('Test UndoManager.getUndoStackSize() and getRedoStackSize() functions', function() {
111 expect(18);
112
113 setUp();
114
115 undoMgr.addCommandToHistory(new MockCommand());
116 undoMgr.addCommandToHistory(new MockCommand());
117 undoMgr.addCommandToHistory(new MockCommand());
118
119 equals(undoMgr.getUndoStackSize(), 3);
120 equals(undoMgr.getRedoStackSize(), 0);
121
122 undoMgr.undo();
123 equals(undoMgr.getUndoStackSize(), 2);
124 equals(undoMgr.getRedoStackSize(), 1);
125
126 undoMgr.undo();
127 equals(undoMgr.getUndoStackSize(), 1);
128 equals(undoMgr.getRedoStackSize(), 2);
129
130 undoMgr.undo();
131 equals(undoMgr.getUndoStackSize(), 0);
132 equals(undoMgr.getRedoStackSize(), 3);
133
134 undoMgr.undo();
135 equals(undoMgr.getUndoStackSize(), 0);
136 equals(undoMgr.getRedoStackSize(), 3);
137
138 undoMgr.redo();
139 equals(undoMgr.getUndoStackSize(), 1);
140 equals(undoMgr.getRedoStackSize(), 2);
141
142 undoMgr.redo();
143 equals(undoMgr.getUndoStackSize(), 2);
144 equals(undoMgr.getRedoStackSize(), 1);
145
146 undoMgr.redo();
147 equals(undoMgr.getUndoStackSize(), 3);
148 equals(undoMgr.getRedoStackSize(), 0);
149
150 undoMgr.redo();
151 equals(undoMgr.getUndoStackSize(), 3);
152 equals(undoMgr.getRedoStackSize(), 0);
153
154 tearDown();
155 });
156
157 test('Test UndoManager.resetUndoStackSize() function', function() {
158 expect(4);
159
160 setUp();
161
162 undoMgr.addCommandToHistory(new MockCommand());
163 undoMgr.addCommandToHistory(new MockCommand());
164 undoMgr.addCommandToHistory(new MockCommand());
165 undoMgr.undo();
166
167 equals(undoMgr.getUndoStackSize(), 2);
168 equals(undoMgr.getRedoStackSize(), 1);
169
170 undoMgr.resetUndoStack();
171
172 equals(undoMgr.getUndoStackSize(), 0);
173 equals(undoMgr.getRedoStackSize(), 0);
174
175 tearDown();
176 });
177
178 test('Test UndoManager.getNextUndoCommandText() function', function() {
179 expect(9);
180
181 setUp();
182
183 equals(undoMgr.getNextUndoCommandText(), '');
184
185 undoMgr.addCommandToHistory(new MockCommand('First'));
186 undoMgr.addCommandToHistory(new MockCommand('Second'));
187 undoMgr.addCommandToHistory(new MockCommand('Third'));
188
189 equals(undoMgr.getNextUndoCommandText(), 'Third');
190
191 undoMgr.undo();
192 equals(undoMgr.getNextUndoCommandText(), 'Second');
193
194 undoMgr.undo();
195 equals(undoMgr.getNextUndoCommandText(), 'First');
196
197 undoMgr.undo();
198 equals(undoMgr.getNextUndoCommandText(), '');
199
200 undoMgr.redo();
201 equals(undoMgr.getNextUndoCommandText(), 'First');
202
203 undoMgr.redo();
204 equals(undoMgr.getNextUndoCommandText(), 'Second');
205
206 undoMgr.redo();
207 equals(undoMgr.getNextUndoCommandText(), 'Third');
208
209 undoMgr.redo();
210 equals(undoMgr.getNextUndoCommandText(), 'Third');
211
212 tearDown();
213 });
214
215 test('Test UndoManager.getNextRedoCommandText() function', function() {
216 expect(8);
217
218 setUp();
219
220 equals(undoMgr.getNextRedoCommandText(), '');
221
222 undoMgr.addCommandToHistory(new MockCommand('First'));
223 undoMgr.addCommandToHistory(new MockCommand('Second'));
224 undoMgr.addCommandToHistory(new MockCommand('Third'));
225
226 equals(undoMgr.getNextRedoCommandText(), '');
227
228 undoMgr.undo();
229 equals(undoMgr.getNextRedoCommandText(), 'Third');
230
231 undoMgr.undo();
232 equals(undoMgr.getNextRedoCommandText(), 'Second');
233
234 undoMgr.undo();
235 equals(undoMgr.getNextRedoCommandText(), 'First');
236
237 undoMgr.redo();
238 equals(undoMgr.getNextRedoCommandText(), 'Second');
239
240 undoMgr.redo();
241 equals(undoMgr.getNextRedoCommandText(), 'Third');
242
243 undoMgr.redo();
244 equals(undoMgr.getNextRedoCommandText(), '');
245
246 tearDown();
247 });
248
249 test('Test UndoManager.undo() and redo() functions', function() {
250 expect(10);
251
252 setUp();
253
254 var lastCalled = null;
255 var cmd1 = new MockCommand();
256 var cmd2 = new MockCommand();
257 var cmd3 = new MockCommand();
258 cmd1.apply = function() { lastCalled = 'cmd1.apply'; };
259 cmd2.apply = function() { lastCalled = 'cmd2.apply'; };
260 cmd3.apply = function() { lastCalled = 'cmd3.apply'; };
261 cmd1.unapply = function() { lastCalled = 'cmd1.unapply'; };
262 cmd2.unapply = function() { lastCalled = 'cmd2.unapply'; };
263 cmd3.unapply = function() { lastCalled = 'cmd3.unapply'; };
264
265 undoMgr.addCommandToHistory(cmd1);
266 undoMgr.addCommandToHistory(cmd2);
267 undoMgr.addCommandToHistory(cmd3);
268
269 ok(!lastCalled);
270
271 undoMgr.undo();
272 equals(lastCalled, 'cmd3.unapply');
273
274 undoMgr.redo();
275 equals(lastCalled, 'cmd3.apply');
276
277 undoMgr.undo();
278 undoMgr.undo();
279 equals(lastCalled, 'cmd2.unapply');
280
281 undoMgr.undo();
282 equals(lastCalled, 'cmd1.unapply');
283 lastCalled = null;
284
285 undoMgr.undo();
286 ok(!lastCalled);
287
288 undoMgr.redo();
289 equals(lastCalled, 'cmd1.apply');
290
291 undoMgr.redo();
292 equals(lastCalled, 'cmd2.apply');
293
294 undoMgr.redo();
295 equals(lastCalled, 'cmd3.apply');
296 lastCalled = null;
297
298 undoMgr.redo();
299 ok(!lastCalled);
300
301 tearDown();
302 });
303
304 test('Test MoveElementCommand', function() {
305 expect(26);
306
307 setUp();
308
309 var move = new svgedit.history.MoveElementCommand(div3, div1, divparent);
310 ok(move.unapply);
311 ok(move.apply);
312 equals(typeof move.unapply, typeof function(){});
313 equals(typeof move.apply, typeof function(){});
314
315 move.unapply();
316 equals(divparent.firstElementChild, div3);
317 equals(divparent.firstElementChild.nextElementSibling, div1);
318 equals(divparent.lastElementChild, div2);
319
320 move.apply();
321 equals(divparent.firstElementChild, div1);
322 equals(divparent.firstElementChild.nextElementSibling, div2);
323 equals(divparent.lastElementChild, div3);
324
325 move = new svgedit.history.MoveElementCommand(div1, null, divparent);
326
327 move.unapply();
328 equals(divparent.firstElementChild, div2);
329 equals(divparent.firstElementChild.nextElementSibling, div3);
330 equals(divparent.lastElementChild, div1);
331
332 move.apply();
333 equals(divparent.firstElementChild, div1);
334 equals(divparent.firstElementChild.nextElementSibling, div2);
335 equals(divparent.lastElementChild, div3);
336
337 move = new svgedit.history.MoveElementCommand(div2, div5, div4);
338
339 move.unapply();
340 equals(divparent.firstElementChild, div1);
341 equals(divparent.firstElementChild.nextElementSibling, div3);
342 equals(divparent.lastElementChild, div3);
343 equals(div4.firstElementChild, div2);
344 equals(div4.firstElementChild.nextElementSibling, div5);
345
346 move.apply();
347 equals(divparent.firstElementChild, div1);
348 equals(divparent.firstElementChild.nextElementSibling, div2);
349 equals(divparent.lastElementChild, div3);
350 equals(div4.firstElementChild, div5);
351 equals(div4.lastElementChild, div5);
352
353 tearDown();
354 });
355
356 test('Test InsertElementCommand', function() {
357 expect(20);
358
359 setUp();
360
361 var insert = new svgedit.history.InsertElementCommand(div3);
362 ok(insert.unapply);
363 ok(insert.apply);
364 equals(typeof insert.unapply, typeof function(){});
365 equals(typeof insert.apply, typeof function(){});
366
367 insert.unapply();
368 equals(divparent.childElementCount, 2);
369 equals(divparent.firstElementChild, div1);
370 equals(div1.nextElementSibling, div2);
371 equals(divparent.lastElementChild, div2);
372
373 insert.apply();
374 equals(divparent.childElementCount, 3);
375 equals(divparent.firstElementChild, div1);
376 equals(div1.nextElementSibling, div2);
377 equals(div2.nextElementSibling, div3);
378
379 insert = new svgedit.history.InsertElementCommand(div2);
380
381 insert.unapply();
382 equals(divparent.childElementCount, 2);
383 equals(divparent.firstElementChild, div1);
384 equals(div1.nextElementSibling, div3);
385 equals(divparent.lastElementChild, div3);
386
387 insert.apply();
388 equals(divparent.childElementCount, 3);
389 equals(divparent.firstElementChild, div1);
390 equals(div1.nextElementSibling, div2);
391 equals(div2.nextElementSibling, div3);
392
393 tearDown();
394 });
395
396 test('Test RemoveElementCommand', function() {
397 expect(22);
398
399 setUp();
400
401 var div6 = document.createElement('div');
402 div6.id = 'div6';
403
404 var remove = new svgedit.history.RemoveElementCommand(div6, null, divparent);
405 ok(remove.unapply);
406 ok(remove.apply);
407 equals(typeof remove.unapply, typeof function(){});
408 equals(typeof remove.apply, typeof function(){});
409
410 remove.unapply();
411 equals(divparent.childElementCount, 4);
412 equals(divparent.firstElementChild, div1);
413 equals(div1.nextElementSibling, div2);
414 equals(div2.nextElementSibling, div3);
415 equals(div3.nextElementSibling, div6);
416
417 remove.apply();
418 equals(divparent.childElementCount, 3);
419 equals(divparent.firstElementChild, div1);
420 equals(div1.nextElementSibling, div2);
421 equals(div2.nextElementSibling, div3);
422
423 remove = new svgedit.history.RemoveElementCommand(div6, div2, divparent);
424
425 remove.unapply();
426 equals(divparent.childElementCount, 4);
427 equals(divparent.firstElementChild, div1);
428 equals(div1.nextElementSibling, div6);
429 equals(div6.nextElementSibling, div2);
430 equals(div2.nextElementSibling, div3);
431
432 remove.apply();
433 equals(divparent.childElementCount, 3);
434 equals(divparent.firstElementChild, div1);
435 equals(div1.nextElementSibling, div2);
436 equals(div2.nextElementSibling, div3);
437
438 tearDown();
439 });
440
441 test('Test ChangeElementCommand', function() {
442 expect(26);
443
444 setUp();
445
446 div1.setAttribute('title', 'new title');
447 var change = new svgedit.history.ChangeElementCommand(div1,
448 {'title': 'old title', 'class': 'foo'});
449 ok(change.unapply);
450 ok(change.apply);
451 equals(typeof change.unapply, typeof function(){});
452 equals(typeof change.apply, typeof function(){});
453
454 change.unapply();
455 equals(div1.getAttribute('title'), 'old title');
456 equals(div1.getAttribute('class'), 'foo');
457
458 change.apply();
459 equals(div1.getAttribute('title'), 'new title');
460 ok(!div1.getAttribute('class'));
461
462 div1.textContent = 'inner text';
463 change = new svgedit.history.ChangeElementCommand(div1,
464 {'#text': null});
465
466 change.unapply();
467 ok(!div1.textContent);
468
469 change.apply();
470 equals(div1.textContent, 'inner text');
471
472 div1.textContent = '';
473 change = new svgedit.history.ChangeElementCommand(div1,
474 {'#text': 'old text'});
475
476 change.unapply();
477 equals(div1.textContent, 'old text');
478
479 change.apply();
480 ok(!div1.textContent);
481
482 // TODO(codedread): Refactor this #href stuff in history.js and svgcanvas.js
483 var rect = document.createElementNS(svgns, 'rect');
484 var justCalled = null;
485 var gethrefvalue = null;
486 var sethrefvalue = null;
487 svgedit.utilities.getHref = function(elem) {
488 equals(elem, rect);
489 justCalled = 'getHref';
490 return gethrefvalue;
491 };
492 svgedit.utilities.setHref = function(elem, val) {
493 equals(elem, rect);
494 equals(val, sethrefvalue);
495 justCalled = 'setHref';
496 };
497
498 gethrefvalue = '#newhref';
499 change = new svgedit.history.ChangeElementCommand(rect,
500 {'#href': '#oldhref'});
501 equals(justCalled, 'getHref');
502
503 justCalled = null;
504 sethrefvalue = '#oldhref';
505 change.unapply();
506 equals(justCalled, 'setHref');
507
508 justCalled = null;
509 sethrefvalue = '#newhref';
510 change.apply();
511 equals(justCalled, 'setHref');
512
513 var line = document.createElementNS(svgns,'line');
514 line.setAttributeNS(null, 'class', 'newClass');
515 change = new svgedit.history.ChangeElementCommand(line,{class:'oldClass'});
516
517 ok(change.unapply);
518 ok(change.apply);
519 equals(typeof change.unapply, typeof function(){});
520 equals(typeof change.apply, typeof function(){});
521
522 change.unapply();
523 equals(line.getAttributeNS(null, 'class'), 'oldClass');
524
525 change.apply();
526 equals(line.getAttributeNS(null, 'class'), 'newClass');
527
528 tearDown();
529 });
530
531 test('Test BatchCommand', function() {
532 expect(13);
533
534 setUp();
535
536 var concatResult = '';
537 MockCommand.prototype.apply = function() { concatResult += this.text_; };
538
539 var batch = new svgedit.history.BatchCommand();
540 ok(batch.unapply);
541 ok(batch.apply);
542 ok(batch.addSubCommand);
543 ok(batch.isEmpty);
544 equals(typeof batch.unapply, typeof function(){});
545 equals(typeof batch.apply, typeof function(){});
546 equals(typeof batch.addSubCommand, typeof function(){});
547 equals(typeof batch.isEmpty, typeof function(){});
548
549 ok(batch.isEmpty());
550
551 batch.addSubCommand(new MockCommand('a'));
552 ok(!batch.isEmpty());
553 batch.addSubCommand(new MockCommand('b'));
554 batch.addSubCommand(new MockCommand('c'));
555
556 ok(!concatResult);
557 batch.apply();
558 equals(concatResult, 'abc');
559
560 MockCommand.prototype.apply = function() {};
561 MockCommand.prototype.unapply = function() { concatResult += this.text_; };
562 concatResult = '';
563 batch.unapply();
564 equals(concatResult, 'cba');
565
566 MockCommand.prototype.unapply = function() {};
567
568 tearDown();
569 });
570
571
572 });
573 </script>
574</head>
575<body>
576 <h1 id='qunit-header'>Unit Tests for history.js</h1>
577 <h2 id='qunit-banner'></h2>
578 <h2 id='qunit-userAgent'></h2>
579 <ol id='qunit-tests'>
580 </ol>
581 <div id='divparent' style='visibility:hidden'>
582 <div id='div1'></div>
583 <div id='div2'></div>
584 <div id='div3'></div>
585 </div>
586 <div id='div4' style='visibility:hidden'>
587 <div id='div5'></div>
588 </div>
589
590</body>
591</html>
Note: See TracBrowser for help on using the repository browser.