1 use std::cell::RefCell; 2 use std::collections::VecDeque; 3 4 use uwb_uci_packets::GetDeviceInfoRspPacket; 5 use uwb_uci_rust::error::UwbErr; 6 use uwb_uci_rust::uci::{uci_hrcv::UciResponse, Dispatcher, JNICommand, Result}; 7 8 #[cfg(test)] 9 #[derive(Default)] 10 pub struct MockDispatcher { 11 expected_calls: RefCell<VecDeque<ExpectedCall>>, 12 device_info: Option<GetDeviceInfoRspPacket>, 13 } 14 15 #[cfg(test)] 16 impl MockDispatcher { new() -> Self17 pub fn new() -> Self { 18 Default::default() 19 } 20 expect_send_jni_command(&mut self, expected_cmd: JNICommand, out: Result<()>)21 pub fn expect_send_jni_command(&mut self, expected_cmd: JNICommand, out: Result<()>) { 22 self.expected_calls 23 .borrow_mut() 24 .push_back(ExpectedCall::SendJniCommand { expected_cmd, out }) 25 } 26 expect_block_on_jni_command( &mut self, expected_cmd: JNICommand, out: Result<UciResponse>, )27 pub fn expect_block_on_jni_command( 28 &mut self, 29 expected_cmd: JNICommand, 30 out: Result<UciResponse>, 31 ) { 32 self.expected_calls 33 .borrow_mut() 34 .push_back(ExpectedCall::BlockOnJniCommand { expected_cmd, out }) 35 } 36 expect_wait_for_exit(&mut self, out: Result<()>)37 pub fn expect_wait_for_exit(&mut self, out: Result<()>) { 38 self.expected_calls.borrow_mut().push_back(ExpectedCall::WaitForExit { out }) 39 } 40 } 41 42 #[cfg(test)] 43 impl Drop for MockDispatcher { drop(&mut self)44 fn drop(&mut self) { 45 assert!(self.expected_calls.borrow().is_empty()); 46 } 47 } 48 49 #[cfg(test)] 50 impl Dispatcher for MockDispatcher { send_jni_command(&self, cmd: JNICommand) -> Result<()>51 fn send_jni_command(&self, cmd: JNICommand) -> Result<()> { 52 let mut expected_calls = self.expected_calls.borrow_mut(); 53 match expected_calls.pop_front() { 54 Some(ExpectedCall::SendJniCommand { expected_cmd, out }) if cmd == expected_cmd => out, 55 Some(call) => { 56 expected_calls.push_front(call); 57 Err(UwbErr::Undefined) 58 } 59 None => Err(UwbErr::Undefined), 60 } 61 } block_on_jni_command(&self, cmd: JNICommand) -> Result<UciResponse>62 fn block_on_jni_command(&self, cmd: JNICommand) -> Result<UciResponse> { 63 let mut expected_calls = self.expected_calls.borrow_mut(); 64 match expected_calls.pop_front() { 65 Some(ExpectedCall::BlockOnJniCommand { expected_cmd, out }) if cmd == expected_cmd => { 66 out 67 } 68 Some(call) => { 69 expected_calls.push_front(call); 70 Err(UwbErr::Undefined) 71 } 72 None => Err(UwbErr::Undefined), 73 } 74 } wait_for_exit(&mut self) -> Result<()>75 fn wait_for_exit(&mut self) -> Result<()> { 76 let mut expected_calls = self.expected_calls.borrow_mut(); 77 match expected_calls.pop_front() { 78 Some(ExpectedCall::WaitForExit { out }) => out, 79 Some(call) => { 80 expected_calls.push_front(call); 81 Err(UwbErr::Undefined) 82 } 83 None => Err(UwbErr::Undefined), 84 } 85 } 86 set_device_info(&mut self, device_info: Option<GetDeviceInfoRspPacket>)87 fn set_device_info(&mut self, device_info: Option<GetDeviceInfoRspPacket>) { 88 self.device_info = device_info; 89 } 90 get_device_info(&self) -> &Option<GetDeviceInfoRspPacket>91 fn get_device_info(&self) -> &Option<GetDeviceInfoRspPacket> { 92 &self.device_info 93 } 94 } 95 96 #[cfg(test)] 97 enum ExpectedCall { 98 SendJniCommand { expected_cmd: JNICommand, out: Result<()> }, 99 BlockOnJniCommand { expected_cmd: JNICommand, out: Result<UciResponse> }, 100 WaitForExit { out: Result<()> }, 101 } 102