11ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski/*
21ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski * Copyright (C) 2015 The Android Open Source Project
31ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski *
41ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski * Licensed under the Apache License, Version 2.0 (the "License");
51ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski * you may not use this file except in compliance with the License.
61ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski * You may obtain a copy of the License at
71ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski *
81ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski *      http://www.apache.org/licenses/LICENSE-2.0
91ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski *
101ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski * Unless required by applicable law or agreed to in writing, software
111ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski * distributed under the License is distributed on an "AS IS" BASIS,
121ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
131ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski * See the License for the specific language governing permissions and
141ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski * limitations under the License.
151ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski */
161ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski
171ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski#include "process/SymbolTable.h"
18ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski
1964587af8179affd38ee26543b748f2d63b7f67bbAdam Lesinski#include "test/Test.h"
201ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski
211ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinskinamespace aapt {
221ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski
2364587af8179affd38ee26543b748f2d63b7f67bbAdam LesinskiTEST(ResourceTableSymbolSourceTest, FindSymbols) {
24cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski  std::unique_ptr<ResourceTable> table =
25cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski      test::ResourceTableBuilder()
26ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski          .AddSimple("android:id/foo", ResourceId(0x01020000))
27ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski          .AddSimple("android:id/bar")
28ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski          .AddValue("android:attr/foo", ResourceId(0x01010000),
29ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski                    test::AttributeBuilder().Build())
30ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski          .Build();
31cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski
32ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski  ResourceTableSymbolSource symbol_source(table.get());
33f34b6f4f2b969b47a3f371eb9549e92ef1680d91Adam Lesinski  EXPECT_NE(nullptr, symbol_source.FindByName(test::ParseNameOrDie("android:id/foo")));
34f34b6f4f2b969b47a3f371eb9549e92ef1680d91Adam Lesinski  EXPECT_NE(nullptr, symbol_source.FindByName(test::ParseNameOrDie("android:id/bar")));
35cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski
36cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski  std::unique_ptr<SymbolTable::Symbol> s =
37ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski      symbol_source.FindByName(test::ParseNameOrDie("android:attr/foo"));
38cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski  ASSERT_NE(nullptr, s);
39cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski  EXPECT_NE(nullptr, s->attribute);
401ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski}
411ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski
4264587af8179affd38ee26543b748f2d63b7f67bbAdam LesinskiTEST(ResourceTableSymbolSourceTest, FindPrivateAttrSymbol) {
43cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski  std::unique_ptr<ResourceTable> table =
44cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski      test::ResourceTableBuilder()
45ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski          .AddValue("android:^attr-private/foo", ResourceId(0x01010000),
46ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski                    test::AttributeBuilder().Build())
47ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski          .Build();
48cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski
49ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski  ResourceTableSymbolSource symbol_source(table.get());
50cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski  std::unique_ptr<SymbolTable::Symbol> s =
51ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski      symbol_source.FindByName(test::ParseNameOrDie("android:attr/foo"));
52cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski  ASSERT_NE(nullptr, s);
53cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski  EXPECT_NE(nullptr, s->attribute);
541ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski}
551ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski
56ceb9b2f80f853059233cdd29057f39a5960a74aeAdam LesinskiTEST(SymbolTableTest, FindByName) {
57ceb9b2f80f853059233cdd29057f39a5960a74aeAdam Lesinski  std::unique_ptr<ResourceTable> table =
58ceb9b2f80f853059233cdd29057f39a5960a74aeAdam Lesinski      test::ResourceTableBuilder()
59ceb9b2f80f853059233cdd29057f39a5960a74aeAdam Lesinski          .AddSimple("com.android.app:id/foo")
60ceb9b2f80f853059233cdd29057f39a5960a74aeAdam Lesinski          .AddSimple("com.android.app:id/" + NameMangler::MangleEntry("com.android.lib", "foo"))
61ceb9b2f80f853059233cdd29057f39a5960a74aeAdam Lesinski          .Build();
62ceb9b2f80f853059233cdd29057f39a5960a74aeAdam Lesinski
63ceb9b2f80f853059233cdd29057f39a5960a74aeAdam Lesinski  NameMangler mangler(NameManglerPolicy{"com.android.app", {"com.android.lib"}});
64ceb9b2f80f853059233cdd29057f39a5960a74aeAdam Lesinski  SymbolTable symbol_table(&mangler);
65ceb9b2f80f853059233cdd29057f39a5960a74aeAdam Lesinski  symbol_table.AppendSource(util::make_unique<ResourceTableSymbolSource>(table.get()));
66ceb9b2f80f853059233cdd29057f39a5960a74aeAdam Lesinski
67ceb9b2f80f853059233cdd29057f39a5960a74aeAdam Lesinski  EXPECT_NE(nullptr, symbol_table.FindByName(test::ParseNameOrDie("id/foo")));
68ceb9b2f80f853059233cdd29057f39a5960a74aeAdam Lesinski  EXPECT_NE(nullptr, symbol_table.FindByName(test::ParseNameOrDie("com.android.lib:id/foo")));
69ceb9b2f80f853059233cdd29057f39a5960a74aeAdam Lesinski}
70ceb9b2f80f853059233cdd29057f39a5960a74aeAdam Lesinski
71cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski}  // namespace aapt
72