ISSUE-20455: introduce stronger types for textinput indexing

This commit is contained in:
Thomas Delacour 2019-04-13 14:35:49 -04:00
parent ce93e017c6
commit 14c8bbb49d
No known key found for this signature in database
GPG key ID: 690B2D136C0C1576
5 changed files with 570 additions and 324 deletions

View file

@ -10,7 +10,10 @@
use keyboard_types::{Key, Modifiers};
use script::clipboard_provider::DummyClipboardContext;
use script::test::DOMString;
use script::textinput::{Direction, Lines, Selection, SelectionDirection, TextInput, TextPoint};
use script::textinput::{
Direction, Lines, Selection, SelectionDirection, TextInput, TextPoint, UTF16CodeUnits,
UTF8Bytes,
};
fn text_input(lines: Lines, s: &str) -> TextInput<DummyClipboardContext> {
TextInput::new(
@ -29,7 +32,7 @@ fn test_set_content_ignores_max_length() {
Lines::Single,
DOMString::from(""),
DummyClipboardContext::new(""),
Some(1),
Some(UTF16CodeUnits::one()),
None,
SelectionDirection::None,
);
@ -44,13 +47,13 @@ fn test_textinput_when_inserting_multiple_lines_over_a_selection_respects_max_le
Lines::Multiple,
DOMString::from("hello\nworld"),
DummyClipboardContext::new(""),
Some(17),
Some(UTF16CodeUnits(17)),
None,
SelectionDirection::None,
);
textinput.adjust_horizontal(1, Selection::NotSelected);
textinput.adjust_horizontal(3, Selection::Selected);
textinput.adjust_horizontal(UTF8Bytes::one(), Direction::Forward, Selection::NotSelected);
textinput.adjust_horizontal(UTF8Bytes(3), Direction::Forward, Selection::Selected);
textinput.adjust_vertical(1, Selection::Selected);
// Selection is now "hello\n
@ -69,7 +72,7 @@ fn test_textinput_when_inserting_multiple_lines_still_respects_max_length() {
Lines::Multiple,
DOMString::from("hello\nworld"),
DummyClipboardContext::new(""),
Some(17),
Some(UTF16CodeUnits(17)),
None,
SelectionDirection::None,
);
@ -87,7 +90,7 @@ fn test_textinput_when_content_is_already_longer_than_max_length_and_theres_no_s
Lines::Single,
DOMString::from("abc"),
DummyClipboardContext::new(""),
Some(1),
Some(UTF16CodeUnits::one()),
None,
SelectionDirection::None,
);
@ -104,7 +107,7 @@ fn test_multi_line_textinput_with_maxlength_doesnt_allow_appending_characters_wh
Lines::Multiple,
DOMString::from("abc\nd"),
DummyClipboardContext::new(""),
Some(5),
Some(UTF16CodeUnits(5)),
None,
SelectionDirection::None,
);
@ -121,13 +124,13 @@ fn test_single_line_textinput_with_max_length_doesnt_allow_appending_characters_
Lines::Single,
DOMString::from("abcde"),
DummyClipboardContext::new(""),
Some(5),
Some(UTF16CodeUnits(5)),
None,
SelectionDirection::None,
);
textinput.adjust_horizontal(1, Selection::NotSelected);
textinput.adjust_horizontal(3, Selection::Selected);
textinput.adjust_horizontal(UTF8Bytes::one(), Direction::Forward, Selection::NotSelected);
textinput.adjust_horizontal(UTF8Bytes(3), Direction::Forward, Selection::Selected);
// Selection is now "abcde"
// ---
@ -143,7 +146,7 @@ fn test_single_line_textinput_with_max_length_multibyte() {
Lines::Single,
DOMString::from(""),
DummyClipboardContext::new(""),
Some(2),
Some(UTF16CodeUnits(2)),
None,
SelectionDirection::None,
);
@ -162,7 +165,7 @@ fn test_single_line_textinput_with_max_length_multi_code_unit() {
Lines::Single,
DOMString::from(""),
DummyClipboardContext::new(""),
Some(3),
Some(UTF16CodeUnits(3)),
None,
SelectionDirection::None,
);
@ -183,7 +186,7 @@ fn test_single_line_textinput_with_max_length_inside_char() {
Lines::Single,
DOMString::from("\u{10437}"),
DummyClipboardContext::new(""),
Some(1),
Some(UTF16CodeUnits::one()),
None,
SelectionDirection::None,
);
@ -199,7 +202,7 @@ fn test_single_line_textinput_with_max_length_doesnt_allow_appending_characters_
Lines::Single,
DOMString::from("a"),
DummyClipboardContext::new(""),
Some(1),
Some(UTF16CodeUnits::one()),
None,
SelectionDirection::None,
);
@ -211,14 +214,14 @@ fn test_single_line_textinput_with_max_length_doesnt_allow_appending_characters_
#[test]
fn test_textinput_delete_char() {
let mut textinput = text_input(Lines::Single, "abcdefg");
textinput.adjust_horizontal(2, Selection::NotSelected);
textinput.adjust_horizontal(UTF8Bytes(2), Direction::Forward, Selection::NotSelected);
textinput.delete_char(Direction::Backward);
assert_eq!(textinput.get_content(), "acdefg");
textinput.delete_char(Direction::Forward);
assert_eq!(textinput.get_content(), "adefg");
textinput.adjust_horizontal(2, Selection::Selected);
textinput.adjust_horizontal(UTF8Bytes(2), Direction::Forward, Selection::Selected);
textinput.delete_char(Direction::Forward);
assert_eq!(textinput.get_content(), "afg");
@ -238,11 +241,11 @@ fn test_textinput_delete_char() {
#[test]
fn test_textinput_insert_char() {
let mut textinput = text_input(Lines::Single, "abcdefg");
textinput.adjust_horizontal(2, Selection::NotSelected);
textinput.adjust_horizontal(UTF8Bytes(2), Direction::Forward, Selection::NotSelected);
textinput.insert_char('a');
assert_eq!(textinput.get_content(), "abacdefg");
textinput.adjust_horizontal(2, Selection::Selected);
textinput.adjust_horizontal(UTF8Bytes(2), Direction::Forward, Selection::Selected);
textinput.insert_char('b');
assert_eq!(textinput.get_content(), "ababefg");
@ -258,25 +261,25 @@ fn test_textinput_insert_char() {
#[test]
fn test_textinput_get_sorted_selection() {
let mut textinput = text_input(Lines::Single, "abcdefg");
textinput.adjust_horizontal(2, Selection::NotSelected);
textinput.adjust_horizontal(2, Selection::Selected);
textinput.adjust_horizontal(UTF8Bytes(2), Direction::Forward, Selection::NotSelected);
textinput.adjust_horizontal(UTF8Bytes(2), Direction::Forward, Selection::Selected);
let (start, end) = textinput.sorted_selection_bounds();
assert_eq!(start.index, 2);
assert_eq!(end.index, 4);
assert_eq!(start.index, UTF8Bytes(2));
assert_eq!(end.index, UTF8Bytes(4));
textinput.clear_selection();
textinput.adjust_horizontal(-2, Selection::Selected);
textinput.adjust_horizontal(UTF8Bytes(2), Direction::Backward, Selection::Selected);
let (start, end) = textinput.sorted_selection_bounds();
assert_eq!(start.index, 2);
assert_eq!(end.index, 4);
assert_eq!(start.index, UTF8Bytes(2));
assert_eq!(end.index, UTF8Bytes(4));
}
#[test]
fn test_textinput_replace_selection() {
let mut textinput = text_input(Lines::Single, "abcdefg");
textinput.adjust_horizontal(2, Selection::NotSelected);
textinput.adjust_horizontal(2, Selection::Selected);
textinput.adjust_horizontal(UTF8Bytes(2), Direction::Forward, Selection::NotSelected);
textinput.adjust_horizontal(UTF8Bytes(2), Direction::Forward, Selection::Selected);
textinput.replace_selection(DOMString::from("xyz"));
assert_eq!(textinput.get_content(), "abxyzefg");
@ -294,34 +297,34 @@ fn test_textinput_replace_selection_multibyte_char() {
#[test]
fn test_textinput_current_line_length() {
let mut textinput = text_input(Lines::Multiple, "abc\nde\nf");
assert_eq!(textinput.current_line_length(), 3);
assert_eq!(textinput.current_line_length(), UTF8Bytes(3));
textinput.adjust_vertical(1, Selection::NotSelected);
assert_eq!(textinput.current_line_length(), 2);
assert_eq!(textinput.current_line_length(), UTF8Bytes(2));
textinput.adjust_vertical(1, Selection::NotSelected);
assert_eq!(textinput.current_line_length(), 1);
assert_eq!(textinput.current_line_length(), UTF8Bytes::one());
}
#[test]
fn test_textinput_adjust_vertical() {
let mut textinput = text_input(Lines::Multiple, "abc\nde\nf");
textinput.adjust_horizontal(3, Selection::NotSelected);
textinput.adjust_horizontal(UTF8Bytes(3), Direction::Forward, Selection::NotSelected);
textinput.adjust_vertical(1, Selection::NotSelected);
assert_eq!(textinput.edit_point().line, 1);
assert_eq!(textinput.edit_point().index, 2);
assert_eq!(textinput.edit_point().index, UTF8Bytes(2));
textinput.adjust_vertical(-1, Selection::NotSelected);
assert_eq!(textinput.edit_point().line, 0);
assert_eq!(textinput.edit_point().index, 2);
assert_eq!(textinput.edit_point().index, UTF8Bytes(2));
textinput.adjust_vertical(2, Selection::NotSelected);
assert_eq!(textinput.edit_point().line, 2);
assert_eq!(textinput.edit_point().index, 1);
assert_eq!(textinput.edit_point().index, UTF8Bytes(1));
textinput.adjust_vertical(-1, Selection::Selected);
assert_eq!(textinput.edit_point().line, 1);
assert_eq!(textinput.edit_point().index, 1);
assert_eq!(textinput.edit_point().index, UTF8Bytes(1));
}
#[test]
@ -330,31 +333,35 @@ fn test_textinput_adjust_vertical_multibyte() {
textinput.adjust_horizontal_by_one(Direction::Forward, Selection::NotSelected);
assert_eq!(textinput.edit_point().line, 0);
assert_eq!(textinput.edit_point().index, 2);
assert_eq!(textinput.edit_point().index, UTF8Bytes(2));
textinput.adjust_vertical(1, Selection::NotSelected);
assert_eq!(textinput.edit_point().line, 1);
assert_eq!(textinput.edit_point().index, 1);
assert_eq!(textinput.edit_point().index, UTF8Bytes(1));
}
#[test]
fn test_textinput_adjust_horizontal() {
let mut textinput = text_input(Lines::Multiple, "abc\nde\nf");
textinput.adjust_horizontal(4, Selection::NotSelected);
textinput.adjust_horizontal(UTF8Bytes(4), Direction::Forward, Selection::NotSelected);
assert_eq!(textinput.edit_point().line, 1);
assert_eq!(textinput.edit_point().index, 0);
assert_eq!(textinput.edit_point().index, UTF8Bytes::zero());
textinput.adjust_horizontal(1, Selection::NotSelected);
textinput.adjust_horizontal(UTF8Bytes::one(), Direction::Forward, Selection::NotSelected);
assert_eq!(textinput.edit_point().line, 1);
assert_eq!(textinput.edit_point().index, 1);
assert_eq!(textinput.edit_point().index, UTF8Bytes(1));
textinput.adjust_horizontal(2, Selection::NotSelected);
textinput.adjust_horizontal(UTF8Bytes(2), Direction::Forward, Selection::NotSelected);
assert_eq!(textinput.edit_point().line, 2);
assert_eq!(textinput.edit_point().index, 0);
assert_eq!(textinput.edit_point().index, UTF8Bytes::zero());
textinput.adjust_horizontal(-1, Selection::NotSelected);
textinput.adjust_horizontal(
UTF8Bytes::one(),
Direction::Backward,
Selection::NotSelected,
);
assert_eq!(textinput.edit_point().line, 1);
assert_eq!(textinput.edit_point().index, 2);
assert_eq!(textinput.edit_point().index, UTF8Bytes(2));
}
#[test]
@ -364,44 +371,44 @@ fn test_textinput_adjust_horizontal_by_word() {
textinput.adjust_horizontal_by_word(Direction::Forward, Selection::NotSelected);
textinput.adjust_horizontal_by_word(Direction::Forward, Selection::NotSelected);
assert_eq!(textinput.edit_point().line, 0);
assert_eq!(textinput.edit_point().index, 7);
assert_eq!(textinput.edit_point().index, UTF8Bytes(7));
textinput.adjust_horizontal_by_word(Direction::Backward, Selection::NotSelected);
assert_eq!(textinput.edit_point().line, 0);
assert_eq!(textinput.edit_point().index, 4);
assert_eq!(textinput.edit_point().index, UTF8Bytes(4));
textinput.adjust_horizontal_by_word(Direction::Backward, Selection::NotSelected);
assert_eq!(textinput.edit_point().line, 0);
assert_eq!(textinput.edit_point().index, 0);
assert_eq!(textinput.edit_point().index, UTF8Bytes::zero());
// Test new line case of movement word by word based on UAX#29 rules
let mut textinput_2 = text_input(Lines::Multiple, "abc\ndef");
textinput_2.adjust_horizontal_by_word(Direction::Forward, Selection::NotSelected);
textinput_2.adjust_horizontal_by_word(Direction::Forward, Selection::NotSelected);
assert_eq!(textinput_2.edit_point().line, 1);
assert_eq!(textinput_2.edit_point().index, 3);
assert_eq!(textinput_2.edit_point().index, UTF8Bytes(3));
textinput_2.adjust_horizontal_by_word(Direction::Backward, Selection::NotSelected);
assert_eq!(textinput_2.edit_point().line, 1);
assert_eq!(textinput_2.edit_point().index, 0);
assert_eq!(textinput_2.edit_point().index, UTF8Bytes::zero());
textinput_2.adjust_horizontal_by_word(Direction::Backward, Selection::NotSelected);
assert_eq!(textinput_2.edit_point().line, 0);
assert_eq!(textinput_2.edit_point().index, 0);
assert_eq!(textinput_2.edit_point().index, UTF8Bytes::zero());
// Test non-standard sized characters case of movement word by word based on UAX#29 rules
let mut textinput_3 = text_input(Lines::Single, "áéc d🌠bc");
textinput_3.adjust_horizontal_by_word(Direction::Forward, Selection::NotSelected);
assert_eq!(textinput_3.edit_point().line, 0);
assert_eq!(textinput_3.edit_point().index, 5);
assert_eq!(textinput_3.edit_point().index, UTF8Bytes(5));
textinput_3.adjust_horizontal_by_word(Direction::Forward, Selection::NotSelected);
assert_eq!(textinput_3.edit_point().line, 0);
assert_eq!(textinput_3.edit_point().index, 7);
assert_eq!(textinput_3.edit_point().index, UTF8Bytes(7));
textinput_3.adjust_horizontal_by_word(Direction::Forward, Selection::NotSelected);
assert_eq!(textinput_3.edit_point().line, 0);
assert_eq!(textinput_3.edit_point().index, 13);
assert_eq!(textinput_3.edit_point().index, UTF8Bytes(13));
textinput_3.adjust_horizontal_by_word(Direction::Backward, Selection::NotSelected);
assert_eq!(textinput_3.edit_point().line, 0);
assert_eq!(textinput_3.edit_point().index, 11);
assert_eq!(textinput_3.edit_point().index, UTF8Bytes(11));
textinput_3.adjust_horizontal_by_word(Direction::Backward, Selection::NotSelected);
assert_eq!(textinput_3.edit_point().line, 0);
assert_eq!(textinput_3.edit_point().index, 6);
assert_eq!(textinput_3.edit_point().index, UTF8Bytes(6));
}
#[test]
@ -410,28 +417,28 @@ fn test_textinput_adjust_horizontal_to_line_end() {
let mut textinput = text_input(Lines::Single, "abc def");
textinput.adjust_horizontal_to_line_end(Direction::Forward, Selection::NotSelected);
assert_eq!(textinput.edit_point().line, 0);
assert_eq!(textinput.edit_point().index, 7);
assert_eq!(textinput.edit_point().index, UTF8Bytes(7));
// Test new line case of movement to end based on UAX#29 rules
let mut textinput_2 = text_input(Lines::Multiple, "abc\ndef");
textinput_2.adjust_horizontal_to_line_end(Direction::Forward, Selection::NotSelected);
assert_eq!(textinput_2.edit_point().line, 0);
assert_eq!(textinput_2.edit_point().index, 3);
assert_eq!(textinput_2.edit_point().index, UTF8Bytes(3));
textinput_2.adjust_horizontal_to_line_end(Direction::Forward, Selection::NotSelected);
assert_eq!(textinput_2.edit_point().line, 0);
assert_eq!(textinput_2.edit_point().index, 3);
assert_eq!(textinput_2.edit_point().index, UTF8Bytes(3));
textinput_2.adjust_horizontal_to_line_end(Direction::Backward, Selection::NotSelected);
assert_eq!(textinput_2.edit_point().line, 0);
assert_eq!(textinput_2.edit_point().index, 0);
assert_eq!(textinput_2.edit_point().index, UTF8Bytes::zero());
// Test non-standard sized characters case of movement to end based on UAX#29 rules
let mut textinput_3 = text_input(Lines::Single, "áéc d🌠bc");
textinput_3.adjust_horizontal_to_line_end(Direction::Forward, Selection::NotSelected);
assert_eq!(textinput_3.edit_point().line, 0);
assert_eq!(textinput_3.edit_point().index, 13);
assert_eq!(textinput_3.edit_point().index, UTF8Bytes(13));
textinput_3.adjust_horizontal_to_line_end(Direction::Backward, Selection::NotSelected);
assert_eq!(textinput_3.edit_point().line, 0);
assert_eq!(textinput_3.edit_point().index, 0);
assert_eq!(textinput_3.edit_point().index, UTF8Bytes::zero());
}
#[test]
@ -440,56 +447,64 @@ fn test_navigation_keyboard_shortcuts() {
// Test that CMD + Right moves to the end of the current line.
textinput.handle_keydown_aux(Key::ArrowRight, Modifiers::META, true);
assert_eq!(textinput.edit_point().index, 11);
assert_eq!(textinput.edit_point().index, UTF8Bytes(11));
// Test that CMD + Right moves to the beginning of the current line.
textinput.handle_keydown_aux(Key::ArrowLeft, Modifiers::META, true);
assert_eq!(textinput.edit_point().index, 0);
assert_eq!(textinput.edit_point().index, UTF8Bytes::zero());
// Test that CTRL + ALT + E moves to the end of the current line also.
textinput.handle_keydown_aux(
Key::Character("e".to_owned()),
Modifiers::CONTROL | Modifiers::ALT,
true,
);
assert_eq!(textinput.edit_point().index, 11);
assert_eq!(textinput.edit_point().index, UTF8Bytes(11));
// Test that CTRL + ALT + A moves to the beginning of the current line also.
textinput.handle_keydown_aux(
Key::Character("a".to_owned()),
Modifiers::CONTROL | Modifiers::ALT,
true,
);
assert_eq!(textinput.edit_point().index, 0);
assert_eq!(textinput.edit_point().index, UTF8Bytes::zero());
// Test that ALT + Right moves to the end of the word.
textinput.handle_keydown_aux(Key::ArrowRight, Modifiers::ALT, true);
assert_eq!(textinput.edit_point().index, 5);
assert_eq!(textinput.edit_point().index, UTF8Bytes(5));
// Test that CTRL + ALT + F moves to the end of the word also.
textinput.handle_keydown_aux(
Key::Character("f".to_owned()),
Modifiers::CONTROL | Modifiers::ALT,
true,
);
assert_eq!(textinput.edit_point().index, 11);
assert_eq!(textinput.edit_point().index, UTF8Bytes(11));
// Test that ALT + Left moves to the end of the word.
textinput.handle_keydown_aux(Key::ArrowLeft, Modifiers::ALT, true);
assert_eq!(textinput.edit_point().index, 6);
assert_eq!(textinput.edit_point().index, UTF8Bytes(6));
// Test that CTRL + ALT + B moves to the end of the word also.
textinput.handle_keydown_aux(
Key::Character("b".to_owned()),
Modifiers::CONTROL | Modifiers::ALT,
true,
);
assert_eq!(textinput.edit_point().index, 0);
assert_eq!(textinput.edit_point().index, UTF8Bytes::zero());
}
#[test]
fn test_textinput_handle_return() {
let mut single_line_textinput = text_input(Lines::Single, "abcdef");
single_line_textinput.adjust_horizontal(3, Selection::NotSelected);
single_line_textinput.adjust_horizontal(
UTF8Bytes(3),
Direction::Forward,
Selection::NotSelected,
);
single_line_textinput.handle_return();
assert_eq!(single_line_textinput.get_content(), "abcdef");
let mut multi_line_textinput = text_input(Lines::Multiple, "abcdef");
multi_line_textinput.adjust_horizontal(3, Selection::NotSelected);
multi_line_textinput.adjust_horizontal(
UTF8Bytes(3),
Direction::Forward,
Selection::NotSelected,
);
multi_line_textinput.handle_return();
assert_eq!(multi_line_textinput.get_content(), "abc\ndef");
}
@ -498,11 +513,11 @@ fn test_textinput_handle_return() {
fn test_textinput_select_all() {
let mut textinput = text_input(Lines::Multiple, "abc\nde\nf");
assert_eq!(textinput.edit_point().line, 0);
assert_eq!(textinput.edit_point().index, 0);
assert_eq!(textinput.edit_point().index, UTF8Bytes::zero());
textinput.select_all();
assert_eq!(textinput.edit_point().line, 2);
assert_eq!(textinput.edit_point().index, 1);
assert_eq!(textinput.edit_point().index, UTF8Bytes(1));
}
#[test]
@ -523,15 +538,15 @@ fn test_textinput_set_content() {
assert_eq!(textinput.get_content(), "abc\nf");
assert_eq!(textinput.edit_point().line, 0);
assert_eq!(textinput.edit_point().index, 0);
assert_eq!(textinput.edit_point().index, UTF8Bytes::zero());
textinput.adjust_horizontal(3, Selection::Selected);
textinput.adjust_horizontal(UTF8Bytes(3), Direction::Forward, Selection::Selected);
assert_eq!(textinput.edit_point().line, 0);
assert_eq!(textinput.edit_point().index, 3);
assert_eq!(textinput.edit_point().index, UTF8Bytes(3));
textinput.set_content(DOMString::from("de"));
assert_eq!(textinput.get_content(), "de");
assert_eq!(textinput.edit_point().line, 0);
assert_eq!(textinput.edit_point().index, 2);
assert_eq!(textinput.edit_point().index, UTF8Bytes(2));
}
#[test]
@ -550,7 +565,7 @@ fn test_clipboard_paste() {
SelectionDirection::None,
);
assert_eq!(textinput.get_content(), "defg");
assert_eq!(textinput.edit_point().index, 0);
assert_eq!(textinput.edit_point().index, UTF8Bytes::zero());
textinput.handle_keydown_aux(Key::Character("v".to_owned()), MODIFIERS, false);
assert_eq!(textinput.get_content(), "abcdefg");
}
@ -560,51 +575,59 @@ fn test_textinput_cursor_position_correct_after_clearing_selection() {
let mut textinput = text_input(Lines::Single, "abcdef");
// Single line - Forward
textinput.adjust_horizontal(3, Selection::Selected);
textinput.adjust_horizontal(1, Selection::NotSelected);
assert_eq!(textinput.edit_point().index, 3);
textinput.adjust_horizontal(UTF8Bytes(3), Direction::Forward, Selection::Selected);
textinput.adjust_horizontal(UTF8Bytes::one(), Direction::Forward, Selection::NotSelected);
assert_eq!(textinput.edit_point().index, UTF8Bytes(3));
textinput.adjust_horizontal(-3, Selection::NotSelected);
textinput.adjust_horizontal(3, Selection::Selected);
textinput.adjust_horizontal(UTF8Bytes(3), Direction::Backward, Selection::NotSelected);
textinput.adjust_horizontal(UTF8Bytes(3), Direction::Forward, Selection::Selected);
textinput.adjust_horizontal_by_one(Direction::Forward, Selection::NotSelected);
assert_eq!(textinput.edit_point().index, 3);
assert_eq!(textinput.edit_point().index, UTF8Bytes(3));
// Single line - Backward
textinput.adjust_horizontal(-3, Selection::NotSelected);
textinput.adjust_horizontal(3, Selection::Selected);
textinput.adjust_horizontal(-1, Selection::NotSelected);
assert_eq!(textinput.edit_point().index, 0);
textinput.adjust_horizontal(UTF8Bytes(3), Direction::Backward, Selection::NotSelected);
textinput.adjust_horizontal(UTF8Bytes(3), Direction::Forward, Selection::Selected);
textinput.adjust_horizontal(
UTF8Bytes::one(),
Direction::Backward,
Selection::NotSelected,
);
assert_eq!(textinput.edit_point().index, UTF8Bytes::zero());
textinput.adjust_horizontal(-3, Selection::NotSelected);
textinput.adjust_horizontal(3, Selection::Selected);
textinput.adjust_horizontal(UTF8Bytes(3), Direction::Backward, Selection::NotSelected);
textinput.adjust_horizontal(UTF8Bytes(3), Direction::Forward, Selection::Selected);
textinput.adjust_horizontal_by_one(Direction::Backward, Selection::NotSelected);
assert_eq!(textinput.edit_point().index, 0);
assert_eq!(textinput.edit_point().index, UTF8Bytes::zero());
let mut textinput = text_input(Lines::Multiple, "abc\nde\nf");
// Multiline - Forward
textinput.adjust_horizontal(4, Selection::Selected);
textinput.adjust_horizontal(1, Selection::NotSelected);
assert_eq!(textinput.edit_point().index, 0);
textinput.adjust_horizontal(UTF8Bytes(4), Direction::Forward, Selection::Selected);
textinput.adjust_horizontal(UTF8Bytes::one(), Direction::Forward, Selection::NotSelected);
assert_eq!(textinput.edit_point().index, UTF8Bytes::zero());
assert_eq!(textinput.edit_point().line, 1);
textinput.adjust_horizontal(-4, Selection::NotSelected);
textinput.adjust_horizontal(4, Selection::Selected);
textinput.adjust_horizontal(UTF8Bytes(4), Direction::Backward, Selection::NotSelected);
textinput.adjust_horizontal(UTF8Bytes(4), Direction::Forward, Selection::Selected);
textinput.adjust_horizontal_by_one(Direction::Forward, Selection::NotSelected);
assert_eq!(textinput.edit_point().index, 0);
assert_eq!(textinput.edit_point().index, UTF8Bytes::zero());
assert_eq!(textinput.edit_point().line, 1);
// Multiline - Backward
textinput.adjust_horizontal(-4, Selection::NotSelected);
textinput.adjust_horizontal(4, Selection::Selected);
textinput.adjust_horizontal(-1, Selection::NotSelected);
assert_eq!(textinput.edit_point().index, 0);
textinput.adjust_horizontal(UTF8Bytes(4), Direction::Backward, Selection::NotSelected);
textinput.adjust_horizontal(UTF8Bytes(4), Direction::Forward, Selection::Selected);
textinput.adjust_horizontal(
UTF8Bytes::one(),
Direction::Backward,
Selection::NotSelected,
);
assert_eq!(textinput.edit_point().index, UTF8Bytes::zero());
assert_eq!(textinput.edit_point().line, 0);
textinput.adjust_horizontal(-4, Selection::NotSelected);
textinput.adjust_horizontal(4, Selection::Selected);
textinput.adjust_horizontal(UTF8Bytes(4), Direction::Backward, Selection::NotSelected);
textinput.adjust_horizontal(UTF8Bytes(4), Direction::Forward, Selection::Selected);
textinput.adjust_horizontal_by_one(Direction::Backward, Selection::NotSelected);
assert_eq!(textinput.edit_point().index, 0);
assert_eq!(textinput.edit_point().index, UTF8Bytes::zero());
assert_eq!(textinput.edit_point().line, 0);
}
@ -613,16 +636,16 @@ fn test_textinput_set_selection_with_direction() {
let mut textinput = text_input(Lines::Single, "abcdef");
textinput.set_selection_range(2, 6, SelectionDirection::Forward);
assert_eq!(textinput.edit_point().line, 0);
assert_eq!(textinput.edit_point().index, 6);
assert_eq!(textinput.edit_point().index, UTF8Bytes(6));
assert_eq!(textinput.selection_direction(), SelectionDirection::Forward);
assert!(textinput.selection_origin().is_some());
assert_eq!(textinput.selection_origin().unwrap().line, 0);
assert_eq!(textinput.selection_origin().unwrap().index, 2);
assert_eq!(textinput.selection_origin().unwrap().index, UTF8Bytes(2));
textinput.set_selection_range(2, 6, SelectionDirection::Backward);
assert_eq!(textinput.edit_point().line, 0);
assert_eq!(textinput.edit_point().index, 2);
assert_eq!(textinput.edit_point().index, UTF8Bytes(2));
assert_eq!(
textinput.selection_direction(),
SelectionDirection::Backward
@ -630,37 +653,43 @@ fn test_textinput_set_selection_with_direction() {
assert!(textinput.selection_origin().is_some());
assert_eq!(textinput.selection_origin().unwrap().line, 0);
assert_eq!(textinput.selection_origin().unwrap().index, 6);
assert_eq!(textinput.selection_origin().unwrap().index, UTF8Bytes(6));
textinput = text_input(Lines::Multiple, "\n\n");
textinput.set_selection_range(0, 1, SelectionDirection::Forward);
assert_eq!(textinput.edit_point().line, 1);
assert_eq!(textinput.edit_point().index, 0);
assert_eq!(textinput.edit_point().index, UTF8Bytes::zero());
assert_eq!(textinput.selection_direction(), SelectionDirection::Forward);
assert!(textinput.selection_origin().is_some());
assert_eq!(textinput.selection_origin().unwrap().line, 0);
assert_eq!(textinput.selection_origin().unwrap().index, 0);
assert_eq!(
textinput.selection_origin().unwrap().index,
UTF8Bytes::zero()
);
textinput = text_input(Lines::Multiple, "\n");
textinput.set_selection_range(0, 1, SelectionDirection::Forward);
assert_eq!(textinput.edit_point().line, 1);
assert_eq!(textinput.edit_point().index, 0);
assert_eq!(textinput.edit_point().index, UTF8Bytes::zero());
assert_eq!(textinput.selection_direction(), SelectionDirection::Forward);
assert!(textinput.selection_origin().is_some());
assert_eq!(textinput.selection_origin().unwrap().line, 0);
assert_eq!(textinput.selection_origin().unwrap().index, 0);
assert_eq!(
textinput.selection_origin().unwrap().index,
UTF8Bytes::zero()
);
}
#[test]
fn test_textinput_unicode_handling() {
let mut textinput = text_input(Lines::Single, "éèùµ$£");
assert_eq!(textinput.edit_point().index, 0);
assert_eq!(textinput.edit_point().index, UTF8Bytes::zero());
textinput.set_edit_point_index(1);
assert_eq!(textinput.edit_point().index, 2);
assert_eq!(textinput.edit_point().index, UTF8Bytes(2));
textinput.set_edit_point_index(4);
assert_eq!(textinput.edit_point().index, 8);
assert_eq!(textinput.edit_point().index, UTF8Bytes(8));
}
#[test]
@ -668,40 +697,100 @@ fn test_selection_bounds() {
let mut textinput = text_input(Lines::Single, "abcdef");
assert_eq!(
TextPoint { line: 0, index: 0 },
TextPoint {
line: 0,
index: UTF8Bytes::zero()
},
textinput.selection_origin_or_edit_point()
);
assert_eq!(TextPoint { line: 0, index: 0 }, textinput.selection_start());
assert_eq!(TextPoint { line: 0, index: 0 }, textinput.selection_end());
assert_eq!(
TextPoint {
line: 0,
index: UTF8Bytes::zero()
},
textinput.selection_start()
);
assert_eq!(
TextPoint {
line: 0,
index: UTF8Bytes::zero()
},
textinput.selection_end()
);
textinput.set_selection_range(2, 5, SelectionDirection::Forward);
assert_eq!(
TextPoint { line: 0, index: 2 },
TextPoint {
line: 0,
index: UTF8Bytes(2)
},
textinput.selection_origin_or_edit_point()
);
assert_eq!(TextPoint { line: 0, index: 2 }, textinput.selection_start());
assert_eq!(TextPoint { line: 0, index: 5 }, textinput.selection_end());
assert_eq!(2, textinput.selection_start_offset());
assert_eq!(5, textinput.selection_end_offset());
assert_eq!(
TextPoint {
line: 0,
index: UTF8Bytes(2)
},
textinput.selection_start()
);
assert_eq!(
TextPoint {
line: 0,
index: UTF8Bytes(5)
},
textinput.selection_end()
);
assert_eq!(UTF8Bytes(2), textinput.selection_start_offset());
assert_eq!(UTF8Bytes(5), textinput.selection_end_offset());
textinput.set_selection_range(3, 6, SelectionDirection::Backward);
assert_eq!(
TextPoint { line: 0, index: 6 },
TextPoint {
line: 0,
index: UTF8Bytes(6)
},
textinput.selection_origin_or_edit_point()
);
assert_eq!(TextPoint { line: 0, index: 3 }, textinput.selection_start());
assert_eq!(TextPoint { line: 0, index: 6 }, textinput.selection_end());
assert_eq!(3, textinput.selection_start_offset());
assert_eq!(6, textinput.selection_end_offset());
assert_eq!(
TextPoint {
line: 0,
index: UTF8Bytes(3)
},
textinput.selection_start()
);
assert_eq!(
TextPoint {
line: 0,
index: UTF8Bytes(6)
},
textinput.selection_end()
);
assert_eq!(UTF8Bytes(3), textinput.selection_start_offset());
assert_eq!(UTF8Bytes(6), textinput.selection_end_offset());
textinput = text_input(Lines::Multiple, "\n\n");
textinput.set_selection_range(0, 1, SelectionDirection::Forward);
assert_eq!(
TextPoint { line: 0, index: 0 },
TextPoint {
line: 0,
index: UTF8Bytes::zero()
},
textinput.selection_origin_or_edit_point()
);
assert_eq!(TextPoint { line: 0, index: 0 }, textinput.selection_start());
assert_eq!(TextPoint { line: 1, index: 0 }, textinput.selection_end());
assert_eq!(
TextPoint {
line: 0,
index: UTF8Bytes::zero()
},
textinput.selection_start()
);
assert_eq!(
TextPoint {
line: 1,
index: UTF8Bytes::zero()
},
textinput.selection_end()
);
}
#[test]
@ -710,6 +799,18 @@ fn test_select_all() {
textinput.set_selection_range(2, 3, SelectionDirection::Backward);
textinput.select_all();
assert_eq!(textinput.selection_direction(), SelectionDirection::Forward);
assert_eq!(TextPoint { line: 0, index: 0 }, textinput.selection_start());
assert_eq!(TextPoint { line: 0, index: 3 }, textinput.selection_end());
assert_eq!(
TextPoint {
line: 0,
index: UTF8Bytes::zero()
},
textinput.selection_start()
);
assert_eq!(
TextPoint {
line: 0,
index: UTF8Bytes(3)
},
textinput.selection_end()
);
}