1 /*
2  * Copyright (C) 2021 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.settings.notification;
18 
19 import static com.android.settings.core.BasePreferenceController.AVAILABLE;
20 import static com.android.settings.core.BasePreferenceController.UNSUPPORTED_ON_DEVICE;
21 
22 import static com.google.common.truth.Truth.assertThat;
23 
24 import static org.mockito.Mockito.spy;
25 import static org.mockito.Mockito.verify;
26 import static org.mockito.Mockito.when;
27 
28 import android.content.Context;
29 import android.media.AudioManager;
30 import android.media.Spatializer;
31 
32 import org.junit.Before;
33 import org.junit.Ignore;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.mockito.Answers;
37 import org.mockito.Mock;
38 import org.mockito.MockitoAnnotations;
39 import org.robolectric.RobolectricTestRunner;
40 import org.robolectric.RuntimeEnvironment;
41 
42 @Ignore("b/200896161")
43 @RunWith(RobolectricTestRunner.class)
44 public class SpatialAudioPreferenceControllerTest {
45 
46     @Mock(answer = Answers.RETURNS_DEEP_STUBS)
47     private Context mContext;
48     @Mock
49     private AudioManager mAudioManager;
50     @Mock
51     private Spatializer mSpatializer;
52 
53     private SpatialAudioPreferenceController mController;
54 
55     @Before
setUp()56     public void setUp() {
57         MockitoAnnotations.initMocks(this);
58         mContext = spy(RuntimeEnvironment.application);
59         when((Object) mContext.getSystemService(AudioManager.class)).thenReturn(mAudioManager);
60         when(mAudioManager.getSpatializer()).thenReturn(mSpatializer);
61         mController = new SpatialAudioPreferenceController(mContext);
62     }
63 
64     @Test
getAvailabilityStatus_levelNone_shouldReturnUnsupported()65     public void getAvailabilityStatus_levelNone_shouldReturnUnsupported() {
66         when(mSpatializer.getImmersiveAudioLevel()).thenReturn(
67                 Spatializer.SPATIALIZER_IMMERSIVE_LEVEL_NONE);
68         assertThat(mController.getAvailabilityStatus()).isEqualTo(UNSUPPORTED_ON_DEVICE);
69     }
70 
71     @Test
getAvailabilityStatus_levelMultiChannel_shouldReturnAvailable()72     public void getAvailabilityStatus_levelMultiChannel_shouldReturnAvailable() {
73         when(mSpatializer.getImmersiveAudioLevel()).thenReturn(
74                 Spatializer.SPATIALIZER_IMMERSIVE_LEVEL_MULTICHANNEL);
75         assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE);
76     }
77 
78     @Test
setChecked_withTrue_shouldEnableSpatializer()79     public void setChecked_withTrue_shouldEnableSpatializer() {
80         mController.setChecked(true);
81 
82         verify(mSpatializer).setEnabled(true);
83     }
84 
85     @Test
setChecked_withFalse_shouldDisableSpatializer()86     public void setChecked_withFalse_shouldDisableSpatializer() {
87         mController.setChecked(false);
88 
89         verify(mSpatializer).setEnabled(false);
90     }
91 }
92