• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 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 package com.android.settings.connecteddevice;
17 
18 import static com.android.settings.core.BasePreferenceController.AVAILABLE;
19 import static com.android.settings.core.BasePreferenceController.UNSUPPORTED_ON_DEVICE;
20 
21 import static com.google.common.truth.Truth.assertThat;
22 
23 import static org.mockito.Mockito.doReturn;
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.content.pm.PackageManager;
30 
31 import androidx.lifecycle.LifecycleOwner;
32 import androidx.preference.PreferenceManager;
33 
34 import com.android.settings.bluetooth.BluetoothDeviceUpdater;
35 import com.android.settings.connecteddevice.dock.DockUpdater;
36 import com.android.settings.dashboard.DashboardFragment;
37 import com.android.settingslib.core.lifecycle.Lifecycle;
38 
39 import org.junit.Before;
40 import org.junit.Test;
41 import org.junit.runner.RunWith;
42 import org.mockito.Answers;
43 import org.mockito.Mock;
44 import org.mockito.MockitoAnnotations;
45 import org.robolectric.RobolectricTestRunner;
46 import org.robolectric.RuntimeEnvironment;
47 
48 @RunWith(RobolectricTestRunner.class)
49 public class SavedDeviceGroupControllerTest {
50 
51     @Mock
52     private DashboardFragment mDashboardFragment;
53     @Mock
54     private BluetoothDeviceUpdater mBluetoothDeviceUpdater;
55     @Mock
56     private DockUpdater mSavedDockUpdater;
57     @Mock
58     private PackageManager mPackageManager;
59 
60     private Context mContext;
61     private SavedDeviceGroupController mSavedDeviceGroupController;
62     private LifecycleOwner mLifecycleOwner;
63     private Lifecycle mLifecycle;
64 
65     @Before
setUp()66     public void setUp() {
67         MockitoAnnotations.initMocks(this);
68         mContext = spy(RuntimeEnvironment.application);
69         mLifecycleOwner = () -> mLifecycle;
70         mLifecycle = new Lifecycle(mLifecycleOwner);
71         doReturn(mContext).when(mDashboardFragment).getContext();
72         doReturn(mPackageManager).when(mContext).getPackageManager();
73         mSavedDeviceGroupController = new SavedDeviceGroupController(mContext);
74         mSavedDeviceGroupController.setBluetoothDeviceUpdater(mBluetoothDeviceUpdater);
75         mSavedDeviceGroupController.setSavedDockUpdater(mSavedDockUpdater);
76     }
77 
78     @Test
testRegister()79     public void testRegister() {
80         // register the callback in onStart()
81         mSavedDeviceGroupController.onStart();
82 
83         verify(mBluetoothDeviceUpdater).registerCallback();
84         verify(mSavedDockUpdater).registerCallback();
85         verify(mBluetoothDeviceUpdater).refreshPreference();
86     }
87 
88     @Test
testUnregister()89     public void testUnregister() {
90         // unregister the callback in onStop()
91         mSavedDeviceGroupController.onStop();
92         verify(mBluetoothDeviceUpdater).unregisterCallback();
93         verify(mSavedDockUpdater).unregisterCallback();
94     }
95 
96     @Test
testGetAvailabilityStatus_noBluetoothDockFeature_returnUnSupported()97     public void testGetAvailabilityStatus_noBluetoothDockFeature_returnUnSupported() {
98         doReturn(false).when(mPackageManager).hasSystemFeature(PackageManager.FEATURE_BLUETOOTH);
99         mSavedDeviceGroupController.setSavedDockUpdater(null);
100 
101         assertThat(mSavedDeviceGroupController.getAvailabilityStatus()).isEqualTo(
102                 UNSUPPORTED_ON_DEVICE);
103     }
104 
105     @Test
testGetAvailabilityStatus_BluetoothFeature_returnSupported()106     public void testGetAvailabilityStatus_BluetoothFeature_returnSupported() {
107         doReturn(true).when(mPackageManager).hasSystemFeature(PackageManager.FEATURE_BLUETOOTH);
108         mSavedDeviceGroupController.setSavedDockUpdater(null);
109 
110         assertThat(mSavedDeviceGroupController.getAvailabilityStatus()).isEqualTo(
111                 AVAILABLE);
112     }
113 
114     @Test
getAvailabilityStatus_haveDockFeature_returnSupported()115     public void getAvailabilityStatus_haveDockFeature_returnSupported() {
116         when(mPackageManager.hasSystemFeature(PackageManager.FEATURE_BLUETOOTH)).thenReturn(false);
117 
118         assertThat(mSavedDeviceGroupController.getAvailabilityStatus()).isEqualTo(
119             AVAILABLE);
120     }
121 }
122