2014-11-14 14:48:12 -08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2015 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "JavaClassGenerator.h"
|
2015-08-14 14:26:04 -07:00
|
|
|
#include "util/Util.h"
|
|
|
|
|
|
|
|
#include "test/Builders.h"
|
2014-11-14 14:48:12 -08:00
|
|
|
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
#include <sstream>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
namespace aapt {
|
|
|
|
|
2015-08-14 14:26:04 -07:00
|
|
|
TEST(JavaClassGeneratorTest, FailWhenEntryIsJavaKeyword) {
|
|
|
|
std::unique_ptr<ResourceTable> table = test::ResourceTableBuilder()
|
|
|
|
.setPackageId(u"android", 0x01)
|
|
|
|
.addSimple(u"@android:id/class", ResourceId(0x01020000))
|
|
|
|
.build();
|
2014-11-14 14:48:12 -08:00
|
|
|
|
2015-08-14 14:26:04 -07:00
|
|
|
JavaClassGenerator generator(table.get(), {});
|
2014-11-14 14:48:12 -08:00
|
|
|
|
|
|
|
std::stringstream out;
|
2015-08-14 14:26:04 -07:00
|
|
|
EXPECT_FALSE(generator.generate(u"android", &out));
|
2014-11-14 14:48:12 -08:00
|
|
|
}
|
|
|
|
|
2015-08-14 14:26:04 -07:00
|
|
|
TEST(JavaClassGeneratorTest, TransformInvalidJavaIdentifierCharacter) {
|
|
|
|
std::unique_ptr<ResourceTable> table = test::ResourceTableBuilder()
|
|
|
|
.setPackageId(u"android", 0x01)
|
|
|
|
.addSimple(u"@android:id/hey-man", ResourceId(0x01020000))
|
|
|
|
.addSimple(u"@android:attr/cool.attr", ResourceId(0x01010000))
|
|
|
|
.addValue(u"@android:styleable/hey.dude", ResourceId(0x01030000),
|
|
|
|
test::StyleableBuilder()
|
|
|
|
.addItem(u"@android:attr/cool.attr", ResourceId(0x01010000))
|
|
|
|
.build())
|
|
|
|
.build();
|
2014-11-14 14:48:12 -08:00
|
|
|
|
2015-08-14 14:26:04 -07:00
|
|
|
JavaClassGenerator generator(table.get(), {});
|
2014-11-14 14:48:12 -08:00
|
|
|
|
|
|
|
std::stringstream out;
|
2015-08-14 14:26:04 -07:00
|
|
|
EXPECT_TRUE(generator.generate(u"android", &out));
|
|
|
|
|
2014-11-14 14:48:12 -08:00
|
|
|
std::string output = out.str();
|
|
|
|
|
|
|
|
EXPECT_NE(std::string::npos,
|
|
|
|
output.find("public static final int hey_man = 0x01020000;"));
|
|
|
|
|
|
|
|
EXPECT_NE(std::string::npos,
|
|
|
|
output.find("public static final int[] hey_dude = {"));
|
|
|
|
|
|
|
|
EXPECT_NE(std::string::npos,
|
|
|
|
output.find("public static final int hey_dude_cool_attr = 0;"));
|
|
|
|
}
|
|
|
|
|
2015-10-16 14:37:48 -07:00
|
|
|
TEST(JavaClassGeneratorTest, CorrectPackageNameIsUsed) {
|
|
|
|
std::unique_ptr<ResourceTable> table = test::ResourceTableBuilder()
|
|
|
|
.setPackageId(u"android", 0x01)
|
|
|
|
.addSimple(u"@android:id/one", ResourceId(0x01020000))
|
|
|
|
.addSimple(u"@android:id/com.foo$two", ResourceId(0x01020001))
|
|
|
|
.build();
|
|
|
|
|
|
|
|
JavaClassGenerator generator(table.get(), {});
|
|
|
|
std::stringstream out;
|
|
|
|
ASSERT_TRUE(generator.generate(u"android", u"com.android.internal", &out));
|
|
|
|
|
|
|
|
std::string output = out.str();
|
|
|
|
EXPECT_NE(std::string::npos, output.find("package com.android.internal;"));
|
|
|
|
EXPECT_NE(std::string::npos, output.find("public static final int one = 0x01020000;"));
|
|
|
|
EXPECT_EQ(std::string::npos, output.find("two"));
|
|
|
|
EXPECT_EQ(std::string::npos, output.find("com_foo$two"));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(JavaClassGeneratorTest, AttrPrivateIsWrittenAsAttr) {
|
|
|
|
std::unique_ptr<ResourceTable> table = test::ResourceTableBuilder()
|
|
|
|
.setPackageId(u"android", 0x01)
|
|
|
|
.addSimple(u"@android:^attr-private/one", ResourceId(0x01010000))
|
|
|
|
.build();
|
|
|
|
|
|
|
|
JavaClassGenerator generator(table.get(), {});
|
|
|
|
std::stringstream out;
|
|
|
|
ASSERT_TRUE(generator.generate(u"android", &out));
|
|
|
|
|
|
|
|
std::string output = out.str();
|
|
|
|
EXPECT_NE(std::string::npos, output.find("public static final class attr"));
|
|
|
|
EXPECT_EQ(std::string::npos, output.find("public static final class ^attr-private"));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(JavaClassGeneratorTest, OnlyWritePublicResources) {
|
|
|
|
StdErrDiagnostics diag;
|
|
|
|
std::unique_ptr<ResourceTable> table = test::ResourceTableBuilder()
|
|
|
|
.setPackageId(u"android", 0x01)
|
|
|
|
.addSimple(u"@android:id/one", ResourceId(0x01020000))
|
|
|
|
.addSimple(u"@android:id/two", ResourceId(0x01020001))
|
|
|
|
.addSimple(u"@android:id/three", ResourceId(0x01020002))
|
2015-10-22 12:48:43 -07:00
|
|
|
.setSymbolState(u"@android:id/one", ResourceId(0x01020000), SymbolState::kPublic)
|
|
|
|
.setSymbolState(u"@android:id/two", ResourceId(0x01020001), SymbolState::kPrivate)
|
2015-10-16 14:37:48 -07:00
|
|
|
.build();
|
|
|
|
|
|
|
|
JavaClassGeneratorOptions options;
|
|
|
|
options.types = JavaClassGeneratorOptions::SymbolTypes::kPublic;
|
|
|
|
{
|
|
|
|
JavaClassGenerator generator(table.get(), options);
|
|
|
|
std::stringstream out;
|
|
|
|
ASSERT_TRUE(generator.generate(u"android", &out));
|
|
|
|
std::string output = out.str();
|
|
|
|
EXPECT_NE(std::string::npos, output.find("public static final int one = 0x01020000;"));
|
|
|
|
EXPECT_EQ(std::string::npos, output.find("two"));
|
|
|
|
EXPECT_EQ(std::string::npos, output.find("three"));
|
|
|
|
}
|
|
|
|
|
|
|
|
options.types = JavaClassGeneratorOptions::SymbolTypes::kPublicPrivate;
|
|
|
|
{
|
|
|
|
JavaClassGenerator generator(table.get(), options);
|
|
|
|
std::stringstream out;
|
|
|
|
ASSERT_TRUE(generator.generate(u"android", &out));
|
|
|
|
std::string output = out.str();
|
|
|
|
EXPECT_NE(std::string::npos, output.find("public static final int one = 0x01020000;"));
|
|
|
|
EXPECT_NE(std::string::npos, output.find("public static final int two = 0x01020001;"));
|
|
|
|
EXPECT_EQ(std::string::npos, output.find("three"));
|
|
|
|
}
|
|
|
|
|
|
|
|
options.types = JavaClassGeneratorOptions::SymbolTypes::kAll;
|
|
|
|
{
|
|
|
|
JavaClassGenerator generator(table.get(), options);
|
|
|
|
std::stringstream out;
|
|
|
|
ASSERT_TRUE(generator.generate(u"android", &out));
|
|
|
|
std::string output = out.str();
|
|
|
|
EXPECT_NE(std::string::npos, output.find("public static final int one = 0x01020000;"));
|
|
|
|
EXPECT_NE(std::string::npos, output.find("public static final int two = 0x01020001;"));
|
|
|
|
EXPECT_NE(std::string::npos, output.find("public static final int three = 0x01020002;"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-14 14:26:04 -07:00
|
|
|
/*
|
|
|
|
* TODO(adamlesinski): Re-enable this once we get merging working again.
|
|
|
|
* TEST(JavaClassGeneratorTest, EmitPackageMangledSymbols) {
|
2015-04-10 19:43:55 -07:00
|
|
|
ASSERT_TRUE(addResource(ResourceName{ {}, ResourceType::kId, u"foo" },
|
|
|
|
ResourceId{ 0x01, 0x02, 0x0000 }));
|
|
|
|
ResourceTable table;
|
|
|
|
table.setPackage(u"com.lib");
|
|
|
|
ASSERT_TRUE(table.addResource(ResourceName{ {}, ResourceType::kId, u"test" }, {},
|
2015-08-14 14:26:04 -07:00
|
|
|
Source{ "lib.xml", 33 }, util::make_unique<Id>()));
|
2015-04-10 19:43:55 -07:00
|
|
|
ASSERT_TRUE(mTable->merge(std::move(table)));
|
|
|
|
|
2015-05-04 17:40:56 -07:00
|
|
|
Linker linker(mTable,
|
|
|
|
std::make_shared<MockResolver>(mTable, std::map<ResourceName, ResourceId>()),
|
|
|
|
{});
|
2015-04-10 19:43:55 -07:00
|
|
|
ASSERT_TRUE(linker.linkAndValidate());
|
|
|
|
|
|
|
|
JavaClassGenerator generator(mTable, {});
|
|
|
|
|
|
|
|
std::stringstream out;
|
|
|
|
EXPECT_TRUE(generator.generate(mTable->getPackage(), out));
|
|
|
|
std::string output = out.str();
|
|
|
|
EXPECT_NE(std::string::npos, output.find("int foo ="));
|
|
|
|
EXPECT_EQ(std::string::npos, output.find("int test ="));
|
|
|
|
|
|
|
|
out.str("");
|
|
|
|
EXPECT_TRUE(generator.generate(u"com.lib", out));
|
|
|
|
output = out.str();
|
|
|
|
EXPECT_NE(std::string::npos, output.find("int test ="));
|
|
|
|
EXPECT_EQ(std::string::npos, output.find("int foo ="));
|
2015-08-14 14:26:04 -07:00
|
|
|
}*/
|
|
|
|
|
|
|
|
TEST(JavaClassGeneratorTest, EmitOtherPackagesAttributesInStyleable) {
|
|
|
|
std::unique_ptr<ResourceTable> table = test::ResourceTableBuilder()
|
|
|
|
.setPackageId(u"android", 0x01)
|
|
|
|
.setPackageId(u"com.lib", 0x02)
|
|
|
|
.addSimple(u"@android:attr/bar", ResourceId(0x01010000))
|
|
|
|
.addSimple(u"@com.lib:attr/bar", ResourceId(0x02010000))
|
|
|
|
.addValue(u"@android:styleable/foo", ResourceId(0x01030000),
|
|
|
|
test::StyleableBuilder()
|
|
|
|
.addItem(u"@android:attr/bar", ResourceId(0x01010000))
|
|
|
|
.addItem(u"@com.lib:attr/bar", ResourceId(0x02010000))
|
|
|
|
.build())
|
|
|
|
.build();
|
|
|
|
|
|
|
|
JavaClassGenerator generator(table.get(), {});
|
2015-05-01 13:14:05 -07:00
|
|
|
|
|
|
|
std::stringstream out;
|
2015-08-14 14:26:04 -07:00
|
|
|
EXPECT_TRUE(generator.generate(u"android", &out));
|
|
|
|
|
2015-05-01 13:14:05 -07:00
|
|
|
std::string output = out.str();
|
2015-08-14 14:26:04 -07:00
|
|
|
EXPECT_NE(std::string::npos, output.find("int foo_bar ="));
|
|
|
|
EXPECT_NE(std::string::npos, output.find("int foo_com_lib_bar ="));
|
2015-05-01 13:14:05 -07:00
|
|
|
}
|
2015-04-10 19:43:55 -07:00
|
|
|
|
2014-11-14 14:48:12 -08:00
|
|
|
} // namespace aapt
|