1 /*
2  * Copyright (C) 2017 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.google.common.truth.Truth.assertThat;
19 
20 import static org.mockito.Mockito.spy;
21 
22 import android.content.Context;
23 import android.nfc.NfcAdapter;
24 import android.provider.SearchIndexableResource;
25 
26 import com.android.settings.nfc.AndroidBeamPreferenceController;
27 import com.android.settings.testutils.shadow.ShadowConnectivityManager;
28 import com.android.settings.testutils.shadow.ShadowNfcAdapter;
29 import com.android.settings.testutils.shadow.ShadowUserManager;
30 import com.android.settingslib.drawer.CategoryKey;
31 
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 import org.mockito.MockitoAnnotations;
36 import org.robolectric.RobolectricTestRunner;
37 import org.robolectric.RuntimeEnvironment;
38 import org.robolectric.annotation.Config;
39 import org.robolectric.shadow.api.Shadow;
40 
41 import java.util.List;
42 
43 @RunWith(RobolectricTestRunner.class)
44 @Config(shadows = {ShadowUserManager.class,
45         ShadowConnectivityManager.class, ShadowNfcAdapter.class})
46 public class AdvancedConnectedDeviceDashboardFragmentTest {
47 
48     private AdvancedConnectedDeviceDashboardFragment mFragment;
49 
50     private Context mContext;
51     private ShadowNfcAdapter mShadowNfcAdapter;
52 
53     @Before
setUp()54     public void setUp() {
55         MockitoAnnotations.initMocks(this);
56 
57         mContext = spy(RuntimeEnvironment.application);
58         mFragment = new AdvancedConnectedDeviceDashboardFragment();
59         mShadowNfcAdapter = Shadow.extract(NfcAdapter.getDefaultAdapter(mContext));
60     }
61 
62     @Test
testCategory_isConnectedDevice()63     public void testCategory_isConnectedDevice() {
64         assertThat(mFragment.getCategoryKey()).isEqualTo(CategoryKey.CATEGORY_DEVICE);
65     }
66 
67     @Test
testSearchIndexProvider_shouldIndexResource()68     public void testSearchIndexProvider_shouldIndexResource() {
69         final List<SearchIndexableResource> indexRes =
70                 AdvancedConnectedDeviceDashboardFragment.SEARCH_INDEX_DATA_PROVIDER
71                         .getXmlResourcesToIndex(RuntimeEnvironment.application, true /* enabled */);
72 
73         assertThat(indexRes).isNotNull();
74         assertThat(indexRes.get(0).xmlResId).isEqualTo(mFragment.getPreferenceScreenResId());
75     }
76 
77     @Test
testGetCategoryKey_returnCategoryDevice()78     public void testGetCategoryKey_returnCategoryDevice() {
79         assertThat(mFragment.getCategoryKey()).isEqualTo(CategoryKey.CATEGORY_DEVICE);
80     }
81 
82     @Test
testSearchIndexProvider_correctNonIndexables()83     public void testSearchIndexProvider_correctNonIndexables() {
84         mShadowNfcAdapter.setSecureNfcSupported(true);
85         final List<String> niks =
86                 AdvancedConnectedDeviceDashboardFragment.SEARCH_INDEX_DATA_PROVIDER
87                         .getNonIndexableKeys(mContext);
88 
89         assertThat(niks).contains(AndroidBeamPreferenceController.KEY_ANDROID_BEAM_SETTINGS);
90     }
91 }