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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
// 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 crate::input_device::{self, InputEvent};
use crate::input_handler::InputHandlerStatus;
use crate::keyboard_binding::{KeyboardDeviceDescriptor, KeyboardEvent};
use anyhow::{Context, Result};
use fidl_fuchsia_ui_composition_internal as fcomp;
use fidl_fuchsia_ui_input3::KeyEventType;
use fuchsia_async::{OnSignals, Task};
use fuchsia_inspect::health::Reporter;
use fuchsia_zircon::{AsHandleRef, Duration, Signals, Status, Time};
use futures::{
    channel::mpsc::{self, UnboundedReceiver, UnboundedSender},
    select, StreamExt,
};
use keymaps::KeyState;
use lazy_static::lazy_static;
use std::{cell::RefCell, rc::Rc};

lazy_static! {
    // The signal value corresponding to the `DISPLAY_OWNED_SIGNAL`.  Same as zircon's signal
    // USER_0.
    static ref DISPLAY_OWNED: Signals = Signals::from_bits(fcomp::SIGNAL_DISPLAY_OWNED)
        .expect("static init should not fail")    ;

    // The signal value corresponding to the `DISPLAY_NOT_OWNED_SIGNAL`.  Same as zircon's signal
    // USER_1.
    static ref DISPLAY_UNOWNED: Signals = Signals::from_bits(fcomp::SIGNAL_DISPLAY_NOT_OWNED)
        .expect("static init should not fail")    ;

    // Any display-related signal.
    static ref ANY_DISPLAY_EVENT: Signals = *DISPLAY_OWNED | *DISPLAY_UNOWNED;
}

// Stores the last received ownership signals.
#[derive(Debug, Clone, PartialEq)]
struct Ownership {
    signals: Signals,
}

impl std::convert::From<Signals> for Ownership {
    fn from(signals: Signals) -> Self {
        Ownership { signals }
    }
}

impl Ownership {
    // Returns true if the display is currently indicated to be not owned by
    // Scenic.
    fn is_display_ownership_lost(&self) -> bool {
        self.signals.contains(*DISPLAY_UNOWNED)
    }

    // Returns the mask of the next signal to watch.
    //
    // Since the ownership alternates, so does the next signal to wait on.
    fn next_signal(&self) -> Signals {
        match self.is_display_ownership_lost() {
            true => *DISPLAY_OWNED,
            false => *DISPLAY_UNOWNED,
        }
    }

    /// Waits for the next signal change.
    ///
    /// If the display is owned, it will wait for display to become unowned.
    /// If the display is unowned, it will wait for the display to become owned.
    async fn wait_ownership_change<'a, T: AsHandleRef>(
        &self,
        event: &'a T,
    ) -> Result<Signals, Status> {
        OnSignals::new(event, self.next_signal()).await
    }
}

/// A handler that turns the input pipeline off or on based on whether
/// the Scenic owns the display.
///
/// This allows us to turn off keyboard processing when the user switches away
/// from the product (e.g. terminal) into virtual console.
///
/// See the `README.md` file in this crate for details.
pub struct DisplayOwnership {
    /// The current view of the display ownership.  It is mutated by the
    /// display ownership task when appropriate signals arrive.
    ownership: Rc<RefCell<Ownership>>,

    /// The registry of currently pressed keys.
    key_state: RefCell<KeyState>,

    /// The source of ownership change events for the main loop.
    display_ownership_change_receiver: RefCell<UnboundedReceiver<Ownership>>,

    /// A background task that watches for display ownership changes.  We keep
    /// it alive to ensure that it keeps running.
    _display_ownership_task: Task<()>,

    /// The inventory of this handler's Inspect status.
    inspect_status: InputHandlerStatus,

    /// The event processing loop will do an `unbounded_send(())` on this
    /// channel once at the end of each loop pass, in test configurations only.
    /// The test fixture uses this channel to execute test fixture in
    /// lock-step with the event processing loop for test cases where the
    /// precise event sequencing is relevant.
    #[cfg(test)]
    loop_done: RefCell<Option<UnboundedSender<()>>>,
}

