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 org.junit.Assert.assertNotEquals;
20 
21 import android.media.AudioAttributes;
22 import android.media.AudioSystem;
23 import android.media.audiopolicy.AudioProductStrategy;
24 import android.media.audiopolicy.AudioVolumeGroup;
25 
26 import java.util.List;
27 
28 public class AudioVolumeGroupTest extends AudioVolumesTestBase {
29     private static final String TAG = "AudioVolumeGroupTest";
30 
31     //-----------------------------------------------------------------
32     // Test getAudioVolumeGroups and validate groud id
33     //-----------------------------------------------------------------
testGetVolumeGroupsFromNonServiceCaller()34     public void testGetVolumeGroupsFromNonServiceCaller() throws Exception {
35         // The transaction behind getAudioVolumeGroups will fail. Check is done at binder level
36         // with policy service. Error is not reported, the list is just empty.
37         // Request must come from service components
38         List<AudioVolumeGroup> audioVolumeGroup = AudioVolumeGroup.getAudioVolumeGroups();
39 
40         assertNotNull(audioVolumeGroup);
41         assertEquals(audioVolumeGroup.size(), 0);
42     }
43 
44     //-----------------------------------------------------------------
45     // Test getAudioVolumeGroups and validate groud id
46     //-----------------------------------------------------------------
testGetVolumeGroups()47     public void testGetVolumeGroups() throws Exception {
48         // Through AudioManager, the transaction behind getAudioVolumeGroups will succeed
49         final List<AudioVolumeGroup> audioVolumeGroup = mAudioManager.getAudioVolumeGroups();
50         assertNotNull(audioVolumeGroup);
51         assertTrue(audioVolumeGroup.size() > 0);
52 
53         final List<AudioProductStrategy> audioProductStrategies =
54                 mAudioManager.getAudioProductStrategies();
55         assertTrue(audioProductStrategies.size() > 0);
56 
57         for (final AudioVolumeGroup avg : audioVolumeGroup) {
58             int avgId = avg.getId();
59             assertNotEquals(avgId, AudioVolumeGroup.DEFAULT_VOLUME_GROUP);
60 
61             List<AudioAttributes> avgAttributes = avg.getAudioAttributes();
62             assertNotNull(avgAttributes);
63 
64             final int[] avgStreamTypes = avg.getLegacyStreamTypes();
65             assertNotNull(avgStreamTypes);
66 
67             // for each volume group attributes, find the matching product strategy and ensure
68             // it is linked the considered volume group
69             for (final AudioAttributes aa : avgAttributes) {
70                 if (aa.equals(sDefaultAttributes)) {
71                     // Some volume groups may not have valid attributes, used for internal
72                     // volume management like patch/rerouting
73                     // so bailing out strategy retrieval from attributes
74                     continue;
75                 }
76                 boolean isVolumeGroupAssociatedToStrategy = false;
77                 for (final AudioProductStrategy aps : audioProductStrategies) {
78                     int groupId = aps.getVolumeGroupIdForAudioAttributes(aa);
79                     if (groupId != AudioVolumeGroup.DEFAULT_VOLUME_GROUP) {
80                         // Note that Audio Product Strategies are priority ordered, and the
81                         // the first one matching the AudioAttributes will be used to identify
82                         // the volume group associated to the request.
83                         assertTrue(aps.supportsAudioAttributes(aa));
84                         assertEquals("Volume Group ID (" + avg.toString()
85                                 + "), and Volume group ID associated to Strategy ("
86                                 + aps.toString() + ") both supporting attributes "
87                                 + aa.toString() + " are mismatching",
88                                 avgId, groupId);
89                         isVolumeGroupAssociatedToStrategy = true;
90                         break;
91                     }
92                 }
93                 assertTrue("Volume Group (" + avg.toString()
94                         + ") has no associated strategy for attributes " + aa.toString(),
95                         isVolumeGroupAssociatedToStrategy);
96             }
97 
98             // for each volume group stream type, find the matching product strategy and ensure
99             // it is linked the considered volume group
100             for (final int avgStreamType : avgStreamTypes) {
101                 if (avgStreamType == AudioSystem.STREAM_DEFAULT) {
102                     // Some Volume Groups may not have corresponding stream types as they
103                     // intends to address volume setting per attributes to avoid adding new
104                     // stream type and going on deprecating the stream type even for volume
105                     // so bailing out strategy retrieval from stream type
106                     continue;
107                 }
108                 boolean isVolumeGroupAssociatedToStrategy = false;
109                 for (final AudioProductStrategy aps : audioProductStrategies) {
110                     int groupId = aps.getVolumeGroupIdForLegacyStreamType(avgStreamType);
111                     if (groupId != AudioVolumeGroup.DEFAULT_VOLUME_GROUP) {
112 
113                         assertEquals("Volume Group ID (" + avg.toString()
114                                 + "), and Volume group ID associated to Strategy ("
115                                 + aps.toString() + ") both supporting stream "
116                                 + AudioSystem.streamToString(avgStreamType) + "("
117                                 + avgStreamType + ") are mismatching",
118                                 avgId, groupId);
119 
120                         isVolumeGroupAssociatedToStrategy = true;
121                         break;
122                     }
123                 }
124                 assertTrue("Volume Group (" + avg.toString()
125                         + ") has no associated strategy for stream "
126                         + AudioSystem.streamToString(avgStreamType) + "(" + avgStreamType + ")",
127                         isVolumeGroupAssociatedToStrategy);
128             }
129         }
130     }
131 }
132