Commit cb5827d8 authored by xji@google.com's avatar xji@google.com

Remove PREVIOUS_GRAPHEME_TRAILING.

Remove const from GetIndexOfPreviousGrapheme() so the override function could
update local data.
Construct SelectionModel correctly in unittest.

BUG=90426
TEST=--use-pure-view.
Review URL: http://codereview.chromium.org/7607018

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@97029 0039d316-1c4b-4281-b951-d872f2087c98
parent cc033374
......@@ -87,11 +87,15 @@ StyleRange::StyleRange()
}
SelectionModel::SelectionModel() {
Init(0, 0, 0, PREVIOUS_GRAPHEME_TRAILING);
Init(0, 0, 0, LEADING);
}
SelectionModel::SelectionModel(size_t pos) {
Init(pos, pos, pos, PREVIOUS_GRAPHEME_TRAILING);
Init(pos, pos, pos, LEADING);
}
SelectionModel::SelectionModel(size_t start, size_t end) {
Init(start, end, end, LEADING);
}
SelectionModel::SelectionModel(size_t end,
......@@ -266,6 +270,8 @@ void RenderText::SelectAll() {
SetSelectionModel(sel);
}
// TODO(xji): it does not work for languages do not use space as word breaker,
// such as Chinese. Should use BreakIterator.
void RenderText::SelectWord() {
size_t selection_start = GetSelectionStart();
size_t cursor_position = GetCursorPosition();
......@@ -305,7 +311,8 @@ void RenderText::SelectWord() {
SelectionModel sel(selection_model());
sel.set_selection_start(selection_start);
sel.set_selection_end(cursor_position);
sel.set_caret_placement(SelectionModel::PREVIOUS_GRAPHEME_TRAILING);
sel.set_caret_pos(GetIndexOfPreviousGrapheme(cursor_position));
sel.set_caret_placement(SelectionModel::TRAILING);
SetSelectionModel(sel);
}
......@@ -542,7 +549,7 @@ SelectionModel RenderText::GetRightSelectionModel(const SelectionModel& current,
return SelectionModel(pos, pos, SelectionModel::LEADING);
}
size_t RenderText::GetIndexOfPreviousGrapheme(size_t position) const {
size_t RenderText::GetIndexOfPreviousGrapheme(size_t position) {
// TODO(msw): Handle complex script.
return std::max(static_cast<int>(position - 1), static_cast<int>(0));
}
......
......@@ -90,15 +90,13 @@ enum BreakType {
class UI_EXPORT SelectionModel {
public:
enum CaretPlacement {
// PREVIOUS_GRAPHEME_TRAILING means cursor is visually attached to the
// trailing edge of previous grapheme.
PREVIOUS_GRAPHEME_TRAILING,
LEADING,
TRAILING,
};
SelectionModel();
explicit SelectionModel(size_t pos);
SelectionModel(size_t start, size_t end);
SelectionModel(size_t end, size_t pos, CaretPlacement status);
SelectionModel(size_t start, size_t end, size_t pos, CaretPlacement status);
......@@ -277,7 +275,7 @@ class UI_EXPORT RenderText {
BreakType break_type);
// Get the logical index of the grapheme preceeding the argument |position|.
virtual size_t GetIndexOfPreviousGrapheme(size_t position) const;
virtual size_t GetIndexOfPreviousGrapheme(size_t position);
// Apply composition style (underline) to composition range and selection
// style (foreground) to selection range.
......
......@@ -414,8 +414,7 @@ void TextfieldViewsModel::GetSelectedRange(ui::Range* range) const {
}
void TextfieldViewsModel::SelectRange(const ui::Range& range) {
gfx::SelectionModel selection(range.start(), range.end(),
range.end(), gfx::SelectionModel::PREVIOUS_GRAPHEME_TRAILING);
gfx::SelectionModel selection(range.start(), range.end());
SelectSelectionModel(selection);
}
......@@ -589,8 +588,7 @@ void TextfieldViewsModel::SetCompositionText(
std::min(range.start() + composition.selection.start(), range.end());
size_t end =
std::min(range.start() + composition.selection.end(), range.end());
gfx::SelectionModel sel(start, end, end,
gfx::SelectionModel::PREVIOUS_GRAPHEME_TRAILING);
gfx::SelectionModel sel(start, end);
render_text_->SetSelectionModel(sel);
} else {
render_text_->SetCursorPosition(range.end());
......
......@@ -126,18 +126,11 @@ TEST_F(TextfieldViewsModelTest, Selection) {
EXPECT_EQ(5U, range.end());
// Select and move cursor
gfx::SelectionModel selection(1U);
model.MoveCursorTo(selection);
selection.set_selection_end(3U);
model.MoveCursorTo(selection);
model.MoveCursorTo(gfx::SelectionModel(1U, 3U));
EXPECT_STR_EQ("EL", model.GetSelectedText());
model.MoveCursorLeft(gfx::CHARACTER_BREAK, false);
EXPECT_EQ(1U, model.GetCursorPosition());
selection.set_selection_end(1U);
selection.set_selection_start(selection.selection_end());
model.MoveCursorTo(selection);
selection.set_selection_end(3U);
model.MoveCursorTo(selection);
model.MoveCursorTo(gfx::SelectionModel(1U, 3U));
model.MoveCursorRight(gfx::CHARACTER_BREAK, false);
EXPECT_EQ(3U, model.GetCursorPosition());
......@@ -342,24 +335,21 @@ TEST_F(TextfieldViewsModelTest, SelectWordTest) {
SelectWordTestVerifier(model, "HELLO", 7U);
// Test when cursor is at the end of a word.
selection.set_selection_end(15U);
selection.set_selection_start(selection.selection_end());
selection = gfx::SelectionModel(15U);
model.MoveCursorTo(selection);
model.SelectWord();
SelectWordTestVerifier(model, "WO", 15U);
// Test when cursor is somewhere in a non-alph-numeric fragment.
for (size_t cursor_pos = 8; cursor_pos < 13U; cursor_pos++) {
selection.set_selection_end(cursor_pos);
selection.set_selection_start(selection.selection_end());
selection = gfx::SelectionModel(cursor_pos);
model.MoveCursorTo(selection);
model.SelectWord();
SelectWordTestVerifier(model, " !! ", 13U);
}
// Test when cursor is somewhere in a whitespace fragment.
selection.set_selection_end(17U);
selection.set_selection_start(selection.selection_end());
selection = gfx::SelectionModel(17U);
model.MoveCursorTo(selection);
model.SelectWord();
SelectWordTestVerifier(model, " ", 20U);
......@@ -809,10 +799,7 @@ TEST_F(TextfieldViewsModelTest, UndoRedo_CutCopyPasteTest) {
model.SetText(ASCIIToUTF16("ABCDE"));
EXPECT_FALSE(model.Redo()); // nothing to redo
// Cut
gfx::SelectionModel sel(1);
model.MoveCursorTo(sel);
sel.set_selection_end(3);
model.MoveCursorTo(sel);
model.MoveCursorTo(gfx::SelectionModel(1, 3));
model.Cut();
EXPECT_STR_EQ("ADE", model.GetText());
EXPECT_EQ(1U, model.GetCursorPosition());
......@@ -898,11 +885,7 @@ TEST_F(TextfieldViewsModelTest, UndoRedo_CutCopyPasteTest) {
model.SetText(ASCIIToUTF16("12345"));
EXPECT_STR_EQ("12345", model.GetText());
EXPECT_EQ(0U, model.GetCursorPosition());
sel.set_selection_end(1);
sel.set_selection_start(sel.selection_end());
model.MoveCursorTo(sel);
sel.set_selection_end(3);
model.MoveCursorTo(sel);
model.MoveCursorTo(gfx::SelectionModel(1, 3));
model.Copy(); // Copy "23"
EXPECT_STR_EQ("12345", model.GetText());
EXPECT_EQ(3U, model.GetCursorPosition());
......@@ -1026,40 +1009,28 @@ TEST_F(TextfieldViewsModelTest, UndoRedo_ReplaceTest) {
SCOPED_TRACE("forward & insert by cursor");
TextfieldViewsModel model(NULL);
model.SetText(ASCIIToUTF16("abcd"));
gfx::SelectionModel sel(1);
model.MoveCursorTo(sel);
sel.set_selection_end(3);
model.MoveCursorTo(sel);
model.MoveCursorTo(gfx::SelectionModel(1, 3));
RunInsertReplaceTest(model);
}
{
SCOPED_TRACE("backward & insert by cursor");
TextfieldViewsModel model(NULL);
model.SetText(ASCIIToUTF16("abcd"));
gfx::SelectionModel sel(3);
model.MoveCursorTo(sel);
sel.set_selection_end(1);
model.MoveCursorTo(sel);
model.MoveCursorTo(gfx::SelectionModel(3, 1));
RunInsertReplaceTest(model);
}
{
SCOPED_TRACE("forward & overwrite by cursor");
TextfieldViewsModel model(NULL);
model.SetText(ASCIIToUTF16("abcd"));
gfx::SelectionModel sel(1);
model.MoveCursorTo(sel);
sel.set_selection_end(3);
model.MoveCursorTo(sel);
model.MoveCursorTo(gfx::SelectionModel(1, 3));
RunOverwriteReplaceTest(model);
}
{
SCOPED_TRACE("backward & overwrite by cursor");
TextfieldViewsModel model(NULL);
model.SetText(ASCIIToUTF16("abcd"));
gfx::SelectionModel sel(3);
model.MoveCursorTo(sel);
sel.set_selection_end(1);
model.MoveCursorTo(sel);
model.MoveCursorTo(gfx::SelectionModel(3, 1));
RunOverwriteReplaceTest(model);
}
// By SelectRange API
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment