1 /*
2  * Copyright (C) 2023 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.car.test.util;
18 
19 import static android.car.test.mocks.JavaMockitoHelper.silentAwait;
20 
21 import android.car.media.CarAudioManager;
22 import android.car.media.CarVolumeGroupEvent;
23 import android.car.media.CarVolumeGroupEventCallback;
24 import android.util.Log;
25 
26 import java.util.List;
27 import java.util.concurrent.CountDownLatch;
28 
29 /**
30  * Test utility class to be used by CarAudioManager tests.
31  */
32 public final class CarAudioManagerTestUtils {
33 
34     private static final String TAG = CarAudioManagerTestUtils.class.getSimpleName();
35 
36     private static final long WAIT_TIMEOUT_MS = 5_000;
37 
38     public static final class SyncCarVolumeCallback extends CarAudioManager.CarVolumeCallback {
39 
40         private final CountDownLatch mGroupVolumeChangeLatch = new CountDownLatch(1);
41         private final CountDownLatch mGroupMuteChangeLatch = new CountDownLatch(1);
42         private final CountDownLatch mMasterMuteChangeLatch = new CountDownLatch(1);
43 
44         public int zoneId;
45         public int groupId;
46 
receivedGroupVolumeChanged()47         public boolean receivedGroupVolumeChanged() {
48             return silentAwait(mGroupVolumeChangeLatch, WAIT_TIMEOUT_MS);
49         }
50 
receivedGroupMuteChanged()51         public boolean receivedGroupMuteChanged() {
52             return silentAwait(mGroupMuteChangeLatch, WAIT_TIMEOUT_MS);
53         }
54 
receivedMasterMuteChanged()55         public boolean receivedMasterMuteChanged() {
56             return silentAwait(mMasterMuteChangeLatch, WAIT_TIMEOUT_MS);
57         }
58 
59         @Override
onGroupVolumeChanged(int zoneId, int groupId, int flags)60         public void onGroupVolumeChanged(int zoneId, int groupId, int flags) {
61             Log.v(TAG, "onGroupVolumeChanged");
62             mGroupVolumeChangeLatch.countDown();
63         }
64 
65         @Override
onMasterMuteChanged(int zoneId, int flags)66         public void onMasterMuteChanged(int zoneId, int flags) {
67             Log.v(TAG, "onMasterMuteChanged");
68             mMasterMuteChangeLatch.countDown();
69         }
70 
71         @Override
onGroupMuteChanged(int zoneId, int groupId, int flags)72         public void onGroupMuteChanged(int zoneId, int groupId, int flags) {
73             Log.v(TAG, "onGroupMuteChanged");
74             this.zoneId = zoneId;
75             this.groupId = groupId;
76             mGroupMuteChangeLatch.countDown();
77         }
78     }
79 
80     public static final class TestCarVolumeGroupEventCallback implements
81             CarVolumeGroupEventCallback {
82 
83         private CountDownLatch mVolumeGroupEventLatch = new CountDownLatch(1);
84         List<CarVolumeGroupEvent> mEvents;
85 
receivedVolumeGroupEvents()86         public boolean receivedVolumeGroupEvents() throws InterruptedException {
87             return silentAwait(mVolumeGroupEventLatch, WAIT_TIMEOUT_MS);
88         }
89 
90         @Override
onVolumeGroupEvent(List<CarVolumeGroupEvent> volumeGroupEvents)91         public void onVolumeGroupEvent(List<CarVolumeGroupEvent> volumeGroupEvents) {
92             mEvents = volumeGroupEvents;
93             Log.v(TAG, "onVolumeGroupEvent events " + volumeGroupEvents);
94             mVolumeGroupEventLatch.countDown();
95         }
96     }
97 
CarAudioManagerTestUtils()98     private CarAudioManagerTestUtils() {
99     }
100 }
101