• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 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 android.content.Context;
19 import android.content.pm.PackageManager;
20 import android.provider.SearchIndexableResource;
21 
22 import com.android.settings.bluetooth.BluetoothMasterSwitchPreferenceController;
23 import com.android.settings.nfc.NfcPreferenceController;
24 import com.android.settings.SettingsRobolectricTestRunner;
25 import com.android.settings.TestConfig;
26 import com.android.settings.testutils.XmlTestUtils;
27 import com.android.settingslib.drawer.CategoryKey;
28 
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.mockito.Mock;
33 import org.mockito.MockitoAnnotations;
34 import org.robolectric.RuntimeEnvironment;
35 import org.robolectric.annotation.Config;
36 
37 import java.util.List;
38 
39 import static com.google.common.truth.Truth.assertThat;
40 import static org.mockito.Mockito.when;
41 
42 @RunWith(SettingsRobolectricTestRunner.class)
43 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
44 public class ConnectedDeviceDashboardFragmentTest {
45     @Mock
46     Context mContext;
47 
48     @Mock
49     private PackageManager mManager;
50 
51     private ConnectedDeviceDashboardFragment mFragment;
52 
53     @Before
setUp()54     public void setUp() {
55         MockitoAnnotations.initMocks(this);
56         mFragment = new ConnectedDeviceDashboardFragment();
57         when(mContext.getPackageManager()).thenReturn(mManager);
58     }
59 
60     @Test
testCategory_isConnectedDevice()61     public void testCategory_isConnectedDevice() {
62         assertThat(mFragment.getCategoryKey()).isEqualTo(CategoryKey.CATEGORY_DEVICE);
63     }
64 
65     @Test
testSearchIndexProvider_shouldIndexResource()66     public void testSearchIndexProvider_shouldIndexResource() {
67         final List<SearchIndexableResource> indexRes =
68                 mFragment.SEARCH_INDEX_DATA_PROVIDER.getXmlResourcesToIndex(mContext, true /* enabled */);
69 
70         assertThat(indexRes).isNotNull();
71         assertThat(indexRes.get(0).xmlResId).isEqualTo(mFragment.getPreferenceScreenResId());
72     }
73 
74     @Test
testSearchIndexProvider_NoNfc_KeyAdded()75     public void testSearchIndexProvider_NoNfc_KeyAdded() {
76         when(mManager.hasSystemFeature(PackageManager.FEATURE_NFC)).thenReturn(false);
77         final List<String> keys = mFragment.SEARCH_INDEX_DATA_PROVIDER.getNonIndexableKeys(mContext);
78 
79         assertThat(keys).isNotNull();
80         assertThat(keys).contains(NfcPreferenceController.KEY_TOGGLE_NFC);
81         assertThat(keys).contains(NfcPreferenceController.KEY_ANDROID_BEAM_SETTINGS);
82     }
83 
84     @Test
testSearchIndexProvider_NFC_KeyNotAdded()85     public void testSearchIndexProvider_NFC_KeyNotAdded() {
86         when(mManager.hasSystemFeature(PackageManager.FEATURE_NFC)).thenReturn(true);
87         final List<String> keys = mFragment.SEARCH_INDEX_DATA_PROVIDER.getNonIndexableKeys(mContext);
88 
89         assertThat(keys).isNotNull();
90         assertThat(keys).doesNotContain(NfcPreferenceController.KEY_TOGGLE_NFC);
91         assertThat(keys).doesNotContain(NfcPreferenceController.KEY_ANDROID_BEAM_SETTINGS);
92     }
93 
94     @Test
testNonIndexableKeys_existInXmlLayout()95     public void testNonIndexableKeys_existInXmlLayout() {
96         final Context context = RuntimeEnvironment.application;
97         when(mManager.hasSystemFeature(PackageManager.FEATURE_NFC)).thenReturn(false);
98         final List<String> niks = ConnectedDeviceDashboardFragment.SEARCH_INDEX_DATA_PROVIDER
99                 .getNonIndexableKeys(context);
100         final int xmlId = (new ConnectedDeviceDashboardFragment()).getPreferenceScreenResId();
101 
102         final List<String> keys = XmlTestUtils.getKeysFromPreferenceXml(context, xmlId);
103 
104         assertThat(keys).containsAllIn(niks);
105     }
106 }
107