power_framework_test_realm/
lib.rs1use anyhow::Result;
6use fuchsia_component::client::Service;
7use fuchsia_component_test::{Capability, ChildOptions, RealmBuilder, RealmInstance, Ref, Route};
8
9use {
10 fidl_fuchsia_hardware_suspend as fhsuspend, fidl_fuchsia_power_broker as fbroker,
11 fidl_fuchsia_power_suspend as fsuspend, fidl_fuchsia_power_system as fsystem,
12 fidl_test_sagcontrol as ftsagcontrol, fidl_test_suspendcontrol as ftsuspendcontrol,
13};
14
15pub const COMPONENT_NAME: &str = "power_framework_test_realm";
16pub const POWER_FRAMEWORK_TEST_REALM_URL: &str = "power-framework#meta/power-framework.cm";
17
18#[async_trait::async_trait]
19pub trait PowerFrameworkTestRealmBuilder {
20 async fn power_framework_test_realm_manifest_setup(&self, manifest_url: &str) -> Result<&Self>;
25 async fn power_framework_test_realm_setup(&self) -> Result<&Self>;
28}
29
30#[async_trait::async_trait]
31impl PowerFrameworkTestRealmBuilder for RealmBuilder {
32 async fn power_framework_test_realm_manifest_setup(&self, manifest_url: &str) -> Result<&Self> {
33 let power_framework_realm =
34 self.add_child(COMPONENT_NAME, manifest_url, ChildOptions::new()).await?;
35
36 self.add_route(
38 Route::new()
39 .capability(Capability::protocol::<fbroker::TopologyMarker>())
40 .capability(Capability::protocol::<fsystem::ActivityGovernorMarker>())
41 .capability(Capability::protocol::<fsuspend::StatsMarker>())
42 .capability(Capability::protocol::<ftsagcontrol::StateMarker>())
43 .capability(Capability::protocol::<ftsuspendcontrol::DeviceMarker>())
44 .capability(Capability::service::<fhsuspend::SuspendServiceMarker>())
45 .from(&power_framework_realm)
46 .to(Ref::parent()),
47 )
48 .await?;
49 Ok(&self)
50 }
51
52 async fn power_framework_test_realm_setup(&self) -> Result<&Self> {
53 self.power_framework_test_realm_manifest_setup(POWER_FRAMEWORK_TEST_REALM_URL).await
54 }
55}
56
57#[async_trait::async_trait]
58pub trait PowerFrameworkTestRealmInstance {
59 async fn power_framework_test_realm_connect_to_suspender(
61 &self,
62 ) -> Result<fhsuspend::SuspenderProxy>;
63}
64
65#[async_trait::async_trait]
66impl PowerFrameworkTestRealmInstance for RealmInstance {
67 async fn power_framework_test_realm_connect_to_suspender(
68 &self,
69 ) -> Result<fhsuspend::SuspenderProxy> {
70 Service::open_from_dir(self.root.get_exposed_dir(), fhsuspend::SuspendServiceMarker)
71 .unwrap()
72 .connect_to_instance("default")?
73 .connect_to_suspender()
74 .map_err(|e| anyhow::anyhow!("Failed to connect to suspender: {:?}", e))
75 }
76}