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
#![deny(missing_docs)]
use {
fidl::endpoints::{RequestStream, ServerEnd},
fidl_fuchsia_hardware_display::{self as display, ControllerMarker, ControllerRequestStream},
fuchsia_zircon as zx,
itertools::Itertools,
std::collections::HashMap,
thiserror::Error,
};
#[derive(Error, Debug)]
pub enum MockControllerError {
#[error("duplicate IDs provided")]
DuplicateIds,
#[error("FIDL error: {0}")]
FidlError(#[from] fidl::Error),
}
pub type Result<T> = std::result::Result<T, MockControllerError>;
pub struct MockController {
#[allow(unused)]
stream: ControllerRequestStream,
control_handle: <ControllerRequestStream as RequestStream>::ControlHandle,
displays: HashMap<DisplayId, display::Info>,
}
#[derive(Eq, Hash, Ord, PartialOrd, PartialEq)]
struct DisplayId(u64);
impl MockController {
pub fn new(server_end: ServerEnd<ControllerMarker>) -> Result<MockController> {
let (stream, control_handle) = server_end.into_stream_and_control_handle()?;
Ok(MockController { stream, control_handle, displays: HashMap::new() })
}
pub fn assign_displays(&mut self, displays: Vec<display::Info>) -> Result<()> {
let mut added = HashMap::new();
if !displays.into_iter().all(|info| added.insert(DisplayId(info.id), info).is_none()) {
return Err(MockControllerError::DuplicateIds);
}
let removed: Vec<u64> = self.displays.iter().map(|(_, info)| info.id).collect();
self.displays = added;
self.control_handle.send_on_displays_changed(
&mut self.displays.iter_mut().sorted().map(|(_, info)| info),
&removed,
)?;
Ok(())
}
pub fn emit_vsync_event(&self, display_id: u64, mut stamp: display::ConfigStamp) -> Result<()> {
self.control_handle
.send_on_vsync(display_id, zx::Time::get_monotonic().into_nanos() as u64, &mut stamp, 0)
.map_err(MockControllerError::from)
}
}
pub fn create_proxy_and_mock() -> Result<(display::ControllerProxy, MockController)> {
let (proxy, server) = fidl::endpoints::create_proxy::<ControllerMarker>()?;
Ok((proxy, MockController::new(server)?))
}
#[cfg(test)]
mod tests {
use super::*;
use {
anyhow::{Context, Result},
fidl_fuchsia_hardware_display as display,
futures::{future, TryStreamExt},
};
async fn wait_for_displays_changed_event(
events: &mut display::ControllerEventStream,
) -> Result<(Vec<display::Info>, Vec<u64>)> {
let mut stream = events.try_filter_map(|event| match event {
display::ControllerEvent::OnDisplaysChanged { added, removed } => {
future::ok(Some((added, removed)))
}
_ => future::ok(None),
});
stream.try_next().await?.context("failed to listen to controller events")
}
#[fuchsia::test]
async fn assign_displays_fails_with_duplicate_display_ids() {
let displays = vec![
display::Info {
id: 1,
modes: Vec::new(),
pixel_format: Vec::new(),
cursor_configs: Vec::new(),
manufacturer_name: "Foo".to_string(),
monitor_name: "what".to_string(),
monitor_serial: "".to_string(),
horizontal_size_mm: 0,
vertical_size_mm: 0,
using_fallback_size: false,
},
display::Info {
id: 1,
modes: Vec::new(),
pixel_format: Vec::new(),
cursor_configs: Vec::new(),
manufacturer_name: "Bar".to_string(),
monitor_name: "who".to_string(),
monitor_serial: "".to_string(),
horizontal_size_mm: 0,
vertical_size_mm: 0,
using_fallback_size: false,
},
];
let (_proxy, mut mock) = create_proxy_and_mock().expect("failed to create MockController");
let result = mock.assign_displays(displays);
assert!(result.is_err());
}
#[fuchsia::test]
async fn assign_displays_displays_added() -> Result<()> {
let displays = vec![
display::Info {
id: 1,
modes: Vec::new(),
pixel_format: Vec::new(),
cursor_configs: Vec::new(),
manufacturer_name: "Foo".to_string(),
monitor_name: "what".to_string(),
monitor_serial: "".to_string(),
horizontal_size_mm: 0,
vertical_size_mm: 0,
using_fallback_size: false,
},
display::Info {
id: 2,
modes: Vec::new(),
pixel_format: Vec::new(),
cursor_configs: Vec::new(),
manufacturer_name: "Bar".to_string(),
monitor_name: "who".to_string(),
monitor_serial: "".to_string(),
horizontal_size_mm: 0,
vertical_size_mm: 0,
using_fallback_size: false,
},
];
let (proxy, mut mock) = create_proxy_and_mock().expect("failed to create MockController");
mock.assign_displays(displays.clone())?;
let mut events = proxy.take_event_stream();
let (added, removed) = wait_for_displays_changed_event(&mut events).await?;
assert_eq!(added, displays);
assert_eq!(removed, vec![]);
Ok(())
}
#[fuchsia::test]
async fn assign_displays_displays_removed() -> Result<()> {
let displays = vec![display::Info {
id: 1,
modes: Vec::new(),
pixel_format: Vec::new(),
cursor_configs: Vec::new(),
manufacturer_name: "Foo".to_string(),
monitor_name: "what".to_string(),
monitor_serial: "".to_string(),
horizontal_size_mm: 0,
vertical_size_mm: 0,
using_fallback_size: false,
}];
let (proxy, mut mock) = create_proxy_and_mock().expect("failed to create MockController");
mock.assign_displays(displays)?;
let mut events = proxy.take_event_stream();
let _ = wait_for_displays_changed_event(&mut events).await?;
mock.assign_displays(vec![])?;
let (added, removed) = wait_for_displays_changed_event(&mut events).await?;
assert_eq!(added, vec![]);
assert_eq!(removed, vec![1]);
Ok(())
}
}