blob: cf6660cbff2839b93a425c82bbd7cfdaaa55aa47 [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 Lesinskid0f116b2016-07-08 15:00:32 -070020#include "test/Test.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070021#include "util/Util.h"
22
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080023#include <algorithm>
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080024#include <ostream>
25#include <string>
26
27namespace aapt {
28
Adam Lesinskicc5609d2016-04-05 12:41:07 -070029TEST(ResourceTableTest, FailToAddResourceWithBadName) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080030 ResourceTable table;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080031
32 EXPECT_FALSE(table.addResource(
Adam Lesinskid0f116b2016-07-08 15:00:32 -070033 test::parseNameOrDie("@android:id/hey,there"),
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -080034 ConfigDescription{}, "",
Adam Lesinskie78fd612015-10-22 12:48:43 -070035 test::ValueBuilder<Id>().setSource("test.xml", 21u).build(),
Adam Lesinskicc5609d2016-04-05 12:41:07 -070036 test::getDiagnostics()));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080037
38 EXPECT_FALSE(table.addResource(
Adam Lesinskid0f116b2016-07-08 15:00:32 -070039 test::parseNameOrDie("@android:id/hey:there"),
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -080040 ConfigDescription{}, "",
Adam Lesinskie78fd612015-10-22 12:48:43 -070041 test::ValueBuilder<Id>().setSource("test.xml", 21u).build(),
Adam Lesinskicc5609d2016-04-05 12:41:07 -070042 test::getDiagnostics()));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080043}
44
Adam Lesinskicc5609d2016-04-05 12:41:07 -070045TEST(ResourceTableTest, AddOneResource) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080046 ResourceTable table;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080047
Adam Lesinskid0f116b2016-07-08 15:00:32 -070048 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()));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080053
Adam Lesinskid0f116b2016-07-08 15:00:32 -070054 ASSERT_NE(nullptr, test::getValue<Id>(&table, "@android:attr/id"));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080055}
56
Adam Lesinskicc5609d2016-04-05 12:41:07 -070057TEST(ResourceTableTest, AddMultipleResources) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080058 ResourceTable table;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080059
60 ConfigDescription config;
61 ConfigDescription languageConfig;
62 memcpy(languageConfig.language, "pl", sizeof(languageConfig.language));
63
64 EXPECT_TRUE(table.addResource(
Adam Lesinskid0f116b2016-07-08 15:00:32 -070065 test::parseNameOrDie("@android:attr/layout_width"),
66 config, "",
Adam Lesinskie78fd612015-10-22 12:48:43 -070067 test::ValueBuilder<Id>().setSource("test/path/file.xml", 10u).build(),
Adam Lesinskicc5609d2016-04-05 12:41:07 -070068 test::getDiagnostics()));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080069
70 EXPECT_TRUE(table.addResource(
Adam Lesinskid0f116b2016-07-08 15:00:32 -070071 test::parseNameOrDie("@android:attr/id"),
72 config, "",
Adam Lesinskie78fd612015-10-22 12:48:43 -070073 test::ValueBuilder<Id>().setSource("test/path/file.xml", 12u).build(),
Adam Lesinskicc5609d2016-04-05 12:41:07 -070074 test::getDiagnostics()));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080075
76 EXPECT_TRUE(table.addResource(
Adam Lesinskid0f116b2016-07-08 15:00:32 -070077 test::parseNameOrDie("@android:string/ok"),
78 config, "",
Adam Lesinskie78fd612015-10-22 12:48:43 -070079 test::ValueBuilder<Id>().setSource("test/path/file.xml", 14u).build(),
Adam Lesinskicc5609d2016-04-05 12:41:07 -070080 test::getDiagnostics()));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080081
82 EXPECT_TRUE(table.addResource(
Adam Lesinskid0f116b2016-07-08 15:00:32 -070083 test::parseNameOrDie("@android:string/ok"),
84 languageConfig, "",
Adam Lesinskie78fd612015-10-22 12:48:43 -070085 test::ValueBuilder<BinaryPrimitive>(android::Res_value{})
86 .setSource("test/path/file.xml", 20u)
87 .build(),
Adam Lesinskicc5609d2016-04-05 12:41:07 -070088 test::getDiagnostics()));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080089
Adam Lesinskid0f116b2016-07-08 15:00:32 -070090 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",
Adam Lesinski1ab598f2015-08-14 14:26:04 -070094 languageConfig));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080095}
96
Adam Lesinskicc5609d2016-04-05 12:41:07 -070097TEST(ResourceTableTest, OverrideWeakResourceValue) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080098 ResourceTable table;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080099
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700100 ASSERT_TRUE(table.addResource(
101 test::parseNameOrDie("@android:attr/foo"),
102 ConfigDescription{}, "",
103 util::make_unique<Attribute>(true),
104 test::getDiagnostics()));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800105
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700106 Attribute* attr = test::getValue<Attribute>(&table, "@android:attr/foo");
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700107 ASSERT_NE(nullptr, attr);
108 EXPECT_TRUE(attr->isWeak());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800109
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700110 ASSERT_TRUE(table.addResource(
111 test::parseNameOrDie("@android:attr/foo"),
112 ConfigDescription{}, "",
113 util::make_unique<Attribute>(false),
114 test::getDiagnostics()));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800115
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700116 attr = test::getValue<Attribute>(&table, "@android:attr/foo");
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700117 ASSERT_NE(nullptr, attr);
118 EXPECT_FALSE(attr->isWeak());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800119}
120
Adam Lesinskicc5609d2016-04-05 12:41:07 -0700121TEST(ResourceTableTest, ProductVaryingValues) {
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800122 ResourceTable table;
123
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700124 EXPECT_TRUE(table.addResource(test::parseNameOrDie("@android:string/foo"),
125 test::parseConfigOrDie("land"), "tablet",
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800126 util::make_unique<Id>(),
Adam Lesinskicc5609d2016-04-05 12:41:07 -0700127 test::getDiagnostics()));
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700128 EXPECT_TRUE(table.addResource(test::parseNameOrDie("@android:string/foo"),
129 test::parseConfigOrDie("land"), "phone",
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800130 util::make_unique<Id>(),
Adam Lesinskicc5609d2016-04-05 12:41:07 -0700131 test::getDiagnostics()));
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800132
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700133 EXPECT_NE(nullptr, test::getValueForConfigAndProduct<Id>(&table, "@android:string/foo",
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800134 test::parseConfigOrDie("land"),
135 "tablet"));
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700136 EXPECT_NE(nullptr, test::getValueForConfigAndProduct<Id>(&table, "@android:string/foo",
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800137 test::parseConfigOrDie("land"),
138 "phone"));
139
140 Maybe<ResourceTable::SearchResult> sr = table.findResource(
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700141 test::parseNameOrDie("@android:string/foo"));
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800142 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
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800150} // namespace aapt