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.VibrationEffect; 29 import android.os.VibratorInfo; 30 31 import org.junit.Test; 32 import org.junit.runner.RunWith; 33 import org.junit.runners.JUnit4; 34 35 @RunWith(JUnit4.class) 36 public class PrebakedSegmentTest { 37 38 @Test testCreation()39 public void testCreation() { 40 PrebakedSegment prebaked = new PrebakedSegment( 41 VibrationEffect.EFFECT_CLICK, true, VibrationEffect.EFFECT_STRENGTH_MEDIUM); 42 43 assertEquals(-1, prebaked.getDuration()); 44 assertEquals(VibrationEffect.EFFECT_CLICK, prebaked.getEffectId()); 45 assertEquals(VibrationEffect.EFFECT_STRENGTH_MEDIUM, prebaked.getEffectStrength()); 46 assertTrue(prebaked.shouldFallback()); 47 } 48 49 @Test testSerialization()50 public void testSerialization() { 51 PrebakedSegment original = new PrebakedSegment( 52 VibrationEffect.EFFECT_CLICK, true, VibrationEffect.EFFECT_STRENGTH_MEDIUM); 53 Parcel parcel = Parcel.obtain(); 54 original.writeToParcel(parcel, 0); 55 parcel.setDataPosition(0); 56 assertEquals(original, PrebakedSegment.CREATOR.createFromParcel(parcel)); 57 } 58 59 @Test testValidate()60 public void testValidate() { 61 new PrebakedSegment(VibrationEffect.EFFECT_CLICK, true, 62 VibrationEffect.EFFECT_STRENGTH_MEDIUM).validate(); 63 64 assertThrows(IllegalArgumentException.class, 65 () -> new PrebakedSegment(1000, true, VibrationEffect.EFFECT_STRENGTH_MEDIUM) 66 .validate()); 67 assertThrows(IllegalArgumentException.class, 68 () -> new PrebakedSegment(VibrationEffect.EFFECT_TICK, false, 1000) 69 .validate()); 70 } 71 72 @Test testResolve_ignoresAndReturnsSameEffect()73 public void testResolve_ignoresAndReturnsSameEffect() { 74 PrebakedSegment prebaked = new PrebakedSegment( 75 VibrationEffect.EFFECT_CLICK, true, VibrationEffect.EFFECT_STRENGTH_MEDIUM); 76 assertSame(prebaked, prebaked.resolve(1000)); 77 } 78 79 @Test testApplyEffectStrength()80 public void testApplyEffectStrength() { 81 PrebakedSegment medium = new PrebakedSegment( 82 VibrationEffect.EFFECT_THUD, true, VibrationEffect.EFFECT_STRENGTH_MEDIUM); 83 84 PrebakedSegment light = medium.applyEffectStrength(VibrationEffect.EFFECT_STRENGTH_LIGHT); 85 assertNotEquals(medium, light); 86 assertEquals(medium.getEffectId(), light.getEffectId()); 87 assertEquals(medium.shouldFallback(), light.shouldFallback()); 88 assertEquals(VibrationEffect.EFFECT_STRENGTH_LIGHT, light.getEffectStrength()); 89 90 PrebakedSegment strong = medium.applyEffectStrength(VibrationEffect.EFFECT_STRENGTH_STRONG); 91 assertNotEquals(medium, strong); 92 assertEquals(medium.getEffectId(), strong.getEffectId()); 93 assertEquals(medium.shouldFallback(), strong.shouldFallback()); 94 assertEquals(VibrationEffect.EFFECT_STRENGTH_STRONG, strong.getEffectStrength()); 95 96 assertSame(medium, medium.applyEffectStrength(VibrationEffect.EFFECT_STRENGTH_MEDIUM)); 97 // Invalid vibration effect strength is ignored. 98 assertSame(medium, medium.applyEffectStrength(1000)); 99 } 100 101 @Test testScale_ignoresAndReturnsSameEffect()102 public void testScale_ignoresAndReturnsSameEffect() { 103 PrebakedSegment prebaked = new PrebakedSegment( 104 VibrationEffect.EFFECT_CLICK, true, VibrationEffect.EFFECT_STRENGTH_MEDIUM); 105 assertSame(prebaked, prebaked.scale(0.5f)); 106 } 107 108 @Test testScaleLinearly_ignoresAndReturnsSameEffect()109 public void testScaleLinearly_ignoresAndReturnsSameEffect() { 110 PrebakedSegment prebaked = new PrebakedSegment( 111 VibrationEffect.EFFECT_CLICK, true, VibrationEffect.EFFECT_STRENGTH_MEDIUM); 112 assertSame(prebaked, prebaked.scaleLinearly(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 VibratorInfo info = createVibratorInfoWithSupportedEffects( 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(info)); 163 assertTrue(createSegmentWithFallback(VibrationEffect.EFFECT_CLICK) 164 .areVibrationFeaturesSupported(info)); 165 assertTrue(createSegmentWithFallback(VibrationEffect.EFFECT_DOUBLE_CLICK) 166 .areVibrationFeaturesSupported(info)); 167 assertTrue(createSegmentWithFallback(VibrationEffect.EFFECT_HEAVY_CLICK) 168 .areVibrationFeaturesSupported(info)); 169 170 } 171 172 @Test testVibrationFeaturesSupport_idsWithFallback_fallbackEnabled_noVibratorSupport()173 public void testVibrationFeaturesSupport_idsWithFallback_fallbackEnabled_noVibratorSupport() { 174 VibratorInfo info = createVibratorInfoWithSupportedEffects(new int[0]); 175 176 assertTrue(createSegmentWithFallback(VibrationEffect.EFFECT_TICK) 177 .areVibrationFeaturesSupported(info)); 178 assertTrue(createSegmentWithFallback(VibrationEffect.EFFECT_CLICK) 179 .areVibrationFeaturesSupported(info)); 180 assertTrue(createSegmentWithFallback(VibrationEffect.EFFECT_DOUBLE_CLICK) 181 .areVibrationFeaturesSupported(info)); 182 assertTrue(createSegmentWithFallback(VibrationEffect.EFFECT_HEAVY_CLICK) 183 .areVibrationFeaturesSupported(info)); 184 } 185 186 @Test testVibrationFeaturesSupport_idsWithFallback_fallbackDisabled_vibratorSupport()187 public void testVibrationFeaturesSupport_idsWithFallback_fallbackDisabled_vibratorSupport() { 188 VibratorInfo info = createVibratorInfoWithSupportedEffects( 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(info)); 196 assertTrue(createSegmentWithoutFallback(VibrationEffect.EFFECT_CLICK) 197 .areVibrationFeaturesSupported(info)); 198 assertTrue(createSegmentWithoutFallback(VibrationEffect.EFFECT_DOUBLE_CLICK) 199 .areVibrationFeaturesSupported(info)); 200 assertTrue(createSegmentWithoutFallback(VibrationEffect.EFFECT_HEAVY_CLICK) 201 .areVibrationFeaturesSupported(info)); 202 } 203 204 @Test testVibrationFeaturesSupport_idsWithFallback_fallbackDisabled_noVibratorSupport()205 public void testVibrationFeaturesSupport_idsWithFallback_fallbackDisabled_noVibratorSupport() { 206 VibratorInfo info = createVibratorInfoWithSupportedEffects(new int[0]); 207 208 assertFalse(createSegmentWithoutFallback(VibrationEffect.EFFECT_TICK) 209 .areVibrationFeaturesSupported(info)); 210 assertFalse(createSegmentWithoutFallback(VibrationEffect.EFFECT_CLICK) 211 .areVibrationFeaturesSupported(info)); 212 assertFalse(createSegmentWithoutFallback(VibrationEffect.EFFECT_DOUBLE_CLICK) 213 .areVibrationFeaturesSupported(info)); 214 assertFalse(createSegmentWithoutFallback(VibrationEffect.EFFECT_HEAVY_CLICK) 215 .areVibrationFeaturesSupported(info)); 216 } 217 218 @Test testVibrationFeaturesSupport_idsWithNoFallback_fallbackEnabled_vibratorSupport()219 public void testVibrationFeaturesSupport_idsWithNoFallback_fallbackEnabled_vibratorSupport() { 220 VibratorInfo info = createVibratorInfoWithSupportedEffects( 221 VibrationEffect.EFFECT_THUD, 222 VibrationEffect.EFFECT_POP, 223 VibrationEffect.EFFECT_TEXTURE_TICK); 224 225 assertTrue(createSegmentWithFallback(VibrationEffect.EFFECT_THUD) 226 .areVibrationFeaturesSupported(info)); 227 assertTrue(createSegmentWithFallback(VibrationEffect.EFFECT_POP) 228 .areVibrationFeaturesSupported(info)); 229 assertTrue(createSegmentWithFallback(VibrationEffect.EFFECT_TEXTURE_TICK) 230 .areVibrationFeaturesSupported(info)); 231 } 232 233 @Test testVibrationFeaturesSupport_idsWithNoFallback_fallbackEnabled_noVibratorSupport()234 public void testVibrationFeaturesSupport_idsWithNoFallback_fallbackEnabled_noVibratorSupport() { 235 VibratorInfo info = createVibratorInfoWithSupportedEffects(new int[0]); 236 237 assertFalse(createSegmentWithFallback(VibrationEffect.EFFECT_THUD) 238 .areVibrationFeaturesSupported(info)); 239 assertFalse(createSegmentWithFallback(VibrationEffect.EFFECT_POP) 240 .areVibrationFeaturesSupported(info)); 241 assertFalse(createSegmentWithFallback(VibrationEffect.EFFECT_TEXTURE_TICK) 242 .areVibrationFeaturesSupported(info)); 243 } 244 245 @Test testVibrationFeaturesSupport_idsWithNoFallback_fallbackDisabled_vibratorSupport()246 public void testVibrationFeaturesSupport_idsWithNoFallback_fallbackDisabled_vibratorSupport() { 247 VibratorInfo info = createVibratorInfoWithSupportedEffects( 248 VibrationEffect.EFFECT_THUD, 249 VibrationEffect.EFFECT_POP, 250 VibrationEffect.EFFECT_TEXTURE_TICK); 251 252 assertTrue(createSegmentWithoutFallback(VibrationEffect.EFFECT_THUD) 253 .areVibrationFeaturesSupported(info)); 254 assertTrue(createSegmentWithoutFallback(VibrationEffect.EFFECT_POP) 255 .areVibrationFeaturesSupported(info)); 256 assertTrue(createSegmentWithoutFallback(VibrationEffect.EFFECT_TEXTURE_TICK) 257 .areVibrationFeaturesSupported(info)); 258 } 259 260 @Test testVibrationFeaturesSupport_idsWithNoFallback_fallbackDisabled_noVibSupport()261 public void testVibrationFeaturesSupport_idsWithNoFallback_fallbackDisabled_noVibSupport() { 262 VibratorInfo info = createVibratorInfoWithSupportedEffects(new int[0]); 263 264 assertFalse(createSegmentWithoutFallback(VibrationEffect.EFFECT_THUD) 265 .areVibrationFeaturesSupported(info)); 266 assertFalse(createSegmentWithoutFallback(VibrationEffect.EFFECT_POP) 267 .areVibrationFeaturesSupported(info)); 268 assertFalse(createSegmentWithoutFallback(VibrationEffect.EFFECT_TEXTURE_TICK) 269 .areVibrationFeaturesSupported(info)); 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 createVibratorInfoWithSupportedEffects(int... supportedEffects)289 private static VibratorInfo createVibratorInfoWithSupportedEffects(int... supportedEffects) { 290 return new VibratorInfo.Builder(/* id= */ 1) 291 .setSupportedEffects(supportedEffects) 292 .build(); 293 } 294 } 295