1 /*
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"
18 
19 #include "test/Test.h"
20 
21 namespace aapt {
22 
TEST(PrivateAttributeMoverTest,MovePrivateAttributes)23 TEST(PrivateAttributeMoverTest, MovePrivateAttributes) {
24   std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
25 
26   std::unique_ptr<ResourceTable> table =
27       test::ResourceTableBuilder()
28           .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),
33                           Visibility::Level::kPublic)
34           .SetSymbolState("android:attr/publicB", ResourceId(0x01010000),
35                           Visibility::Level::kPublic)
36           .Build();
37 
38   PrivateAttributeMover mover;
39   ASSERT_TRUE(mover.Consume(context.get(), table.get()));
40 
41   ResourceTablePackage* package = table->FindPackage("android");
42   ASSERT_NE(package, nullptr);
43 
44   ResourceTableType* type = package->FindType(ResourceType::kAttr);
45   ASSERT_NE(type, nullptr);
46   ASSERT_EQ(type->entries.size(), 2u);
47   EXPECT_NE(type->FindEntry("publicA"), nullptr);
48   EXPECT_NE(type->FindEntry("publicB"), nullptr);
49 
50   type = package->FindType(ResourceType::kAttrPrivate);
51   ASSERT_NE(type, nullptr);
52   ASSERT_EQ(type->entries.size(), 2u);
53   EXPECT_NE(type->FindEntry("privateA"), nullptr);
54   EXPECT_NE(type->FindEntry("privateB"), nullptr);
55 }
56 
TEST(PrivateAttributeMoverTest,LeavePrivateAttributesWhenNoPublicAttributesDefined)57 TEST(PrivateAttributeMoverTest, LeavePrivateAttributesWhenNoPublicAttributesDefined) {
58   std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
59 
60   std::unique_ptr<ResourceTable> table = test::ResourceTableBuilder()
61                                              .AddSimple("android:attr/privateA")
62                                              .AddSimple("android:attr/privateB")
63                                              .Build();
64 
65   PrivateAttributeMover mover;
66   ASSERT_TRUE(mover.Consume(context.get(), table.get()));
67 
68   ResourceTablePackage* package = table->FindPackage("android");
69   ASSERT_NE(package, nullptr);
70 
71   ResourceTableType* type = package->FindType(ResourceType::kAttr);
72   ASSERT_NE(type, nullptr);
73   ASSERT_EQ(type->entries.size(), 2u);
74 
75   type = package->FindType(ResourceType::kAttrPrivate);
76   ASSERT_EQ(type, nullptr);
77 }
78 
TEST(PrivateAttributeMoverTest,DoNotCreatePrivateAttrsIfNoneExist)79 TEST(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), Visibility::Level::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 
98 }  // namespace aapt
99