summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Yurii Zubrytskyi <zyy@google.com> 2024-07-02 15:31:01 -0700
committer Yurii Zubrytskyi <zyy@google.com> 2024-07-03 03:15:36 +0000
commit97f0f1e4ea9232e86ee839d5d9e5e6d029f3700c (patch)
tree4c779d7f7b1ad32087a2cc558f2418fda94a6a26
parent92a51cacc06ada68e9a54a165d104bc32efcff77 (diff)
[aapt2] Improve dump chunks for string pools, fix diff
- `dump chunks` command now prints extra header data for the string pool, and outputs all style information for styled strings. - `diff` command used to only correctly compare the first span in the StyledString data type, making them appear different if there was a string with more than one span Flag: EXEMPT bugfix Test: atest aapt2_tests Change-Id: I377718c03d6a464cb4db22399b0f067e6a6e04d6
-rw-r--r--libs/androidfw/StringPool.cpp2
-rw-r--r--tools/aapt2/Debug.cpp27
-rw-r--r--tools/aapt2/ResourceValues_test.cpp29
-rw-r--r--tools/aapt2/cmd/Diff.cpp2
-rw-r--r--tools/aapt2/test/Fixture.h2
5 files changed, 58 insertions, 4 deletions
diff --git a/libs/androidfw/StringPool.cpp b/libs/androidfw/StringPool.cpp
index 1cb8df311c89..ad445c042e63 100644
--- a/libs/androidfw/StringPool.cpp
+++ b/libs/androidfw/StringPool.cpp
@@ -132,7 +132,7 @@ bool StringPool::StyleRef::operator==(const StyleRef& rhs) const {
auto rhs_iter = rhs.entry_->spans.begin();
for (const Span& span : entry_->spans) {
- const Span& rhs_span = *rhs_iter;
+ const Span& rhs_span = *rhs_iter++;
if (span.first_char != rhs_span.first_char || span.last_char != rhs_span.last_char ||
span.name != rhs_span.name) {
return false;
diff --git a/tools/aapt2/Debug.cpp b/tools/aapt2/Debug.cpp
index 6a17ef85a755..df1d51e37660 100644
--- a/tools/aapt2/Debug.cpp
+++ b/tools/aapt2/Debug.cpp
@@ -763,10 +763,35 @@ class ChunkPrinter {
pool->setTo(chunk, android::util::DeviceToHost32(
(reinterpret_cast<const ResChunk_header*>(chunk))->size));
- printer_->Print("\n");
+ printer_->Print(StringPrintf(" strings: %zd styles %zd flags: %s|%s\n", pool->size(),
+ pool->styleCount(), pool->isUTF8() ? "UTF-8" : "UTF-16",
+ pool->isSorted() ? "SORTED" : "NON-SORTED"));
for (size_t i = 0; i < pool->size(); i++) {
printer_->Print(StringPrintf("#%zd : %s\n", i, android::util::GetString(*pool, i).c_str()));
+ if (i < pool->styleCount()) {
+ printer_->Print(" [Style] ");
+ auto maybe_style = pool->styleAt(i);
+ if (!maybe_style) {
+ printer_->Print("??? missing\n");
+ } else {
+ std::vector<const ResStringPool_span*> spans;
+ for (auto style = maybe_style.value().unsafe_ptr();
+ style->name.index != android::ResStringPool_span::END; ++style) {
+ spans.push_back(style);
+ }
+ printer_->Print(StringPrintf("(%zd)", spans.size()));
+ if (!spans.empty()) {
+ printer_->Print(" :");
+ for (const auto& span : spans) {
+ printer_->Print(StringPrintf(
+ " %s:%u,%u", android::util::GetString(*pool, span->name.index).c_str(),
+ span->firstChar, span->lastChar));
+ }
+ printer_->Print("\n");
+ }
+ }
+ }
}
}
diff --git a/tools/aapt2/ResourceValues_test.cpp b/tools/aapt2/ResourceValues_test.cpp
index d788e3fd5fc7..b30348ddd4b4 100644
--- a/tools/aapt2/ResourceValues_test.cpp
+++ b/tools/aapt2/ResourceValues_test.cpp
@@ -184,6 +184,35 @@ TEST(ResourcesValuesTest, StringClones) {
EXPECT_THAT(pool_b.strings()[0]->value, StrEq("hello"));
}
+TEST(ResourcesValuesTest, StringEquals) {
+ android::StringPool pool;
+
+ String str(pool.MakeRef("hello", android::StringPool::Context(test::ParseConfigOrDie("en"))));
+ String str2(pool.MakeRef("hello"));
+ EXPECT_TRUE(str.Equals(&str2));
+ EXPECT_TRUE(str2.Equals(&str));
+
+ String str3(pool.MakeRef("how are you"));
+ EXPECT_FALSE(str.Equals(&str3));
+}
+
+TEST(ResourcesValuesTest, StyledStringEquals) {
+ android::StringPool pool;
+
+ StyledString ss(pool.MakeRef(android::StyleString{"hello", {{"b", 0, 1}, {"u", 2, 4}}}));
+ StyledString ss2(pool.MakeRef(android::StyleString{"hello", {{"b", 0, 1}, {"u", 2, 4}}}));
+ StyledString ss3(pool.MakeRef(android::StyleString{"hi", {{"b", 0, 1}, {"u", 2, 4}}}));
+ StyledString ss4(pool.MakeRef(android::StyleString{"hello", {{"b", 0, 1}}}));
+ StyledString ss5(pool.MakeRef(android::StyleString{"hello", {{"b", 0, 1}, {"u", 3, 4}}}));
+ StyledString ss6(pool.MakeRef(android::StyleString{"hello", {{"b", 0, 1}, {"s", 2, 4}}}));
+ EXPECT_TRUE(ss.Equals(&ss2));
+ EXPECT_TRUE(ss2.Equals(&ss));
+ EXPECT_FALSE(ss.Equals(&ss3));
+ EXPECT_FALSE(ss.Equals(&ss4));
+ EXPECT_FALSE(ss.Equals(&ss5));
+ EXPECT_FALSE(ss.Equals(&ss6));
+}
+
TEST(ResourceValuesTest, StyleMerges) {
android::StringPool pool_a;
android::StringPool pool_b;
diff --git a/tools/aapt2/cmd/Diff.cpp b/tools/aapt2/cmd/Diff.cpp
index 5bfc73233bfe..6da3176b2bee 100644
--- a/tools/aapt2/cmd/Diff.cpp
+++ b/tools/aapt2/cmd/Diff.cpp
@@ -106,7 +106,7 @@ static bool EmitResourceConfigValueDiff(
if (!value_a->Equals(value_b)) {
std::stringstream str_stream;
str_stream << "value " << pkg_a.name << ":" << type_a.named_type << "/" << entry_a.name
- << " config=" << config_value_a->config << " does not match:\n";
+ << " config='" << config_value_a->config << "' does not match:\n";
value_a->Print(&str_stream);
str_stream << "\n vs \n";
value_b->Print(&str_stream);
diff --git a/tools/aapt2/test/Fixture.h b/tools/aapt2/test/Fixture.h
index ba4a734e03bb..14298d1678f0 100644
--- a/tools/aapt2/test/Fixture.h
+++ b/tools/aapt2/test/Fixture.h
@@ -127,4 +127,4 @@ struct LinkCommandBuilder {
} // namespace aapt
-#endif // AAPT_TEST_FIXTURE_H \ No newline at end of file
+#endif // AAPT_TEST_FIXTURE_H