summaryrefslogtreecommitdiffstats
path: root/devtools/client/shared/test/xpcshell/test_undoStack.js
blob: dafb00712022f973c7f174d8486f343dd7011d7f (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

const { UndoStack } = require("resource://devtools/client/shared/undo.js");

const MAX_SIZE = 5;

function run_test() {
  let str = "";
  const stack = new UndoStack(MAX_SIZE);

  function add(ch) {
    stack.do(
      function () {
        str += ch;
      },
      function () {
        str = str.slice(0, -1);
      }
    );
  }

  Assert.ok(!stack.canUndo());
  Assert.ok(!stack.canRedo());

  // Check adding up to the limit of the size
  add("a");
  Assert.ok(stack.canUndo());
  Assert.ok(!stack.canRedo());

  add("b");
  add("c");
  add("d");
  add("e");

  Assert.equal(str, "abcde");

  // Check a simple undo+redo
  stack.undo();

  Assert.equal(str, "abcd");
  Assert.ok(stack.canRedo());

  stack.redo();
  Assert.equal(str, "abcde");
  Assert.ok(!stack.canRedo());

  // Check an undo followed by a new action
  stack.undo();
  Assert.equal(str, "abcd");

  add("q");
  Assert.equal(str, "abcdq");
  Assert.ok(!stack.canRedo());

  stack.undo();
  Assert.equal(str, "abcd");
  stack.redo();
  Assert.equal(str, "abcdq");

  // Revert back to the beginning of the queue...
  while (stack.canUndo()) {
    stack.undo();
  }
  Assert.equal(str, "");

  // Now put it all back....
  while (stack.canRedo()) {
    stack.redo();
  }
  Assert.equal(str, "abcdq");

  // Now go over the undo limit...
  add("1");
  add("2");
  add("3");

  Assert.equal(str, "abcdq123");

  // And now undoing the whole stack should only undo 5 actions.
  while (stack.canUndo()) {
    stack.undo();
  }

  Assert.equal(str, "abc");
}