• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6 
7 #include "core/fxcrt/css/cfx_cssvaluelistparser.h"
8 
9 #include "core/fxcrt/widestring.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11 #include "third_party/base/ptr_util.h"
12 
TEST(CFX_CSSValueListParserTest,rgb_short)13 TEST(CFX_CSSValueListParserTest, rgb_short) {
14   CFX_CSSPrimitiveType type;
15   const wchar_t* start;
16   int32_t len;
17 
18   auto parser = pdfium::MakeUnique<CFX_CSSValueListParser>(L"#abc", 4, L' ');
19   EXPECT_TRUE(parser->NextValue(&type, &start, &len));
20   EXPECT_EQ(CFX_CSSPrimitiveType::RGB, type);
21   EXPECT_EQ(L"#abc", WideString(start, len));
22   EXPECT_FALSE(parser->NextValue(&type, &start, &len));
23 
24   parser = pdfium::MakeUnique<CFX_CSSValueListParser>(L"#abcdef", 7, L' ');
25   EXPECT_TRUE(parser->NextValue(&type, &start, &len));
26   EXPECT_EQ(CFX_CSSPrimitiveType::RGB, type);
27   EXPECT_EQ(L"#abcdef", WideString(start, len));
28   EXPECT_FALSE(parser->NextValue(&type, &start, &len));
29 
30   parser =
31       pdfium::MakeUnique<CFX_CSSValueListParser>(L"rgb(1, 255, 4)", 14, L' ');
32   EXPECT_TRUE(parser->NextValue(&type, &start, &len));
33   EXPECT_EQ(CFX_CSSPrimitiveType::RGB, type);
34   EXPECT_EQ(L"rgb(1, 255, 4)", WideString(start, len));
35 
36   parser = pdfium::MakeUnique<CFX_CSSValueListParser>(L"#abcdefghij", 11, L' ');
37   EXPECT_TRUE(parser->NextValue(&type, &start, &len));
38   EXPECT_EQ(CFX_CSSPrimitiveType::Unknown, type);
39   EXPECT_EQ(L"#abcdefghij", WideString(start, len));
40   EXPECT_FALSE(parser->NextValue(&type, &start, &len));
41 }
42 
TEST(CFX_CSSValueListParserTest,number_parsing)43 TEST(CFX_CSSValueListParserTest, number_parsing) {
44   CFX_CSSPrimitiveType type;
45   const wchar_t* start;
46   int32_t len;
47 
48   auto parser = pdfium::MakeUnique<CFX_CSSValueListParser>(L"1234", 4, L' ');
49   EXPECT_TRUE(parser->NextValue(&type, &start, &len));
50   EXPECT_EQ(CFX_CSSPrimitiveType::Number, type);
51   EXPECT_EQ(L"1234", WideString(start, len));
52 
53   parser = pdfium::MakeUnique<CFX_CSSValueListParser>(L"-1234", 5, L' ');
54   EXPECT_TRUE(parser->NextValue(&type, &start, &len));
55   EXPECT_EQ(CFX_CSSPrimitiveType::Number, type);
56   EXPECT_EQ(L"-1234", WideString(start, len));
57 
58   parser = pdfium::MakeUnique<CFX_CSSValueListParser>(L"+1234", 5, L' ');
59   EXPECT_TRUE(parser->NextValue(&type, &start, &len));
60   EXPECT_EQ(CFX_CSSPrimitiveType::Number, type);
61   EXPECT_EQ(L"+1234", WideString(start, len));
62 
63   parser = pdfium::MakeUnique<CFX_CSSValueListParser>(L".1234", 5, L' ');
64   EXPECT_TRUE(parser->NextValue(&type, &start, &len));
65   EXPECT_EQ(CFX_CSSPrimitiveType::Number, type);
66   EXPECT_EQ(L".1234", WideString(start, len));
67 
68   parser = pdfium::MakeUnique<CFX_CSSValueListParser>(L"4321.1234", 9, L' ');
69   EXPECT_TRUE(parser->NextValue(&type, &start, &len));
70   EXPECT_EQ(CFX_CSSPrimitiveType::Number, type);
71   EXPECT_EQ(L"4321.1234", WideString(start, len));
72 
73   // TODO(dsinclair): These should probably fail but currently don't.
74   parser = pdfium::MakeUnique<CFX_CSSValueListParser>(L"4321.12.34", 10, L' ');
75   EXPECT_TRUE(parser->NextValue(&type, &start, &len));
76   EXPECT_EQ(CFX_CSSPrimitiveType::Number, type);
77   EXPECT_EQ(L"4321.12.34", WideString(start, len));
78 
79   parser = pdfium::MakeUnique<CFX_CSSValueListParser>(L"43a1.12.34", 10, L' ');
80   EXPECT_TRUE(parser->NextValue(&type, &start, &len));
81   EXPECT_EQ(CFX_CSSPrimitiveType::Number, type);
82   EXPECT_EQ(L"43a1.12.34", WideString(start, len));
83 }
84 
TEST(CFX_CSSValueListParserTest,string_parsing)85 TEST(CFX_CSSValueListParserTest, string_parsing) {
86   CFX_CSSPrimitiveType type;
87   const wchar_t* start;
88   int32_t len;
89 
90   auto parser =
91       pdfium::MakeUnique<CFX_CSSValueListParser>(L"'string'", 8, L' ');
92   EXPECT_TRUE(parser->NextValue(&type, &start, &len));
93   EXPECT_EQ(CFX_CSSPrimitiveType::String, type);
94   EXPECT_EQ(L"string", WideString(start, len));
95 
96   parser = pdfium::MakeUnique<CFX_CSSValueListParser>(L"\"another string\"", 16,
97                                                       L' ');
98   EXPECT_TRUE(parser->NextValue(&type, &start, &len));
99   EXPECT_EQ(CFX_CSSPrimitiveType::String, type);
100   EXPECT_EQ(L"another string", WideString(start, len));
101 
102   parser = pdfium::MakeUnique<CFX_CSSValueListParser>(L"standalone", 10, L' ');
103   EXPECT_TRUE(parser->NextValue(&type, &start, &len));
104   EXPECT_EQ(CFX_CSSPrimitiveType::String, type);
105   EXPECT_EQ(L"standalone", WideString(start, len));
106 }
107 
TEST(CFX_CSSValueListParserTest,multiparsing)108 TEST(CFX_CSSValueListParserTest, multiparsing) {
109   CFX_CSSPrimitiveType type;
110   const wchar_t* start;
111   int32_t len;
112 
113   auto parser = pdfium::MakeUnique<CFX_CSSValueListParser>(L"1, 2, 3", 7, L',');
114   EXPECT_TRUE(parser->NextValue(&type, &start, &len));
115   EXPECT_EQ(CFX_CSSPrimitiveType::Number, type);
116   EXPECT_EQ(L"1", WideString(start, len));
117 
118   EXPECT_TRUE(parser->NextValue(&type, &start, &len));
119   EXPECT_EQ(CFX_CSSPrimitiveType::Number, type);
120   EXPECT_EQ(L"2", WideString(start, len));
121 
122   EXPECT_TRUE(parser->NextValue(&type, &start, &len));
123   EXPECT_EQ(CFX_CSSPrimitiveType::Number, type);
124   EXPECT_EQ(L"3", WideString(start, len));
125 
126   EXPECT_FALSE(parser->NextValue(&type, &start, &len));
127 
128   parser = pdfium::MakeUnique<CFX_CSSValueListParser>(L"'str', rgb(1, 2, 3), 4",
129                                                       22, L',');
130   EXPECT_TRUE(parser->NextValue(&type, &start, &len));
131   EXPECT_EQ(CFX_CSSPrimitiveType::String, type);
132   EXPECT_EQ(L"str", WideString(start, len));
133 
134   EXPECT_TRUE(parser->NextValue(&type, &start, &len));
135   EXPECT_EQ(CFX_CSSPrimitiveType::RGB, type);
136   EXPECT_EQ(L"rgb(1, 2, 3)", WideString(start, len));
137 
138   EXPECT_TRUE(parser->NextValue(&type, &start, &len));
139   EXPECT_EQ(CFX_CSSPrimitiveType::Number, type);
140   EXPECT_EQ(L"4", WideString(start, len));
141 }
142