test: Phase2 Task4 — 新增4个光标控制测试 总计77测试通过

This commit is contained in:
showen
2026-03-13 07:47:32 +08:00
parent bf41c4513f
commit be08c63181
3 changed files with 116 additions and 2 deletions

View File

@@ -33,7 +33,11 @@ impl DeviceBackend for MockBackend {
}
fn capabilities(&self) -> Vec<DeviceCapability> {
vec![DeviceCapability::Display, DeviceCapability::Backlight]
vec![
DeviceCapability::Display,
DeviceCapability::Backlight,
DeviceCapability::Cursor,
]
}
fn handle_command(&mut self, cmd: DeviceCommand) -> Result<DeviceResponse> {
@@ -45,6 +49,7 @@ impl DeviceBackend for MockBackend {
}),
DeviceCommand::SetSleepInhibit(_) => Ok(DeviceResponse::Ok),
DeviceCommand::SetBacklight(_) => Ok(DeviceResponse::Ok),
DeviceCommand::SetCursorVisible(_) => Ok(DeviceResponse::Ok),
_ => Ok(DeviceResponse::Error("not supported".to_string())),
}
}
@@ -154,9 +159,10 @@ fn test_device_event_serialization() {
fn test_mock_backend_capabilities() {
let backend = MockBackend::new();
let caps = backend.capabilities();
assert_eq!(caps.len(), 2);
assert_eq!(caps.len(), 3);
assert!(caps.contains(&DeviceCapability::Display));
assert!(caps.contains(&DeviceCapability::Backlight));
assert!(caps.contains(&DeviceCapability::Cursor));
}
#[test]
@@ -216,3 +222,60 @@ fn test_mock_backend_unsupported_command() {
_ => panic!("expected Error response for unsupported command"),
}
}
#[test]
fn test_mock_backend_set_cursor_visible() {
let mut backend = MockBackend::new();
backend
.init(&serde_json::json!({}))
.expect("init should succeed");
let response_hide = backend
.handle_command(DeviceCommand::SetCursorVisible(false))
.expect("SetCursorVisible(false) should succeed");
assert!(matches!(response_hide, DeviceResponse::Ok));
let response_show = backend
.handle_command(DeviceCommand::SetCursorVisible(true))
.expect("SetCursorVisible(true) should succeed");
assert!(matches!(response_show, DeviceResponse::Ok));
}
#[test]
fn test_mock_backend_cursor_capability() {
let backend = MockBackend::new();
let caps = backend.capabilities();
assert!(
caps.contains(&DeviceCapability::Cursor),
"MockBackend should declare Cursor capability"
);
}
#[test]
fn test_device_command_cursor_serialization() {
let commands = vec![
DeviceCommand::SetCursorVisible(true),
DeviceCommand::SetCursorVisible(false),
];
for cmd in commands {
let json = serde_json::to_string(&cmd).expect("SetCursorVisible should serialize");
let decoded: DeviceCommand =
serde_json::from_str(&json).expect("SetCursorVisible should deserialize");
let json2 = serde_json::to_string(&decoded).expect("decoded should serialize again");
assert_eq!(json, json2, "SetCursorVisible round trip should be stable");
}
}
#[test]
fn test_device_capability_cursor() {
let cap = DeviceCapability::Cursor;
let json = serde_json::to_string(&cap).expect("DeviceCapability::Cursor should serialize");
let decoded: DeviceCapability =
serde_json::from_str(&json).expect("DeviceCapability::Cursor should deserialize");
let json2 = serde_json::to_string(&decoded).expect("decoded should serialize again");
assert_eq!(
json, json2,
"DeviceCapability::Cursor round trip should be stable"
);
}