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