1 /*
2  * Copyright (C) 2023 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.accessibility;
18 
19 import static com.android.settings.core.BasePreferenceController.AVAILABLE;
20 import static com.android.settings.core.BasePreferenceController.CONDITIONALLY_UNAVAILABLE;
21 
22 import static com.google.common.truth.Truth.assertThat;
23 
24 import static org.mockito.Mockito.doReturn;
25 
26 import android.content.Context;
27 import android.content.pm.PackageManager;
28 
29 import androidx.preference.Preference;
30 import androidx.preference.PreferenceCategory;
31 import androidx.preference.PreferenceManager;
32 import androidx.preference.PreferenceScreen;
33 import androidx.test.core.app.ApplicationProvider;
34 
35 import com.android.settings.bluetooth.Utils;
36 import com.android.settings.testutils.FakeFeatureFactory;
37 import com.android.settings.testutils.shadow.ShadowBluetoothUtils;
38 import com.android.settingslib.bluetooth.LocalBluetoothManager;
39 
40 import org.junit.Before;
41 import org.junit.Rule;
42 import org.junit.Test;
43 import org.junit.runner.RunWith;
44 import org.mockito.Mock;
45 import org.mockito.Spy;
46 import org.mockito.junit.MockitoJUnit;
47 import org.mockito.junit.MockitoRule;
48 import org.robolectric.RobolectricTestRunner;
49 import org.robolectric.annotation.Config;
50 
51 /** Tests for {@link BaseBluetoothDevicePreferenceController}. */
52 @RunWith(RobolectricTestRunner.class)
53 @Config(shadows = {ShadowBluetoothUtils.class})
54 public class BaseBluetoothDevicePreferenceControllerTest {
55     @Rule
56     public MockitoRule mMockitoRule = MockitoJUnit.rule();
57 
58     private static final String FAKE_KEY = "fake_key";
59     @Spy
60     private final Context mContext = ApplicationProvider.getApplicationContext();
61 
62     @Mock
63     private LocalBluetoothManager mLocalBluetoothManager;
64     @Mock
65     private PackageManager mPackageManager;
66     private PreferenceCategory mPreferenceCategory;
67     private PreferenceManager mPreferenceManager;
68     private PreferenceScreen mScreen;
69     private TestBaseBluetoothDevicePreferenceController mController;
70 
71     @Before
setUp()72     public void setUp() {
73         FakeFeatureFactory.setupForTest();
74         ShadowBluetoothUtils.sLocalBluetoothManager = mLocalBluetoothManager;
75         mPreferenceCategory = new PreferenceCategory(mContext);
76         mPreferenceCategory.setKey(FAKE_KEY);
77         mLocalBluetoothManager = Utils.getLocalBtManager(mContext);
78         mPreferenceManager = new PreferenceManager(mContext);
79         mScreen = mPreferenceManager.createPreferenceScreen(mContext);
80         mScreen.addPreference(mPreferenceCategory);
81         doReturn(mPackageManager).when(mContext).getPackageManager();
82         mController = new TestBaseBluetoothDevicePreferenceController(mContext, FAKE_KEY);
83     }
84 
85     @Test
getAvailabilityStatus_hasBluetoothFeature_available()86     public void getAvailabilityStatus_hasBluetoothFeature_available() {
87         doReturn(true).when(mPackageManager).hasSystemFeature(PackageManager.FEATURE_BLUETOOTH);
88 
89         assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE);
90     }
91 
92     @Test
getAvailabilityStatus_noBluetoothFeature_conditionallyUnavailalbe()93     public void getAvailabilityStatus_noBluetoothFeature_conditionallyUnavailalbe() {
94         doReturn(false).when(mPackageManager).hasSystemFeature(PackageManager.FEATURE_BLUETOOTH);
95 
96         assertThat(mController.getAvailabilityStatus()).isEqualTo(CONDITIONALLY_UNAVAILABLE);
97     }
98 
99     @Test
displayPreference_preferenceCategoryInVisible()100     public void displayPreference_preferenceCategoryInVisible() {
101         mController.displayPreference(mScreen);
102 
103         assertThat(mPreferenceCategory.isVisible()).isFalse();
104     }
105 
106     @Test
onDeviceAdded_preferenceCategoryVisible()107     public void onDeviceAdded_preferenceCategoryVisible() {
108         Preference preference = new Preference(mContext);
109         mController.displayPreference(mScreen);
110 
111         mController.onDeviceAdded(preference);
112 
113         assertThat(mPreferenceCategory.isVisible()).isTrue();
114     }
115 
116     @Test
onDeviceRemoved_addedPreferenceFirst_preferenceCategoryInVisible()117     public void onDeviceRemoved_addedPreferenceFirst_preferenceCategoryInVisible() {
118         Preference preference = new Preference(mContext);
119         mController.displayPreference(mScreen);
120 
121         mController.onDeviceAdded(preference);
122         mController.onDeviceRemoved(preference);
123 
124         assertThat(mPreferenceCategory.isVisible()).isFalse();
125     }
126 
127     public static class TestBaseBluetoothDevicePreferenceController extends
128             BaseBluetoothDevicePreferenceController {
129 
TestBaseBluetoothDevicePreferenceController(Context context, String preferenceKey)130         public TestBaseBluetoothDevicePreferenceController(Context context,
131                 String preferenceKey) {
132             super(context, preferenceKey);
133         }
134     }
135 }
136