• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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 com.android.server.cts.device.statsdatom;
18 
19 import android.os.VibrationEffect;
20 import android.os.Vibrator;
21 
22 import androidx.test.InstrumentationRegistry;
23 
24 import com.android.compatibility.common.util.NonApiTest;
25 
26 import org.junit.Before;
27 import org.junit.Test;
28 
29 /**
30  * These tests are run by VibratorStatsTest to generate some stats by exercising vibrator APIs.
31  *
32  * <p>They only trigger the APIs, but don't test anything themselves.
33  */
34 @NonApiTest(exemptionReasons = {}, justification = "METRIC")
35 public class VibratorTests {
36     private Vibrator mVibrator;
37 
38     @Before
setUp()39     public void setUp() {
40         mVibrator = InstrumentationRegistry.getContext().getSystemService(Vibrator.class);
41     }
42 
43     @Test
testOneShotVibration()44     public void testOneShotVibration() throws Exception {
45         mVibrator.vibrate(VibrationEffect.createOneShot(500, VibrationEffect.DEFAULT_AMPLITUDE));
46         // Sleep so that the app does not get killed while it's vibrating.
47         Thread.sleep(1000);
48     }
49 
50     @Test
testPredefinedClickVibration()51     public void testPredefinedClickVibration() throws Exception {
52         mVibrator.vibrate(VibrationEffect.createPredefined(VibrationEffect.EFFECT_CLICK));
53         // Sleep so that the app does not get killed while it's vibrating.
54         Thread.sleep(500);
55     }
56 
57     @Test
testComposedTickThenClickVibration()58     public void testComposedTickThenClickVibration() throws Exception {
59         mVibrator.vibrate(
60                 VibrationEffect.startComposition()
61                         .addPrimitive(VibrationEffect.Composition.PRIMITIVE_TICK)
62                         .addPrimitive(VibrationEffect.Composition.PRIMITIVE_CLICK,
63                                 /* scale= */ 0.5f, /* delay= */ 50)
64                         .compose());
65 
66         // Sleep so that the app does not get killed while it's vibrating.
67         Thread.sleep(500);
68     }
69 
70     @Test
testRepeatedWaveformVibration()71     public void testRepeatedWaveformVibration() throws Exception {
72         mVibrator.vibrate(VibrationEffect.createWaveform(new long[] { 50, 50 }, /* repeat= */ 0));
73         // Let the vibration play for a while.
74         Thread.sleep(500);
75         mVibrator.cancel();
76         // Sleep so that the app does not get killed while it's cancelling.
77         Thread.sleep(100);
78     }
79 }
80