power_manager_integration_test_lib/mocks/
activity_service.rs1use fidl::endpoints::ServerEnd;
6use fidl_fuchsia_io::DirectoryMarker;
7use fuchsia_component::server::ServiceFs;
8use fuchsia_component_test::LocalComponentHandles;
9use futures::channel::mpsc;
10use futures::lock::Mutex;
11use futures::{StreamExt, TryStreamExt};
12use log::*;
13use std::sync::Arc;
14use {fidl_fuchsia_ui_activity as factivity, fuchsia_async as fasync};
15
16pub struct MockActivityService {
18 state_sender: Mutex<mpsc::Sender<factivity::State>>,
21
22 state_receiver: Mutex<mpsc::Receiver<factivity::State>>,
26}
27
28impl MockActivityService {
29 pub fn new() -> Arc<MockActivityService> {
30 let (state_sender, state_receiver) = mpsc::channel(1);
31 Arc::new(Self {
32 state_sender: Mutex::new(state_sender),
33 state_receiver: Mutex::new(state_receiver),
34 })
35 }
36
37 pub async fn run(self: Arc<Self>, handles: LocalComponentHandles) -> Result<(), anyhow::Error> {
54 self.run_inner(handles.outgoing_dir).await
55 }
56
57 async fn run_inner(
58 self: Arc<Self>,
59 outgoing_dir: ServerEnd<DirectoryMarker>,
60 ) -> Result<(), anyhow::Error> {
61 let mut fs = ServiceFs::new();
62 fs.dir("svc").add_fidl_service(move |mut stream: factivity::ProviderRequestStream| {
63 let this = self.clone();
64 fasync::Task::local(async move {
65 info!("MockActivityService: new connection");
66 let factivity::ProviderRequest::WatchState { listener, .. } =
67 stream.try_next().await.unwrap().unwrap();
68 info!("MockActivityService: received WatchState request");
69 let listener = listener.into_proxy();
70 while let Some(state) = this.state_receiver.lock().await.next().await {
71 info!("MockActivityService: sending activity state: {:?}", state);
72 let _ = listener.on_state_changed(state, 0).await;
73 }
74 info!("MockActivityService: closing connection")
75 })
76 .detach();
77 });
78
79 fs.serve_connection(outgoing_dir).unwrap();
80 fs.collect::<()>().await;
81
82 Ok(())
83 }
84
85 pub async fn set_activity_state(&self, state: factivity::State) {
86 info!("MockActivityService: set activity state: {:?}", state);
87 self.state_sender.lock().await.try_send(state).expect("try_send() failed");
88 }
89}
90
91#[cfg(test)]
92mod tests {
93 use super::*;
94 use assert_matches::assert_matches;
95 use fuchsia_component::client::connect_to_protocol_at_dir_svc;
96
97 #[fuchsia::test]
98 async fn test_set_activity_state() {
99 let (dir, outgoing_dir) = fidl::endpoints::create_proxy::<DirectoryMarker>();
101 let mock = MockActivityService::new();
102 let _task = fasync::Task::local(mock.clone().run_inner(outgoing_dir));
103
104 let provider_client =
106 connect_to_protocol_at_dir_svc::<factivity::ProviderMarker>(&dir).unwrap();
107
108 let (listener_client, mut listener_stream) =
110 fidl::endpoints::create_request_stream::<factivity::ListenerMarker>();
111 provider_client.watch_state(listener_client).unwrap();
112
113 mock.set_activity_state(factivity::State::Active).await;
115 let factivity::ListenerRequest::OnStateChanged { state, responder, .. } =
116 listener_stream.next().await.unwrap().unwrap();
117 assert_matches!(responder.send(), Ok(()));
118 assert_eq!(state, factivity::State::Active);
119
120 mock.set_activity_state(factivity::State::Idle).await;
122 let factivity::ListenerRequest::OnStateChanged { state, responder, .. } =
123 listener_stream.next().await.unwrap().unwrap();
124 assert_matches!(responder.send(), Ok(()));
125 assert_eq!(state, factivity::State::Idle);
126 }
127}