xref: /aosp_15_r20/external/uwb/src/rust/uwb_core/src/service/mock_uwb_service_callback.rs (revision e0df40009cb5d71e642272d38ba1bb7ffccfce41)
1 // Copyright 2022, The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 use std::collections::VecDeque;
16 use std::sync::{Arc, Mutex};
17 
18 use tokio::sync::Notify;
19 use tokio::time::{timeout, Duration};
20 
21 use crate::params::{DeviceState, ReasonCode, SessionId, SessionState};
22 use crate::service::uwb_service::UwbServiceCallback;
23 use crate::uci::SessionRangeData;
24 
25 #[derive(Clone, Default)]
26 pub(crate) struct MockUwbServiceCallback {
27     expected_calls: Arc<Mutex<VecDeque<ExpectedCall>>>,
28     expect_call_consumed: Arc<Notify>,
29 }
30 
31 impl MockUwbServiceCallback {
new() -> Self32     pub fn new() -> Self {
33         Default::default()
34     }
35 
expect_on_service_reset(&mut self, success: bool)36     pub fn expect_on_service_reset(&mut self, success: bool) {
37         self.push_expected_call(ExpectedCall::ServiceReset { success });
38     }
39 
expect_on_uci_device_status_changed(&mut self, state: DeviceState)40     pub fn expect_on_uci_device_status_changed(&mut self, state: DeviceState) {
41         self.push_expected_call(ExpectedCall::UciDeviceStatus { state });
42     }
43 
expect_on_session_state_changed( &mut self, session_id: SessionId, session_state: SessionState, reason_code: ReasonCode, )44     pub fn expect_on_session_state_changed(
45         &mut self,
46         session_id: SessionId,
47         session_state: SessionState,
48         reason_code: ReasonCode,
49     ) {
50         self.push_expected_call(ExpectedCall::SessionState {
51             session_id,
52             session_state,
53             reason_code,
54         });
55     }
56 
expect_on_range_data_received( &mut self, session_id: SessionId, range_data: SessionRangeData, )57     pub fn expect_on_range_data_received(
58         &mut self,
59         session_id: SessionId,
60         range_data: SessionRangeData,
61     ) {
62         self.push_expected_call(ExpectedCall::RangeData { session_id, range_data });
63     }
64 
expect_on_vendor_notification_received(&mut self, gid: u32, oid: u32, payload: Vec<u8>)65     pub fn expect_on_vendor_notification_received(&mut self, gid: u32, oid: u32, payload: Vec<u8>) {
66         self.push_expected_call(ExpectedCall::VendorNotification { gid, oid, payload });
67     }
68 
wait_expected_calls_done(&mut self) -> bool69     pub async fn wait_expected_calls_done(&mut self) -> bool {
70         while !self.expected_calls.lock().unwrap().is_empty() {
71             if timeout(Duration::from_secs(1), self.expect_call_consumed.notified()).await.is_err()
72             {
73                 return false;
74             }
75         }
76         true
77     }
78 
push_expected_call(&mut self, call: ExpectedCall)79     fn push_expected_call(&mut self, call: ExpectedCall) {
80         self.expected_calls.lock().unwrap().push_back(call);
81     }
82 
pop_expected_call(&mut self) -> ExpectedCall83     fn pop_expected_call(&mut self) -> ExpectedCall {
84         let call = self.expected_calls.lock().unwrap().pop_front().unwrap();
85         self.expect_call_consumed.notify_one();
86         call
87     }
88 }
89 
90 impl UwbServiceCallback for MockUwbServiceCallback {
on_service_reset(&mut self, success: bool)91     fn on_service_reset(&mut self, success: bool) {
92         assert_eq!(self.pop_expected_call(), ExpectedCall::ServiceReset { success });
93     }
94 
on_uci_device_status_changed(&mut self, state: DeviceState)95     fn on_uci_device_status_changed(&mut self, state: DeviceState) {
96         assert_eq!(self.pop_expected_call(), ExpectedCall::UciDeviceStatus { state });
97     }
98 
on_session_state_changed( &mut self, session_id: SessionId, session_state: SessionState, reason_code: ReasonCode, )99     fn on_session_state_changed(
100         &mut self,
101         session_id: SessionId,
102         session_state: SessionState,
103         reason_code: ReasonCode,
104     ) {
105         assert_eq!(
106             self.pop_expected_call(),
107             ExpectedCall::SessionState { session_id, session_state, reason_code }
108         );
109     }
110 
on_range_data_received(&mut self, session_id: SessionId, range_data: SessionRangeData)111     fn on_range_data_received(&mut self, session_id: SessionId, range_data: SessionRangeData) {
112         assert_eq!(self.pop_expected_call(), ExpectedCall::RangeData { session_id, range_data });
113     }
114 
on_vendor_notification_received(&mut self, gid: u32, oid: u32, payload: Vec<u8>)115     fn on_vendor_notification_received(&mut self, gid: u32, oid: u32, payload: Vec<u8>) {
116         assert_eq!(
117             self.pop_expected_call(),
118             ExpectedCall::VendorNotification { gid, oid, payload }
119         );
120     }
121 }
122 
123 #[derive(PartialEq, Debug)]
124 pub(crate) enum ExpectedCall {
125     ServiceReset { success: bool },
126     UciDeviceStatus { state: DeviceState },
127     SessionState { session_id: SessionId, session_state: SessionState, reason_code: ReasonCode },
128     RangeData { session_id: SessionId, range_data: SessionRangeData },
129     VendorNotification { gid: u32, oid: u32, payload: Vec<u8> },
130 }
131