impl DisplayOwnership {
    /// Creates a new handler that watches `display_ownership_event` for events.
    ///
    /// The `display_ownership_event` is assumed to be an [Event] obtained from
    /// Scenic using `fuchsia.ui.scenic.Scenic/GetDisplayOwnershipEvent`.  There
    /// isn't really a way for this code to know here whether this is true or
    /// not, so implementor beware.
    pub fn new(
        display_ownership_event: impl AsHandleRef + 'static,
        input_handlers_node: &fuchsia_inspect::Node,
    ) -> Rc<Self> {
        DisplayOwnership::new_internal(display_ownership_event, None, input_handlers_node)
    }

    #[cfg(test)]
    pub fn new_for_test(
        display_ownership_event: impl AsHandleRef + 'static,
        loop_done: UnboundedSender<()>,
    ) -> Rc<Self> {
        let inspector = fuchsia_inspect::Inspector::default();
        let fake_handlers_node = inspector.root().create_child("input_handlers_node");
        DisplayOwnership::new_internal(
            display_ownership_event,
            Some(loop_done),
            &fake_handlers_node,
        )
    }

    fn new_internal(
        display_ownership_event: impl AsHandleRef + 'static,
        _loop_done: Option<UnboundedSender<()>>,
        input_handlers_node: &fuchsia_inspect::Node,
    ) -> Rc<Self> {
        let initial_state = display_ownership_event
            // scenic guarantees that ANY_DISPLAY_EVENT is asserted. If it is
            // not, this will fail with a timeout error.
            .wait_handle(*ANY_DISPLAY_EVENT, Time::INFINITE_PAST)
            .expect("unable to set the initial display state");
        tracing::debug!("setting initial display ownership to: {:?}", &initial_state);
        let initial_ownership: Ownership = initial_state.into();
        let ownership = Rc::new(RefCell::new(initial_ownership.clone()));

        let mut ownership_clone = initial_ownership.clone();
        let (ownership_sender, ownership_receiver) = mpsc::unbounded();
        let display_ownership_task = Task::local(async move {
            loop {
                let signals = ownership_clone.wait_ownership_change(&display_ownership_event).await;
                match signals {
                    Err(e) => {
                        tracing::warn!("could not read display state: {:?}", e);
                        break;
                    }
                    Ok(signals) => {
                        tracing::debug!("setting display ownership to: {:?}", &signals);
                        ownership_sender.unbounded_send(signals.into()).unwrap();
                        ownership_clone = signals.into();
                    }
                }
            }
            tracing::warn!("display loop exiting and will no longer monitor display changes - this is not expected");
        });
        tracing::info!("Display ownership handler installed");
        let inspect_status = InputHandlerStatus::new(
            input_handlers_node,
            "display_ownership",
            /* generates_events */ false,
        );
        Rc::new(Self {
            ownership,
            key_state: RefCell::new(KeyState::new()),
            display_ownership_change_receiver: RefCell::new(ownership_receiver),
            _display_ownership_task: display_ownership_task,
            inspect_status,
            #[cfg(test)]
            loop_done: RefCell::new(_loop_done),
        })
    }

    /// Returns true if the display is currently *not* owned by Scenic.
    fn is_display_ownership_lost(&self) -> bool {
        self.ownership.borrow().is_display_ownership_lost()
    }

