blob: 2055a80aaba6c50df4b684518d1bcd777e1314ac [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
22#include "test/Common.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(
45 ResourceNameRef{ u"android", ResourceType::kId, u"hey,there" },
Adam Lesinski1ab598f2015-08-14 14:26:04 -070046 {}, Source{ "test.xml", 21 },
47 util::make_unique<Id>(), &mDiagnostics));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080048
49 EXPECT_FALSE(table.addResource(
50 ResourceNameRef{ u"android", ResourceType::kId, u"hey:there" },
Adam Lesinski1ab598f2015-08-14 14:26:04 -070051 {}, Source{ "test.xml", 21 },
52 util::make_unique<Id>(), &mDiagnostics));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080053}
54
Adam Lesinski1ab598f2015-08-14 14:26:04 -070055TEST_F(ResourceTableTest, AddOneResource) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080056 ResourceTable table;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080057
Adam Lesinski1ab598f2015-08-14 14:26:04 -070058 EXPECT_TRUE(table.addResource(test::parseNameOrDie(u"@android:attr/id"), {},
59 Source{ "test/path/file.xml", 23 },
60 util::make_unique<Id>(), &mDiagnostics));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080061
Adam Lesinski1ab598f2015-08-14 14:26:04 -070062 ASSERT_NE(nullptr, test::getValue<Id>(&table, u"@android:attr/id"));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080063}
64
Adam Lesinski1ab598f2015-08-14 14:26:04 -070065TEST_F(ResourceTableTest, AddMultipleResources) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080066 ResourceTable table;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080067
68 ConfigDescription config;
69 ConfigDescription languageConfig;
70 memcpy(languageConfig.language, "pl", sizeof(languageConfig.language));
71
72 EXPECT_TRUE(table.addResource(
Adam Lesinski1ab598f2015-08-14 14:26:04 -070073 test::parseNameOrDie(u"@android:attr/layout_width"),
74 config, Source{ "test/path/file.xml", 10 },
75 util::make_unique<Id>(), &mDiagnostics));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080076
77 EXPECT_TRUE(table.addResource(
Adam Lesinski1ab598f2015-08-14 14:26:04 -070078 test::parseNameOrDie(u"@android:attr/id"),
79 config, Source{ "test/path/file.xml", 12 },
80 util::make_unique<Id>(), &mDiagnostics));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080081
82 EXPECT_TRUE(table.addResource(
Adam Lesinski1ab598f2015-08-14 14:26:04 -070083 test::parseNameOrDie(u"@android:string/ok"),
84 config, Source{ "test/path/file.xml", 14 },
85 util::make_unique<Id>(), &mDiagnostics));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080086
87 EXPECT_TRUE(table.addResource(
Adam Lesinski1ab598f2015-08-14 14:26:04 -070088 test::parseNameOrDie(u"@android:string/ok"),
89 languageConfig, Source{ "test/path/file.xml", 20 },
90 util::make_unique<BinaryPrimitive>(android::Res_value{}), &mDiagnostics));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080091
Adam Lesinski1ab598f2015-08-14 14:26:04 -070092 ASSERT_NE(nullptr, test::getValue<Id>(&table, u"@android:attr/layout_width"));
93 ASSERT_NE(nullptr, test::getValue<Id>(&table, u"@android:attr/id"));
94 ASSERT_NE(nullptr, test::getValue<Id>(&table, u"@android:string/ok"));
95 ASSERT_NE(nullptr, test::getValueForConfig<BinaryPrimitive>(&table, u"@android:string/ok",
96 languageConfig));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080097}
98
Adam Lesinski1ab598f2015-08-14 14:26:04 -070099TEST_F(ResourceTableTest, OverrideWeakResourceValue) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800100 ResourceTable table;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800101
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700102 ASSERT_TRUE(table.addResource(test::parseNameOrDie(u"@android:attr/foo"), {}, {},
103 util::make_unique<Attribute>(true), &mDiagnostics));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800104
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700105 Attribute* attr = test::getValue<Attribute>(&table, u"@android:attr/foo");
106 ASSERT_NE(nullptr, attr);
107 EXPECT_TRUE(attr->isWeak());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800108
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700109 ASSERT_TRUE(table.addResource(test::parseNameOrDie(u"@android:attr/foo"), {}, {},
110 util::make_unique<Attribute>(false), &mDiagnostics));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800111
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700112 attr = test::getValue<Attribute>(&table, u"@android:attr/foo");
113 ASSERT_NE(nullptr, attr);
114 EXPECT_FALSE(attr->isWeak());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800115}
116
117} // namespace aapt