brlapi 0.4.1

Safe Rust bindings for the BrlAPI library
// SPDX-License-Identifier: LGPL-2.1

//! Text writing operations tests

use brlapi::{BrlApiError, TtyMode};

mod common;

#[test]
fn test_text_writing_operations() {
    // Test text writing operations based on Python apitest.py patterns
    let conn_result = common::try_connect();

    if let Ok(conn) = conn_result {
        // First try to enter TTY mode
        match TtyMode::try_from(&conn) {
            Ok(tty_mode) => {
                println!("TTY mode entered, testing text operations");

                // Skip text operations for NoBraille driver
                match conn.display_driver() {
                    Ok(driver) if driver == "NoBraille" => {
                        println!("Skipping text write tests for NoBraille driver (not supported)");
                    }
                    _ => {
                        // Test basic text writing with sample texts
                        for text in common::test_data::sample_texts() {
                            match tty_mode.write_text(text) {
                                Ok(()) => println!(
                                    "Text write succeeded: '{}'",
                                    text.chars().take(20).collect::<String>()
                                ),
                                Err(e) => println!(
                                    "Text write failed for '{}': {e}",
                                    text.chars().take(20).collect::<String>()
                                ),
                            }
                        }
                    }
                }

                // Test text writing with cursor positioning (skip for NoBraille)
                match conn.display_driver() {
                    Ok(driver) if driver == "NoBraille" => {
                        println!("Skipping cursor positioning tests for NoBraille driver");
                    }
                    _ => {
                        for cursor_pos in common::test_data::cursor_positions() {
                            let cursor = if cursor_pos == brlapi_sys::BRLAPI_CURSOR_LEAVE {
                                brlapi::text::CursorPosition::Leave
                            } else if cursor_pos == brlapi_sys::BRLAPI_CURSOR_OFF as i32 {
                                brlapi::text::CursorPosition::Off
                            } else {
                                brlapi::text::CursorPosition::At(cursor_pos as u32)
                            };
                            match tty_mode
                                .write_text_with_cursor(&format!("Cursor at {cursor_pos}"), cursor)
                            {
                                Ok(()) => println!("Text write with cursor {cursor_pos} succeeded"),
                                Err(e) => {
                                    println!("Text write with cursor {cursor_pos} failed: {e}")
                                }
                            }
                        }
                    }
                }

                // TTY mode automatically cleaned up when tty_mode drops
            }
            Err(e) => {
                println!("TTY mode entry failed, testing text operations without TTY mode: {e}");

                // Skip text operations for NoBraille driver
                match conn.display_driver() {
                    Ok(driver) if driver == "NoBraille" => {
                        println!("Skipping text write without TTY mode test for NoBraille driver");
                    }
                    _ => {
                        // Test text operations without TTY mode (should fail appropriately)
                        match brlapi::text::util::write_message(&conn, "No TTY mode") {
                            Ok(()) => {
                                println!("Text write without TTY mode succeeded (unexpected)")
                            }
                            Err(e) => {
                                println!("Text write without TTY mode failed (expected): {e}");

                                // Should get appropriate error type
                                match e {
                                    BrlApiError::IllegalInstruction
                                    | BrlApiError::InvalidParameter
                                    | BrlApiError::OperationNotSupported => {
                                        // Expected error types
                                    }
                                    _ => println!("Unexpected error type for no TTY mode: {e}"),
                                }
                            }
                        }
                    }
                }
            }
        }
    } else {
        println!("No connection available for text writing testing");
    }
}

#[test]
fn test_text_writing_with_tty_helper() {
    // Test text writing using the helper function
    if let Some(_) = common::with_tty_mode(|conn| {
        // Skip text operations for NoBraille driver
        match conn.display_driver() {
            Ok(driver) if driver == "NoBraille" => {
                println!("Skipping text write helper tests for NoBraille driver");
                return Ok(());
            }
            _ => {}
        }

        // Test various text samples
        for text in common::test_data::sample_texts() {
            brlapi::text::util::write_message(conn, text)?;
            println!(
                "Successfully wrote: '{}'",
                text.chars().take(30).collect::<String>()
            );
        }

        Ok(())
    }) {
        println!("Text writing tests completed with TTY mode");
    } else {
        println!("TTY mode not available for text writing tests");
    }
}

#[test]
fn test_cursor_positioning() {
    // Test cursor positioning specifically
    if let Some(_) = common::with_tty_mode(|conn| {
        // Skip cursor positioning for NoBraille driver
        match conn.display_driver() {
            Ok(driver) if driver == "NoBraille" => {
                println!("Skipping cursor positioning tests for NoBraille driver");
                return Ok(());
            }
            _ => {}
        }

        let test_text = "Cursor test";

        for cursor_pos in common::test_data::cursor_positions() {
            let cursor = if cursor_pos == brlapi_sys::BRLAPI_CURSOR_LEAVE {
                brlapi::text::CursorPosition::Leave
            } else if cursor_pos == brlapi_sys::BRLAPI_CURSOR_OFF as i32 {
                brlapi::text::CursorPosition::Off
            } else {
                brlapi::text::CursorPosition::At(cursor_pos as u32)
            };
            brlapi::text::util::write_message_with_cursor(conn, test_text, cursor)?;
            println!("Cursor positioning test passed for position: {cursor_pos}");
        }

        Ok(())
    }) {
        println!("Cursor positioning tests completed");
    } else {
        println!("Cursor positioning tests skipped - no TTY mode available");
    }
}