Web Inspector: Don't throw exceptions in WebInspector.Color
[WebKit-https.git] / LayoutTests / inspector / styles / styles-invalid-color-values.html
1 <html>
2 <head>
3 <script src="../../http/tests/inspector/inspector-test.js"></script>
4 <script src="../../http/tests/inspector/elements-test.js"></script>
5 <script>
6 function test()
7 {
8     var colors = [
9         // Each of these is red. Some may need to be clipped to [0, 255].
10         'red',
11         '#F00',
12         'rgb(255,0,0)',
13         'rgb(300,0,0)',        // clipped to rgb(255,0,0)
14         'rgb(255,-10,0)',      // clipped to rgb(255,0,0)
15         'rgb(110%, 0%, 0%)',   // clipped to rgb(100%,0%,0%)
16
17         // Each of these has their alpha clipped [0.0, 1.0].
18         'rgba(255, 0, 0, -5)', // clipped to rgba(255,0,0,0)
19         'rgba(255, 0, 0, 5)',  // clipped to rgba(255,0,0,1)
20         ];
21
22     var invalidColors = [
23         // An invalid color, eg a value for a shorthand like 'border' which can have a color
24         'none',
25     ];
26
27     InspectorTest.runTestSuite([
28         function testColors(next)
29         {
30             for (var i = 0; i < colors.length; ++i)
31                 dumpColorRepresentationsForColor(colors[i]);
32             next();
33         },
34         function testInvalidColors(next)
35         {
36             for (var i = 0; i < invalidColors.length; ++i)
37                 dumpErrorsForInvalidColor(invalidColors[i]);
38             next();
39         },
40     ]);
41
42     function dumpErrorsForInvalidColor(colorString) 
43     {
44         var color = WebInspector.Color.parse(colorString);
45         if (!color) {
46             InspectorTest.addResult("");
47             InspectorTest.addResult("SUCCESS: parsed invalid color " + colorString + " to null");
48             return;
49         } else {
50             InspectorTest.addResult("");
51             InspectorTest.addResult("FAIL: invalid color " + colorString + " did not parse to to null");
52         }
53     }
54
55     function dumpColorRepresentationsForColor(colorString)
56     {
57         var color = WebInspector.Color.parse(colorString);
58         if (!color)
59             return;
60
61         InspectorTest.addResult("");
62         InspectorTest.addResult("color: " + colorString);
63         InspectorTest.addResult("  simple: " + color.simple);
64         var cf = WebInspector.Color.Format;
65         for (var colorFormatKey in cf) {
66             var colorFormat = cf[colorFormatKey];
67             // Simple colors do not have RGBA and HSLA representations.
68             if (color.simple && (colorFormat === cf.RGBA || colorFormat === cf.HSLA))
69                 continue;
70             // Advanced colors do not have HEX representations.
71             if (!color.simple && (colorFormat === cf.ShortHEX || colorFormat === cf.HEX))
72                 continue;
73             // If there is no ShortHEX then skip it.
74             if (colorFormat === cf.ShortHEX && !color.hasShortHex())
75                 continue;
76             // If there is no nickname, then skip it.
77             if (colorFormat === cf.Nickname && !color.nickname)
78                 continue;
79             InspectorTest.addResult('  ' + colorFormat + ": " + color.toString(colorFormat));
80         }
81     }
82 }
83 </script>
84 </head>
85
86 <body onload="runTest()">
87 <p>
88 Tests that the displayed string for colors correctly handles clipped CSS values and RGB percentages.
89 </p>
90
91 </body>
92 </html>