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 "xfa/fde/css/cfde_cssdeclaration.h"
6
7 #include "testing/gtest/include/gtest/gtest.h"
8
TEST(CFDE_CSSDecalration,HexEncodingParsing)9 TEST(CFDE_CSSDecalration, HexEncodingParsing) {
10 FX_ARGB color;
11
12 // Length value invalid.
13 EXPECT_FALSE(CFDE_CSSDeclaration::ParseCSSColor(L"#000", 3, &color));
14 EXPECT_FALSE(CFDE_CSSDeclaration::ParseCSSColor(L"#000000", 5, &color));
15 EXPECT_FALSE(CFDE_CSSDeclaration::ParseCSSColor(L"#000000", 8, &color));
16
17 // Invalid characters
18 EXPECT_TRUE(CFDE_CSSDeclaration::ParseCSSColor(L"#zxytlm", 7, &color));
19 EXPECT_EQ(0, FXARGB_R(color));
20 EXPECT_EQ(0, FXARGB_G(color));
21 EXPECT_EQ(0, FXARGB_B(color));
22
23 EXPECT_TRUE(CFDE_CSSDeclaration::ParseCSSColor(L"#000", 4, &color));
24 EXPECT_EQ(0, FXARGB_R(color));
25 EXPECT_EQ(0, FXARGB_G(color));
26 EXPECT_EQ(0, FXARGB_B(color));
27
28 EXPECT_TRUE(CFDE_CSSDeclaration::ParseCSSColor(L"#FFF", 4, &color));
29 EXPECT_EQ(255, FXARGB_R(color));
30 EXPECT_EQ(255, FXARGB_G(color));
31 EXPECT_EQ(255, FXARGB_B(color));
32
33 EXPECT_TRUE(CFDE_CSSDeclaration::ParseCSSColor(L"#F0F0F0", 7, &color));
34 EXPECT_EQ(240, FXARGB_R(color));
35 EXPECT_EQ(240, FXARGB_G(color));
36 EXPECT_EQ(240, FXARGB_B(color));
37
38 // Upper and lower case characters.
39 EXPECT_TRUE(CFDE_CSSDeclaration::ParseCSSColor(L"#1b2F3c", 7, &color));
40 EXPECT_EQ(27, FXARGB_R(color));
41 EXPECT_EQ(47, FXARGB_G(color));
42 EXPECT_EQ(60, FXARGB_B(color));
43 }
44
TEST(CFDE_CSSDecalration,RGBEncodingParsing)45 TEST(CFDE_CSSDecalration, RGBEncodingParsing) {
46 FX_ARGB color;
47
48 // Invalid input for rgb() syntax.
49 EXPECT_FALSE(CFDE_CSSDeclaration::ParseCSSColor(L"blahblahblah", 11, &color));
50
51 EXPECT_TRUE(CFDE_CSSDeclaration::ParseCSSColor(L"rgb(0, 0, 0)", 12, &color));
52 EXPECT_EQ(0, FXARGB_R(color));
53 EXPECT_EQ(0, FXARGB_G(color));
54 EXPECT_EQ(0, FXARGB_B(color));
55
56 EXPECT_TRUE(
57 CFDE_CSSDeclaration::ParseCSSColor(L"rgb(128,255,48)", 15, &color));
58 EXPECT_EQ(128, FXARGB_R(color));
59 EXPECT_EQ(255, FXARGB_G(color));
60 EXPECT_EQ(48, FXARGB_B(color));
61 }
62