1 /*
2  * Copyright (C) 2018 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.nfc;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import static org.mockito.Mockito.spy;
22 import static org.mockito.Mockito.verify;
23 import static org.mockito.Mockito.when;
24 
25 import android.content.Context;
26 import android.content.pm.PackageManager;
27 import android.os.UserHandle;
28 import android.os.UserManager;
29 
30 import androidx.preference.PreferenceScreen;
31 
32 import com.android.settings.R;
33 import com.android.settings.nfc.PaymentBackend.PaymentAppInfo;
34 import com.android.settings.testutils.shadow.ShadowNfcAdapter;
35 
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.junit.runner.RunWith;
39 import org.mockito.Mock;
40 import org.mockito.MockitoAnnotations;
41 import org.robolectric.RobolectricTestRunner;
42 import org.robolectric.RuntimeEnvironment;
43 import org.robolectric.annotation.Config;
44 
45 import java.util.ArrayList;
46 
47 @RunWith(RobolectricTestRunner.class)
48 @Config(shadows = ShadowNfcAdapter.class)
49 public class NfcPaymentPreferenceControllerTest {
50 
51     private static final String USER_TEST = "user_test";
52     private static final String PREF_KEY = PaymentSettingsTest.PAYMENT_KEY;
53 
54     @Mock
55     private PaymentBackend mPaymentBackend;
56     @Mock
57     private PreferenceScreen mScreen;
58     @Mock
59     private PackageManager mManager;
60     @Mock
61     private UserManager mUserManager;
62     @Mock
63     private UserHandle mUserHandle;
64     @Mock
65     private Context mContextAsUser;
66 
67     private Context mContext;
68     private NfcPaymentPreference mPreference;
69     private NfcPaymentPreferenceController mController;
70 
71     @Before
setUp()72     public void setUp() {
73         MockitoAnnotations.initMocks(this);
74         mContext = spy(RuntimeEnvironment.application);
75         when(mContext.getPackageManager()).thenReturn(mManager);
76         mController = new NfcPaymentPreferenceController(mContext, PREF_KEY);
77         mPreference = spy(new NfcPaymentPreference(mContext, null));
78         when(mScreen.findPreference(PREF_KEY)).thenReturn(mPreference);
79         when(mContext.createContextAsUser(UserHandle.CURRENT, 0)).thenReturn(mContextAsUser);
80         when(mContextAsUser.getSystemService(UserManager.class)).thenReturn(mUserManager);
81         when(mUserManager.getUserName()).thenReturn(USER_TEST);
82     }
83 
84     @Test
getAvailabilityStatus_noNFC_DISABLED()85     public void getAvailabilityStatus_noNFC_DISABLED() {
86         when(mManager.hasSystemFeature(PackageManager.FEATURE_NFC)).thenReturn(false);
87 
88         assertThat(mController.getAvailabilityStatus())
89                 .isNotEqualTo(NfcPaymentPreferenceController.AVAILABLE);
90     }
91 
92     @Test
getAvailabilityStatus_noPaymentApps_DISABLED()93     public void getAvailabilityStatus_noPaymentApps_DISABLED() {
94         when(mManager.hasSystemFeature(PackageManager.FEATURE_NFC)).thenReturn(true);
95         mController.setPaymentBackend(mPaymentBackend);
96         when(mPaymentBackend.getPaymentAppInfos()).thenReturn(null);
97 
98         assertThat(mController.getAvailabilityStatus())
99                 .isNotEqualTo(NfcPaymentPreferenceController.AVAILABLE);
100 
101         when(mPaymentBackend.getPaymentAppInfos()).thenReturn(new ArrayList<>());
102 
103         assertThat(mController.getAvailabilityStatus())
104                 .isNotEqualTo(NfcPaymentPreferenceController.AVAILABLE);
105     }
106 
107     @Test
getAvailabilityStatus_hasPaymentApps_AVAILABLE()108     public void getAvailabilityStatus_hasPaymentApps_AVAILABLE() {
109         when(mManager.hasSystemFeature(PackageManager.FEATURE_NFC)).thenReturn(true);
110         mController.setPaymentBackend(mPaymentBackend);
111         final ArrayList<PaymentAppInfo> appInfos = new ArrayList<>();
112         appInfos.add(new PaymentAppInfo());
113         when(mPaymentBackend.getPaymentAppInfos()).thenReturn(appInfos);
114 
115         assertThat(mController.getAvailabilityStatus())
116                 .isEqualTo(NfcPaymentPreferenceController.AVAILABLE);
117     }
118 
119     @Test
onStart_shouldRegisterCallback()120     public void onStart_shouldRegisterCallback() {
121         mController.setPaymentBackend(mPaymentBackend);
122 
123         mController.onStart();
124 
125         verify(mPaymentBackend).registerCallback(mController);
126     }
127 
128     @Test
onStop_shouldUnregisterCallback()129     public void onStop_shouldUnregisterCallback() {
130         mController.setPaymentBackend(mPaymentBackend);
131         mController.onStart();
132 
133         mController.onStop();
134 
135         verify(mPaymentBackend).unregisterCallback(mController);
136     }
137 
138     @Test
displayPreference_shouldInitialize()139     public void displayPreference_shouldInitialize() {
140         mController.setPaymentBackend(mPaymentBackend);
141 
142         mController.displayPreference(mScreen);
143 
144         verify(mPreference).initialize(mController);
145     }
146 
147     @Test
onPaymentAppsChanged_shouldRefreshSummary()148     public void onPaymentAppsChanged_shouldRefreshSummary() {
149         mController.setPaymentBackend(mPaymentBackend);
150         mController.displayPreference(mScreen);
151         when(mPaymentBackend.getDefaultApp()).thenReturn(null);
152 
153         mController.onPaymentAppsChanged();
154 
155         assertThat(mPreference.getSummary())
156                 .isEqualTo(mContext.getText(R.string.nfc_payment_default_not_set));
157 
158         final PaymentAppInfo appInfo = new PaymentAppInfo();
159         appInfo.label = "test label";
160         appInfo.userHandle = UserHandle.CURRENT;
161         when(mPaymentBackend.getDefaultApp()).thenReturn(appInfo);
162 
163         mController.onPaymentAppsChanged();
164 
165         assertThat(mPreference.getSummary())
166                 .isEqualTo(appInfo.label + " (" + USER_TEST + ")");
167     }
168 }
169