|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +#include <gtest/gtest.h> |
| 9 | +#include <react/renderer/css/CSSColor.h> |
| 10 | +#include <react/renderer/css/CSSValueParser.h> |
| 11 | + |
| 12 | +namespace facebook::react { |
| 13 | + |
| 14 | +TEST(CSSColor, hex_color_values) { |
| 15 | + auto emptyValue = parseCSSProperty<CSSColor>(""); |
| 16 | + EXPECT_TRUE(std::holds_alternative<std::monostate>(emptyValue)); |
| 17 | + |
| 18 | + auto hex3DigitColorValue = parseCSSProperty<CSSColor>("#fff"); |
| 19 | + EXPECT_TRUE(std::holds_alternative<CSSColor>(hex3DigitColorValue)); |
| 20 | + EXPECT_EQ(std::get<CSSColor>(hex3DigitColorValue).r, 255); |
| 21 | + EXPECT_EQ(std::get<CSSColor>(hex3DigitColorValue).g, 255); |
| 22 | + EXPECT_EQ(std::get<CSSColor>(hex3DigitColorValue).b, 255); |
| 23 | + EXPECT_EQ(std::get<CSSColor>(hex3DigitColorValue).a, 255); |
| 24 | + |
| 25 | + auto hex4DigitColorValue = parseCSSProperty<CSSColor>("#ffff"); |
| 26 | + EXPECT_TRUE(std::holds_alternative<CSSColor>(hex4DigitColorValue)); |
| 27 | + EXPECT_EQ(std::get<CSSColor>(hex4DigitColorValue).r, 255); |
| 28 | + EXPECT_EQ(std::get<CSSColor>(hex4DigitColorValue).g, 255); |
| 29 | + EXPECT_EQ(std::get<CSSColor>(hex4DigitColorValue).b, 255); |
| 30 | + EXPECT_EQ(std::get<CSSColor>(hex4DigitColorValue).a, 255); |
| 31 | + |
| 32 | + auto hex6DigitColorValue = parseCSSProperty<CSSColor>("#ffffff"); |
| 33 | + EXPECT_TRUE(std::holds_alternative<CSSColor>(hex6DigitColorValue)); |
| 34 | + EXPECT_EQ(std::get<CSSColor>(hex6DigitColorValue).r, 255); |
| 35 | + EXPECT_EQ(std::get<CSSColor>(hex6DigitColorValue).g, 255); |
| 36 | + EXPECT_EQ(std::get<CSSColor>(hex6DigitColorValue).b, 255); |
| 37 | + EXPECT_EQ(std::get<CSSColor>(hex6DigitColorValue).a, 255); |
| 38 | + |
| 39 | + auto hex8DigitColorValue = parseCSSProperty<CSSColor>("#ffffffff"); |
| 40 | + EXPECT_TRUE(std::holds_alternative<CSSColor>(hex8DigitColorValue)); |
| 41 | + EXPECT_EQ(std::get<CSSColor>(hex8DigitColorValue).r, 255); |
| 42 | + EXPECT_EQ(std::get<CSSColor>(hex8DigitColorValue).g, 255); |
| 43 | + EXPECT_EQ(std::get<CSSColor>(hex8DigitColorValue).b, 255); |
| 44 | + EXPECT_EQ(std::get<CSSColor>(hex8DigitColorValue).a, 255); |
| 45 | + |
| 46 | + auto hexMixedCaseColorValue = parseCSSProperty<CSSColor>("#FFCc99"); |
| 47 | + EXPECT_TRUE(std::holds_alternative<CSSColor>(hexMixedCaseColorValue)); |
| 48 | + EXPECT_EQ(std::get<CSSColor>(hexMixedCaseColorValue).r, 255); |
| 49 | + EXPECT_EQ(std::get<CSSColor>(hexMixedCaseColorValue).g, 204); |
| 50 | + EXPECT_EQ(std::get<CSSColor>(hexMixedCaseColorValue).b, 153); |
| 51 | + EXPECT_EQ(std::get<CSSColor>(hexMixedCaseColorValue).a, 255); |
| 52 | + |
| 53 | + auto hexDigitOnlyColorValue = parseCSSProperty<CSSColor>("#369"); |
| 54 | + EXPECT_TRUE(std::holds_alternative<CSSColor>(hexDigitOnlyColorValue)); |
| 55 | + EXPECT_EQ(std::get<CSSColor>(hexDigitOnlyColorValue).r, 51); |
| 56 | + EXPECT_EQ(std::get<CSSColor>(hexDigitOnlyColorValue).g, 102); |
| 57 | + EXPECT_EQ(std::get<CSSColor>(hexDigitOnlyColorValue).b, 153); |
| 58 | + EXPECT_EQ(std::get<CSSColor>(hexDigitOnlyColorValue).a, 255); |
| 59 | + |
| 60 | + auto hexAlphaTestValue = parseCSSProperty<CSSColor>("#FFFFFFCC"); |
| 61 | + EXPECT_TRUE(std::holds_alternative<CSSColor>(hexAlphaTestValue)); |
| 62 | + EXPECT_EQ(std::get<CSSColor>(hexAlphaTestValue).r, 255); |
| 63 | + EXPECT_EQ(std::get<CSSColor>(hexAlphaTestValue).g, 255); |
| 64 | + EXPECT_EQ(std::get<CSSColor>(hexAlphaTestValue).b, 255); |
| 65 | + EXPECT_EQ(std::get<CSSColor>(hexAlphaTestValue).a, 204); |
| 66 | +} |
| 67 | + |
| 68 | +TEST(CSSColor, named_colors) { |
| 69 | + auto invalidNamedColorTestValue = parseCSSProperty<CSSColor>("redd"); |
| 70 | + EXPECT_TRUE( |
| 71 | + std::holds_alternative<std::monostate>(invalidNamedColorTestValue)); |
| 72 | + |
| 73 | + auto namedColorTestValue1 = parseCSSProperty<CSSColor>("red"); |
| 74 | + EXPECT_TRUE(std::holds_alternative<CSSColor>(namedColorTestValue1)); |
| 75 | + EXPECT_EQ(std::get<CSSColor>(namedColorTestValue1).r, 255); |
| 76 | + EXPECT_EQ(std::get<CSSColor>(namedColorTestValue1).g, 0); |
| 77 | + EXPECT_EQ(std::get<CSSColor>(namedColorTestValue1).b, 0); |
| 78 | + EXPECT_EQ(std::get<CSSColor>(namedColorTestValue1).a, 255); |
| 79 | + |
| 80 | + auto namedColorTestValue2 = parseCSSProperty<CSSColor>("cornsilk"); |
| 81 | + EXPECT_TRUE(std::holds_alternative<CSSColor>(namedColorTestValue2)); |
| 82 | + EXPECT_EQ(std::get<CSSColor>(namedColorTestValue2).r, 255); |
| 83 | + EXPECT_EQ(std::get<CSSColor>(namedColorTestValue2).g, 248); |
| 84 | + EXPECT_EQ(std::get<CSSColor>(namedColorTestValue2).b, 220); |
| 85 | + EXPECT_EQ(std::get<CSSColor>(namedColorTestValue2).a, 255); |
| 86 | + |
| 87 | + auto namedColorMixedCaseTestValue = parseCSSProperty<CSSColor>("sPrINgGrEEn"); |
| 88 | + EXPECT_TRUE(std::holds_alternative<CSSColor>(namedColorMixedCaseTestValue)); |
| 89 | + EXPECT_EQ(std::get<CSSColor>(namedColorMixedCaseTestValue).r, 0); |
| 90 | + EXPECT_EQ(std::get<CSSColor>(namedColorMixedCaseTestValue).g, 255); |
| 91 | + EXPECT_EQ(std::get<CSSColor>(namedColorMixedCaseTestValue).b, 127); |
| 92 | + EXPECT_EQ(std::get<CSSColor>(namedColorMixedCaseTestValue).a, 255); |
| 93 | + |
| 94 | + auto transparentColor = parseCSSProperty<CSSColor>("transparent"); |
| 95 | + EXPECT_TRUE(std::holds_alternative<CSSColor>(transparentColor)); |
| 96 | + EXPECT_EQ(std::get<CSSColor>(transparentColor).r, 0); |
| 97 | + EXPECT_EQ(std::get<CSSColor>(transparentColor).g, 0); |
| 98 | + EXPECT_EQ(std::get<CSSColor>(transparentColor).b, 0); |
| 99 | + EXPECT_EQ(std::get<CSSColor>(transparentColor).a, 0); |
| 100 | +} |
| 101 | + |
| 102 | +} // namespace facebook::react |
0 commit comments