    /// Run this function in an executor to handle events.
    pub async fn handle_input_events(
        self: &Rc<Self>,
        mut input: UnboundedReceiver<InputEvent>,
        output: UnboundedSender<InputEvent>,
    ) -> Result<()> {
        loop {
            let mut ownership_source = self.display_ownership_change_receiver.borrow_mut();
            select! {
                // Display ownership changed.
                new_ownership = ownership_source.select_next_some() => {
                    let is_display_ownership_lost = new_ownership.is_display_ownership_lost();
                    // When the ownership is modified, float a set of cancel or sync
                    // events to scoop up stale keyboard state, treating it the same
                    // as loss of focus.
                    let event_type = match is_display_ownership_lost {
                        true => KeyEventType::Cancel,
                        false => KeyEventType::Sync,
                    };
                    let keys = self.key_state.borrow().get_set();
                    let mut event_time = Time::get_monotonic();
                    for key in keys.into_iter() {
                        let key_event = KeyboardEvent::new(key, event_type);
                        output.unbounded_send(into_input_event(key_event, event_time))
                            .context("unable to send display updates")?;
                        event_time = event_time + Duration::from_nanos(1);
                    }
                    *(self.ownership.borrow_mut()) = new_ownership;
                },

                // An input event arrived.
                event = input.select_next_some() => {
                    if event.is_handled() {
                        // Forward handled events unmodified.
                        output.unbounded_send(event).context("unable to send handled event")?;
                        continue;
                    }
                    self.inspect_status.count_received_event(input_device::InputEvent::from(event.clone()));
                    match event.device_event {
                        input_device::InputDeviceEvent::Keyboard(ref e) => {
                            self.key_state.borrow_mut().update(e.get_event_type(), e.get_key());
                        },
                        _ => {},
                    }
                    let is_display_ownership_lost = self.is_display_ownership_lost();
                    if is_display_ownership_lost {
                        self.inspect_status.count_handled_event();
                    }
                    output.unbounded_send(
                        input_device::InputEvent::from(event)
                            .into_handled_if(is_display_ownership_lost)
                    ).context("unable to send input event updates")?;
                },
            };
            #[cfg(test)]
            {
                self.loop_done.borrow_mut().as_ref().unwrap().unbounded_send(()).unwrap();
            }
        }
    }

    pub fn set_handler_healthy(self: std::rc::Rc<Self>) {
        self.inspect_status.health_node.borrow_mut().set_ok();
    }

    pub fn set_handler_unhealthy(self: std::rc::Rc<Self>, msg: &str) {
        self.inspect_status.health_node.borrow_mut().set_unhealthy(msg);
    }
}

fn empty_keyboard_device_descriptor() -> input_device::InputDeviceDescriptor {
    input_device::InputDeviceDescriptor::Keyboard(
        // Should descriptor be something sensible?
        KeyboardDeviceDescriptor {
            keys: vec![],
            device_info: fidl_fuchsia_input_report::DeviceInfo {
                vendor_id: 0,
                product_id: 0,
                version: 0,
                polling_rate: 0,
            },
            device_id: 0,
        },
    )
}

