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.ArgumentMatchers.anyString;
22 import static org.mockito.Mockito.spy;
23 import static org.mockito.Mockito.when;
24 
25 import android.content.Context;
26 import android.content.pm.PackageManager;
27 import android.nfc.NfcAdapter;
28 import android.nfc.NfcManager;
29 import android.os.UserManager;
30 
31 import com.android.settings.R;
32 import com.android.settings.testutils.shadow.ShadowNfcAdapter;
33 
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.mockito.Mock;
38 import org.mockito.MockitoAnnotations;
39 import org.robolectric.RobolectricTestRunner;
40 import org.robolectric.RuntimeEnvironment;
41 import org.robolectric.annotation.Config;
42 import org.robolectric.shadow.api.Shadow;
43 import org.robolectric.util.ReflectionHelpers;
44 
45 @RunWith(RobolectricTestRunner.class)
46 @Config(shadows = ShadowNfcAdapter.class)
47 public class NfcAndPaymentFragmentControllerTest {
48     private NfcAndPaymentFragmentController mController;
49     private Context mContext;
50 
51     @Mock
52     private PackageManager mPackageManager;
53     @Mock
54     private UserManager mUserManager;
55     @Mock
56     private NfcManager mNfcManager;
57 
58     private ShadowNfcAdapter mShadowNfcAdapter;
59 
60     @Before
setUp()61     public void setUp() {
62         MockitoAnnotations.initMocks(this);
63         mContext = spy(RuntimeEnvironment.application);
64         mShadowNfcAdapter = Shadow.extract(NfcAdapter.getDefaultAdapter(mContext));
65 
66         when(mContext.getApplicationContext()).thenReturn(mContext);
67         when(mContext.getPackageManager()).thenReturn(mPackageManager);
68         when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager);
69         when(mContext.getSystemService(Context.NFC_SERVICE)).thenReturn(mNfcManager);
70 
71         mController = new NfcAndPaymentFragmentController(mContext, "fakeKey");
72     }
73 
74     @Test
getAvailabilityStatus_hasNfc_shouldReturnAvailable()75     public void getAvailabilityStatus_hasNfc_shouldReturnAvailable() {
76         when(mPackageManager.hasSystemFeature(anyString())).thenReturn(true);
77         when(mUserManager.isAdminUser()).thenReturn(true);
78         mShadowNfcAdapter.setEnabled(true);
79 
80         assertThat(mController.getAvailabilityStatus())
81                 .isEqualTo(NfcAndPaymentFragmentController.AVAILABLE);
82     }
83 
84     @Test
getAvailabilityStatus_noNfcAdapter_shouldReturnUnsupported()85     public void getAvailabilityStatus_noNfcAdapter_shouldReturnUnsupported() {
86         ReflectionHelpers.setField(mController, "mNfcAdapter", null);
87         assertThat(mController.getAvailabilityStatus())
88                 .isEqualTo(NfcAndPaymentFragmentController.UNSUPPORTED_ON_DEVICE);
89     }
90 
91     @Test
getSummary_nfcOn_shouldProvideOnSummary()92     public void getSummary_nfcOn_shouldProvideOnSummary() {
93         mShadowNfcAdapter.setEnabled(true);
94         assertThat(mController.getSummary().toString()).contains(
95                 mContext.getString(R.string.switch_on_text));
96     }
97 
98     @Test
getSummary_nfcOff_shouldProvideOffSummary()99     public void getSummary_nfcOff_shouldProvideOffSummary() {
100         mShadowNfcAdapter.setEnabled(false);
101         assertThat(mController.getSummary().toString()).contains(
102                 mContext.getString(R.string.switch_off_text));
103     }
104 }
105