test_undoStack.js (1649B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 const { UndoStack } = require("resource://devtools/client/shared/undo.js"); 7 8 const MAX_SIZE = 5; 9 10 function run_test() { 11 let str = ""; 12 const stack = new UndoStack(MAX_SIZE); 13 14 function add(ch) { 15 stack.do( 16 function () { 17 str += ch; 18 }, 19 function () { 20 str = str.slice(0, -1); 21 } 22 ); 23 } 24 25 Assert.ok(!stack.canUndo()); 26 Assert.ok(!stack.canRedo()); 27 28 // Check adding up to the limit of the size 29 add("a"); 30 Assert.ok(stack.canUndo()); 31 Assert.ok(!stack.canRedo()); 32 33 add("b"); 34 add("c"); 35 add("d"); 36 add("e"); 37 38 Assert.equal(str, "abcde"); 39 40 // Check a simple undo+redo 41 stack.undo(); 42 43 Assert.equal(str, "abcd"); 44 Assert.ok(stack.canRedo()); 45 46 stack.redo(); 47 Assert.equal(str, "abcde"); 48 Assert.ok(!stack.canRedo()); 49 50 // Check an undo followed by a new action 51 stack.undo(); 52 Assert.equal(str, "abcd"); 53 54 add("q"); 55 Assert.equal(str, "abcdq"); 56 Assert.ok(!stack.canRedo()); 57 58 stack.undo(); 59 Assert.equal(str, "abcd"); 60 stack.redo(); 61 Assert.equal(str, "abcdq"); 62 63 // Revert back to the beginning of the queue... 64 while (stack.canUndo()) { 65 stack.undo(); 66 } 67 Assert.equal(str, ""); 68 69 // Now put it all back.... 70 while (stack.canRedo()) { 71 stack.redo(); 72 } 73 Assert.equal(str, "abcdq"); 74 75 // Now go over the undo limit... 76 add("1"); 77 add("2"); 78 add("3"); 79 80 Assert.equal(str, "abcdq123"); 81 82 // And now undoing the whole stack should only undo 5 actions. 83 while (stack.canUndo()) { 84 stack.undo(); 85 } 86 87 Assert.equal(str, "abc"); 88 }