common/
common.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
// Copyright 2022 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use anyhow::Error;
use async_trait::async_trait;
use fidl_fuchsia_settings::*;
use fidl_fuchsia_ui_brightness::{ControlRequest, ControlRequestStream};
use fuchsia_async as fasync;
use fuchsia_component::server::{ServiceFs, ServiceFsDir};
use fuchsia_component_test::{
    Capability, ChildOptions, LocalComponentHandles, RealmBuilder, RealmInstance, Ref, Route,
};
use futures::channel::mpsc::Sender;
use futures::lock::Mutex;
use futures::{SinkExt, StreamExt, TryStreamExt};
use std::sync::atomic::{AtomicU32, Ordering};
use std::sync::Arc;

const COMPONENT_URL: &str = "#meta/setui_service.cm";

#[derive(PartialEq, Debug)]
pub enum Request {
    SetAutoBrightness,
    SetManualBrightness,
}

#[async_trait]
pub trait Mocks {
    async fn brightness_service_impl(
        handles: LocalComponentHandles,
        manual_brightness: Arc<Mutex<Option<f32>>>,
        auto_brightness: Arc<Mutex<Option<bool>>>,
        num_changes: Arc<AtomicU32>,
        requests_sender: Sender<Request>,
    ) -> Result<(), Error>;
}

#[async_trait]
impl Mocks for DisplayTest {
    // Mock the brightness dependency and verify the settings service interacts with the brightness
    // service by checking input value changes through the various requests.
    async fn brightness_service_impl(
        handles: LocalComponentHandles,
        manual_brightness: Arc<Mutex<Option<f32>>>,
        auto_brightness: Arc<Mutex<Option<bool>>>,
        num_changes: Arc<AtomicU32>,
        requests_sender: Sender<Request>,
    ) -> Result<(), Error> {
        let mut fs = ServiceFs::new();
        let _: &mut ServiceFsDir<'_, _> =
            fs.dir("svc").add_fidl_service(move |mut stream: ControlRequestStream| {
                let auto_brightness_handle = auto_brightness.clone();
                let brightness_handle = manual_brightness.clone();
                let num_changes_handle = num_changes.clone();
                let mut requests_sender = requests_sender.clone();
                fasync::Task::spawn(async move {
                    while let Ok(Some(req)) = stream.try_next().await {
                        // Support future expansion of FIDL.
                        #[allow(unreachable_patterns)]
                        match req {
                            ControlRequest::WatchCurrentBrightness { responder } => {
                                responder
                                    .send(
                                        brightness_handle
                                            .lock()
                                            .await
                                            .expect("brightness not yet set"),
                                    )
                                    .unwrap();
                            }
                            ControlRequest::SetAutoBrightness { control_handle: _ } => {
                                *auto_brightness_handle.lock().await = Some(true);
                                let current_num = num_changes_handle.load(Ordering::Relaxed);
                                (*num_changes_handle).store(current_num + 1, Ordering::Relaxed);
                                requests_sender
                                    .send(Request::SetAutoBrightness)
                                    .await
                                    .expect("Finished processing SetAutoBrightness call");
                            }
                            ControlRequest::SetManualBrightness { value, control_handle: _ } => {
                                *brightness_handle.lock().await = Some(value);
                                *auto_brightness_handle.lock().await = Some(false);
                                let current_num = num_changes_handle.load(Ordering::Relaxed);
                                (*num_changes_handle).store(current_num + 1, Ordering::Relaxed);
                                requests_sender
                                    .send(Request::SetManualBrightness)
                                    .await
                                    .expect("Finished processing SetManualBrightness call");
                            }
                            ControlRequest::WatchAutoBrightness { responder } => {
                                responder
                                    .send(auto_brightness_handle.lock().await.unwrap_or(false))
                                    .unwrap();
                            }
                            _ => {}
                        }
                    }
                })
                .detach();
            });
        let _: &mut ServiceFs<_> = fs.serve_connection(handles.outgoing_dir).unwrap();
        fs.collect::<()>().await;
        Ok(())
    }
}

pub struct DisplayTest {}

impl DisplayTest {
    // This function creates a realm without external brightness dependency, and is used for tests
    // in the internal_display_integration_test package.
    pub async fn create_realm() -> Result<RealmInstance, Error> {
        let builder = RealmBuilder::new().await?;
        // Add setui_service as child of the realm builder.
        let setui_service =
            builder.add_child("setui_service", COMPONENT_URL, ChildOptions::new()).await?;
        let info = utils::SettingsRealmInfo {
            builder,
            settings: &setui_service,
            has_config_data: true,
            capabilities: vec!["fuchsia.settings.Display"],
        };
        // Add basic Settings service realm information.
        utils::create_realm_basic(&info).await?;
        let instance = info.builder.build().await?;
        Ok(instance)
    }

