diff options
Diffstat (limited to 'tests/unit/script/textinput.rs')
-rw-r--r-- | tests/unit/script/textinput.rs | 107 |
1 files changed, 104 insertions, 3 deletions
diff --git a/tests/unit/script/textinput.rs b/tests/unit/script/textinput.rs index 3d62f1f32ed..25ff3e827a0 100644 --- a/tests/unit/script/textinput.rs +++ b/tests/unit/script/textinput.rs @@ -13,11 +13,111 @@ use msg::constellation_msg::CONTROL; use msg::constellation_msg::SUPER; use msg::constellation_msg::{Key, KeyModifiers}; use script::clipboard_provider::DummyClipboardContext; -use script::textinput::{TextInput, Selection, Lines, Direction}; +use script::textinput::{TextInput, TextPoint, Selection, Lines, Direction}; use util::str::DOMString; fn text_input(lines: Lines, s: &str) -> TextInput<DummyClipboardContext> { - TextInput::new(lines, DOMString::from(s), DummyClipboardContext::new("")) + TextInput::new(lines, DOMString::from(s), DummyClipboardContext::new(""), None) +} + +#[test] +fn test_textinput_when_inserting_multiple_lines_over_a_selection_respects_max_length() { + let mut textinput = TextInput::new( + Lines::Multiple, + DOMString::from("hello\nworld"), + DummyClipboardContext::new(""), + Some(17) + ); + + textinput.edit_point = TextPoint { line: 0, index: 1 }; + textinput.adjust_horizontal(3, Selection::Selected); + textinput.adjust_vertical(1, Selection::Selected); + + // Selection is now "hello\n + // ------ + // world" + // ---- + + textinput.insert_string("cruel\nterrible\nbad".to_string()); + + assert_eq!(textinput.get_content(), "hcruel\nterrible\nd"); +} + +#[test] +fn test_textinput_when_inserting_multiple_lines_still_respects_max_length() { + let mut textinput = TextInput::new( + Lines::Multiple, + DOMString::from("hello\nworld"), + DummyClipboardContext::new(""), + Some(17) + ); + + textinput.edit_point = TextPoint { line: 1, index: 0 }; + + textinput.insert_string("cruel\nterrible".to_string()); + + assert_eq!(textinput.get_content(), "hello\ncruel\nworld"); +} + +#[test] +fn test_textinput_when_content_is_already_longer_than_max_length_and_theres_no_selection_dont_insert_anything() { + let mut textinput = TextInput::new( + Lines::Single, + DOMString::from("abc"), + DummyClipboardContext::new(""), + Some(1) + ); + + textinput.insert_char('a'); + + assert_eq!(textinput.get_content(), "abc"); +} + +#[test] +fn test_multi_line_textinput_with_maxlength_doesnt_allow_appending_characters_when_input_spans_lines() { + let mut textinput = TextInput::new( + Lines::Multiple, + DOMString::from("abc\nd"), + DummyClipboardContext::new(""), + Some(5) + ); + + textinput.insert_char('a'); + + assert_eq!(textinput.get_content(), "abc\nd"); +} + +#[test] +fn test_single_line_textinput_with_max_length_doesnt_allow_appending_characters_when_replacing_a_selection() { + let mut textinput = TextInput::new( + Lines::Single, + DOMString::from("abcde"), + DummyClipboardContext::new(""), + Some(5) + ); + + textinput.edit_point = TextPoint { line: 0, index: 1 }; + textinput.adjust_horizontal(3, Selection::Selected); + + // Selection is now "abcde" + // --- + + textinput.replace_selection(DOMString::from("too long")); + + assert_eq!(textinput.get_content(), "atooe"); +} + +#[test] +fn test_single_line_textinput_with_max_length_doesnt_allow_appending_characters_after_max_length_is_reached() { + let mut textinput = TextInput::new( + Lines::Single, + DOMString::from("a"), + DummyClipboardContext::new(""), + Some(1) + ); + + textinput.insert_char('b'); + assert_eq!(textinput.get_content(), "a"); } #[test] @@ -199,7 +299,8 @@ fn test_clipboard_paste() { let mut textinput = TextInput::new(Lines::Single, DOMString::from("defg"), - DummyClipboardContext::new("abc")); + DummyClipboardContext::new("abc"), + None); assert_eq!(textinput.get_content(), "defg"); assert_eq!(textinput.edit_point.index, 0); textinput.handle_keydown_aux(Key::V, MODIFIERS); |