c0b0796844c7d54d81eba20fd330aa6f7362e35d
[WebKit-https.git] / LayoutTests / inspector / model / color.html
1 <!doctype html>
2 <html>
3 <head>
4 <script src="../../http/tests/inspector/resources/inspector-test.js"></script>
5 <script>
6 function test()
7 {
8     function formatToString(format) {
9         switch (format) {
10         case WebInspector.Color.Format.Original:
11             return "Original";
12         case WebInspector.Color.Format.Nickname:
13             return "Nickname";
14         case WebInspector.Color.Format.HEX:
15             return "HEX";
16         case WebInspector.Color.Format.ShortHEX:
17             return "Short HEX";
18         case WebInspector.Color.Format.HEXAlpha:
19             return "HEX with Alpha";
20         case WebInspector.Color.Format.ShortHEXAlpha:
21             return "Short HEX with Alpha";
22         case WebInspector.Color.Format.RGB:
23             return "RGB";
24         case WebInspector.Color.Format.RGBA:
25             return "RGBA";
26         case WebInspector.Color.Format.HSL:
27             return "HSL";
28         case WebInspector.Color.Format.HSLA:
29             return "HSLA";
30         default:
31             return "Unexpected format";
32         }
33     }
34
35     let suite = InspectorTest.createAsyncSuite("WebInspector.Color");
36
37     suite.addTestCase({
38         name: "WebInspector.Color.fromString",
39         description: "Test we can detect colors from strings.",
40         test: (resolve, reject) => {
41             function testGood(string, expectedFormat) {
42                 let color = WebInspector.Color.fromString(string);
43                 InspectorTest.expectThat(color instanceof WebInspector.Color, `'${string}' should be detected`);
44                 InspectorTest.expectThat(color && color.format === expectedFormat, `'${string}' was the expected '${formatToString(expectedFormat)}' format`);
45             }
46
47             function testBad(string) {
48                 let color = WebInspector.Color.fromString(string);
49                 InspectorTest.expectThat(color === null, `'${string}' should not be detected`);
50                 if (color) InspectorTest.log(`'${string}' detected with format '${formatToString(color.format)}'`);
51             }
52
53             testGood("#000", WebInspector.Color.Format.ShortHEX);
54             testGood("#a0A", WebInspector.Color.Format.ShortHEX);
55             testGood("#000000", WebInspector.Color.Format.HEX);
56             testGood("#a0Aa0A", WebInspector.Color.Format.HEX);
57
58             testGood("#0000", WebInspector.Color.Format.ShortHEXAlpha);
59             testGood("#a0Af", WebInspector.Color.Format.ShortHEXAlpha);
60             testGood("#00000000", WebInspector.Color.Format.HEXAlpha);
61             testGood("#a0Aa0Aff", WebInspector.Color.Format.HEXAlpha);
62
63             testGood("rgb(1,2,3)", WebInspector.Color.Format.RGB);
64             testGood("RGB(1,2,3)", WebInspector.Color.Format.RGB);
65             testGood("rgb(999, 999, 999)", WebInspector.Color.Format.RGB);
66             testGood("rgb( 1 , 1 , 1 )", WebInspector.Color.Format.RGB);
67
68             testGood("rgba(1,2,3,0)", WebInspector.Color.Format.RGBA);
69             testGood("RGBA(1,2,3,0)", WebInspector.Color.Format.RGBA);
70             testGood("rgba(999, 999, 999, 999)", WebInspector.Color.Format.RGBA);
71             testGood("rgba( 1 , 1 , 1 , 0.5 )", WebInspector.Color.Format.RGBA);
72
73             testGood("hsl(0, 0%, 50%)", WebInspector.Color.Format.HSL);
74             testGood("HSL(0, 0%, 50%)", WebInspector.Color.Format.HSL);
75             testGood("hsl(999, 999%, 999%)", WebInspector.Color.Format.HSL);
76             testGood("hsl( 0 , 0% , 50% )", WebInspector.Color.Format.HSL);
77             
78             testGood("hsla(0, 0%, 50%, 0)", WebInspector.Color.Format.HSLA);
79             testGood("HSLA(0, 0%, 50%, 0)", WebInspector.Color.Format.HSLA);
80             testGood("hsla(999, 999%, 999%, 999)", WebInspector.Color.Format.HSLA);
81             testGood("hsla( 0 , 0% , 50% , 0.5 )", WebInspector.Color.Format.HSLA);
82
83             testGood("blue", WebInspector.Color.Format.Nickname);
84             testGood("BLuE", WebInspector.Color.Format.Nickname);
85             testGood("midnightblue", WebInspector.Color.Format.Nickname);
86             testGood("royalblue", WebInspector.Color.Format.Nickname);
87             testGood("steelblue", WebInspector.Color.Format.Nickname);
88             testGood("transparent", WebInspector.Color.Format.Nickname);
89
90             InspectorTest.log("");
91
92             testBad(" #000 "); // whitespace
93             testBad("#rgb"); // bad hex
94             testBad("#1"); // 1
95             testBad("#12"); // 2
96             testBad("#12345"); // 5
97             testBad("#1234567"); // 7
98             testBad("#123456789"); // 9
99             testBad("rgb(255, 255, 255, 0.5)"); // extra values
100             testBad("rgba(255, 255, 255, 0.5, 1)"); // extra values
101             testBad("hsl(0, 0%, 50%, 1)"); // extra value
102             testBad("hsla(0, 0%, 50%, 1, 2)"); // extra values
103             testBad("superblue"); // not a nickname
104
105             // FIXME: currentColor?
106             // FIXME: Should we consider missing %s as bad? Currently we just strip them.
107             // testBad("hsl(0, 0, 50)"); // missing %s
108             // testBad("hsla(0, 0, 50, 1)"); // missing %s
109
110             resolve();
111         }
112     });
113
114     suite.addTestCase({
115         name: "WebInspector.Color properties",
116         description: "Test different color properties.",
117         test: (resolve, reject) => {
118             function shallowEqual(arr1, arr2) {
119                 if (arr1.length !== arr2.length)
120                     return false;
121
122                 for (let i = 0; i < arr1.length; ++i) {
123                     if (arr1[i] !== arr2[i])
124                         return false;
125                 }
126
127                 return true;
128             }
129
130             let color;
131
132             color = WebInspector.Color.fromString("red");
133             InspectorTest.expectThat(color.alpha === 1, "'red' should have alpha of 1.");
134             InspectorTest.expectThat(color.simple === true, "'red' should be simple.");
135             InspectorTest.expectThat(shallowEqual(color.rgb, [255, 0, 0]), "'red' has rgb of [255, 0, 0].");
136             InspectorTest.expectThat(shallowEqual(color.rgba, [255, 0, 0, 1]), "'red' has rgba of [255, 0, 0, 1].");
137             InspectorTest.expectThat(shallowEqual(color.hsl, [0, 100, 50]), "'red' has hsl of [0, 100, 50].");
138             InspectorTest.expectThat(shallowEqual(color.hsla, [0, 100, 50, 1]), "'red' has hsla of [0, 100, 50, 1].");
139
140             color = WebInspector.Color.fromString("transparent");
141             InspectorTest.expectThat(color.alpha === 0, "'transparent' should have alpha of 0.");
142             InspectorTest.expectThat(color.simple === false, "'transparent' should be not be simple.");
143             InspectorTest.expectThat(shallowEqual(color.rgb, [0, 0, 0]), "'transparent' has rgb of [0, 0, 0].");
144             InspectorTest.expectThat(shallowEqual(color.rgba, [0, 0, 0, 0]), "'transparent' has rgba of [0, 0, 0, 0].");
145             InspectorTest.expectThat(shallowEqual(color.hsl, [0, 0, 0]), "'transparent' has hsl of [0, 0, 0].");
146             InspectorTest.expectThat(shallowEqual(color.hsla, [0, 0, 0, 0]), "'transparent' has hsla of [0, 0, 0, 0].");
147
148             resolve();
149         }
150     });
151
152     suite.addTestCase({
153         name: "WebInspector.Color.prototype.nextFormat",
154         description: "Test we can cycle through color formats for different colors.",
155         test: (resolve, reject) => {
156             function test(string, phases) {
157                 let color = WebInspector.Color.fromString(string);
158                 color.format = WebInspector.Color.Format.Original;
159
160                 let pass = true;
161                 for (let expectedNextFormat of phases) {
162                     let nextFormat = color.nextFormat();
163                     InspectorTest.assert(nextFormat === expectedNextFormat, `Next format '${formatToString(nextFormat)}' was not the expected '${formatToString(expectedNextFormat)}'`);
164                     pass = pass && nextFormat === expectedNextFormat;
165                     color.format = nextFormat;
166                 }
167
168                 InspectorTest.expectThat(pass, `All format phases of '${string}' should be as expected.`);
169             }
170
171             // All with alpha.
172             test("transparent", [
173                 WebInspector.Color.Format.RGBA,
174                 WebInspector.Color.Format.HSLA,
175                 WebInspector.Color.Format.Nickname,
176                 WebInspector.Color.Format.ShortHEXAlpha,
177                 WebInspector.Color.Format.HEXAlpha,
178                 WebInspector.Color.Format.Original,
179             ]);
180
181             // All without alpha.
182             test("red", [
183                 WebInspector.Color.Format.RGB,
184                 WebInspector.Color.Format.HSL,
185                 WebInspector.Color.Format.Nickname,
186                 WebInspector.Color.Format.ShortHEX,
187                 WebInspector.Color.Format.HEX,
188                 WebInspector.Color.Format.Original,
189             ]);
190
191             // No short hex or nickname.
192             test("rgb(100, 150, 200)", [
193                 WebInspector.Color.Format.RGB,
194                 WebInspector.Color.Format.HSL,
195                 WebInspector.Color.Format.HEX,
196                 WebInspector.Color.Format.Original,
197             ]);
198
199             // No short hex alpha or nickname.
200             test("rgba(100, 150, 200, 0.5)", [
201                 WebInspector.Color.Format.RGBA,
202                 WebInspector.Color.Format.HSLA,
203                 WebInspector.Color.Format.HEXAlpha,
204                 WebInspector.Color.Format.Original,
205             ]);
206
207             resolve();
208         }
209     });
210
211     suite.addTestCase({
212         name: "WebInspector.Color.prototype.toString",
213         description: "Test the different toString outputs.",
214         test: (resolve, reject) => {
215             let color;
216             function test(expected, format) {
217                 let pass = color.toString(format) === expected;
218                 InspectorTest.expectThat(pass, `Color as '${formatToString(format)}' should be '${expected}'`);
219                 if (!pass) InspectorTest.log("WAS: " + color.toString(format));
220             }
221
222             // A color with all formats.
223             color = WebInspector.Color.fromString("RED");
224             test("RED", WebInspector.Color.Format.Original);
225             test("red", WebInspector.Color.Format.Nickname);
226             test("#f00", WebInspector.Color.Format.ShortHEX);
227             test("#ff0000", WebInspector.Color.Format.HEX);
228             test("#f00f", WebInspector.Color.Format.ShortHEXAlpha);
229             test("#ff0000ff", WebInspector.Color.Format.HEXAlpha);
230             test("rgb(255, 0, 0)", WebInspector.Color.Format.RGB);
231             test("rgba(255, 0, 0, 1)", WebInspector.Color.Format.RGBA);
232             test("hsl(0, 100%, 50%)", WebInspector.Color.Format.HSL);
233             test("hsla(0, 100%, 50%, 1)", WebInspector.Color.Format.HSLA);
234
235             // A color which cannot be some formats, those fallback to something else.
236             color = WebInspector.Color.fromString("rGbA(  100, 200, 255, 0.5  )");
237             test("rgba(100, 200, 255, 0.5)", WebInspector.Color.Format.Original); // Original text ignored for some formats.
238             test("rgba(100, 200, 255, 0.5)", WebInspector.Color.Format.Nickname); // fallback (rgba)
239             test("rgba(100, 200, 255, 0.5)", WebInspector.Color.Format.ShortHEX); // fallback (rgba)
240             test("rgba(100, 200, 255, 0.5)", WebInspector.Color.Format.HEX); // fallback (rgba)
241             test("#64c8ff80", WebInspector.Color.Format.ShortHEXAlpha); // fallback (hex alpha)
242             test("#64c8ff80", WebInspector.Color.Format.HEXAlpha);
243             test("rgba(100, 200, 255, 0.5)", WebInspector.Color.Format.RGB); // fallback (rgba)
244             test("rgba(100, 200, 255, 0.5)", WebInspector.Color.Format.RGBA);
245             test("hsla(201, 100%, 70%, 0.5)", WebInspector.Color.Format.HSL); // fallback (hsla)
246             test("hsla(201, 100%, 70%, 0.5)", WebInspector.Color.Format.HSLA);
247
248             // FIXME: Should we clamp rgb(300, 300, 300) => rgb(255, 255, 255) in toStrings?
249             // FIXME: Should we always stash the original string, no matter how poor?
250
251             resolve();
252         }
253     });
254
255     suite.runTestCasesAndFinish();
256 }
257 </script>
258 </head>
259 <body onload="runTest()">
260 <p>Tests for the WebInspector.Color model object.</p>
261 </body>
262 </html>