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 package com.android.settings.connecteddevice;
17 
18 import static com.android.settings.connecteddevice.ConnectedDeviceDashboardFragment.KEY_AVAILABLE_DEVICES;
19 import static com.android.settings.connecteddevice.ConnectedDeviceDashboardFragment.KEY_CONNECTED_DEVICES;
20 
21 import static com.google.common.truth.Truth.assertThat;
22 
23 import static org.mockito.ArgumentMatchers.any;
24 import static org.mockito.Mockito.doReturn;
25 import static org.mockito.Mockito.spy;
26 import static org.mockito.Mockito.when;
27 
28 import android.content.Context;
29 import android.content.pm.PackageManager;
30 import android.platform.test.flag.junit.CheckFlagsRule;
31 import android.platform.test.flag.junit.DeviceFlagsValueProvider;
32 import android.platform.test.flag.junit.SetFlagsRule;
33 import android.provider.SearchIndexableResource;
34 
35 import com.android.settings.R;
36 import com.android.settings.connecteddevice.fastpair.FastPairDeviceUpdater;
37 import com.android.settings.core.BasePreferenceController;
38 import com.android.settings.core.PreferenceControllerListHelper;
39 import com.android.settings.flags.Flags;
40 import com.android.settings.slices.SlicePreferenceController;
41 import com.android.settings.testutils.FakeFeatureFactory;
42 import com.android.settings.testutils.shadow.ShadowBluetoothAdapter;
43 import com.android.settings.testutils.shadow.ShadowConnectivityManager;
44 import com.android.settings.testutils.shadow.ShadowUserManager;
45 
46 import org.junit.Before;
47 import org.junit.Rule;
48 import org.junit.Test;
49 import org.junit.runner.RunWith;
50 import org.mockito.Mock;
51 import org.mockito.MockitoAnnotations;
52 import org.robolectric.RobolectricTestRunner;
53 import org.robolectric.RuntimeEnvironment;
54 import org.robolectric.annotation.Config;
55 
56 import java.util.List;
57 
58 @RunWith(RobolectricTestRunner.class)
59 @Config(
60         shadows = {
61             ShadowUserManager.class,
62             ShadowConnectivityManager.class,
63             ShadowBluetoothAdapter.class
64         })
65 public class ConnectedDeviceDashboardFragmentTest {
66     @Rule
67     public final CheckFlagsRule mCheckFlagsRule = DeviceFlagsValueProvider.createCheckFlagsRule();
68 
69     @Rule public final SetFlagsRule mSetFlagsRule = new SetFlagsRule();
70 
71     private static final String KEY_NEARBY_DEVICES = "bt_nearby_slice";
72     private static final String KEY_DISCOVERABLE_FOOTER = "discoverable_footer";
73     private static final String KEY_SAVED_DEVICE_SEE_ALL = "previously_connected_devices_see_all";
74     private static final String KEY_FAST_PAIR_DEVICE_SEE_ALL = "fast_pair_devices_see_all";
75     private static final String KEY_AUDIO_SHARING_DEVICES = "audio_sharing_device_list";
76     private static final String KEY_AUDIO_SHARING_SETTINGS =
77             "connected_device_audio_sharing_settings";
78     private static final String KEY_ADD_BT_DEVICES = "add_bt_devices";
79     private static final String SETTINGS_PACKAGE_NAME = "com.android.settings";
80     private static final String SYSTEMUI_PACKAGE_NAME = "com.android.systemui";
81     private static final String SLICE_ACTION = "com.android.settings.SEARCH_RESULT_TRAMPOLINE";
82     private static final String TEST_APP_NAME = "com.testapp.settings";
83     private static final String TEST_ACTION = "com.testapp.settings.ACTION_START";
84 
85     @Mock private PackageManager mPackageManager;
86     @Mock private FastPairDeviceUpdater mFastPairDeviceUpdater;
87     private Context mContext;
88     private ConnectedDeviceDashboardFragment mFragment;
89     private FakeFeatureFactory mFeatureFactory;
90 
91     @Before
setUp()92     public void setUp() {
93         MockitoAnnotations.initMocks(this);
94 
95         mContext = spy(RuntimeEnvironment.application);
96         mFragment = new ConnectedDeviceDashboardFragment();
97         mSetFlagsRule.enableFlags(Flags.FLAG_ENABLE_SUBSEQUENT_PAIR_SETTINGS_INTEGRATION);
98         mSetFlagsRule.enableFlags(com.android.settingslib.flags.Flags.FLAG_ENABLE_LE_AUDIO_SHARING);
99         mFeatureFactory = FakeFeatureFactory.setupForTest();
100         when(mFeatureFactory
101                         .getFastPairFeatureProvider()
102                         .getFastPairDeviceUpdater(
103                                 any(Context.class), any(DevicePreferenceCallback.class)))
104                 .thenReturn(mFastPairDeviceUpdater);
105         doReturn(mPackageManager).when(mContext).getPackageManager();
106         doReturn(true).when(mPackageManager).hasSystemFeature(PackageManager.FEATURE_BLUETOOTH);
107     }
108 
109     @Test
searchIndexProvider_shouldIndexResource()110     public void searchIndexProvider_shouldIndexResource() {
111         final List<SearchIndexableResource> indexRes =
112                 ConnectedDeviceDashboardFragment.SEARCH_INDEX_DATA_PROVIDER.getXmlResourcesToIndex(
113                         mContext, true /* enabled */);
114 
115         assertThat(indexRes).isNotNull();
116         assertThat(indexRes.get(0).xmlResId).isEqualTo(R.xml.connected_devices);
117     }
118 
119     @Test
nonIndexableKeys_existInXmlLayout()120     public void nonIndexableKeys_existInXmlLayout() {
121         final List<String> niks =
122                 ConnectedDeviceDashboardFragment.SEARCH_INDEX_DATA_PROVIDER.getNonIndexableKeys(
123                         mContext);
124 
125         assertThat(niks)
126                 .containsExactly(
127                         KEY_CONNECTED_DEVICES,
128                         KEY_AVAILABLE_DEVICES,
129                         KEY_NEARBY_DEVICES,
130                         KEY_DISCOVERABLE_FOOTER,
131                         KEY_SAVED_DEVICE_SEE_ALL,
132                         KEY_FAST_PAIR_DEVICE_SEE_ALL,
133                         KEY_AUDIO_SHARING_DEVICES,
134                         KEY_AUDIO_SHARING_SETTINGS);
135     }
136 
137     @Test
isAlwaysDiscoverable_callingAppIsNotFromSystemApp_returnsFalse()138     public void isAlwaysDiscoverable_callingAppIsNotFromSystemApp_returnsFalse() {
139         assertThat(mFragment.isAlwaysDiscoverable(TEST_APP_NAME, TEST_ACTION)).isFalse();
140     }
141 
142     @Test
isAlwaysDiscoverable_callingAppIsFromSettings_returnsTrue()143     public void isAlwaysDiscoverable_callingAppIsFromSettings_returnsTrue() {
144         assertThat(mFragment.isAlwaysDiscoverable(SETTINGS_PACKAGE_NAME, TEST_ACTION)).isTrue();
145     }
146 
147     @Test
isAlwaysDiscoverable_callingAppIsFromSystemUI_returnsTrue()148     public void isAlwaysDiscoverable_callingAppIsFromSystemUI_returnsTrue() {
149         assertThat(mFragment.isAlwaysDiscoverable(SYSTEMUI_PACKAGE_NAME, TEST_ACTION)).isTrue();
150     }
151 
152     @Test
isAlwaysDiscoverable_actionIsFromSlice_returnsFalse()153     public void isAlwaysDiscoverable_actionIsFromSlice_returnsFalse() {
154         assertThat(mFragment.isAlwaysDiscoverable(SYSTEMUI_PACKAGE_NAME, SLICE_ACTION)).isFalse();
155     }
156 
157     @Test
getPreferenceControllers_containSlicePrefController()158     public void getPreferenceControllers_containSlicePrefController() {
159         final List<BasePreferenceController> controllers =
160                 PreferenceControllerListHelper.getPreferenceControllersFromXml(
161                         mContext, R.xml.connected_devices);
162 
163         assertThat(
164                         controllers.stream()
165                                 .filter(
166                                         controller ->
167                                                 controller instanceof SlicePreferenceController)
168                                 .count())
169                 .isEqualTo(1);
170     }
171 }
172