From 761d4341fcbb711f55d73a31e79098837146236d Mon Sep 17 00:00:00 2001 From: Adam Lesinski Date: Fri, 29 Sep 2017 11:15:17 -0700 Subject: AAPT2: Fix R.java styleable + indices ordering Make sure that Styleables are directly followed by their indices. If not, Robolectric breaks. This is not strictly incorrect to have an arbitrary ordering in R.java, but its easier to just support Robolectric in this case. Bug: 65837293 Test: make aapt2_tests (cherry picked from commit af85c4deb667843a227d62275fe6992005f4c38d) Change-Id: Ia59ba58427ade386d075ca9fc9eb5b53e35beca0 --- tools/aapt2/java/JavaClassGenerator_test.cpp | 49 ++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) (limited to 'tools/aapt2/java/JavaClassGenerator_test.cpp') diff --git a/tools/aapt2/java/JavaClassGenerator_test.cpp b/tools/aapt2/java/JavaClassGenerator_test.cpp index 4f449f0db41a..668e4340e839 100644 --- a/tools/aapt2/java/JavaClassGenerator_test.cpp +++ b/tools/aapt2/java/JavaClassGenerator_test.cpp @@ -24,6 +24,8 @@ using ::android::StringPiece; using ::testing::HasSubstr; +using ::testing::Lt; +using ::testing::Ne; using ::testing::Not; namespace aapt { @@ -306,6 +308,53 @@ TEST(JavaClassGeneratorTest, CommentsForStyleablesAndNestedAttributesArePresent) EXPECT_THAT(output, HasSubstr(styleable.GetComment())); } +TEST(JavaClassGeneratorTest, StyleableAndIndicesAreColocated) { + std::unique_ptr table = + test::ResourceTableBuilder() + .SetPackageId("android", 0x01) + .AddValue("android:attr/layout_gravity", util::make_unique()) + .AddValue("android:attr/background", util::make_unique()) + .AddValue("android:styleable/ActionBar", + test::StyleableBuilder() + .AddItem("android:attr/background", ResourceId(0x01010000)) + .Build()) + .AddValue("android:styleable/ActionBar.LayoutParams", + test::StyleableBuilder() + .AddItem("android:attr/layout_gravity", ResourceId(0x01010001)) + .Build()) + .Build(); + + std::unique_ptr context = + test::ContextBuilder() + .AddSymbolSource(util::make_unique(table.get())) + .SetNameManglerPolicy(NameManglerPolicy{"android"}) + .Build(); + + JavaClassGeneratorOptions options; + JavaClassGenerator generator(context.get(), table.get(), {}); + std::stringstream out; + ASSERT_TRUE(generator.Generate("android", &out)); + std::string output = out.str(); + + std::string::size_type actionbar_pos = output.find("int[] ActionBar"); + ASSERT_THAT(actionbar_pos, Ne(std::string::npos)); + + std::string::size_type actionbar_background_pos = output.find("int ActionBar_background"); + ASSERT_THAT(actionbar_background_pos, Ne(std::string::npos)); + + std::string::size_type actionbar_layout_params_pos = output.find("int[] ActionBar_LayoutParams"); + ASSERT_THAT(actionbar_layout_params_pos, Ne(std::string::npos)); + + std::string::size_type actionbar_layout_params_layout_gravity_pos = + output.find("int ActionBar_LayoutParams_layout_gravity"); + ASSERT_THAT(actionbar_layout_params_layout_gravity_pos, Ne(std::string::npos)); + + EXPECT_THAT(actionbar_pos, Lt(actionbar_background_pos)); + EXPECT_THAT(actionbar_pos, Lt(actionbar_layout_params_pos)); + EXPECT_THAT(actionbar_background_pos, Lt(actionbar_layout_params_pos)); + EXPECT_THAT(actionbar_layout_params_pos, Lt(actionbar_layout_params_layout_gravity_pos)); +} + TEST(JavaClassGeneratorTest, CommentsForRemovedAttributesAreNotPresentInClass) { Attribute attr(false); attr.SetComment(StringPiece("removed")); -- cgit v1.2.3