blob: d6c52ab83d93de9cddaf7d57b1d261baf74ed482 [file] [log] [blame]
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001/*
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
Adam Lesinski1ab598f2015-08-14 14:26:04 -070017#include "Diagnostics.h"
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080018#include "ResourceTable.h"
19#include "ResourceValues.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070020#include "util/Util.h"
21
Adam Lesinskie78fd612015-10-22 12:48:43 -070022#include "test/Builders.h"
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080023
24#include <algorithm>
25#include <gtest/gtest.h>
26#include <ostream>
27#include <string>
28
29namespace aapt {
30
Adam Lesinskicc5609d2016-04-05 12:41:07 -070031TEST(ResourceTableTest, FailToAddResourceWithBadName) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080032 ResourceTable table;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080033
34 EXPECT_FALSE(table.addResource(
Adam Lesinskie78fd612015-10-22 12:48:43 -070035 ResourceNameRef(u"android", ResourceType::kId, u"hey,there"),
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -080036 ConfigDescription{}, "",
Adam Lesinskie78fd612015-10-22 12:48:43 -070037 test::ValueBuilder<Id>().setSource("test.xml", 21u).build(),
Adam Lesinskicc5609d2016-04-05 12:41:07 -070038 test::getDiagnostics()));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080039
40 EXPECT_FALSE(table.addResource(
Adam Lesinskie78fd612015-10-22 12:48:43 -070041 ResourceNameRef(u"android", ResourceType::kId, u"hey:there"),
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -080042 ConfigDescription{}, "",
Adam Lesinskie78fd612015-10-22 12:48:43 -070043 test::ValueBuilder<Id>().setSource("test.xml", 21u).build(),
Adam Lesinskicc5609d2016-04-05 12:41:07 -070044 test::getDiagnostics()));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080045}
46
Adam Lesinskicc5609d2016-04-05 12:41:07 -070047TEST(ResourceTableTest, AddOneResource) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080048 ResourceTable table;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080049
Adam Lesinskie78fd612015-10-22 12:48:43 -070050 EXPECT_TRUE(table.addResource(test::parseNameOrDie(u"@android:attr/id"),
51 ConfigDescription{},
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -080052 "",
Adam Lesinskie78fd612015-10-22 12:48:43 -070053 test::ValueBuilder<Id>()
54 .setSource("test/path/file.xml", 23u).build(),
Adam Lesinskicc5609d2016-04-05 12:41:07 -070055 test::getDiagnostics()));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080056
Adam Lesinski1ab598f2015-08-14 14:26:04 -070057 ASSERT_NE(nullptr, test::getValue<Id>(&table, u"@android:attr/id"));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080058}
59
Adam Lesinskicc5609d2016-04-05 12:41:07 -070060TEST(ResourceTableTest, AddMultipleResources) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080061 ResourceTable table;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080062
63 ConfigDescription config;
64 ConfigDescription languageConfig;
65 memcpy(languageConfig.language, "pl", sizeof(languageConfig.language));
66
67 EXPECT_TRUE(table.addResource(
Adam Lesinski1ab598f2015-08-14 14:26:04 -070068 test::parseNameOrDie(u"@android:attr/layout_width"),
Adam Lesinskie78fd612015-10-22 12:48:43 -070069 config,
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -080070 "",
Adam Lesinskie78fd612015-10-22 12:48:43 -070071 test::ValueBuilder<Id>().setSource("test/path/file.xml", 10u).build(),
Adam Lesinskicc5609d2016-04-05 12:41:07 -070072 test::getDiagnostics()));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080073
74 EXPECT_TRUE(table.addResource(
Adam Lesinski1ab598f2015-08-14 14:26:04 -070075 test::parseNameOrDie(u"@android:attr/id"),
Adam Lesinskie78fd612015-10-22 12:48:43 -070076 config,
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -080077 "",
Adam Lesinskie78fd612015-10-22 12:48:43 -070078 test::ValueBuilder<Id>().setSource("test/path/file.xml", 12u).build(),
Adam Lesinskicc5609d2016-04-05 12:41:07 -070079 test::getDiagnostics()));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080080
81 EXPECT_TRUE(table.addResource(
Adam Lesinski1ab598f2015-08-14 14:26:04 -070082 test::parseNameOrDie(u"@android:string/ok"),
Adam Lesinskie78fd612015-10-22 12:48:43 -070083 config,
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -080084 "",
Adam Lesinskie78fd612015-10-22 12:48:43 -070085 test::ValueBuilder<Id>().setSource("test/path/file.xml", 14u).build(),
Adam Lesinskicc5609d2016-04-05 12:41:07 -070086 test::getDiagnostics()));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080087
88 EXPECT_TRUE(table.addResource(
Adam Lesinski1ab598f2015-08-14 14:26:04 -070089 test::parseNameOrDie(u"@android:string/ok"),
Adam Lesinskie78fd612015-10-22 12:48:43 -070090 languageConfig,
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -080091 "",
Adam Lesinskie78fd612015-10-22 12:48:43 -070092 test::ValueBuilder<BinaryPrimitive>(android::Res_value{})
93 .setSource("test/path/file.xml", 20u)
94 .build(),
Adam Lesinskicc5609d2016-04-05 12:41:07 -070095 test::getDiagnostics()));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080096
Adam Lesinski1ab598f2015-08-14 14:26:04 -070097 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));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800102}
103
Adam Lesinskicc5609d2016-04-05 12:41:07 -0700104TEST(ResourceTableTest, OverrideWeakResourceValue) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800105 ResourceTable table;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800106
Adam Lesinskie78fd612015-10-22 12:48:43 -0700107 ASSERT_TRUE(table.addResource(test::parseNameOrDie(u"@android:attr/foo"), ConfigDescription{},
Adam Lesinskicc5609d2016-04-05 12:41:07 -0700108 "", util::make_unique<Attribute>(true), test::getDiagnostics()));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800109
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700110 Attribute* attr = test::getValue<Attribute>(&table, u"@android:attr/foo");
111 ASSERT_NE(nullptr, attr);
112 EXPECT_TRUE(attr->isWeak());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800113
Adam Lesinskie78fd612015-10-22 12:48:43 -0700114 ASSERT_TRUE(table.addResource(test::parseNameOrDie(u"@android:attr/foo"), ConfigDescription{},
Adam Lesinskicc5609d2016-04-05 12:41:07 -0700115 "", util::make_unique<Attribute>(false), test::getDiagnostics()));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800116
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700117 attr = test::getValue<Attribute>(&table, u"@android:attr/foo");
118 ASSERT_NE(nullptr, attr);
119 EXPECT_FALSE(attr->isWeak());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800120}
121
Adam Lesinskicc5609d2016-04-05 12:41:07 -0700122TEST(ResourceTableTest, ProductVaryingValues) {
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800123 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>(),
Adam Lesinskicc5609d2016-04-05 12:41:07 -0700129 test::getDiagnostics()));
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800130 EXPECT_TRUE(table.addResource(test::parseNameOrDie(u"@android:string/foo"),
131 test::parseConfigOrDie("land"),
132 "phone",
133 util::make_unique<Id>(),
Adam Lesinskicc5609d2016-04-05 12:41:07 -0700134 test::getDiagnostics()));
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800135
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
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800153} // namespace aapt