test_undoStack.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
  2. /* Any copyright is dedicated to the Public Domain.
  3. http://creativecommons.org/publicdomain/zero/1.0/ */
  4. "use strict";
  5. const {Loader} = Components.utils.import("resource://gre/modules/commonjs/toolkit/loader.js", {});
  6. const loader = new Loader.Loader({
  7. paths: {
  8. "": "resource://gre/modules/commonjs/",
  9. "devtools": "resource://devtools",
  10. },
  11. globals: {},
  12. });
  13. const require = Loader.Require(loader, { id: "undo-test" });
  14. const {UndoStack} = require("devtools/client/shared/undo");
  15. const MAX_SIZE = 5;
  16. function run_test() {
  17. let str = "";
  18. let stack = new UndoStack(MAX_SIZE);
  19. function add(ch) {
  20. stack.do(function () {
  21. str += ch;
  22. }, function () {
  23. str = str.slice(0, -1);
  24. });
  25. }
  26. do_check_false(stack.canUndo());
  27. do_check_false(stack.canRedo());
  28. // Check adding up to the limit of the size
  29. add("a");
  30. do_check_true(stack.canUndo());
  31. do_check_false(stack.canRedo());
  32. add("b");
  33. add("c");
  34. add("d");
  35. add("e");
  36. do_check_eq(str, "abcde");
  37. // Check a simple undo+redo
  38. stack.undo();
  39. do_check_eq(str, "abcd");
  40. do_check_true(stack.canRedo());
  41. stack.redo();
  42. do_check_eq(str, "abcde");
  43. do_check_false(stack.canRedo());
  44. // Check an undo followed by a new action
  45. stack.undo();
  46. do_check_eq(str, "abcd");
  47. add("q");
  48. do_check_eq(str, "abcdq");
  49. do_check_false(stack.canRedo());
  50. stack.undo();
  51. do_check_eq(str, "abcd");
  52. stack.redo();
  53. do_check_eq(str, "abcdq");
  54. // Revert back to the beginning of the queue...
  55. while (stack.canUndo()) {
  56. stack.undo();
  57. }
  58. do_check_eq(str, "");
  59. // Now put it all back....
  60. while (stack.canRedo()) {
  61. stack.redo();
  62. }
  63. do_check_eq(str, "abcdq");
  64. // Now go over the undo limit...
  65. add("1");
  66. add("2");
  67. add("3");
  68. do_check_eq(str, "abcdq123");
  69. // And now undoing the whole stack should only undo 5 actions.
  70. while (stack.canUndo()) {
  71. stack.undo();
  72. }
  73. do_check_eq(str, "abc");
  74. }