blob: 7fcf6e7ab5cd13d5093bca4f8c0536c76176e42e [file] [log] [blame]
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001/*
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 "link/Linkers.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070018
Adam Lesinskid0f116b2016-07-08 15:00:32 -070019#include "test/Test.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070020
21namespace aapt {
22
23TEST(PrivateAttributeMoverTest, MovePrivateAttributes) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070024 std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
Adam Lesinski1ab598f2015-08-14 14:26:04 -070025
Adam Lesinskicacb28f2016-10-19 12:18:14 -070026 std::unique_ptr<ResourceTable> table =
27 test::ResourceTableBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -070028 .AddSimple("android:attr/publicA")
29 .AddSimple("android:attr/privateA")
30 .AddSimple("android:attr/publicB")
31 .AddSimple("android:attr/privateB")
32 .SetSymbolState("android:attr/publicA", ResourceId(0x01010000),
Adam Lesinskicacb28f2016-10-19 12:18:14 -070033 SymbolState::kPublic)
Adam Lesinskice5e56e2016-10-21 17:56:45 -070034 .SetSymbolState("android:attr/publicB", ResourceId(0x01010000),
Adam Lesinskicacb28f2016-10-19 12:18:14 -070035 SymbolState::kPublic)
Adam Lesinskice5e56e2016-10-21 17:56:45 -070036 .Build();
Adam Lesinski1ab598f2015-08-14 14:26:04 -070037
Adam Lesinskicacb28f2016-10-19 12:18:14 -070038 PrivateAttributeMover mover;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070039 ASSERT_TRUE(mover.Consume(context.get(), table.get()));
Adam Lesinski1ab598f2015-08-14 14:26:04 -070040
Adam Lesinskice5e56e2016-10-21 17:56:45 -070041 ResourceTablePackage* package = table->FindPackage("android");
Adam Lesinskicacb28f2016-10-19 12:18:14 -070042 ASSERT_NE(package, nullptr);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070043
Adam Lesinskice5e56e2016-10-21 17:56:45 -070044 ResourceTableType* type = package->FindType(ResourceType::kAttr);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070045 ASSERT_NE(type, nullptr);
46 ASSERT_EQ(type->entries.size(), 2u);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070047 EXPECT_NE(type->FindEntry("publicA"), nullptr);
48 EXPECT_NE(type->FindEntry("publicB"), nullptr);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070049
Adam Lesinskice5e56e2016-10-21 17:56:45 -070050 type = package->FindType(ResourceType::kAttrPrivate);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070051 ASSERT_NE(type, nullptr);
52 ASSERT_EQ(type->entries.size(), 2u);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070053 EXPECT_NE(type->FindEntry("privateA"), nullptr);
54 EXPECT_NE(type->FindEntry("privateB"), nullptr);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070055}
56
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080057TEST(PrivateAttributeMoverTest, LeavePrivateAttributesWhenNoPublicAttributesDefined) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070058 std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
Adam Lesinski1ab598f2015-08-14 14:26:04 -070059
Adam Lesinskicacb28f2016-10-19 12:18:14 -070060 std::unique_ptr<ResourceTable> table = test::ResourceTableBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -070061 .AddSimple("android:attr/privateA")
62 .AddSimple("android:attr/privateB")
63 .Build();
Adam Lesinski1ab598f2015-08-14 14:26:04 -070064
Adam Lesinskicacb28f2016-10-19 12:18:14 -070065 PrivateAttributeMover mover;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070066 ASSERT_TRUE(mover.Consume(context.get(), table.get()));
Adam Lesinski1ab598f2015-08-14 14:26:04 -070067
Adam Lesinskice5e56e2016-10-21 17:56:45 -070068 ResourceTablePackage* package = table->FindPackage("android");
Adam Lesinskicacb28f2016-10-19 12:18:14 -070069 ASSERT_NE(package, nullptr);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070070
Adam Lesinskice5e56e2016-10-21 17:56:45 -070071 ResourceTableType* type = package->FindType(ResourceType::kAttr);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070072 ASSERT_NE(type, nullptr);
73 ASSERT_EQ(type->entries.size(), 2u);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070074
Adam Lesinskice5e56e2016-10-21 17:56:45 -070075 type = package->FindType(ResourceType::kAttrPrivate);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070076 ASSERT_EQ(type, nullptr);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070077}
78
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080079TEST(PrivateAttributeMoverTest, DoNotCreatePrivateAttrsIfNoneExist) {
80 std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
81 std::unique_ptr<ResourceTable> table =
82 test::ResourceTableBuilder()
83 .AddSimple("android:attr/pub")
84 .SetSymbolState("android:attr/pub", ResourceId(0x01010000), SymbolState::kPublic)
85 .Build();
86
87 ResourceTablePackage* package = table->FindPackage("android");
88 ASSERT_NE(nullptr, package);
89
90 ASSERT_EQ(nullptr, package->FindType(ResourceType::kAttrPrivate));
91
92 PrivateAttributeMover mover;
93 ASSERT_TRUE(mover.Consume(context.get(), table.get()));
94
95 ASSERT_EQ(nullptr, package->FindType(ResourceType::kAttrPrivate));
96}
97
Adam Lesinskicacb28f2016-10-19 12:18:14 -070098} // namespace aapt