power_manager_integration_test_lib/mocks/
activity_service.rsuse fidl::endpoints::ServerEnd;
use fidl_fuchsia_io::DirectoryMarker;
use fuchsia_component::server::ServiceFs;
use fuchsia_component_test::LocalComponentHandles;
use futures::channel::mpsc;
use futures::lock::Mutex;
use futures::{StreamExt, TryStreamExt};
use std::sync::Arc;
use tracing::*;
use {fidl_fuchsia_ui_activity as factivity, fuchsia_async as fasync};
pub struct MockActivityService {
state_sender: Mutex<mpsc::Sender<factivity::State>>,
state_receiver: Mutex<mpsc::Receiver<factivity::State>>,
}
impl MockActivityService {
pub fn new() -> Arc<MockActivityService> {
let (state_sender, state_receiver) = mpsc::channel(1);
Arc::new(Self {
state_sender: Mutex::new(state_sender),
state_receiver: Mutex::new(state_receiver),
})
}
pub async fn run(self: Arc<Self>, handles: LocalComponentHandles) -> Result<(), anyhow::Error> {
self.run_inner(handles.outgoing_dir).await
}
async fn run_inner(
self: Arc<Self>,
outgoing_dir: ServerEnd<DirectoryMarker>,
) -> Result<(), anyhow::Error> {
let mut fs = ServiceFs::new();
fs.dir("svc").add_fidl_service(move |mut stream: factivity::ProviderRequestStream| {
let this = self.clone();
fasync::Task::local(async move {
info!("MockActivityService: new connection");
let factivity::ProviderRequest::WatchState { listener, .. } =
stream.try_next().await.unwrap().unwrap();
info!("MockActivityService: received WatchState request");
let listener = listener.into_proxy();
while let Some(state) = this.state_receiver.lock().await.next().await {
info!("MockActivityService: sending activity state: {:?}", state);
let _ = listener.on_state_changed(state, 0).await;
}
info!("MockActivityService: closing connection")
})
.detach();
});
fs.serve_connection(outgoing_dir).unwrap();
fs.collect::<()>().await;
Ok(())
}
pub async fn set_activity_state(&self, state: factivity::State) {
info!("MockActivityService: set activity state: {:?}", state);
self.state_sender.lock().await.try_send(state).expect("try_send() failed");
}
}
#[cfg(test)]
mod tests {
use super::*;
use assert_matches::assert_matches;
use fuchsia_component::client::connect_to_protocol_at_dir_svc;
#[fuchsia::test]
async fn test_set_activity_state() {
let (dir, outgoing_dir) = fidl::endpoints::create_proxy::<DirectoryMarker>();
let mock = MockActivityService::new();
let _task = fasync::Task::local(mock.clone().run_inner(outgoing_dir));
let provider_client =
connect_to_protocol_at_dir_svc::<factivity::ProviderMarker>(&dir).unwrap();
let (listener_client, mut listener_stream) =
fidl::endpoints::create_request_stream::<factivity::ListenerMarker>();
provider_client.watch_state(listener_client).unwrap();
mock.set_activity_state(factivity::State::Active).await;
let factivity::ListenerRequest::OnStateChanged { state, responder, .. } =
listener_stream.next().await.unwrap().unwrap();
assert_matches!(responder.send(), Ok(()));
assert_eq!(state, factivity::State::Active);
mock.set_activity_state(factivity::State::Idle).await;
let factivity::ListenerRequest::OnStateChanged { state, responder, .. } =
listener_stream.next().await.unwrap().unwrap();
assert_matches!(responder.send(), Ok(()));
assert_eq!(state, factivity::State::Idle);
}
}