• 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 "ResourceTable.h"
18 #include "Diagnostics.h"
19 #include "ResourceValues.h"
20 #include "test/Test.h"
21 #include "util/Util.h"
22 
23 #include <algorithm>
24 #include <ostream>
25 #include <string>
26 
27 namespace aapt {
28 
TEST(ResourceTableTest,FailToAddResourceWithBadName)29 TEST(ResourceTableTest, FailToAddResourceWithBadName) {
30   ResourceTable table;
31 
32   EXPECT_FALSE(table.AddResource(
33       test::ParseNameOrDie("android:id/hey,there"), ConfigDescription{}, "",
34       test::ValueBuilder<Id>().SetSource("test.xml", 21u).Build(),
35       test::GetDiagnostics()));
36 
37   EXPECT_FALSE(table.AddResource(
38       test::ParseNameOrDie("android:id/hey:there"), ConfigDescription{}, "",
39       test::ValueBuilder<Id>().SetSource("test.xml", 21u).Build(),
40       test::GetDiagnostics()));
41 }
42 
TEST(ResourceTableTest,AddResourceWithWeirdNameWhenAddingMangledResources)43 TEST(ResourceTableTest, AddResourceWithWeirdNameWhenAddingMangledResources) {
44   ResourceTable table;
45 
46   EXPECT_TRUE(table.AddResourceAllowMangled(
47       test::ParseNameOrDie("android:id/heythere       "), ConfigDescription{}, "",
48       test::ValueBuilder<Id>().SetSource("test.xml", 21u).Build(), test::GetDiagnostics()));
49 }
50 
TEST(ResourceTableTest,AddOneResource)51 TEST(ResourceTableTest, AddOneResource) {
52   ResourceTable table;
53 
54   EXPECT_TRUE(table.AddResource(
55       test::ParseNameOrDie("android:attr/id"), ConfigDescription{}, "",
56       test::ValueBuilder<Id>().SetSource("test/path/file.xml", 23u).Build(),
57       test::GetDiagnostics()));
58 
59   ASSERT_NE(nullptr, test::GetValue<Id>(&table, "android:attr/id"));
60 }
61 
TEST(ResourceTableTest,AddMultipleResources)62 TEST(ResourceTableTest, AddMultipleResources) {
63   ResourceTable table;
64 
65   ConfigDescription config;
66   ConfigDescription language_config;
67   memcpy(language_config.language, "pl", sizeof(language_config.language));
68 
69   EXPECT_TRUE(table.AddResource(
70       test::ParseNameOrDie("android:attr/layout_width"), config, "",
71       test::ValueBuilder<Id>().SetSource("test/path/file.xml", 10u).Build(),
72       test::GetDiagnostics()));
73 
74   EXPECT_TRUE(table.AddResource(
75       test::ParseNameOrDie("android:attr/id"), config, "",
76       test::ValueBuilder<Id>().SetSource("test/path/file.xml", 12u).Build(),
77       test::GetDiagnostics()));
78 
79   EXPECT_TRUE(table.AddResource(
80       test::ParseNameOrDie("android:string/ok"), config, "",
81       test::ValueBuilder<Id>().SetSource("test/path/file.xml", 14u).Build(),
82       test::GetDiagnostics()));
83 
84   EXPECT_TRUE(table.AddResource(
85       test::ParseNameOrDie("android:string/ok"), language_config, "",
86       test::ValueBuilder<BinaryPrimitive>(android::Res_value{})
87           .SetSource("test/path/file.xml", 20u)
88           .Build(),
89       test::GetDiagnostics()));
90 
91   ASSERT_NE(nullptr, test::GetValue<Id>(&table, "android:attr/layout_width"));
92   ASSERT_NE(nullptr, test::GetValue<Id>(&table, "android:attr/id"));
93   ASSERT_NE(nullptr, test::GetValue<Id>(&table, "android:string/ok"));
94   ASSERT_NE(nullptr, test::GetValueForConfig<BinaryPrimitive>(
95                          &table, "android:string/ok", language_config));
96 }
97 
TEST(ResourceTableTest,OverrideWeakResourceValue)98 TEST(ResourceTableTest, OverrideWeakResourceValue) {
99   ResourceTable table;
100 
101   ASSERT_TRUE(table.AddResource(
102       test::ParseNameOrDie("android:attr/foo"), ConfigDescription{}, "",
103       util::make_unique<Attribute>(true), test::GetDiagnostics()));
104 
105   Attribute* attr = test::GetValue<Attribute>(&table, "android:attr/foo");
106   ASSERT_NE(nullptr, attr);
107   EXPECT_TRUE(attr->IsWeak());
108 
109   ASSERT_TRUE(table.AddResource(
110       test::ParseNameOrDie("android:attr/foo"), ConfigDescription{}, "",
111       util::make_unique<Attribute>(false), test::GetDiagnostics()));
112 
113   attr = test::GetValue<Attribute>(&table, "android:attr/foo");
114   ASSERT_NE(nullptr, attr);
115   EXPECT_FALSE(attr->IsWeak());
116 }
117 
TEST(ResourceTableTest,ProductVaryingValues)118 TEST(ResourceTableTest, ProductVaryingValues) {
119   ResourceTable table;
120 
121   EXPECT_TRUE(table.AddResource(test::ParseNameOrDie("android:string/foo"),
122                                 test::ParseConfigOrDie("land"), "tablet",
123                                 util::make_unique<Id>(),
124                                 test::GetDiagnostics()));
125   EXPECT_TRUE(table.AddResource(test::ParseNameOrDie("android:string/foo"),
126                                 test::ParseConfigOrDie("land"), "phone",
127                                 util::make_unique<Id>(),
128                                 test::GetDiagnostics()));
129 
130   EXPECT_NE(nullptr, test::GetValueForConfigAndProduct<Id>(
131                          &table, "android:string/foo",
132                          test::ParseConfigOrDie("land"), "tablet"));
133   EXPECT_NE(nullptr, test::GetValueForConfigAndProduct<Id>(
134                          &table, "android:string/foo",
135                          test::ParseConfigOrDie("land"), "phone"));
136 
137   Maybe<ResourceTable::SearchResult> sr =
138       table.FindResource(test::ParseNameOrDie("android:string/foo"));
139   AAPT_ASSERT_TRUE(sr);
140   std::vector<ResourceConfigValue*> values =
141       sr.value().entry->FindAllValues(test::ParseConfigOrDie("land"));
142   ASSERT_EQ(2u, values.size());
143   EXPECT_EQ(std::string("phone"), values[0]->product);
144   EXPECT_EQ(std::string("tablet"), values[1]->product);
145 }
146 
147 }  // namespace aapt
148