Split Color serialization out of Color classes
[WebKit-https.git] / Tools / TestWebKitAPI / Tests / WebCore / ExtendedColorTests.cpp
1 /*
2  * Copyright (C) 2016 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "config.h"
27
28 #include "Test.h"
29 #include "WTFStringUtilities.h"
30 #include <WebCore/Color.h>
31 #include <WebCore/ColorSerialization.h>
32 #include <wtf/MathExtras.h>
33
34 using namespace WebCore;
35
36 namespace TestWebKitAPI {
37
38 TEST(ExtendedColor, Constructor)
39 {
40     Color c1 { makeExtendedColor(1.0, 0.5, 0.25, 1.0, ColorSpace::DisplayP3) };
41     EXPECT_TRUE(c1.isExtended());
42
43     auto [r, g, b, alpha] = c1.asExtended().components();
44
45     EXPECT_FLOAT_EQ(1.0, r);
46     EXPECT_FLOAT_EQ(0.5, g);
47     EXPECT_FLOAT_EQ(0.25, b);
48     EXPECT_FLOAT_EQ(1.0, alpha);
49     EXPECT_EQ(1u, c1.asExtended().refCount());
50     EXPECT_EQ(serializationForCSS(c1), "color(display-p3 1 0.5 0.25)");
51 }
52
53 TEST(ExtendedColor, CopyConstructor)
54 {
55     Color c1 { makeExtendedColor(1.0, 0.5, 0.25, 1.0, ColorSpace::DisplayP3) };
56     EXPECT_TRUE(c1.isExtended());
57
58     Color c2(c1);
59
60     auto [r, g, b, alpha] = c2.asExtended().components();
61
62     EXPECT_FLOAT_EQ(1.0, r);
63     EXPECT_FLOAT_EQ(0.5, g);
64     EXPECT_FLOAT_EQ(0.25, b);
65     EXPECT_FLOAT_EQ(1.0, alpha);
66     EXPECT_EQ(2u, c1.asExtended().refCount());
67     EXPECT_EQ(2u, c2.asExtended().refCount());
68     EXPECT_EQ(serializationForCSS(c2), "color(display-p3 1 0.5 0.25)");
69 }
70
71 TEST(ExtendedColor, Assignment)
72 {
73     Color c1 { makeExtendedColor(1.0, 0.5, 0.25, 1.0, ColorSpace::DisplayP3) };
74     EXPECT_TRUE(c1.isExtended());
75
76     Color c2 = c1;
77
78     auto [r, g, b, alpha] = c2.asExtended().components();
79
80     EXPECT_FLOAT_EQ(1.0, r);
81     EXPECT_FLOAT_EQ(0.5, g);
82     EXPECT_FLOAT_EQ(0.25, b);
83     EXPECT_FLOAT_EQ(1.0, alpha);
84     EXPECT_EQ(2u, c1.asExtended().refCount());
85     EXPECT_EQ(2u, c2.asExtended().refCount());
86     EXPECT_EQ(serializationForCSS(c2), "color(display-p3 1 0.5 0.25)");
87 }
88
89 TEST(ExtendedColor, Equality)
90 {
91     {
92         Color c1 { makeExtendedColor(1.0, 0.5, 0.25, 1.0, ColorSpace::DisplayP3) };
93         EXPECT_TRUE(c1.isExtended());
94
95         Color c2 { makeExtendedColor(1.0, 0.5, 0.25, 1.0, ColorSpace::DisplayP3) };
96         EXPECT_TRUE(c1.isExtended());
97
98         EXPECT_EQ(c1, c2);
99     }
100
101     {
102         Color c1 { makeExtendedColor(1.0, 0.5, 0.25, 1.0, ColorSpace::DisplayP3) };
103         EXPECT_TRUE(c1.isExtended());
104
105         Color c2 { makeExtendedColor(1.0, 0.5, 0.25, 1.0, ColorSpace::SRGB) };
106         EXPECT_TRUE(c1.isExtended());
107
108         EXPECT_NE(c1, c2);
109     }
110
111     int r = 255;
112     int g = 128;
113     int b = 63;
114     int a = 127;
115     Color rgb1 { makeExtendedColor(r / 255.0, g / 255.0, b / 255.0, a / 255.0, ColorSpace::SRGB) };
116     Color rgb2 = makeSimpleColor(r, g, b, a);
117     EXPECT_NE(rgb1, rgb2);
118     EXPECT_NE(rgb2, rgb1);
119 }
120
121 TEST(ExtendedColor, Hash)
122 {
123     {
124         Color c1 { makeExtendedColor(1.0, 0.5, 0.25, 1.0, ColorSpace::DisplayP3) };
125         EXPECT_TRUE(c1.isExtended());
126
127         Color c2 { makeExtendedColor(1.0, 0.5, 0.25, 1.0, ColorSpace::DisplayP3) };
128         EXPECT_TRUE(c1.isExtended());
129
130         EXPECT_EQ(c1.hash(), c2.hash());
131     }
132
133     {
134         Color c1 { makeExtendedColor(1.0, 0.5, 0.25, 1.0, ColorSpace::DisplayP3) };
135         EXPECT_TRUE(c1.isExtended());
136
137         Color c2 { makeExtendedColor(1.0, 0.5, 0.25, 1.0, ColorSpace::SRGB) };
138         EXPECT_TRUE(c1.isExtended());
139
140         EXPECT_NE(c1.hash(), c2.hash());
141     }
142
143     int r = 255;
144     int g = 128;
145     int b = 63;
146     int a = 127;
147     Color rgb1 { makeExtendedColor(r / 255.0, g / 255.0, b / 255.0, a / 255.0, ColorSpace::SRGB) };
148     Color rgb2 = makeSimpleColor(r, g, b, a);
149     EXPECT_NE(rgb1.hash(), rgb2.hash());
150 }
151
152 TEST(ExtendedColor, MoveConstructor)
153 {
154     Color c1 { makeExtendedColor(1.0, 0.5, 0.25, 1.0, ColorSpace::DisplayP3) };
155     EXPECT_TRUE(c1.isExtended());
156
157     Color c2(WTFMove(c1));
158     // We should have moved the extended color pointer into c2,
159     // and set c1 to invalid so that it doesn't cause deletion.
160     EXPECT_FALSE(c1.isExtended());
161     EXPECT_FALSE(c1.isValid());
162
163     auto [r, g, b, alpha] = c2.asExtended().components();
164
165     EXPECT_FLOAT_EQ(1.0, r);
166     EXPECT_FLOAT_EQ(0.5, g);
167     EXPECT_FLOAT_EQ(0.25, b);
168     EXPECT_FLOAT_EQ(1.0, alpha);
169     EXPECT_EQ(1u, c2.asExtended().refCount());
170     EXPECT_EQ(serializationForCSS(c2), "color(display-p3 1 0.5 0.25)");
171 }
172
173 TEST(ExtendedColor, MoveAssignment)
174 {
175     Color c1 { makeExtendedColor(1.0, 0.5, 0.25, 1.0, ColorSpace::DisplayP3) };
176     EXPECT_TRUE(c1.isExtended());
177
178     Color c2 = WTFMove(c1);
179
180     // We should have moved the extended color pointer into c2,
181     // and set c1 to invalid so that it doesn't cause deletion.
182     EXPECT_FALSE(c1.isExtended());
183     EXPECT_FALSE(c1.isValid());
184
185     auto [r, g, b, alpha] = c2.asExtended().components();
186
187     EXPECT_FLOAT_EQ(1.0, r);
188     EXPECT_FLOAT_EQ(0.5, g);
189     EXPECT_FLOAT_EQ(0.25, b);
190     EXPECT_FLOAT_EQ(1.0, alpha);
191     EXPECT_EQ(1u, c2.asExtended().refCount());
192     EXPECT_EQ(serializationForCSS(c2), "color(display-p3 1 0.5 0.25)");
193 }
194
195 TEST(ExtendedColor, BasicReferenceCounting)
196 {
197     Color* c1 = new Color { makeExtendedColor(1.0, 0.5, 0.25, 1.0, ColorSpace::DisplayP3) };
198     EXPECT_TRUE(c1->isExtended());
199
200     Color* c2 = new Color(*c1);
201     Color* c3 = new Color(*c2);
202
203     auto [r, g, b, alpha] = c2->asExtended().components();
204
205     EXPECT_FLOAT_EQ(1.0, r);
206     EXPECT_FLOAT_EQ(0.5, g);
207     EXPECT_FLOAT_EQ(0.25, b);
208     EXPECT_FLOAT_EQ(1.0, alpha);
209     EXPECT_EQ(3u, c2->asExtended().refCount());
210     EXPECT_EQ(serializationForCSS(*c2), "color(display-p3 1 0.5 0.25)");
211
212     delete c1;
213     EXPECT_EQ(2u, c2->asExtended().refCount());
214
215     delete c2;
216     EXPECT_EQ(1u, c3->asExtended().refCount());
217
218     delete c3;
219 }
220
221 Color makeColor()
222 {
223     Color c1 { makeExtendedColor(1.0, 0.5, 0.25, 1.0, ColorSpace::DisplayP3) };
224     EXPECT_TRUE(c1.isExtended());
225     EXPECT_EQ(1u, c1.asExtended().refCount());
226
227     return c1;
228 }
229
230 TEST(ExtendedColor, ReturnValues)
231 {
232     Color c2 = makeColor();
233     EXPECT_TRUE(c2.isExtended());
234
235     EXPECT_EQ(1u, c2.asExtended().refCount());
236     EXPECT_EQ(serializationForCSS(c2), "color(display-p3 1 0.5 0.25)");
237 }
238
239 TEST(ExtendedColor, P3ConversionToSRGB)
240 {
241     Color p3Color { makeExtendedColor(1.0, 0.5, 0.25, 0.75, ColorSpace::DisplayP3) };
242     EXPECT_TRUE(p3Color.isExtended());
243
244     auto sRGBAColor = p3Color.toSRGBALossy();
245     EXPECT_TRUE(WTF::areEssentiallyEqual(sRGBAColor.red, 1.0f));
246     EXPECT_TRUE(WTF::areEssentiallyEqual(sRGBAColor.green, 0.462537885f));
247     EXPECT_TRUE(WTF::areEssentiallyEqual(sRGBAColor.blue, 0.149147838f));
248     EXPECT_TRUE(WTF::areEssentiallyEqual(sRGBAColor.alpha, 0.75f));
249 }
250
251 TEST(ExtendedColor, LinearSRGBConversionToSRGB)
252 {
253     Color linearColor { makeExtendedColor(1.0, 0.5, 0.25, 0.75, ColorSpace::LinearRGB) };
254     EXPECT_TRUE(linearColor.isExtended());
255     auto sRGBAColor = linearColor.toSRGBALossy();
256     EXPECT_TRUE(WTF::areEssentiallyEqual(sRGBAColor.red, 1.0f));
257     EXPECT_TRUE(WTF::areEssentiallyEqual(sRGBAColor.green, 0.735356927f));
258     EXPECT_TRUE(WTF::areEssentiallyEqual(sRGBAColor.blue, 0.537098706f));
259     EXPECT_TRUE(WTF::areEssentiallyEqual(sRGBAColor.alpha, 0.75f));
260 }
261
262 } // namespace TestWebKitAPI