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