blob: 96bb10b9232c4379a5bfaea33fb220b42063a4c8 [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 "JavaClassGenerator.h"
Adam Lesinski769de982015-04-10 19:43:55 -070018#include "Linker.h"
19#include "Resolver.h"
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080020#include "ResourceTable.h"
21#include "ResourceValues.h"
22#include "Util.h"
23
24#include <gtest/gtest.h>
25#include <sstream>
26#include <string>
27
28namespace aapt {
29
30struct JavaClassGeneratorTest : public ::testing::Test {
31 virtual void SetUp() override {
32 mTable = std::make_shared<ResourceTable>();
33 mTable->setPackage(u"android");
34 mTable->setPackageId(0x01);
35 }
36
37 bool addResource(const ResourceNameRef& name, ResourceId id) {
38 return mTable->addResource(name, id, {}, SourceLine{ "test.xml", 21 },
39 util::make_unique<Id>());
40 }
41
42 std::shared_ptr<ResourceTable> mTable;
43};
44
45TEST_F(JavaClassGeneratorTest, FailWhenEntryIsJavaKeyword) {
46 ASSERT_TRUE(addResource(ResourceName{ {}, ResourceType::kId, u"class" },
47 ResourceId{ 0x01, 0x02, 0x0000 }));
48
49 JavaClassGenerator generator(mTable, {});
50
51 std::stringstream out;
Adam Lesinski769de982015-04-10 19:43:55 -070052 EXPECT_FALSE(generator.generate(mTable->getPackage(), out));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080053}
54
55TEST_F(JavaClassGeneratorTest, TransformInvalidJavaIdentifierCharacter) {
56 ASSERT_TRUE(addResource(ResourceName{ {}, ResourceType::kId, u"hey-man" },
57 ResourceId{ 0x01, 0x02, 0x0000 }));
58
59 ASSERT_TRUE(addResource(ResourceName{ {}, ResourceType::kAttr, u"cool.attr" },
60 ResourceId{ 0x01, 0x01, 0x0000 }));
61
62 std::unique_ptr<Styleable> styleable = util::make_unique<Styleable>();
63 Reference ref(ResourceName{ u"android", ResourceType::kAttr, u"cool.attr"});
64 ref.id = ResourceId{ 0x01, 0x01, 0x0000 };
65 styleable->entries.emplace_back(ref);
66
67 ASSERT_TRUE(mTable->addResource(ResourceName{ {}, ResourceType::kStyleable, u"hey.dude" },
68 ResourceId{ 0x01, 0x03, 0x0000 }, {},
69 SourceLine{ "test.xml", 21 }, std::move(styleable)));
70
71 JavaClassGenerator generator(mTable, {});
72
73 std::stringstream out;
Adam Lesinski769de982015-04-10 19:43:55 -070074 EXPECT_TRUE(generator.generate(mTable->getPackage(), out));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080075 std::string output = out.str();
76
77 EXPECT_NE(std::string::npos,
78 output.find("public static final int hey_man = 0x01020000;"));
79
80 EXPECT_NE(std::string::npos,
81 output.find("public static final int[] hey_dude = {"));
82
83 EXPECT_NE(std::string::npos,
84 output.find("public static final int hey_dude_cool_attr = 0;"));
85}
86
Adam Lesinski769de982015-04-10 19:43:55 -070087TEST_F(JavaClassGeneratorTest, EmitPackageMangledSymbols) {
88 ASSERT_TRUE(addResource(ResourceName{ {}, ResourceType::kId, u"foo" },
89 ResourceId{ 0x01, 0x02, 0x0000 }));
90 ResourceTable table;
91 table.setPackage(u"com.lib");
92 ASSERT_TRUE(table.addResource(ResourceName{ {}, ResourceType::kId, u"test" }, {},
93 SourceLine{ "lib.xml", 33 }, util::make_unique<Id>()));
94 ASSERT_TRUE(mTable->merge(std::move(table)));
95
96 std::shared_ptr<Resolver> resolver = std::make_shared<Resolver>(mTable,
97 std::make_shared<const android::AssetManager>());
98 Linker linker(mTable, resolver);
99 ASSERT_TRUE(linker.linkAndValidate());
100
101 JavaClassGenerator generator(mTable, {});
102
103 std::stringstream out;
104 EXPECT_TRUE(generator.generate(mTable->getPackage(), out));
105 std::string output = out.str();
106 EXPECT_NE(std::string::npos, output.find("int foo ="));
107 EXPECT_EQ(std::string::npos, output.find("int test ="));
108
109 out.str("");
110 EXPECT_TRUE(generator.generate(u"com.lib", out));
111 output = out.str();
112 EXPECT_NE(std::string::npos, output.find("int test ="));
113 EXPECT_EQ(std::string::npos, output.find("int foo ="));
114}
115
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800116} // namespace aapt