From dcad15284d328c0382e048b9ea0100f4dafe9030 Mon Sep 17 00:00:00 2001 From: bunnie Date: Thu, 16 Dec 2021 01:48:11 +0800 Subject: [PATCH] refactor tests into a separate file --- services/modals/Cargo.toml | 3 ++- services/modals/src/lib.rs | 2 +- services/modals/src/main.rs | 6 +++++- services/modals/src/tests.rs | 34 ++++++++++++++++++++++++++++++++++ services/pddb/src/main.rs | 17 ++--------------- 5 files changed, 44 insertions(+), 18 deletions(-) create mode 100644 services/modals/src/tests.rs diff --git a/services/modals/Cargo.toml b/services/modals/Cargo.toml index 75e4fa5be..8fcfef248 100644 --- a/services/modals/Cargo.toml +++ b/services/modals/Cargo.toml @@ -22,4 +22,5 @@ locales = {path = "../../locales"} utralib = { path = "../../utralib"} [features] -default = [] +ux_tests = [] +default = ["ux_tests"] diff --git a/services/modals/src/lib.rs b/services/modals/src/lib.rs index 7699238c5..866ed013a 100644 --- a/services/modals/src/lib.rs +++ b/services/modals/src/lib.rs @@ -23,7 +23,7 @@ impl Modals { pub fn get_text_input(&self, prompt: &str, - maybe_validator: Option Option >, + maybe_validator: Option Option>, maybe_validator_op: Option, ) -> Result { let validator = if let Some(validator) = maybe_validator { diff --git a/services/modals/src/main.rs b/services/modals/src/main.rs index d7c16b933..d87b6ca47 100644 --- a/services/modals/src/main.rs +++ b/services/modals/src/main.rs @@ -64,7 +64,7 @@ fn xmain() -> ! { let op = Arc::new(Mutex::new(RendererState::None)); // create a thread that just handles the redrawing requests - let redraw_handle = thread::spawn({ + let _redraw_handle = thread::spawn({ let op = Arc::clone(&op); move || { // build the core data structure here @@ -202,6 +202,10 @@ fn xmain() -> ! { } }); + if cfg!(feature = "ux_tests") { + tests::spawn_test(); + } + loop { let mut msg = xous::receive_message(modals_sid).unwrap(); log::debug!("message: {:?}", msg); diff --git a/services/modals/src/tests.rs b/services/modals/src/tests.rs new file mode 100644 index 000000000..ba561a3c4 --- /dev/null +++ b/services/modals/src/tests.rs @@ -0,0 +1,34 @@ +#![allow(dead_code)] +use std::thread; +use gam::*; + +use xous_names::XousNames; + +pub(crate) fn spawn_test() { + thread::spawn({ + move || { + let xns = XousNames::new().unwrap(); + + // test the modal dialog box function + let modals = modals::Modals::new(&xns).unwrap(); + log::info!("test text input"); + match modals.get_text_input("Test input", Some(test_validator), None) { + Ok(text) => { + log::info!("Input: {}", text.0); + } + _ => { + log::error!("get_text_input failed"); + } + } + log::info!("text input test done"); + } + }); +} + +fn test_validator(input: TextEntryPayload, _opcode: u32) -> Option> { + let text_str = input.as_str(); + match text_str.parse::() { + Ok(_input_int) => None, + _ => return Some(xous_ipc::String::<256>::from_str("enter an integer value")) + } +} diff --git a/services/pddb/src/main.rs b/services/pddb/src/main.rs index d33989a2c..b73d01d40 100644 --- a/services/pddb/src/main.rs +++ b/services/pddb/src/main.rs @@ -624,19 +624,6 @@ fn xmain() -> ! { let sr_cid = xous::connect(pddb_sid).expect("couldn't create suspend callback connection"); let _susres = susres::Susres::new(&xns, api::Opcode::SuspendResume as u32, sr_cid).expect("couldn't create suspend/resume object"); - // test the modal dialog box function - let modals = modals::Modals::new(&xns).unwrap(); - log::info!("testing modals"); - match modals.get_text_input("Test input", Some(test_validator), None) { - Ok(text) => { - log::info!("Input: {}", text.0); - } - _ => { - log::error!("get_text_input failed"); - } - } - log::info!("done"); - // our very own password modal. Password modals are precious and privately owned, to avoid // other processes from crafting them. let pw_sid = xous::create_server().expect("couldn't create a server for the password UX handler"); @@ -649,7 +636,7 @@ fn xmain() -> ! { ) } }); - + /* just for testing let request = BasisRequestPassword { db_name: xous_ipc::String::<{crate::api::BASIS_NAME_LEN}>::from_str("test basis"), plaintext_pw: None, @@ -658,7 +645,7 @@ fn xmain() -> ! { buf.lend_mut(pw_cid, PwManagerOpcode::RequestPassword.to_u32().unwrap()).unwrap(); let ret = buf.to_original::().unwrap(); log::info!("Got password: {:?}", ret.plaintext_pw); - + */ loop { let msg = xous::receive_message(pddb_sid).unwrap(); match FromPrimitive::from_usize(msg.body.id()) {