1 /*
2  * Copyright (C) 2021 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.hardware.input;
18 
19 import static com.google.common.truth.Truth.assertWithMessage;
20 
21 import static org.testng.Assert.assertThrows;
22 
23 import android.view.KeyEvent;
24 
25 import androidx.test.runner.AndroidJUnit4;
26 
27 import org.junit.Test;
28 import org.junit.runner.RunWith;
29 
30 @RunWith(AndroidJUnit4.class)
31 public class VirtualKeyEventTest {
32 
33     @Test
keyEvent_emptyBuilder()34     public void keyEvent_emptyBuilder() {
35         assertThrows(IllegalArgumentException.class, () -> new VirtualKeyEvent.Builder().build());
36     }
37 
38     @Test
keyEvent_noKeyCode()39     public void keyEvent_noKeyCode() {
40         assertThrows(IllegalArgumentException.class,
41                 () -> new VirtualKeyEvent.Builder().setAction(VirtualKeyEvent.ACTION_DOWN).build());
42     }
43 
44     @Test
keyEvent_noAction()45     public void keyEvent_noAction() {
46         assertThrows(IllegalArgumentException.class,
47                 () -> new VirtualKeyEvent.Builder().setKeyCode(KeyEvent.KEYCODE_A).build());
48     }
49 
50     @Test
keyEvent_created()51     public void keyEvent_created() {
52         final VirtualKeyEvent event = new VirtualKeyEvent.Builder()
53                 .setAction(VirtualKeyEvent.ACTION_DOWN)
54                 .setKeyCode(KeyEvent.KEYCODE_A).build();
55         assertWithMessage("Incorrect key code").that(event.getKeyCode()).isEqualTo(
56                 KeyEvent.KEYCODE_A);
57         assertWithMessage("Incorrect action").that(event.getAction()).isEqualTo(
58                 VirtualKeyEvent.ACTION_DOWN);
59     }
60 }
61