blob: 3c5b8b4b7627eac9a0f7ac5ea4d5267e22aa189b [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 "Linker.h"
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080018#include "ResourceTable.h"
Adam Lesinski24aad162015-04-24 19:19:30 -070019#include "ResourceTableResolver.h"
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080020#include "ResourceValues.h"
21#include "Util.h"
22
23#include <androidfw/AssetManager.h>
24#include <gtest/gtest.h>
25#include <string>
26
27namespace aapt {
28
29struct LinkerTest : public ::testing::Test {
30 virtual void SetUp() override {
31 mTable = std::make_shared<ResourceTable>();
32 mTable->setPackage(u"android");
Adam Lesinski769de982015-04-10 19:43:55 -070033 mTable->setPackageId(0x01);
Adam Lesinski24aad162015-04-24 19:19:30 -070034 mLinker = std::make_shared<Linker>(mTable, std::make_shared<ResourceTableResolver>(
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080035 mTable, std::make_shared<android::AssetManager>()));
36
37 // Create a few attributes for use in the tests.
38
39 addResource(ResourceName{ {}, ResourceType::kAttr, u"integer" },
40 util::make_unique<Attribute>(false, android::ResTable_map::TYPE_INTEGER));
41
42 addResource(ResourceName{ {}, ResourceType::kAttr, u"string" },
43 util::make_unique<Attribute>(false, android::ResTable_map::TYPE_STRING));
44
45 addResource(ResourceName{ {}, ResourceType::kId, u"apple" }, util::make_unique<Id>());
46
47 addResource(ResourceName{ {}, ResourceType::kId, u"banana" }, util::make_unique<Id>());
48
49 std::unique_ptr<Attribute> flagAttr = util::make_unique<Attribute>(
50 false, android::ResTable_map::TYPE_FLAGS);
51 flagAttr->symbols.push_back(Attribute::Symbol{
52 ResourceNameRef{ u"android", ResourceType::kId, u"apple" }, 1 });
53 flagAttr->symbols.push_back(Attribute::Symbol{
54 ResourceNameRef{ u"android", ResourceType::kId, u"banana" }, 2 });
55 addResource(ResourceName{ {}, ResourceType::kAttr, u"flags" }, std::move(flagAttr));
56 }
57
58 /*
59 * Convenience method for adding resources with the default configuration and some
60 * bogus source line.
61 */
62 bool addResource(const ResourceNameRef& name, std::unique_ptr<Value> value) {
63 return mTable->addResource(name, {}, SourceLine{ "test.xml", 21 }, std::move(value));
64 }
65
66 std::shared_ptr<ResourceTable> mTable;
67 std::shared_ptr<Linker> mLinker;
68};
69
70TEST_F(LinkerTest, DoNotInterpretEscapedStringAsReference) {
71 ASSERT_TRUE(addResource(ResourceName{ u"android", ResourceType::kString, u"foo" },
72 util::make_unique<String>(mTable->getValueStringPool().makeRef(u"?123"))));
73
74 ASSERT_TRUE(mLinker->linkAndValidate());
75 EXPECT_TRUE(mLinker->getUnresolvedReferences().empty());
76}
77
78TEST_F(LinkerTest, EscapeAndConvertRawString) {
Adam Lesinski769de982015-04-10 19:43:55 -070079 std::unique_ptr<Style> style = util::make_unique<Style>(false);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080080 style->entries.push_back(Style::Entry{
81 ResourceNameRef{ u"android", ResourceType::kAttr, u"integer" },
82 util::make_unique<RawString>(mTable->getValueStringPool().makeRef(u" 123"))
83 });
84 const Style* result = style.get();
85 ASSERT_TRUE(addResource(ResourceName{ u"android", ResourceType::kStyle, u"foo" },
86 std::move(style)));
87
88 ASSERT_TRUE(mLinker->linkAndValidate());
89 EXPECT_TRUE(mLinker->getUnresolvedReferences().empty());
90
91 EXPECT_NE(nullptr, dynamic_cast<BinaryPrimitive*>(result->entries.front().value.get()));
92}
93
94TEST_F(LinkerTest, FailToConvertRawString) {
Adam Lesinski769de982015-04-10 19:43:55 -070095 std::unique_ptr<Style> style = util::make_unique<Style>(false);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080096 style->entries.push_back(Style::Entry{
97 ResourceNameRef{ u"android", ResourceType::kAttr, u"integer" },
98 util::make_unique<RawString>(mTable->getValueStringPool().makeRef(u"yo what is up?"))
99 });
100 ASSERT_TRUE(addResource(ResourceName{ u"android", ResourceType::kStyle, u"foo" },
101 std::move(style)));
102
103 ASSERT_FALSE(mLinker->linkAndValidate());
104}
105
106TEST_F(LinkerTest, ConvertRawStringToString) {
Adam Lesinski769de982015-04-10 19:43:55 -0700107 std::unique_ptr<Style> style = util::make_unique<Style>(false);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800108 style->entries.push_back(Style::Entry{
109 ResourceNameRef{ u"android", ResourceType::kAttr, u"string" },
110 util::make_unique<RawString>(
111 mTable->getValueStringPool().makeRef(u" \"this is \\u00fa\"."))
112 });
113 const Style* result = style.get();
114 ASSERT_TRUE(addResource(ResourceName{ u"android", ResourceType::kStyle, u"foo" },
115 std::move(style)));
116
117 ASSERT_TRUE(mLinker->linkAndValidate());
118 EXPECT_TRUE(mLinker->getUnresolvedReferences().empty());
119
120 const String* str = dynamic_cast<const String*>(result->entries.front().value.get());
121 ASSERT_NE(nullptr, str);
122 EXPECT_EQ(*str->value, u"this is \u00fa.");
123}
124
125TEST_F(LinkerTest, ConvertRawStringToFlags) {
Adam Lesinski769de982015-04-10 19:43:55 -0700126 std::unique_ptr<Style> style = util::make_unique<Style>(false);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800127 style->entries.push_back(Style::Entry{
128 ResourceNameRef{ u"android", ResourceType::kAttr, u"flags" },
129 util::make_unique<RawString>(mTable->getValueStringPool().makeRef(u"banana | apple"))
130 });
131 const Style* result = style.get();
132 ASSERT_TRUE(addResource(ResourceName{ u"android", ResourceType::kStyle, u"foo" },
133 std::move(style)));
134
135 ASSERT_TRUE(mLinker->linkAndValidate());
136 EXPECT_TRUE(mLinker->getUnresolvedReferences().empty());
137
138 const BinaryPrimitive* bin = dynamic_cast<const BinaryPrimitive*>(
139 result->entries.front().value.get());
140 ASSERT_NE(nullptr, bin);
141 EXPECT_EQ(bin->value.data, 1u | 2u);
142}
143
Adam Lesinski769de982015-04-10 19:43:55 -0700144TEST_F(LinkerTest, AllowReferenceWithOnlyResourceIdPointingToDifferentPackage) {
145 ASSERT_TRUE(addResource(ResourceName{ u"android", ResourceType::kInteger, u"foo" },
146 util::make_unique<Reference>(ResourceId{ 0x02, 0x01, 0x01 })));
147
148 ASSERT_TRUE(mLinker->linkAndValidate());
149 EXPECT_TRUE(mLinker->getUnresolvedReferences().empty());
150}
151
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800152} // namespace aapt