1 // Copyright 2017 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "core/fxcrt/css/cfx_cssdeclaration.h"
6 
7 #include "testing/gtest/include/gtest/gtest.h"
8 #include "testing/test_support.h"
9 
TEST(CFX_CSSDeclarationTest,HexEncodingParsing)10 TEST(CFX_CSSDeclarationTest, HexEncodingParsing) {
11   FX_ARGB color;
12 
13   // Length value invalid.
14   EXPECT_FALSE(CFX_CSSDeclaration::ParseCSSColor(L"#000", 3, &color));
15   EXPECT_FALSE(CFX_CSSDeclaration::ParseCSSColor(L"#000000", 5, &color));
16   EXPECT_FALSE(CFX_CSSDeclaration::ParseCSSColor(L"#000000", 8, &color));
17 
18   // Invalid characters
19   EXPECT_TRUE(CFX_CSSDeclaration::ParseCSSColor(L"#zxytlm", 7, &color));
20   EXPECT_EQ(0, FXARGB_R(color));
21   EXPECT_EQ(0, FXARGB_G(color));
22   EXPECT_EQ(0, FXARGB_B(color));
23 
24   EXPECT_TRUE(CFX_CSSDeclaration::ParseCSSColor(L"#000", 4, &color));
25   EXPECT_EQ(0, FXARGB_R(color));
26   EXPECT_EQ(0, FXARGB_G(color));
27   EXPECT_EQ(0, FXARGB_B(color));
28 
29   EXPECT_TRUE(CFX_CSSDeclaration::ParseCSSColor(L"#FFF", 4, &color));
30   EXPECT_EQ(255, FXARGB_R(color));
31   EXPECT_EQ(255, FXARGB_G(color));
32   EXPECT_EQ(255, FXARGB_B(color));
33 
34   EXPECT_TRUE(CFX_CSSDeclaration::ParseCSSColor(L"#F0F0F0", 7, &color));
35   EXPECT_EQ(240, FXARGB_R(color));
36   EXPECT_EQ(240, FXARGB_G(color));
37   EXPECT_EQ(240, FXARGB_B(color));
38 
39   // Upper and lower case characters.
40   EXPECT_TRUE(CFX_CSSDeclaration::ParseCSSColor(L"#1b2F3c", 7, &color));
41   EXPECT_EQ(27, FXARGB_R(color));
42   EXPECT_EQ(47, FXARGB_G(color));
43   EXPECT_EQ(60, FXARGB_B(color));
44 }
45 
TEST(CFX_CSSDeclarationTest,RGBEncodingParsing)46 TEST(CFX_CSSDeclarationTest, RGBEncodingParsing) {
47   FX_ARGB color;
48 
49   // Invalid input for rgb() syntax.
50   EXPECT_FALSE(CFX_CSSDeclaration::ParseCSSColor(L"blahblahblah", 11, &color));
51 
52   EXPECT_TRUE(CFX_CSSDeclaration::ParseCSSColor(L"rgb(0, 0, 0)", 12, &color));
53   EXPECT_EQ(0, FXARGB_R(color));
54   EXPECT_EQ(0, FXARGB_G(color));
55   EXPECT_EQ(0, FXARGB_B(color));
56 
57   EXPECT_TRUE(
58       CFX_CSSDeclaration::ParseCSSColor(L"rgb(128,255,48)", 15, &color));
59   EXPECT_EQ(128, FXARGB_R(color));
60   EXPECT_EQ(255, FXARGB_G(color));
61   EXPECT_EQ(48, FXARGB_B(color));
62 }
63