• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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.bluetooth;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 import static org.mockito.Matchers.any;
21 import static org.mockito.Matchers.eq;
22 import static org.mockito.Mockito.verify;
23 import static org.mockito.Mockito.when;
24 
25 import android.content.Context;
26 import android.os.Bundle;
27 import android.view.View;
28 import android.view.ViewGroup;
29 import android.widget.CheckBox;
30 
31 import com.android.settings.testutils.SettingsRobolectricTestRunner;
32 import com.android.settingslib.R;
33 import com.android.settingslib.bluetooth.A2dpProfile;
34 import com.android.settingslib.bluetooth.CachedBluetoothDevice;
35 import com.android.settingslib.bluetooth.CachedBluetoothDeviceManager;
36 import com.android.settingslib.bluetooth.LocalBluetoothAdapter;
37 import com.android.settingslib.bluetooth.LocalBluetoothManager;
38 import com.android.settingslib.bluetooth.LocalBluetoothProfile;
39 import com.android.settingslib.bluetooth.LocalBluetoothProfileManager;
40 
41 import org.junit.Before;
42 import org.junit.Test;
43 import org.junit.runner.RunWith;
44 import org.mockito.Mock;
45 import org.mockito.MockitoAnnotations;
46 import org.robolectric.RuntimeEnvironment;
47 import org.robolectric.util.FragmentTestUtil;
48 import org.robolectric.util.ReflectionHelpers;
49 
50 import java.util.ArrayList;
51 import java.util.List;
52 
53 @RunWith(SettingsRobolectricTestRunner.class)
54 public class DeviceProfilesSettingsTest {
55 
56     @Mock
57     private LocalBluetoothManager mManager;
58     @Mock
59     private LocalBluetoothAdapter mAdapter;
60     @Mock
61     private LocalBluetoothProfileManager mProfileManager;
62     @Mock
63     private CachedBluetoothDeviceManager mDeviceManager;
64     @Mock
65     private CachedBluetoothDevice mCachedDevice;
66     @Mock
67     private A2dpProfile mProfile;
68 
69     private DeviceProfilesSettings mFragment;
70 
71     @Before
setUp()72     public void setUp() {
73         MockitoAnnotations.initMocks(this);
74 
75         when(mProfile.getNameResource(any())).thenReturn(R.string.bluetooth_profile_a2dp);
76         List<LocalBluetoothProfile> profiles = new ArrayList<>();
77         profiles.add(mProfile);
78         when(mCachedDevice.getConnectableProfiles()).thenReturn(profiles);
79 
80         mFragment = new DeviceProfilesSettings();
81         mFragment.setArguments(new Bundle());
82 
83         ReflectionHelpers.setStaticField(LocalBluetoothManager.class, "sInstance", mManager);
84         when(mManager.getCachedDeviceManager()).thenReturn(mDeviceManager);
85         when(mManager.getBluetoothAdapter()).thenReturn(mAdapter);
86         when(mManager.getProfileManager()).thenReturn(mProfileManager);
87         when(mProfileManager.getMapProfile()).thenReturn(null);
88         when(mDeviceManager.findDevice(any())).thenReturn(mCachedDevice);
89     }
90 
91     @Test
deviceHasHighQualityAudio()92     public void deviceHasHighQualityAudio() {
93         when(mProfile.supportsHighQualityAudio(any())).thenReturn(true);
94         when(mProfile.isHighQualityAudioEnabled(any())).thenReturn(true);
95         when(mProfile.isPreferred(any())).thenReturn(true);
96         FragmentTestUtil.startFragment(mFragment);
97 
98         ViewGroup profilesGroup = mFragment.getDialog().findViewById(R.id.profiles_section);
99         CheckBox box =
100             profilesGroup.findViewWithTag(DeviceProfilesSettings.HIGH_QUALITY_AUDIO_PREF_TAG);
101         assertThat(box).isNotNull();
102         assertThat(box.getVisibility()).isEqualTo(View.VISIBLE);
103         assertThat(box.isEnabled()).isTrue();
104         assertThat(box.isChecked()).isTrue();
105 
106         box.performClick();
107         verify(mProfile).setHighQualityAudioEnabled(any(), eq(false));
108         box.performClick();
109         verify(mProfile).setHighQualityAudioEnabled(any(), eq(true));
110     }
111 
112     @Test
busyDeviceDisablesControl()113     public void busyDeviceDisablesControl() {
114         when(mProfile.supportsHighQualityAudio(any())).thenReturn(true);
115         when(mProfile.isHighQualityAudioEnabled(any())).thenReturn(true);
116         when(mProfile.isPreferred(any())).thenReturn(true);
117         when(mCachedDevice.isBusy()).thenReturn(true);
118         FragmentTestUtil.startFragment(mFragment);
119 
120         // Make sure that the high quality audio option is present but disabled when the device
121         // is busy.
122         ViewGroup profilesGroup = mFragment.getDialog().findViewById(R.id.profiles_section);
123         CheckBox box =
124             profilesGroup.findViewWithTag(DeviceProfilesSettings.HIGH_QUALITY_AUDIO_PREF_TAG);
125         assertThat(box).isNotNull();
126         assertThat(box.getVisibility()).isEqualTo(View.VISIBLE);
127         assertThat(box.isEnabled()).isFalse();
128     }
129 
130     @Test
mediaAudioGetsDisabledAndReEnabled()131     public void mediaAudioGetsDisabledAndReEnabled() {
132         when(mProfile.supportsHighQualityAudio(any())).thenReturn(true);
133         when(mProfile.isHighQualityAudioEnabled(any())).thenReturn(true);
134         when(mProfile.isPreferred(any())).thenReturn(true);
135         FragmentTestUtil.startFragment(mFragment);
136 
137         ViewGroup profilesGroup = mFragment.getDialog().findViewById(R.id.profiles_section);
138         CheckBox audioBox = profilesGroup.findViewWithTag(mProfile.toString());
139         CheckBox highQualityAudioBox =
140             profilesGroup.findViewWithTag(DeviceProfilesSettings.HIGH_QUALITY_AUDIO_PREF_TAG);
141         assertThat(audioBox).isNotNull();
142         assertThat(audioBox.isChecked()).isTrue();
143         assertThat(highQualityAudioBox).isNotNull();
144         assertThat(highQualityAudioBox.isChecked()).isTrue();
145 
146         // Disabling media audio should cause the high quality audio box to disappear.
147         when(mProfile.isPreferred(any())).thenReturn(false);
148         mFragment.onDeviceAttributesChanged();
149         audioBox = profilesGroup.findViewWithTag(mProfile.toString());
150         highQualityAudioBox =
151             profilesGroup.findViewWithTag(DeviceProfilesSettings.HIGH_QUALITY_AUDIO_PREF_TAG);
152         assertThat(audioBox).isNotNull();
153         assertThat(audioBox.isChecked()).isFalse();
154         assertThat(highQualityAudioBox).isNotNull();
155         assertThat(highQualityAudioBox.getVisibility()).isEqualTo(View.GONE);
156 
157         // And re-enabling media audio should make it reappear.
158         when(mProfile.isPreferred(any())).thenReturn(true);
159         mFragment.onDeviceAttributesChanged();
160         audioBox = profilesGroup.findViewWithTag(mProfile.toString());
161         highQualityAudioBox =
162             profilesGroup.findViewWithTag(DeviceProfilesSettings.HIGH_QUALITY_AUDIO_PREF_TAG);
163         assertThat(audioBox).isNotNull();
164         assertThat(audioBox.isChecked()).isTrue();
165         assertThat(highQualityAudioBox).isNotNull();
166         assertThat(highQualityAudioBox.isChecked()).isTrue();
167     }
168 
169     @Test
mediaAudioStartsDisabled()170     public void mediaAudioStartsDisabled() {
171         when(mProfile.supportsHighQualityAudio(any())).thenReturn(true);
172         when(mProfile.isHighQualityAudioEnabled(any())).thenReturn(true);
173         when(mProfile.isPreferred(any())).thenReturn(false);
174 
175         FragmentTestUtil.startFragment(mFragment);
176         ViewGroup profilesGroup = mFragment.getDialog().findViewById(R.id.profiles_section);
177         CheckBox audioBox = profilesGroup.findViewWithTag(mProfile.toString());
178         CheckBox highQualityAudioBox =
179             profilesGroup.findViewWithTag(DeviceProfilesSettings.HIGH_QUALITY_AUDIO_PREF_TAG);
180 
181         assertThat(audioBox).isNotNull();
182         assertThat(audioBox.isChecked()).isFalse();
183         assertThat(highQualityAudioBox).isNotNull();
184         assertThat(highQualityAudioBox.getVisibility()).isEqualTo(View.GONE);
185     }
186 
187     @Test
deviceDoesntHaveHighQualityAudio()188     public void deviceDoesntHaveHighQualityAudio() {
189         when(mProfile.supportsHighQualityAudio(any())).thenReturn(false);
190         when(mProfile.isPreferred(any())).thenReturn(true);
191         FragmentTestUtil.startFragment(mFragment);
192 
193         // A device that doesn't support high quality audio shouldn't have the checkbox for
194         // high quality audio support.
195         ViewGroup profilesGroup = mFragment.getDialog().findViewById(R.id.profiles_section);
196         CheckBox box =
197             profilesGroup.findViewWithTag(DeviceProfilesSettings.HIGH_QUALITY_AUDIO_PREF_TAG);
198         assertThat(box).isNull();
199     }
200 }
201