test_battery_manager/
lib.rs

1// Copyright 2021 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use battery_client::BatteryClient;
6use fidl::client::QueryResponseFut;
7use fidl_fuchsia_power_battery as fpower;
8use futures::StreamExt;
9
10pub struct TestBatteryManager {
11    _stream: fpower::BatteryManagerRequestStream,
12    watcher_client: fpower::BatteryInfoWatcherProxy,
13}
14
15impl TestBatteryManager {
16    pub fn new(
17        s: fpower::BatteryManagerRequestStream,
18        watcher_client: fpower::BatteryInfoWatcherProxy,
19    ) -> Self {
20        Self { _stream: s, watcher_client }
21    }
22
23    pub async fn make_battery_client_with_test_manager() -> (BatteryClient, TestBatteryManager) {
24        let (c, mut s) = fidl::endpoints::create_proxy_and_stream::<fpower::BatteryManagerMarker>();
25
26        let battery_client =
27            BatteryClient::register_updates(c).expect("can register battery client");
28        let watcher_client = match s.next().await.expect("valid fidl request") {
29            Ok(fpower::BatteryManagerRequest::Watch { watcher, .. }) => watcher.into_proxy(),
30            x => panic!("Expected watch request, got: {:?}", x),
31        };
32
33        let mgr = TestBatteryManager::new(s, watcher_client);
34        (battery_client, mgr)
35    }
36
37    /// Sends the provided `update` to the `watcher_client`. Returns a Future associated with the
38    /// send request.
39    pub fn send_update(&self, update: fpower::BatteryInfo) -> QueryResponseFut<()> {
40        let update_fut = self.watcher_client.on_change_battery_info(&update);
41        update_fut
42    }
43}