1 /*
2  * Copyright (C) 2020 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.connecteddevice;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import static org.mockito.Mockito.spy;
22 import static org.mockito.Mockito.when;
23 
24 import android.content.Context;
25 import android.content.pm.PackageManager;
26 import android.nfc.NfcAdapter;
27 import android.provider.SearchIndexableResource;
28 
29 import com.android.settings.testutils.shadow.ShadowNfcAdapter;
30 
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 import org.mockito.Mock;
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 = ShadowNfcAdapter.class)
45 public class NfcAndPaymentFragmentTest {
46     @Mock
47     private PackageManager mPackageManager;
48 
49     private NfcAndPaymentFragment mFragment;
50     private Context mContext;
51     private ShadowNfcAdapter mShadowNfcAdapter;
52 
53     @Before
setUp()54     public void setUp() {
55         MockitoAnnotations.initMocks(this);
56 
57         mFragment = new NfcAndPaymentFragment();
58         mContext = spy(RuntimeEnvironment.application);
59         mShadowNfcAdapter = Shadow.extract(NfcAdapter.getDefaultAdapter(mContext));
60 
61         when(mContext.getPackageManager()).thenReturn(mPackageManager);
62     }
63 
64     @Test
searchIndexProvider_shouldIndexResource()65     public void searchIndexProvider_shouldIndexResource() {
66         final List<SearchIndexableResource> indexRes =
67                 NfcAndPaymentFragment.SEARCH_INDEX_DATA_PROVIDER
68                         .getXmlResourcesToIndex(mContext, true /* enabled */);
69 
70         assertThat(indexRes).isNotNull();
71         assertThat(indexRes.get(0).xmlResId).isEqualTo(mFragment.getPreferenceScreenResId());
72     }
73 
74     @Test
searchIndexProvider_shouldIndexValidItems()75     public void searchIndexProvider_shouldIndexValidItems() {
76         when(mContext.getApplicationContext()).thenReturn(mContext);
77         when(mPackageManager.hasSystemFeature(PackageManager.FEATURE_NFC)).thenReturn(true);
78         when(mPackageManager.hasSystemFeature(
79                 PackageManager.FEATURE_NFC_HOST_CARD_EMULATION)).thenReturn(true);
80         mShadowNfcAdapter.setSecureNfcSupported(true);
81 
82         final List<String> niks = NfcAndPaymentFragment.SEARCH_INDEX_DATA_PROVIDER
83                 .getNonIndexableKeys(mContext);
84 
85         assertThat(niks).isNotNull();
86         assertThat(niks).containsExactly("nfc_detection_point");
87     }
88 }
89