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