• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "Diagnostics.h"
18 #include "ResourceTable.h"
19 #include "ResourceValues.h"
20 #include "util/Util.h"
21 
22 #include "test/Builders.h"
23 
24 #include <algorithm>
25 #include <gtest/gtest.h>
26 #include <ostream>
27 #include <string>
28 
29 namespace aapt {
30 
TEST(ResourceTableTest,FailToAddResourceWithBadName)31 TEST(ResourceTableTest, FailToAddResourceWithBadName) {
32     ResourceTable table;
33 
34     EXPECT_FALSE(table.addResource(
35             ResourceNameRef(u"android", ResourceType::kId, u"hey,there"),
36             ConfigDescription{}, "",
37             test::ValueBuilder<Id>().setSource("test.xml", 21u).build(),
38             test::getDiagnostics()));
39 
40     EXPECT_FALSE(table.addResource(
41             ResourceNameRef(u"android", ResourceType::kId, u"hey:there"),
42             ConfigDescription{}, "",
43             test::ValueBuilder<Id>().setSource("test.xml", 21u).build(),
44             test::getDiagnostics()));
45 }
46 
TEST(ResourceTableTest,AddOneResource)47 TEST(ResourceTableTest, AddOneResource) {
48     ResourceTable table;
49 
50     EXPECT_TRUE(table.addResource(test::parseNameOrDie(u"@android:attr/id"),
51                                   ConfigDescription{},
52                                   "",
53                                   test::ValueBuilder<Id>()
54                                           .setSource("test/path/file.xml", 23u).build(),
55                                   test::getDiagnostics()));
56 
57     ASSERT_NE(nullptr, test::getValue<Id>(&table, u"@android:attr/id"));
58 }
59 
TEST(ResourceTableTest,AddMultipleResources)60 TEST(ResourceTableTest, AddMultipleResources) {
61     ResourceTable table;
62 
63     ConfigDescription config;
64     ConfigDescription languageConfig;
65     memcpy(languageConfig.language, "pl", sizeof(languageConfig.language));
66 
67     EXPECT_TRUE(table.addResource(
68             test::parseNameOrDie(u"@android:attr/layout_width"),
69             config,
70             "",
71             test::ValueBuilder<Id>().setSource("test/path/file.xml", 10u).build(),
72             test::getDiagnostics()));
73 
74     EXPECT_TRUE(table.addResource(
75             test::parseNameOrDie(u"@android:attr/id"),
76             config,
77             "",
78             test::ValueBuilder<Id>().setSource("test/path/file.xml", 12u).build(),
79             test::getDiagnostics()));
80 
81     EXPECT_TRUE(table.addResource(
82             test::parseNameOrDie(u"@android:string/ok"),
83             config,
84             "",
85             test::ValueBuilder<Id>().setSource("test/path/file.xml", 14u).build(),
86             test::getDiagnostics()));
87 
88     EXPECT_TRUE(table.addResource(
89             test::parseNameOrDie(u"@android:string/ok"),
90             languageConfig,
91             "",
92             test::ValueBuilder<BinaryPrimitive>(android::Res_value{})
93                     .setSource("test/path/file.xml", 20u)
94                     .build(),
95             test::getDiagnostics()));
96 
97     ASSERT_NE(nullptr, test::getValue<Id>(&table, u"@android:attr/layout_width"));
98     ASSERT_NE(nullptr, test::getValue<Id>(&table, u"@android:attr/id"));
99     ASSERT_NE(nullptr, test::getValue<Id>(&table, u"@android:string/ok"));
100     ASSERT_NE(nullptr, test::getValueForConfig<BinaryPrimitive>(&table, u"@android:string/ok",
101                                                                 languageConfig));
102 }
103 
TEST(ResourceTableTest,OverrideWeakResourceValue)104 TEST(ResourceTableTest, OverrideWeakResourceValue) {
105     ResourceTable table;
106 
107     ASSERT_TRUE(table.addResource(test::parseNameOrDie(u"@android:attr/foo"), ConfigDescription{},
108                                   "", util::make_unique<Attribute>(true), test::getDiagnostics()));
109 
110     Attribute* attr = test::getValue<Attribute>(&table, u"@android:attr/foo");
111     ASSERT_NE(nullptr, attr);
112     EXPECT_TRUE(attr->isWeak());
113 
114     ASSERT_TRUE(table.addResource(test::parseNameOrDie(u"@android:attr/foo"), ConfigDescription{},
115                                   "", util::make_unique<Attribute>(false), test::getDiagnostics()));
116 
117     attr = test::getValue<Attribute>(&table, u"@android:attr/foo");
118     ASSERT_NE(nullptr, attr);
119     EXPECT_FALSE(attr->isWeak());
120 }
121 
TEST(ResourceTableTest,ProductVaryingValues)122 TEST(ResourceTableTest, ProductVaryingValues) {
123     ResourceTable table;
124 
125     EXPECT_TRUE(table.addResource(test::parseNameOrDie(u"@android:string/foo"),
126                                   test::parseConfigOrDie("land"),
127                                   "tablet",
128                                   util::make_unique<Id>(),
129                                   test::getDiagnostics()));
130     EXPECT_TRUE(table.addResource(test::parseNameOrDie(u"@android:string/foo"),
131                                   test::parseConfigOrDie("land"),
132                                   "phone",
133                                   util::make_unique<Id>(),
134                                   test::getDiagnostics()));
135 
136     EXPECT_NE(nullptr, test::getValueForConfigAndProduct<Id>(&table, u"@android:string/foo",
137                                                              test::parseConfigOrDie("land"),
138                                                              "tablet"));
139     EXPECT_NE(nullptr, test::getValueForConfigAndProduct<Id>(&table, u"@android:string/foo",
140                                                              test::parseConfigOrDie("land"),
141                                                              "phone"));
142 
143     Maybe<ResourceTable::SearchResult> sr = table.findResource(
144             test::parseNameOrDie(u"@android:string/foo"));
145     AAPT_ASSERT_TRUE(sr);
146     std::vector<ResourceConfigValue*> values = sr.value().entry->findAllValues(
147             test::parseConfigOrDie("land"));
148     ASSERT_EQ(2u, values.size());
149     EXPECT_EQ(std::string("phone"), values[0]->product);
150     EXPECT_EQ(std::string("tablet"), values[1]->product);
151 }
152 
153 } // namespace aapt
154