• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 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.audiopolicytest;
18 
19 import static androidx.test.core.app.ApplicationProvider.getApplicationContext;
20 
21 import static com.android.audiopolicytest.AudioVolumeTestUtil.DEFAULT_ATTRIBUTES;
22 
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotEquals;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertTrue;
27 
28 import android.media.AudioAttributes;
29 import android.media.AudioManager;
30 import android.media.AudioSystem;
31 import android.media.audiopolicy.AudioProductStrategy;
32 import android.media.audiopolicy.AudioVolumeGroup;
33 import android.platform.test.annotations.Presubmit;
34 
35 import androidx.test.ext.junit.runners.AndroidJUnit4;
36 
37 import org.junit.Before;
38 import org.junit.Rule;
39 import org.junit.Test;
40 import org.junit.runner.RunWith;
41 
42 import java.util.List;
43 
44 @Presubmit
45 @RunWith(AndroidJUnit4.class)
46 public class AudioVolumeGroupTest {
47     private static final String TAG = "AudioVolumeGroupTest";
48 
49     @Rule
50     public final AudioVolumesTestRule rule = new AudioVolumesTestRule();
51 
52     private AudioManager mAudioManager;
53 
54     @Before
setUp()55     public void setUp() {
56         mAudioManager = getApplicationContext().getSystemService(AudioManager.class);
57     }
58 
59     //-----------------------------------------------------------------
60     // Test getAudioVolumeGroups and validate groud id
61     //-----------------------------------------------------------------
62     @Test
testGetVolumeGroupsFromNonServiceCaller()63     public void testGetVolumeGroupsFromNonServiceCaller() {
64         // The transaction behind getAudioVolumeGroups will fail. Check is done at binder level
65         // with policy service. Error is not reported, the list is just empty.
66         // Request must come from service components
67         List<AudioVolumeGroup> audioVolumeGroup = AudioVolumeGroup.getAudioVolumeGroups();
68 
69         assertNotNull(audioVolumeGroup);
70         assertEquals(audioVolumeGroup.size(), 0);
71     }
72 
73     //-----------------------------------------------------------------
74     // Test getAudioVolumeGroups and validate groud id
75     //-----------------------------------------------------------------
76     @Test
testGetVolumeGroups()77     public void testGetVolumeGroups() {
78         // Through AudioManager, the transaction behind getAudioVolumeGroups will succeed
79         final List<AudioVolumeGroup> audioVolumeGroup = mAudioManager.getAudioVolumeGroups();
80         assertNotNull(audioVolumeGroup);
81         assertTrue(audioVolumeGroup.size() > 0);
82 
83         final List<AudioProductStrategy> audioProductStrategies =
84                 mAudioManager.getAudioProductStrategies();
85         assertTrue(audioProductStrategies.size() > 0);
86 
87         for (final AudioVolumeGroup avg : audioVolumeGroup) {
88             int avgId = avg.getId();
89             assertNotEquals(avgId, AudioVolumeGroup.DEFAULT_VOLUME_GROUP);
90 
91             List<AudioAttributes> avgAttributes = avg.getAudioAttributes();
92             assertNotNull(avgAttributes);
93 
94             final int[] avgStreamTypes = avg.getLegacyStreamTypes();
95             assertNotNull(avgStreamTypes);
96 
97             // for each volume group attributes, find the matching product strategy and ensure
98             // it is linked the considered volume group
99             for (final AudioAttributes aa : avgAttributes) {
100                 if (aa.equals(DEFAULT_ATTRIBUTES)) {
101                     // Some volume groups may not have valid attributes, used for internal
102                     // volume management like patch/rerouting
103                     // so bailing out strategy retrieval from attributes
104                     continue;
105                 }
106                 boolean isVolumeGroupAssociatedToStrategy = false;
107                 for (final AudioProductStrategy aps : audioProductStrategies) {
108                     int groupId = aps.getVolumeGroupIdForAudioAttributes(aa);
109                     if (groupId != AudioVolumeGroup.DEFAULT_VOLUME_GROUP) {
110                         // Note that Audio Product Strategies are priority ordered, and the
111                         // the first one matching the AudioAttributes will be used to identify
112                         // the volume group associated to the request.
113                         assertTrue(aps.supportsAudioAttributes(aa));
114                         assertEquals("Volume Group ID (" + avg.toString()
115                                 + "), and Volume group ID associated to Strategy ("
116                                 + aps.toString() + ") both supporting attributes "
117                                 + aa.toString() + " are mismatching",
118                                 avgId, groupId);
119                         isVolumeGroupAssociatedToStrategy = true;
120                         break;
121                     }
122                 }
123                 assertTrue("Volume Group (" + avg.toString()
124                         + ") has no associated strategy for attributes " + aa.toString(),
125                         isVolumeGroupAssociatedToStrategy);
126             }
127 
128             // for each volume group stream type, find the matching product strategy and ensure
129             // it is linked the considered volume group
130             for (final int avgStreamType : avgStreamTypes) {
131                 if (avgStreamType == AudioSystem.STREAM_DEFAULT) {
132                     // Some Volume Groups may not have corresponding stream types as they
133                     // intends to address volume setting per attributes to avoid adding new
134                     // stream type and going on deprecating the stream type even for volume
135                     // so bailing out strategy retrieval from stream type
136                     continue;
137                 }
138                 boolean isVolumeGroupAssociatedToStrategy = false;
139                 for (final AudioProductStrategy aps : audioProductStrategies) {
140                     int groupId = aps.getVolumeGroupIdForLegacyStreamType(avgStreamType);
141                     if (groupId != AudioVolumeGroup.DEFAULT_VOLUME_GROUP) {
142 
143                         assertEquals("Volume Group ID (" + avg.toString()
144                                 + "), and Volume group ID associated to Strategy ("
145                                 + aps.toString() + ") both supporting stream "
146                                 + AudioSystem.streamToString(avgStreamType) + "("
147                                 + avgStreamType + ") are mismatching",
148                                 avgId, groupId);
149 
150                         isVolumeGroupAssociatedToStrategy = true;
151                         break;
152                     }
153                 }
154                 assertTrue("Volume Group (" + avg.toString()
155                         + ") has no associated strategy for stream "
156                         + AudioSystem.streamToString(avgStreamType) + "(" + avgStreamType + ")",
157                         isVolumeGroupAssociatedToStrategy);
158             }
159         }
160     }
161 }
162