JavaClassGenerator_test.cpp revision ce5e56e243d262a9b65459c3bd0bb9eaadd40628
16f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski/* 26f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski * Copyright (C) 2015 The Android Open Source Project 36f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski * 46f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski * Licensed under the Apache License, Version 2.0 (the "License"); 56f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski * you may not use this file except in compliance with the License. 66f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski * You may obtain a copy of the License at 76f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski * 86f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski * http://www.apache.org/licenses/LICENSE-2.0 96f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski * 106f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski * Unless required by applicable law or agreed to in writing, software 116f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski * distributed under the License is distributed on an "AS IS" BASIS, 126f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 136f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski * See the License for the specific language governing permissions and 146f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski * limitations under the License. 156f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski */ 166f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski 17ca5638fd85098c3d0a699492751043545f75553aAdam Lesinski#include "java/JavaClassGenerator.h" 181ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski 196f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski#include <sstream> 206f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski#include <string> 216f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski 22ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski#include "test/Test.h" 23ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski#include "util/Util.h" 24ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski 256f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinskinamespace aapt { 266f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski 271ab598f46c3ff520a67f9d80194847741f3467abAdam LesinskiTEST(JavaClassGeneratorTest, FailWhenEntryIsJavaKeyword) { 28ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski std::unique_ptr<ResourceTable> table = 29ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski test::ResourceTableBuilder() 30ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski .SetPackageId("android", 0x01) 31ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski .AddSimple("android:id/class", ResourceId(0x01020000)) 32ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski .Build(); 33ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski 34ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski std::unique_ptr<IAaptContext> context = 35ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski test::ContextBuilder() 36ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski .AddSymbolSource( 37ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski util::make_unique<ResourceTableSymbolSource>(table.get())) 38ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski .SetNameManglerPolicy(NameManglerPolicy{"android"}) 39ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski .Build(); 40ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski JavaClassGenerator generator(context.get(), table.get(), {}); 41ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski 42ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski std::stringstream out; 43ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski EXPECT_FALSE(generator.Generate("android", &out)); 446f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski} 456f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski 461ab598f46c3ff520a67f9d80194847741f3467abAdam LesinskiTEST(JavaClassGeneratorTest, TransformInvalidJavaIdentifierCharacter) { 47ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski std::unique_ptr<ResourceTable> table = 48ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski test::ResourceTableBuilder() 49ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski .SetPackageId("android", 0x01) 50ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski .AddSimple("android:id/hey-man", ResourceId(0x01020000)) 51ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski .AddValue("android:attr/cool.attr", ResourceId(0x01010000), 52ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski test::AttributeBuilder(false).Build()) 53ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski .AddValue( 54ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski "android:styleable/hey.dude", ResourceId(0x01030000), 55ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski test::StyleableBuilder() 56ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski .AddItem("android:attr/cool.attr", ResourceId(0x01010000)) 57ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski .Build()) 58ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski .Build(); 59ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski 60ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski std::unique_ptr<IAaptContext> context = 61ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski test::ContextBuilder() 62ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski .AddSymbolSource( 63ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski util::make_unique<ResourceTableSymbolSource>(table.get())) 64ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski .SetNameManglerPolicy(NameManglerPolicy{"android"}) 65ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski .Build(); 66ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski JavaClassGenerator generator(context.get(), table.get(), {}); 67ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski 68ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski std::stringstream out; 69ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski EXPECT_TRUE(generator.Generate("android", &out)); 70ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski 71ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski std::string output = out.str(); 72ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski 73ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski EXPECT_NE(std::string::npos, 74ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski output.find("public static final int hey_man=0x01020000;")); 75ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski 76ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski EXPECT_NE(std::string::npos, 77ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski output.find("public static final int[] hey_dude={")); 78ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski 79ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski EXPECT_NE(std::string::npos, 80ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski output.find("public static final int hey_dude_cool_attr=0;")); 81ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski} 826f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski 83ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam LesinskiTEST(JavaClassGeneratorTest, CorrectPackageNameIsUsed) { 84ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski std::unique_ptr<ResourceTable> table = 85ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski test::ResourceTableBuilder() 86ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski .SetPackageId("android", 0x01) 87ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski .AddSimple("android:id/one", ResourceId(0x01020000)) 88ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski .AddSimple("android:id/com.foo$two", ResourceId(0x01020001)) 89ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski .Build(); 90ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski 91ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski std::unique_ptr<IAaptContext> context = 92ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski test::ContextBuilder() 93ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski .AddSymbolSource( 94ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski util::make_unique<ResourceTableSymbolSource>(table.get())) 95ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski .SetNameManglerPolicy(NameManglerPolicy{"android"}) 96ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski .Build(); 97ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski JavaClassGenerator generator(context.get(), table.get(), {}); 98ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski std::stringstream out; 99ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski ASSERT_TRUE(generator.Generate("android", "com.android.internal", &out)); 100ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski 101ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski std::string output = out.str(); 102ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski EXPECT_NE(std::string::npos, output.find("package com.android.internal;")); 103ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski EXPECT_NE(std::string::npos, 104ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski output.find("public static final int one=0x01020000;")); 105ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski EXPECT_EQ(std::string::npos, output.find("two")); 106ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski EXPECT_EQ(std::string::npos, output.find("com_foo$two")); 107ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski} 1086f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski 109ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam LesinskiTEST(JavaClassGeneratorTest, AttrPrivateIsWrittenAsAttr) { 110ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski std::unique_ptr<ResourceTable> table = 111ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski test::ResourceTableBuilder() 112ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski .SetPackageId("android", 0x01) 113ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski .AddSimple("android:attr/two", ResourceId(0x01010001)) 114ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski .AddSimple("android:^attr-private/one", ResourceId(0x01010000)) 115ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski .Build(); 116ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski 117ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski std::unique_ptr<IAaptContext> context = 118ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski test::ContextBuilder() 119ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski .AddSymbolSource( 120ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski util::make_unique<ResourceTableSymbolSource>(table.get())) 121ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski .SetNameManglerPolicy(NameManglerPolicy{"android"}) 122ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski .Build(); 123ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski JavaClassGenerator generator(context.get(), table.get(), {}); 124ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski std::stringstream out; 125ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski ASSERT_TRUE(generator.Generate("android", &out)); 126ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski 127ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski std::string output = out.str(); 128ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski EXPECT_NE(std::string::npos, output.find("public static final class attr")); 129ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski EXPECT_EQ(std::string::npos, 130ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski output.find("public static final class ^attr-private")); 1316f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski} 1326f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski 133ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam LesinskiTEST(JavaClassGeneratorTest, OnlyWritePublicResources) { 134ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski StdErrDiagnostics diag; 135ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski std::unique_ptr<ResourceTable> table = 136ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski test::ResourceTableBuilder() 137ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski .SetPackageId("android", 0x01) 138ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski .AddSimple("android:id/one", ResourceId(0x01020000)) 139ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski .AddSimple("android:id/two", ResourceId(0x01020001)) 140ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski .AddSimple("android:id/three", ResourceId(0x01020002)) 141ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski .SetSymbolState("android:id/one", ResourceId(0x01020000), 142ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski SymbolState::kPublic) 143ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski .SetSymbolState("android:id/two", ResourceId(0x01020001), 144ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski SymbolState::kPrivate) 145ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski .Build(); 146ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski 147ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski std::unique_ptr<IAaptContext> context = 148ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski test::ContextBuilder() 149ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski .AddSymbolSource( 150ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski util::make_unique<ResourceTableSymbolSource>(table.get())) 151ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski .SetNameManglerPolicy(NameManglerPolicy{"android"}) 152ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski .Build(); 153ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski 154ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski JavaClassGeneratorOptions options; 155ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski options.types = JavaClassGeneratorOptions::SymbolTypes::kPublic; 156ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski { 157ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski JavaClassGenerator generator(context.get(), table.get(), options); 1589e10ac70155c993e7053323ad36beaea7bf7d54fAdam Lesinski std::stringstream out; 159ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski ASSERT_TRUE(generator.Generate("android", &out)); 1609e10ac70155c993e7053323ad36beaea7bf7d54fAdam Lesinski std::string output = out.str(); 161ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski EXPECT_NE(std::string::npos, 162ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski output.find("public static final int one=0x01020000;")); 1639e10ac70155c993e7053323ad36beaea7bf7d54fAdam Lesinski EXPECT_EQ(std::string::npos, output.find("two")); 164ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski EXPECT_EQ(std::string::npos, output.find("three")); 165ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski } 1669e10ac70155c993e7053323ad36beaea7bf7d54fAdam Lesinski 167ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski options.types = JavaClassGeneratorOptions::SymbolTypes::kPublicPrivate; 168ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski { 169ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski JavaClassGenerator generator(context.get(), table.get(), options); 1709e10ac70155c993e7053323ad36beaea7bf7d54fAdam Lesinski std::stringstream out; 171ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski ASSERT_TRUE(generator.Generate("android", &out)); 1729e10ac70155c993e7053323ad36beaea7bf7d54fAdam Lesinski std::string output = out.str(); 173ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski EXPECT_NE(std::string::npos, 174ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski output.find("public static final int one=0x01020000;")); 175ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski EXPECT_NE(std::string::npos, 176ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski output.find("public static final int two=0x01020001;")); 177ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski EXPECT_EQ(std::string::npos, output.find("three")); 178ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski } 1799e10ac70155c993e7053323ad36beaea7bf7d54fAdam Lesinski 180ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski options.types = JavaClassGeneratorOptions::SymbolTypes::kAll; 181ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski { 182ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski JavaClassGenerator generator(context.get(), table.get(), options); 183ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski std::stringstream out; 184ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski ASSERT_TRUE(generator.Generate("android", &out)); 185ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski std::string output = out.str(); 186ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski EXPECT_NE(std::string::npos, 187ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski output.find("public static final int one=0x01020000;")); 188ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski EXPECT_NE(std::string::npos, 189ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski output.find("public static final int two=0x01020001;")); 190ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski EXPECT_NE(std::string::npos, 191ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski output.find("public static final int three=0x01020002;")); 192ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski } 1939e10ac70155c993e7053323ad36beaea7bf7d54fAdam Lesinski} 1949e10ac70155c993e7053323ad36beaea7bf7d54fAdam Lesinski 1951ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski/* 1961ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski * TODO(adamlesinski): Re-enable this once we get merging working again. 1971ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski * TEST(JavaClassGeneratorTest, EmitPackageMangledSymbols) { 198769de98f2dd41bfe39a1c9f76aefd1ad58942733Adam Lesinski ASSERT_TRUE(addResource(ResourceName{ {}, ResourceType::kId, u"foo" }, 199769de98f2dd41bfe39a1c9f76aefd1ad58942733Adam Lesinski ResourceId{ 0x01, 0x02, 0x0000 })); 200769de98f2dd41bfe39a1c9f76aefd1ad58942733Adam Lesinski ResourceTable table; 201769de98f2dd41bfe39a1c9f76aefd1ad58942733Adam Lesinski table.setPackage(u"com.lib"); 202ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski ASSERT_TRUE(table.addResource(ResourceName{ {}, ResourceType::kId, u"test" 203ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski}, {}, 204ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski Source{ "lib.xml", 33 }, 205ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinskiutil::make_unique<Id>())); 206769de98f2dd41bfe39a1c9f76aefd1ad58942733Adam Lesinski ASSERT_TRUE(mTable->merge(std::move(table))); 207769de98f2dd41bfe39a1c9f76aefd1ad58942733Adam Lesinski 208330edcdf1316ed599fe0eb16a64330821fd92f18Adam Lesinski Linker linker(mTable, 209ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski std::make_shared<MockResolver>(mTable, std::map<ResourceName, 210ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam LesinskiResourceId>()), 211330edcdf1316ed599fe0eb16a64330821fd92f18Adam Lesinski {}); 212769de98f2dd41bfe39a1c9f76aefd1ad58942733Adam Lesinski ASSERT_TRUE(linker.linkAndValidate()); 213769de98f2dd41bfe39a1c9f76aefd1ad58942733Adam Lesinski 214769de98f2dd41bfe39a1c9f76aefd1ad58942733Adam Lesinski JavaClassGenerator generator(mTable, {}); 215769de98f2dd41bfe39a1c9f76aefd1ad58942733Adam Lesinski 216769de98f2dd41bfe39a1c9f76aefd1ad58942733Adam Lesinski std::stringstream out; 217769de98f2dd41bfe39a1c9f76aefd1ad58942733Adam Lesinski EXPECT_TRUE(generator.generate(mTable->getPackage(), out)); 218769de98f2dd41bfe39a1c9f76aefd1ad58942733Adam Lesinski std::string output = out.str(); 219769de98f2dd41bfe39a1c9f76aefd1ad58942733Adam Lesinski EXPECT_NE(std::string::npos, output.find("int foo =")); 220769de98f2dd41bfe39a1c9f76aefd1ad58942733Adam Lesinski EXPECT_EQ(std::string::npos, output.find("int test =")); 221769de98f2dd41bfe39a1c9f76aefd1ad58942733Adam Lesinski 222769de98f2dd41bfe39a1c9f76aefd1ad58942733Adam Lesinski out.str(""); 223769de98f2dd41bfe39a1c9f76aefd1ad58942733Adam Lesinski EXPECT_TRUE(generator.generate(u"com.lib", out)); 224769de98f2dd41bfe39a1c9f76aefd1ad58942733Adam Lesinski output = out.str(); 225769de98f2dd41bfe39a1c9f76aefd1ad58942733Adam Lesinski EXPECT_NE(std::string::npos, output.find("int test =")); 226769de98f2dd41bfe39a1c9f76aefd1ad58942733Adam Lesinski EXPECT_EQ(std::string::npos, output.find("int foo =")); 2271ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski}*/ 2281ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski 2291ab598f46c3ff520a67f9d80194847741f3467abAdam LesinskiTEST(JavaClassGeneratorTest, EmitOtherPackagesAttributesInStyleable) { 230ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski std::unique_ptr<ResourceTable> table = 231ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski test::ResourceTableBuilder() 232ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski .SetPackageId("android", 0x01) 233ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski .SetPackageId("com.lib", 0x02) 234ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski .AddValue("android:attr/bar", ResourceId(0x01010000), 235ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski test::AttributeBuilder(false).Build()) 236ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski .AddValue("com.lib:attr/bar", ResourceId(0x02010000), 237ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski test::AttributeBuilder(false).Build()) 238ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski .AddValue("android:styleable/foo", ResourceId(0x01030000), 239ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski test::StyleableBuilder() 240ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski .AddItem("android:attr/bar", ResourceId(0x01010000)) 241ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski .AddItem("com.lib:attr/bar", ResourceId(0x02010000)) 242ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski .Build()) 243ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski .Build(); 244ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski 245ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski std::unique_ptr<IAaptContext> context = 246ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski test::ContextBuilder() 247ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski .AddSymbolSource( 248ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski util::make_unique<ResourceTableSymbolSource>(table.get())) 249ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski .SetNameManglerPolicy(NameManglerPolicy{"android"}) 250ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski .Build(); 251ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski JavaClassGenerator generator(context.get(), table.get(), {}); 252ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski 253ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski std::stringstream out; 254ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski EXPECT_TRUE(generator.Generate("android", &out)); 255ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski 256ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski std::string output = out.str(); 257ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski EXPECT_NE(std::string::npos, output.find("int foo_bar=")); 258ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski EXPECT_NE(std::string::npos, output.find("int foo_com_lib_bar=")); 259838a68705bc79527e592265371cbe4d8e888d9d9Adam Lesinski} 260769de98f2dd41bfe39a1c9f76aefd1ad58942733Adam Lesinski 2613b4cd94034ff3e5567a2ba6da35d640ff61db4b9Adam LesinskiTEST(JavaClassGeneratorTest, CommentsForSimpleResourcesArePresent) { 262ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski std::unique_ptr<ResourceTable> table = 263ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski test::ResourceTableBuilder() 264ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski .SetPackageId("android", 0x01) 265ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski .AddSimple("android:id/foo", ResourceId(0x01010000)) 266ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski .Build(); 267ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski test::GetValue<Id>(table.get(), "android:id/foo") 268ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski ->SetComment(std::string("This is a comment\n@deprecated")); 269ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski 270ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski std::unique_ptr<IAaptContext> context = 271ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski test::ContextBuilder() 272ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski .AddSymbolSource( 273ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski util::make_unique<ResourceTableSymbolSource>(table.get())) 274ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski .SetNameManglerPolicy(NameManglerPolicy{"android"}) 275ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski .Build(); 276ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski JavaClassGenerator generator(context.get(), table.get(), {}); 277ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski std::stringstream out; 278ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski ASSERT_TRUE(generator.Generate("android", &out)); 279ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski std::string actual = out.str(); 280ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski 281ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski const char* expectedText = 282ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski R"EOF(/** 2833b4cd94034ff3e5567a2ba6da35d640ff61db4b9Adam Lesinski * This is a comment 2843b4cd94034ff3e5567a2ba6da35d640ff61db4b9Adam Lesinski * @deprecated 2853b4cd94034ff3e5567a2ba6da35d640ff61db4b9Adam Lesinski */ 2863b4cd94034ff3e5567a2ba6da35d640ff61db4b9Adam Lesinski @Deprecated 287803c7c807969bea1f1c50f348832f5b60ad05d8eAdam Lesinski public static final int foo=0x01010000;)EOF"; 288803c7c807969bea1f1c50f348832f5b60ad05d8eAdam Lesinski 289ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski EXPECT_NE(std::string::npos, actual.find(expectedText)); 2903b4cd94034ff3e5567a2ba6da35d640ff61db4b9Adam Lesinski} 2913b4cd94034ff3e5567a2ba6da35d640ff61db4b9Adam Lesinski 292ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam LesinskiTEST(JavaClassGeneratorTest, CommentsForEnumAndFlagAttributesArePresent) {} 293ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski 294ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam LesinskiTEST(JavaClassGeneratorTest, 295ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski CommentsForStyleablesAndNestedAttributesArePresent) { 296ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski Attribute attr(false); 297ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski attr.SetComment(StringPiece("This is an attribute")); 298ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski 299ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski Styleable styleable; 300ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski styleable.entries.push_back( 301ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski Reference(test::ParseNameOrDie("android:attr/one"))); 302ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski styleable.SetComment(StringPiece("This is a styleable")); 303ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski 304ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski std::unique_ptr<ResourceTable> table = 305ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski test::ResourceTableBuilder() 306ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski .SetPackageId("android", 0x01) 307ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski .AddValue("android:attr/one", util::make_unique<Attribute>(attr)) 308ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski .AddValue("android:styleable/Container", 309ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski std::unique_ptr<Styleable>(styleable.Clone(nullptr))) 310ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski .Build(); 311ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski 312ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski std::unique_ptr<IAaptContext> context = 313ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski test::ContextBuilder() 314ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski .AddSymbolSource( 315ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski util::make_unique<ResourceTableSymbolSource>(table.get())) 316ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski .SetNameManglerPolicy(NameManglerPolicy{"android"}) 317ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski .Build(); 318ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski JavaClassGeneratorOptions options; 319ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski options.use_final = false; 320ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski JavaClassGenerator generator(context.get(), table.get(), options); 321ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski std::stringstream out; 322ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski ASSERT_TRUE(generator.Generate("android", &out)); 323ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski std::string actual = out.str(); 324ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski 325ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski EXPECT_NE(std::string::npos, actual.find("attr name android:one")); 326ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski EXPECT_NE(std::string::npos, actual.find("attr description")); 327ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski EXPECT_NE(std::string::npos, actual.find(attr.GetComment().data())); 328ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski EXPECT_NE(std::string::npos, actual.find(styleable.GetComment().data())); 3293b4cd94034ff3e5567a2ba6da35d640ff61db4b9Adam Lesinski} 3303b4cd94034ff3e5567a2ba6da35d640ff61db4b9Adam Lesinski 331feaf99fa1b7563f15dbd4211718a6cfb7a3cc3c8Michael WrightTEST(JavaClassGeneratorTest, CommentsForRemovedAttributesAreNotPresentInClass) { 332ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski Attribute attr(false); 333ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski attr.SetComment(StringPiece("removed")); 334ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski 335ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski std::unique_ptr<ResourceTable> table = 336ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski test::ResourceTableBuilder() 337ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski .SetPackageId("android", 0x01) 338ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski .AddValue("android:attr/one", util::make_unique<Attribute>(attr)) 339ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski .Build(); 340ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski 341ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski std::unique_ptr<IAaptContext> context = 342ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski test::ContextBuilder() 343ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski .AddSymbolSource( 344ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski util::make_unique<ResourceTableSymbolSource>(table.get())) 345ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski .SetNameManglerPolicy(NameManglerPolicy{"android"}) 346ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski .Build(); 347ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski JavaClassGeneratorOptions options; 348ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski options.use_final = false; 349ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski JavaClassGenerator generator(context.get(), table.get(), options); 350ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski std::stringstream out; 351ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski ASSERT_TRUE(generator.Generate("android", &out)); 352ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski std::string actual = out.str(); 353ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski 354ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski EXPECT_EQ(std::string::npos, actual.find("@attr name android:one")); 355ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski EXPECT_EQ(std::string::npos, actual.find("@attr description")); 356ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski 357ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski // We should find @removed only in the attribute javadoc and not anywhere else 358ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski // (i.e. the class 359ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski // javadoc). 360ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski const size_t pos = actual.find("removed"); 361ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski EXPECT_NE(std::string::npos, pos); 362ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski EXPECT_EQ(std::string::npos, actual.find("removed", pos + 1)); 363feaf99fa1b7563f15dbd4211718a6cfb7a3cc3c8Michael Wright} 364feaf99fa1b7563f15dbd4211718a6cfb7a3cc3c8Michael Wright 365ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski} // namespace aapt 366