summaryrefslogtreecommitdiffstats
path: root/devtools/server/tests/xpcshell/test_shapes_highlighter_helpers.js
blob: 6876f0a53255c84d63243a0145d15629bd725dbb (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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

/**
 * Test the helper functions of the shapes highlighter.
 */

"use strict";

const {
  splitCoords,
  coordToPercent,
  evalCalcExpression,
  shapeModeToCssPropertyName,
  getCirclePath,
  getDecimalPrecision,
  getUnit,
} = require("resource://devtools/server/actors/highlighters/shapes.js");

function run_test() {
  test_split_coords();
  test_coord_to_percent();
  test_eval_calc_expression();
  test_shape_mode_to_css_property_name();
  test_get_circle_path();
  test_get_decimal_precision();
  test_get_unit();
  run_next_test();
}

function test_split_coords() {
  const tests = [
    {
      desc: "splitCoords for basic coordinate pair",
      expr: "30% 20%",
      expected: ["30%", "20%"],
    },
    {
      desc: "splitCoords for coord pair with calc()",
      expr: "calc(50px + 20%) 30%",
      expected: ["calc(50px\u00a0+\u00a020%)", "30%"],
    },
  ];

  for (const { desc, expr, expected } of tests) {
    deepEqual(splitCoords(expr), expected, desc);
  }
}

function test_coord_to_percent() {
  const size = 1000;
  const tests = [
    {
      desc: "coordToPercent for percent value",
      expr: "50%",
      expected: 50,
    },
    {
      desc: "coordToPercent for px value",
      expr: "500px",
      expected: 50,
    },
    {
      desc: "coordToPercent for zero value",
      expr: "0",
      expected: 0,
    },
  ];

  for (const { desc, expr, expected } of tests) {
    equal(coordToPercent(expr, size), expected, desc);
  }
}

function test_eval_calc_expression() {
  const size = 1000;
  const tests = [
    {
      desc: "evalCalcExpression with one value",
      expr: "50%",
      expected: 50,
    },
    {
      desc: "evalCalcExpression with percent and px values",
      expr: "50% + 100px",
      expected: 60,
    },
    {
      desc: "evalCalcExpression with a zero value",
      expr: "0 + 100px",
      expected: 10,
    },
    {
      desc: "evalCalcExpression with a negative value",
      expr: "-200px+50%",
      expected: 30,
    },
  ];

  for (const { desc, expr, expected } of tests) {
    equal(evalCalcExpression(expr, size), expected, desc);
  }
}

function test_shape_mode_to_css_property_name() {
  const tests = [
    {
      desc: "shapeModeToCssPropertyName for clip-path",
      expr: "cssClipPath",
      expected: "clipPath",
    },
    {
      desc: "shapeModeToCssPropertyName for shape-outside",
      expr: "cssShapeOutside",
      expected: "shapeOutside",
    },
  ];

  for (const { desc, expr, expected } of tests) {
    equal(shapeModeToCssPropertyName(expr), expected, desc);
  }
}

function test_get_circle_path() {
  const tests = [
    {
      desc: "getCirclePath with size 5, no resizing, no zoom, 1:1 ratio",
      size: 5,
      cx: 0,
      cy: 0,
      width: 100,
      height: 100,
      zoom: 1,
      expected: "M-5,0a5,5 0 1,0 10,0a5,5 0 1,0 -10,0",
    },
    {
      desc: "getCirclePath with size 7, resizing, no zoom, 1:1 ratio",
      size: 7,
      cx: 0,
      cy: 0,
      width: 200,
      height: 200,
      zoom: 1,
      expected: "M-3.5,0a3.5,3.5 0 1,0 7,0a3.5,3.5 0 1,0 -7,0",
    },
    {
      desc: "getCirclePath with size 5, resizing, zoom, 1:1 ratio",
      size: 5,
      cx: 0,
      cy: 0,
      width: 200,
      height: 200,
      zoom: 2,
      expected: "M-1.25,0a1.25,1.25 0 1,0 2.5,0a1.25,1.25 0 1,0 -2.5,0",
    },
    {
      desc: "getCirclePath with size 5, resizing, zoom, non-square ratio",
      size: 5,
      cx: 0,
      cy: 0,
      width: 100,
      height: 200,
      zoom: 2,
      expected: "M-2.5,0a2.5,1.25 0 1,0 5,0a2.5,1.25 0 1,0 -5,0",
    },
  ];

  for (const { desc, size, cx, cy, width, height, zoom, expected } of tests) {
    equal(getCirclePath(size, cx, cy, width, height, zoom), expected, desc);
  }
}

function test_get_decimal_precision() {
  const tests = [
    {
      desc: "getDecimalPrecision with px",
      expr: "px",
      expected: 0,
    },
    {
      desc: "getDecimalPrecision with %",
      expr: "%",
      expected: 2,
    },
    {
      desc: "getDecimalPrecision with em",
      expr: "em",
      expected: 2,
    },
    {
      desc: "getDecimalPrecision with undefined",
      expr: undefined,
      expected: 0,
    },
    {
      desc: "getDecimalPrecision with empty string",
      expr: "",
      expected: 0,
    },
  ];

  for (const { desc, expr, expected } of tests) {
    equal(getDecimalPrecision(expr), expected, desc);
  }
}

function test_get_unit() {
  const tests = [
    {
      desc: "getUnit with %",
      expr: "30%",
      expected: "%",
    },
    {
      desc: "getUnit with px",
      expr: "400px",
      expected: "px",
    },
    {
      desc: "getUnit with em",
      expr: "4em",
      expected: "em",
    },
    {
      desc: "getUnit with 0",
      expr: "0",
      expected: "px",
    },
    {
      desc: "getUnit with 0%",
      expr: "0%",
      expected: "%",
    },
    {
      desc: "getUnit with 0.00%",
      expr: "0.00%",
      expected: "%",
    },
    {
      desc: "getUnit with 0px",
      expr: "0px",
      expected: "px",
    },
    {
      desc: "getUnit with 0em",
      expr: "0em",
      expected: "em",
    },
    {
      desc: "getUnit with calc",
      expr: "calc(30px + 5%)",
      expected: "px",
    },
    {
      desc: "getUnit with var",
      expr: "var(--variable)",
      expected: "px",
    },
    {
      desc: "getUnit with closest-side",
      expr: "closest-side",
      expected: "px",
    },
    {
      desc: "getUnit with farthest-side",
      expr: "farthest-side",
      expected: "px",
    },
  ];

  for (const { desc, expr, expected } of tests) {
    equal(getUnit(expr), expected, desc);
  }
}