1 // Copyright 2018 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/fxfa/parser/xfa_basic_data.h"
6 
7 #include <utility>
8 
9 #include "testing/gtest/include/gtest/gtest.h"
10 
TEST(XFABasicDataTest,GetPacketByName)11 TEST(XFABasicDataTest, GetPacketByName) {
12   Optional<XFA_PACKETINFO> result = XFA_GetPacketByName(L"");
13   EXPECT_FALSE(result.has_value());
14 
15   result = XFA_GetPacketByName(L"nonesuch");
16   EXPECT_FALSE(result.has_value());
17 
18   result = XFA_GetPacketByName(L"datasets");
19   ASSERT_TRUE(result.has_value());
20   EXPECT_EQ(XFA_PacketType::Datasets, result.value().packet_type);
21 
22   result = XFA_GetPacketByName(L"sourceSet");
23   ASSERT_TRUE(result.has_value());
24   EXPECT_EQ(XFA_PacketType::SourceSet, result.value().packet_type);
25 }
26 
TEST(XFABasicDataTest,PacketToName)27 TEST(XFABasicDataTest, PacketToName) {
28   XFA_PACKETINFO result = XFA_GetPacketByIndex(XFA_PacketType::Datasets);
29   EXPECT_STREQ(L"datasets", result.name);
30 
31   result = XFA_GetPacketByIndex(XFA_PacketType::ConnectionSet);
32   EXPECT_STREQ(L"connectionSet", result.name);
33 }
34 
TEST(XFABasicDataTest,GetElementByName)35 TEST(XFABasicDataTest, GetElementByName) {
36   EXPECT_EQ(XFA_Element::Unknown, XFA_GetElementByName(L""));
37   EXPECT_EQ(XFA_Element::Unknown, XFA_GetElementByName(L"nonesuch"));
38   EXPECT_EQ(XFA_Element::ConnectionSet, XFA_GetElementByName(L"connectionSet"));
39   EXPECT_EQ(XFA_Element::Items, XFA_GetElementByName(L"items"));
40 
41   // Internal elements are not retrievable by name.
42   EXPECT_EQ(XFA_Element::Unknown, XFA_GetElementByName(L"model"));
43 }
44 
TEST(XFABasicDataTest,ElementToName)45 TEST(XFABasicDataTest, ElementToName) {
46   EXPECT_EQ("conformance", XFA_ElementToName(XFA_Element::Conformance));
47   EXPECT_EQ("tagged", XFA_ElementToName(XFA_Element::Tagged));
48 
49   // Internal elements resolve back to real names.
50   EXPECT_EQ("node", XFA_ElementToName(XFA_Element::Node));
51 }
52 
TEST(XFABasicDataTest,GetAttributeByName)53 TEST(XFABasicDataTest, GetAttributeByName) {
54   Optional<XFA_ATTRIBUTEINFO> result = XFA_GetAttributeByName(L"");
55   EXPECT_FALSE(result.has_value());
56 
57   result = XFA_GetAttributeByName(L"nonesuch");
58   EXPECT_FALSE(result.has_value());
59 
60   result = XFA_GetAttributeByName(L"h");
61   ASSERT_TRUE(result.has_value());
62   EXPECT_EQ(XFA_Attribute::H, result.value().attribute);
63 
64   result = XFA_GetAttributeByName(L"short");
65   ASSERT_TRUE(result.has_value());
66   EXPECT_EQ(XFA_Attribute::Short, result.value().attribute);
67 
68   result = XFA_GetAttributeByName(L"decipherOnly");
69   ASSERT_TRUE(result.has_value());
70   EXPECT_EQ(XFA_Attribute::DecipherOnly, result.value().attribute);
71 }
72 
TEST(XFABasicDataTest,AttributeToName)73 TEST(XFABasicDataTest, AttributeToName) {
74   EXPECT_EQ("spaceBelow", XFA_AttributeToName(XFA_Attribute::SpaceBelow));
75   EXPECT_EQ("decipherOnly", XFA_AttributeToName(XFA_Attribute::DecipherOnly));
76 }
77 
TEST(XFABasicDataTest,GetAttributeValueByName)78 TEST(XFABasicDataTest, GetAttributeValueByName) {
79   Optional<XFA_AttributeValue> result = XFA_GetAttributeValueByName(L"");
80   EXPECT_FALSE(result.has_value());
81 
82   result = XFA_GetAttributeValueByName(L"nonesuch");
83   EXPECT_FALSE(result.has_value());
84 
85   result = XFA_GetAttributeValueByName(L"*");
86   ASSERT_TRUE(result.has_value());
87   EXPECT_EQ(XFA_AttributeValue::Asterisk, result.value());
88 
89   result = XFA_GetAttributeValueByName(L"visible");
90   ASSERT_TRUE(result.has_value());
91   EXPECT_EQ(XFA_AttributeValue::Visible, result.value());
92 
93   result = XFA_GetAttributeValueByName(L"lowered");
94   ASSERT_TRUE(result.has_value());
95   EXPECT_EQ(XFA_AttributeValue::Lowered, result.value());
96 }
97 
TEST(XFABasicDataTest,AttributeValueToName)98 TEST(XFABasicDataTest, AttributeValueToName) {
99   EXPECT_EQ("rl-tb", XFA_AttributeValueToName(XFA_AttributeValue::Rl_tb));
100   EXPECT_EQ("lowered", XFA_AttributeValueToName(XFA_AttributeValue::Lowered));
101 }
102