summaryrefslogtreecommitdiffstats
path: root/devtools/client/shared/test/unit/test_cubicBezier.js
blob: 050f6d22671e796765614fc5be7539556bab8f2b (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/* -*- Mode: Javascript; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

// Tests the CubicBezier API in the CubicBezierWidget module

var Cu = Components.utils;
var {require} = Cu.import("resource://devtools/shared/Loader.jsm", {});
var {CubicBezier, _parseTimingFunction} = require("devtools/client/shared/widgets/CubicBezierWidget");

function run_test() {
  throwsWhenMissingCoordinates();
  throwsWhenIncorrectCoordinates();
  convertsStringCoordinates();
  coordinatesToStringOutputsAString();
  pointGettersReturnPointCoordinatesArrays();
  toStringOutputsCubicBezierValue();
  toStringOutputsCssPresetValues();
  testParseTimingFunction();
}

function throwsWhenMissingCoordinates() {
  do_check_throws(() => {
    new CubicBezier();
  }, "Throws an exception when coordinates are missing");
}

function throwsWhenIncorrectCoordinates() {
  do_check_throws(() => {
    new CubicBezier([]);
  }, "Throws an exception when coordinates are incorrect (empty array)");

  do_check_throws(() => {
    new CubicBezier([0, 0]);
  }, "Throws an exception when coordinates are incorrect (incomplete array)");

  do_check_throws(() => {
    new CubicBezier(["a", "b", "c", "d"]);
  }, "Throws an exception when coordinates are incorrect (invalid type)");

  do_check_throws(() => {
    new CubicBezier([1.5, 0, 1.5, 0]);
  }, "Throws an exception when coordinates are incorrect (time range invalid)");

  do_check_throws(() => {
    new CubicBezier([-0.5, 0, -0.5, 0]);
  }, "Throws an exception when coordinates are incorrect (time range invalid)");
}

function convertsStringCoordinates() {
  do_print("Converts string coordinates to numbers");
  let c = new CubicBezier(["0", "1", ".5", "-2"]);

  do_check_eq(c.coordinates[0], 0);
  do_check_eq(c.coordinates[1], 1);
  do_check_eq(c.coordinates[2], .5);
  do_check_eq(c.coordinates[3], -2);
}

function coordinatesToStringOutputsAString() {
  do_print("coordinates.toString() outputs a string representation");

  let c = new CubicBezier(["0", "1", "0.5", "-2"]);
  let string = c.coordinates.toString();
  do_check_eq(string, "0,1,.5,-2");

  c = new CubicBezier([1, 1, 1, 1]);
  string = c.coordinates.toString();
  do_check_eq(string, "1,1,1,1");
}

function pointGettersReturnPointCoordinatesArrays() {
  do_print("Points getters return arrays of coordinates");

  let c = new CubicBezier([0, .2, .5, 1]);
  do_check_eq(c.P1[0], 0);
  do_check_eq(c.P1[1], .2);
  do_check_eq(c.P2[0], .5);
  do_check_eq(c.P2[1], 1);
}

function toStringOutputsCubicBezierValue() {
  do_print("toString() outputs the cubic-bezier() value");

  let c = new CubicBezier([0, 1, 1, 0]);
  do_check_eq(c.toString(), "cubic-bezier(0,1,1,0)");
}

function toStringOutputsCssPresetValues() {
  do_print("toString() outputs the css predefined values");

  let c = new CubicBezier([0, 0, 1, 1]);
  do_check_eq(c.toString(), "linear");

  c = new CubicBezier([0.25, 0.1, 0.25, 1]);
  do_check_eq(c.toString(), "ease");

  c = new CubicBezier([0.42, 0, 1, 1]);
  do_check_eq(c.toString(), "ease-in");

  c = new CubicBezier([0, 0, 0.58, 1]);
  do_check_eq(c.toString(), "ease-out");

  c = new CubicBezier([0.42, 0, 0.58, 1]);
  do_check_eq(c.toString(), "ease-in-out");
}

function testParseTimingFunction() {
  do_print("test parseTimingFunction");

  for (let test of ["ease", "linear", "ease-in", "ease-out", "ease-in-out"]) {
    ok(_parseTimingFunction(test), test);
  }

  ok(!_parseTimingFunction("something"), "non-function token");
  ok(!_parseTimingFunction("something()"), "non-cubic-bezier function");
  ok(!_parseTimingFunction("cubic-bezier(something)",
                           "cubic-bezier with non-numeric argument"));
  ok(!_parseTimingFunction("cubic-bezier(1,2,3:7)",
                           "did not see comma"));
  ok(!_parseTimingFunction("cubic-bezier(1,2,3,7:",
                           "did not see close paren"));
  ok(!_parseTimingFunction("cubic-bezier(1,2", "early EOF after number"));
  ok(!_parseTimingFunction("cubic-bezier(1,2,", "early EOF after comma"));
  deepEqual(_parseTimingFunction("cubic-bezier(1,2,3,7)"), [1, 2, 3, 7],
            "correct invocation");
  deepEqual(_parseTimingFunction("cubic-bezier(1,  /* */ 2,3,   7  )"),
            [1, 2, 3, 7],
            "correct with comments and whitespace");
}

function do_check_throws(cb, info) {
  do_print(info);

  let hasThrown = false;
  try {
    cb();
  } catch (e) {
    hasThrown = true;
  }

  do_check_true(hasThrown);
}