OSDN Git Service

Merge "Fixes crash in security settings." into pi-dev
[android-x86/packages-apps-Settings.git] / tests / robotests / src / com / android / settings / sound / AudioOutputSwitchPreferenceControllerTest.java
1 /*
2  * Copyright (C) 2018 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.sound;
18
19
20 import static com.android.settings.core.BasePreferenceController.AVAILABLE;
21 import static com.android.settings.core.BasePreferenceController.DISABLED_UNSUPPORTED;
22
23 import static com.google.common.truth.Truth.assertThat;
24
25 import static org.mockito.ArgumentMatchers.any;
26 import static org.mockito.Mockito.mock;
27 import static org.mockito.Mockito.spy;
28 import static org.mockito.Mockito.verify;
29 import static org.mockito.Mockito.when;
30
31 import android.bluetooth.BluetoothAdapter;
32 import android.bluetooth.BluetoothDevice;
33 import android.bluetooth.BluetoothManager;
34 import android.content.BroadcastReceiver;
35 import android.content.Context;
36 import android.content.IntentFilter;
37 import android.support.v7.preference.ListPreference;
38 import android.support.v7.preference.PreferenceManager;
39 import android.support.v7.preference.PreferenceScreen;
40 import android.util.FeatureFlagUtils;
41
42 import com.android.settings.R;
43 import com.android.settings.core.FeatureFlags;
44 import com.android.settings.testutils.SettingsRobolectricTestRunner;
45 import com.android.settings.testutils.shadow.ShadowAudioManager;
46 import com.android.settings.testutils.shadow.ShadowBluetoothUtils;
47 import com.android.settings.testutils.shadow.ShadowMediaRouter;
48 import com.android.settingslib.bluetooth.A2dpProfile;
49 import com.android.settingslib.bluetooth.BluetoothCallback;
50 import com.android.settingslib.bluetooth.BluetoothEventManager;
51 import com.android.settingslib.bluetooth.CachedBluetoothDevice;
52 import com.android.settingslib.bluetooth.LocalBluetoothManager;
53 import com.android.settingslib.bluetooth.LocalBluetoothProfileManager;
54
55 import org.junit.After;
56 import org.junit.Before;
57 import org.junit.Test;
58 import org.junit.runner.RunWith;
59 import org.mockito.Mock;
60 import org.mockito.MockitoAnnotations;
61 import org.robolectric.RuntimeEnvironment;
62 import org.robolectric.Shadows;
63 import org.robolectric.annotation.Config;
64 import org.robolectric.shadows.ShadowBluetoothDevice;
65
66 import java.util.ArrayList;
67 import java.util.List;
68
69 @RunWith(SettingsRobolectricTestRunner.class)
70 @Config(shadows = {
71         ShadowAudioManager.class,
72         ShadowMediaRouter.class,
73         ShadowBluetoothUtils.class,
74         ShadowBluetoothDevice.class}
75 )
76 public class AudioOutputSwitchPreferenceControllerTest {
77     private static final String TEST_KEY = "Test_Key";
78     private static final String TEST_DEVICE_NAME_1 = "Test_A2DP_BT_Device_NAME_1";
79     private static final String TEST_DEVICE_NAME_2 = "Test_A2DP_BT_Device_NAME_2";
80     private static final String TEST_DEVICE_ADDRESS_1 = "00:07:80:78:A4:69";
81     private static final String TEST_DEVICE_ADDRESS_2 = "00:00:00:00:00:00";
82
83     @Mock
84     private LocalBluetoothManager mLocalManager;
85     @Mock
86     private BluetoothEventManager mBluetoothEventManager;
87     @Mock
88     private CachedBluetoothDevice mCachedBluetoothDevice;
89     @Mock
90     private LocalBluetoothProfileManager mLocalBluetoothProfileManager;
91     @Mock
92     private A2dpProfile mA2dpProfile;
93
94     private Context mContext;
95     private PreferenceScreen mScreen;
96     private ListPreference mPreference;
97     private ShadowAudioManager mShadowAudioManager;
98     private ShadowMediaRouter mShadowMediaRouter;
99     private BluetoothManager mBluetoothManager;
100     private BluetoothAdapter mBluetoothAdapter;
101     private BluetoothDevice mBluetoothDevice;
102     private ShadowBluetoothDevice mShadowBluetoothDevice;
103     private LocalBluetoothManager mLocalBluetoothManager;
104     private AudioSwitchPreferenceController mController;
105     private List<BluetoothDevice> mConnectedDevices;
106
107     @Before
108     public void setUp() {
109         MockitoAnnotations.initMocks(this);
110         mContext = spy(RuntimeEnvironment.application);
111
112         mShadowAudioManager = ShadowAudioManager.getShadow();
113         mShadowMediaRouter = ShadowMediaRouter.getShadow();
114
115         ShadowBluetoothUtils.sLocalBluetoothManager = mLocalManager;
116         mLocalBluetoothManager = ShadowBluetoothUtils.getLocalBtManager(mContext);
117
118         when(mLocalBluetoothManager.getEventManager()).thenReturn(mBluetoothEventManager);
119         when(mLocalBluetoothManager.getProfileManager()).thenReturn(mLocalBluetoothProfileManager);
120         when(mLocalBluetoothProfileManager.getA2dpProfile()).thenReturn(mA2dpProfile);
121
122         mBluetoothManager = new BluetoothManager(mContext);
123         mBluetoothAdapter = mBluetoothManager.getAdapter();
124
125         mBluetoothDevice = mBluetoothAdapter.getRemoteDevice(TEST_DEVICE_ADDRESS_1);
126         mShadowBluetoothDevice = Shadows.shadowOf(mBluetoothDevice);
127         mShadowBluetoothDevice.setName(TEST_DEVICE_NAME_1);
128         when(mCachedBluetoothDevice.getDevice()).thenReturn(mBluetoothDevice);
129
130         mController = new AudioSwitchPreferenceControllerTestable(mContext, TEST_KEY);
131         mScreen = spy(new PreferenceScreen(mContext, null));
132         mPreference = new ListPreference(mContext);
133         mConnectedDevices = new ArrayList<>(1);
134         mConnectedDevices.add(mBluetoothDevice);
135
136         when(mScreen.getPreferenceManager()).thenReturn(mock(PreferenceManager.class));
137         when(mScreen.getContext()).thenReturn(mContext);
138         when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
139         mScreen.addPreference(mPreference);
140         mController.displayPreference(mScreen);
141     }
142
143     @After
144     public void tearDown() {
145         mShadowAudioManager.reset();
146         mShadowMediaRouter.reset();
147         ShadowBluetoothUtils.reset();
148     }
149
150     @Test
151     public void getAvailabilityStatus_byDefault_isAvailable() {
152         assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE);
153     }
154
155     @Test
156     public void getAvailabilityStatus_whenNotVisible_isDisable() {
157         FeatureFlagUtils.setEnabled(mContext, FeatureFlags.AUDIO_SWITCHER_SETTINGS, false);
158         assertThat(mController.getAvailabilityStatus()).isEqualTo(DISABLED_UNSUPPORTED);
159     }
160
161     @Test
162     public void onStart_shouldRegisterCallbackAndRegisterReceiver() {
163         mController.onStart();
164
165         verify(mLocalBluetoothManager.getEventManager()).registerCallback(
166                 any(BluetoothCallback.class));
167         verify(mContext).registerReceiver(any(BroadcastReceiver.class), any(IntentFilter.class));
168     }
169
170     @Test
171     public void onStop_shouldUnregisterCallbackAndUnregisterReceiver() {
172         mController.onStart();
173         mController.onStop();
174
175         verify(mLocalBluetoothManager.getEventManager()).unregisterCallback(
176                 any(BluetoothCallback.class));
177         verify(mContext).unregisterReceiver(any(BroadcastReceiver.class));
178     }
179
180     @Test
181     public void onPreferenceChange_toThisDevice_shouldSetDefaultSummary() {
182         mController.mConnectedDevices = mConnectedDevices;
183
184         mController.onPreferenceChange(mPreference,
185                 mContext.getText(R.string.media_output_default_summary));
186
187         assertThat(mPreference.getSummary()).isEqualTo(
188                 mContext.getText(R.string.media_output_default_summary));
189     }
190
191     /**
192      * One Bluetooth devices are available, and select the device.
193      * Preference summary should be device name.
194      */
195     @Test
196     public void onPreferenceChange_toBtDevice_shouldSetBtDeviceName() {
197         mController.mConnectedDevices = mConnectedDevices;
198
199         mController.onPreferenceChange(mPreference, TEST_DEVICE_ADDRESS_1);
200
201         assertThat(mPreference.getSummary()).isEqualTo(mBluetoothDevice.getName());
202     }
203
204     /**
205      * More than one Bluetooth devices are available, and select second device.
206      * Preference summary should be second device name.
207      */
208     @Test
209     public void onPreferenceChange_toBtDevices_shouldSetSecondBtDeviceName() {
210         ShadowBluetoothDevice shadowBluetoothDevice;
211         BluetoothDevice secondBluetoothDevice;
212         secondBluetoothDevice = mBluetoothAdapter.getRemoteDevice(TEST_DEVICE_ADDRESS_2);
213         shadowBluetoothDevice = Shadows.shadowOf(secondBluetoothDevice);
214         shadowBluetoothDevice.setName(TEST_DEVICE_NAME_2);
215         List<BluetoothDevice> connectedDevices = new ArrayList<>(2);
216         connectedDevices.add(mBluetoothDevice);
217         connectedDevices.add(secondBluetoothDevice);
218         mController.mConnectedDevices = connectedDevices;
219
220         mController.onPreferenceChange(mPreference, TEST_DEVICE_ADDRESS_2);
221
222         assertThat(mPreference.getSummary()).isEqualTo(secondBluetoothDevice.getName());
223     }
224
225     /**
226      * mConnectedDevices is Null.
227      * onPreferenceChange should return false.
228      */
229     @Test
230     public void onPreferenceChange_connectedDeviceIsNull_shouldReturnFalse() {
231         mController.mConnectedDevices = null;
232
233         assertThat(mController.onPreferenceChange(mPreference, TEST_DEVICE_ADDRESS_1)).isFalse();
234     }
235
236     private class AudioSwitchPreferenceControllerTestable extends
237             AudioSwitchPreferenceController {
238         AudioSwitchPreferenceControllerTestable(Context context, String key) {
239             super(context, key);
240         }
241
242         @Override
243         public void setActiveBluetoothDevice(BluetoothDevice device) {
244         }
245
246         @Override
247         public String getPreferenceKey() {
248             return TEST_KEY;
249         }
250     }
251 }