editor_spec.js 3.0 KB

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