blob: e2b37be994ffb2224b3b941e36fe5500f9834815 [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
17#include "ResourceTable.h"
Adam Lesinskicacb28f2016-10-19 12:18:14 -070018#include "Diagnostics.h"
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080019#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 Lesinskicacb28f2016-10-19 12:18:14 -070030 ResourceTable table;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080031
Adam Lesinskice5e56e2016-10-21 17:56:45 -070032 EXPECT_FALSE(table.AddResource(
33 test::ParseNameOrDie("android:id/hey,there"), ConfigDescription{}, "",
34 test::ValueBuilder<Id>().SetSource("test.xml", 21u).Build(),
35 test::GetDiagnostics()));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080036
Adam Lesinskice5e56e2016-10-21 17:56:45 -070037 EXPECT_FALSE(table.AddResource(
38 test::ParseNameOrDie("android:id/hey:there"), ConfigDescription{}, "",
39 test::ValueBuilder<Id>().SetSource("test.xml", 21u).Build(),
40 test::GetDiagnostics()));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080041}
42
Adam Lesinskib1afa072017-03-29 13:52:38 -070043TEST(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
Adam Lesinskicc5609d2016-04-05 12:41:07 -070051TEST(ResourceTableTest, AddOneResource) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070052 ResourceTable table;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080053
Adam Lesinskice5e56e2016-10-21 17:56:45 -070054 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()));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080058
Adam Lesinskice5e56e2016-10-21 17:56:45 -070059 ASSERT_NE(nullptr, test::GetValue<Id>(&table, "android:attr/id"));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080060}
61
Adam Lesinskicc5609d2016-04-05 12:41:07 -070062TEST(ResourceTableTest, AddMultipleResources) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070063 ResourceTable table;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080064
Adam Lesinskicacb28f2016-10-19 12:18:14 -070065 ConfigDescription config;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070066 ConfigDescription language_config;
67 memcpy(language_config.language, "pl", sizeof(language_config.language));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080068
Adam Lesinskice5e56e2016-10-21 17:56:45 -070069 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()));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080073
Adam Lesinskice5e56e2016-10-21 17:56:45 -070074 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()));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080078
Adam Lesinskice5e56e2016-10-21 17:56:45 -070079 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()));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080083
Adam Lesinskice5e56e2016-10-21 17:56:45 -070084 EXPECT_TRUE(table.AddResource(
85 test::ParseNameOrDie("android:string/ok"), language_config, "",
Adam Lesinskicacb28f2016-10-19 12:18:14 -070086 test::ValueBuilder<BinaryPrimitive>(android::Res_value{})
Adam Lesinskice5e56e2016-10-21 17:56:45 -070087 .SetSource("test/path/file.xml", 20u)
88 .Build(),
89 test::GetDiagnostics()));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080090
Adam Lesinskice5e56e2016-10-21 17:56:45 -070091 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));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080096}
97
Adam Lesinskicc5609d2016-04-05 12:41:07 -070098TEST(ResourceTableTest, OverrideWeakResourceValue) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070099 ResourceTable table;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800100
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700101 ASSERT_TRUE(table.AddResource(
102 test::ParseNameOrDie("android:attr/foo"), ConfigDescription{}, "",
103 util::make_unique<Attribute>(true), test::GetDiagnostics()));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800104
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700105 Attribute* attr = test::GetValue<Attribute>(&table, "android:attr/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700106 ASSERT_NE(nullptr, attr);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700107 EXPECT_TRUE(attr->IsWeak());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800108
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700109 ASSERT_TRUE(table.AddResource(
110 test::ParseNameOrDie("android:attr/foo"), ConfigDescription{}, "",
111 util::make_unique<Attribute>(false), test::GetDiagnostics()));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800112
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700113 attr = test::GetValue<Attribute>(&table, "android:attr/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700114 ASSERT_NE(nullptr, attr);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700115 EXPECT_FALSE(attr->IsWeak());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800116}
117
Adam Lesinskicc5609d2016-04-05 12:41:07 -0700118TEST(ResourceTableTest, ProductVaryingValues) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700119 ResourceTable table;
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800120
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700121 EXPECT_TRUE(table.AddResource(test::ParseNameOrDie("android:string/foo"),
122 test::ParseConfigOrDie("land"), "tablet",
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700123 util::make_unique<Id>(),
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700124 test::GetDiagnostics()));
125 EXPECT_TRUE(table.AddResource(test::ParseNameOrDie("android:string/foo"),
126 test::ParseConfigOrDie("land"), "phone",
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700127 util::make_unique<Id>(),
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700128 test::GetDiagnostics()));
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800129
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700130 EXPECT_NE(nullptr, test::GetValueForConfigAndProduct<Id>(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700131 &table, "android:string/foo",
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700132 test::ParseConfigOrDie("land"), "tablet"));
133 EXPECT_NE(nullptr, test::GetValueForConfigAndProduct<Id>(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700134 &table, "android:string/foo",
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700135 test::ParseConfigOrDie("land"), "phone"));
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800136
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700137 Maybe<ResourceTable::SearchResult> sr =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700138 table.FindResource(test::ParseNameOrDie("android:string/foo"));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700139 AAPT_ASSERT_TRUE(sr);
140 std::vector<ResourceConfigValue*> values =
Adam Lesinskib1afa072017-03-29 13:52:38 -0700141 sr.value().entry->FindAllValues(test::ParseConfigOrDie("land"));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700142 ASSERT_EQ(2u, values.size());
143 EXPECT_EQ(std::string("phone"), values[0]->product);
144 EXPECT_EQ(std::string("tablet"), values[1]->product);
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800145}
146
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700147} // namespace aapt