inspect_rust_codelab_testing/
lib.rs1use anyhow::Error;
6use fidl_fuchsia_examples_inspect::{ReverserMarker, ReverserProxy};
7use fuchsia_component_test::{Capability, ChildOptions, RealmBuilder, RealmInstance, Ref, Route};
8
9const FIZZBUZZ_URL: &'static str = "#meta/fizzbuzz.cm";
10
11pub struct TestOptions {
12 pub include_fizzbuzz: bool,
13}
14
15impl Default for TestOptions {
16 fn default() -> Self {
17 TestOptions { include_fizzbuzz: true }
18 }
19}
20
21pub struct IntegrationTest {
22 instance: RealmInstance,
23}
24
25impl IntegrationTest {
26 pub async fn start(part: usize, options: TestOptions) -> Result<Self, Error> {
27 let builder = RealmBuilder::new().await?;
28 let reverser = builder
29 .add_child("reverser", format!("#meta/part_{}.cm", part), ChildOptions::new())
30 .await?;
31 if options.include_fizzbuzz {
32 let fizzbuzz = builder.add_child("fizzbuzz", FIZZBUZZ_URL, ChildOptions::new()).await?;
33 builder
34 .add_route(
35 Route::new()
36 .capability(Capability::protocol_by_name(
37 "fuchsia.examples.inspect.FizzBuzz",
38 ))
39 .from(&fizzbuzz)
40 .to(&reverser),
41 )
42 .await?;
43 builder
44 .add_route(
45 Route::new()
46 .capability(Capability::protocol_by_name("fuchsia.logger.LogSink"))
47 .from(Ref::parent())
48 .to(&fizzbuzz),
49 )
50 .await?;
51 }
52 builder
53 .add_route(
54 Route::new()
55 .capability(Capability::protocol::<ReverserMarker>())
56 .from(&reverser)
57 .to(Ref::parent()),
58 )
59 .await?;
60 builder
61 .add_route(
62 Route::new()
63 .capability(Capability::protocol_by_name("fuchsia.logger.LogSink"))
64 .from(Ref::parent())
65 .to(&reverser),
66 )
67 .await?;
68 let instance = builder.build().await?;
69 Ok(Self { instance })
70 }
71
72 pub fn connect_to_reverser(&self) -> Result<ReverserProxy, Error> {
73 self.instance.root.connect_to_protocol_at_exposed_dir::<ReverserMarker>()
74 }
75
76 pub fn reverser_moniker_for_selectors(&self) -> String {
77 format!("realm_builder\\:{}/reverser", self.instance.root.child_name())
78 }
79}