fn into_input_event(keyboard_event: KeyboardEvent, event_time: Time) -> input_device::InputEvent {
    input_device::InputEvent {
        device_event: input_device::InputDeviceEvent::Keyboard(keyboard_event),
        device_descriptor: empty_keyboard_device_descriptor(),
        event_time,
        handled: input_device::Handled::No,
        trace_id: None,
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::testing_utilities::{create_fake_input_event, create_input_event};
    use fidl_fuchsia_input::Key;
    use fuchsia_async as fasync;
    use fuchsia_zircon::{EventPair, Peered};
    use pretty_assertions::assert_eq;

    // Manages losing and regaining display, since manual management is error-prone:
    // if signal_peer does not change the signal state, the waiting process will block
    // forever, which makes tests run longer than needed.
    struct DisplayWrangler {
        event: EventPair,
        last: Signals,
    }

    impl DisplayWrangler {
        fn new(event: EventPair) -> Self {
            let mut instance = DisplayWrangler { event, last: *DISPLAY_OWNED };
            // Signal needs to be initialized before the handlers attempts to read it.
            // This is normally always the case in production.
            // Else, the `new_for_test` below will panic with a TIMEOUT error.
            instance.set_unowned();
            instance
        }

        fn set_unowned(&mut self) {
            assert!(self.last != *DISPLAY_UNOWNED, "display is already unowned");
            self.event.signal_peer(*DISPLAY_OWNED, *DISPLAY_UNOWNED).unwrap();
            self.last = *DISPLAY_UNOWNED;
        }

        fn set_owned(&mut self) {
            assert!(self.last != *DISPLAY_OWNED, "display is already owned");
            self.event.signal_peer(*DISPLAY_UNOWNED, *DISPLAY_OWNED).unwrap();
            self.last = *DISPLAY_OWNED;
        }
    }

    #[fuchsia::test]
    async fn display_ownership_change() {
        // handler_event is the event that the unit under test will examine for
        // display ownership changes.  test_event is used to set the appropriate
        // signals.
        let (test_event, handler_event) = EventPair::create();

        // test_sender is used to pipe input events into the handler.
        let (test_sender, handler_receiver) = mpsc::unbounded::<InputEvent>();

        // test_receiver is used to pipe input events out of the handler.
        let (handler_sender, test_receiver) = mpsc::unbounded::<InputEvent>();

        // The unit under test adds a () each time it completes one pass through
        // its event loop.  Use to ensure synchronization.
        let (loop_done_sender, mut loop_done) = mpsc::unbounded::<()>();

        // We use a wrapper to signal test_event correctly, since doing it wrong
        // by hand causes tests to hang, which isn't the best dev experience.
        let mut wrangler = DisplayWrangler::new(test_event);
        let handler = DisplayOwnership::new_for_test(handler_event, loop_done_sender);

        let _task = fasync::Task::local(async move {
            handler.handle_input_events(handler_receiver, handler_sender).await.unwrap();
        });

        let fake_time = Time::from_nanos(42);

        // Go two full circles of signaling.

        // 1
        wrangler.set_owned();
        loop_done.next().await;
        test_sender.unbounded_send(create_fake_input_event(fake_time)).unwrap();
        loop_done.next().await;

        // 2
        wrangler.set_unowned();
        loop_done.next().await;
        test_sender.unbounded_send(create_fake_input_event(fake_time)).unwrap();
        loop_done.next().await;

        // 3
        wrangler.set_owned();
        loop_done.next().await;
        test_sender.unbounded_send(create_fake_input_event(fake_time)).unwrap();
        loop_done.next().await;

        // 4
        wrangler.set_unowned();
        loop_done.next().await;
        test_sender.unbounded_send(create_fake_input_event(fake_time)).unwrap();
        loop_done.next().await;

        let actual: Vec<InputEvent> =
            test_receiver.take(4).map(|e| e.into_with_event_time(fake_time)).collect().await;

        assert_eq!(
            actual,
            vec![
                // Event received while we owned the display.
                create_fake_input_event(fake_time),
                // Event received when we lost the display.
                create_fake_input_event(fake_time).into_handled(),
                // Display ownership regained.
                create_fake_input_event(fake_time),
                // Display ownership lost.
                create_fake_input_event(fake_time).into_handled(),
            ]
        );
    }

    fn new_keyboard_input_event(key: Key, event_type: KeyEventType) -> InputEvent {
        let fake_time = Time::from_nanos(42);
        create_input_event(
            KeyboardEvent::new(key, event_type),
            &input_device::InputDeviceDescriptor::Fake,
            fake_time,
            input_device::Handled::No,
        )
    }

    #[fuchsia::test]
    async fn basic_key_state_handling() {
        let (test_event, handler_event) = EventPair::create();
        let (test_sender, handler_receiver) = mpsc::unbounded::<InputEvent>();
        let (handler_sender, test_receiver) = mpsc::unbounded::<InputEvent>();
        let (loop_done_sender, mut loop_done) = mpsc::unbounded::<()>();
        let mut wrangler = DisplayWrangler::new(test_event);
        let handler = DisplayOwnership::new_for_test(handler_event, loop_done_sender);
        let _task = fasync::Task::local(async move {
            handler.handle_input_events(handler_receiver, handler_sender).await.unwrap();
        });

        let fake_time = Time::from_nanos(42);

        // Gain the display, and press a key.
        wrangler.set_owned();
        loop_done.next().await;
        test_sender
            .unbounded_send(new_keyboard_input_event(Key::A, KeyEventType::Pressed))
            .unwrap();
        loop_done.next().await;

        // Lose display.
        wrangler.set_unowned();
        loop_done.next().await;

        // Regain display
        wrangler.set_owned();
        loop_done.next().await;

        // Key event after regaining.
        test_sender
            .unbounded_send(new_keyboard_input_event(Key::A, KeyEventType::Released))
            .unwrap();
        loop_done.next().await;

        let actual: Vec<InputEvent> =
            test_receiver.take(4).map(|e| e.into_with_event_time(fake_time)).collect().await;

        assert_eq!(
            actual,
            vec![
                new_keyboard_input_event(Key::A, KeyEventType::Pressed),
                new_keyboard_input_event(Key::A, KeyEventType::Cancel)
                    .into_with_device_descriptor(empty_keyboard_device_descriptor()),
                new_keyboard_input_event(Key::A, KeyEventType::Sync)
                    .into_with_device_descriptor(empty_keyboard_device_descriptor()),
                new_keyboard_input_event(Key::A, KeyEventType::Released),
            ]
        );
    }

    #[fuchsia::test]
    async fn more_key_state_handling() {
        let (test_event, handler_event) = EventPair::create();
        let (test_sender, handler_receiver) = mpsc::unbounded::<InputEvent>();
        let (handler_sender, test_receiver) = mpsc::unbounded::<InputEvent>();
        let (loop_done_sender, mut loop_done) = mpsc::unbounded::<()>();
        let mut wrangler = DisplayWrangler::new(test_event);
        let handler = DisplayOwnership::new_for_test(handler_event, loop_done_sender);
        let _task = fasync::Task::local(async move {
            handler.handle_input_events(handler_receiver, handler_sender).await.unwrap();
        });

        let fake_time = Time::from_nanos(42);

        wrangler.set_owned();
        loop_done.next().await;
        test_sender
            .unbounded_send(new_keyboard_input_event(Key::A, KeyEventType::Pressed))
            .unwrap();
        loop_done.next().await;
        test_sender
            .unbounded_send(new_keyboard_input_event(Key::B, KeyEventType::Pressed))
            .unwrap();
        loop_done.next().await;

        // Lose display, release a key, press a key.
        wrangler.set_unowned();
        loop_done.next().await;
        test_sender
            .unbounded_send(new_keyboard_input_event(Key::B, KeyEventType::Released))
            .unwrap();
        loop_done.next().await;
        test_sender
            .unbounded_send(new_keyboard_input_event(Key::C, KeyEventType::Pressed))
            .unwrap();
        loop_done.next().await;

        // Regain display
        wrangler.set_owned();
        loop_done.next().await;

        // Key event after regaining.
        test_sender
            .unbounded_send(new_keyboard_input_event(Key::A, KeyEventType::Released))
            .unwrap();
        loop_done.next().await;
        test_sender
            .unbounded_send(new_keyboard_input_event(Key::C, KeyEventType::Released))
            .unwrap();
        loop_done.next().await;

        let actual: Vec<InputEvent> =
            test_receiver.take(10).map(|e| e.into_with_event_time(fake_time)).collect().await;

        assert_eq!(
            actual,
            vec![
                new_keyboard_input_event(Key::A, KeyEventType::Pressed),
                new_keyboard_input_event(Key::B, KeyEventType::Pressed),
                new_keyboard_input_event(Key::A, KeyEventType::Cancel)
                    .into_with_device_descriptor(empty_keyboard_device_descriptor()),
                new_keyboard_input_event(Key::B, KeyEventType::Cancel)
                    .into_with_device_descriptor(empty_keyboard_device_descriptor()),
                new_keyboard_input_event(Key::B, KeyEventType::Released).into_handled(),
                new_keyboard_input_event(Key::C, KeyEventType::Pressed).into_handled(),
                // The CANCEL and SYNC events are emitted in the sort ordering of the
                // `Key` enum values. Perhaps they should be emitted instead in the order
                // they have been received for SYNC, and in reverse order for CANCEL.
                new_keyboard_input_event(Key::A, KeyEventType::Sync)
                    .into_with_device_descriptor(empty_keyboard_device_descriptor()),
                new_keyboard_input_event(Key::C, KeyEventType::Sync)
                    .into_with_device_descriptor(empty_keyboard_device_descriptor()),
                new_keyboard_input_event(Key::A, KeyEventType::Released),
                new_keyboard_input_event(Key::C, KeyEventType::Released),
            ]
        );
    }

    #[fuchsia::test]
    async fn display_ownership_initialized_with_inspect_node() {
        let (test_event, handler_event) = EventPair::create();
        let (loop_done_sender, _) = mpsc::unbounded::<()>();
        let inspector = fuchsia_inspect::Inspector::default();
        let fake_handlers_node = inspector.root().create_child("input_handlers_node");
        // Signal needs to be initialized first so DisplayOwnership::new doesn't panic with a TIMEOUT error
        let _ = DisplayWrangler::new(test_event);
        let _handler = DisplayOwnership::new_internal(
            handler_event,
            Some(loop_done_sender),
            &fake_handlers_node,
        );
        diagnostics_assertions::assert_data_tree!(inspector, root: {
            input_handlers_node: {
                display_ownership: {
                    events_received_count: 0u64,
                    events_handled_count: 0u64,
                    last_received_timestamp_ns: 0u64,
                    "fuchsia.inspect.Health": {
                        status: "STARTING_UP",
                        // Timestamp value is unpredictable and not relevant in this context,
                        // so we only assert that the property is present.
                        start_timestamp_nanos: diagnostics_assertions::AnyProperty
                    },
                }
            }
        });
    }

    #[fuchsia::test]
    async fn display_ownership_inspect_counts_events() {
        let (test_event, handler_event) = EventPair::create();
        let (test_sender, handler_receiver) = mpsc::unbounded::<InputEvent>();
        let (handler_sender, _test_receiver) = mpsc::unbounded::<InputEvent>();
        let (loop_done_sender, mut loop_done) = mpsc::unbounded::<()>();
        let mut wrangler = DisplayWrangler::new(test_event);
        let inspector = fuchsia_inspect::Inspector::default();
        let fake_handlers_node = inspector.root().create_child("input_handlers_node");
        let handler = DisplayOwnership::new_internal(
            handler_event,
            Some(loop_done_sender),
            &fake_handlers_node,
        );
        let _task = fasync::Task::local(async move {
            handler.handle_input_events(handler_receiver, handler_sender).await.unwrap();
        });

        // Gain the display, and press a key.
        wrangler.set_owned();
        loop_done.next().await;
        test_sender
            .unbounded_send(new_keyboard_input_event(Key::A, KeyEventType::Pressed))
            .unwrap();
        loop_done.next().await;

        // Lose display
        // Input event is marked `Handled` if received after display ownership is lost
        wrangler.set_unowned();
        loop_done.next().await;
        test_sender
            .unbounded_send(new_keyboard_input_event(Key::B, KeyEventType::Pressed))
            .unwrap();
        loop_done.next().await;

        // Regain display
        wrangler.set_owned();
        loop_done.next().await;

        // Key event after regaining.
        test_sender
            .unbounded_send(new_keyboard_input_event(Key::A, KeyEventType::Released))
            .unwrap();
        loop_done.next().await;

        diagnostics_assertions::assert_data_tree!(inspector, root: {
            input_handlers_node: {
                display_ownership: {
                    events_received_count: 3u64,
                    events_handled_count: 1u64,
                    last_received_timestamp_ns: 42u64,
                    "fuchsia.inspect.Health": {
                        status: "STARTING_UP",
                        // Timestamp value is unpredictable and not relevant in this context,
                        // so we only assert that the property is present.
                        start_timestamp_nanos: diagnostics_assertions::AnyProperty
                    },
                }
            }
        });
    }
}