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         verify(mBluetoothDeviceUpdater).registerCallback();
83         verify(mSavedDockUpdater).registerCallback();
84     }
85 
86     @Test
testUnregister()87     public void testUnregister() {
88         // unregister the callback in onStop()
89         mSavedDeviceGroupController.onStop();
90         verify(mBluetoothDeviceUpdater).unregisterCallback();
91         verify(mSavedDockUpdater).unregisterCallback();
92     }
93 
94     @Test
testGetAvailabilityStatus_noBluetoothDockFeature_returnUnSupported()95     public void testGetAvailabilityStatus_noBluetoothDockFeature_returnUnSupported() {
96         doReturn(false).when(mPackageManager).hasSystemFeature(PackageManager.FEATURE_BLUETOOTH);
97         mSavedDeviceGroupController.setSavedDockUpdater(null);
98 
99         assertThat(mSavedDeviceGroupController.getAvailabilityStatus()).isEqualTo(
100                 UNSUPPORTED_ON_DEVICE);
101     }
102 
103     @Test
testGetAvailabilityStatus_BluetoothFeature_returnSupported()104     public void testGetAvailabilityStatus_BluetoothFeature_returnSupported() {
105         doReturn(true).when(mPackageManager).hasSystemFeature(PackageManager.FEATURE_BLUETOOTH);
106         mSavedDeviceGroupController.setSavedDockUpdater(null);
107 
108         assertThat(mSavedDeviceGroupController.getAvailabilityStatus()).isEqualTo(
109                 AVAILABLE);
110     }
111 
112     @Test
getAvailabilityStatus_haveDockFeature_returnSupported()113     public void getAvailabilityStatus_haveDockFeature_returnSupported() {
114         when(mPackageManager.hasSystemFeature(PackageManager.FEATURE_BLUETOOTH)).thenReturn(false);
115 
116         assertThat(mSavedDeviceGroupController.getAvailabilityStatus()).isEqualTo(
117             AVAILABLE);
118     }
119 }
120