use crate::tests::mono_style_test;
use crate::{Action, ActionResult, Point, TextContext, TextState};
#[test]
pub fn test_actions_disabled() {
let mut ctx = TextContext::default();
let initial_text = "Hello World".to_string();
let mut text_state = TextState::new_with_text(initial_text, &mut ctx.font_system, ());
text_state.set_style(&mono_style_test());
text_state.set_outer_size(&Point::from((200.0, 25.0)));
text_state.are_actions_enabled = false;
text_state.recalculate(&mut ctx);
assert!(matches!(
text_state.apply_action(&mut ctx, &Action::InsertChar("x".into())),
ActionResult::ActionsDisabled
));
assert!(matches!(
text_state.apply_action(&mut ctx, &Action::MoveCursorRight),
ActionResult::ActionsDisabled
));
assert!(matches!(
text_state.apply_action(&mut ctx, &Action::MoveCursorLeft),
ActionResult::ActionsDisabled
));
assert!(matches!(
text_state.apply_action(&mut ctx, &Action::MoveCursorUp),
ActionResult::ActionsDisabled
));
assert!(matches!(
text_state.apply_action(&mut ctx, &Action::MoveCursorDown),
ActionResult::ActionsDisabled
));
assert!(matches!(
text_state.apply_action(&mut ctx, &Action::DeleteBackward),
ActionResult::ActionsDisabled
));
assert!(matches!(
text_state.apply_action(&mut ctx, &Action::Paste("test".to_string())),
ActionResult::ActionsDisabled
));
assert!(matches!(
text_state.apply_action(&mut ctx, &Action::Cut),
ActionResult::ActionsDisabled
));
assert!(matches!(
text_state.apply_action(&mut ctx, &Action::CopySelectedText),
ActionResult::ActionsDisabled
));
assert!(matches!(
text_state.apply_action(&mut ctx, &Action::SelectAll),
ActionResult::ActionsDisabled
));
}
#[test]
pub fn test_not_selectable() {
let mut ctx = TextContext::default();
let initial_text = "Hello World".to_string();
let mut text_state = TextState::new_with_text(initial_text, &mut ctx.font_system, ());
text_state.set_style(&mono_style_test());
text_state.set_outer_size(&Point::from((200.0, 25.0)));
text_state.are_actions_enabled = true;
text_state.is_selectable = false;
text_state.recalculate(&mut ctx);
assert!(matches!(
text_state.apply_action(&mut ctx, &Action::InsertChar("x".into())),
ActionResult::None
));
assert!(matches!(
text_state.apply_action(&mut ctx, &Action::MoveCursorRight),
ActionResult::None
));
assert!(matches!(
text_state.apply_action(&mut ctx, &Action::MoveCursorLeft),
ActionResult::None
));
assert!(matches!(
text_state.apply_action(&mut ctx, &Action::MoveCursorUp),
ActionResult::None
));
assert!(matches!(
text_state.apply_action(&mut ctx, &Action::MoveCursorDown),
ActionResult::None
));
assert!(matches!(
text_state.apply_action(&mut ctx, &Action::DeleteBackward),
ActionResult::None
));
assert!(matches!(
text_state.apply_action(&mut ctx, &Action::Paste("test".to_string())),
ActionResult::None
));
assert!(matches!(
text_state.apply_action(&mut ctx, &Action::Cut),
ActionResult::None
));
assert!(matches!(
text_state.apply_action(&mut ctx, &Action::CopySelectedText),
ActionResult::None
));
assert!(matches!(
text_state.apply_action(&mut ctx, &Action::SelectAll),
ActionResult::None
));
}
#[test]
pub fn test_selectable_not_editable() {
let mut ctx = TextContext::default();
let initial_text = "Hello World".to_string();
let mut text_state = TextState::new_with_text(initial_text, &mut ctx.font_system, ());
text_state.set_style(&mono_style_test());
text_state.set_outer_size(&Point::from((200.0, 25.0)));
text_state.are_actions_enabled = true;
text_state.is_selectable = true;
text_state.is_editable = false;
text_state.recalculate(&mut ctx);
assert!(matches!(
text_state.apply_action(&mut ctx, &Action::InsertChar("x".into())),
ActionResult::None
));
assert!(matches!(
text_state.apply_action(&mut ctx, &Action::MoveCursorRight),
ActionResult::None
));
assert!(matches!(
text_state.apply_action(&mut ctx, &Action::MoveCursorLeft),
ActionResult::None
));
assert!(matches!(
text_state.apply_action(&mut ctx, &Action::MoveCursorUp),
ActionResult::None
));
assert!(matches!(
text_state.apply_action(&mut ctx, &Action::MoveCursorDown),
ActionResult::None
));
assert!(matches!(
text_state.apply_action(&mut ctx, &Action::DeleteBackward),
ActionResult::None
));
assert!(matches!(
text_state.apply_action(&mut ctx, &Action::Paste("test".to_string())),
ActionResult::None
));
assert!(matches!(
text_state.apply_action(&mut ctx, &Action::Cut),
ActionResult::None
));
let select_all_result = text_state.apply_action(&mut ctx, &Action::SelectAll);
assert!(matches!(select_all_result, ActionResult::CursorUpdated));
let copy_result = text_state.apply_action(&mut ctx, &Action::CopySelectedText);
assert!(matches!(copy_result, ActionResult::InsertToClipboard(s) if s == "Hello World"));
}
#[test]
pub fn test_selectable_and_editable() {
let mut ctx = TextContext::default();
let initial_text = "Hello World".to_string();
let mut text_state = TextState::new_with_text(initial_text, &mut ctx.font_system, ());
text_state.set_style(&mono_style_test());
text_state.set_outer_size(&Point::from((200.0, 25.0)));
text_state.are_actions_enabled = true;
text_state.is_selectable = true;
text_state.is_editable = true;
text_state.is_editing = true;
text_state.recalculate(&mut ctx);
assert!(matches!(
text_state.apply_action(&mut ctx, &Action::MoveCursorRight),
ActionResult::CursorUpdated
));
assert_eq!(text_state.cursor_char_index(), Some(1));
assert!(matches!(
text_state.apply_action(&mut ctx, &Action::MoveCursorLeft),
ActionResult::CursorUpdated
));
assert_eq!(text_state.cursor_char_index(), Some(0));
assert!(matches!(
text_state.apply_action(&mut ctx, &Action::InsertChar("X".into())),
ActionResult::TextChanged
));
assert_eq!(text_state.text(), "XHello World");
assert!(matches!(
text_state.apply_action(&mut ctx, &Action::DeleteBackward),
ActionResult::TextChanged
));
assert_eq!(text_state.text(), "Hello World");
text_state.apply_action(&mut ctx, &Action::SelectAll);
assert!(text_state.is_text_selected());
let copy_result = text_state.apply_action(&mut ctx, &Action::CopySelectedText);
assert!(matches!(copy_result, ActionResult::InsertToClipboard(s) if s == "Hello World"));
assert!(matches!(
text_state.apply_action(&mut ctx, &Action::Cut),
ActionResult::InsertToClipboard(s) if s == "Hello World"
));
assert_eq!(text_state.text(), "");
assert!(matches!(
text_state.apply_action(&mut ctx, &Action::Paste("Pasted Text".to_string())),
ActionResult::TextChanged
));
assert_eq!(text_state.text(), "Pasted Text");
}
#[test]
pub fn test_cursor_movement_with_selection() {
let mut ctx = TextContext::default();
let initial_text = "Hello World".to_string();
let mut text_state = TextState::new_with_text(initial_text, &mut ctx.font_system, ());
text_state.set_style(&mono_style_test());
text_state.set_outer_size(&Point::from((200.0, 25.0)));
text_state.are_actions_enabled = true;
text_state.is_selectable = true;
text_state.is_editable = true;
text_state.is_editing = true;
text_state.recalculate(&mut ctx);
text_state.apply_action(&mut ctx, &Action::SelectAll);
assert!(text_state.is_text_selected());
text_state.apply_action(&mut ctx, &Action::MoveCursorRight);
assert!(!text_state.is_text_selected());
assert_eq!(text_state.cursor_char_index(), Some(11));
text_state.apply_action(&mut ctx, &Action::SelectAll);
assert!(text_state.is_text_selected());
text_state.apply_action(&mut ctx, &Action::MoveCursorLeft);
assert!(!text_state.is_text_selected());
assert_eq!(text_state.cursor_char_index(), Some(0)); }
#[test]
pub fn test_vertical_cursor_movement() {
let mut ctx = TextContext::default();
let initial_text = "Line 1\nLine 2\nLine 3".to_string();
let mut text_state = TextState::new_with_text(initial_text, &mut ctx.font_system, ());
text_state.set_style(&mono_style_test());
text_state.set_outer_size(&Point::from((200.0, 100.0)));
text_state.are_actions_enabled = true;
text_state.is_selectable = true;
text_state.is_editable = true;
text_state.is_editing = true;
text_state.recalculate(&mut ctx);
assert_eq!(text_state.cursor_char_index(), Some(0));
assert!(matches!(
text_state.apply_action(&mut ctx, &Action::MoveCursorDown),
ActionResult::CursorUpdated
));
let pos_after_down = text_state.cursor_char_index();
assert!(pos_after_down.is_some());
assert!(pos_after_down.unwrap() > 6);
assert!(matches!(
text_state.apply_action(&mut ctx, &Action::MoveCursorUp),
ActionResult::CursorUpdated
));
assert_eq!(text_state.cursor_char_index(), Some(0));
}
#[test]
pub fn test_delete_with_selection() {
let mut ctx = TextContext::default();
let initial_text = "Hello World".to_string();
let mut text_state = TextState::new_with_text(initial_text, &mut ctx.font_system, ());
text_state.set_style(&mono_style_test());
text_state.set_outer_size(&Point::from((200.0, 25.0)));
text_state.are_actions_enabled = true;
text_state.is_selectable = true;
text_state.is_editable = true;
text_state.is_editing = true;
text_state.recalculate(&mut ctx);
text_state.handle_press(&mut ctx, Point { x: 0.0, y: 10.0 });
text_state.handle_drag(&mut ctx, true, Point { x: 40.0, y: 10.0 });
assert!(text_state.is_text_selected());
assert!(matches!(
text_state.apply_action(&mut ctx, &Action::DeleteBackward),
ActionResult::TextChanged
));
assert_eq!(text_state.text(), " World");
assert!(!text_state.is_text_selected());
}
#[test]
pub fn test_insert_char_with_selection() {
let mut ctx = TextContext::default();
let initial_text = "Hello World".to_string();
let mut text_state = TextState::new_with_text(initial_text, &mut ctx.font_system, ());
text_state.set_style(&mono_style_test());
text_state.set_outer_size(&Point::from((200.0, 25.0)));
text_state.are_actions_enabled = true;
text_state.is_selectable = true;
text_state.is_editable = true;
text_state.is_editing = true;
text_state.recalculate(&mut ctx);
text_state.handle_press(&mut ctx, Point { x: 0.0, y: 10.0 });
text_state.handle_drag(&mut ctx, true, Point { x: 40.0, y: 10.0 });
assert!(text_state.is_text_selected());
assert!(matches!(
text_state.apply_action(&mut ctx, &Action::InsertChar("X".into())),
ActionResult::TextChanged
));
assert_eq!(text_state.text(), "X World");
assert!(!text_state.is_text_selected());
}