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 androidx.test.runner.AndroidJUnit4; 24 25 import org.junit.Test; 26 import org.junit.runner.RunWith; 27 28 @RunWith(AndroidJUnit4.class) 29 public class VirtualTouchEventTest { 30 31 @Test touchEvent_emptyBuilder()32 public void touchEvent_emptyBuilder() { 33 assertThrows(IllegalArgumentException.class, () -> new VirtualTouchEvent.Builder().build()); 34 } 35 36 @Test touchEvent_noAction()37 public void touchEvent_noAction() { 38 assertThrows(IllegalArgumentException.class, () -> new VirtualTouchEvent.Builder() 39 .setToolType(VirtualTouchEvent.TOOL_TYPE_FINGER) 40 .setX(0f) 41 .setY(1f) 42 .setPointerId(1) 43 .build()); 44 } 45 46 @Test touchEvent_noPointerId()47 public void touchEvent_noPointerId() { 48 assertThrows(IllegalArgumentException.class, () -> new VirtualTouchEvent.Builder() 49 .setAction(VirtualTouchEvent.ACTION_DOWN) 50 .setToolType(VirtualTouchEvent.TOOL_TYPE_FINGER) 51 .setX(0f) 52 .setY(1f) 53 .build()); 54 } 55 56 @Test touchEvent_noToolType()57 public void touchEvent_noToolType() { 58 assertThrows(IllegalArgumentException.class, () -> new VirtualTouchEvent.Builder() 59 .setAction(VirtualTouchEvent.ACTION_DOWN) 60 .setX(0f) 61 .setY(1f) 62 .setPointerId(1) 63 .build()); 64 } 65 66 @Test touchEvent_noX()67 public void touchEvent_noX() { 68 assertThrows(IllegalArgumentException.class, () -> new VirtualTouchEvent.Builder() 69 .setAction(VirtualTouchEvent.ACTION_DOWN) 70 .setToolType(VirtualTouchEvent.TOOL_TYPE_FINGER) 71 .setY(1f) 72 .setPointerId(1) 73 .build()); 74 } 75 76 77 @Test touchEvent_noY()78 public void touchEvent_noY() { 79 assertThrows(IllegalArgumentException.class, () -> new VirtualTouchEvent.Builder() 80 .setAction(VirtualTouchEvent.ACTION_DOWN) 81 .setToolType(VirtualTouchEvent.TOOL_TYPE_FINGER) 82 .setX(0f) 83 .setPointerId(1) 84 .build()); 85 } 86 87 @Test touchEvent_created()88 public void touchEvent_created() { 89 final VirtualTouchEvent event = new VirtualTouchEvent.Builder() 90 .setAction(VirtualTouchEvent.ACTION_DOWN) 91 .setToolType(VirtualTouchEvent.TOOL_TYPE_FINGER) 92 .setX(0f) 93 .setY(1f) 94 .setPointerId(1) 95 .build(); 96 assertWithMessage("Incorrect action").that(event.getAction()).isEqualTo( 97 VirtualTouchEvent.ACTION_DOWN); 98 assertWithMessage("Incorrect tool type").that(event.getToolType()).isEqualTo( 99 VirtualTouchEvent.TOOL_TYPE_FINGER); 100 assertWithMessage("Incorrect x").that(event.getX()).isEqualTo(0f); 101 assertWithMessage("Incorrect y").that(event.getY()).isEqualTo(1f); 102 assertWithMessage("Incorrect pointer id").that(event.getPointerId()).isEqualTo(1); 103 } 104 105 @Test touchEvent_created_withPressureAndAxis()106 public void touchEvent_created_withPressureAndAxis() { 107 final VirtualTouchEvent event = new VirtualTouchEvent.Builder() 108 .setAction(VirtualTouchEvent.ACTION_DOWN) 109 .setToolType(VirtualTouchEvent.TOOL_TYPE_FINGER) 110 .setX(0f) 111 .setY(1f) 112 .setPointerId(1) 113 .setPressure(0.5f) 114 .setMajorAxisSize(10f) 115 .build(); 116 assertWithMessage("Incorrect action").that(event.getAction()).isEqualTo( 117 VirtualTouchEvent.ACTION_DOWN); 118 assertWithMessage("Incorrect tool type").that(event.getToolType()).isEqualTo( 119 VirtualTouchEvent.TOOL_TYPE_FINGER); 120 assertWithMessage("Incorrect x").that(event.getX()).isEqualTo(0f); 121 assertWithMessage("Incorrect y").that(event.getY()).isEqualTo(1f); 122 assertWithMessage("Incorrect pointer id").that(event.getPointerId()).isEqualTo(1); 123 assertWithMessage("Incorrect pressure").that(event.getPressure()).isEqualTo(0.5f); 124 assertWithMessage("Incorrect major axis size").that(event.getMajorAxisSize()).isEqualTo( 125 10f); 126 } 127 128 @Test touchEvent_cancelUsedImproperly()129 public void touchEvent_cancelUsedImproperly() { 130 assertThrows(IllegalArgumentException.class, () -> new VirtualTouchEvent.Builder() 131 .setAction(VirtualTouchEvent.ACTION_CANCEL) 132 .setToolType(VirtualTouchEvent.TOOL_TYPE_FINGER) 133 .setX(0f) 134 .setY(1f) 135 .setPointerId(1) 136 .build()); 137 } 138 139 /** 140 * The combination of TOOL_TYPE_PALM with anything else than ACTION_CANCEL should throw an 141 * exception. This is due to an underlying implementation detail. See documentation of {@link 142 * VirtualTouchEvent} 143 * for details. 144 */ 145 @Test touchEvent_palmUsedImproperly()146 public void touchEvent_palmUsedImproperly() { 147 assertThrows(IllegalArgumentException.class, () -> new VirtualTouchEvent.Builder() 148 .setAction(VirtualTouchEvent.ACTION_MOVE) 149 .setToolType(VirtualTouchEvent.TOOL_TYPE_PALM) 150 .setX(0f) 151 .setY(1f) 152 .setPointerId(1) 153 .build()); 154 } 155 156 @Test touchEvent_palmAndCancelUsedProperly()157 public void touchEvent_palmAndCancelUsedProperly() { 158 final VirtualTouchEvent event = new VirtualTouchEvent.Builder() 159 .setAction(VirtualTouchEvent.ACTION_CANCEL) 160 .setToolType(VirtualTouchEvent.TOOL_TYPE_PALM) 161 .setX(0f) 162 .setY(1f) 163 .setPointerId(1) 164 .setPressure(0.5f) 165 .setMajorAxisSize(10f) 166 .build(); 167 assertWithMessage("Incorrect action").that(event.getAction()).isEqualTo( 168 VirtualTouchEvent.ACTION_CANCEL); 169 assertWithMessage("Incorrect tool type").that(event.getToolType()).isEqualTo( 170 VirtualTouchEvent.TOOL_TYPE_PALM); 171 assertWithMessage("Incorrect x").that(event.getX()).isEqualTo(0f); 172 assertWithMessage("Incorrect y").that(event.getY()).isEqualTo(1f); 173 assertWithMessage("Incorrect pointer id").that(event.getPointerId()).isEqualTo(1); 174 assertWithMessage("Incorrect pressure").that(event.getPressure()).isEqualTo(0.5f); 175 assertWithMessage("Incorrect major axis size").that(event.getMajorAxisSize()).isEqualTo( 176 10f); 177 } 178 } 179