1 /*
2  * Copyright (C) 2019 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.assertEquals;
20 import static org.junit.Assert.assertNotEquals;
21 
22 import android.media.AudioAttributes;
23 import android.os.VibrationAttributes;
24 
25 import androidx.test.filters.SmallTest;
26 import androidx.test.runner.AndroidJUnit4;
27 
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 
31 import java.util.HashMap;
32 import java.util.Map;
33 
34 @SmallTest
35 @RunWith(AndroidJUnit4.class)
36 public class VibrationAttributesTest {
37     private static final Map<Integer, Integer> AUDIO_TO_VIBRATION_USAGE_MAP;
38     static {
39         Map<Integer, Integer> map = new HashMap<>();
map.put(AudioAttributes.USAGE_ALARM, VibrationAttributes.USAGE_ALARM)40         map.put(AudioAttributes.USAGE_ALARM, VibrationAttributes.USAGE_ALARM);
map.put(AudioAttributes.USAGE_ASSISTANCE_ACCESSIBILITY, VibrationAttributes.USAGE_ACCESSIBILITY)41         map.put(AudioAttributes.USAGE_ASSISTANCE_ACCESSIBILITY,
42                 VibrationAttributes.USAGE_ACCESSIBILITY);
map.put(AudioAttributes.USAGE_ASSISTANCE_SONIFICATION, VibrationAttributes.USAGE_TOUCH)43         map.put(AudioAttributes.USAGE_ASSISTANCE_SONIFICATION, VibrationAttributes.USAGE_TOUCH);
map.put(AudioAttributes.USAGE_ASSISTANCE_NAVIGATION_GUIDANCE, VibrationAttributes.USAGE_COMMUNICATION_REQUEST)44         map.put(AudioAttributes.USAGE_ASSISTANCE_NAVIGATION_GUIDANCE,
45                 VibrationAttributes.USAGE_COMMUNICATION_REQUEST);
map.put(AudioAttributes.USAGE_ASSISTANT, VibrationAttributes.USAGE_COMMUNICATION_REQUEST)46         map.put(AudioAttributes.USAGE_ASSISTANT, VibrationAttributes.USAGE_COMMUNICATION_REQUEST);
map.put(AudioAttributes.USAGE_GAME, VibrationAttributes.USAGE_MEDIA)47         map.put(AudioAttributes.USAGE_GAME, VibrationAttributes.USAGE_MEDIA);
map.put(AudioAttributes.USAGE_MEDIA, VibrationAttributes.USAGE_MEDIA)48         map.put(AudioAttributes.USAGE_MEDIA, VibrationAttributes.USAGE_MEDIA);
map.put(AudioAttributes.USAGE_NOTIFICATION, VibrationAttributes.USAGE_NOTIFICATION)49         map.put(AudioAttributes.USAGE_NOTIFICATION, VibrationAttributes.USAGE_NOTIFICATION);
map.put(AudioAttributes.USAGE_NOTIFICATION_COMMUNICATION_DELAYED, VibrationAttributes.USAGE_NOTIFICATION)50         map.put(AudioAttributes.USAGE_NOTIFICATION_COMMUNICATION_DELAYED,
51                 VibrationAttributes.USAGE_NOTIFICATION);
map.put(AudioAttributes.USAGE_NOTIFICATION_COMMUNICATION_REQUEST, VibrationAttributes.USAGE_NOTIFICATION)52         map.put(AudioAttributes.USAGE_NOTIFICATION_COMMUNICATION_REQUEST,
53                 VibrationAttributes.USAGE_NOTIFICATION);
map.put(AudioAttributes.USAGE_NOTIFICATION_COMMUNICATION_INSTANT, VibrationAttributes.USAGE_NOTIFICATION)54         map.put(AudioAttributes.USAGE_NOTIFICATION_COMMUNICATION_INSTANT,
55                 VibrationAttributes.USAGE_NOTIFICATION);
map.put(AudioAttributes.USAGE_NOTIFICATION_EVENT, VibrationAttributes.USAGE_NOTIFICATION)56         map.put(AudioAttributes.USAGE_NOTIFICATION_EVENT, VibrationAttributes.USAGE_NOTIFICATION);
map.put(AudioAttributes.USAGE_NOTIFICATION_RINGTONE, VibrationAttributes.USAGE_RINGTONE)57         map.put(AudioAttributes.USAGE_NOTIFICATION_RINGTONE, VibrationAttributes.USAGE_RINGTONE);
map.put(AudioAttributes.USAGE_UNKNOWN, VibrationAttributes.USAGE_UNKNOWN)58         map.put(AudioAttributes.USAGE_UNKNOWN, VibrationAttributes.USAGE_UNKNOWN);
map.put(AudioAttributes.USAGE_VOICE_COMMUNICATION, VibrationAttributes.USAGE_COMMUNICATION_REQUEST)59         map.put(AudioAttributes.USAGE_VOICE_COMMUNICATION,
60                 VibrationAttributes.USAGE_COMMUNICATION_REQUEST);
map.put(AudioAttributes.USAGE_VOICE_COMMUNICATION_SIGNALLING, VibrationAttributes.USAGE_COMMUNICATION_REQUEST)61         map.put(AudioAttributes.USAGE_VOICE_COMMUNICATION_SIGNALLING,
62                 VibrationAttributes.USAGE_COMMUNICATION_REQUEST);
63         AUDIO_TO_VIBRATION_USAGE_MAP = map;
64     }
65 
66     private static final Map<Integer, Integer> VIBRATION_TO_AUDIO_USAGE_MAP;
67     static {
68         Map<Integer, Integer> map = new HashMap<>();
map.put(VibrationAttributes.USAGE_ALARM, AudioAttributes.USAGE_ALARM)69         map.put(VibrationAttributes.USAGE_ALARM, AudioAttributes.USAGE_ALARM);
map.put(VibrationAttributes.USAGE_ACCESSIBILITY, AudioAttributes.USAGE_ASSISTANCE_ACCESSIBILITY)70         map.put(VibrationAttributes.USAGE_ACCESSIBILITY,
71                 AudioAttributes.USAGE_ASSISTANCE_ACCESSIBILITY);
map.put(VibrationAttributes.USAGE_COMMUNICATION_REQUEST, AudioAttributes.USAGE_VOICE_COMMUNICATION)72         map.put(VibrationAttributes.USAGE_COMMUNICATION_REQUEST,
73                 AudioAttributes.USAGE_VOICE_COMMUNICATION);
map.put(VibrationAttributes.USAGE_HARDWARE_FEEDBACK, AudioAttributes.USAGE_UNKNOWN)74         map.put(VibrationAttributes.USAGE_HARDWARE_FEEDBACK, AudioAttributes.USAGE_UNKNOWN);
map.put(VibrationAttributes.USAGE_MEDIA, AudioAttributes.USAGE_MEDIA)75         map.put(VibrationAttributes.USAGE_MEDIA, AudioAttributes.USAGE_MEDIA);
map.put(VibrationAttributes.USAGE_NOTIFICATION, AudioAttributes.USAGE_NOTIFICATION)76         map.put(VibrationAttributes.USAGE_NOTIFICATION, AudioAttributes.USAGE_NOTIFICATION);
map.put(VibrationAttributes.USAGE_PHYSICAL_EMULATION, AudioAttributes.USAGE_UNKNOWN)77         map.put(VibrationAttributes.USAGE_PHYSICAL_EMULATION, AudioAttributes.USAGE_UNKNOWN);
map.put(VibrationAttributes.USAGE_RINGTONE, AudioAttributes.USAGE_NOTIFICATION_RINGTONE)78         map.put(VibrationAttributes.USAGE_RINGTONE, AudioAttributes.USAGE_NOTIFICATION_RINGTONE);
map.put(VibrationAttributes.USAGE_TOUCH, AudioAttributes.USAGE_ASSISTANCE_SONIFICATION)79         map.put(VibrationAttributes.USAGE_TOUCH, AudioAttributes.USAGE_ASSISTANCE_SONIFICATION);
80         VIBRATION_TO_AUDIO_USAGE_MAP = map;
81     }
82 
83     @Test
testCreateFromVibrationAttributes()84     public void testCreateFromVibrationAttributes() {
85         VibrationAttributes tmp = VibrationAttributes.createForUsage(
86                 VibrationAttributes.USAGE_TOUCH);
87         VibrationAttributes attr = new VibrationAttributes.Builder(tmp).build();
88         assertEquals(attr.getUsage(), VibrationAttributes.USAGE_TOUCH);
89         assertEquals(attr.getUsageClass(), VibrationAttributes.USAGE_CLASS_FEEDBACK);
90         assertEquals(attr.getFlags(), 0);
91         assertEquals(attr.getAudioUsage(), AudioAttributes.USAGE_ASSISTANCE_SONIFICATION);
92     }
93 
94     @Test
testCreateFromAudioAttributes()95     public void testCreateFromAudioAttributes() {
96         AudioAttributes audioAttributes = createAudioAttributes(AudioAttributes.USAGE_ALARM);
97         VibrationAttributes attr = new VibrationAttributes.Builder(audioAttributes).build();
98         assertEquals(attr.getUsage(), VibrationAttributes.USAGE_ALARM);
99         assertEquals(attr.getUsageClass(), VibrationAttributes.USAGE_CLASS_ALARM);
100         assertEquals(attr.getFlags(), 0);
101         assertEquals(attr.getAudioUsage(), AudioAttributes.USAGE_ALARM);
102     }
103 
104     @Test
testCreateForUsage()105     public void testCreateForUsage() {
106         VibrationAttributes attr = VibrationAttributes.createForUsage(
107                 VibrationAttributes.USAGE_RINGTONE);
108         assertEquals(attr.getUsage(), VibrationAttributes.USAGE_RINGTONE);
109         assertEquals(attr.getUsageClass(), VibrationAttributes.USAGE_CLASS_ALARM);
110         assertEquals(attr.getFlags(), 0);
111         assertEquals(attr.getAudioUsage(), AudioAttributes.USAGE_NOTIFICATION_RINGTONE);
112     }
113 
114     @Test
testGetAudioUsageReturnOriginalUsage()115     public void testGetAudioUsageReturnOriginalUsage() {
116         VibrationAttributes attr = createForAudioUsage(AudioAttributes.USAGE_VOICE_COMMUNICATION);
117         assertEquals(attr.getUsage(), VibrationAttributes.USAGE_COMMUNICATION_REQUEST);
118         assertEquals(attr.getAudioUsage(), AudioAttributes.USAGE_VOICE_COMMUNICATION);
119     }
120 
121     @Test
testGetAudioUsageUnknownReturnsBasedOnVibrationUsage()122     public void testGetAudioUsageUnknownReturnsBasedOnVibrationUsage() {
123         VibrationAttributes attr = VibrationAttributes.createForUsage(
124                 VibrationAttributes.USAGE_NOTIFICATION);
125         assertEquals(attr.getUsage(), VibrationAttributes.USAGE_NOTIFICATION);
126         assertEquals(attr.getAudioUsage(), AudioAttributes.USAGE_NOTIFICATION);
127     }
128 
129     @Test
testAudioToVibrationUsageMapping()130     public void testAudioToVibrationUsageMapping() {
131         for (Map.Entry<Integer, Integer> entry : AUDIO_TO_VIBRATION_USAGE_MAP.entrySet()) {
132             assertEquals(entry.getValue().intValue(), new VibrationAttributes.Builder(
133                     createForAudioUsage(entry.getKey())).build().getUsage());
134         }
135     }
136 
137     @Test
testVibrationToAudioUsageMapping()138     public void testVibrationToAudioUsageMapping() {
139         for (Map.Entry<Integer, Integer> entry : VIBRATION_TO_AUDIO_USAGE_MAP.entrySet()) {
140             assertEquals(entry.getValue().intValue(),
141                     new VibrationAttributes.Builder()
142                             .setUsage(entry.getKey())
143                             .build()
144                             .getAudioUsage());
145         }
146     }
147 
148     @Test
testEquals()149     public void testEquals() {
150         AudioAttributes audioAttributes = createAudioAttributes(
151                 AudioAttributes.USAGE_ASSISTANCE_ACCESSIBILITY);
152         VibrationAttributes attr = new VibrationAttributes.Builder(audioAttributes).build();
153         VibrationAttributes attr2 = new VibrationAttributes.Builder(audioAttributes).build();
154         assertEquals(attr, attr2);
155     }
156 
157     @Test
testNotEqualsDifferentAudioUsage()158     public void testNotEqualsDifferentAudioUsage() {
159         VibrationAttributes attr = createForAudioUsage(AudioAttributes.USAGE_NOTIFICATION);
160         VibrationAttributes attr2 = createForAudioUsage(AudioAttributes.USAGE_NOTIFICATION_EVENT);
161         assertEquals(attr.getUsage(), attr2.getUsage());
162         assertNotEquals(attr, attr2);
163     }
164 
165     @Test
testNotEqualsDifferentVibrationUsage()166     public void testNotEqualsDifferentVibrationUsage() {
167         VibrationAttributes attr = VibrationAttributes.createForUsage(
168                 VibrationAttributes.USAGE_TOUCH);
169         VibrationAttributes attr2 = VibrationAttributes.createForUsage(
170                 VibrationAttributes.USAGE_NOTIFICATION);
171         assertNotEquals(attr, attr2);
172     }
173 
174     @Test
testNotEqualsDifferentFlags()175     public void testNotEqualsDifferentFlags() {
176         AudioAttributes audioAttributes = createAudioAttributes(
177                 AudioAttributes.USAGE_ASSISTANCE_SONIFICATION);
178         VibrationAttributes attr = new VibrationAttributes.Builder(audioAttributes).build();
179         VibrationAttributes attr2 = new VibrationAttributes.Builder(audioAttributes).setFlags(1, 1)
180                 .build();
181         assertNotEquals(attr, attr2);
182     }
183 
createAudioAttributes(int audioUsage)184     private static AudioAttributes createAudioAttributes(int audioUsage) {
185         return new AudioAttributes.Builder().setUsage(audioUsage).build();
186     }
187 
createForAudioUsage(int audioUsage)188     private static VibrationAttributes createForAudioUsage(int audioUsage) {
189         return new VibrationAttributes.Builder(createAudioAttributes(audioUsage)).build();
190     }
191 }
192 
193