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 android.content.Context;
     20 import android.media.AudioAttributes;
     21 import android.os.Parcel;
     22 import android.os.VibrationEffect;
     23 import android.os.Vibrator;
     24 import android.support.test.InstrumentationRegistry;
     25 import android.support.test.filters.SmallTest;
     26 import android.support.test.runner.AndroidJUnit4;
     27 import android.util.Log;
     28 
     29 import org.junit.Test;
     30 import org.junit.runner.RunWith;
     31 
     32 import java.util.Arrays;
     33 
     34 import static org.junit.Assert.assertEquals;
     35 import static org.junit.Assert.assertNotEquals;
     36 import static org.junit.Assert.fail;
     37 
     38 @SmallTest
     39 @RunWith(AndroidJUnit4.class)
     40 public class VibrationEffectTest {
     41     private static final long TEST_TIMING = 100;
     42     private static final int TEST_AMPLITUDE = 100;
     43 
     44     private static final long[] TEST_TIMINGS = new long[] { 100, 100, 200 };
     45     private static final int[] TEST_AMPLITUDES =
     46             new int[] { 255, 0, VibrationEffect.DEFAULT_AMPLITUDE };
     47 
     48     private static final VibrationEffect TEST_ONE_SHOT =
     49             VibrationEffect.createOneShot(TEST_TIMING, TEST_AMPLITUDE);
     50     private static final VibrationEffect TEST_WAVEFORM =
     51             VibrationEffect.createWaveform(TEST_TIMINGS, TEST_AMPLITUDES, -1);
     52     private static final VibrationEffect TEST_WAVEFORM_NO_AMPLITUDES =
     53             VibrationEffect.createWaveform(TEST_TIMINGS, -1);
     54 
     55 
     56     @Test
     57     public void testCreateOneShot() {
     58         VibrationEffect.createOneShot(100, VibrationEffect.DEFAULT_AMPLITUDE);
     59         VibrationEffect.createOneShot(1, 1);
     60         VibrationEffect.createOneShot(1000, 255);
     61     }
     62 
     63     @Test
     64     public void testCreateOneShotFailsBadTiming() {
     65         try {
     66             VibrationEffect.createOneShot(0, TEST_AMPLITUDE);
     67             fail("Invalid timing, should throw IllegalArgumentException");
     68         } catch (IllegalArgumentException expected) { }
     69     }
     70 
     71     @Test
     72     public void testCreateOneShotFailsBadAmplitude() {
     73         try {
     74             VibrationEffect.createOneShot(TEST_TIMING, -2);
     75             fail("Invalid amplitude, should throw IllegalArgumentException");
     76         } catch (IllegalArgumentException expected) { }
     77 
     78         try {
     79             VibrationEffect.createOneShot(TEST_TIMING, 256);
     80             fail("Invalid amplitude, should throw IllegalArgumentException");
     81         } catch (IllegalArgumentException expected) { }
     82     }
     83 
     84     @Test
     85     public void testOneShotEquals() {
     86         VibrationEffect otherEffect = VibrationEffect.createOneShot(TEST_TIMING, TEST_AMPLITUDE);
     87         assertEquals(TEST_ONE_SHOT, otherEffect);
     88         assertEquals(TEST_ONE_SHOT.hashCode(), otherEffect.hashCode());
     89     }
     90 
     91     @Test
     92     public void testOneShotNotEqualsAmplitude() {
     93         VibrationEffect otherEffect =
     94                 VibrationEffect.createOneShot(TEST_TIMING, TEST_AMPLITUDE - 1);
     95         assertNotEquals(TEST_ONE_SHOT, otherEffect);
     96     }
     97 
     98     @Test
     99     public void testOneShotNotEqualsTiming() {
    100         VibrationEffect otherEffect =
    101                 VibrationEffect.createOneShot(TEST_TIMING - 1, TEST_AMPLITUDE);
    102         assertNotEquals(TEST_ONE_SHOT, otherEffect);
    103     }
    104 
    105     @Test
    106     public void testOneShotEqualsWithDefaultAmplitude() {
    107         VibrationEffect effect =
    108                 VibrationEffect.createOneShot(TEST_TIMING, VibrationEffect.DEFAULT_AMPLITUDE);
    109         VibrationEffect otherEffect =
    110                 VibrationEffect.createOneShot(TEST_TIMING, VibrationEffect.DEFAULT_AMPLITUDE);
    111         assertEquals(effect, otherEffect);
    112         assertEquals(effect.hashCode(), otherEffect.hashCode());
    113     }
    114 
    115     @Test
    116     public void testCreateWaveform() {
    117         VibrationEffect.createWaveform(TEST_TIMINGS, TEST_AMPLITUDES, -1);
    118         VibrationEffect.createWaveform(TEST_TIMINGS, TEST_AMPLITUDES, 0);
    119         VibrationEffect.createWaveform(TEST_TIMINGS, TEST_AMPLITUDES, TEST_AMPLITUDES.length - 1);
    120     }
    121 
    122     @Test
    123     public void testCreateWaveformFailsDifferentArraySize() {
    124         try {
    125             VibrationEffect.createWaveform(
    126                     Arrays.copyOfRange(TEST_TIMINGS, 0, TEST_TIMINGS.length - 1),
    127                     TEST_AMPLITUDES, -1);
    128             fail("Timing and amplitudes arrays are different sizes, " +
    129                     "should throw IllegalArgumentException");
    130         } catch (IllegalArgumentException expected) { }
    131 
    132         try {
    133             VibrationEffect.createWaveform(
    134                     TEST_TIMINGS,
    135                     Arrays.copyOfRange(TEST_AMPLITUDES, 0, TEST_AMPLITUDES.length - 1), -1);
    136             fail("Timing and amplitudes arrays are different sizes, " +
    137                     "should throw IllegalArgumentException");
    138         } catch (IllegalArgumentException expected) { }
    139     }
    140 
    141     @Test
    142     public void testCreateWaveformFailsRepeatIndexOutOfBounds() {
    143         try {
    144             VibrationEffect.createWaveform(TEST_TIMINGS, TEST_AMPLITUDES, -2);
    145             fail("Repeat index is < -1, should throw IllegalArgumentException");
    146         } catch (IllegalArgumentException expected) { }
    147 
    148         try {
    149             VibrationEffect.createWaveform(TEST_TIMINGS, TEST_AMPLITUDES, TEST_AMPLITUDES.length);
    150             fail("Repeat index is >= array length, should throw IllegalArgumentException");
    151         } catch (IllegalArgumentException expected) { }
    152     }
    153 
    154     @Test
    155     public void testCreateWaveformFailsBadTimingValues() {
    156         try {
    157             final long[] badTimings = Arrays.copyOf(TEST_TIMINGS, TEST_TIMINGS.length);
    158             badTimings[1] = -1;
    159             VibrationEffect.createWaveform(badTimings,TEST_AMPLITUDES, -1);
    160             fail("Has a timing < 0, should throw IllegalArgumentException");
    161         } catch (IllegalArgumentException expected) { }
    162 
    163         try {
    164             final long[] badTimings = new long[TEST_TIMINGS.length];
    165             VibrationEffect.createWaveform(badTimings, TEST_AMPLITUDES, -1);
    166             fail("Has no non-zero timings, should throw IllegalArgumentException");
    167         } catch (IllegalArgumentException expected) { }
    168     }
    169 
    170     @Test
    171     public void testCreateWaveformFailsBadAmplitudeValues() {
    172         try {
    173             final int[] badAmplitudes = new int[TEST_TIMINGS.length];
    174             badAmplitudes[1] = -2;
    175             VibrationEffect.createWaveform(TEST_TIMINGS, badAmplitudes, -1);
    176             fail("Has an amplitude < VibrationEffect.DEFAULT_AMPLITUDE, " +
    177                     "should throw IllegalArgumentException");
    178         } catch (IllegalArgumentException expected) { }
    179 
    180         try {
    181             final int[] badAmplitudes = new int[TEST_TIMINGS.length];
    182             badAmplitudes[1] = 256;
    183             VibrationEffect.createWaveform(TEST_TIMINGS, badAmplitudes, -1);
    184             fail("Has an amplitude > 255, should throw IllegalArgumentException");
    185         } catch (IllegalArgumentException expected) { }
    186     }
    187 
    188     @Test
    189     public void testCreateWaveformWithNoAmplitudes() {
    190         VibrationEffect.createWaveform(TEST_TIMINGS, -1);
    191         VibrationEffect.createWaveform(TEST_TIMINGS, 0);
    192         VibrationEffect.createWaveform(TEST_TIMINGS, TEST_TIMINGS.length - 1);
    193     }
    194 
    195     @Test
    196     public void testCreateWaveformWithNoAmplitudesFailsRepeatIndexOutOfBounds() {
    197         try {
    198             VibrationEffect.createWaveform(TEST_TIMINGS, -2);
    199             fail("Repeat index is < -1, should throw IllegalArgumentException");
    200         } catch (IllegalArgumentException expected) { }
    201 
    202         try {
    203             VibrationEffect.createWaveform(TEST_TIMINGS, TEST_TIMINGS.length);
    204             fail("Repeat index is >= timings array length, should throw IllegalArgumentException");
    205         } catch (IllegalArgumentException expected) { }
    206     }
    207 
    208     @Test
    209     public void testWaveformEquals() {
    210         VibrationEffect effect = VibrationEffect.createWaveform(TEST_TIMINGS, TEST_AMPLITUDES, -1);
    211         VibrationEffect otherEffect =
    212                 VibrationEffect.createWaveform(TEST_TIMINGS, TEST_AMPLITUDES, -1);
    213         assertEquals(effect, otherEffect);
    214         assertEquals(effect.hashCode(), otherEffect.hashCode());
    215     }
    216 
    217     @Test
    218     public void testWaveformNotEqualsDifferentRepeatIndex() {
    219         VibrationEffect otherEffect =
    220                 VibrationEffect.createWaveform(TEST_TIMINGS, TEST_AMPLITUDES, 0);
    221         assertNotEquals(TEST_WAVEFORM, otherEffect);
    222     }
    223 
    224     @Test
    225     public void testWaveformNotEqualsDifferentTimingArrayValue() {
    226         long[] newTimings = Arrays.copyOf(TEST_TIMINGS, TEST_TIMINGS.length);
    227         newTimings[0] = 200;
    228         VibrationEffect otherEffect =
    229                 VibrationEffect.createWaveform(newTimings, TEST_AMPLITUDES, -1);
    230         assertNotEquals(TEST_WAVEFORM, otherEffect);
    231     }
    232 
    233     @Test
    234     public void testWaveformNotEqualsDifferentAmplitudeArrayValue() {
    235         int[] newAmplitudes = Arrays.copyOf(TEST_AMPLITUDES, TEST_AMPLITUDES.length);
    236         newAmplitudes[0] = 1;
    237         VibrationEffect otherEffect =
    238                 VibrationEffect.createWaveform(TEST_TIMINGS, newAmplitudes, -1);
    239         assertNotEquals(TEST_WAVEFORM, otherEffect);
    240     }
    241 
    242     @Test
    243     public void testWaveformNotEqualsDifferentArrayLength() {
    244         long[] newTimings = Arrays.copyOfRange(TEST_TIMINGS, 0, TEST_TIMINGS.length - 1);
    245         int[] newAmplitudes = Arrays.copyOfRange(TEST_AMPLITUDES, 0, TEST_AMPLITUDES.length -1);
    246         VibrationEffect otherEffect =
    247                 VibrationEffect.createWaveform(newTimings, newAmplitudes, -1);
    248         assertNotEquals(TEST_WAVEFORM, otherEffect);
    249         assertNotEquals(otherEffect, TEST_WAVEFORM);
    250     }
    251 
    252     @Test
    253     public void testWaveformWithNoAmplitudesEquals() {
    254         VibrationEffect otherEffect = VibrationEffect.createWaveform(TEST_TIMINGS, -1);
    255         assertEquals(TEST_WAVEFORM_NO_AMPLITUDES, otherEffect);
    256         assertEquals(TEST_WAVEFORM_NO_AMPLITUDES.hashCode(), otherEffect.hashCode());
    257     }
    258 
    259     @Test
    260     public void testWaveformWithNoAmplitudesNotEqualsDifferentRepeatIndex() {
    261         VibrationEffect otherEffect = VibrationEffect.createWaveform(TEST_TIMINGS, 0);
    262         assertNotEquals(TEST_WAVEFORM_NO_AMPLITUDES, otherEffect);
    263     }
    264 
    265     @Test
    266     public void testWaveformWithNoAmplitudesNotEqualsDifferentArrayLength() {
    267         long[] newTimings = Arrays.copyOfRange(TEST_TIMINGS, 0, TEST_TIMINGS.length - 1);
    268         VibrationEffect otherEffect = VibrationEffect.createWaveform(newTimings, -1);
    269         assertNotEquals(TEST_WAVEFORM_NO_AMPLITUDES, otherEffect);
    270     }
    271 
    272     @Test
    273     public void testWaveformWithNoAmplitudesNotEqualsDifferentTimingValue() {
    274         long[] newTimings = Arrays.copyOf(TEST_TIMINGS, TEST_TIMINGS.length);
    275         newTimings[0] = 1;
    276         VibrationEffect otherEffect = VibrationEffect.createWaveform(newTimings, -1);
    277         assertNotEquals(TEST_WAVEFORM_NO_AMPLITUDES, otherEffect);
    278     }
    279 
    280     @Test
    281     public void testParceling() {
    282         Parcel p = Parcel.obtain();
    283         TEST_ONE_SHOT.writeToParcel(p, 0);
    284         p.setDataPosition(0);
    285         VibrationEffect parceledEffect = VibrationEffect.CREATOR.createFromParcel(p);
    286         assertEquals(TEST_ONE_SHOT, parceledEffect);
    287 
    288         p.setDataPosition(0);
    289         TEST_WAVEFORM.writeToParcel(p, 0);
    290         p.setDataPosition(0);
    291         parceledEffect = VibrationEffect.CREATOR.createFromParcel(p);
    292         assertEquals(TEST_WAVEFORM, parceledEffect);
    293     }
    294 }
    295