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.hdmi;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import static org.mockito.Mockito.clearInvocations;
22 
23 import android.hardware.hdmi.DeviceFeatures;
24 import android.hardware.hdmi.HdmiControlManager;
25 import android.hardware.hdmi.HdmiDeviceInfo;
26 import android.media.AudioManager;
27 import android.platform.test.annotations.Presubmit;
28 
29 import androidx.test.filters.SmallTest;
30 
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 import org.junit.runners.JUnit4;
34 
35 import java.util.Arrays;
36 import java.util.Collections;
37 
38 /**
39  * Tests for absolute volume behavior where the local device is a Playback device and the
40  * System Audio device is a TV.
41  */
42 @SmallTest
43 @Presubmit
44 @RunWith(JUnit4.class)
45 public class PlaybackDeviceToTvAvbTest extends BasePlaybackDeviceAvbTest {
46 
47     @Override
getSystemAudioDeviceLogicalAddress()48     protected int getSystemAudioDeviceLogicalAddress() {
49         return Constants.ADDR_TV;
50     }
51 
52     @Override
getSystemAudioDeviceType()53     protected int getSystemAudioDeviceType() {
54         return HdmiDeviceInfo.DEVICE_TV;
55     }
56 
57     /**
58      * AVB is disabled when an Audio System with unknown support for <Set Audio Volume Level>
59      * becomes the System Audio device. It is enabled once the Audio System reports that it
60      * supports <Set Audio Volume Level> and sends <Report Audio Status>.
61      */
62     @Test
switchToAudioSystem_absoluteVolumeControlDisabledUntilAllConditionsMet()63     public void switchToAudioSystem_absoluteVolumeControlDisabledUntilAllConditionsMet() {
64         enableAbsoluteVolumeBehavior();
65 
66         // Audio System enables System Audio Mode. AVB should be disabled.
67         receiveSetSystemAudioMode(true);
68         assertThat(mAudioManager.getDeviceVolumeBehavior(getAudioOutputDevice())).isEqualTo(
69                 AudioManager.DEVICE_VOLUME_BEHAVIOR_FULL);
70 
71         clearInvocations(mAudioManager, mAudioDeviceVolumeManager);
72 
73         // Audio System reports support for <Set Audio Volume Level>
74         mNativeWrapper.onCecMessage(ReportFeaturesMessage.build(
75                 Constants.ADDR_AUDIO_SYSTEM, HdmiControlManager.HDMI_CEC_VERSION_2_0,
76                 Arrays.asList(HdmiDeviceInfo.DEVICE_AUDIO_SYSTEM), Constants.RC_PROFILE_SOURCE,
77                 Collections.emptyList(),
78                 DeviceFeatures.NO_FEATURES_SUPPORTED.toBuilder()
79                         .setSetAudioVolumeLevelSupport(DeviceFeatures.FEATURE_SUPPORTED)
80                         .build()));
81         mTestLooper.dispatchAll();
82 
83         // Audio system reports its initial audio status
84         mNativeWrapper.onCecMessage(HdmiCecMessageBuilder.buildReportAudioStatus(
85                 Constants.ADDR_AUDIO_SYSTEM,
86                 getLogicalAddress(),
87                 30,
88                 false));
89         mTestLooper.dispatchAll();
90 
91         assertThat(mAudioManager.getDeviceVolumeBehavior(getAudioOutputDevice())).isEqualTo(
92                 AudioManager.DEVICE_VOLUME_BEHAVIOR_ABSOLUTE);
93     }
94 }
95