aboutsummaryrefslogtreecommitdiff
path: root/tests/carservice_unit_test/src/com/android/car/hal/InputHalServiceTest.java
blob: 5235fda33c23e5e2fceebd643378f1ccc3e86315 (plain)
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
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
/*
 * Copyright (C) 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.car.hal;

import static android.car.CarOccupantZoneManager.DisplayTypeEnum;
import static android.hardware.automotive.vehicle.CustomInputType.CUSTOM_EVENT_F1;

import static com.android.car.CarServiceUtils.toIntArray;

import static com.google.common.truth.Truth.assertThat;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import android.car.CarOccupantZoneManager;
import android.car.input.CarInputManager;
import android.car.input.CustomInputEvent;
import android.car.input.RotaryEvent;
import android.hardware.automotive.vehicle.RotaryInputType;
import android.hardware.automotive.vehicle.VehicleDisplay;
import android.hardware.automotive.vehicle.VehicleHwKeyInputAction;
import android.hardware.automotive.vehicle.VehicleProperty;
import android.view.Display;
import android.view.KeyEvent;

import androidx.test.filters.RequiresDevice;

import com.android.car.hal.test.AidlVehiclePropConfigBuilder;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.function.LongSupplier;

@RunWith(MockitoJUnitRunner.class)
public class InputHalServiceTest {
    @Mock VehicleHal mVehicleHal;
    @Mock InputHalService.InputListener mInputListener;
    @Mock LongSupplier mUptimeSupplier;

    private static final HalPropConfig HW_KEY_INPUT_CONFIG = new AidlHalPropConfig(
            AidlVehiclePropConfigBuilder.newBuilder(VehicleProperty.HW_KEY_INPUT).build());
    private static final HalPropConfig HW_ROTARY_INPUT_CONFIG = new AidlHalPropConfig(
            AidlVehiclePropConfigBuilder.newBuilder(VehicleProperty.HW_ROTARY_INPUT).build());
    private static final HalPropConfig HW_CUSTOM_INPUT_CONFIG = new AidlHalPropConfig(
            AidlVehiclePropConfigBuilder.newBuilder(VehicleProperty.HW_CUSTOM_INPUT).build());

    private final HalPropValueBuilder mPropValueBuilder = new HalPropValueBuilder(/*isAidl=*/true);

    private enum Key {DOWN, UP}

    private InputHalService mInputHalService;

    @Before
    public void setUp() {
        when(mUptimeSupplier.getAsLong()).thenReturn(0L);
        mInputHalService = new InputHalService(mVehicleHal, mUptimeSupplier);
        mInputHalService.init();
    }

    @After
    public void tearDown() {
        mInputHalService.release();
        mInputHalService = null;
    }

    @Test
    public void ignoresSetListener_beforeKeyInputSupported() {
        assertThat(mInputHalService.isKeyInputSupported()).isFalse();

        mInputHalService.setInputListener(mInputListener);

        int anyDisplay = VehicleDisplay.MAIN;
        mInputHalService.onHalEvents(List.of(makeKeyPropValue(Key.DOWN, KeyEvent.KEYCODE_ENTER,
                anyDisplay)));
        verify(mInputListener, never()).onKeyEvent(any(), anyInt());
    }

    @Test
    public void takesKeyInputProperty() {
        Set<HalPropConfig> offeredProps = Set.of(new AidlHalPropConfig(
                AidlVehiclePropConfigBuilder.newBuilder(VehicleProperty.ABS_ACTIVE).build()),
                HW_KEY_INPUT_CONFIG,
                new AidlHalPropConfig(AidlVehiclePropConfigBuilder.newBuilder(
                        VehicleProperty.CURRENT_GEAR).build()));

        mInputHalService.takeProperties(offeredProps);

        assertThat(mInputHalService.isKeyInputSupported()).isTrue();
        assertThat(mInputHalService.isRotaryInputSupported()).isFalse();
        assertThat(mInputHalService.isCustomInputSupported()).isFalse();
    }

    @Test
    public void takesRotaryInputProperty() {
        Set<HalPropConfig> offeredProps = Set.of(
                new AidlHalPropConfig(AidlVehiclePropConfigBuilder.newBuilder(
                        VehicleProperty.ABS_ACTIVE).build()),
                HW_ROTARY_INPUT_CONFIG,
                new AidlHalPropConfig(AidlVehiclePropConfigBuilder.newBuilder(
                        VehicleProperty.CURRENT_GEAR).build()));

        mInputHalService.takeProperties(offeredProps);

        assertThat(mInputHalService.isRotaryInputSupported()).isTrue();
        assertThat(mInputHalService.isKeyInputSupported()).isFalse();
        assertThat(mInputHalService.isCustomInputSupported()).isFalse();
    }

    @Test
    public void takesCustomInputProperty() {
        Set<HalPropConfig> offeredProps = Set.of(
                new AidlHalPropConfig(AidlVehiclePropConfigBuilder.newBuilder(
                        VehicleProperty.ABS_ACTIVE).build()),
                HW_CUSTOM_INPUT_CONFIG,
                new AidlHalPropConfig(AidlVehiclePropConfigBuilder.newBuilder(
                        VehicleProperty.CURRENT_GEAR).build()));

        mInputHalService.takeProperties(offeredProps);

        assertThat(mInputHalService.isRotaryInputSupported()).isFalse();
        assertThat(mInputHalService.isKeyInputSupported()).isFalse();
        assertThat(mInputHalService.isCustomInputSupported()).isTrue();
    }

    @Test
    public void takesKeyAndRotaryAndCustomInputProperty() {
        Set<HalPropConfig> offeredProps = Set.of(
                new AidlHalPropConfig(AidlVehiclePropConfigBuilder.newBuilder(
                        VehicleProperty.ABS_ACTIVE).build()),
                HW_KEY_INPUT_CONFIG,
                HW_ROTARY_INPUT_CONFIG,
                HW_CUSTOM_INPUT_CONFIG,
                new AidlHalPropConfig(AidlVehiclePropConfigBuilder.newBuilder(
                        VehicleProperty.CURRENT_GEAR).build()));

        mInputHalService.takeProperties(offeredProps);

        assertThat(mInputHalService.isKeyInputSupported()).isTrue();
        assertThat(mInputHalService.isRotaryInputSupported()).isTrue();
        assertThat(mInputHalService.isCustomInputSupported()).isTrue();
    }

    @Test
    public void dispatchesInputEvent_single_toListener_mainDisplay() {
        subscribeListener();

        KeyEvent event = dispatchSingleEvent(Key.DOWN, KeyEvent.KEYCODE_ENTER, VehicleDisplay.MAIN,
                CarOccupantZoneManager.DISPLAY_TYPE_MAIN);
        assertThat(event.getAction()).isEqualTo(KeyEvent.ACTION_DOWN);
        assertThat(event.getKeyCode()).isEqualTo(KeyEvent.KEYCODE_ENTER);
    }

    @Test
    public void dispatchesInputEvent_single_toListener_clusterDisplay() {
        subscribeListener();

        KeyEvent event = dispatchSingleEvent(Key.DOWN, KeyEvent.KEYCODE_ENTER,
                VehicleDisplay.INSTRUMENT_CLUSTER,
                CarOccupantZoneManager.DISPLAY_TYPE_INSTRUMENT_CLUSTER);
        assertThat(event.getAction()).isEqualTo(KeyEvent.ACTION_DOWN);
        assertThat(event.getKeyCode()).isEqualTo(KeyEvent.KEYCODE_ENTER);
    }

    @Test
    public void dispatchesInputEvent_multiple_toListener_mainDisplay() {
        subscribeListener();

        // KeyEvents get recycled, so we can't just use ArgumentCaptor#getAllValues here.
        // We need to make a copy of the information we need at the time of the call.
        List<KeyEvent> events = new ArrayList<>();
        doAnswer(inv -> {
            KeyEvent event = inv.getArgument(0);
            events.add(event.copy());
            return null;
        }).when(mInputListener).onKeyEvent(any(), eq(CarOccupantZoneManager.DISPLAY_TYPE_MAIN));

        mInputHalService.onHalEvents(
                List.of(
                        makeKeyPropValue(Key.DOWN, KeyEvent.KEYCODE_ENTER, VehicleDisplay.MAIN),
                        makeKeyPropValue(Key.DOWN, KeyEvent.KEYCODE_MENU, VehicleDisplay.MAIN)));

        assertThat(events.get(0).getKeyCode()).isEqualTo(KeyEvent.KEYCODE_ENTER);
        assertThat(events.get(1).getKeyCode()).isEqualTo(KeyEvent.KEYCODE_MENU);

        events.forEach(KeyEvent::recycle);
    }

    @Test
    public void dispatchesInputEvent_multiple_toListener_clusterDisplay() {
        subscribeListener();

        // KeyEvents get recycled, so we can't just use ArgumentCaptor#getAllValues here.
        // We need to make a copy of the information we need at the time of the call.
        List<KeyEvent> events = new ArrayList<>();
        doAnswer(inv -> {
            KeyEvent event = inv.getArgument(0);
            events.add(event.copy());
            return null;
        }).when(mInputListener).onKeyEvent(any(),
                eq(CarOccupantZoneManager.DISPLAY_TYPE_INSTRUMENT_CLUSTER));

        mInputHalService.onHalEvents(
                List.of(
                        makeKeyPropValue(Key.DOWN, KeyEvent.KEYCODE_ENTER,
                                VehicleDisplay.INSTRUMENT_CLUSTER),
                        makeKeyPropValue(Key.DOWN, KeyEvent.KEYCODE_MENU,
                                VehicleDisplay.INSTRUMENT_CLUSTER)));

        assertThat(events.get(0).getKeyCode()).isEqualTo(KeyEvent.KEYCODE_ENTER);
        assertThat(events.get(1).getKeyCode()).isEqualTo(KeyEvent.KEYCODE_MENU);

        events.forEach(KeyEvent::recycle);
    }

    @Test
    public void dispatchesInputEvent_invalidInputEvent() {
        subscribeListener();
        HalPropValue v = mPropValueBuilder.build(VehicleProperty.HW_KEY_INPUT, /* areaId= */ 0);
        // Missing action, code, display_type.
        mInputHalService.onHalEvents(List.of(v));
        verify(mInputListener, never()).onKeyEvent(any(),  anyInt());

        // Missing code, display_type.
        v = mPropValueBuilder.build(VehicleProperty.HW_KEY_INPUT, /* areaId= */ 0,
                VehicleHwKeyInputAction.ACTION_DOWN);
        mInputHalService.onHalEvents(List.of(v));
        verify(mInputListener, never()).onKeyEvent(any(),  anyInt());

        // Missing display_type.
        v = mPropValueBuilder.build(VehicleProperty.HW_KEY_INPUT, /* areaId= */ 0,
                new int[]{VehicleHwKeyInputAction.ACTION_DOWN, KeyEvent.KEYCODE_ENTER});
        mInputHalService.onHalEvents(List.of(v));
        verify(mInputListener, never()).onKeyEvent(any(),  anyInt());
    }

    @Test
    public void handlesRepeatedKeys_anyDisplay() {
        subscribeListener();

        KeyEvent event = dispatchSingleEvent(Key.DOWN, KeyEvent.KEYCODE_ENTER, VehicleDisplay.MAIN,
                CarOccupantZoneManager.DISPLAY_TYPE_MAIN);
        assertThat(event.getAction()).isEqualTo(KeyEvent.ACTION_DOWN);
        assertThat(event.getKeyCode()).isEqualTo(KeyEvent.KEYCODE_ENTER);
        assertThat(event.getEventTime()).isEqualTo(0L);
        assertThat(event.getDownTime()).isEqualTo(0L);
        assertThat(event.getRepeatCount()).isEqualTo(0);

        when(mUptimeSupplier.getAsLong()).thenReturn(5L);
        event = dispatchSingleEvent(Key.DOWN, KeyEvent.KEYCODE_ENTER, VehicleDisplay.MAIN,
                CarOccupantZoneManager.DISPLAY_TYPE_MAIN);

        assertThat(event.getAction()).isEqualTo(KeyEvent.ACTION_DOWN);
        assertThat(event.getKeyCode()).isEqualTo(KeyEvent.KEYCODE_ENTER);
        assertThat(event.getEventTime()).isEqualTo(5L);
        assertThat(event.getDownTime()).isEqualTo(5L);
        assertThat(event.getRepeatCount()).isEqualTo(1);

        when(mUptimeSupplier.getAsLong()).thenReturn(10L);
        event = dispatchSingleEvent(Key.UP, KeyEvent.KEYCODE_ENTER, VehicleDisplay.MAIN,
                CarOccupantZoneManager.DISPLAY_TYPE_MAIN);

        assertThat(event.getAction()).isEqualTo(KeyEvent.ACTION_UP);
        assertThat(event.getKeyCode()).isEqualTo(KeyEvent.KEYCODE_ENTER);
        assertThat(event.getEventTime()).isEqualTo(10L);
        assertThat(event.getDownTime()).isEqualTo(5L);
        assertThat(event.getRepeatCount()).isEqualTo(0);

        when(mUptimeSupplier.getAsLong()).thenReturn(15L);
        event = dispatchSingleEvent(Key.DOWN, KeyEvent.KEYCODE_ENTER, VehicleDisplay.MAIN,
                CarOccupantZoneManager.DISPLAY_TYPE_MAIN);

        assertThat(event.getAction()).isEqualTo(KeyEvent.ACTION_DOWN);
        assertThat(event.getKeyCode()).isEqualTo(KeyEvent.KEYCODE_ENTER);
        assertThat(event.getEventTime()).isEqualTo(15L);
        assertThat(event.getDownTime()).isEqualTo(15L);
        assertThat(event.getRepeatCount()).isEqualTo(0);
    }

    /**
     * Test for handling rotary knob event.
     */
    @RequiresDevice
    @Test
    public void handlesRepeatedKeyWithIndents_anyDisplay() {
        subscribeListener();
        KeyEvent event = dispatchSingleEventWithIndents(KeyEvent.KEYCODE_VOLUME_UP, 5,
                VehicleDisplay.MAIN, CarOccupantZoneManager.DISPLAY_TYPE_MAIN);
        assertThat(event.getAction()).isEqualTo(KeyEvent.ACTION_DOWN);
        assertThat(event.getKeyCode()).isEqualTo(KeyEvent.KEYCODE_VOLUME_UP);
        assertThat(event.getEventTime()).isEqualTo(0L);
        assertThat(event.getDownTime()).isEqualTo(0L);
        assertThat(event.getRepeatCount()).isEqualTo(4);

        when(mUptimeSupplier.getAsLong()).thenReturn(5L);
        event = dispatchSingleEventWithIndents(KeyEvent.KEYCODE_VOLUME_UP, 5,
                VehicleDisplay.MAIN, CarOccupantZoneManager.DISPLAY_TYPE_MAIN);
        assertThat(event.getAction()).isEqualTo(KeyEvent.ACTION_DOWN);
        assertThat(event.getKeyCode()).isEqualTo(KeyEvent.KEYCODE_VOLUME_UP);
        assertThat(event.getEventTime()).isEqualTo(5L);
        assertThat(event.getDownTime()).isEqualTo(5L);
        assertThat(event.getRepeatCount()).isEqualTo(9);
    }

    @Test
    public void handlesKeyUp_withoutKeyDown_mainDisplay() {
        subscribeListener();

        when(mUptimeSupplier.getAsLong()).thenReturn(42L);
        KeyEvent event = dispatchSingleEvent(Key.UP, KeyEvent.KEYCODE_ENTER, VehicleDisplay.MAIN,
                CarOccupantZoneManager.DISPLAY_TYPE_MAIN);

        assertThat(event.getEventTime()).isEqualTo(42L);
        assertThat(event.getDownTime()).isEqualTo(42L);
        assertThat(event.getRepeatCount()).isEqualTo(0);
        assertThat(event.getDisplayId()).isEqualTo(Display.DEFAULT_DISPLAY);
    }

    @Test
    public void handlesKeyUp_withoutKeyDown_clusterDisplay() {
        subscribeListener();

        when(mUptimeSupplier.getAsLong()).thenReturn(42L);
        KeyEvent event = dispatchSingleEvent(Key.UP, KeyEvent.KEYCODE_ENTER,
                VehicleDisplay.INSTRUMENT_CLUSTER,
                CarOccupantZoneManager.DISPLAY_TYPE_INSTRUMENT_CLUSTER);

        assertThat(event.getEventTime()).isEqualTo(42L);
        assertThat(event.getDownTime()).isEqualTo(42L);
        assertThat(event.getRepeatCount()).isEqualTo(0);
        // event.getDisplayId is not tested since it is assigned by CarInputService
    }

    @Test
    public void separateKeyDownEvents_areIndependent_mainDisplay() {
        subscribeListener();

        when(mUptimeSupplier.getAsLong()).thenReturn(27L);
        dispatchSingleEvent(Key.DOWN, KeyEvent.KEYCODE_ENTER, VehicleDisplay.MAIN,
                CarOccupantZoneManager.DISPLAY_TYPE_MAIN);

        when(mUptimeSupplier.getAsLong()).thenReturn(42L);
        KeyEvent event = dispatchSingleEvent(Key.DOWN, KeyEvent.KEYCODE_MENU, VehicleDisplay.MAIN,
                CarOccupantZoneManager.DISPLAY_TYPE_MAIN);

        assertThat(event.getKeyCode()).isEqualTo(KeyEvent.KEYCODE_MENU);
        assertThat(event.getDownTime()).isEqualTo(42L);
        assertThat(event.getRepeatCount()).isEqualTo(0);
    }

    @Test
    public void separateKeyDownEvents_areIndependent_clusterDisplay() {
        subscribeListener();

        when(mUptimeSupplier.getAsLong()).thenReturn(27L);
        dispatchSingleEvent(Key.DOWN, KeyEvent.KEYCODE_ENTER, VehicleDisplay.INSTRUMENT_CLUSTER,
                CarOccupantZoneManager.DISPLAY_TYPE_INSTRUMENT_CLUSTER);

        when(mUptimeSupplier.getAsLong()).thenReturn(42L);
        KeyEvent event = dispatchSingleEvent(Key.DOWN, KeyEvent.KEYCODE_MENU, VehicleDisplay.MAIN,
                CarOccupantZoneManager.DISPLAY_TYPE_MAIN);

        assertThat(event.getKeyCode()).isEqualTo(KeyEvent.KEYCODE_MENU);
        assertThat(event.getDownTime()).isEqualTo(42L);
        assertThat(event.getRepeatCount()).isEqualTo(0);
        // event.getDisplayid is not tested since it is assigned by CarInputService
    }

    @Test
    public void dispatchesRotaryEvent_singleVolumeUp_anyDisplay() {
        subscribeListener();

        // Arrange mInputListener to capture incoming RotaryEvent
        List<RotaryEvent> events = new ArrayList<>();
        doAnswer(invocation -> {
            RotaryEvent event = invocation.getArgument(0);
            events.add(event);
            return null;
        }).when(mInputListener).onRotaryEvent(any(), eq(CarOccupantZoneManager.DISPLAY_TYPE_MAIN));

        // Arrange
        long timestampNanos = 12_345_678_901L;

        // Act
        mInputHalService.onHalEvents(List.of(
                makeRotaryPropValue(RotaryInputType.ROTARY_INPUT_TYPE_AUDIO_VOLUME, 1,
                        timestampNanos, 0, VehicleDisplay.MAIN)));

        // Assert

        // Expected Rotary event to have only one value for uptimeMillisForClicks since the input
        // property was created with one detent only. This value will correspond to the event
        // startup time. See CarServiceUtils#getUptimeToElapsedTimeDeltaInMillis for more detailed
        // information on how this value is calculated.
        assertThat(events).containsExactly(new RotaryEvent(
                /* inputType= */ CarInputManager.INPUT_TYPE_ROTARY_VOLUME,
                /* clockwise= */ true,
                /* uptimeMillisForClicks= */ new long[]{12345L}));
    }

    @Test
    public void dispatchesRotaryEvent_multipleNavigatePrevious_anyDisplay() {
        subscribeListener();

        // Arrange mInputListener to capture incoming RotaryEvent
        List<RotaryEvent> events = new ArrayList<>();
        doAnswer(invocation -> {
            RotaryEvent event = invocation.getArgument(0);
            events.add(event);
            return null;
        }).when(mInputListener).onRotaryEvent(any(), eq(CarOccupantZoneManager.DISPLAY_TYPE_MAIN));

        // Arrange
        long timestampNanos = 12_345_000_000L;
        int deltaNanos = 2_000_000;
        int numberOfDetents = 3;

        // Act
        mInputHalService.onHalEvents(List.of(
                makeRotaryPropValue(RotaryInputType.ROTARY_INPUT_TYPE_SYSTEM_NAVIGATION,
                        -numberOfDetents, timestampNanos, deltaNanos, VehicleDisplay.MAIN)));

        // Assert

        // Expected Rotary event to have 3 values for uptimeMillisForClicks since the input
        // property value was created with 3 detents. Each value in uptimeMillisForClicks
        // represents the calculated deltas (in nanoseconds) between pairs of consecutive detents
        // up times. See InputHalService#dispatchRotaryInput for more detailed information on how
        // delta times are calculated.
        assertThat(events).containsExactly(new RotaryEvent(
                /* inputType= */CarInputManager.INPUT_TYPE_ROTARY_NAVIGATION,
                /* clockwise= */ false,
                /* uptimeMillisForClicks= */ new long[]{12345L, 12347L, 12349L}));
    }

    @Test
    public void dispatchesRotaryEvent_invalidInputEvent() {
        subscribeListener();
        HalPropValue v = mPropValueBuilder.build(VehicleProperty.HW_ROTARY_INPUT, /* areaId= */ 0);

        // Missing rotaryInputType, detentCount, targetDisplayType.
        mInputHalService.onHalEvents(List.of(v));
        verify(mInputListener, never()).onRotaryEvent(any(),  anyInt());

        // Missing detentCount, targetDisplayType.
        v = mPropValueBuilder.build(VehicleProperty.HW_ROTARY_INPUT, /* areaId= */ 0,
                RotaryInputType.ROTARY_INPUT_TYPE_SYSTEM_NAVIGATION);
        mInputHalService.onHalEvents(List.of(v));
        verify(mInputListener, never()).onRotaryEvent(any(),  anyInt());

        // Missing targetDisplayType.
        v = mPropValueBuilder.build(VehicleProperty.HW_ROTARY_INPUT, /* areaId= */ 0,
                new int[]{RotaryInputType.ROTARY_INPUT_TYPE_SYSTEM_NAVIGATION, 1});
        mInputHalService.onHalEvents(List.of(v));
        verify(mInputListener, never()).onRotaryEvent(any(),  anyInt());

        // Add targetDisplayType and set detentCount to 0.
        v = mPropValueBuilder.build(VehicleProperty.HW_ROTARY_INPUT, /* areaId= */ 0,
                new int[]{RotaryInputType.ROTARY_INPUT_TYPE_SYSTEM_NAVIGATION, 0,
                        VehicleDisplay.MAIN});
        mInputHalService.onHalEvents(List.of(v));
        verify(mInputListener, never()).onRotaryEvent(any(),  anyInt());

        // Set detentCount to 1.
        // Add additional unnecessary arguments so that the array size does not match detentCount.
        v = mPropValueBuilder.build(VehicleProperty.HW_ROTARY_INPUT, /* areaId= */ 1,
                new int[]{RotaryInputType.ROTARY_INPUT_TYPE_SYSTEM_NAVIGATION, 1,
                        VehicleDisplay.MAIN, 0});
        mInputHalService.onHalEvents(List.of(v));
        verify(mInputListener, never()).onRotaryEvent(any(),  anyInt());

        // Set invalid detentCount.
        v = mPropValueBuilder.build(VehicleProperty.HW_ROTARY_INPUT, /* areaId= */ 1,
                new int[]{RotaryInputType.ROTARY_INPUT_TYPE_SYSTEM_NAVIGATION, Integer.MAX_VALUE,
                        VehicleDisplay.MAIN, 0});
        mInputHalService.onHalEvents(List.of(v));
        verify(mInputListener, never()).onRotaryEvent(any(),  anyInt());

        // Set invalid detentCount.
        v = mPropValueBuilder.build(VehicleProperty.HW_ROTARY_INPUT, /* areaId= */ 1,
                new int[]{RotaryInputType.ROTARY_INPUT_TYPE_SYSTEM_NAVIGATION, Integer.MIN_VALUE,
                        VehicleDisplay.MAIN, 0});
        mInputHalService.onHalEvents(List.of(v));
        verify(mInputListener, never()).onRotaryEvent(any(),  anyInt());
    }

    @Test
    public void dispatchesCustomInputEvent_mainDisplay() {
        // Arrange mInputListener to capture incoming CustomInputEvent
        subscribeListener();

        List<CustomInputEvent> events = new ArrayList<>();
        doAnswer(invocation -> {
            CustomInputEvent event = invocation.getArgument(0);
            events.add(event);
            return null;
        }).when(mInputListener).onCustomInputEvent(any());

        // Arrange
        int repeatCounter = 1;
        HalPropValue customInputPropValue = makeCustomInputPropValue(
                CUSTOM_EVENT_F1, VehicleDisplay.MAIN, repeatCounter);

        // Act
        mInputHalService.onHalEvents(List.of(customInputPropValue));

        // Assert
        assertThat(events).containsExactly(new CustomInputEvent(
                CustomInputEvent.INPUT_CODE_F1, CarOccupantZoneManager.DISPLAY_TYPE_MAIN,
                repeatCounter));
    }

    @Test
    public void dispatchesCustomInputEvent_clusterDisplay() {
        // Arrange mInputListener to capture incoming CustomInputEvent
        subscribeListener();

        List<CustomInputEvent> events = new ArrayList<>();
        doAnswer(invocation -> {
            CustomInputEvent event = invocation.getArgument(0);
            events.add(event);
            return null;
        }).when(mInputListener).onCustomInputEvent(any());

        // Arrange
        int repeatCounter = 1;
        HalPropValue customInputPropValue = makeCustomInputPropValue(
                CUSTOM_EVENT_F1, VehicleDisplay.INSTRUMENT_CLUSTER, repeatCounter);

        // Act
        mInputHalService.onHalEvents(List.of(customInputPropValue));

        // Assert
        assertThat(events).containsExactly(new CustomInputEvent(
                CustomInputEvent.INPUT_CODE_F1,
                CarOccupantZoneManager.DISPLAY_TYPE_INSTRUMENT_CLUSTER,
                repeatCounter));
    }

    @Test
    public void dispatchesCustomInputEvent_InvalidEvent() {
        // Arrange mInputListener to capture incoming CustomInputEvent
        subscribeListener();

        HalPropValue v = mPropValueBuilder.build(VehicleProperty.HW_CUSTOM_INPUT, /* areaId= */ 0);

        // Missing inputCode, targetDisplayType, repeatCounter.
        mInputHalService.onHalEvents(List.of(v));
        verify(mInputListener, never()).onCustomInputEvent(any());

        // Missing targetDisplayType, repeatCounter.
        v = mPropValueBuilder.build(VehicleProperty.HW_CUSTOM_INPUT, /* areaId= */ 0,
                CustomInputEvent.INPUT_CODE_F1);
        mInputHalService.onHalEvents(List.of(v));
        verify(mInputListener, never()).onCustomInputEvent(any());

        // Missing repeatCounter.
        v = mPropValueBuilder.build(VehicleProperty.HW_CUSTOM_INPUT, /* areaId= */ 0,
                new int[]{CustomInputEvent.INPUT_CODE_F1,
                        CarOccupantZoneManager.DISPLAY_TYPE_INSTRUMENT_CLUSTER});
        mInputHalService.onHalEvents(List.of(v));

        verify(mInputListener, never()).onCustomInputEvent(any());
    }

    @Test
    public void dispatchesCustomInputEvent_acceptInputCodeHigherThanF10() {
        // Custom Input Events may accept input code values outside the
        // CUSTOM_EVENT_F1 to F10 range.
        int someInputCodeValueHigherThanF10 = 1000;

        // Arrange mInputListener to capture incoming CustomInputEvent
        subscribeListener();

        List<CustomInputEvent> events = new ArrayList<>();
        doAnswer(invocation -> {
            CustomInputEvent event = invocation.getArgument(0);
            events.add(event);
            return null;
        }).when(mInputListener).onCustomInputEvent(any());

        // Arrange
        int repeatCounter = 1;
        HalPropValue customInputPropValue = makeCustomInputPropValue(
                someInputCodeValueHigherThanF10, VehicleDisplay.MAIN, repeatCounter);

        // Act
        mInputHalService.onHalEvents(List.of(customInputPropValue));

        // Assert
        assertThat(events).containsExactly(new CustomInputEvent(
                someInputCodeValueHigherThanF10, CarOccupantZoneManager.DISPLAY_TYPE_MAIN,
                repeatCounter));
    }

    private void subscribeListener() {
        mInputHalService.takeProperties(Set.of(HW_KEY_INPUT_CONFIG));
        assertThat(mInputHalService.isKeyInputSupported()).isTrue();

        mInputHalService.setInputListener(mInputListener);
        verify(mVehicleHal).subscribeProperty(mInputHalService, VehicleProperty.HW_KEY_INPUT);
    }

    private KeyEvent dispatchSingleEvent(Key action, int code, int actualDisplay,
            @DisplayTypeEnum int expectedDisplay) {
        ArgumentCaptor<KeyEvent> captor = ArgumentCaptor.forClass(KeyEvent.class);
        reset(mInputListener);
        mInputHalService.onHalEvents(
                List.of(makeKeyPropValue(action, code, actualDisplay)));
        verify(mInputListener).onKeyEvent(captor.capture(), eq(expectedDisplay));
        reset(mInputListener);
        return captor.getValue();
    }

    private KeyEvent dispatchSingleEventWithIndents(int code, int indents, int actualDisplay,
            @DisplayTypeEnum int expectedDisplay) {
        ArgumentCaptor<KeyEvent> captor = ArgumentCaptor.forClass(KeyEvent.class);
        reset(mInputListener);
        mInputHalService.onHalEvents(
                List.of(makeKeyPropValueWithIndents(code, indents, actualDisplay)));
        verify(mInputListener, times(indents)).onKeyEvent(captor.capture(),
                eq(expectedDisplay));
        reset(mInputListener);
        return captor.getValue();
    }

    private HalPropValue makeKeyPropValue(Key action, int code,
            @DisplayTypeEnum int targetDisplayType) {
        int actionValue = (action == Key.DOWN
                        ? VehicleHwKeyInputAction.ACTION_DOWN
                        : VehicleHwKeyInputAction.ACTION_UP);
        return mPropValueBuilder.build(VehicleProperty.HW_KEY_INPUT, /* areaId= */ 0,
                new int[]{actionValue, code, targetDisplayType});
    }

    private HalPropValue makeKeyPropValueWithIndents(int code, int indents,
            @DisplayTypeEnum int targetDisplayType) {
        // Only Key.down can have indents.
        return mPropValueBuilder.build(VehicleProperty.HW_KEY_INPUT, /* areaId= */ 0,
                new int[]{VehicleHwKeyInputAction.ACTION_DOWN, code, targetDisplayType, indents});
    }

    private HalPropValue makeRotaryPropValue(int rotaryInputType, int detents, long timestamp,
            int delayBetweenDetents, @DisplayTypeEnum int targetDisplayType) {
        ArrayList<Integer> int32Values = new ArrayList<>();
        int32Values.add(rotaryInputType);
        int32Values.add(detents);
        int32Values.add(targetDisplayType);
        for (int i = 0; i < Math.abs(detents) - 1; i++) {
            int32Values.add(delayBetweenDetents);
        }
        return mPropValueBuilder.build(VehicleProperty.HW_ROTARY_INPUT, /* areaId= */ 0, timestamp,
                /*status=*/0, toIntArray(int32Values));
    }

    private HalPropValue makeCustomInputPropValue(int inputCode,
            @DisplayTypeEnum int targetDisplayType, int repeatCounter) {
        return mPropValueBuilder.build(VehicleProperty.HW_CUSTOM_INPUT, /* areaId= */ 0,
                new int[]{inputCode, targetDisplayType, repeatCounter});
    }
}