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 <sstream> |
| 20 | #include <string> |
| 21 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 22 | #include "test/Test.h" |
| 23 | #include "util/Util.h" |
| 24 | |
Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 25 | using ::android::StringPiece; |
| 26 | using ::testing::HasSubstr; |
| 27 | using ::testing::Not; |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 28 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 29 | namespace aapt { |
| 30 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 31 | TEST(JavaClassGeneratorTest, FailWhenEntryIsJavaKeyword) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 32 | std::unique_ptr<ResourceTable> table = |
| 33 | test::ResourceTableBuilder() |
| 34 | .SetPackageId("android", 0x01) |
| 35 | .AddSimple("android:id/class", ResourceId(0x01020000)) |
| 36 | .Build(); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 37 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 38 | 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 Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 45 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 46 | std::stringstream out; |
| 47 | EXPECT_FALSE(generator.Generate("android", &out)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 48 | } |
| 49 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 50 | TEST(JavaClassGeneratorTest, TransformInvalidJavaIdentifierCharacter) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 51 | 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 Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 57 | .AddValue("android:styleable/hey.dude", ResourceId(0x01030000), |
| 58 | test::StyleableBuilder() |
| 59 | .AddItem("android:attr/cool.attr", ResourceId(0x01010000)) |
| 60 | .Build()) |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 61 | .Build(); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 62 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 63 | std::unique_ptr<IAaptContext> context = |
| 64 | test::ContextBuilder() |
Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 65 | .AddSymbolSource(util::make_unique<ResourceTableSymbolSource>(table.get())) |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 66 | .SetNameManglerPolicy(NameManglerPolicy{"android"}) |
| 67 | .Build(); |
| 68 | JavaClassGenerator generator(context.get(), table.get(), {}); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 69 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 70 | std::stringstream out; |
| 71 | EXPECT_TRUE(generator.Generate("android", &out)); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 72 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 73 | std::string output = out.str(); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 74 | |
Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 75 | 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 Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 78 | } |
| 79 | |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 80 | TEST(JavaClassGeneratorTest, CorrectPackageNameIsUsed) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 81 | 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 Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 87 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 88 | std::unique_ptr<IAaptContext> context = |
| 89 | test::ContextBuilder() |
Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 90 | .AddSymbolSource(util::make_unique<ResourceTableSymbolSource>(table.get())) |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 91 | .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 Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 96 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 97 | std::string output = out.str(); |
Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 98 | 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 Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | TEST(JavaClassGeneratorTest, AttrPrivateIsWrittenAsAttr) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 105 | 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 Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 111 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 112 | std::unique_ptr<IAaptContext> context = |
| 113 | test::ContextBuilder() |
Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 114 | .AddSymbolSource(util::make_unique<ResourceTableSymbolSource>(table.get())) |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 115 | .SetNameManglerPolicy(NameManglerPolicy{"android"}) |
| 116 | .Build(); |
| 117 | JavaClassGenerator generator(context.get(), table.get(), {}); |
| 118 | std::stringstream out; |
| 119 | ASSERT_TRUE(generator.Generate("android", &out)); |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 120 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 121 | std::string output = out.str(); |
Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 122 | EXPECT_THAT(output, HasSubstr("public static final class attr")); |
| 123 | EXPECT_THAT(output, Not(HasSubstr("public static final class ^attr-private"))); |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | TEST(JavaClassGeneratorTest, OnlyWritePublicResources) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 127 | 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 Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 134 | .SetSymbolState("android:id/one", ResourceId(0x01020000), SymbolState::kPublic) |
| 135 | .SetSymbolState("android:id/two", ResourceId(0x01020001), SymbolState::kPrivate) |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 136 | .Build(); |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 137 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 138 | std::unique_ptr<IAaptContext> context = |
| 139 | test::ContextBuilder() |
Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 140 | .AddSymbolSource(util::make_unique<ResourceTableSymbolSource>(table.get())) |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 141 | .SetNameManglerPolicy(NameManglerPolicy{"android"}) |
| 142 | .Build(); |
Adam Lesinski | 7656554 | 2016-03-10 21:55:04 -0800 | [diff] [blame] | 143 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 144 | 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 Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 151 | 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 Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 154 | } |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 155 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 156 | 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 Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 162 | 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 Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 165 | } |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 166 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 167 | 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 Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 173 | 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 Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 176 | } |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 177 | } |
| 178 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 179 | /* |
| 180 | * TODO(adamlesinski): Re-enable this once we get merging working again. |
| 181 | * TEST(JavaClassGeneratorTest, EmitPackageMangledSymbols) { |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 182 | ASSERT_TRUE(addResource(ResourceName{ {}, ResourceType::kId, u"foo" }, |
| 183 | ResourceId{ 0x01, 0x02, 0x0000 })); |
| 184 | ResourceTable table; |
| 185 | table.setPackage(u"com.lib"); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 186 | ASSERT_TRUE(table.addResource(ResourceName{ {}, ResourceType::kId, u"test" |
| 187 | }, {}, |
| 188 | Source{ "lib.xml", 33 }, |
| 189 | util::make_unique<Id>())); |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 190 | ASSERT_TRUE(mTable->merge(std::move(table))); |
| 191 | |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 192 | Linker linker(mTable, |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 193 | std::make_shared<MockResolver>(mTable, std::map<ResourceName, |
| 194 | ResourceId>()), |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 195 | {}); |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 196 | 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 Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 211 | }*/ |
Adam Lesinski | 838a687 | 2015-05-01 13:14:05 -0700 | [diff] [blame] | 212 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 213 | TEST(JavaClassGeneratorTest, EmitOtherPackagesAttributesInStyleable) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 214 | 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 Lesinski | 838a687 | 2015-05-01 13:14:05 -0700 | [diff] [blame] | 228 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 229 | std::unique_ptr<IAaptContext> context = |
| 230 | test::ContextBuilder() |
Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 231 | .AddSymbolSource(util::make_unique<ResourceTableSymbolSource>(table.get())) |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 232 | .SetNameManglerPolicy(NameManglerPolicy{"android"}) |
| 233 | .Build(); |
| 234 | JavaClassGenerator generator(context.get(), table.get(), {}); |
Adam Lesinski | 838a687 | 2015-05-01 13:14:05 -0700 | [diff] [blame] | 235 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 236 | std::stringstream out; |
| 237 | EXPECT_TRUE(generator.Generate("android", &out)); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 238 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 239 | std::string output = out.str(); |
Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 240 | EXPECT_THAT(output, HasSubstr("int foo_bar=")); |
| 241 | EXPECT_THAT(output, HasSubstr("int foo_com_lib_bar=")); |
Adam Lesinski | 838a687 | 2015-05-01 13:14:05 -0700 | [diff] [blame] | 242 | } |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 243 | |
Adam Lesinski | 3b4cd94 | 2015-10-30 16:31:42 -0700 | [diff] [blame] | 244 | TEST(JavaClassGeneratorTest, CommentsForSimpleResourcesArePresent) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 245 | 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 Lesinski | 3b4cd94 | 2015-10-30 16:31:42 -0700 | [diff] [blame] | 252 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 253 | std::unique_ptr<IAaptContext> context = |
| 254 | test::ContextBuilder() |
Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 255 | .AddSymbolSource(util::make_unique<ResourceTableSymbolSource>(table.get())) |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 256 | .SetNameManglerPolicy(NameManglerPolicy{"android"}) |
| 257 | .Build(); |
| 258 | JavaClassGenerator generator(context.get(), table.get(), {}); |
| 259 | std::stringstream out; |
| 260 | ASSERT_TRUE(generator.Generate("android", &out)); |
Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 261 | std::string output = out.str(); |
Adam Lesinski | 3b4cd94 | 2015-10-30 16:31:42 -0700 | [diff] [blame] | 262 | |
Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 263 | const char* expected_text = |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 264 | R"EOF(/** |
Adam Lesinski | 3b4cd94 | 2015-10-30 16:31:42 -0700 | [diff] [blame] | 265 | * This is a comment |
| 266 | * @deprecated |
| 267 | */ |
| 268 | @Deprecated |
Adam Lesinski | 803c7c8 | 2016-04-06 16:09:43 -0700 | [diff] [blame] | 269 | public static final int foo=0x01010000;)EOF"; |
Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 270 | EXPECT_THAT(output, HasSubstr(expected_text)); |
Adam Lesinski | 3b4cd94 | 2015-10-30 16:31:42 -0700 | [diff] [blame] | 271 | } |
| 272 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 273 | TEST(JavaClassGeneratorTest, CommentsForEnumAndFlagAttributesArePresent) {} |
Adam Lesinski | 3b4cd94 | 2015-10-30 16:31:42 -0700 | [diff] [blame] | 274 | |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 275 | TEST(JavaClassGeneratorTest, CommentsForStyleablesAndNestedAttributesArePresent) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 276 | Attribute attr(false); |
| 277 | attr.SetComment(StringPiece("This is an attribute")); |
Adam Lesinski | 3b4cd94 | 2015-10-30 16:31:42 -0700 | [diff] [blame] | 278 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 279 | Styleable styleable; |
Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 280 | styleable.entries.push_back(Reference(test::ParseNameOrDie("android:attr/one"))); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 281 | styleable.SetComment(StringPiece("This is a styleable")); |
Adam Lesinski | 3b4cd94 | 2015-10-30 16:31:42 -0700 | [diff] [blame] | 282 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 283 | 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 Lesinski | 74605cd | 2016-03-03 15:39:50 -0800 | [diff] [blame] | 290 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 291 | std::unique_ptr<IAaptContext> context = |
| 292 | test::ContextBuilder() |
Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 293 | .AddSymbolSource(util::make_unique<ResourceTableSymbolSource>(table.get())) |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 294 | .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 Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 301 | std::string output = out.str(); |
Adam Lesinski | 74605cd | 2016-03-03 15:39:50 -0800 | [diff] [blame] | 302 | |
Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 303 | 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 Lesinski | 3b4cd94 | 2015-10-30 16:31:42 -0700 | [diff] [blame] | 307 | } |
| 308 | |
Michael Wright | feaf99f | 2016-05-06 17:16:06 +0100 | [diff] [blame] | 309 | TEST(JavaClassGeneratorTest, CommentsForRemovedAttributesAreNotPresentInClass) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 310 | Attribute attr(false); |
| 311 | attr.SetComment(StringPiece("removed")); |
Michael Wright | feaf99f | 2016-05-06 17:16:06 +0100 | [diff] [blame] | 312 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 313 | 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 Wright | feaf99f | 2016-05-06 17:16:06 +0100 | [diff] [blame] | 318 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 319 | std::unique_ptr<IAaptContext> context = |
| 320 | test::ContextBuilder() |
Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 321 | .AddSymbolSource(util::make_unique<ResourceTableSymbolSource>(table.get())) |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 322 | .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 Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 329 | std::string output = out.str(); |
Michael Wright | feaf99f | 2016-05-06 17:16:06 +0100 | [diff] [blame] | 330 | |
Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 331 | EXPECT_THAT(output, Not(HasSubstr("@attr name android:one"))); |
| 332 | EXPECT_THAT(output, Not(HasSubstr("@attr description"))); |
Michael Wright | feaf99f | 2016-05-06 17:16:06 +0100 | [diff] [blame] | 333 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 334 | // 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] | 335 | // (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 Wright | feaf99f | 2016-05-06 17:16:06 +0100 | [diff] [blame] | 340 | } |
| 341 | |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 342 | TEST(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 Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 360 | options.rewrite_callback_options = OnResourcesLoadedCallbackOptions{{"com.foo", "com.boo"}}; |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 361 | JavaClassGenerator generator(context.get(), table.get(), options); |
| 362 | |
| 363 | std::stringstream out; |
| 364 | ASSERT_TRUE(generator.Generate("android", &out)); |
| 365 | |
Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 366 | std::string output = out.str(); |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 367 | |
Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 368 | 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 Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 371 | } |
| 372 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 373 | } // namespace aapt |