blob: 668e4340e839d1f2cc07a01f38cbd89386477b76 [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 Lesinskice5e56e22016-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;
Adam Lesinski761d4342017-09-29 11:15:17 -070027using ::testing::Lt;
28using ::testing::Ne;
Adam Lesinski09f4d702017-08-08 10:39:55 -070029using ::testing::Not;
Adam Lesinskid5083f62017-01-16 15:07:21 -080030
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080031namespace aapt {
32
Adam Lesinski1ab598f2015-08-14 14:26:04 -070033TEST(JavaClassGeneratorTest, FailWhenEntryIsJavaKeyword) {
Adam Lesinskice5e56e22016-10-21 17:56:45 -070034 std::unique_ptr<ResourceTable> table =
35 test::ResourceTableBuilder()
36 .SetPackageId("android", 0x01)
37 .AddSimple("android:id/class", ResourceId(0x01020000))
38 .Build();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080039
Adam Lesinskice5e56e22016-10-21 17:56:45 -070040 std::unique_ptr<IAaptContext> context =
41 test::ContextBuilder()
42 .AddSymbolSource(
43 util::make_unique<ResourceTableSymbolSource>(table.get()))
44 .SetNameManglerPolicy(NameManglerPolicy{"android"})
45 .Build();
46 JavaClassGenerator generator(context.get(), table.get(), {});
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080047
Adam Lesinskice5e56e22016-10-21 17:56:45 -070048 std::stringstream out;
49 EXPECT_FALSE(generator.Generate("android", &out));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080050}
51
Adam Lesinski1ab598f2015-08-14 14:26:04 -070052TEST(JavaClassGeneratorTest, TransformInvalidJavaIdentifierCharacter) {
Adam Lesinskice5e56e22016-10-21 17:56:45 -070053 std::unique_ptr<ResourceTable> table =
54 test::ResourceTableBuilder()
55 .SetPackageId("android", 0x01)
56 .AddSimple("android:id/hey-man", ResourceId(0x01020000))
57 .AddValue("android:attr/cool.attr", ResourceId(0x01010000),
58 test::AttributeBuilder(false).Build())
Adam Lesinski09f4d702017-08-08 10:39:55 -070059 .AddValue("android:styleable/hey.dude", ResourceId(0x01030000),
60 test::StyleableBuilder()
61 .AddItem("android:attr/cool.attr", ResourceId(0x01010000))
62 .Build())
Adam Lesinskice5e56e22016-10-21 17:56:45 -070063 .Build();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080064
Adam Lesinskice5e56e22016-10-21 17:56:45 -070065 std::unique_ptr<IAaptContext> context =
66 test::ContextBuilder()
Adam Lesinski09f4d702017-08-08 10:39:55 -070067 .AddSymbolSource(util::make_unique<ResourceTableSymbolSource>(table.get()))
Adam Lesinskice5e56e22016-10-21 17:56:45 -070068 .SetNameManglerPolicy(NameManglerPolicy{"android"})
69 .Build();
70 JavaClassGenerator generator(context.get(), table.get(), {});
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080071
Adam Lesinskice5e56e22016-10-21 17:56:45 -070072 std::stringstream out;
73 EXPECT_TRUE(generator.Generate("android", &out));
Adam Lesinski1ab598f2015-08-14 14:26:04 -070074
Adam Lesinskice5e56e22016-10-21 17:56:45 -070075 std::string output = out.str();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080076
Adam Lesinski09f4d702017-08-08 10:39:55 -070077 EXPECT_THAT(output, HasSubstr("public static final int hey_man=0x01020000;"));
78 EXPECT_THAT(output, HasSubstr("public static final int[] hey_dude={"));
79 EXPECT_THAT(output, HasSubstr("public static final int hey_dude_cool_attr=0;"));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080080}
81
Adam Lesinski9e10ac72015-10-16 14:37:48 -070082TEST(JavaClassGeneratorTest, CorrectPackageNameIsUsed) {
Adam Lesinskice5e56e22016-10-21 17:56:45 -070083 std::unique_ptr<ResourceTable> table =
84 test::ResourceTableBuilder()
85 .SetPackageId("android", 0x01)
86 .AddSimple("android:id/one", ResourceId(0x01020000))
87 .AddSimple("android:id/com.foo$two", ResourceId(0x01020001))
88 .Build();
Adam Lesinski9e10ac72015-10-16 14:37:48 -070089
Adam Lesinskice5e56e22016-10-21 17:56:45 -070090 std::unique_ptr<IAaptContext> context =
91 test::ContextBuilder()
Adam Lesinski09f4d702017-08-08 10:39:55 -070092 .AddSymbolSource(util::make_unique<ResourceTableSymbolSource>(table.get()))
Adam Lesinskice5e56e22016-10-21 17:56:45 -070093 .SetNameManglerPolicy(NameManglerPolicy{"android"})
94 .Build();
95 JavaClassGenerator generator(context.get(), table.get(), {});
96 std::stringstream out;
97 ASSERT_TRUE(generator.Generate("android", "com.android.internal", &out));
Adam Lesinski9e10ac72015-10-16 14:37:48 -070098
Adam Lesinskice5e56e22016-10-21 17:56:45 -070099 std::string output = out.str();
Adam Lesinski09f4d702017-08-08 10:39:55 -0700100 EXPECT_THAT(output, HasSubstr("package com.android.internal;"));
101 EXPECT_THAT(output, HasSubstr("public static final int one=0x01020000;"));
102 EXPECT_THAT(output, Not(HasSubstr("two")));
103 EXPECT_THAT(output, Not(HasSubstr("com_foo$two")));
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700104}
105
106TEST(JavaClassGeneratorTest, AttrPrivateIsWrittenAsAttr) {
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700107 std::unique_ptr<ResourceTable> table =
108 test::ResourceTableBuilder()
109 .SetPackageId("android", 0x01)
110 .AddSimple("android:attr/two", ResourceId(0x01010001))
111 .AddSimple("android:^attr-private/one", ResourceId(0x01010000))
112 .Build();
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700113
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700114 std::unique_ptr<IAaptContext> context =
115 test::ContextBuilder()
Adam Lesinski09f4d702017-08-08 10:39:55 -0700116 .AddSymbolSource(util::make_unique<ResourceTableSymbolSource>(table.get()))
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700117 .SetNameManglerPolicy(NameManglerPolicy{"android"})
118 .Build();
119 JavaClassGenerator generator(context.get(), table.get(), {});
120 std::stringstream out;
121 ASSERT_TRUE(generator.Generate("android", &out));
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700122
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700123 std::string output = out.str();
Adam Lesinski09f4d702017-08-08 10:39:55 -0700124 EXPECT_THAT(output, HasSubstr("public static final class attr"));
125 EXPECT_THAT(output, Not(HasSubstr("public static final class ^attr-private")));
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700126}
127
128TEST(JavaClassGeneratorTest, OnlyWritePublicResources) {
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700129 StdErrDiagnostics diag;
130 std::unique_ptr<ResourceTable> table =
131 test::ResourceTableBuilder()
132 .SetPackageId("android", 0x01)
133 .AddSimple("android:id/one", ResourceId(0x01020000))
134 .AddSimple("android:id/two", ResourceId(0x01020001))
135 .AddSimple("android:id/three", ResourceId(0x01020002))
Adam Lesinski09f4d702017-08-08 10:39:55 -0700136 .SetSymbolState("android:id/one", ResourceId(0x01020000), SymbolState::kPublic)
137 .SetSymbolState("android:id/two", ResourceId(0x01020001), SymbolState::kPrivate)
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700138 .Build();
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700139
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700140 std::unique_ptr<IAaptContext> context =
141 test::ContextBuilder()
Adam Lesinski09f4d702017-08-08 10:39:55 -0700142 .AddSymbolSource(util::make_unique<ResourceTableSymbolSource>(table.get()))
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700143 .SetNameManglerPolicy(NameManglerPolicy{"android"})
144 .Build();
Adam Lesinski76565542016-03-10 21:55:04 -0800145
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700146 JavaClassGeneratorOptions options;
147 options.types = JavaClassGeneratorOptions::SymbolTypes::kPublic;
148 {
149 JavaClassGenerator generator(context.get(), table.get(), options);
150 std::stringstream out;
151 ASSERT_TRUE(generator.Generate("android", &out));
152 std::string output = out.str();
Adam Lesinski09f4d702017-08-08 10:39:55 -0700153 EXPECT_THAT(output, HasSubstr("public static final int one=0x01020000;"));
154 EXPECT_THAT(output, Not(HasSubstr("two")));
155 EXPECT_THAT(output, Not(HasSubstr("three")));
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700156 }
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700157
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700158 options.types = JavaClassGeneratorOptions::SymbolTypes::kPublicPrivate;
159 {
160 JavaClassGenerator generator(context.get(), table.get(), options);
161 std::stringstream out;
162 ASSERT_TRUE(generator.Generate("android", &out));
163 std::string output = out.str();
Adam Lesinski09f4d702017-08-08 10:39:55 -0700164 EXPECT_THAT(output, HasSubstr("public static final int one=0x01020000;"));
165 EXPECT_THAT(output, HasSubstr("public static final int two=0x01020001;"));
166 EXPECT_THAT(output, Not(HasSubstr("three")));
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700167 }
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700168
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700169 options.types = JavaClassGeneratorOptions::SymbolTypes::kAll;
170 {
171 JavaClassGenerator generator(context.get(), table.get(), options);
172 std::stringstream out;
173 ASSERT_TRUE(generator.Generate("android", &out));
174 std::string output = out.str();
Adam Lesinski09f4d702017-08-08 10:39:55 -0700175 EXPECT_THAT(output, HasSubstr("public static final int one=0x01020000;"));
176 EXPECT_THAT(output, HasSubstr("public static final int two=0x01020001;"));
177 EXPECT_THAT(output, HasSubstr("public static final int three=0x01020002;"));
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700178 }
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700179}
180
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700181/*
182 * TODO(adamlesinski): Re-enable this once we get merging working again.
183 * TEST(JavaClassGeneratorTest, EmitPackageMangledSymbols) {
Adam Lesinski769de982015-04-10 19:43:55 -0700184 ASSERT_TRUE(addResource(ResourceName{ {}, ResourceType::kId, u"foo" },
185 ResourceId{ 0x01, 0x02, 0x0000 }));
186 ResourceTable table;
187 table.setPackage(u"com.lib");
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700188 ASSERT_TRUE(table.addResource(ResourceName{ {}, ResourceType::kId, u"test"
189}, {},
190 Source{ "lib.xml", 33 },
191util::make_unique<Id>()));
Adam Lesinski769de982015-04-10 19:43:55 -0700192 ASSERT_TRUE(mTable->merge(std::move(table)));
193
Adam Lesinski330edcd2015-05-04 17:40:56 -0700194 Linker linker(mTable,
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700195 std::make_shared<MockResolver>(mTable, std::map<ResourceName,
196ResourceId>()),
Adam Lesinski330edcd2015-05-04 17:40:56 -0700197 {});
Adam Lesinski769de982015-04-10 19:43:55 -0700198 ASSERT_TRUE(linker.linkAndValidate());
199
200 JavaClassGenerator generator(mTable, {});
201
202 std::stringstream out;
203 EXPECT_TRUE(generator.generate(mTable->getPackage(), out));
204 std::string output = out.str();
205 EXPECT_NE(std::string::npos, output.find("int foo ="));
206 EXPECT_EQ(std::string::npos, output.find("int test ="));
207
208 out.str("");
209 EXPECT_TRUE(generator.generate(u"com.lib", out));
210 output = out.str();
211 EXPECT_NE(std::string::npos, output.find("int test ="));
212 EXPECT_EQ(std::string::npos, output.find("int foo ="));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700213}*/
Adam Lesinski838a6872015-05-01 13:14:05 -0700214
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700215TEST(JavaClassGeneratorTest, EmitOtherPackagesAttributesInStyleable) {
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700216 std::unique_ptr<ResourceTable> table =
217 test::ResourceTableBuilder()
218 .SetPackageId("android", 0x01)
219 .SetPackageId("com.lib", 0x02)
220 .AddValue("android:attr/bar", ResourceId(0x01010000),
221 test::AttributeBuilder(false).Build())
222 .AddValue("com.lib:attr/bar", ResourceId(0x02010000),
223 test::AttributeBuilder(false).Build())
224 .AddValue("android:styleable/foo", ResourceId(0x01030000),
225 test::StyleableBuilder()
226 .AddItem("android:attr/bar", ResourceId(0x01010000))
227 .AddItem("com.lib:attr/bar", ResourceId(0x02010000))
228 .Build())
229 .Build();
Adam Lesinski838a6872015-05-01 13:14:05 -0700230
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700231 std::unique_ptr<IAaptContext> context =
232 test::ContextBuilder()
Adam Lesinski09f4d702017-08-08 10:39:55 -0700233 .AddSymbolSource(util::make_unique<ResourceTableSymbolSource>(table.get()))
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700234 .SetNameManglerPolicy(NameManglerPolicy{"android"})
235 .Build();
236 JavaClassGenerator generator(context.get(), table.get(), {});
Adam Lesinski838a6872015-05-01 13:14:05 -0700237
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700238 std::stringstream out;
239 EXPECT_TRUE(generator.Generate("android", &out));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700240
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700241 std::string output = out.str();
Adam Lesinski09f4d702017-08-08 10:39:55 -0700242 EXPECT_THAT(output, HasSubstr("int foo_bar="));
243 EXPECT_THAT(output, HasSubstr("int foo_com_lib_bar="));
Adam Lesinski838a6872015-05-01 13:14:05 -0700244}
Adam Lesinski769de982015-04-10 19:43:55 -0700245
Adam Lesinski3b4cd942015-10-30 16:31:42 -0700246TEST(JavaClassGeneratorTest, CommentsForSimpleResourcesArePresent) {
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700247 std::unique_ptr<ResourceTable> table =
248 test::ResourceTableBuilder()
249 .SetPackageId("android", 0x01)
250 .AddSimple("android:id/foo", ResourceId(0x01010000))
251 .Build();
252 test::GetValue<Id>(table.get(), "android:id/foo")
253 ->SetComment(std::string("This is a comment\n@deprecated"));
Adam Lesinski3b4cd942015-10-30 16:31:42 -0700254
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700255 std::unique_ptr<IAaptContext> context =
256 test::ContextBuilder()
Adam Lesinski09f4d702017-08-08 10:39:55 -0700257 .AddSymbolSource(util::make_unique<ResourceTableSymbolSource>(table.get()))
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700258 .SetNameManglerPolicy(NameManglerPolicy{"android"})
259 .Build();
260 JavaClassGenerator generator(context.get(), table.get(), {});
261 std::stringstream out;
262 ASSERT_TRUE(generator.Generate("android", &out));
Adam Lesinski09f4d702017-08-08 10:39:55 -0700263 std::string output = out.str();
Adam Lesinski3b4cd942015-10-30 16:31:42 -0700264
Adam Lesinski09f4d702017-08-08 10:39:55 -0700265 const char* expected_text =
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700266 R"EOF(/**
Adam Lesinski3b4cd942015-10-30 16:31:42 -0700267 * This is a comment
268 * @deprecated
269 */
270 @Deprecated
Adam Lesinski803c7c82016-04-06 16:09:43 -0700271 public static final int foo=0x01010000;)EOF";
Adam Lesinski09f4d702017-08-08 10:39:55 -0700272 EXPECT_THAT(output, HasSubstr(expected_text));
Adam Lesinski3b4cd942015-10-30 16:31:42 -0700273}
274
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700275TEST(JavaClassGeneratorTest, CommentsForEnumAndFlagAttributesArePresent) {}
Adam Lesinski3b4cd942015-10-30 16:31:42 -0700276
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800277TEST(JavaClassGeneratorTest, CommentsForStyleablesAndNestedAttributesArePresent) {
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700278 Attribute attr(false);
279 attr.SetComment(StringPiece("This is an attribute"));
Adam Lesinski3b4cd942015-10-30 16:31:42 -0700280
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700281 Styleable styleable;
Adam Lesinski09f4d702017-08-08 10:39:55 -0700282 styleable.entries.push_back(Reference(test::ParseNameOrDie("android:attr/one")));
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700283 styleable.SetComment(StringPiece("This is a styleable"));
Adam Lesinski3b4cd942015-10-30 16:31:42 -0700284
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700285 std::unique_ptr<ResourceTable> table =
286 test::ResourceTableBuilder()
287 .SetPackageId("android", 0x01)
288 .AddValue("android:attr/one", util::make_unique<Attribute>(attr))
289 .AddValue("android:styleable/Container",
290 std::unique_ptr<Styleable>(styleable.Clone(nullptr)))
291 .Build();
Adam Lesinski74605cd2016-03-03 15:39:50 -0800292
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700293 std::unique_ptr<IAaptContext> context =
294 test::ContextBuilder()
Adam Lesinski09f4d702017-08-08 10:39:55 -0700295 .AddSymbolSource(util::make_unique<ResourceTableSymbolSource>(table.get()))
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700296 .SetNameManglerPolicy(NameManglerPolicy{"android"})
297 .Build();
298 JavaClassGeneratorOptions options;
299 options.use_final = false;
300 JavaClassGenerator generator(context.get(), table.get(), options);
301 std::stringstream out;
302 ASSERT_TRUE(generator.Generate("android", &out));
Adam Lesinski09f4d702017-08-08 10:39:55 -0700303 std::string output = out.str();
Adam Lesinski74605cd2016-03-03 15:39:50 -0800304
Adam Lesinski09f4d702017-08-08 10:39:55 -0700305 EXPECT_THAT(output, HasSubstr("attr name android:one"));
306 EXPECT_THAT(output, HasSubstr("attr description"));
307 EXPECT_THAT(output, HasSubstr(attr.GetComment()));
308 EXPECT_THAT(output, HasSubstr(styleable.GetComment()));
Adam Lesinski3b4cd942015-10-30 16:31:42 -0700309}
310
Adam Lesinski761d4342017-09-29 11:15:17 -0700311TEST(JavaClassGeneratorTest, StyleableAndIndicesAreColocated) {
312 std::unique_ptr<ResourceTable> table =
313 test::ResourceTableBuilder()
314 .SetPackageId("android", 0x01)
315 .AddValue("android:attr/layout_gravity", util::make_unique<Attribute>())
316 .AddValue("android:attr/background", util::make_unique<Attribute>())
317 .AddValue("android:styleable/ActionBar",
318 test::StyleableBuilder()
319 .AddItem("android:attr/background", ResourceId(0x01010000))
320 .Build())
321 .AddValue("android:styleable/ActionBar.LayoutParams",
322 test::StyleableBuilder()
323 .AddItem("android:attr/layout_gravity", ResourceId(0x01010001))
324 .Build())
325 .Build();
326
327 std::unique_ptr<IAaptContext> context =
328 test::ContextBuilder()
329 .AddSymbolSource(util::make_unique<ResourceTableSymbolSource>(table.get()))
330 .SetNameManglerPolicy(NameManglerPolicy{"android"})
331 .Build();
332
333 JavaClassGeneratorOptions options;
334 JavaClassGenerator generator(context.get(), table.get(), {});
335 std::stringstream out;
336 ASSERT_TRUE(generator.Generate("android", &out));
337 std::string output = out.str();
338
339 std::string::size_type actionbar_pos = output.find("int[] ActionBar");
340 ASSERT_THAT(actionbar_pos, Ne(std::string::npos));
341
342 std::string::size_type actionbar_background_pos = output.find("int ActionBar_background");
343 ASSERT_THAT(actionbar_background_pos, Ne(std::string::npos));
344
345 std::string::size_type actionbar_layout_params_pos = output.find("int[] ActionBar_LayoutParams");
346 ASSERT_THAT(actionbar_layout_params_pos, Ne(std::string::npos));
347
348 std::string::size_type actionbar_layout_params_layout_gravity_pos =
349 output.find("int ActionBar_LayoutParams_layout_gravity");
350 ASSERT_THAT(actionbar_layout_params_layout_gravity_pos, Ne(std::string::npos));
351
352 EXPECT_THAT(actionbar_pos, Lt(actionbar_background_pos));
353 EXPECT_THAT(actionbar_pos, Lt(actionbar_layout_params_pos));
354 EXPECT_THAT(actionbar_background_pos, Lt(actionbar_layout_params_pos));
355 EXPECT_THAT(actionbar_layout_params_pos, Lt(actionbar_layout_params_layout_gravity_pos));
356}
357
Michael Wrightfeaf99f2016-05-06 17:16:06 +0100358TEST(JavaClassGeneratorTest, CommentsForRemovedAttributesAreNotPresentInClass) {
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700359 Attribute attr(false);
360 attr.SetComment(StringPiece("removed"));
Michael Wrightfeaf99f2016-05-06 17:16:06 +0100361
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700362 std::unique_ptr<ResourceTable> table =
363 test::ResourceTableBuilder()
364 .SetPackageId("android", 0x01)
365 .AddValue("android:attr/one", util::make_unique<Attribute>(attr))
366 .Build();
Michael Wrightfeaf99f2016-05-06 17:16:06 +0100367
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700368 std::unique_ptr<IAaptContext> context =
369 test::ContextBuilder()
Adam Lesinski09f4d702017-08-08 10:39:55 -0700370 .AddSymbolSource(util::make_unique<ResourceTableSymbolSource>(table.get()))
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700371 .SetNameManglerPolicy(NameManglerPolicy{"android"})
372 .Build();
373 JavaClassGeneratorOptions options;
374 options.use_final = false;
375 JavaClassGenerator generator(context.get(), table.get(), options);
376 std::stringstream out;
377 ASSERT_TRUE(generator.Generate("android", &out));
Adam Lesinski09f4d702017-08-08 10:39:55 -0700378 std::string output = out.str();
Michael Wrightfeaf99f2016-05-06 17:16:06 +0100379
Adam Lesinski09f4d702017-08-08 10:39:55 -0700380 EXPECT_THAT(output, Not(HasSubstr("@attr name android:one")));
381 EXPECT_THAT(output, Not(HasSubstr("@attr description")));
Michael Wrightfeaf99f2016-05-06 17:16:06 +0100382
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700383 // We should find @removed only in the attribute javadoc and not anywhere else
Adam Lesinski09f4d702017-08-08 10:39:55 -0700384 // (i.e. the class javadoc).
385 const std::string kRemoved("removed");
386 ASSERT_THAT(output, HasSubstr(kRemoved));
387 std::string after_first_match = output.substr(output.find(kRemoved) + kRemoved.size());
388 EXPECT_THAT(after_first_match, Not(HasSubstr(kRemoved)));
Michael Wrightfeaf99f2016-05-06 17:16:06 +0100389}
390
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800391TEST(JavaClassGeneratorTest, GenerateOnResourcesLoadedCallbackForSharedLibrary) {
392 std::unique_ptr<ResourceTable> table =
393 test::ResourceTableBuilder()
394 .SetPackageId("android", 0x00)
395 .AddValue("android:attr/foo", ResourceId(0x00010000), util::make_unique<Attribute>(false))
396 .AddValue("android:id/foo", ResourceId(0x00020000), util::make_unique<Id>())
397 .AddValue(
398 "android:style/foo", ResourceId(0x00030000),
399 test::StyleBuilder()
400 .AddItem("android:attr/foo", ResourceId(0x00010000), util::make_unique<Id>())
401 .Build())
402 .Build();
403
404 std::unique_ptr<IAaptContext> context =
405 test::ContextBuilder().SetPackageId(0x00).SetCompilationPackage("android").Build();
406
407 JavaClassGeneratorOptions options;
408 options.use_final = false;
Adam Lesinski09f4d702017-08-08 10:39:55 -0700409 options.rewrite_callback_options = OnResourcesLoadedCallbackOptions{{"com.foo", "com.boo"}};
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800410 JavaClassGenerator generator(context.get(), table.get(), options);
411
412 std::stringstream out;
413 ASSERT_TRUE(generator.Generate("android", &out));
414
Adam Lesinski09f4d702017-08-08 10:39:55 -0700415 std::string output = out.str();
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800416
Adam Lesinski09f4d702017-08-08 10:39:55 -0700417 EXPECT_THAT(output, HasSubstr("void onResourcesLoaded"));
418 EXPECT_THAT(output, HasSubstr("com.foo.R.onResourcesLoaded"));
419 EXPECT_THAT(output, HasSubstr("com.boo.R.onResourcesLoaded"));
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800420}
421
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700422} // namespace aapt