blob: 180bd11275df93c9125e1fcfd147a8370eb3c277 [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 Lesinski1ab598f2015-08-14 14:26:04 -070031struct ResourceTableTest : public ::testing::Test {
32 struct EmptyDiagnostics : public IDiagnostics {
33 void error(const DiagMessage& msg) override {}
34 void warn(const DiagMessage& msg) override {}
35 void note(const DiagMessage& msg) override {}
36 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080037
Adam Lesinski1ab598f2015-08-14 14:26:04 -070038 EmptyDiagnostics mDiagnostics;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080039};
40
Adam Lesinski1ab598f2015-08-14 14:26:04 -070041TEST_F(ResourceTableTest, FailToAddResourceWithBadName) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080042 ResourceTable table;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080043
44 EXPECT_FALSE(table.addResource(
Adam Lesinskie78fd612015-10-22 12:48:43 -070045 ResourceNameRef(u"android", ResourceType::kId, u"hey,there"),
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -080046 ConfigDescription{}, "",
Adam Lesinskie78fd612015-10-22 12:48:43 -070047 test::ValueBuilder<Id>().setSource("test.xml", 21u).build(),
48 &mDiagnostics));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080049
50 EXPECT_FALSE(table.addResource(
Adam Lesinskie78fd612015-10-22 12:48:43 -070051 ResourceNameRef(u"android", ResourceType::kId, u"hey:there"),
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -080052 ConfigDescription{}, "",
Adam Lesinskie78fd612015-10-22 12:48:43 -070053 test::ValueBuilder<Id>().setSource("test.xml", 21u).build(),
54 &mDiagnostics));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080055}
56
Adam Lesinski1ab598f2015-08-14 14:26:04 -070057TEST_F(ResourceTableTest, AddOneResource) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080058 ResourceTable table;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080059
Adam Lesinskie78fd612015-10-22 12:48:43 -070060 EXPECT_TRUE(table.addResource(test::parseNameOrDie(u"@android:attr/id"),
61 ConfigDescription{},
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -080062 "",
Adam Lesinskie78fd612015-10-22 12:48:43 -070063 test::ValueBuilder<Id>()
64 .setSource("test/path/file.xml", 23u).build(),
65 &mDiagnostics));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080066
Adam Lesinski1ab598f2015-08-14 14:26:04 -070067 ASSERT_NE(nullptr, test::getValue<Id>(&table, u"@android:attr/id"));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080068}
69
Adam Lesinski1ab598f2015-08-14 14:26:04 -070070TEST_F(ResourceTableTest, AddMultipleResources) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080071 ResourceTable table;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080072
73 ConfigDescription config;
74 ConfigDescription languageConfig;
75 memcpy(languageConfig.language, "pl", sizeof(languageConfig.language));
76
77 EXPECT_TRUE(table.addResource(
Adam Lesinski1ab598f2015-08-14 14:26:04 -070078 test::parseNameOrDie(u"@android:attr/layout_width"),
Adam Lesinskie78fd612015-10-22 12:48:43 -070079 config,
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -080080 "",
Adam Lesinskie78fd612015-10-22 12:48:43 -070081 test::ValueBuilder<Id>().setSource("test/path/file.xml", 10u).build(),
82 &mDiagnostics));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080083
84 EXPECT_TRUE(table.addResource(
Adam Lesinski1ab598f2015-08-14 14:26:04 -070085 test::parseNameOrDie(u"@android:attr/id"),
Adam Lesinskie78fd612015-10-22 12:48:43 -070086 config,
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -080087 "",
Adam Lesinskie78fd612015-10-22 12:48:43 -070088 test::ValueBuilder<Id>().setSource("test/path/file.xml", 12u).build(),
89 &mDiagnostics));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080090
91 EXPECT_TRUE(table.addResource(
Adam Lesinski1ab598f2015-08-14 14:26:04 -070092 test::parseNameOrDie(u"@android:string/ok"),
Adam Lesinskie78fd612015-10-22 12:48:43 -070093 config,
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -080094 "",
Adam Lesinskie78fd612015-10-22 12:48:43 -070095 test::ValueBuilder<Id>().setSource("test/path/file.xml", 14u).build(),
96 &mDiagnostics));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080097
98 EXPECT_TRUE(table.addResource(
Adam Lesinski1ab598f2015-08-14 14:26:04 -070099 test::parseNameOrDie(u"@android:string/ok"),
Adam Lesinskie78fd612015-10-22 12:48:43 -0700100 languageConfig,
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800101 "",
Adam Lesinskie78fd612015-10-22 12:48:43 -0700102 test::ValueBuilder<BinaryPrimitive>(android::Res_value{})
103 .setSource("test/path/file.xml", 20u)
104 .build(),
105 &mDiagnostics));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800106
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700107 ASSERT_NE(nullptr, test::getValue<Id>(&table, u"@android:attr/layout_width"));
108 ASSERT_NE(nullptr, test::getValue<Id>(&table, u"@android:attr/id"));
109 ASSERT_NE(nullptr, test::getValue<Id>(&table, u"@android:string/ok"));
110 ASSERT_NE(nullptr, test::getValueForConfig<BinaryPrimitive>(&table, u"@android:string/ok",
111 languageConfig));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800112}
113
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700114TEST_F(ResourceTableTest, OverrideWeakResourceValue) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800115 ResourceTable table;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800116
Adam Lesinskie78fd612015-10-22 12:48:43 -0700117 ASSERT_TRUE(table.addResource(test::parseNameOrDie(u"@android:attr/foo"), ConfigDescription{},
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800118 "", util::make_unique<Attribute>(true), &mDiagnostics));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800119
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700120 Attribute* attr = test::getValue<Attribute>(&table, u"@android:attr/foo");
121 ASSERT_NE(nullptr, attr);
122 EXPECT_TRUE(attr->isWeak());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800123
Adam Lesinskie78fd612015-10-22 12:48:43 -0700124 ASSERT_TRUE(table.addResource(test::parseNameOrDie(u"@android:attr/foo"), ConfigDescription{},
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800125 "", util::make_unique<Attribute>(false), &mDiagnostics));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800126
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700127 attr = test::getValue<Attribute>(&table, u"@android:attr/foo");
128 ASSERT_NE(nullptr, attr);
129 EXPECT_FALSE(attr->isWeak());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800130}
131
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800132TEST_F(ResourceTableTest, ProductVaryingValues) {
133 ResourceTable table;
134
135 EXPECT_TRUE(table.addResource(test::parseNameOrDie(u"@android:string/foo"),
136 test::parseConfigOrDie("land"),
137 "tablet",
138 util::make_unique<Id>(),
139 &mDiagnostics));
140 EXPECT_TRUE(table.addResource(test::parseNameOrDie(u"@android:string/foo"),
141 test::parseConfigOrDie("land"),
142 "phone",
143 util::make_unique<Id>(),
144 &mDiagnostics));
145
146 EXPECT_NE(nullptr, test::getValueForConfigAndProduct<Id>(&table, u"@android:string/foo",
147 test::parseConfigOrDie("land"),
148 "tablet"));
149 EXPECT_NE(nullptr, test::getValueForConfigAndProduct<Id>(&table, u"@android:string/foo",
150 test::parseConfigOrDie("land"),
151 "phone"));
152
153 Maybe<ResourceTable::SearchResult> sr = table.findResource(
154 test::parseNameOrDie(u"@android:string/foo"));
155 AAPT_ASSERT_TRUE(sr);
156 std::vector<ResourceConfigValue*> values = sr.value().entry->findAllValues(
157 test::parseConfigOrDie("land"));
158 ASSERT_EQ(2u, values.size());
159 EXPECT_EQ(std::string("phone"), values[0]->product);
160 EXPECT_EQ(std::string("tablet"), values[1]->product);
161}
162
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800163} // namespace aapt