0f5e604e8cc1ca416663902104a9a7d10276b63d
[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 <wtf/MathExtras.h>
32
33 using namespace WebCore;
34
35 namespace TestWebKitAPI {
36
37 TEST(ExtendedColor, Constructor)
38 {
39     Color c1 { makeExtendedColor(1.0, 0.5, 0.25, 1.0, ColorSpace::DisplayP3) };
40     EXPECT_TRUE(c1.isExtended());
41
42     auto [r, g, b, alpha] = c1.asExtended().components();
43
44     EXPECT_FLOAT_EQ(1.0, r);
45     EXPECT_FLOAT_EQ(0.5, g);
46     EXPECT_FLOAT_EQ(0.25, b);
47     EXPECT_FLOAT_EQ(1.0, alpha);
48     EXPECT_EQ(1u, c1.asExtended().refCount());
49     EXPECT_EQ(c1.cssText(), "color(display-p3 1 0.5 0.25)");
50 }
51
52 TEST(ExtendedColor, CopyConstructor)
53 {
54     Color c1 { makeExtendedColor(1.0, 0.5, 0.25, 1.0, ColorSpace::DisplayP3) };
55     EXPECT_TRUE(c1.isExtended());
56
57     Color c2(c1);
58
59     auto [r, g, b, alpha] = c2.asExtended().components();
60
61     EXPECT_FLOAT_EQ(1.0, r);
62     EXPECT_FLOAT_EQ(0.5, g);
63     EXPECT_FLOAT_EQ(0.25, b);
64     EXPECT_FLOAT_EQ(1.0, alpha);
65     EXPECT_EQ(2u, c1.asExtended().refCount());
66     EXPECT_EQ(2u, c2.asExtended().refCount());
67     EXPECT_EQ(c2.cssText(), "color(display-p3 1 0.5 0.25)");
68 }
69
70 TEST(ExtendedColor, Assignment)
71 {
72     Color c1 { makeExtendedColor(1.0, 0.5, 0.25, 1.0, ColorSpace::DisplayP3) };
73     EXPECT_TRUE(c1.isExtended());
74
75     Color c2 = c1;
76
77     auto [r, g, b, alpha] = c2.asExtended().components();
78
79     EXPECT_FLOAT_EQ(1.0, r);
80     EXPECT_FLOAT_EQ(0.5, g);
81     EXPECT_FLOAT_EQ(0.25, b);
82     EXPECT_FLOAT_EQ(1.0, alpha);
83     EXPECT_EQ(2u, c1.asExtended().refCount());
84     EXPECT_EQ(2u, c2.asExtended().refCount());
85     EXPECT_EQ(c2.cssText(), "color(display-p3 1 0.5 0.25)");
86 }
87
88 TEST(ExtendedColor, Equality)
89 {
90     {
91         Color c1 { makeExtendedColor(1.0, 0.5, 0.25, 1.0, ColorSpace::DisplayP3) };
92         EXPECT_TRUE(c1.isExtended());
93
94         Color c2 { makeExtendedColor(1.0, 0.5, 0.25, 1.0, ColorSpace::DisplayP3) };
95         EXPECT_TRUE(c1.isExtended());
96
97         EXPECT_EQ(c1, c2);
98     }
99
100     {
101         Color c1 { makeExtendedColor(1.0, 0.5, 0.25, 1.0, ColorSpace::DisplayP3) };
102         EXPECT_TRUE(c1.isExtended());
103
104         Color c2 { makeExtendedColor(1.0, 0.5, 0.25, 1.0, ColorSpace::SRGB) };
105         EXPECT_TRUE(c1.isExtended());
106
107         EXPECT_NE(c1, c2);
108     }
109
110     int r = 255;
111     int g = 128;
112     int b = 63;
113     int a = 127;
114     Color rgb1 { makeExtendedColor(r / 255.0, g / 255.0, b / 255.0, a / 255.0, ColorSpace::SRGB) };
115     Color rgb2 = makeSimpleColor(r, g, b, a);
116     EXPECT_NE(rgb1, rgb2);
117     EXPECT_NE(rgb2, rgb1);
118 }
119
120 TEST(ExtendedColor, Hash)
121 {
122     {
123         Color c1 { makeExtendedColor(1.0, 0.5, 0.25, 1.0, ColorSpace::DisplayP3) };
124         EXPECT_TRUE(c1.isExtended());
125
126         Color c2 { makeExtendedColor(1.0, 0.5, 0.25, 1.0, ColorSpace::DisplayP3) };
127         EXPECT_TRUE(c1.isExtended());
128
129         EXPECT_EQ(c1.hash(), c2.hash());
130     }
131
132     {
133         Color c1 { makeExtendedColor(1.0, 0.5, 0.25, 1.0, ColorSpace::DisplayP3) };
134         EXPECT_TRUE(c1.isExtended());
135
136         Color c2 { makeExtendedColor(1.0, 0.5, 0.25, 1.0, ColorSpace::SRGB) };
137         EXPECT_TRUE(c1.isExtended());
138
139         EXPECT_NE(c1.hash(), c2.hash());
140     }
141
142     int r = 255;
143     int g = 128;
144     int b = 63;
145     int a = 127;
146     Color rgb1 { makeExtendedColor(r / 255.0, g / 255.0, b / 255.0, a / 255.0, ColorSpace::SRGB) };
147     Color rgb2 = makeSimpleColor(r, g, b, a);
148     EXPECT_NE(rgb1.hash(), rgb2.hash());
149 }
150
151 TEST(ExtendedColor, MoveConstructor)
152 {
153     Color c1 { makeExtendedColor(1.0, 0.5, 0.25, 1.0, ColorSpace::DisplayP3) };
154     EXPECT_TRUE(c1.isExtended());
155
156     Color c2(WTFMove(c1));
157     // We should have moved the extended color pointer into c2,
158     // and set c1 to invalid so that it doesn't cause deletion.
159     EXPECT_FALSE(c1.isExtended());
160     EXPECT_FALSE(c1.isValid());
161
162     auto [r, g, b, alpha] = c2.asExtended().components();
163
164     EXPECT_FLOAT_EQ(1.0, r);
165     EXPECT_FLOAT_EQ(0.5, g);
166     EXPECT_FLOAT_EQ(0.25, b);
167     EXPECT_FLOAT_EQ(1.0, alpha);
168     EXPECT_EQ(1u, c2.asExtended().refCount());
169     EXPECT_EQ(c2.cssText(), "color(display-p3 1 0.5 0.25)");
170 }
171
172 TEST(ExtendedColor, MoveAssignment)
173 {
174     Color c1 { makeExtendedColor(1.0, 0.5, 0.25, 1.0, ColorSpace::DisplayP3) };
175     EXPECT_TRUE(c1.isExtended());
176
177     Color c2 = WTFMove(c1);
178
179     // We should have moved the extended color pointer into c2,
180     // and set c1 to invalid so that it doesn't cause deletion.
181     EXPECT_FALSE(c1.isExtended());
182     EXPECT_FALSE(c1.isValid());
183
184     auto [r, g, b, alpha] = c2.asExtended().components();
185
186     EXPECT_FLOAT_EQ(1.0, r);
187     EXPECT_FLOAT_EQ(0.5, g);
188     EXPECT_FLOAT_EQ(0.25, b);
189     EXPECT_FLOAT_EQ(1.0, alpha);
190     EXPECT_EQ(1u, c2.asExtended().refCount());
191     EXPECT_EQ(c2.cssText(), "color(display-p3 1 0.5 0.25)");
192 }
193
194 TEST(ExtendedColor, BasicReferenceCounting)
195 {
196     Color* c1 = new Color { makeExtendedColor(1.0, 0.5, 0.25, 1.0, ColorSpace::DisplayP3) };
197     EXPECT_TRUE(c1->isExtended());
198
199     Color* c2 = new Color(*c1);
200     Color* c3 = new Color(*c2);
201
202     auto [r, g, b, alpha] = c2->asExtended().components();
203
204     EXPECT_FLOAT_EQ(1.0, r);
205     EXPECT_FLOAT_EQ(0.5, g);
206     EXPECT_FLOAT_EQ(0.25, b);
207     EXPECT_FLOAT_EQ(1.0, alpha);
208     EXPECT_EQ(3u, c2->asExtended().refCount());
209     EXPECT_EQ(c2->cssText(), "color(display-p3 1 0.5 0.25)");
210
211     delete c1;
212     EXPECT_EQ(2u, c2->asExtended().refCount());
213
214     delete c2;
215     EXPECT_EQ(1u, c3->asExtended().refCount());
216
217     delete c3;
218 }
219
220 Color makeColor()
221 {
222     Color c1 { makeExtendedColor(1.0, 0.5, 0.25, 1.0, ColorSpace::DisplayP3) };
223     EXPECT_TRUE(c1.isExtended());
224     EXPECT_EQ(1u, c1.asExtended().refCount());
225
226     return c1;
227 }
228
229 TEST(ExtendedColor, ReturnValues)
230 {
231     Color c2 = makeColor();
232     EXPECT_TRUE(c2.isExtended());
233
234     EXPECT_EQ(1u, c2.asExtended().refCount());
235     EXPECT_EQ(c2.cssText(), "color(display-p3 1 0.5 0.25)");
236 }
237
238 TEST(ExtendedColor, P3ConversionToSRGB)
239 {
240     Color p3Color { makeExtendedColor(1.0, 0.5, 0.25, 0.75, ColorSpace::DisplayP3) };
241     EXPECT_TRUE(p3Color.isExtended());
242
243     auto sRGBAColor = p3Color.toSRGBALossy();
244     EXPECT_TRUE(WTF::areEssentiallyEqual(sRGBAColor.red, 1.0f));
245     EXPECT_TRUE(WTF::areEssentiallyEqual(sRGBAColor.green, 0.462537885f));
246     EXPECT_TRUE(WTF::areEssentiallyEqual(sRGBAColor.blue, 0.149147838f));
247     EXPECT_TRUE(WTF::areEssentiallyEqual(sRGBAColor.alpha, 0.75f));
248 }
249
250 TEST(ExtendedColor, LinearSRGBConversionToSRGB)
251 {
252     Color linearColor { makeExtendedColor(1.0, 0.5, 0.25, 0.75, ColorSpace::LinearRGB) };
253     EXPECT_TRUE(linearColor.isExtended());
254     auto sRGBAColor = linearColor.toSRGBALossy();
255     EXPECT_TRUE(WTF::areEssentiallyEqual(sRGBAColor.red, 1.0f));
256     EXPECT_TRUE(WTF::areEssentiallyEqual(sRGBAColor.green, 0.735356927f));
257     EXPECT_TRUE(WTF::areEssentiallyEqual(sRGBAColor.blue, 0.537098706f));
258     EXPECT_TRUE(WTF::areEssentiallyEqual(sRGBAColor.alpha, 0.75f));
259 }
260
261 } // namespace TestWebKitAPI