summaryrefslogtreecommitdiff
path: root/tools/aapt2/ResourceParser_test.cpp
diff options
context:
space:
mode:
authorAdam Lesinski <adamlesinski@google.com>2016-09-07 13:45:13 -0700
committerAdam Lesinski <adamlesinski@google.com>2016-09-07 13:45:13 -0700
commit8c3f31f022f7e094fd227ef0c2987e0846cb3e43 (patch)
treed9550e3643a3eea0d0e323d3234edb52274a96fc /tools/aapt2/ResourceParser_test.cpp
parent43321e02bc3553bdd7ea5f2af823080414d59b54 (diff)
AAPT2: Fix issue with styled string indices
Styled strings use spans to denote which part is styled (<b>, <i>, etc). Spans are simply a range of indices into the original string. In Java, we use String and its internal representation, meaning we must encode the indices using UTF16 lengths. When the internal AAPT2 representation of strings switched to UTF8, the indices also began to index into the UTF8 string. This change reverts the indices to use UTF16 lengths. Bug:31170115 Change-Id: I07b8b5b67d2542c7e0a855b601cdbd3ac4ebffb0
Diffstat (limited to 'tools/aapt2/ResourceParser_test.cpp')
-rw-r--r--tools/aapt2/ResourceParser_test.cpp34
1 files changed, 34 insertions, 0 deletions
diff --git a/tools/aapt2/ResourceParser_test.cpp b/tools/aapt2/ResourceParser_test.cpp
index 3d03a882cd02..e097740966f6 100644
--- a/tools/aapt2/ResourceParser_test.cpp
+++ b/tools/aapt2/ResourceParser_test.cpp
@@ -90,6 +90,40 @@ TEST_F(ResourceParserTest, ParseFormattedString) {
ASSERT_TRUE(testParse(input));
}
+TEST_F(ResourceParserTest, ParseStyledString) {
+ // Use a surrogate pair unicode point so that we can verify that the span indices
+ // use UTF-16 length and not UTF-18 length.
+ std::string input = "<string name=\"foo\">This is my aunt\u2019s <b>string</b></string>";
+ ASSERT_TRUE(testParse(input));
+
+ StyledString* str = test::getValue<StyledString>(&mTable, "string/foo");
+ ASSERT_NE(nullptr, str);
+
+ const std::string expectedStr = "This is my aunt\u2019s string";
+ EXPECT_EQ(expectedStr, *str->value->str);
+ EXPECT_EQ(1u, str->value->spans.size());
+
+ EXPECT_EQ(std::string("b"), *str->value->spans[0].name);
+ EXPECT_EQ(17u, str->value->spans[0].firstChar);
+ EXPECT_EQ(23u, str->value->spans[0].lastChar);
+}
+
+TEST_F(ResourceParserTest, ParseStringWithWhitespace) {
+ std::string input = "<string name=\"foo\"> This is what I think </string>";
+ ASSERT_TRUE(testParse(input));
+
+ String* str = test::getValue<String>(&mTable, "string/foo");
+ ASSERT_NE(nullptr, str);
+ EXPECT_EQ(std::string("This is what I think"), *str->value);
+
+ input = "<string name=\"foo2\">\" This is what I think \"</string>";
+ ASSERT_TRUE(testParse(input));
+
+ str = test::getValue<String>(&mTable, "string/foo2");
+ ASSERT_NE(nullptr, str);
+ EXPECT_EQ(std::string(" This is what I think "), *str->value);
+}
+
TEST_F(ResourceParserTest, IgnoreXliffTags) {
std::string input = "<string name=\"foo\" \n"
" xmlns:xliff=\"urn:oasis:names:tc:xliff:document:1.2\">\n"