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