| 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), |
| 60 | test::AttributeBuilder(false).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 | |
| 110 | TEST(JavaClassGeneratorTest, AttrPrivateIsWrittenAsAttr) { |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 111 | std::unique_ptr<ResourceTable> table = |
| 112 | test::ResourceTableBuilder() |
| 113 | .SetPackageId("android", 0x01) |
| 114 | .AddSimple("android:attr/two", ResourceId(0x01010001)) |
| 115 | .AddSimple("android:^attr-private/one", ResourceId(0x01010000)) |
| 116 | .Build(); |
| Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 117 | |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 118 | std::unique_ptr<IAaptContext> context = |
| 119 | test::ContextBuilder() |
| Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 120 | .AddSymbolSource(util::make_unique<ResourceTableSymbolSource>(table.get())) |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 121 | .SetNameManglerPolicy(NameManglerPolicy{"android"}) |
| 122 | .Build(); |
| 123 | JavaClassGenerator generator(context.get(), table.get(), {}); |
| Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 124 | |
| Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame^] | 125 | std::string output; |
| 126 | StringOutputStream out(&output); |
| 127 | ASSERT_TRUE(generator.Generate("android", &out)); |
| 128 | out.Flush(); |
| 129 | |
| Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 130 | EXPECT_THAT(output, HasSubstr("public static final class attr")); |
| 131 | EXPECT_THAT(output, Not(HasSubstr("public static final class ^attr-private"))); |
| Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | TEST(JavaClassGeneratorTest, OnlyWritePublicResources) { |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 135 | StdErrDiagnostics diag; |
| 136 | std::unique_ptr<ResourceTable> table = |
| 137 | test::ResourceTableBuilder() |
| 138 | .SetPackageId("android", 0x01) |
| 139 | .AddSimple("android:id/one", ResourceId(0x01020000)) |
| 140 | .AddSimple("android:id/two", ResourceId(0x01020001)) |
| 141 | .AddSimple("android:id/three", ResourceId(0x01020002)) |
| Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 142 | .SetSymbolState("android:id/one", ResourceId(0x01020000), SymbolState::kPublic) |
| 143 | .SetSymbolState("android:id/two", ResourceId(0x01020001), SymbolState::kPrivate) |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 144 | .Build(); |
| Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 145 | |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 146 | std::unique_ptr<IAaptContext> context = |
| 147 | test::ContextBuilder() |
| Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 148 | .AddSymbolSource(util::make_unique<ResourceTableSymbolSource>(table.get())) |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 149 | .SetNameManglerPolicy(NameManglerPolicy{"android"}) |
| 150 | .Build(); |
| Adam Lesinski | 7656554 | 2016-03-10 21:55:04 -0800 | [diff] [blame] | 151 | |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 152 | JavaClassGeneratorOptions options; |
| 153 | options.types = JavaClassGeneratorOptions::SymbolTypes::kPublic; |
| 154 | { |
| 155 | JavaClassGenerator generator(context.get(), table.get(), options); |
| Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame^] | 156 | std::string output; |
| 157 | StringOutputStream out(&output); |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 158 | ASSERT_TRUE(generator.Generate("android", &out)); |
| Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame^] | 159 | out.Flush(); |
| 160 | |
| Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 161 | EXPECT_THAT(output, HasSubstr("public static final int one=0x01020000;")); |
| 162 | EXPECT_THAT(output, Not(HasSubstr("two"))); |
| 163 | EXPECT_THAT(output, Not(HasSubstr("three"))); |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 164 | } |
| Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 165 | |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 166 | options.types = JavaClassGeneratorOptions::SymbolTypes::kPublicPrivate; |
| 167 | { |
| 168 | JavaClassGenerator generator(context.get(), table.get(), options); |
| Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame^] | 169 | std::string output; |
| 170 | StringOutputStream out(&output); |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 171 | ASSERT_TRUE(generator.Generate("android", &out)); |
| Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame^] | 172 | out.Flush(); |
| 173 | |
| Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 174 | EXPECT_THAT(output, HasSubstr("public static final int one=0x01020000;")); |
| 175 | EXPECT_THAT(output, HasSubstr("public static final int two=0x01020001;")); |
| 176 | EXPECT_THAT(output, Not(HasSubstr("three"))); |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 177 | } |
| Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 178 | |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 179 | options.types = JavaClassGeneratorOptions::SymbolTypes::kAll; |
| 180 | { |
| 181 | JavaClassGenerator generator(context.get(), table.get(), options); |
| Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame^] | 182 | std::string output; |
| 183 | StringOutputStream out(&output); |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 184 | ASSERT_TRUE(generator.Generate("android", &out)); |
| Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame^] | 185 | out.Flush(); |
| 186 | |
| Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 187 | EXPECT_THAT(output, HasSubstr("public static final int one=0x01020000;")); |
| 188 | EXPECT_THAT(output, HasSubstr("public static final int two=0x01020001;")); |
| 189 | EXPECT_THAT(output, HasSubstr("public static final int three=0x01020002;")); |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 190 | } |
| Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 191 | } |
| 192 | |
| Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 193 | /* |
| 194 | * TODO(adamlesinski): Re-enable this once we get merging working again. |
| 195 | * TEST(JavaClassGeneratorTest, EmitPackageMangledSymbols) { |
| Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 196 | ASSERT_TRUE(addResource(ResourceName{ {}, ResourceType::kId, u"foo" }, |
| 197 | ResourceId{ 0x01, 0x02, 0x0000 })); |
| 198 | ResourceTable table; |
| 199 | table.setPackage(u"com.lib"); |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 200 | ASSERT_TRUE(table.addResource(ResourceName{ {}, ResourceType::kId, u"test" |
| 201 | }, {}, |
| 202 | Source{ "lib.xml", 33 }, |
| 203 | util::make_unique<Id>())); |
| Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 204 | ASSERT_TRUE(mTable->merge(std::move(table))); |
| 205 | |
| Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 206 | Linker linker(mTable, |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 207 | std::make_shared<MockResolver>(mTable, std::map<ResourceName, |
| 208 | ResourceId>()), |
| Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 209 | {}); |
| Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 210 | ASSERT_TRUE(linker.linkAndValidate()); |
| 211 | |
| 212 | JavaClassGenerator generator(mTable, {}); |
| 213 | |
| 214 | std::stringstream out; |
| 215 | EXPECT_TRUE(generator.generate(mTable->getPackage(), out)); |
| 216 | std::string output = out.str(); |
| 217 | EXPECT_NE(std::string::npos, output.find("int foo =")); |
| 218 | EXPECT_EQ(std::string::npos, output.find("int test =")); |
| 219 | |
| 220 | out.str(""); |
| 221 | EXPECT_TRUE(generator.generate(u"com.lib", out)); |
| 222 | output = out.str(); |
| 223 | EXPECT_NE(std::string::npos, output.find("int test =")); |
| 224 | EXPECT_EQ(std::string::npos, output.find("int foo =")); |
| Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 225 | }*/ |
| Adam Lesinski | 838a687 | 2015-05-01 13:14:05 -0700 | [diff] [blame] | 226 | |
| Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 227 | TEST(JavaClassGeneratorTest, EmitOtherPackagesAttributesInStyleable) { |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 228 | std::unique_ptr<ResourceTable> table = |
| 229 | test::ResourceTableBuilder() |
| 230 | .SetPackageId("android", 0x01) |
| 231 | .SetPackageId("com.lib", 0x02) |
| 232 | .AddValue("android:attr/bar", ResourceId(0x01010000), |
| 233 | test::AttributeBuilder(false).Build()) |
| 234 | .AddValue("com.lib:attr/bar", ResourceId(0x02010000), |
| 235 | test::AttributeBuilder(false).Build()) |
| 236 | .AddValue("android:styleable/foo", ResourceId(0x01030000), |
| 237 | test::StyleableBuilder() |
| 238 | .AddItem("android:attr/bar", ResourceId(0x01010000)) |
| 239 | .AddItem("com.lib:attr/bar", ResourceId(0x02010000)) |
| 240 | .Build()) |
| 241 | .Build(); |
| Adam Lesinski | 838a687 | 2015-05-01 13:14:05 -0700 | [diff] [blame] | 242 | |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 243 | std::unique_ptr<IAaptContext> context = |
| 244 | test::ContextBuilder() |
| Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 245 | .AddSymbolSource(util::make_unique<ResourceTableSymbolSource>(table.get())) |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 246 | .SetNameManglerPolicy(NameManglerPolicy{"android"}) |
| 247 | .Build(); |
| 248 | JavaClassGenerator generator(context.get(), table.get(), {}); |
| Adam Lesinski | 838a687 | 2015-05-01 13:14:05 -0700 | [diff] [blame] | 249 | |
| Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame^] | 250 | std::string output; |
| 251 | StringOutputStream out(&output); |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 252 | EXPECT_TRUE(generator.Generate("android", &out)); |
| Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame^] | 253 | out.Flush(); |
| Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 254 | |
| Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 255 | EXPECT_THAT(output, HasSubstr("int foo_bar=")); |
| 256 | EXPECT_THAT(output, HasSubstr("int foo_com_lib_bar=")); |
| Adam Lesinski | 838a687 | 2015-05-01 13:14:05 -0700 | [diff] [blame] | 257 | } |
| Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 258 | |
| Adam Lesinski | 3b4cd94 | 2015-10-30 16:31:42 -0700 | [diff] [blame] | 259 | TEST(JavaClassGeneratorTest, CommentsForSimpleResourcesArePresent) { |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 260 | std::unique_ptr<ResourceTable> table = |
| 261 | test::ResourceTableBuilder() |
| 262 | .SetPackageId("android", 0x01) |
| 263 | .AddSimple("android:id/foo", ResourceId(0x01010000)) |
| 264 | .Build(); |
| 265 | test::GetValue<Id>(table.get(), "android:id/foo") |
| 266 | ->SetComment(std::string("This is a comment\n@deprecated")); |
| Adam Lesinski | 3b4cd94 | 2015-10-30 16:31:42 -0700 | [diff] [blame] | 267 | |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 268 | std::unique_ptr<IAaptContext> context = |
| 269 | test::ContextBuilder() |
| Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 270 | .AddSymbolSource(util::make_unique<ResourceTableSymbolSource>(table.get())) |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 271 | .SetNameManglerPolicy(NameManglerPolicy{"android"}) |
| 272 | .Build(); |
| 273 | JavaClassGenerator generator(context.get(), table.get(), {}); |
| Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame^] | 274 | |
| 275 | std::string output; |
| 276 | StringOutputStream out(&output); |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 277 | ASSERT_TRUE(generator.Generate("android", &out)); |
| Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame^] | 278 | out.Flush(); |
| Adam Lesinski | 3b4cd94 | 2015-10-30 16:31:42 -0700 | [diff] [blame] | 279 | |
| Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 280 | const char* expected_text = |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 281 | R"EOF(/** |
| Adam Lesinski | 3b4cd94 | 2015-10-30 16:31:42 -0700 | [diff] [blame] | 282 | * This is a comment |
| 283 | * @deprecated |
| 284 | */ |
| 285 | @Deprecated |
| Adam Lesinski | 803c7c8 | 2016-04-06 16:09:43 -0700 | [diff] [blame] | 286 | public static final int foo=0x01010000;)EOF"; |
| Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 287 | EXPECT_THAT(output, HasSubstr(expected_text)); |
| Adam Lesinski | 3b4cd94 | 2015-10-30 16:31:42 -0700 | [diff] [blame] | 288 | } |
| 289 | |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 290 | TEST(JavaClassGeneratorTest, CommentsForEnumAndFlagAttributesArePresent) {} |
| Adam Lesinski | 3b4cd94 | 2015-10-30 16:31:42 -0700 | [diff] [blame] | 291 | |
| Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 292 | TEST(JavaClassGeneratorTest, CommentsForStyleablesAndNestedAttributesArePresent) { |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 293 | Attribute attr(false); |
| 294 | attr.SetComment(StringPiece("This is an attribute")); |
| Adam Lesinski | 3b4cd94 | 2015-10-30 16:31:42 -0700 | [diff] [blame] | 295 | |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 296 | Styleable styleable; |
| Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 297 | styleable.entries.push_back(Reference(test::ParseNameOrDie("android:attr/one"))); |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 298 | styleable.SetComment(StringPiece("This is a styleable")); |
| Adam Lesinski | 3b4cd94 | 2015-10-30 16:31:42 -0700 | [diff] [blame] | 299 | |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 300 | std::unique_ptr<ResourceTable> table = |
| 301 | test::ResourceTableBuilder() |
| 302 | .SetPackageId("android", 0x01) |
| 303 | .AddValue("android:attr/one", util::make_unique<Attribute>(attr)) |
| 304 | .AddValue("android:styleable/Container", |
| 305 | std::unique_ptr<Styleable>(styleable.Clone(nullptr))) |
| 306 | .Build(); |
| Adam Lesinski | 74605cd | 2016-03-03 15:39:50 -0800 | [diff] [blame] | 307 | |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 308 | std::unique_ptr<IAaptContext> context = |
| 309 | test::ContextBuilder() |
| Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 310 | .AddSymbolSource(util::make_unique<ResourceTableSymbolSource>(table.get())) |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 311 | .SetNameManglerPolicy(NameManglerPolicy{"android"}) |
| 312 | .Build(); |
| 313 | JavaClassGeneratorOptions options; |
| 314 | options.use_final = false; |
| 315 | JavaClassGenerator generator(context.get(), table.get(), options); |
| Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame^] | 316 | |
| 317 | std::string output; |
| 318 | StringOutputStream out(&output); |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 319 | ASSERT_TRUE(generator.Generate("android", &out)); |
| Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame^] | 320 | out.Flush(); |
| Adam Lesinski | 74605cd | 2016-03-03 15:39:50 -0800 | [diff] [blame] | 321 | |
| Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 322 | EXPECT_THAT(output, HasSubstr("attr name android:one")); |
| 323 | EXPECT_THAT(output, HasSubstr("attr description")); |
| 324 | EXPECT_THAT(output, HasSubstr(attr.GetComment())); |
| 325 | EXPECT_THAT(output, HasSubstr(styleable.GetComment())); |
| Adam Lesinski | 3b4cd94 | 2015-10-30 16:31:42 -0700 | [diff] [blame] | 326 | } |
| 327 | |
| Adam Lesinski | 761d434 | 2017-09-29 11:15:17 -0700 | [diff] [blame] | 328 | TEST(JavaClassGeneratorTest, StyleableAndIndicesAreColocated) { |
| 329 | std::unique_ptr<ResourceTable> table = |
| 330 | test::ResourceTableBuilder() |
| 331 | .SetPackageId("android", 0x01) |
| 332 | .AddValue("android:attr/layout_gravity", util::make_unique<Attribute>()) |
| 333 | .AddValue("android:attr/background", util::make_unique<Attribute>()) |
| 334 | .AddValue("android:styleable/ActionBar", |
| 335 | test::StyleableBuilder() |
| 336 | .AddItem("android:attr/background", ResourceId(0x01010000)) |
| 337 | .Build()) |
| 338 | .AddValue("android:styleable/ActionBar.LayoutParams", |
| 339 | test::StyleableBuilder() |
| 340 | .AddItem("android:attr/layout_gravity", ResourceId(0x01010001)) |
| 341 | .Build()) |
| 342 | .Build(); |
| 343 | |
| 344 | std::unique_ptr<IAaptContext> context = |
| 345 | test::ContextBuilder() |
| 346 | .AddSymbolSource(util::make_unique<ResourceTableSymbolSource>(table.get())) |
| 347 | .SetNameManglerPolicy(NameManglerPolicy{"android"}) |
| 348 | .Build(); |
| 349 | |
| 350 | JavaClassGeneratorOptions options; |
| 351 | JavaClassGenerator generator(context.get(), table.get(), {}); |
| Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame^] | 352 | |
| 353 | std::string output; |
| 354 | StringOutputStream out(&output); |
| Adam Lesinski | 761d434 | 2017-09-29 11:15:17 -0700 | [diff] [blame] | 355 | ASSERT_TRUE(generator.Generate("android", &out)); |
| Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame^] | 356 | out.Flush(); |
| Adam Lesinski | 761d434 | 2017-09-29 11:15:17 -0700 | [diff] [blame] | 357 | |
| 358 | std::string::size_type actionbar_pos = output.find("int[] ActionBar"); |
| 359 | ASSERT_THAT(actionbar_pos, Ne(std::string::npos)); |
| 360 | |
| 361 | std::string::size_type actionbar_background_pos = output.find("int ActionBar_background"); |
| 362 | ASSERT_THAT(actionbar_background_pos, Ne(std::string::npos)); |
| 363 | |
| 364 | std::string::size_type actionbar_layout_params_pos = output.find("int[] ActionBar_LayoutParams"); |
| 365 | ASSERT_THAT(actionbar_layout_params_pos, Ne(std::string::npos)); |
| 366 | |
| 367 | std::string::size_type actionbar_layout_params_layout_gravity_pos = |
| 368 | output.find("int ActionBar_LayoutParams_layout_gravity"); |
| 369 | ASSERT_THAT(actionbar_layout_params_layout_gravity_pos, Ne(std::string::npos)); |
| 370 | |
| 371 | EXPECT_THAT(actionbar_pos, Lt(actionbar_background_pos)); |
| 372 | EXPECT_THAT(actionbar_pos, Lt(actionbar_layout_params_pos)); |
| 373 | EXPECT_THAT(actionbar_background_pos, Lt(actionbar_layout_params_pos)); |
| 374 | EXPECT_THAT(actionbar_layout_params_pos, Lt(actionbar_layout_params_layout_gravity_pos)); |
| 375 | } |
| 376 | |
| Michael Wright | feaf99f | 2016-05-06 17:16:06 +0100 | [diff] [blame] | 377 | TEST(JavaClassGeneratorTest, CommentsForRemovedAttributesAreNotPresentInClass) { |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 378 | Attribute attr(false); |
| 379 | attr.SetComment(StringPiece("removed")); |
| Michael Wright | feaf99f | 2016-05-06 17:16:06 +0100 | [diff] [blame] | 380 | |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 381 | std::unique_ptr<ResourceTable> table = |
| 382 | test::ResourceTableBuilder() |
| 383 | .SetPackageId("android", 0x01) |
| 384 | .AddValue("android:attr/one", util::make_unique<Attribute>(attr)) |
| 385 | .Build(); |
| Michael Wright | feaf99f | 2016-05-06 17:16:06 +0100 | [diff] [blame] | 386 | |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 387 | std::unique_ptr<IAaptContext> context = |
| 388 | test::ContextBuilder() |
| Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 389 | .AddSymbolSource(util::make_unique<ResourceTableSymbolSource>(table.get())) |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 390 | .SetNameManglerPolicy(NameManglerPolicy{"android"}) |
| 391 | .Build(); |
| 392 | JavaClassGeneratorOptions options; |
| 393 | options.use_final = false; |
| 394 | JavaClassGenerator generator(context.get(), table.get(), options); |
| Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame^] | 395 | |
| 396 | std::string output; |
| 397 | StringOutputStream out(&output); |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 398 | ASSERT_TRUE(generator.Generate("android", &out)); |
| Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame^] | 399 | out.Flush(); |
| Michael Wright | feaf99f | 2016-05-06 17:16:06 +0100 | [diff] [blame] | 400 | |
| Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 401 | EXPECT_THAT(output, Not(HasSubstr("@attr name android:one"))); |
| 402 | EXPECT_THAT(output, Not(HasSubstr("@attr description"))); |
| Michael Wright | feaf99f | 2016-05-06 17:16:06 +0100 | [diff] [blame] | 403 | |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 404 | // 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] | 405 | // (i.e. the class javadoc). |
| 406 | const std::string kRemoved("removed"); |
| 407 | ASSERT_THAT(output, HasSubstr(kRemoved)); |
| 408 | std::string after_first_match = output.substr(output.find(kRemoved) + kRemoved.size()); |
| 409 | EXPECT_THAT(after_first_match, Not(HasSubstr(kRemoved))); |
| Michael Wright | feaf99f | 2016-05-06 17:16:06 +0100 | [diff] [blame] | 410 | } |
| 411 | |
| Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 412 | TEST(JavaClassGeneratorTest, GenerateOnResourcesLoadedCallbackForSharedLibrary) { |
| 413 | std::unique_ptr<ResourceTable> table = |
| 414 | test::ResourceTableBuilder() |
| 415 | .SetPackageId("android", 0x00) |
| 416 | .AddValue("android:attr/foo", ResourceId(0x00010000), util::make_unique<Attribute>(false)) |
| 417 | .AddValue("android:id/foo", ResourceId(0x00020000), util::make_unique<Id>()) |
| 418 | .AddValue( |
| 419 | "android:style/foo", ResourceId(0x00030000), |
| 420 | test::StyleBuilder() |
| 421 | .AddItem("android:attr/foo", ResourceId(0x00010000), util::make_unique<Id>()) |
| 422 | .Build()) |
| 423 | .Build(); |
| 424 | |
| 425 | std::unique_ptr<IAaptContext> context = |
| 426 | test::ContextBuilder().SetPackageId(0x00).SetCompilationPackage("android").Build(); |
| 427 | |
| 428 | JavaClassGeneratorOptions options; |
| 429 | options.use_final = false; |
| Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 430 | options.rewrite_callback_options = OnResourcesLoadedCallbackOptions{{"com.foo", "com.boo"}}; |
| Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 431 | JavaClassGenerator generator(context.get(), table.get(), options); |
| 432 | |
| Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame^] | 433 | std::string output; |
| 434 | StringOutputStream out(&output); |
| Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 435 | ASSERT_TRUE(generator.Generate("android", &out)); |
| Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame^] | 436 | out.Flush(); |
| Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 437 | |
| Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 438 | EXPECT_THAT(output, HasSubstr("void onResourcesLoaded")); |
| 439 | EXPECT_THAT(output, HasSubstr("com.foo.R.onResourcesLoaded")); |
| 440 | EXPECT_THAT(output, HasSubstr("com.boo.R.onResourcesLoaded")); |
| Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 441 | } |
| 442 | |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 443 | } // namespace aapt |