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.audio; 18 19 import static org.mockito.Mockito.atLeast; 20 import static org.mockito.Mockito.mock; 21 import static org.mockito.Mockito.spy; 22 import static org.mockito.Mockito.verify; 23 24 import android.content.Context; 25 import android.media.AudioDeviceAttributes; 26 import android.media.AudioDeviceInfo; 27 import android.media.AudioManager; 28 import android.media.AudioSystem; 29 import android.media.VolumeInfo; 30 import android.os.test.TestLooper; 31 32 import androidx.test.InstrumentationRegistry; 33 34 import org.junit.Before; 35 import org.junit.Test; 36 37 public class AudioDeviceVolumeManagerTest { 38 private static final String TAG = "AudioDeviceVolumeManagerTest"; 39 40 private static final AudioDeviceAttributes DEVICE_SPEAKER_OUT = new AudioDeviceAttributes( 41 AudioDeviceAttributes.ROLE_OUTPUT, AudioDeviceInfo.TYPE_BUILTIN_SPEAKER, ""); 42 43 private Context mContext; 44 private String mPackageName; 45 private AudioSystemAdapter mSpyAudioSystem; 46 private SystemServerAdapter mSystemServer; 47 private SettingsAdapter mSettingsAdapter; 48 private TestLooper mTestLooper; 49 private AudioPolicyFacade mAudioPolicyMock = mock(AudioPolicyFacade.class); 50 51 private AudioService mAudioService; 52 53 54 @Before setUp()55 public void setUp() throws Exception { 56 mTestLooper = new TestLooper(); 57 mContext = InstrumentationRegistry.getTargetContext(); 58 mPackageName = mContext.getOpPackageName(); 59 mSpyAudioSystem = spy(new NoOpAudioSystemAdapter()); 60 61 mSystemServer = new NoOpSystemServerAdapter(); 62 mSettingsAdapter = new NoOpSettingsAdapter(); 63 mAudioService = new AudioService(mContext, mSpyAudioSystem, mSystemServer, 64 mSettingsAdapter, mAudioPolicyMock, mTestLooper.getLooper()) { 65 @Override 66 public int getDeviceForStream(int stream) { 67 return AudioSystem.DEVICE_OUT_SPEAKER; 68 } 69 }; 70 71 mTestLooper.dispatchAll(); 72 } 73 74 @Test testSetDeviceVolume()75 public void testSetDeviceVolume() { 76 AudioManager am = mContext.getSystemService(AudioManager.class); 77 final int minIndex = am.getStreamMinVolume(AudioManager.STREAM_MUSIC); 78 final int maxIndex = am.getStreamMaxVolume(AudioManager.STREAM_MUSIC); 79 final int midIndex = (minIndex + maxIndex) / 2; 80 final VolumeInfo volMedia = new VolumeInfo.Builder(AudioManager.STREAM_MUSIC) 81 .setMinVolumeIndex(minIndex) 82 .setMaxVolumeIndex(maxIndex) 83 .build(); 84 final VolumeInfo volMin = new VolumeInfo.Builder(volMedia).setVolumeIndex(minIndex).build(); 85 final VolumeInfo volMid = new VolumeInfo.Builder(volMedia).setVolumeIndex(midIndex).build(); 86 final AudioDeviceAttributes usbDevice = new AudioDeviceAttributes( 87 /*native type*/ AudioSystem.DEVICE_OUT_USB_DEVICE, /*address*/ "bla"); 88 89 mAudioService.setDeviceVolume(volMin, usbDevice, mPackageName); 90 mTestLooper.dispatchAll(); 91 verify(mSpyAudioSystem, atLeast(1)).setStreamVolumeIndexAS( 92 AudioManager.STREAM_MUSIC, minIndex, AudioSystem.DEVICE_OUT_USB_DEVICE); 93 94 mAudioService.setDeviceVolume(volMid, usbDevice, mPackageName); 95 mTestLooper.dispatchAll(); 96 verify(mSpyAudioSystem, atLeast(1)).setStreamVolumeIndexAS( 97 AudioManager.STREAM_MUSIC, midIndex, AudioSystem.DEVICE_OUT_USB_DEVICE); 98 } 99 } 100