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 | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 25 | using android::StringPiece; |
| 26 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 27 | namespace aapt { |
| 28 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 29 | TEST(JavaClassGeneratorTest, FailWhenEntryIsJavaKeyword) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 30 | std::unique_ptr<ResourceTable> table = |
| 31 | test::ResourceTableBuilder() |
| 32 | .SetPackageId("android", 0x01) |
| 33 | .AddSimple("android:id/class", ResourceId(0x01020000)) |
| 34 | .Build(); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 35 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 36 | std::unique_ptr<IAaptContext> context = |
| 37 | test::ContextBuilder() |
| 38 | .AddSymbolSource( |
| 39 | util::make_unique<ResourceTableSymbolSource>(table.get())) |
| 40 | .SetNameManglerPolicy(NameManglerPolicy{"android"}) |
| 41 | .Build(); |
| 42 | JavaClassGenerator generator(context.get(), table.get(), {}); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 43 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 44 | std::stringstream out; |
| 45 | EXPECT_FALSE(generator.Generate("android", &out)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 46 | } |
| 47 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 48 | TEST(JavaClassGeneratorTest, TransformInvalidJavaIdentifierCharacter) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 49 | std::unique_ptr<ResourceTable> table = |
| 50 | test::ResourceTableBuilder() |
| 51 | .SetPackageId("android", 0x01) |
| 52 | .AddSimple("android:id/hey-man", ResourceId(0x01020000)) |
| 53 | .AddValue("android:attr/cool.attr", ResourceId(0x01010000), |
| 54 | test::AttributeBuilder(false).Build()) |
| 55 | .AddValue( |
| 56 | "android:styleable/hey.dude", ResourceId(0x01030000), |
| 57 | test::StyleableBuilder() |
| 58 | .AddItem("android:attr/cool.attr", ResourceId(0x01010000)) |
| 59 | .Build()) |
| 60 | .Build(); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 61 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 62 | std::unique_ptr<IAaptContext> context = |
| 63 | test::ContextBuilder() |
| 64 | .AddSymbolSource( |
| 65 | util::make_unique<ResourceTableSymbolSource>(table.get())) |
| 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 | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 75 | EXPECT_NE(std::string::npos, |
| 76 | output.find("public static final int hey_man=0x01020000;")); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 77 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 78 | EXPECT_NE(std::string::npos, |
| 79 | output.find("public static final int[] hey_dude={")); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 80 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 81 | EXPECT_NE(std::string::npos, |
| 82 | output.find("public static final int hey_dude_cool_attr=0;")); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 83 | } |
| 84 | |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 85 | TEST(JavaClassGeneratorTest, CorrectPackageNameIsUsed) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 86 | std::unique_ptr<ResourceTable> table = |
| 87 | test::ResourceTableBuilder() |
| 88 | .SetPackageId("android", 0x01) |
| 89 | .AddSimple("android:id/one", ResourceId(0x01020000)) |
| 90 | .AddSimple("android:id/com.foo$two", ResourceId(0x01020001)) |
| 91 | .Build(); |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 92 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 93 | std::unique_ptr<IAaptContext> context = |
| 94 | test::ContextBuilder() |
| 95 | .AddSymbolSource( |
| 96 | util::make_unique<ResourceTableSymbolSource>(table.get())) |
| 97 | .SetNameManglerPolicy(NameManglerPolicy{"android"}) |
| 98 | .Build(); |
| 99 | JavaClassGenerator generator(context.get(), table.get(), {}); |
| 100 | std::stringstream out; |
| 101 | ASSERT_TRUE(generator.Generate("android", "com.android.internal", &out)); |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 102 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 103 | std::string output = out.str(); |
| 104 | EXPECT_NE(std::string::npos, output.find("package com.android.internal;")); |
| 105 | EXPECT_NE(std::string::npos, |
| 106 | output.find("public static final int one=0x01020000;")); |
| 107 | EXPECT_EQ(std::string::npos, output.find("two")); |
| 108 | EXPECT_EQ(std::string::npos, output.find("com_foo$two")); |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | TEST(JavaClassGeneratorTest, AttrPrivateIsWrittenAsAttr) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 112 | std::unique_ptr<ResourceTable> table = |
| 113 | test::ResourceTableBuilder() |
| 114 | .SetPackageId("android", 0x01) |
| 115 | .AddSimple("android:attr/two", ResourceId(0x01010001)) |
| 116 | .AddSimple("android:^attr-private/one", ResourceId(0x01010000)) |
| 117 | .Build(); |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 118 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 119 | std::unique_ptr<IAaptContext> context = |
| 120 | test::ContextBuilder() |
| 121 | .AddSymbolSource( |
| 122 | util::make_unique<ResourceTableSymbolSource>(table.get())) |
| 123 | .SetNameManglerPolicy(NameManglerPolicy{"android"}) |
| 124 | .Build(); |
| 125 | JavaClassGenerator generator(context.get(), table.get(), {}); |
| 126 | std::stringstream out; |
| 127 | ASSERT_TRUE(generator.Generate("android", &out)); |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 128 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 129 | std::string output = out.str(); |
| 130 | EXPECT_NE(std::string::npos, output.find("public static final class attr")); |
| 131 | EXPECT_EQ(std::string::npos, |
| 132 | output.find("public static final class ^attr-private")); |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | TEST(JavaClassGeneratorTest, OnlyWritePublicResources) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 136 | StdErrDiagnostics diag; |
| 137 | std::unique_ptr<ResourceTable> table = |
| 138 | test::ResourceTableBuilder() |
| 139 | .SetPackageId("android", 0x01) |
| 140 | .AddSimple("android:id/one", ResourceId(0x01020000)) |
| 141 | .AddSimple("android:id/two", ResourceId(0x01020001)) |
| 142 | .AddSimple("android:id/three", ResourceId(0x01020002)) |
| 143 | .SetSymbolState("android:id/one", ResourceId(0x01020000), |
| 144 | SymbolState::kPublic) |
| 145 | .SetSymbolState("android:id/two", ResourceId(0x01020001), |
| 146 | SymbolState::kPrivate) |
| 147 | .Build(); |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 148 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 149 | std::unique_ptr<IAaptContext> context = |
| 150 | test::ContextBuilder() |
| 151 | .AddSymbolSource( |
| 152 | util::make_unique<ResourceTableSymbolSource>(table.get())) |
| 153 | .SetNameManglerPolicy(NameManglerPolicy{"android"}) |
| 154 | .Build(); |
Adam Lesinski | 7656554 | 2016-03-10 21:55:04 -0800 | [diff] [blame] | 155 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 156 | JavaClassGeneratorOptions options; |
| 157 | options.types = JavaClassGeneratorOptions::SymbolTypes::kPublic; |
| 158 | { |
| 159 | JavaClassGenerator generator(context.get(), table.get(), options); |
| 160 | std::stringstream out; |
| 161 | ASSERT_TRUE(generator.Generate("android", &out)); |
| 162 | std::string output = out.str(); |
| 163 | EXPECT_NE(std::string::npos, |
| 164 | output.find("public static final int one=0x01020000;")); |
| 165 | EXPECT_EQ(std::string::npos, output.find("two")); |
| 166 | EXPECT_EQ(std::string::npos, output.find("three")); |
| 167 | } |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 168 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 169 | options.types = JavaClassGeneratorOptions::SymbolTypes::kPublicPrivate; |
| 170 | { |
| 171 | JavaClassGenerator generator(context.get(), table.get(), options); |
| 172 | std::stringstream out; |
| 173 | ASSERT_TRUE(generator.Generate("android", &out)); |
| 174 | std::string output = out.str(); |
| 175 | EXPECT_NE(std::string::npos, |
| 176 | output.find("public static final int one=0x01020000;")); |
| 177 | EXPECT_NE(std::string::npos, |
| 178 | output.find("public static final int two=0x01020001;")); |
| 179 | EXPECT_EQ(std::string::npos, output.find("three")); |
| 180 | } |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 181 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 182 | options.types = JavaClassGeneratorOptions::SymbolTypes::kAll; |
| 183 | { |
| 184 | JavaClassGenerator generator(context.get(), table.get(), options); |
| 185 | std::stringstream out; |
| 186 | ASSERT_TRUE(generator.Generate("android", &out)); |
| 187 | std::string output = out.str(); |
| 188 | EXPECT_NE(std::string::npos, |
| 189 | output.find("public static final int one=0x01020000;")); |
| 190 | EXPECT_NE(std::string::npos, |
| 191 | output.find("public static final int two=0x01020001;")); |
| 192 | EXPECT_NE(std::string::npos, |
| 193 | output.find("public static final int three=0x01020002;")); |
| 194 | } |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 195 | } |
| 196 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 197 | /* |
| 198 | * TODO(adamlesinski): Re-enable this once we get merging working again. |
| 199 | * TEST(JavaClassGeneratorTest, EmitPackageMangledSymbols) { |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 200 | ASSERT_TRUE(addResource(ResourceName{ {}, ResourceType::kId, u"foo" }, |
| 201 | ResourceId{ 0x01, 0x02, 0x0000 })); |
| 202 | ResourceTable table; |
| 203 | table.setPackage(u"com.lib"); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 204 | ASSERT_TRUE(table.addResource(ResourceName{ {}, ResourceType::kId, u"test" |
| 205 | }, {}, |
| 206 | Source{ "lib.xml", 33 }, |
| 207 | util::make_unique<Id>())); |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 208 | ASSERT_TRUE(mTable->merge(std::move(table))); |
| 209 | |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 210 | Linker linker(mTable, |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 211 | std::make_shared<MockResolver>(mTable, std::map<ResourceName, |
| 212 | ResourceId>()), |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 213 | {}); |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 214 | ASSERT_TRUE(linker.linkAndValidate()); |
| 215 | |
| 216 | JavaClassGenerator generator(mTable, {}); |
| 217 | |
| 218 | std::stringstream out; |
| 219 | EXPECT_TRUE(generator.generate(mTable->getPackage(), out)); |
| 220 | std::string output = out.str(); |
| 221 | EXPECT_NE(std::string::npos, output.find("int foo =")); |
| 222 | EXPECT_EQ(std::string::npos, output.find("int test =")); |
| 223 | |
| 224 | out.str(""); |
| 225 | EXPECT_TRUE(generator.generate(u"com.lib", out)); |
| 226 | output = out.str(); |
| 227 | EXPECT_NE(std::string::npos, output.find("int test =")); |
| 228 | EXPECT_EQ(std::string::npos, output.find("int foo =")); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 229 | }*/ |
Adam Lesinski | 838a687 | 2015-05-01 13:14:05 -0700 | [diff] [blame] | 230 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 231 | TEST(JavaClassGeneratorTest, EmitOtherPackagesAttributesInStyleable) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 232 | std::unique_ptr<ResourceTable> table = |
| 233 | test::ResourceTableBuilder() |
| 234 | .SetPackageId("android", 0x01) |
| 235 | .SetPackageId("com.lib", 0x02) |
| 236 | .AddValue("android:attr/bar", ResourceId(0x01010000), |
| 237 | test::AttributeBuilder(false).Build()) |
| 238 | .AddValue("com.lib:attr/bar", ResourceId(0x02010000), |
| 239 | test::AttributeBuilder(false).Build()) |
| 240 | .AddValue("android:styleable/foo", ResourceId(0x01030000), |
| 241 | test::StyleableBuilder() |
| 242 | .AddItem("android:attr/bar", ResourceId(0x01010000)) |
| 243 | .AddItem("com.lib:attr/bar", ResourceId(0x02010000)) |
| 244 | .Build()) |
| 245 | .Build(); |
Adam Lesinski | 838a687 | 2015-05-01 13:14:05 -0700 | [diff] [blame] | 246 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 247 | std::unique_ptr<IAaptContext> context = |
| 248 | test::ContextBuilder() |
| 249 | .AddSymbolSource( |
| 250 | util::make_unique<ResourceTableSymbolSource>(table.get())) |
| 251 | .SetNameManglerPolicy(NameManglerPolicy{"android"}) |
| 252 | .Build(); |
| 253 | JavaClassGenerator generator(context.get(), table.get(), {}); |
Adam Lesinski | 838a687 | 2015-05-01 13:14:05 -0700 | [diff] [blame] | 254 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 255 | std::stringstream out; |
| 256 | EXPECT_TRUE(generator.Generate("android", &out)); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 257 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 258 | std::string output = out.str(); |
| 259 | EXPECT_NE(std::string::npos, output.find("int foo_bar=")); |
| 260 | EXPECT_NE(std::string::npos, output.find("int foo_com_lib_bar=")); |
Adam Lesinski | 838a687 | 2015-05-01 13:14:05 -0700 | [diff] [blame] | 261 | } |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 262 | |
Adam Lesinski | 3b4cd94 | 2015-10-30 16:31:42 -0700 | [diff] [blame] | 263 | TEST(JavaClassGeneratorTest, CommentsForSimpleResourcesArePresent) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 264 | std::unique_ptr<ResourceTable> table = |
| 265 | test::ResourceTableBuilder() |
| 266 | .SetPackageId("android", 0x01) |
| 267 | .AddSimple("android:id/foo", ResourceId(0x01010000)) |
| 268 | .Build(); |
| 269 | test::GetValue<Id>(table.get(), "android:id/foo") |
| 270 | ->SetComment(std::string("This is a comment\n@deprecated")); |
Adam Lesinski | 3b4cd94 | 2015-10-30 16:31:42 -0700 | [diff] [blame] | 271 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 272 | std::unique_ptr<IAaptContext> context = |
| 273 | test::ContextBuilder() |
| 274 | .AddSymbolSource( |
| 275 | util::make_unique<ResourceTableSymbolSource>(table.get())) |
| 276 | .SetNameManglerPolicy(NameManglerPolicy{"android"}) |
| 277 | .Build(); |
| 278 | JavaClassGenerator generator(context.get(), table.get(), {}); |
| 279 | std::stringstream out; |
| 280 | ASSERT_TRUE(generator.Generate("android", &out)); |
| 281 | std::string actual = out.str(); |
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 | const char* expectedText = |
| 284 | R"EOF(/** |
Adam Lesinski | 3b4cd94 | 2015-10-30 16:31:42 -0700 | [diff] [blame] | 285 | * This is a comment |
| 286 | * @deprecated |
| 287 | */ |
| 288 | @Deprecated |
Adam Lesinski | 803c7c8 | 2016-04-06 16:09:43 -0700 | [diff] [blame] | 289 | public static final int foo=0x01010000;)EOF"; |
| 290 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 291 | EXPECT_NE(std::string::npos, actual.find(expectedText)); |
Adam Lesinski | 3b4cd94 | 2015-10-30 16:31:42 -0700 | [diff] [blame] | 292 | } |
| 293 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 294 | TEST(JavaClassGeneratorTest, CommentsForEnumAndFlagAttributesArePresent) {} |
Adam Lesinski | 3b4cd94 | 2015-10-30 16:31:42 -0700 | [diff] [blame] | 295 | |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 296 | TEST(JavaClassGeneratorTest, CommentsForStyleablesAndNestedAttributesArePresent) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 297 | Attribute attr(false); |
| 298 | attr.SetComment(StringPiece("This is an attribute")); |
Adam Lesinski | 3b4cd94 | 2015-10-30 16:31:42 -0700 | [diff] [blame] | 299 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 300 | Styleable styleable; |
| 301 | styleable.entries.push_back( |
| 302 | Reference(test::ParseNameOrDie("android:attr/one"))); |
| 303 | styleable.SetComment(StringPiece("This is a styleable")); |
Adam Lesinski | 3b4cd94 | 2015-10-30 16:31:42 -0700 | [diff] [blame] | 304 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 305 | std::unique_ptr<ResourceTable> table = |
| 306 | test::ResourceTableBuilder() |
| 307 | .SetPackageId("android", 0x01) |
| 308 | .AddValue("android:attr/one", util::make_unique<Attribute>(attr)) |
| 309 | .AddValue("android:styleable/Container", |
| 310 | std::unique_ptr<Styleable>(styleable.Clone(nullptr))) |
| 311 | .Build(); |
Adam Lesinski | 74605cd | 2016-03-03 15:39:50 -0800 | [diff] [blame] | 312 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 313 | std::unique_ptr<IAaptContext> context = |
| 314 | test::ContextBuilder() |
| 315 | .AddSymbolSource( |
| 316 | util::make_unique<ResourceTableSymbolSource>(table.get())) |
| 317 | .SetNameManglerPolicy(NameManglerPolicy{"android"}) |
| 318 | .Build(); |
| 319 | JavaClassGeneratorOptions options; |
| 320 | options.use_final = false; |
| 321 | JavaClassGenerator generator(context.get(), table.get(), options); |
| 322 | std::stringstream out; |
| 323 | ASSERT_TRUE(generator.Generate("android", &out)); |
| 324 | std::string actual = out.str(); |
Adam Lesinski | 74605cd | 2016-03-03 15:39:50 -0800 | [diff] [blame] | 325 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 326 | EXPECT_NE(std::string::npos, actual.find("attr name android:one")); |
| 327 | EXPECT_NE(std::string::npos, actual.find("attr description")); |
| 328 | EXPECT_NE(std::string::npos, actual.find(attr.GetComment().data())); |
| 329 | EXPECT_NE(std::string::npos, actual.find(styleable.GetComment().data())); |
Adam Lesinski | 3b4cd94 | 2015-10-30 16:31:42 -0700 | [diff] [blame] | 330 | } |
| 331 | |
Michael Wright | feaf99f | 2016-05-06 17:16:06 +0100 | [diff] [blame] | 332 | TEST(JavaClassGeneratorTest, CommentsForRemovedAttributesAreNotPresentInClass) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 333 | Attribute attr(false); |
| 334 | attr.SetComment(StringPiece("removed")); |
Michael Wright | feaf99f | 2016-05-06 17:16:06 +0100 | [diff] [blame] | 335 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 336 | std::unique_ptr<ResourceTable> table = |
| 337 | test::ResourceTableBuilder() |
| 338 | .SetPackageId("android", 0x01) |
| 339 | .AddValue("android:attr/one", util::make_unique<Attribute>(attr)) |
| 340 | .Build(); |
Michael Wright | feaf99f | 2016-05-06 17:16:06 +0100 | [diff] [blame] | 341 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 342 | std::unique_ptr<IAaptContext> context = |
| 343 | test::ContextBuilder() |
| 344 | .AddSymbolSource( |
| 345 | util::make_unique<ResourceTableSymbolSource>(table.get())) |
| 346 | .SetNameManglerPolicy(NameManglerPolicy{"android"}) |
| 347 | .Build(); |
| 348 | JavaClassGeneratorOptions options; |
| 349 | options.use_final = false; |
| 350 | JavaClassGenerator generator(context.get(), table.get(), options); |
| 351 | std::stringstream out; |
| 352 | ASSERT_TRUE(generator.Generate("android", &out)); |
| 353 | std::string actual = out.str(); |
Michael Wright | feaf99f | 2016-05-06 17:16:06 +0100 | [diff] [blame] | 354 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 355 | EXPECT_EQ(std::string::npos, actual.find("@attr name android:one")); |
| 356 | EXPECT_EQ(std::string::npos, actual.find("@attr description")); |
Michael Wright | feaf99f | 2016-05-06 17:16:06 +0100 | [diff] [blame] | 357 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 358 | // We should find @removed only in the attribute javadoc and not anywhere else |
| 359 | // (i.e. the class |
| 360 | // javadoc). |
| 361 | const size_t pos = actual.find("removed"); |
| 362 | EXPECT_NE(std::string::npos, pos); |
| 363 | EXPECT_EQ(std::string::npos, actual.find("removed", pos + 1)); |
Michael Wright | feaf99f | 2016-05-06 17:16:06 +0100 | [diff] [blame] | 364 | } |
| 365 | |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 366 | TEST(JavaClassGeneratorTest, GenerateOnResourcesLoadedCallbackForSharedLibrary) { |
| 367 | std::unique_ptr<ResourceTable> table = |
| 368 | test::ResourceTableBuilder() |
| 369 | .SetPackageId("android", 0x00) |
| 370 | .AddValue("android:attr/foo", ResourceId(0x00010000), util::make_unique<Attribute>(false)) |
| 371 | .AddValue("android:id/foo", ResourceId(0x00020000), util::make_unique<Id>()) |
| 372 | .AddValue( |
| 373 | "android:style/foo", ResourceId(0x00030000), |
| 374 | test::StyleBuilder() |
| 375 | .AddItem("android:attr/foo", ResourceId(0x00010000), util::make_unique<Id>()) |
| 376 | .Build()) |
| 377 | .Build(); |
| 378 | |
| 379 | std::unique_ptr<IAaptContext> context = |
| 380 | test::ContextBuilder().SetPackageId(0x00).SetCompilationPackage("android").Build(); |
| 381 | |
| 382 | JavaClassGeneratorOptions options; |
| 383 | options.use_final = false; |
Adam Lesinski | b5dc4bd | 2017-02-22 19:29:29 -0800 | [diff] [blame] | 384 | options.rewrite_callback_options = OnResourcesLoadedCallbackOptions{ |
| 385 | {"com.foo", "com.boo"}, |
| 386 | }; |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 387 | JavaClassGenerator generator(context.get(), table.get(), options); |
| 388 | |
| 389 | std::stringstream out; |
| 390 | ASSERT_TRUE(generator.Generate("android", &out)); |
| 391 | |
| 392 | std::string actual = out.str(); |
| 393 | |
Adam Lesinski | b5dc4bd | 2017-02-22 19:29:29 -0800 | [diff] [blame] | 394 | EXPECT_NE(std::string::npos, actual.find("void onResourcesLoaded")); |
| 395 | EXPECT_NE(std::string::npos, actual.find("com.foo.R.onResourcesLoaded")); |
| 396 | EXPECT_NE(std::string::npos, actual.find("com.boo.R.onResourcesLoaded")); |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 397 | } |
| 398 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 399 | } // namespace aapt |