blob: 4f449f0db41a117729823b7a85b8d58ec234e189 [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
Adam Lesinskica5638f2015-10-21 14:42:43 -070017#include "java/JavaClassGenerator.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070018
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080019#include <sstream>
20#include <string>
21
Adam Lesinskice5e56e2016-10-21 17:56:45 -070022#include "test/Test.h"
23#include "util/Util.h"
24
Adam Lesinski09f4d702017-08-08 10:39:55 -070025using ::android::StringPiece;
26using ::testing::HasSubstr;
27using ::testing::Not;
Adam Lesinskid5083f62017-01-16 15:07:21 -080028
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080029namespace aapt {
30
Adam Lesinski1ab598f2015-08-14 14:26:04 -070031TEST(JavaClassGeneratorTest, FailWhenEntryIsJavaKeyword) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070032 std::unique_ptr<ResourceTable> table =
33 test::ResourceTableBuilder()
34 .SetPackageId("android", 0x01)
35 .AddSimple("android:id/class", ResourceId(0x01020000))
36 .Build();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080037
Adam Lesinskice5e56e2016-10-21 17:56:45 -070038 std::unique_ptr<IAaptContext> context =
39 test::ContextBuilder()
40 .AddSymbolSource(
41 util::make_unique<ResourceTableSymbolSource>(table.get()))
42 .SetNameManglerPolicy(NameManglerPolicy{"android"})
43 .Build();
44 JavaClassGenerator generator(context.get(), table.get(), {});
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080045
Adam Lesinskice5e56e2016-10-21 17:56:45 -070046 std::stringstream out;
47 EXPECT_FALSE(generator.Generate("android", &out));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080048}
49
Adam Lesinski1ab598f2015-08-14 14:26:04 -070050TEST(JavaClassGeneratorTest, TransformInvalidJavaIdentifierCharacter) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070051 std::unique_ptr<ResourceTable> table =
52 test::ResourceTableBuilder()
53 .SetPackageId("android", 0x01)
54 .AddSimple("android:id/hey-man", ResourceId(0x01020000))
55 .AddValue("android:attr/cool.attr", ResourceId(0x01010000),
56 test::AttributeBuilder(false).Build())
Adam Lesinski09f4d702017-08-08 10:39:55 -070057 .AddValue("android:styleable/hey.dude", ResourceId(0x01030000),
58 test::StyleableBuilder()
59 .AddItem("android:attr/cool.attr", ResourceId(0x01010000))
60 .Build())
Adam Lesinskice5e56e2016-10-21 17:56:45 -070061 .Build();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080062
Adam Lesinskice5e56e2016-10-21 17:56:45 -070063 std::unique_ptr<IAaptContext> context =
64 test::ContextBuilder()
Adam Lesinski09f4d702017-08-08 10:39:55 -070065 .AddSymbolSource(util::make_unique<ResourceTableSymbolSource>(table.get()))
Adam Lesinskice5e56e2016-10-21 17:56:45 -070066 .SetNameManglerPolicy(NameManglerPolicy{"android"})
67 .Build();
68 JavaClassGenerator generator(context.get(), table.get(), {});
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080069
Adam Lesinskice5e56e2016-10-21 17:56:45 -070070 std::stringstream out;
71 EXPECT_TRUE(generator.Generate("android", &out));
Adam Lesinski1ab598f2015-08-14 14:26:04 -070072
Adam Lesinskice5e56e2016-10-21 17:56:45 -070073 std::string output = out.str();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080074
Adam Lesinski09f4d702017-08-08 10:39:55 -070075 EXPECT_THAT(output, HasSubstr("public static final int hey_man=0x01020000;"));
76 EXPECT_THAT(output, HasSubstr("public static final int[] hey_dude={"));
77 EXPECT_THAT(output, HasSubstr("public static final int hey_dude_cool_attr=0;"));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080078}
79
Adam Lesinski9e10ac72015-10-16 14:37:48 -070080TEST(JavaClassGeneratorTest, CorrectPackageNameIsUsed) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070081 std::unique_ptr<ResourceTable> table =
82 test::ResourceTableBuilder()
83 .SetPackageId("android", 0x01)
84 .AddSimple("android:id/one", ResourceId(0x01020000))
85 .AddSimple("android:id/com.foo$two", ResourceId(0x01020001))
86 .Build();
Adam Lesinski9e10ac72015-10-16 14:37:48 -070087
Adam Lesinskice5e56e2016-10-21 17:56:45 -070088 std::unique_ptr<IAaptContext> context =
89 test::ContextBuilder()
Adam Lesinski09f4d702017-08-08 10:39:55 -070090 .AddSymbolSource(util::make_unique<ResourceTableSymbolSource>(table.get()))
Adam Lesinskice5e56e2016-10-21 17:56:45 -070091 .SetNameManglerPolicy(NameManglerPolicy{"android"})
92 .Build();
93 JavaClassGenerator generator(context.get(), table.get(), {});
94 std::stringstream out;
95 ASSERT_TRUE(generator.Generate("android", "com.android.internal", &out));
Adam Lesinski9e10ac72015-10-16 14:37:48 -070096
Adam Lesinskice5e56e2016-10-21 17:56:45 -070097 std::string output = out.str();
Adam Lesinski09f4d702017-08-08 10:39:55 -070098 EXPECT_THAT(output, HasSubstr("package com.android.internal;"));
99 EXPECT_THAT(output, HasSubstr("public static final int one=0x01020000;"));
100 EXPECT_THAT(output, Not(HasSubstr("two")));
101 EXPECT_THAT(output, Not(HasSubstr("com_foo$two")));
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700102}
103
104TEST(JavaClassGeneratorTest, AttrPrivateIsWrittenAsAttr) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700105 std::unique_ptr<ResourceTable> table =
106 test::ResourceTableBuilder()
107 .SetPackageId("android", 0x01)
108 .AddSimple("android:attr/two", ResourceId(0x01010001))
109 .AddSimple("android:^attr-private/one", ResourceId(0x01010000))
110 .Build();
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700111
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700112 std::unique_ptr<IAaptContext> context =
113 test::ContextBuilder()
Adam Lesinski09f4d702017-08-08 10:39:55 -0700114 .AddSymbolSource(util::make_unique<ResourceTableSymbolSource>(table.get()))
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700115 .SetNameManglerPolicy(NameManglerPolicy{"android"})
116 .Build();
117 JavaClassGenerator generator(context.get(), table.get(), {});
118 std::stringstream out;
119 ASSERT_TRUE(generator.Generate("android", &out));
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700120
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700121 std::string output = out.str();
Adam Lesinski09f4d702017-08-08 10:39:55 -0700122 EXPECT_THAT(output, HasSubstr("public static final class attr"));
123 EXPECT_THAT(output, Not(HasSubstr("public static final class ^attr-private")));
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700124}
125
126TEST(JavaClassGeneratorTest, OnlyWritePublicResources) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700127 StdErrDiagnostics diag;
128 std::unique_ptr<ResourceTable> table =
129 test::ResourceTableBuilder()
130 .SetPackageId("android", 0x01)
131 .AddSimple("android:id/one", ResourceId(0x01020000))
132 .AddSimple("android:id/two", ResourceId(0x01020001))
133 .AddSimple("android:id/three", ResourceId(0x01020002))
Adam Lesinski09f4d702017-08-08 10:39:55 -0700134 .SetSymbolState("android:id/one", ResourceId(0x01020000), SymbolState::kPublic)
135 .SetSymbolState("android:id/two", ResourceId(0x01020001), SymbolState::kPrivate)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700136 .Build();
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700137
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700138 std::unique_ptr<IAaptContext> context =
139 test::ContextBuilder()
Adam Lesinski09f4d702017-08-08 10:39:55 -0700140 .AddSymbolSource(util::make_unique<ResourceTableSymbolSource>(table.get()))
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700141 .SetNameManglerPolicy(NameManglerPolicy{"android"})
142 .Build();
Adam Lesinski76565542016-03-10 21:55:04 -0800143
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700144 JavaClassGeneratorOptions options;
145 options.types = JavaClassGeneratorOptions::SymbolTypes::kPublic;
146 {
147 JavaClassGenerator generator(context.get(), table.get(), options);
148 std::stringstream out;
149 ASSERT_TRUE(generator.Generate("android", &out));
150 std::string output = out.str();
Adam Lesinski09f4d702017-08-08 10:39:55 -0700151 EXPECT_THAT(output, HasSubstr("public static final int one=0x01020000;"));
152 EXPECT_THAT(output, Not(HasSubstr("two")));
153 EXPECT_THAT(output, Not(HasSubstr("three")));
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700154 }
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700155
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700156 options.types = JavaClassGeneratorOptions::SymbolTypes::kPublicPrivate;
157 {
158 JavaClassGenerator generator(context.get(), table.get(), options);
159 std::stringstream out;
160 ASSERT_TRUE(generator.Generate("android", &out));
161 std::string output = out.str();
Adam Lesinski09f4d702017-08-08 10:39:55 -0700162 EXPECT_THAT(output, HasSubstr("public static final int one=0x01020000;"));
163 EXPECT_THAT(output, HasSubstr("public static final int two=0x01020001;"));
164 EXPECT_THAT(output, Not(HasSubstr("three")));
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700165 }
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700166
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700167 options.types = JavaClassGeneratorOptions::SymbolTypes::kAll;
168 {
169 JavaClassGenerator generator(context.get(), table.get(), options);
170 std::stringstream out;
171 ASSERT_TRUE(generator.Generate("android", &out));
172 std::string output = out.str();
Adam Lesinski09f4d702017-08-08 10:39:55 -0700173 EXPECT_THAT(output, HasSubstr("public static final int one=0x01020000;"));
174 EXPECT_THAT(output, HasSubstr("public static final int two=0x01020001;"));
175 EXPECT_THAT(output, HasSubstr("public static final int three=0x01020002;"));
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700176 }
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700177}
178
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700179/*
180 * TODO(adamlesinski): Re-enable this once we get merging working again.
181 * TEST(JavaClassGeneratorTest, EmitPackageMangledSymbols) {
Adam Lesinski769de982015-04-10 19:43:55 -0700182 ASSERT_TRUE(addResource(ResourceName{ {}, ResourceType::kId, u"foo" },
183 ResourceId{ 0x01, 0x02, 0x0000 }));
184 ResourceTable table;
185 table.setPackage(u"com.lib");
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700186 ASSERT_TRUE(table.addResource(ResourceName{ {}, ResourceType::kId, u"test"
187}, {},
188 Source{ "lib.xml", 33 },
189util::make_unique<Id>()));
Adam Lesinski769de982015-04-10 19:43:55 -0700190 ASSERT_TRUE(mTable->merge(std::move(table)));
191
Adam Lesinski330edcd2015-05-04 17:40:56 -0700192 Linker linker(mTable,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700193 std::make_shared<MockResolver>(mTable, std::map<ResourceName,
194ResourceId>()),
Adam Lesinski330edcd2015-05-04 17:40:56 -0700195 {});
Adam Lesinski769de982015-04-10 19:43:55 -0700196 ASSERT_TRUE(linker.linkAndValidate());
197
198 JavaClassGenerator generator(mTable, {});
199
200 std::stringstream out;
201 EXPECT_TRUE(generator.generate(mTable->getPackage(), out));
202 std::string output = out.str();
203 EXPECT_NE(std::string::npos, output.find("int foo ="));
204 EXPECT_EQ(std::string::npos, output.find("int test ="));
205
206 out.str("");
207 EXPECT_TRUE(generator.generate(u"com.lib", out));
208 output = out.str();
209 EXPECT_NE(std::string::npos, output.find("int test ="));
210 EXPECT_EQ(std::string::npos, output.find("int foo ="));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700211}*/
Adam Lesinski838a6872015-05-01 13:14:05 -0700212
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700213TEST(JavaClassGeneratorTest, EmitOtherPackagesAttributesInStyleable) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700214 std::unique_ptr<ResourceTable> table =
215 test::ResourceTableBuilder()
216 .SetPackageId("android", 0x01)
217 .SetPackageId("com.lib", 0x02)
218 .AddValue("android:attr/bar", ResourceId(0x01010000),
219 test::AttributeBuilder(false).Build())
220 .AddValue("com.lib:attr/bar", ResourceId(0x02010000),
221 test::AttributeBuilder(false).Build())
222 .AddValue("android:styleable/foo", ResourceId(0x01030000),
223 test::StyleableBuilder()
224 .AddItem("android:attr/bar", ResourceId(0x01010000))
225 .AddItem("com.lib:attr/bar", ResourceId(0x02010000))
226 .Build())
227 .Build();
Adam Lesinski838a6872015-05-01 13:14:05 -0700228
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700229 std::unique_ptr<IAaptContext> context =
230 test::ContextBuilder()
Adam Lesinski09f4d702017-08-08 10:39:55 -0700231 .AddSymbolSource(util::make_unique<ResourceTableSymbolSource>(table.get()))
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700232 .SetNameManglerPolicy(NameManglerPolicy{"android"})
233 .Build();
234 JavaClassGenerator generator(context.get(), table.get(), {});
Adam Lesinski838a6872015-05-01 13:14:05 -0700235
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700236 std::stringstream out;
237 EXPECT_TRUE(generator.Generate("android", &out));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700238
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700239 std::string output = out.str();
Adam Lesinski09f4d702017-08-08 10:39:55 -0700240 EXPECT_THAT(output, HasSubstr("int foo_bar="));
241 EXPECT_THAT(output, HasSubstr("int foo_com_lib_bar="));
Adam Lesinski838a6872015-05-01 13:14:05 -0700242}
Adam Lesinski769de982015-04-10 19:43:55 -0700243
Adam Lesinski3b4cd942015-10-30 16:31:42 -0700244TEST(JavaClassGeneratorTest, CommentsForSimpleResourcesArePresent) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700245 std::unique_ptr<ResourceTable> table =
246 test::ResourceTableBuilder()
247 .SetPackageId("android", 0x01)
248 .AddSimple("android:id/foo", ResourceId(0x01010000))
249 .Build();
250 test::GetValue<Id>(table.get(), "android:id/foo")
251 ->SetComment(std::string("This is a comment\n@deprecated"));
Adam Lesinski3b4cd942015-10-30 16:31:42 -0700252
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700253 std::unique_ptr<IAaptContext> context =
254 test::ContextBuilder()
Adam Lesinski09f4d702017-08-08 10:39:55 -0700255 .AddSymbolSource(util::make_unique<ResourceTableSymbolSource>(table.get()))
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700256 .SetNameManglerPolicy(NameManglerPolicy{"android"})
257 .Build();
258 JavaClassGenerator generator(context.get(), table.get(), {});
259 std::stringstream out;
260 ASSERT_TRUE(generator.Generate("android", &out));
Adam Lesinski09f4d702017-08-08 10:39:55 -0700261 std::string output = out.str();
Adam Lesinski3b4cd942015-10-30 16:31:42 -0700262
Adam Lesinski09f4d702017-08-08 10:39:55 -0700263 const char* expected_text =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700264 R"EOF(/**
Adam Lesinski3b4cd942015-10-30 16:31:42 -0700265 * This is a comment
266 * @deprecated
267 */
268 @Deprecated
Adam Lesinski803c7c82016-04-06 16:09:43 -0700269 public static final int foo=0x01010000;)EOF";
Adam Lesinski09f4d702017-08-08 10:39:55 -0700270 EXPECT_THAT(output, HasSubstr(expected_text));
Adam Lesinski3b4cd942015-10-30 16:31:42 -0700271}
272
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700273TEST(JavaClassGeneratorTest, CommentsForEnumAndFlagAttributesArePresent) {}
Adam Lesinski3b4cd942015-10-30 16:31:42 -0700274
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800275TEST(JavaClassGeneratorTest, CommentsForStyleablesAndNestedAttributesArePresent) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700276 Attribute attr(false);
277 attr.SetComment(StringPiece("This is an attribute"));
Adam Lesinski3b4cd942015-10-30 16:31:42 -0700278
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700279 Styleable styleable;
Adam Lesinski09f4d702017-08-08 10:39:55 -0700280 styleable.entries.push_back(Reference(test::ParseNameOrDie("android:attr/one")));
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700281 styleable.SetComment(StringPiece("This is a styleable"));
Adam Lesinski3b4cd942015-10-30 16:31:42 -0700282
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700283 std::unique_ptr<ResourceTable> table =
284 test::ResourceTableBuilder()
285 .SetPackageId("android", 0x01)
286 .AddValue("android:attr/one", util::make_unique<Attribute>(attr))
287 .AddValue("android:styleable/Container",
288 std::unique_ptr<Styleable>(styleable.Clone(nullptr)))
289 .Build();
Adam Lesinski74605cd2016-03-03 15:39:50 -0800290
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700291 std::unique_ptr<IAaptContext> context =
292 test::ContextBuilder()
Adam Lesinski09f4d702017-08-08 10:39:55 -0700293 .AddSymbolSource(util::make_unique<ResourceTableSymbolSource>(table.get()))
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700294 .SetNameManglerPolicy(NameManglerPolicy{"android"})
295 .Build();
296 JavaClassGeneratorOptions options;
297 options.use_final = false;
298 JavaClassGenerator generator(context.get(), table.get(), options);
299 std::stringstream out;
300 ASSERT_TRUE(generator.Generate("android", &out));
Adam Lesinski09f4d702017-08-08 10:39:55 -0700301 std::string output = out.str();
Adam Lesinski74605cd2016-03-03 15:39:50 -0800302
Adam Lesinski09f4d702017-08-08 10:39:55 -0700303 EXPECT_THAT(output, HasSubstr("attr name android:one"));
304 EXPECT_THAT(output, HasSubstr("attr description"));
305 EXPECT_THAT(output, HasSubstr(attr.GetComment()));
306 EXPECT_THAT(output, HasSubstr(styleable.GetComment()));
Adam Lesinski3b4cd942015-10-30 16:31:42 -0700307}
308
Michael Wrightfeaf99f2016-05-06 17:16:06 +0100309TEST(JavaClassGeneratorTest, CommentsForRemovedAttributesAreNotPresentInClass) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700310 Attribute attr(false);
311 attr.SetComment(StringPiece("removed"));
Michael Wrightfeaf99f2016-05-06 17:16:06 +0100312
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700313 std::unique_ptr<ResourceTable> table =
314 test::ResourceTableBuilder()
315 .SetPackageId("android", 0x01)
316 .AddValue("android:attr/one", util::make_unique<Attribute>(attr))
317 .Build();
Michael Wrightfeaf99f2016-05-06 17:16:06 +0100318
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700319 std::unique_ptr<IAaptContext> context =
320 test::ContextBuilder()
Adam Lesinski09f4d702017-08-08 10:39:55 -0700321 .AddSymbolSource(util::make_unique<ResourceTableSymbolSource>(table.get()))
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700322 .SetNameManglerPolicy(NameManglerPolicy{"android"})
323 .Build();
324 JavaClassGeneratorOptions options;
325 options.use_final = false;
326 JavaClassGenerator generator(context.get(), table.get(), options);
327 std::stringstream out;
328 ASSERT_TRUE(generator.Generate("android", &out));
Adam Lesinski09f4d702017-08-08 10:39:55 -0700329 std::string output = out.str();
Michael Wrightfeaf99f2016-05-06 17:16:06 +0100330
Adam Lesinski09f4d702017-08-08 10:39:55 -0700331 EXPECT_THAT(output, Not(HasSubstr("@attr name android:one")));
332 EXPECT_THAT(output, Not(HasSubstr("@attr description")));
Michael Wrightfeaf99f2016-05-06 17:16:06 +0100333
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700334 // We should find @removed only in the attribute javadoc and not anywhere else
Adam Lesinski09f4d702017-08-08 10:39:55 -0700335 // (i.e. the class javadoc).
336 const std::string kRemoved("removed");
337 ASSERT_THAT(output, HasSubstr(kRemoved));
338 std::string after_first_match = output.substr(output.find(kRemoved) + kRemoved.size());
339 EXPECT_THAT(after_first_match, Not(HasSubstr(kRemoved)));
Michael Wrightfeaf99f2016-05-06 17:16:06 +0100340}
341
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800342TEST(JavaClassGeneratorTest, GenerateOnResourcesLoadedCallbackForSharedLibrary) {
343 std::unique_ptr<ResourceTable> table =
344 test::ResourceTableBuilder()
345 .SetPackageId("android", 0x00)
346 .AddValue("android:attr/foo", ResourceId(0x00010000), util::make_unique<Attribute>(false))
347 .AddValue("android:id/foo", ResourceId(0x00020000), util::make_unique<Id>())
348 .AddValue(
349 "android:style/foo", ResourceId(0x00030000),
350 test::StyleBuilder()
351 .AddItem("android:attr/foo", ResourceId(0x00010000), util::make_unique<Id>())
352 .Build())
353 .Build();
354
355 std::unique_ptr<IAaptContext> context =
356 test::ContextBuilder().SetPackageId(0x00).SetCompilationPackage("android").Build();
357
358 JavaClassGeneratorOptions options;
359 options.use_final = false;
Adam Lesinski09f4d702017-08-08 10:39:55 -0700360 options.rewrite_callback_options = OnResourcesLoadedCallbackOptions{{"com.foo", "com.boo"}};
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800361 JavaClassGenerator generator(context.get(), table.get(), options);
362
363 std::stringstream out;
364 ASSERT_TRUE(generator.Generate("android", &out));
365
Adam Lesinski09f4d702017-08-08 10:39:55 -0700366 std::string output = out.str();
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800367
Adam Lesinski09f4d702017-08-08 10:39:55 -0700368 EXPECT_THAT(output, HasSubstr("void onResourcesLoaded"));
369 EXPECT_THAT(output, HasSubstr("com.foo.R.onResourcesLoaded"));
370 EXPECT_THAT(output, HasSubstr("com.boo.R.onResourcesLoaded"));
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800371}
372
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700373} // namespace aapt