1 /* 2 * Copyright (C) 2008 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.fail; 20 21 import android.media.AudioAttributes; 22 import android.os.VibrationEffect; 23 import android.os.Vibrator; 24 25 import androidx.test.InstrumentationRegistry; 26 import androidx.test.filters.LargeTest; 27 import androidx.test.runner.AndroidJUnit4; 28 29 import org.junit.Before; 30 import org.junit.Test; 31 import org.junit.runner.RunWith; 32 33 @RunWith(AndroidJUnit4.class) 34 @LargeTest 35 public class VibratorTest { 36 private static final AudioAttributes AUDIO_ATTRIBUTES = 37 new AudioAttributes.Builder() 38 .setUsage(AudioAttributes.USAGE_MEDIA) 39 .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC) 40 .build(); 41 42 private Vibrator mVibrator; 43 44 @Before setUp()45 public void setUp() { 46 mVibrator = InstrumentationRegistry.getContext().getSystemService(Vibrator.class); 47 } 48 49 @Test testVibratorCancel()50 public void testVibratorCancel() { 51 mVibrator.vibrate(1000); 52 sleep(500); 53 mVibrator.cancel(); 54 } 55 56 @Test testVibratePattern()57 public void testVibratePattern() { 58 long[] pattern = {100, 200, 400, 800, 1600}; 59 mVibrator.vibrate(pattern, 3); 60 try { 61 mVibrator.vibrate(pattern, 10); 62 fail("Should throw ArrayIndexOutOfBoundsException"); 63 } catch (ArrayIndexOutOfBoundsException expected) { } 64 mVibrator.cancel(); 65 } 66 67 @Test testVibrateMultiThread()68 public void testVibrateMultiThread() { 69 new Thread(new Runnable() { 70 public void run() { 71 try { 72 mVibrator.vibrate(100); 73 } catch (Exception e) { 74 fail("MultiThread fail1"); 75 } 76 } 77 }).start(); 78 new Thread(new Runnable() { 79 public void run() { 80 try { 81 // This test only get two threads to run vibrator at the same time 82 // for a functional test, 83 // but it can not verify if the second thread get the precedence. 84 mVibrator.vibrate(1000); 85 } catch (Exception e) { 86 fail("MultiThread fail2"); 87 } 88 } 89 }).start(); 90 sleep(1500); 91 } 92 93 @Test testVibrateOneShot()94 public void testVibrateOneShot() { 95 VibrationEffect oneShot = 96 VibrationEffect.createOneShot(100, VibrationEffect.DEFAULT_AMPLITUDE); 97 mVibrator.vibrate(oneShot); 98 sleep(100); 99 100 oneShot = VibrationEffect.createOneShot(500, 255 /* Max amplitude */); 101 mVibrator.vibrate(oneShot); 102 sleep(100); 103 mVibrator.cancel(); 104 105 oneShot = VibrationEffect.createOneShot(100, 1 /* Min amplitude */); 106 mVibrator.vibrate(oneShot, AUDIO_ATTRIBUTES); 107 sleep(100); 108 } 109 110 @Test testVibrateWaveform()111 public void testVibrateWaveform() { 112 final long[] timings = new long[] {100, 200, 300, 400, 500}; 113 final int[] amplitudes = new int[] {64, 128, 255, 128, 64}; 114 VibrationEffect waveform = VibrationEffect.createWaveform(timings, amplitudes, -1); 115 mVibrator.vibrate(waveform); 116 sleep(1500); 117 118 waveform = VibrationEffect.createWaveform(timings, amplitudes, 0); 119 mVibrator.vibrate(waveform, AUDIO_ATTRIBUTES); 120 sleep(2000); 121 mVibrator.cancel(); 122 } 123 124 @Test testVibratorHasAmplitudeControl()125 public void testVibratorHasAmplitudeControl() { 126 // Just make sure it doesn't crash when this is called; we don't really have a way to test 127 // if the amplitude control works or not. 128 mVibrator.hasAmplitudeControl(); 129 } 130 sleep(long millis)131 private static void sleep(long millis) { 132 try { 133 Thread.sleep(millis); 134 } catch (InterruptedException ignored) { } 135 } 136 } 137