Home | History | Annotate | Download | only in cts
      1 /*
      2  * Copyright (C) 2017 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.cts;
     18 
     19 import static org.junit.Assert.assertArrayEquals;
     20 import static org.junit.Assert.assertEquals;
     21 import static org.junit.Assert.assertNotEquals;
     22 import static org.junit.Assert.fail;
     23 
     24 import android.os.Parcel;
     25 import android.os.VibrationEffect;
     26 
     27 import androidx.test.filters.SmallTest;
     28 import androidx.test.runner.AndroidJUnit4;
     29 
     30 import org.junit.Test;
     31 import org.junit.runner.RunWith;
     32 
     33 import java.util.Arrays;
     34 
     35 @SmallTest
     36 @RunWith(AndroidJUnit4.class)
     37 public class VibrationEffectTest {
     38     private static final long TEST_TIMING = 100;
     39     private static final int TEST_AMPLITUDE = 100;
     40 
     41     private static final long[] TEST_TIMINGS = new long[] { 100, 100, 200 };
     42     private static final int[] TEST_AMPLITUDES =
     43             new int[] { 255, 0, VibrationEffect.DEFAULT_AMPLITUDE };
     44 
     45     private static final VibrationEffect TEST_ONE_SHOT =
     46             VibrationEffect.createOneShot(TEST_TIMING, TEST_AMPLITUDE);
     47     private static final VibrationEffect TEST_WAVEFORM =
     48             VibrationEffect.createWaveform(TEST_TIMINGS, TEST_AMPLITUDES, -1);
     49     private static final VibrationEffect TEST_WAVEFORM_NO_AMPLITUDES =
     50             VibrationEffect.createWaveform(TEST_TIMINGS, -1);
     51     private static final VibrationEffect TEST_PREBAKED =
     52             VibrationEffect.get(VibrationEffect.EFFECT_CLICK, true);
     53 
     54 
     55     @Test
     56     public void testCreateOneShot() {
     57         VibrationEffect e = VibrationEffect.createOneShot(100, VibrationEffect.DEFAULT_AMPLITUDE);
     58         assertEquals(100, e.getDuration());
     59         assertEquals(VibrationEffect.DEFAULT_AMPLITUDE,
     60                 ((VibrationEffect.OneShot)e).getAmplitude());
     61         e = VibrationEffect.createOneShot(1, 1);
     62         assertEquals(1, e.getDuration());
     63         assertEquals(1, ((VibrationEffect.OneShot)e).getAmplitude());
     64         e = VibrationEffect.createOneShot(1000, 255);
     65         assertEquals(1000, e.getDuration());
     66         assertEquals(255, ((VibrationEffect.OneShot)e).getAmplitude());
     67     }
     68 
     69     @Test
     70     public void testCreateOneShotFailsBadTiming() {
     71         try {
     72             VibrationEffect.createOneShot(0, TEST_AMPLITUDE);
     73             fail("Invalid timing, should throw IllegalArgumentException");
     74         } catch (IllegalArgumentException expected) { }
     75     }
     76 
     77     @Test
     78     public void testCreateOneShotFailsBadAmplitude() {
     79         try {
     80             VibrationEffect.createOneShot(TEST_TIMING, -2);
     81             fail("Invalid amplitude, should throw IllegalArgumentException");
     82         } catch (IllegalArgumentException expected) { }
     83 
     84         try {
     85             VibrationEffect.createOneShot(TEST_TIMING, 0);
     86             fail("Invalid amplitude, should throw IllegalArgumentException");
     87         } catch (IllegalArgumentException expected) { }
     88 
     89         try {
     90             VibrationEffect.createOneShot(TEST_TIMING, 256);
     91             fail("Invalid amplitude, should throw IllegalArgumentException");
     92         } catch (IllegalArgumentException expected) { }
     93     }
     94 
     95     @Test
     96     public void testOneShotEquals() {
     97         VibrationEffect otherEffect = VibrationEffect.createOneShot(TEST_TIMING, TEST_AMPLITUDE);
     98         assertEquals(TEST_ONE_SHOT, otherEffect);
     99         assertEquals(TEST_ONE_SHOT.hashCode(), otherEffect.hashCode());
    100     }
    101 
    102     @Test
    103     public void testOneShotNotEqualsAmplitude() {
    104         VibrationEffect otherEffect =
    105                 VibrationEffect.createOneShot(TEST_TIMING, TEST_AMPLITUDE - 1);
    106         assertNotEquals(TEST_ONE_SHOT, otherEffect);
    107     }
    108 
    109     @Test
    110     public void testOneShotNotEqualsTiming() {
    111         VibrationEffect otherEffect =
    112                 VibrationEffect.createOneShot(TEST_TIMING - 1, TEST_AMPLITUDE);
    113         assertNotEquals(TEST_ONE_SHOT, otherEffect);
    114     }
    115 
    116     @Test
    117     public void testOneShotEqualsWithDefaultAmplitude() {
    118         VibrationEffect effect =
    119                 VibrationEffect.createOneShot(TEST_TIMING, VibrationEffect.DEFAULT_AMPLITUDE);
    120         VibrationEffect otherEffect =
    121                 VibrationEffect.createOneShot(TEST_TIMING, VibrationEffect.DEFAULT_AMPLITUDE);
    122         assertEquals(effect, otherEffect);
    123         assertEquals(effect.hashCode(), otherEffect.hashCode());
    124     }
    125 
    126     @Test
    127     public void testCreatePrebaked() {
    128         int[] ids = { VibrationEffect.EFFECT_CLICK, VibrationEffect.EFFECT_DOUBLE_CLICK,
    129                 VibrationEffect.EFFECT_TICK, VibrationEffect.EFFECT_THUD,
    130                 VibrationEffect.EFFECT_POP, VibrationEffect.EFFECT_HEAVY_CLICK,
    131                 VibrationEffect.EFFECT_TEXTURE_TICK };
    132         boolean[] fallbacks = { false, true };
    133         for (int id : ids) {
    134             for (boolean fallback : fallbacks) {
    135                 VibrationEffect.Prebaked effect = (VibrationEffect.Prebaked)
    136                         VibrationEffect.get(id, fallback);
    137                 assertEquals(id, effect.getId());
    138                 assertEquals(fallback, effect.shouldFallback());
    139                 assertEquals(-1, effect.getDuration());
    140             }
    141         }
    142     }
    143 
    144     @Test
    145     public void testCreateWaveform() {
    146         VibrationEffect.Waveform effect = (VibrationEffect.Waveform)
    147                 VibrationEffect.createWaveform(TEST_TIMINGS, TEST_AMPLITUDES, -1);
    148         assertArrayEquals(TEST_TIMINGS, effect.getTimings());
    149         assertArrayEquals(TEST_AMPLITUDES, effect.getAmplitudes());
    150         assertEquals(-1, effect.getRepeatIndex());
    151         assertEquals(400, effect.getDuration());
    152         effect = (VibrationEffect.Waveform)
    153             VibrationEffect.createWaveform(TEST_TIMINGS, TEST_AMPLITUDES, 0);
    154         assertArrayEquals(TEST_TIMINGS, effect.getTimings());
    155         assertArrayEquals(TEST_AMPLITUDES, effect.getAmplitudes());
    156         assertEquals(0, effect.getRepeatIndex());
    157         assertEquals(Long.MAX_VALUE, effect.getDuration());
    158         effect = (VibrationEffect.Waveform)VibrationEffect.createWaveform(TEST_TIMINGS,
    159                 TEST_AMPLITUDES, TEST_AMPLITUDES.length - 1);
    160         assertArrayEquals(TEST_TIMINGS, effect.getTimings());
    161         assertArrayEquals(TEST_AMPLITUDES, effect.getAmplitudes());
    162         assertEquals(TEST_AMPLITUDES.length - 1, effect.getRepeatIndex());
    163         assertEquals(Long.MAX_VALUE, effect.getDuration());
    164     }
    165 
    166     @Test
    167     public void testCreateWaveformFailsDifferentArraySize() {
    168         try {
    169             VibrationEffect.createWaveform(
    170                     Arrays.copyOfRange(TEST_TIMINGS, 0, TEST_TIMINGS.length - 1),
    171                     TEST_AMPLITUDES, -1);
    172             fail("Timing and amplitudes arrays are different sizes, " +
    173                     "should throw IllegalArgumentException");
    174         } catch (IllegalArgumentException expected) { }
    175 
    176         try {
    177             VibrationEffect.createWaveform(
    178                     TEST_TIMINGS,
    179                     Arrays.copyOfRange(TEST_AMPLITUDES, 0, TEST_AMPLITUDES.length - 1), -1);
    180             fail("Timing and amplitudes arrays are different sizes, " +
    181                     "should throw IllegalArgumentException");
    182         } catch (IllegalArgumentException expected) { }
    183     }
    184 
    185     @Test
    186     public void testCreateWaveformFailsRepeatIndexOutOfBounds() {
    187         try {
    188             VibrationEffect.createWaveform(TEST_TIMINGS, TEST_AMPLITUDES, -2);
    189             fail("Repeat index is < -1, should throw IllegalArgumentException");
    190         } catch (IllegalArgumentException expected) { }
    191 
    192         try {
    193             VibrationEffect.createWaveform(TEST_TIMINGS, TEST_AMPLITUDES, TEST_AMPLITUDES.length);
    194             fail("Repeat index is >= array length, should throw IllegalArgumentException");
    195         } catch (IllegalArgumentException expected) { }
    196     }
    197 
    198     @Test
    199     public void testCreateWaveformFailsBadTimingValues() {
    200         try {
    201             final long[] badTimings = Arrays.copyOf(TEST_TIMINGS, TEST_TIMINGS.length);
    202             badTimings[1] = -1;
    203             VibrationEffect.createWaveform(badTimings,TEST_AMPLITUDES, -1);
    204             fail("Has a timing < 0, should throw IllegalArgumentException");
    205         } catch (IllegalArgumentException expected) { }
    206 
    207         try {
    208             final long[] badTimings = new long[TEST_TIMINGS.length];
    209             VibrationEffect.createWaveform(badTimings, TEST_AMPLITUDES, -1);
    210             fail("Has no non-zero timings, should throw IllegalArgumentException");
    211         } catch (IllegalArgumentException expected) { }
    212     }
    213 
    214     @Test
    215     public void testCreateWaveformFailsBadAmplitudeValues() {
    216         try {
    217             final int[] badAmplitudes = new int[TEST_TIMINGS.length];
    218             badAmplitudes[1] = -2;
    219             VibrationEffect.createWaveform(TEST_TIMINGS, badAmplitudes, -1);
    220             fail("Has an amplitude < VibrationEffect.DEFAULT_AMPLITUDE, " +
    221                     "should throw IllegalArgumentException");
    222         } catch (IllegalArgumentException expected) { }
    223 
    224         try {
    225             final int[] badAmplitudes = new int[TEST_TIMINGS.length];
    226             badAmplitudes[1] = 256;
    227             VibrationEffect.createWaveform(TEST_TIMINGS, badAmplitudes, -1);
    228             fail("Has an amplitude > 255, should throw IllegalArgumentException");
    229         } catch (IllegalArgumentException expected) { }
    230     }
    231 
    232     @Test
    233     public void testCreateWaveformWithNoAmplitudes() {
    234         VibrationEffect.createWaveform(TEST_TIMINGS, -1);
    235         VibrationEffect.createWaveform(TEST_TIMINGS, 0);
    236         VibrationEffect.createWaveform(TEST_TIMINGS, TEST_TIMINGS.length - 1);
    237     }
    238 
    239     @Test
    240     public void testCreateWaveformWithNoAmplitudesFailsRepeatIndexOutOfBounds() {
    241         try {
    242             VibrationEffect.createWaveform(TEST_TIMINGS, -2);
    243             fail("Repeat index is < -1, should throw IllegalArgumentException");
    244         } catch (IllegalArgumentException expected) { }
    245 
    246         try {
    247             VibrationEffect.createWaveform(TEST_TIMINGS, TEST_TIMINGS.length);
    248             fail("Repeat index is >= timings array length, should throw IllegalArgumentException");
    249         } catch (IllegalArgumentException expected) { }
    250     }
    251 
    252     @Test
    253     public void testWaveformEquals() {
    254         VibrationEffect effect = VibrationEffect.createWaveform(TEST_TIMINGS, TEST_AMPLITUDES, -1);
    255         VibrationEffect otherEffect =
    256                 VibrationEffect.createWaveform(TEST_TIMINGS, TEST_AMPLITUDES, -1);
    257         assertEquals(effect, otherEffect);
    258         assertEquals(effect.hashCode(), otherEffect.hashCode());
    259     }
    260 
    261     @Test
    262     public void testWaveformNotEqualsDifferentRepeatIndex() {
    263         VibrationEffect otherEffect =
    264                 VibrationEffect.createWaveform(TEST_TIMINGS, TEST_AMPLITUDES, 0);
    265         assertNotEquals(TEST_WAVEFORM, otherEffect);
    266     }
    267 
    268     @Test
    269     public void testWaveformNotEqualsDifferentTimingArrayValue() {
    270         long[] newTimings = Arrays.copyOf(TEST_TIMINGS, TEST_TIMINGS.length);
    271         newTimings[0] = 200;
    272         VibrationEffect otherEffect =
    273                 VibrationEffect.createWaveform(newTimings, TEST_AMPLITUDES, -1);
    274         assertNotEquals(TEST_WAVEFORM, otherEffect);
    275     }
    276 
    277     @Test
    278     public void testWaveformNotEqualsDifferentAmplitudeArrayValue() {
    279         int[] newAmplitudes = Arrays.copyOf(TEST_AMPLITUDES, TEST_AMPLITUDES.length);
    280         newAmplitudes[0] = 1;
    281         VibrationEffect otherEffect =
    282                 VibrationEffect.createWaveform(TEST_TIMINGS, newAmplitudes, -1);
    283         assertNotEquals(TEST_WAVEFORM, otherEffect);
    284     }
    285 
    286     @Test
    287     public void testWaveformNotEqualsDifferentArrayLength() {
    288         long[] newTimings = Arrays.copyOfRange(TEST_TIMINGS, 0, TEST_TIMINGS.length - 1);
    289         int[] newAmplitudes = Arrays.copyOfRange(TEST_AMPLITUDES, 0, TEST_AMPLITUDES.length -1);
    290         VibrationEffect otherEffect =
    291                 VibrationEffect.createWaveform(newTimings, newAmplitudes, -1);
    292         assertNotEquals(TEST_WAVEFORM, otherEffect);
    293         assertNotEquals(otherEffect, TEST_WAVEFORM);
    294     }
    295 
    296     @Test
    297     public void testWaveformWithNoAmplitudesEquals() {
    298         VibrationEffect otherEffect = VibrationEffect.createWaveform(TEST_TIMINGS, -1);
    299         assertEquals(TEST_WAVEFORM_NO_AMPLITUDES, otherEffect);
    300         assertEquals(TEST_WAVEFORM_NO_AMPLITUDES.hashCode(), otherEffect.hashCode());
    301     }
    302 
    303     @Test
    304     public void testWaveformWithNoAmplitudesNotEqualsDifferentRepeatIndex() {
    305         VibrationEffect otherEffect = VibrationEffect.createWaveform(TEST_TIMINGS, 0);
    306         assertNotEquals(TEST_WAVEFORM_NO_AMPLITUDES, otherEffect);
    307     }
    308 
    309     @Test
    310     public void testWaveformWithNoAmplitudesNotEqualsDifferentArrayLength() {
    311         long[] newTimings = Arrays.copyOfRange(TEST_TIMINGS, 0, TEST_TIMINGS.length - 1);
    312         VibrationEffect otherEffect = VibrationEffect.createWaveform(newTimings, -1);
    313         assertNotEquals(TEST_WAVEFORM_NO_AMPLITUDES, otherEffect);
    314     }
    315 
    316     @Test
    317     public void testWaveformWithNoAmplitudesNotEqualsDifferentTimingValue() {
    318         long[] newTimings = Arrays.copyOf(TEST_TIMINGS, TEST_TIMINGS.length);
    319         newTimings[0] = 1;
    320         VibrationEffect otherEffect = VibrationEffect.createWaveform(newTimings, -1);
    321         assertNotEquals(TEST_WAVEFORM_NO_AMPLITUDES, otherEffect);
    322     }
    323 
    324     @Test
    325     public void testParcelingOneShot() {
    326         Parcel p = Parcel.obtain();
    327         TEST_ONE_SHOT.writeToParcel(p, 0);
    328         p.setDataPosition(0);
    329         VibrationEffect parceledEffect = VibrationEffect.CREATOR.createFromParcel(p);
    330         assertEquals(TEST_ONE_SHOT, parceledEffect);
    331     }
    332 
    333     @Test
    334     public void testParcelingWaveForm() {
    335         Parcel p = Parcel.obtain();
    336         TEST_WAVEFORM.writeToParcel(p, 0);
    337         p.setDataPosition(0);
    338         VibrationEffect parceledEffect = VibrationEffect.CREATOR.createFromParcel(p);
    339         assertEquals(TEST_WAVEFORM, parceledEffect);
    340     }
    341 
    342     @Test
    343     public void testParcelingPrebaked() {
    344         Parcel p = Parcel.obtain();
    345         TEST_PREBAKED.writeToParcel(p, 0);
    346         p.setDataPosition(0);
    347         VibrationEffect parceledEffect = VibrationEffect.CREATOR.createFromParcel(p);
    348         assertEquals(TEST_PREBAKED, parceledEffect);
    349     }
    350 
    351     @Test
    352     public void testDescribeContents() {
    353         TEST_ONE_SHOT.describeContents();
    354         TEST_WAVEFORM.describeContents();
    355         TEST_WAVEFORM_NO_AMPLITUDES.describeContents();
    356         TEST_PREBAKED.describeContents();
    357     }
    358 
    359     @Test
    360     public void testSetStrength() {
    361         VibrationEffect.Prebaked effect = (VibrationEffect.Prebaked)VibrationEffect.get(
    362                 VibrationEffect.EFFECT_CLICK, true);
    363         int[] strengths = {
    364                 VibrationEffect.EFFECT_STRENGTH_LIGHT,
    365                 VibrationEffect.EFFECT_STRENGTH_MEDIUM,
    366                 VibrationEffect.EFFECT_STRENGTH_STRONG
    367         };
    368         for (int strength : strengths) {
    369             effect.setEffectStrength(strength);
    370             assertEquals(strength, effect.getEffectStrength());
    371         }
    372     }
    373 
    374     @Test
    375     public void testSetStrengthInvalid() {
    376         VibrationEffect.Prebaked effect = (VibrationEffect.Prebaked)VibrationEffect.get(
    377                 VibrationEffect.EFFECT_CLICK, true);
    378         try {
    379             effect.setEffectStrength(239017);
    380             fail("Illegal strength, should throw IllegalArgumentException");
    381         } catch (IllegalArgumentException expected) {}
    382     }
    383 
    384     @Test
    385     public void testPrebakedEquals() {
    386         VibrationEffect otherEffect = VibrationEffect.get(VibrationEffect.EFFECT_CLICK, true);
    387         assertEquals(TEST_PREBAKED, otherEffect);
    388         assertEquals(TEST_PREBAKED.hashCode(), otherEffect.hashCode());
    389     }
    390 
    391     @Test
    392     public void testToString() {
    393         TEST_ONE_SHOT.toString();
    394         TEST_WAVEFORM.toString();
    395         TEST_PREBAKED.toString();
    396     }
    397 }
    398