| Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 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 | |
| Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 17 | #include "java/JavaClassGenerator.h" |
| Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 18 | |
| Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 19 | #include <string> |
| 20 | |
| Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 21 | #include "io/StringStream.h" |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 22 | #include "test/Test.h" |
| 23 | #include "util/Util.h" |
| 24 | |
| Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 25 | using ::aapt::io::StringOutputStream; |
| Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 26 | using ::android::StringPiece; |
| 27 | using ::testing::HasSubstr; |
| Adam Lesinski | 761d434 | 2017-09-29 11:15:17 -0700 | [diff] [blame] | 28 | using ::testing::Lt; |
| 29 | using ::testing::Ne; |
| Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 30 | using ::testing::Not; |
| Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 31 | |
| Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 32 | namespace aapt { |
| 33 | |
| Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 34 | TEST(JavaClassGeneratorTest, FailWhenEntryIsJavaKeyword) { |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 35 | std::unique_ptr<ResourceTable> table = |
| 36 | test::ResourceTableBuilder() |
| 37 | .SetPackageId("android", 0x01) |
| 38 | .AddSimple("android:id/class", ResourceId(0x01020000)) |
| 39 | .Build(); |
| Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 40 | |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 41 | std::unique_ptr<IAaptContext> context = |
| 42 | test::ContextBuilder() |
| 43 | .AddSymbolSource( |
| 44 | util::make_unique<ResourceTableSymbolSource>(table.get())) |
| 45 | .SetNameManglerPolicy(NameManglerPolicy{"android"}) |
| 46 | .Build(); |
| 47 | JavaClassGenerator generator(context.get(), table.get(), {}); |
| Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 48 | |
| Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 49 | std::string result; |
| 50 | StringOutputStream out(&result); |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 51 | EXPECT_FALSE(generator.Generate("android", &out)); |
| Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 52 | } |
| 53 | |
| Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 54 | TEST(JavaClassGeneratorTest, TransformInvalidJavaIdentifierCharacter) { |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 55 | std::unique_ptr<ResourceTable> table = |
| 56 | test::ResourceTableBuilder() |
| 57 | .SetPackageId("android", 0x01) |
| 58 | .AddSimple("android:id/hey-man", ResourceId(0x01020000)) |
| 59 | .AddValue("android:attr/cool.attr", ResourceId(0x01010000), |
| Adam Lesinski | 73bff1e | 2017-12-08 16:06:10 -0800 | [diff] [blame] | 60 | test::AttributeBuilder().Build()) |
| Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 61 | .AddValue("android:styleable/hey.dude", ResourceId(0x01030000), |
| 62 | test::StyleableBuilder() |
| 63 | .AddItem("android:attr/cool.attr", ResourceId(0x01010000)) |
| 64 | .Build()) |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 65 | .Build(); |
| Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 66 | |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 67 | std::unique_ptr<IAaptContext> context = |
| 68 | test::ContextBuilder() |
| Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 69 | .AddSymbolSource(util::make_unique<ResourceTableSymbolSource>(table.get())) |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 70 | .SetNameManglerPolicy(NameManglerPolicy{"android"}) |
| 71 | .Build(); |
| 72 | JavaClassGenerator generator(context.get(), table.get(), {}); |
| Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 73 | |
| Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 74 | std::string output; |
| 75 | StringOutputStream out(&output); |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 76 | EXPECT_TRUE(generator.Generate("android", &out)); |
| Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 77 | out.Flush(); |
| Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 78 | |
| Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 79 | EXPECT_THAT(output, HasSubstr("public static final int hey_man=0x01020000;")); |
| 80 | EXPECT_THAT(output, HasSubstr("public static final int[] hey_dude={")); |
| 81 | EXPECT_THAT(output, HasSubstr("public static final int hey_dude_cool_attr=0;")); |
| Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 82 | } |
| 83 | |
| Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 84 | TEST(JavaClassGeneratorTest, CorrectPackageNameIsUsed) { |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 85 | std::unique_ptr<ResourceTable> table = |
| 86 | test::ResourceTableBuilder() |
| 87 | .SetPackageId("android", 0x01) |
| 88 | .AddSimple("android:id/one", ResourceId(0x01020000)) |
| 89 | .AddSimple("android:id/com.foo$two", ResourceId(0x01020001)) |
| 90 | .Build(); |
| Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 91 | |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 92 | std::unique_ptr<IAaptContext> context = |
| 93 | test::ContextBuilder() |
| Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 94 | .AddSymbolSource(util::make_unique<ResourceTableSymbolSource>(table.get())) |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 95 | .SetNameManglerPolicy(NameManglerPolicy{"android"}) |
| 96 | .Build(); |
| 97 | JavaClassGenerator generator(context.get(), table.get(), {}); |
| Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 98 | |
| Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 99 | std::string output; |
| 100 | StringOutputStream out(&output); |
| 101 | ASSERT_TRUE(generator.Generate("android", "com.android.internal", &out)); |
| 102 | out.Flush(); |
| 103 | |
| Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 104 | EXPECT_THAT(output, HasSubstr("package com.android.internal;")); |
| 105 | EXPECT_THAT(output, HasSubstr("public static final int one=0x01020000;")); |
| 106 | EXPECT_THAT(output, Not(HasSubstr("two"))); |
| 107 | EXPECT_THAT(output, Not(HasSubstr("com_foo$two"))); |
| Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| Ryan Mitchell | 23cc5d5 | 2018-07-12 17:16:40 -0700 | [diff] [blame^] | 110 | TEST(JavaClassGeneratorTest, StyleableAttributesWithDifferentPackageName) { |
| 111 | std::unique_ptr<ResourceTable> table = |
| 112 | test::ResourceTableBuilder() |
| 113 | .SetPackageId("android", 0x01) |
| 114 | .SetPackageId("app", 0x7f) |
| 115 | .AddValue("app:attr/foo", ResourceId(0x7f010000), |
| 116 | test::AttributeBuilder().Build()) |
| 117 | .AddValue("app:attr/bar", ResourceId(0x7f010001), |
| 118 | test::AttributeBuilder().Build()) |
| 119 | .AddValue("android:attr/baz", ResourceId(0x01010000), |
| 120 | test::AttributeBuilder().Build()) |
| 121 | .AddValue("app:styleable/MyStyleable", ResourceId(0x7f030000), |
| 122 | test::StyleableBuilder() |
| 123 | .AddItem("app:attr/foo", ResourceId(0x7f010000)) |
| 124 | .AddItem("attr/bar", ResourceId(0x7f010001)) |
| 125 | .AddItem("android:attr/baz", ResourceId(0x01010000)) |
| 126 | .Build()) |
| 127 | .Build(); |
| 128 | |
| 129 | std::unique_ptr<IAaptContext> context = |
| 130 | test::ContextBuilder() |
| 131 | .AddSymbolSource(util::make_unique<ResourceTableSymbolSource>(table.get())) |
| 132 | .SetNameManglerPolicy(NameManglerPolicy{"custom"}) |
| 133 | .SetCompilationPackage("custom") |
| 134 | .Build(); |
| 135 | JavaClassGenerator generator(context.get(), table.get(), {}); |
| 136 | |
| 137 | std::string output; |
| 138 | StringOutputStream out(&output); |
| 139 | EXPECT_TRUE(generator.Generate("app", &out)); |
| 140 | out.Flush(); |
| 141 | |
| 142 | EXPECT_THAT(output, Not(HasSubstr("public static final int baz=0x01010000;"))); |
| 143 | EXPECT_THAT(output, HasSubstr("public static final int foo=0x7f010000;")); |
| 144 | EXPECT_THAT(output, HasSubstr("public static final int bar=0x7f010001;")); |
| 145 | |
| 146 | EXPECT_THAT(output, HasSubstr("public static final int MyStyleable_android_baz=0;")); |
| 147 | EXPECT_THAT(output, HasSubstr("public static final int MyStyleable_foo=1;")); |
| 148 | EXPECT_THAT(output, HasSubstr("public static final int MyStyleable_bar=2;")); |
| 149 | |
| 150 | EXPECT_THAT(output, HasSubstr("@link #MyStyleable_android_baz android:baz")); |
| 151 | EXPECT_THAT(output, HasSubstr("@link #MyStyleable_foo app:foo")); |
| 152 | EXPECT_THAT(output, HasSubstr("@link #MyStyleable_bar app:bar")); |
| 153 | |
| 154 | EXPECT_THAT(output, HasSubstr("@link android.R.attr#baz")); |
| 155 | EXPECT_THAT(output, HasSubstr("@link app.R.attr#foo")); |
| 156 | EXPECT_THAT(output, HasSubstr("@link app.R.attr#bar")); |
| 157 | } |
| 158 | |
| Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 159 | TEST(JavaClassGeneratorTest, AttrPrivateIsWrittenAsAttr) { |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 160 | std::unique_ptr<ResourceTable> table = |
| 161 | test::ResourceTableBuilder() |
| 162 | .SetPackageId("android", 0x01) |
| 163 | .AddSimple("android:attr/two", ResourceId(0x01010001)) |
| 164 | .AddSimple("android:^attr-private/one", ResourceId(0x01010000)) |
| 165 | .Build(); |
| Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 166 | |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 167 | std::unique_ptr<IAaptContext> context = |
| 168 | test::ContextBuilder() |
| Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 169 | .AddSymbolSource(util::make_unique<ResourceTableSymbolSource>(table.get())) |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 170 | .SetNameManglerPolicy(NameManglerPolicy{"android"}) |
| 171 | .Build(); |
| 172 | JavaClassGenerator generator(context.get(), table.get(), {}); |
| Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 173 | |
| Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 174 | std::string output; |
| 175 | StringOutputStream out(&output); |
| 176 | ASSERT_TRUE(generator.Generate("android", &out)); |
| 177 | out.Flush(); |
| 178 | |
| Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 179 | EXPECT_THAT(output, HasSubstr("public static final class attr")); |
| 180 | EXPECT_THAT(output, Not(HasSubstr("public static final class ^attr-private"))); |
| Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | TEST(JavaClassGeneratorTest, OnlyWritePublicResources) { |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 184 | StdErrDiagnostics diag; |
| 185 | std::unique_ptr<ResourceTable> table = |
| 186 | test::ResourceTableBuilder() |
| 187 | .SetPackageId("android", 0x01) |
| 188 | .AddSimple("android:id/one", ResourceId(0x01020000)) |
| 189 | .AddSimple("android:id/two", ResourceId(0x01020001)) |
| 190 | .AddSimple("android:id/three", ResourceId(0x01020002)) |
| Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 191 | .SetSymbolState("android:id/one", ResourceId(0x01020000), Visibility::Level::kPublic) |
| 192 | .SetSymbolState("android:id/two", ResourceId(0x01020001), Visibility::Level::kPrivate) |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 193 | .Build(); |
| Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 194 | |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 195 | std::unique_ptr<IAaptContext> context = |
| 196 | test::ContextBuilder() |
| Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 197 | .AddSymbolSource(util::make_unique<ResourceTableSymbolSource>(table.get())) |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 198 | .SetNameManglerPolicy(NameManglerPolicy{"android"}) |
| 199 | .Build(); |
| Adam Lesinski | 7656554 | 2016-03-10 21:55:04 -0800 | [diff] [blame] | 200 | |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 201 | JavaClassGeneratorOptions options; |
| 202 | options.types = JavaClassGeneratorOptions::SymbolTypes::kPublic; |
| 203 | { |
| 204 | JavaClassGenerator generator(context.get(), table.get(), options); |
| Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 205 | std::string output; |
| 206 | StringOutputStream out(&output); |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 207 | ASSERT_TRUE(generator.Generate("android", &out)); |
| Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 208 | out.Flush(); |
| 209 | |
| Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 210 | EXPECT_THAT(output, HasSubstr("public static final int one=0x01020000;")); |
| 211 | EXPECT_THAT(output, Not(HasSubstr("two"))); |
| 212 | EXPECT_THAT(output, Not(HasSubstr("three"))); |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 213 | } |
| Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 214 | |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 215 | options.types = JavaClassGeneratorOptions::SymbolTypes::kPublicPrivate; |
| 216 | { |
| 217 | JavaClassGenerator generator(context.get(), table.get(), options); |
| Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 218 | std::string output; |
| 219 | StringOutputStream out(&output); |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 220 | ASSERT_TRUE(generator.Generate("android", &out)); |
| Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 221 | out.Flush(); |
| 222 | |
| Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 223 | EXPECT_THAT(output, HasSubstr("public static final int one=0x01020000;")); |
| 224 | EXPECT_THAT(output, HasSubstr("public static final int two=0x01020001;")); |
| 225 | EXPECT_THAT(output, Not(HasSubstr("three"))); |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 226 | } |
| Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 227 | |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 228 | options.types = JavaClassGeneratorOptions::SymbolTypes::kAll; |
| 229 | { |
| 230 | JavaClassGenerator generator(context.get(), table.get(), options); |
| Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 231 | std::string output; |
| 232 | StringOutputStream out(&output); |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 233 | ASSERT_TRUE(generator.Generate("android", &out)); |
| Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 234 | out.Flush(); |
| 235 | |
| Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 236 | EXPECT_THAT(output, HasSubstr("public static final int one=0x01020000;")); |
| 237 | EXPECT_THAT(output, HasSubstr("public static final int two=0x01020001;")); |
| 238 | EXPECT_THAT(output, HasSubstr("public static final int three=0x01020002;")); |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 239 | } |
| Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 240 | } |
| 241 | |
| Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 242 | /* |
| 243 | * TODO(adamlesinski): Re-enable this once we get merging working again. |
| 244 | * TEST(JavaClassGeneratorTest, EmitPackageMangledSymbols) { |
| Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 245 | ASSERT_TRUE(addResource(ResourceName{ {}, ResourceType::kId, u"foo" }, |
| 246 | ResourceId{ 0x01, 0x02, 0x0000 })); |
| 247 | ResourceTable table; |
| 248 | table.setPackage(u"com.lib"); |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 249 | ASSERT_TRUE(table.addResource(ResourceName{ {}, ResourceType::kId, u"test" |
| 250 | }, {}, |
| 251 | Source{ "lib.xml", 33 }, |
| 252 | util::make_unique<Id>())); |
| Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 253 | ASSERT_TRUE(mTable->merge(std::move(table))); |
| 254 | |
| Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 255 | Linker linker(mTable, |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 256 | std::make_shared<MockResolver>(mTable, std::map<ResourceName, |
| 257 | ResourceId>()), |
| Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 258 | {}); |
| Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 259 | ASSERT_TRUE(linker.linkAndValidate()); |
| 260 | |
| 261 | JavaClassGenerator generator(mTable, {}); |
| 262 | |
| 263 | std::stringstream out; |
| 264 | EXPECT_TRUE(generator.generate(mTable->getPackage(), out)); |
| 265 | std::string output = out.str(); |
| 266 | EXPECT_NE(std::string::npos, output.find("int foo =")); |
| 267 | EXPECT_EQ(std::string::npos, output.find("int test =")); |
| 268 | |
| 269 | out.str(""); |
| 270 | EXPECT_TRUE(generator.generate(u"com.lib", out)); |
| 271 | output = out.str(); |
| 272 | EXPECT_NE(std::string::npos, output.find("int test =")); |
| 273 | EXPECT_EQ(std::string::npos, output.find("int foo =")); |
| Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 274 | }*/ |
| Adam Lesinski | 838a687 | 2015-05-01 13:14:05 -0700 | [diff] [blame] | 275 | |
| Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 276 | TEST(JavaClassGeneratorTest, EmitOtherPackagesAttributesInStyleable) { |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 277 | std::unique_ptr<ResourceTable> table = |
| 278 | test::ResourceTableBuilder() |
| 279 | .SetPackageId("android", 0x01) |
| 280 | .SetPackageId("com.lib", 0x02) |
| Adam Lesinski | 73bff1e | 2017-12-08 16:06:10 -0800 | [diff] [blame] | 281 | .AddValue("android:attr/bar", ResourceId(0x01010000), test::AttributeBuilder().Build()) |
| 282 | .AddValue("com.lib:attr/bar", ResourceId(0x02010000), test::AttributeBuilder().Build()) |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 283 | .AddValue("android:styleable/foo", ResourceId(0x01030000), |
| 284 | test::StyleableBuilder() |
| 285 | .AddItem("android:attr/bar", ResourceId(0x01010000)) |
| 286 | .AddItem("com.lib:attr/bar", ResourceId(0x02010000)) |
| 287 | .Build()) |
| 288 | .Build(); |
| Adam Lesinski | 838a687 | 2015-05-01 13:14:05 -0700 | [diff] [blame] | 289 | |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 290 | std::unique_ptr<IAaptContext> context = |
| 291 | test::ContextBuilder() |
| Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 292 | .AddSymbolSource(util::make_unique<ResourceTableSymbolSource>(table.get())) |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 293 | .SetNameManglerPolicy(NameManglerPolicy{"android"}) |
| 294 | .Build(); |
| 295 | JavaClassGenerator generator(context.get(), table.get(), {}); |
| Adam Lesinski | 838a687 | 2015-05-01 13:14:05 -0700 | [diff] [blame] | 296 | |
| Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 297 | std::string output; |
| 298 | StringOutputStream out(&output); |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 299 | EXPECT_TRUE(generator.Generate("android", &out)); |
| Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 300 | out.Flush(); |
| Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 301 | |
| Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 302 | EXPECT_THAT(output, HasSubstr("int foo_bar=")); |
| 303 | EXPECT_THAT(output, HasSubstr("int foo_com_lib_bar=")); |
| Adam Lesinski | 838a687 | 2015-05-01 13:14:05 -0700 | [diff] [blame] | 304 | } |
| Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 305 | |
| Adam Lesinski | 3b4cd94 | 2015-10-30 16:31:42 -0700 | [diff] [blame] | 306 | TEST(JavaClassGeneratorTest, CommentsForSimpleResourcesArePresent) { |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 307 | std::unique_ptr<ResourceTable> table = |
| 308 | test::ResourceTableBuilder() |
| 309 | .SetPackageId("android", 0x01) |
| 310 | .AddSimple("android:id/foo", ResourceId(0x01010000)) |
| 311 | .Build(); |
| 312 | test::GetValue<Id>(table.get(), "android:id/foo") |
| 313 | ->SetComment(std::string("This is a comment\n@deprecated")); |
| Adam Lesinski | 3b4cd94 | 2015-10-30 16:31:42 -0700 | [diff] [blame] | 314 | |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 315 | std::unique_ptr<IAaptContext> context = |
| 316 | test::ContextBuilder() |
| Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 317 | .AddSymbolSource(util::make_unique<ResourceTableSymbolSource>(table.get())) |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 318 | .SetNameManglerPolicy(NameManglerPolicy{"android"}) |
| 319 | .Build(); |
| 320 | JavaClassGenerator generator(context.get(), table.get(), {}); |
| Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 321 | |
| 322 | std::string output; |
| 323 | StringOutputStream out(&output); |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 324 | ASSERT_TRUE(generator.Generate("android", &out)); |
| Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 325 | out.Flush(); |
| Adam Lesinski | 3b4cd94 | 2015-10-30 16:31:42 -0700 | [diff] [blame] | 326 | |
| Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 327 | const char* expected_text = |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 328 | R"EOF(/** |
| Adam Lesinski | 3b4cd94 | 2015-10-30 16:31:42 -0700 | [diff] [blame] | 329 | * This is a comment |
| 330 | * @deprecated |
| 331 | */ |
| 332 | @Deprecated |
| Adam Lesinski | 803c7c8 | 2016-04-06 16:09:43 -0700 | [diff] [blame] | 333 | public static final int foo=0x01010000;)EOF"; |
| Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 334 | EXPECT_THAT(output, HasSubstr(expected_text)); |
| Adam Lesinski | 3b4cd94 | 2015-10-30 16:31:42 -0700 | [diff] [blame] | 335 | } |
| 336 | |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 337 | TEST(JavaClassGeneratorTest, CommentsForEnumAndFlagAttributesArePresent) {} |
| Adam Lesinski | 3b4cd94 | 2015-10-30 16:31:42 -0700 | [diff] [blame] | 338 | |
| Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 339 | TEST(JavaClassGeneratorTest, CommentsForStyleablesAndNestedAttributesArePresent) { |
| Adam Lesinski | 73bff1e | 2017-12-08 16:06:10 -0800 | [diff] [blame] | 340 | Attribute attr; |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 341 | attr.SetComment(StringPiece("This is an attribute")); |
| Adam Lesinski | 3b4cd94 | 2015-10-30 16:31:42 -0700 | [diff] [blame] | 342 | |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 343 | Styleable styleable; |
| Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 344 | styleable.entries.push_back(Reference(test::ParseNameOrDie("android:attr/one"))); |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 345 | styleable.SetComment(StringPiece("This is a styleable")); |
| Adam Lesinski | 3b4cd94 | 2015-10-30 16:31:42 -0700 | [diff] [blame] | 346 | |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 347 | std::unique_ptr<ResourceTable> table = |
| 348 | test::ResourceTableBuilder() |
| 349 | .SetPackageId("android", 0x01) |
| 350 | .AddValue("android:attr/one", util::make_unique<Attribute>(attr)) |
| 351 | .AddValue("android:styleable/Container", |
| 352 | std::unique_ptr<Styleable>(styleable.Clone(nullptr))) |
| 353 | .Build(); |
| Adam Lesinski | 74605cd | 2016-03-03 15:39:50 -0800 | [diff] [blame] | 354 | |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 355 | std::unique_ptr<IAaptContext> context = |
| 356 | test::ContextBuilder() |
| Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 357 | .AddSymbolSource(util::make_unique<ResourceTableSymbolSource>(table.get())) |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 358 | .SetNameManglerPolicy(NameManglerPolicy{"android"}) |
| 359 | .Build(); |
| 360 | JavaClassGeneratorOptions options; |
| 361 | options.use_final = false; |
| 362 | JavaClassGenerator generator(context.get(), table.get(), options); |
| Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 363 | |
| 364 | std::string output; |
| 365 | StringOutputStream out(&output); |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 366 | ASSERT_TRUE(generator.Generate("android", &out)); |
| Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 367 | out.Flush(); |
| Adam Lesinski | 74605cd | 2016-03-03 15:39:50 -0800 | [diff] [blame] | 368 | |
| Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 369 | EXPECT_THAT(output, HasSubstr("attr name android:one")); |
| 370 | EXPECT_THAT(output, HasSubstr("attr description")); |
| 371 | EXPECT_THAT(output, HasSubstr(attr.GetComment())); |
| 372 | EXPECT_THAT(output, HasSubstr(styleable.GetComment())); |
| Adam Lesinski | 3b4cd94 | 2015-10-30 16:31:42 -0700 | [diff] [blame] | 373 | } |
| 374 | |
| Adam Lesinski | 761d434 | 2017-09-29 11:15:17 -0700 | [diff] [blame] | 375 | TEST(JavaClassGeneratorTest, StyleableAndIndicesAreColocated) { |
| 376 | std::unique_ptr<ResourceTable> table = |
| 377 | test::ResourceTableBuilder() |
| 378 | .SetPackageId("android", 0x01) |
| 379 | .AddValue("android:attr/layout_gravity", util::make_unique<Attribute>()) |
| 380 | .AddValue("android:attr/background", util::make_unique<Attribute>()) |
| 381 | .AddValue("android:styleable/ActionBar", |
| 382 | test::StyleableBuilder() |
| 383 | .AddItem("android:attr/background", ResourceId(0x01010000)) |
| 384 | .Build()) |
| 385 | .AddValue("android:styleable/ActionBar.LayoutParams", |
| 386 | test::StyleableBuilder() |
| 387 | .AddItem("android:attr/layout_gravity", ResourceId(0x01010001)) |
| 388 | .Build()) |
| 389 | .Build(); |
| 390 | |
| 391 | std::unique_ptr<IAaptContext> context = |
| 392 | test::ContextBuilder() |
| 393 | .AddSymbolSource(util::make_unique<ResourceTableSymbolSource>(table.get())) |
| 394 | .SetNameManglerPolicy(NameManglerPolicy{"android"}) |
| 395 | .Build(); |
| 396 | |
| 397 | JavaClassGeneratorOptions options; |
| 398 | JavaClassGenerator generator(context.get(), table.get(), {}); |
| Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 399 | |
| 400 | std::string output; |
| 401 | StringOutputStream out(&output); |
| Adam Lesinski | 761d434 | 2017-09-29 11:15:17 -0700 | [diff] [blame] | 402 | ASSERT_TRUE(generator.Generate("android", &out)); |
| Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 403 | out.Flush(); |
| Adam Lesinski | 761d434 | 2017-09-29 11:15:17 -0700 | [diff] [blame] | 404 | |
| 405 | std::string::size_type actionbar_pos = output.find("int[] ActionBar"); |
| 406 | ASSERT_THAT(actionbar_pos, Ne(std::string::npos)); |
| 407 | |
| 408 | std::string::size_type actionbar_background_pos = output.find("int ActionBar_background"); |
| 409 | ASSERT_THAT(actionbar_background_pos, Ne(std::string::npos)); |
| 410 | |
| 411 | std::string::size_type actionbar_layout_params_pos = output.find("int[] ActionBar_LayoutParams"); |
| 412 | ASSERT_THAT(actionbar_layout_params_pos, Ne(std::string::npos)); |
| 413 | |
| 414 | std::string::size_type actionbar_layout_params_layout_gravity_pos = |
| 415 | output.find("int ActionBar_LayoutParams_layout_gravity"); |
| 416 | ASSERT_THAT(actionbar_layout_params_layout_gravity_pos, Ne(std::string::npos)); |
| 417 | |
| 418 | EXPECT_THAT(actionbar_pos, Lt(actionbar_background_pos)); |
| 419 | EXPECT_THAT(actionbar_pos, Lt(actionbar_layout_params_pos)); |
| 420 | EXPECT_THAT(actionbar_background_pos, Lt(actionbar_layout_params_pos)); |
| 421 | EXPECT_THAT(actionbar_layout_params_pos, Lt(actionbar_layout_params_layout_gravity_pos)); |
| 422 | } |
| 423 | |
| Michael Wright | feaf99f | 2016-05-06 17:16:06 +0100 | [diff] [blame] | 424 | TEST(JavaClassGeneratorTest, CommentsForRemovedAttributesAreNotPresentInClass) { |
| Adam Lesinski | 73bff1e | 2017-12-08 16:06:10 -0800 | [diff] [blame] | 425 | Attribute attr; |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 426 | attr.SetComment(StringPiece("removed")); |
| Michael Wright | feaf99f | 2016-05-06 17:16:06 +0100 | [diff] [blame] | 427 | |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 428 | std::unique_ptr<ResourceTable> table = |
| 429 | test::ResourceTableBuilder() |
| 430 | .SetPackageId("android", 0x01) |
| 431 | .AddValue("android:attr/one", util::make_unique<Attribute>(attr)) |
| 432 | .Build(); |
| Michael Wright | feaf99f | 2016-05-06 17:16:06 +0100 | [diff] [blame] | 433 | |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 434 | std::unique_ptr<IAaptContext> context = |
| 435 | test::ContextBuilder() |
| Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 436 | .AddSymbolSource(util::make_unique<ResourceTableSymbolSource>(table.get())) |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 437 | .SetNameManglerPolicy(NameManglerPolicy{"android"}) |
| 438 | .Build(); |
| 439 | JavaClassGeneratorOptions options; |
| 440 | options.use_final = false; |
| 441 | JavaClassGenerator generator(context.get(), table.get(), options); |
| Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 442 | |
| 443 | std::string output; |
| 444 | StringOutputStream out(&output); |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 445 | ASSERT_TRUE(generator.Generate("android", &out)); |
| Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 446 | out.Flush(); |
| Michael Wright | feaf99f | 2016-05-06 17:16:06 +0100 | [diff] [blame] | 447 | |
| Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 448 | EXPECT_THAT(output, Not(HasSubstr("@attr name android:one"))); |
| 449 | EXPECT_THAT(output, Not(HasSubstr("@attr description"))); |
| Michael Wright | feaf99f | 2016-05-06 17:16:06 +0100 | [diff] [blame] | 450 | |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 451 | // We should find @removed only in the attribute javadoc and not anywhere else |
| Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 452 | // (i.e. the class javadoc). |
| 453 | const std::string kRemoved("removed"); |
| 454 | ASSERT_THAT(output, HasSubstr(kRemoved)); |
| 455 | std::string after_first_match = output.substr(output.find(kRemoved) + kRemoved.size()); |
| 456 | EXPECT_THAT(after_first_match, Not(HasSubstr(kRemoved))); |
| Michael Wright | feaf99f | 2016-05-06 17:16:06 +0100 | [diff] [blame] | 457 | } |
| 458 | |
| Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 459 | TEST(JavaClassGeneratorTest, GenerateOnResourcesLoadedCallbackForSharedLibrary) { |
| 460 | std::unique_ptr<ResourceTable> table = |
| 461 | test::ResourceTableBuilder() |
| 462 | .SetPackageId("android", 0x00) |
| Adam Lesinski | 73bff1e | 2017-12-08 16:06:10 -0800 | [diff] [blame] | 463 | .AddValue("android:attr/foo", ResourceId(0x00010000), util::make_unique<Attribute>()) |
| Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 464 | .AddValue("android:id/foo", ResourceId(0x00020000), util::make_unique<Id>()) |
| 465 | .AddValue( |
| 466 | "android:style/foo", ResourceId(0x00030000), |
| 467 | test::StyleBuilder() |
| 468 | .AddItem("android:attr/foo", ResourceId(0x00010000), util::make_unique<Id>()) |
| 469 | .Build()) |
| 470 | .Build(); |
| 471 | |
| 472 | std::unique_ptr<IAaptContext> context = |
| 473 | test::ContextBuilder().SetPackageId(0x00).SetCompilationPackage("android").Build(); |
| 474 | |
| 475 | JavaClassGeneratorOptions options; |
| 476 | options.use_final = false; |
| Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 477 | options.rewrite_callback_options = OnResourcesLoadedCallbackOptions{{"com.foo", "com.boo"}}; |
| Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 478 | JavaClassGenerator generator(context.get(), table.get(), options); |
| 479 | |
| Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 480 | std::string output; |
| 481 | StringOutputStream out(&output); |
| Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 482 | ASSERT_TRUE(generator.Generate("android", &out)); |
| Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 483 | out.Flush(); |
| Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 484 | |
| Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 485 | EXPECT_THAT(output, HasSubstr("void onResourcesLoaded")); |
| 486 | EXPECT_THAT(output, HasSubstr("com.foo.R.onResourcesLoaded")); |
| 487 | EXPECT_THAT(output, HasSubstr("com.boo.R.onResourcesLoaded")); |
| Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 488 | } |
| 489 | |
| Todd Kennedy | 949b6253 | 2018-03-02 14:19:45 -0800 | [diff] [blame] | 490 | TEST(JavaClassGeneratorTest, OnlyGenerateRText) { |
| 491 | std::unique_ptr<ResourceTable> table = |
| 492 | test::ResourceTableBuilder() |
| 493 | .SetPackageId("android", 0x01) |
| 494 | .AddValue("android:attr/foo", ResourceId(0x01010000), util::make_unique<Attribute>()) |
| 495 | .AddValue("android:styleable/hey.dude", ResourceId(0x01020000), |
| 496 | test::StyleableBuilder() |
| 497 | .AddItem("android:attr/foo", ResourceId(0x01010000)) |
| 498 | .Build()) |
| 499 | .Build(); |
| 500 | |
| 501 | std::unique_ptr<IAaptContext> context = |
| 502 | test::ContextBuilder().SetPackageId(0x01).SetCompilationPackage("android").Build(); |
| 503 | JavaClassGenerator generator(context.get(), table.get(), {}); |
| 504 | |
| 505 | ASSERT_TRUE(generator.Generate("android", nullptr)); |
| 506 | } |
| 507 | |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 508 | } // namespace aapt |