    // This function creates a realm with external brightness dependency, and is used for tests in
    // the display_integration_test package.
    pub async fn create_realm_with_brightness_controller(
        manual_brightness: Arc<Mutex<Option<f32>>>,
        auto_brightness: Arc<Mutex<Option<bool>>>,
        num_changes: Arc<AtomicU32>,
        requests_sender: Sender<Request>,
    ) -> Result<RealmInstance, Error> {
        let builder = RealmBuilder::new().await?;
        // Add setui_service as child of the realm builder.
        let setui_service =
            builder.add_child("setui_service", COMPONENT_URL, ChildOptions::new()).await?;
        let info = utils::SettingsRealmInfo {
            builder,
            settings: &setui_service,
            has_config_data: true,
            capabilities: vec!["fuchsia.settings.Display"],
        };
        // Add basic Settings service realm information.
        utils::create_realm_basic(&info).await?;
        // Add mock camera dependency to test Display service with camera.
        let brightness_service = info
            .builder
            .add_local_child(
                "brightness_service",
                move |handles: LocalComponentHandles| {
                    Box::pin(DisplayTest::brightness_service_impl(
                        handles,
                        Arc::clone(&manual_brightness),
                        Arc::clone(&auto_brightness),
                        Arc::clone(&num_changes),
                        requests_sender.clone(),
                    ))
                },
                ChildOptions::new().eager(),
            )
            .await?;
        info.builder
            .add_route(
                Route::new()
                    .capability(Capability::protocol_by_name("fuchsia.ui.brightness.Control"))
                    .from(&brightness_service)
                    .to(Ref::parent())
                    .to(&setui_service),
            )
            .await?;
        // Provide LogSink to print out logs of the camera component for debugging purpose.
        info.builder
            .add_route(
                Route::new()
                    .capability(Capability::protocol_by_name("fuchsia.logger.LogSink"))
                    .from(Ref::parent())
                    .to(&brightness_service),
            )
            .await?;
        let instance = info.builder.build().await?;
        Ok(instance)
    }

    pub fn connect_to_displaymarker(instance: &RealmInstance) -> DisplayProxy {
        return instance
            .root
            .connect_to_protocol_at_exposed_dir::<DisplayMarker>()
            .expect("connecting to Display");
    }

    pub fn get_init_manual_brightness() -> Arc<Mutex<Option<f32>>> {
        Arc::new(Mutex::new(None))
    }

    pub fn get_init_auto_brightness() -> Arc<Mutex<Option<bool>>> {
        Arc::new(Mutex::new(None))
    }

    pub fn get_init_num_changes() -> Arc<AtomicU32> {
        Arc::new(AtomicU32::new(0))
    }

    // This is for tests that testing screen enabled fields. It is used for both test packages.
    pub async fn test_screen_enabled(display_proxy: DisplayProxy) {
        // Test that if screen is turned off, it is reflected.
        let mut display_settings = DisplaySettings::default();
        display_settings.auto_brightness = Some(false);
        display_proxy.set(&display_settings).await.expect("set completed").expect("set successful");

        let mut display_settings = DisplaySettings::default();
        display_settings.screen_enabled = Some(false);
        display_proxy.set(&display_settings).await.expect("set completed").expect("set successful");

        let settings = display_proxy.watch().await.expect("watch completed");

        assert_eq!(settings.screen_enabled, Some(false));

        // Test that if display is turned back on, the display and manual brightness are on.
        let mut display_settings = DisplaySettings::default();
        display_settings.screen_enabled = Some(true);
        display_proxy.set(&display_settings).await.expect("set completed").expect("set successful");

        let settings = display_proxy.watch().await.expect("watch completed");

        assert_eq!(settings.screen_enabled, Some(true));
        assert_eq!(settings.auto_brightness, Some(false));

        // Test that if auto brightness is turned on, the display and auto brightness are on.
        let mut display_settings = DisplaySettings::default();
        display_settings.auto_brightness = Some(true);
        display_proxy.set(&display_settings).await.expect("set completed").expect("set successful");

        let settings = display_proxy.watch().await.expect("watch completed");

        assert_eq!(settings.auto_brightness, Some(true));
        assert_eq!(settings.screen_enabled, Some(true));
    }
}