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.os.vibrator; 18 19 import static junit.framework.Assert.assertEquals; 20 import static junit.framework.Assert.assertFalse; 21 import static junit.framework.Assert.assertSame; 22 import static junit.framework.Assert.assertTrue; 23 24 import static org.testng.Assert.assertNotEquals; 25 import static org.testng.Assert.assertThrows; 26 27 import android.os.Parcel; 28 import android.os.SystemVibrator; 29 import android.os.VibrationEffect; 30 import android.os.Vibrator; 31 import android.os.VibratorInfo; 32 import android.platform.test.annotations.Presubmit; 33 34 import androidx.test.InstrumentationRegistry; 35 36 import org.junit.Test; 37 import org.junit.runner.RunWith; 38 import org.mockito.junit.MockitoJUnitRunner; 39 40 @Presubmit 41 @RunWith(MockitoJUnitRunner.class) 42 public class PrebakedSegmentTest { 43 44 @Test testCreation()45 public void testCreation() { 46 PrebakedSegment prebaked = new PrebakedSegment( 47 VibrationEffect.EFFECT_CLICK, true, VibrationEffect.EFFECT_STRENGTH_MEDIUM); 48 49 assertEquals(-1, prebaked.getDuration()); 50 assertTrue(prebaked.hasNonZeroAmplitude()); 51 assertEquals(VibrationEffect.EFFECT_CLICK, prebaked.getEffectId()); 52 assertEquals(VibrationEffect.EFFECT_STRENGTH_MEDIUM, prebaked.getEffectStrength()); 53 assertTrue(prebaked.shouldFallback()); 54 } 55 56 @Test testSerialization()57 public void testSerialization() { 58 PrebakedSegment original = new PrebakedSegment( 59 VibrationEffect.EFFECT_CLICK, true, VibrationEffect.EFFECT_STRENGTH_MEDIUM); 60 Parcel parcel = Parcel.obtain(); 61 original.writeToParcel(parcel, 0); 62 parcel.setDataPosition(0); 63 assertEquals(original, PrebakedSegment.CREATOR.createFromParcel(parcel)); 64 } 65 66 @Test testValidate()67 public void testValidate() { 68 new PrebakedSegment(VibrationEffect.EFFECT_CLICK, true, 69 VibrationEffect.EFFECT_STRENGTH_MEDIUM).validate(); 70 71 assertThrows(IllegalArgumentException.class, 72 () -> new PrebakedSegment(1000, true, VibrationEffect.EFFECT_STRENGTH_MEDIUM) 73 .validate()); 74 assertThrows(IllegalArgumentException.class, 75 () -> new PrebakedSegment(VibrationEffect.EFFECT_TICK, false, 1000) 76 .validate()); 77 } 78 79 @Test testResolve_ignoresAndReturnsSameEffect()80 public void testResolve_ignoresAndReturnsSameEffect() { 81 PrebakedSegment prebaked = new PrebakedSegment( 82 VibrationEffect.EFFECT_CLICK, true, VibrationEffect.EFFECT_STRENGTH_MEDIUM); 83 assertSame(prebaked, prebaked.resolve(1000)); 84 } 85 86 @Test testApplyEffectStrength()87 public void testApplyEffectStrength() { 88 PrebakedSegment medium = new PrebakedSegment( 89 VibrationEffect.EFFECT_THUD, true, VibrationEffect.EFFECT_STRENGTH_MEDIUM); 90 91 PrebakedSegment light = medium.applyEffectStrength(VibrationEffect.EFFECT_STRENGTH_LIGHT); 92 assertNotEquals(medium, light); 93 assertEquals(medium.getEffectId(), light.getEffectId()); 94 assertEquals(medium.shouldFallback(), light.shouldFallback()); 95 assertEquals(VibrationEffect.EFFECT_STRENGTH_LIGHT, light.getEffectStrength()); 96 97 PrebakedSegment strong = medium.applyEffectStrength(VibrationEffect.EFFECT_STRENGTH_STRONG); 98 assertNotEquals(medium, strong); 99 assertEquals(medium.getEffectId(), strong.getEffectId()); 100 assertEquals(medium.shouldFallback(), strong.shouldFallback()); 101 assertEquals(VibrationEffect.EFFECT_STRENGTH_STRONG, strong.getEffectStrength()); 102 103 assertSame(medium, medium.applyEffectStrength(VibrationEffect.EFFECT_STRENGTH_MEDIUM)); 104 // Invalid vibration effect strength is ignored. 105 assertSame(medium, medium.applyEffectStrength(1000)); 106 } 107 108 @Test testScale_ignoresAndReturnsSameEffect()109 public void testScale_ignoresAndReturnsSameEffect() { 110 PrebakedSegment prebaked = new PrebakedSegment( 111 VibrationEffect.EFFECT_CLICK, true, VibrationEffect.EFFECT_STRENGTH_MEDIUM); 112 assertSame(prebaked, prebaked.scale(0.5f)); 113 } 114 115 @Test testDuration()116 public void testDuration() { 117 assertEquals(-1, new PrebakedSegment( 118 VibrationEffect.EFFECT_CLICK, true, VibrationEffect.EFFECT_STRENGTH_MEDIUM) 119 .getDuration()); 120 assertEquals(-1, new PrebakedSegment( 121 VibrationEffect.EFFECT_TICK, true, VibrationEffect.EFFECT_STRENGTH_MEDIUM) 122 .getDuration()); 123 assertEquals(-1, new PrebakedSegment( 124 VibrationEffect.EFFECT_DOUBLE_CLICK, true, VibrationEffect.EFFECT_STRENGTH_MEDIUM) 125 .getDuration()); 126 assertEquals(-1, new PrebakedSegment( 127 VibrationEffect.EFFECT_THUD, true, VibrationEffect.EFFECT_STRENGTH_MEDIUM) 128 .getDuration()); 129 } 130 131 @Test testIsHapticFeedbackCandidate_prebakedConstants_areCandidates()132 public void testIsHapticFeedbackCandidate_prebakedConstants_areCandidates() { 133 assertTrue(new PrebakedSegment( 134 VibrationEffect.EFFECT_CLICK, true, VibrationEffect.EFFECT_STRENGTH_MEDIUM) 135 .isHapticFeedbackCandidate()); 136 assertTrue(new PrebakedSegment( 137 VibrationEffect.EFFECT_TICK, true, VibrationEffect.EFFECT_STRENGTH_MEDIUM) 138 .isHapticFeedbackCandidate()); 139 assertTrue(new PrebakedSegment( 140 VibrationEffect.EFFECT_DOUBLE_CLICK, true, VibrationEffect.EFFECT_STRENGTH_MEDIUM) 141 .isHapticFeedbackCandidate()); 142 assertTrue(new PrebakedSegment( 143 VibrationEffect.EFFECT_HEAVY_CLICK, true, VibrationEffect.EFFECT_STRENGTH_MEDIUM) 144 .isHapticFeedbackCandidate()); 145 assertTrue(new PrebakedSegment( 146 VibrationEffect.EFFECT_THUD, true, VibrationEffect.EFFECT_STRENGTH_MEDIUM) 147 .isHapticFeedbackCandidate()); 148 assertTrue(new PrebakedSegment( 149 VibrationEffect.EFFECT_TEXTURE_TICK, true, VibrationEffect.EFFECT_STRENGTH_MEDIUM) 150 .isHapticFeedbackCandidate()); 151 } 152 153 @Test testVibrationFeaturesSupport_idsWithFallback_fallbackEnabled_vibratorSupport()154 public void testVibrationFeaturesSupport_idsWithFallback_fallbackEnabled_vibratorSupport() { 155 Vibrator vibrator = createVibratorWithSupportedEffects( 156 VibrationEffect.EFFECT_TICK, 157 VibrationEffect.EFFECT_CLICK, 158 VibrationEffect.EFFECT_DOUBLE_CLICK, 159 VibrationEffect.EFFECT_HEAVY_CLICK); 160 161 assertTrue(createSegmentWithFallback(VibrationEffect.EFFECT_TICK) 162 .areVibrationFeaturesSupported(vibrator)); 163 assertTrue(createSegmentWithFallback(VibrationEffect.EFFECT_CLICK) 164 .areVibrationFeaturesSupported(vibrator)); 165 assertTrue(createSegmentWithFallback(VibrationEffect.EFFECT_DOUBLE_CLICK) 166 .areVibrationFeaturesSupported(vibrator)); 167 assertTrue(createSegmentWithFallback(VibrationEffect.EFFECT_HEAVY_CLICK) 168 .areVibrationFeaturesSupported(vibrator)); 169 170 } 171 172 @Test testVibrationFeaturesSupport_idsWithFallback_fallbackEnabled_noVibratorSupport()173 public void testVibrationFeaturesSupport_idsWithFallback_fallbackEnabled_noVibratorSupport() { 174 Vibrator vibrator = createVibratorWithSupportedEffects(new int[0]); 175 176 assertTrue(createSegmentWithFallback(VibrationEffect.EFFECT_TICK) 177 .areVibrationFeaturesSupported(vibrator)); 178 assertTrue(createSegmentWithFallback(VibrationEffect.EFFECT_CLICK) 179 .areVibrationFeaturesSupported(vibrator)); 180 assertTrue(createSegmentWithFallback(VibrationEffect.EFFECT_DOUBLE_CLICK) 181 .areVibrationFeaturesSupported(vibrator)); 182 assertTrue(createSegmentWithFallback(VibrationEffect.EFFECT_HEAVY_CLICK) 183 .areVibrationFeaturesSupported(vibrator)); 184 } 185 186 @Test testVibrationFeaturesSupport_idsWithFallback_fallbackDisabled_vibratorSupport()187 public void testVibrationFeaturesSupport_idsWithFallback_fallbackDisabled_vibratorSupport() { 188 Vibrator vibrator = createVibratorWithSupportedEffects( 189 VibrationEffect.EFFECT_TICK, 190 VibrationEffect.EFFECT_CLICK, 191 VibrationEffect.EFFECT_DOUBLE_CLICK, 192 VibrationEffect.EFFECT_HEAVY_CLICK); 193 194 assertTrue(createSegmentWithoutFallback(VibrationEffect.EFFECT_TICK) 195 .areVibrationFeaturesSupported(vibrator)); 196 assertTrue(createSegmentWithoutFallback(VibrationEffect.EFFECT_CLICK) 197 .areVibrationFeaturesSupported(vibrator)); 198 assertTrue(createSegmentWithoutFallback(VibrationEffect.EFFECT_DOUBLE_CLICK) 199 .areVibrationFeaturesSupported(vibrator)); 200 assertTrue(createSegmentWithoutFallback(VibrationEffect.EFFECT_HEAVY_CLICK) 201 .areVibrationFeaturesSupported(vibrator)); 202 } 203 204 @Test testVibrationFeaturesSupport_idsWithFallback_fallbackDisabled_noVibratorSupport()205 public void testVibrationFeaturesSupport_idsWithFallback_fallbackDisabled_noVibratorSupport() { 206 Vibrator vibrator = createVibratorWithSupportedEffects(new int[0]); 207 208 assertFalse(createSegmentWithoutFallback(VibrationEffect.EFFECT_TICK) 209 .areVibrationFeaturesSupported(vibrator)); 210 assertFalse(createSegmentWithoutFallback(VibrationEffect.EFFECT_CLICK) 211 .areVibrationFeaturesSupported(vibrator)); 212 assertFalse(createSegmentWithoutFallback(VibrationEffect.EFFECT_DOUBLE_CLICK) 213 .areVibrationFeaturesSupported(vibrator)); 214 assertFalse(createSegmentWithoutFallback(VibrationEffect.EFFECT_HEAVY_CLICK) 215 .areVibrationFeaturesSupported(vibrator)); 216 } 217 218 @Test testVibrationFeaturesSupport_idsWithNoFallback_fallbackEnabled_vibratorSupport()219 public void testVibrationFeaturesSupport_idsWithNoFallback_fallbackEnabled_vibratorSupport() { 220 Vibrator vibrator = createVibratorWithSupportedEffects( 221 VibrationEffect.EFFECT_THUD, 222 VibrationEffect.EFFECT_POP, 223 VibrationEffect.EFFECT_TEXTURE_TICK); 224 225 assertTrue(createSegmentWithFallback(VibrationEffect.EFFECT_THUD) 226 .areVibrationFeaturesSupported(vibrator)); 227 assertTrue(createSegmentWithFallback(VibrationEffect.EFFECT_POP) 228 .areVibrationFeaturesSupported(vibrator)); 229 assertTrue(createSegmentWithFallback(VibrationEffect.EFFECT_TEXTURE_TICK) 230 .areVibrationFeaturesSupported(vibrator)); 231 } 232 233 @Test testVibrationFeaturesSupport_idsWithNoFallback_fallbackEnabled_noVibratorSupport()234 public void testVibrationFeaturesSupport_idsWithNoFallback_fallbackEnabled_noVibratorSupport() { 235 Vibrator vibrator = createVibratorWithSupportedEffects(new int[0]); 236 237 assertFalse(createSegmentWithFallback(VibrationEffect.EFFECT_THUD) 238 .areVibrationFeaturesSupported(vibrator)); 239 assertFalse(createSegmentWithFallback(VibrationEffect.EFFECT_POP) 240 .areVibrationFeaturesSupported(vibrator)); 241 assertFalse(createSegmentWithFallback(VibrationEffect.EFFECT_TEXTURE_TICK) 242 .areVibrationFeaturesSupported(vibrator)); 243 } 244 245 @Test testVibrationFeaturesSupport_idsWithNoFallback_fallbackDisabled_vibratorSupport()246 public void testVibrationFeaturesSupport_idsWithNoFallback_fallbackDisabled_vibratorSupport() { 247 Vibrator vibrator = createVibratorWithSupportedEffects( 248 VibrationEffect.EFFECT_THUD, 249 VibrationEffect.EFFECT_POP, 250 VibrationEffect.EFFECT_TEXTURE_TICK); 251 252 assertTrue(createSegmentWithoutFallback(VibrationEffect.EFFECT_THUD) 253 .areVibrationFeaturesSupported(vibrator)); 254 assertTrue(createSegmentWithoutFallback(VibrationEffect.EFFECT_POP) 255 .areVibrationFeaturesSupported(vibrator)); 256 assertTrue(createSegmentWithoutFallback(VibrationEffect.EFFECT_TEXTURE_TICK) 257 .areVibrationFeaturesSupported(vibrator)); 258 } 259 260 @Test testVibrationFeaturesSupport_idsWithNoFallback_fallbackDisabled_noVibSupport()261 public void testVibrationFeaturesSupport_idsWithNoFallback_fallbackDisabled_noVibSupport() { 262 Vibrator vibrator = createVibratorWithSupportedEffects(new int[0]); 263 264 assertFalse(createSegmentWithoutFallback(VibrationEffect.EFFECT_THUD) 265 .areVibrationFeaturesSupported(vibrator)); 266 assertFalse(createSegmentWithoutFallback(VibrationEffect.EFFECT_POP) 267 .areVibrationFeaturesSupported(vibrator)); 268 assertFalse(createSegmentWithoutFallback(VibrationEffect.EFFECT_TEXTURE_TICK) 269 .areVibrationFeaturesSupported(vibrator)); 270 } 271 272 @Test testIsHapticFeedbackCandidate_prebakedRingtones_notCandidates()273 public void testIsHapticFeedbackCandidate_prebakedRingtones_notCandidates() { 274 assertFalse(new PrebakedSegment( 275 VibrationEffect.RINGTONES[1], true, VibrationEffect.EFFECT_STRENGTH_MEDIUM) 276 .isHapticFeedbackCandidate()); 277 } 278 createSegmentWithFallback(int effectId)279 private static PrebakedSegment createSegmentWithFallback(int effectId) { 280 // note: arbitrary effect strength being used. 281 return new PrebakedSegment(effectId, true, VibrationEffect.EFFECT_STRENGTH_MEDIUM); 282 } 283 createSegmentWithoutFallback(int effectId)284 private static PrebakedSegment createSegmentWithoutFallback(int effectId) { 285 // note: arbitrary effect strength being used. 286 return new PrebakedSegment(effectId, false, VibrationEffect.EFFECT_STRENGTH_MEDIUM); 287 } 288 createVibratorWithSupportedEffects(int... supportedEffects)289 private static Vibrator createVibratorWithSupportedEffects(int... supportedEffects) { 290 return new SystemVibrator(InstrumentationRegistry.getContext()) { 291 @Override 292 public VibratorInfo getInfo() { 293 return new VibratorInfo.Builder(/* id= */ 1) 294 .setSupportedEffects(supportedEffects) 295 .build(); 296 } 297 }; 298 } 299 } 300