test_cubicBezier.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /* -*- Mode: Javascript; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* Any copyright is dedicated to the Public Domain.
  3. http://creativecommons.org/publicdomain/zero/1.0/ */
  4. "use strict";
  5. // Tests the CubicBezier API in the CubicBezierWidget module
  6. var Cu = Components.utils;
  7. var {require} = Cu.import("resource://devtools/shared/Loader.jsm", {});
  8. var {CubicBezier, _parseTimingFunction} = require("devtools/client/shared/widgets/CubicBezierWidget");
  9. function run_test() {
  10. throwsWhenMissingCoordinates();
  11. throwsWhenIncorrectCoordinates();
  12. convertsStringCoordinates();
  13. coordinatesToStringOutputsAString();
  14. pointGettersReturnPointCoordinatesArrays();
  15. toStringOutputsCubicBezierValue();
  16. toStringOutputsCssPresetValues();
  17. testParseTimingFunction();
  18. }
  19. function throwsWhenMissingCoordinates() {
  20. do_check_throws(() => {
  21. new CubicBezier();
  22. }, "Throws an exception when coordinates are missing");
  23. }
  24. function throwsWhenIncorrectCoordinates() {
  25. do_check_throws(() => {
  26. new CubicBezier([]);
  27. }, "Throws an exception when coordinates are incorrect (empty array)");
  28. do_check_throws(() => {
  29. new CubicBezier([0, 0]);
  30. }, "Throws an exception when coordinates are incorrect (incomplete array)");
  31. do_check_throws(() => {
  32. new CubicBezier(["a", "b", "c", "d"]);
  33. }, "Throws an exception when coordinates are incorrect (invalid type)");
  34. do_check_throws(() => {
  35. new CubicBezier([1.5, 0, 1.5, 0]);
  36. }, "Throws an exception when coordinates are incorrect (time range invalid)");
  37. do_check_throws(() => {
  38. new CubicBezier([-0.5, 0, -0.5, 0]);
  39. }, "Throws an exception when coordinates are incorrect (time range invalid)");
  40. }
  41. function convertsStringCoordinates() {
  42. do_print("Converts string coordinates to numbers");
  43. let c = new CubicBezier(["0", "1", ".5", "-2"]);
  44. do_check_eq(c.coordinates[0], 0);
  45. do_check_eq(c.coordinates[1], 1);
  46. do_check_eq(c.coordinates[2], .5);
  47. do_check_eq(c.coordinates[3], -2);
  48. }
  49. function coordinatesToStringOutputsAString() {
  50. do_print("coordinates.toString() outputs a string representation");
  51. let c = new CubicBezier(["0", "1", "0.5", "-2"]);
  52. let string = c.coordinates.toString();
  53. do_check_eq(string, "0,1,.5,-2");
  54. c = new CubicBezier([1, 1, 1, 1]);
  55. string = c.coordinates.toString();
  56. do_check_eq(string, "1,1,1,1");
  57. }
  58. function pointGettersReturnPointCoordinatesArrays() {
  59. do_print("Points getters return arrays of coordinates");
  60. let c = new CubicBezier([0, .2, .5, 1]);
  61. do_check_eq(c.P1[0], 0);
  62. do_check_eq(c.P1[1], .2);
  63. do_check_eq(c.P2[0], .5);
  64. do_check_eq(c.P2[1], 1);
  65. }
  66. function toStringOutputsCubicBezierValue() {
  67. do_print("toString() outputs the cubic-bezier() value");
  68. let c = new CubicBezier([0, 1, 1, 0]);
  69. do_check_eq(c.toString(), "cubic-bezier(0,1,1,0)");
  70. }
  71. function toStringOutputsCssPresetValues() {
  72. do_print("toString() outputs the css predefined values");
  73. let c = new CubicBezier([0, 0, 1, 1]);
  74. do_check_eq(c.toString(), "linear");
  75. c = new CubicBezier([0.25, 0.1, 0.25, 1]);
  76. do_check_eq(c.toString(), "ease");
  77. c = new CubicBezier([0.42, 0, 1, 1]);
  78. do_check_eq(c.toString(), "ease-in");
  79. c = new CubicBezier([0, 0, 0.58, 1]);
  80. do_check_eq(c.toString(), "ease-out");
  81. c = new CubicBezier([0.42, 0, 0.58, 1]);
  82. do_check_eq(c.toString(), "ease-in-out");
  83. }
  84. function testParseTimingFunction() {
  85. do_print("test parseTimingFunction");
  86. for (let test of ["ease", "linear", "ease-in", "ease-out", "ease-in-out"]) {
  87. ok(_parseTimingFunction(test), test);
  88. }
  89. ok(!_parseTimingFunction("something"), "non-function token");
  90. ok(!_parseTimingFunction("something()"), "non-cubic-bezier function");
  91. ok(!_parseTimingFunction("cubic-bezier(something)",
  92. "cubic-bezier with non-numeric argument"));
  93. ok(!_parseTimingFunction("cubic-bezier(1,2,3:7)",
  94. "did not see comma"));
  95. ok(!_parseTimingFunction("cubic-bezier(1,2,3,7:",
  96. "did not see close paren"));
  97. ok(!_parseTimingFunction("cubic-bezier(1,2", "early EOF after number"));
  98. ok(!_parseTimingFunction("cubic-bezier(1,2,", "early EOF after comma"));
  99. deepEqual(_parseTimingFunction("cubic-bezier(1,2,3,7)"), [1, 2, 3, 7],
  100. "correct invocation");
  101. deepEqual(_parseTimingFunction("cubic-bezier(1, /* */ 2,3, 7 )"),
  102. [1, 2, 3, 7],
  103. "correct with comments and whitespace");
  104. }
  105. function do_check_throws(cb, info) {
  106. do_print(info);
  107. let hasThrown = false;
  108. try {
  109. cb();
  110. } catch (e) {
  111. hasThrown = true;
  112. }
  113. do_check_true(hasThrown);
  114. }