Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 848585e1 authored by Ryan Mitchell's avatar Ryan Mitchell Committed by Android (Google) Code Review
Browse files

Merge "AAPT2: Fix R.java for styleable in different package"

parents bc458577 23cc5d5d
Loading
Loading
Loading
Loading
+14 −3
Original line number Diff line number Diff line
@@ -256,9 +256,20 @@ void JavaClassGenerator::ProcessStyleable(const ResourceNameRef& name, const Res
    styleable_attr.field_name =
        TransformNestedAttr(attr.name.value(), array_field_name, package_name_to_generate);

    Reference ref = attr;
    if (attr.name.value().package.empty()) {

      // If the resource does not have a package name, set the package to the unmangled package name
      // of the styleable declaration because attributes without package names would have been
      // declared in the same package as the styleable.
      ref.name = ResourceName(package_name_to_generate, ref.name.value().type,
                              ref.name.value().entry);
    }

    // Look up the symbol so that we can write out in the comments what are possible legal values
    // for this attribute.
    const SymbolTable::Symbol* symbol = context_->GetExternalSymbols()->FindByReference(attr);
    const SymbolTable::Symbol* symbol = context_->GetExternalSymbols()->FindByReference(ref);

    if (symbol && symbol->attribute) {
      // Copy the symbol data structure because the returned instance can be destroyed.
      styleable_attr.symbol = *symbol;
@@ -303,7 +314,7 @@ void JavaClassGenerator::ProcessStyleable(const ResourceNameRef& name, const Res
      const ResourceName& attr_name = entry.attr_ref->name.value();
      styleable_comment << "<tr><td><code>{@link #" << entry.field_name << " "
                        << (!attr_name.package.empty() ? attr_name.package
                                                       : context_->GetCompilationPackage())
                                                       : package_name_to_generate)
                        << ":" << attr_name.entry << "}</code></td>";

      // Only use the comment up until the first '.'. This is to stay compatible with
@@ -374,7 +385,7 @@ void JavaClassGenerator::ProcessStyleable(const ResourceNameRef& name, const Res

      StringPiece package_name = attr_name.package;
      if (package_name.empty()) {
        package_name = context_->GetCompilationPackage();
        package_name = package_name_to_generate;
      }

      std::unique_ptr<IntMember> index_member =
+49 −0
Original line number Diff line number Diff line
@@ -107,6 +107,55 @@ TEST(JavaClassGeneratorTest, CorrectPackageNameIsUsed) {
  EXPECT_THAT(output, Not(HasSubstr("com_foo$two")));
}

TEST(JavaClassGeneratorTest, StyleableAttributesWithDifferentPackageName) {
  std::unique_ptr<ResourceTable> table =
      test::ResourceTableBuilder()
          .SetPackageId("android", 0x01)
          .SetPackageId("app", 0x7f)
          .AddValue("app:attr/foo", ResourceId(0x7f010000),
                    test::AttributeBuilder().Build())
          .AddValue("app:attr/bar", ResourceId(0x7f010001),
                    test::AttributeBuilder().Build())
          .AddValue("android:attr/baz", ResourceId(0x01010000),
                    test::AttributeBuilder().Build())
          .AddValue("app:styleable/MyStyleable", ResourceId(0x7f030000),
                    test::StyleableBuilder()
                        .AddItem("app:attr/foo", ResourceId(0x7f010000))
                        .AddItem("attr/bar", ResourceId(0x7f010001))
                        .AddItem("android:attr/baz", ResourceId(0x01010000))
                        .Build())
          .Build();

  std::unique_ptr<IAaptContext> context =
      test::ContextBuilder()
          .AddSymbolSource(util::make_unique<ResourceTableSymbolSource>(table.get()))
          .SetNameManglerPolicy(NameManglerPolicy{"custom"})
          .SetCompilationPackage("custom")
          .Build();
  JavaClassGenerator generator(context.get(), table.get(), {});

  std::string output;
  StringOutputStream out(&output);
  EXPECT_TRUE(generator.Generate("app", &out));
  out.Flush();

  EXPECT_THAT(output, Not(HasSubstr("public static final int baz=0x01010000;")));
  EXPECT_THAT(output, HasSubstr("public static final int foo=0x7f010000;"));
  EXPECT_THAT(output, HasSubstr("public static final int bar=0x7f010001;"));

  EXPECT_THAT(output, HasSubstr("public static final int MyStyleable_android_baz=0;"));
  EXPECT_THAT(output, HasSubstr("public static final int MyStyleable_foo=1;"));
  EXPECT_THAT(output, HasSubstr("public static final int MyStyleable_bar=2;"));

  EXPECT_THAT(output, HasSubstr("@link #MyStyleable_android_baz android:baz"));
  EXPECT_THAT(output, HasSubstr("@link #MyStyleable_foo app:foo"));
  EXPECT_THAT(output, HasSubstr("@link #MyStyleable_bar app:bar"));

  EXPECT_THAT(output, HasSubstr("@link android.R.attr#baz"));
  EXPECT_THAT(output, HasSubstr("@link app.R.attr#foo"));
  EXPECT_THAT(output, HasSubstr("@link app.R.attr#bar"));
}

TEST(JavaClassGeneratorTest, AttrPrivateIsWrittenAsAttr) {
  std::unique_ptr<ResourceTable> table =
      test::ResourceTableBuilder()