summaryrefslogtreecommitdiff
path: root/xfa/fde/cfde_texteditengine_unittest.cpp
diff options
context:
space:
mode:
authorDan Sinclair <dsinclair@chromium.org>2017-09-19 10:07:20 -0400
committerChromium commit bot <commit-bot@chromium.org>2017-09-19 14:16:31 +0000
commit0264e97bceaca69d0b0232ac680bd7b26e2db66a (patch)
tree85ea26e8a518469b74c3db58d5a814c102a3622c /xfa/fde/cfde_texteditengine_unittest.cpp
parentb02aa61946a62639dd9d39bc00eff6f829b1e414 (diff)
downloadpdfium-0264e97bceaca69d0b0232ac680bd7b26e2db66a.tar.xz
Convert selection to use count instead of end index
This CL changes the Text Edit Engine code to use a count instead of an end index for the selection range. Using count lets us differentiate a selection at the beginning of 1 character and an empty selection. A few new tests were added to test unicode word break behaviour, some are not working yet and are commented out. Change-Id: Icce8f5003102ef0a850151ccdf16d3c2226d94bf Reviewed-on: https://pdfium-review.googlesource.com/13491 Commit-Queue: dsinclair <dsinclair@chromium.org> Reviewed-by: Henrique Nakashima <hnakashima@chromium.org>
Diffstat (limited to 'xfa/fde/cfde_texteditengine_unittest.cpp')
-rw-r--r--xfa/fde/cfde_texteditengine_unittest.cpp149
1 files changed, 96 insertions, 53 deletions
diff --git a/xfa/fde/cfde_texteditengine_unittest.cpp b/xfa/fde/cfde_texteditengine_unittest.cpp
index 48ed647945..51940f21a8 100644
--- a/xfa/fde/cfde_texteditengine_unittest.cpp
+++ b/xfa/fde/cfde_texteditengine_unittest.cpp
@@ -244,10 +244,10 @@ TEST_F(CFDE_TextEditEngineTest, Selection) {
engine()->SelectAll();
size_t start_idx;
- size_t end_idx;
- std::tie(start_idx, end_idx) = engine()->GetSelection();
+ size_t count;
+ std::tie(start_idx, count) = engine()->GetSelection();
EXPECT_EQ(0U, start_idx);
- EXPECT_EQ(10U, end_idx);
+ EXPECT_EQ(11U, count);
// Selection before gap.
EXPECT_STREQ(L"Hello World", engine()->GetSelectedText().c_str());
@@ -272,7 +272,7 @@ TEST_F(CFDE_TextEditEngineTest, Selection) {
EXPECT_STREQ(L"", engine()->GetText().c_str());
engine()->Insert(0, L"Hello World");
- engine()->SetSelection(5, 9);
+ engine()->SetSelection(5, 5);
EXPECT_STREQ(L" Worl", engine()->DeleteSelectedText().c_str());
EXPECT_FALSE(engine()->HasSelection());
EXPECT_STREQ(L"Hellod", engine()->GetText().c_str());
@@ -419,112 +419,155 @@ TEST_F(CFDE_TextEditEngineTest, GetIndexForPoint) {
TEST_F(CFDE_TextEditEngineTest, BoundsForWordAt) {
size_t start_idx;
- size_t end_idx;
+ size_t count;
- std::tie(start_idx, end_idx) = engine()->BoundsForWordAt(100);
+ std::tie(start_idx, count) = engine()->BoundsForWordAt(100);
EXPECT_EQ(0U, start_idx);
- EXPECT_EQ(0U, end_idx);
- engine()->SetSelection(start_idx, end_idx);
+ EXPECT_EQ(0U, count);
+ engine()->SetSelection(start_idx, count);
EXPECT_STREQ(L"", engine()->GetSelectedText().c_str());
engine()->Clear();
engine()->Insert(0, L"Hello");
- std::tie(start_idx, end_idx) = engine()->BoundsForWordAt(0);
+ std::tie(start_idx, count) = engine()->BoundsForWordAt(0);
EXPECT_EQ(0U, start_idx);
- EXPECT_EQ(4U, end_idx);
- engine()->SetSelection(start_idx, end_idx);
+ EXPECT_EQ(5U, count);
+ engine()->SetSelection(start_idx, count);
EXPECT_STREQ(L"Hello", engine()->GetSelectedText().c_str());
engine()->Clear();
engine()->Insert(0, L"Hello World");
- std::tie(start_idx, end_idx) = engine()->BoundsForWordAt(100);
- EXPECT_EQ(11U, start_idx);
- EXPECT_EQ(11U, end_idx);
- engine()->SetSelection(start_idx, end_idx);
+ std::tie(start_idx, count) = engine()->BoundsForWordAt(100);
+ EXPECT_EQ(0U, start_idx);
+ EXPECT_EQ(0U, count);
+ engine()->SetSelection(start_idx, count);
EXPECT_STREQ(L"", engine()->GetSelectedText().c_str());
- std::tie(start_idx, end_idx) = engine()->BoundsForWordAt(0);
+ std::tie(start_idx, count) = engine()->BoundsForWordAt(0);
EXPECT_EQ(0U, start_idx);
- EXPECT_EQ(4U, end_idx);
- engine()->SetSelection(start_idx, end_idx);
+ EXPECT_EQ(5U, count);
+ engine()->SetSelection(start_idx, count);
EXPECT_STREQ(L"Hello", engine()->GetSelectedText().c_str());
- std::tie(start_idx, end_idx) = engine()->BoundsForWordAt(1);
+ std::tie(start_idx, count) = engine()->BoundsForWordAt(1);
EXPECT_EQ(0U, start_idx);
- EXPECT_EQ(4U, end_idx);
- engine()->SetSelection(start_idx, end_idx);
+ EXPECT_EQ(5U, count);
+ engine()->SetSelection(start_idx, count);
EXPECT_STREQ(L"Hello", engine()->GetSelectedText().c_str());
- std::tie(start_idx, end_idx) = engine()->BoundsForWordAt(4);
+ std::tie(start_idx, count) = engine()->BoundsForWordAt(4);
EXPECT_EQ(0U, start_idx);
- EXPECT_EQ(4U, end_idx);
- engine()->SetSelection(start_idx, end_idx);
+ EXPECT_EQ(5U, count);
+ engine()->SetSelection(start_idx, count);
EXPECT_STREQ(L"Hello", engine()->GetSelectedText().c_str());
// Select the space
- std::tie(start_idx, end_idx) = engine()->BoundsForWordAt(5);
+ std::tie(start_idx, count) = engine()->BoundsForWordAt(5);
EXPECT_EQ(5U, start_idx);
- EXPECT_EQ(5U, end_idx);
- engine()->SetSelection(start_idx, end_idx);
- EXPECT_STREQ(L"", engine()->GetSelectedText().c_str());
+ EXPECT_EQ(1U, count);
+ engine()->SetSelection(start_idx, count);
+ EXPECT_STREQ(L" ", engine()->GetSelectedText().c_str());
- std::tie(start_idx, end_idx) = engine()->BoundsForWordAt(6);
+ std::tie(start_idx, count) = engine()->BoundsForWordAt(6);
EXPECT_EQ(6U, start_idx);
- EXPECT_EQ(10U, end_idx);
- engine()->SetSelection(start_idx, end_idx);
+ EXPECT_EQ(5U, count);
+ engine()->SetSelection(start_idx, count);
EXPECT_STREQ(L"World", engine()->GetSelectedText().c_str());
engine()->Clear();
engine()->Insert(0, L"123 456 789");
- std::tie(start_idx, end_idx) = engine()->BoundsForWordAt(5);
- engine()->SetSelection(start_idx, end_idx);
+ std::tie(start_idx, count) = engine()->BoundsForWordAt(5);
+ engine()->SetSelection(start_idx, count);
EXPECT_STREQ(L"456", engine()->GetSelectedText().c_str());
engine()->Clear();
engine()->Insert(0, L"123def789");
- std::tie(start_idx, end_idx) = engine()->BoundsForWordAt(5);
- engine()->SetSelection(start_idx, end_idx);
+ std::tie(start_idx, count) = engine()->BoundsForWordAt(5);
+ engine()->SetSelection(start_idx, count);
EXPECT_STREQ(L"123def789", engine()->GetSelectedText().c_str());
engine()->Clear();
engine()->Insert(0, L"abc456ghi");
- std::tie(start_idx, end_idx) = engine()->BoundsForWordAt(5);
- engine()->SetSelection(start_idx, end_idx);
+ std::tie(start_idx, count) = engine()->BoundsForWordAt(5);
+ engine()->SetSelection(start_idx, count);
EXPECT_STREQ(L"abc456ghi", engine()->GetSelectedText().c_str());
engine()->Clear();
engine()->Insert(0, L"hello, world");
- std::tie(start_idx, end_idx) = engine()->BoundsForWordAt(0);
- engine()->SetSelection(start_idx, end_idx);
+ std::tie(start_idx, count) = engine()->BoundsForWordAt(0);
+ engine()->SetSelection(start_idx, count);
EXPECT_STREQ(L"hello", engine()->GetSelectedText().c_str());
engine()->Clear();
engine()->Insert(0, L"hello, world");
- std::tie(start_idx, end_idx) = engine()->BoundsForWordAt(5);
- engine()->SetSelection(start_idx, end_idx);
- EXPECT_STREQ(L"", engine()->GetSelectedText().c_str());
+ std::tie(start_idx, count) = engine()->BoundsForWordAt(5);
+ engine()->SetSelection(start_idx, count);
+ EXPECT_STREQ(L",", engine()->GetSelectedText().c_str());
engine()->Clear();
engine()->Insert(0, L"np-complete");
- std::tie(start_idx, end_idx) = engine()->BoundsForWordAt(6);
- engine()->SetSelection(start_idx, end_idx);
+ std::tie(start_idx, count) = engine()->BoundsForWordAt(6);
+ engine()->SetSelection(start_idx, count);
EXPECT_STREQ(L"complete", engine()->GetSelectedText().c_str());
engine()->Clear();
engine()->Insert(0, L"(123) 456-7890");
- std::tie(start_idx, end_idx) = engine()->BoundsForWordAt(0);
- engine()->SetSelection(start_idx, end_idx);
- EXPECT_STREQ(L"", engine()->GetSelectedText().c_str());
+ std::tie(start_idx, count) = engine()->BoundsForWordAt(0);
+ engine()->SetSelection(start_idx, count);
+ EXPECT_STREQ(L"(", engine()->GetSelectedText().c_str());
- std::tie(start_idx, end_idx) = engine()->BoundsForWordAt(1);
- engine()->SetSelection(start_idx, end_idx);
+ std::tie(start_idx, count) = engine()->BoundsForWordAt(1);
+ engine()->SetSelection(start_idx, count);
EXPECT_STREQ(L"123", engine()->GetSelectedText().c_str());
- std::tie(start_idx, end_idx) = engine()->BoundsForWordAt(7);
- engine()->SetSelection(start_idx, end_idx);
+ std::tie(start_idx, count) = engine()->BoundsForWordAt(7);
+ engine()->SetSelection(start_idx, count);
EXPECT_STREQ(L"456", engine()->GetSelectedText().c_str());
- std::tie(start_idx, end_idx) = engine()->BoundsForWordAt(11);
- engine()->SetSelection(start_idx, end_idx);
+ std::tie(start_idx, count) = engine()->BoundsForWordAt(11);
+ engine()->SetSelection(start_idx, count);
EXPECT_STREQ(L"7890", engine()->GetSelectedText().c_str());
+
+ // Tests from:
+ // http://unicode.org/Public/UNIDATA/auxiliary/WordBreakTest.html#samples
+ struct bounds {
+ size_t start;
+ size_t end;
+ };
+ struct {
+ const wchar_t* str;
+ std::vector<const wchar_t*> results;
+ } tests[] = {
+ // {L"\r\na\n\u0308", {L"\r\n", L"a", L"\n", L"\u0308"}},
+ // {L"a\u0308", {L"a\u0308"}},
+ // {L" \u200d\u0646", {L" \u200d", L"\u0646"}},
+ // {L"\u0646\u200d ", {L"\u0646\u200d", L" "}},
+ {L"AAA", {L"AAA"}},
+ {L"A:A", {L"A:A"}},
+ {L"A::A", {L"A", L":", L":", L"A"}},
+ // {L"\u05d0'", {L"\u05d0'"}},
+ // {L"\u05d0\"\u05d0", {L"\u05d0\"\u05d0"}},
+ {L"A00A", {L"A00A"}},
+ {L"0,0", {L"0,0"}},
+ {L"0,,0", {L"0", L",", L",", L"0"}},
+ {L"\u3031\u3031", {L"\u3031\u3031"}},
+ {L"A_0_\u3031_", {L"A_0_\u3031_"}},
+ {L"A__A", {L"A__A"}},
+ // {L"\u200d\u2640", {L"\u200d\u2640"}},
+ // {L"a\u0308\u200b\u0308b", {L"a\u0308\u200b\u0308b"}},
+ };
+
+ for (auto t : tests) {
+ engine()->Clear();
+ engine()->Insert(0, t.str);
+
+ size_t idx = 0;
+ for (const auto* res : t.results) {
+ std::tie(start_idx, count) = engine()->BoundsForWordAt(idx);
+ engine()->SetSelection(start_idx, count);
+ EXPECT_STREQ(res, engine()->GetSelectedText().c_str())
+ << "Input: '" << t.str << "'";
+ idx += count;
+ }
+ }
}