editor_spec.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * JavaScript code in this page
  4. *
  5. * Copyright 2022 Mozilla Foundation
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. * @licend The above is the entire license notice for the
  20. * JavaScript code in this page
  21. */
  22. "use strict";
  23. var _tools = require("../../display/editor/tools.js");
  24. var _ink = require("../../display/editor/ink.js");
  25. describe("editor", function () {
  26. describe("Command Manager", function () {
  27. it("should check undo/redo", function () {
  28. const manager = new _tools.CommandManager(4);
  29. let x = 0;
  30. const makeDoUndo = n => ({
  31. cmd: () => x += n,
  32. undo: () => x -= n
  33. });
  34. manager.add({
  35. ...makeDoUndo(1),
  36. mustExec: true
  37. });
  38. expect(x).toEqual(1);
  39. manager.add({
  40. ...makeDoUndo(2),
  41. mustExec: true
  42. });
  43. expect(x).toEqual(3);
  44. manager.add({
  45. ...makeDoUndo(3),
  46. mustExec: true
  47. });
  48. expect(x).toEqual(6);
  49. manager.undo();
  50. expect(x).toEqual(3);
  51. manager.undo();
  52. expect(x).toEqual(1);
  53. manager.undo();
  54. expect(x).toEqual(0);
  55. manager.undo();
  56. expect(x).toEqual(0);
  57. manager.redo();
  58. expect(x).toEqual(1);
  59. manager.redo();
  60. expect(x).toEqual(3);
  61. manager.redo();
  62. expect(x).toEqual(6);
  63. manager.redo();
  64. expect(x).toEqual(6);
  65. manager.undo();
  66. expect(x).toEqual(3);
  67. manager.redo();
  68. expect(x).toEqual(6);
  69. });
  70. });
  71. it("should hit the limit of the manager", function () {
  72. const manager = new _tools.CommandManager(3);
  73. let x = 0;
  74. const makeDoUndo = n => ({
  75. cmd: () => x += n,
  76. undo: () => x -= n
  77. });
  78. manager.add({
  79. ...makeDoUndo(1),
  80. mustExec: true
  81. });
  82. manager.add({
  83. ...makeDoUndo(2),
  84. mustExec: true
  85. });
  86. manager.add({
  87. ...makeDoUndo(3),
  88. mustExec: true
  89. });
  90. manager.add({
  91. ...makeDoUndo(4),
  92. mustExec: true
  93. });
  94. expect(x).toEqual(10);
  95. manager.undo();
  96. manager.undo();
  97. expect(x).toEqual(3);
  98. manager.undo();
  99. expect(x).toEqual(1);
  100. manager.undo();
  101. expect(x).toEqual(1);
  102. manager.redo();
  103. manager.redo();
  104. expect(x).toEqual(6);
  105. manager.add({
  106. ...makeDoUndo(5),
  107. mustExec: true
  108. });
  109. expect(x).toEqual(11);
  110. });
  111. describe("fitCurve", function () {
  112. it("should return a function", function () {
  113. expect(typeof _ink.fitCurve).toEqual("function");
  114. });
  115. it("should compute an Array of bezier curves", function () {
  116. const bezier = (0, _ink.fitCurve)([[1, 2], [4, 5]], 30, null);
  117. expect(bezier).toEqual([[[1, 2], [2, 3], [3, 4], [4, 5]]]);
  118. });
  119. });
  120. });