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 
28 import androidx.preference.PreferenceScreen;
29 
30 import com.android.settings.R;
31 import com.android.settings.nfc.PaymentBackend.PaymentAppInfo;
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 
43 import java.util.ArrayList;
44 
45 @RunWith(RobolectricTestRunner.class)
46 @Config(shadows = ShadowNfcAdapter.class)
47 public class NfcPaymentPreferenceControllerTest {
48 
49     private static final String PREF_KEY = PaymentSettingsTest.PAYMENT_KEY;
50 
51     @Mock
52     private PaymentBackend mPaymentBackend;
53     @Mock
54     private PreferenceScreen mScreen;
55     @Mock
56     private PackageManager mManager;
57 
58     private Context mContext;
59     private NfcPaymentPreference mPreference;
60     private NfcPaymentPreferenceController mController;
61 
62     @Before
setUp()63     public void setUp() {
64         MockitoAnnotations.initMocks(this);
65         mContext = spy(RuntimeEnvironment.application);
66         when(mContext.getPackageManager()).thenReturn(mManager);
67         mController = new NfcPaymentPreferenceController(mContext, PREF_KEY);
68         mPreference = spy(new NfcPaymentPreference(mContext, null));
69         when(mScreen.findPreference(PREF_KEY)).thenReturn(mPreference);
70     }
71 
72     @Test
getAvailabilityStatus_noNFC_DISABLED()73     public void getAvailabilityStatus_noNFC_DISABLED() {
74         when(mManager.hasSystemFeature(PackageManager.FEATURE_NFC)).thenReturn(false);
75 
76         assertThat(mController.getAvailabilityStatus())
77                 .isNotEqualTo(NfcPaymentPreferenceController.AVAILABLE);
78     }
79 
80     @Test
getAvailabilityStatus_noPaymentApps_DISABLED()81     public void getAvailabilityStatus_noPaymentApps_DISABLED() {
82         when(mManager.hasSystemFeature(PackageManager.FEATURE_NFC)).thenReturn(true);
83         mController.setPaymentBackend(mPaymentBackend);
84         when(mPaymentBackend.getPaymentAppInfos()).thenReturn(null);
85 
86         assertThat(mController.getAvailabilityStatus())
87                 .isNotEqualTo(NfcPaymentPreferenceController.AVAILABLE);
88 
89         when(mPaymentBackend.getPaymentAppInfos()).thenReturn(new ArrayList<>());
90 
91         assertThat(mController.getAvailabilityStatus())
92                 .isNotEqualTo(NfcPaymentPreferenceController.AVAILABLE);
93     }
94 
95     @Test
getAvailabilityStatus_hasPaymentApps_AVAILABLE()96     public void getAvailabilityStatus_hasPaymentApps_AVAILABLE() {
97         when(mManager.hasSystemFeature(PackageManager.FEATURE_NFC)).thenReturn(true);
98         mController.setPaymentBackend(mPaymentBackend);
99         final ArrayList<PaymentAppInfo> appInfos = new ArrayList<>();
100         appInfos.add(new PaymentAppInfo());
101         when(mPaymentBackend.getPaymentAppInfos()).thenReturn(appInfos);
102 
103         assertThat(mController.getAvailabilityStatus())
104                 .isEqualTo(NfcPaymentPreferenceController.AVAILABLE);
105     }
106 
107     @Test
onStart_shouldRegisterCallback()108     public void onStart_shouldRegisterCallback() {
109         mController.setPaymentBackend(mPaymentBackend);
110 
111         mController.onStart();
112 
113         verify(mPaymentBackend).registerCallback(mController);
114     }
115 
116     @Test
onStop_shouldUnregisterCallback()117     public void onStop_shouldUnregisterCallback() {
118         mController.setPaymentBackend(mPaymentBackend);
119         mController.onStart();
120 
121         mController.onStop();
122 
123         verify(mPaymentBackend).unregisterCallback(mController);
124     }
125 
126     @Test
displayPreference_shouldInitialize()127     public void displayPreference_shouldInitialize() {
128         mController.setPaymentBackend(mPaymentBackend);
129 
130         mController.displayPreference(mScreen);
131 
132         verify(mPreference).initialize(mController);
133     }
134 
135     @Test
onPaymentAppsChanged_shouldRefreshSummary()136     public void onPaymentAppsChanged_shouldRefreshSummary() {
137         mController.setPaymentBackend(mPaymentBackend);
138         mController.displayPreference(mScreen);
139         when(mPaymentBackend.getDefaultApp()).thenReturn(null);
140 
141         mController.onPaymentAppsChanged();
142 
143         assertThat(mPreference.getSummary())
144                 .isEqualTo(mContext.getText(R.string.nfc_payment_default_not_set));
145 
146         final PaymentAppInfo appInfo = new PaymentAppInfo();
147         appInfo.label = "test label";
148         when(mPaymentBackend.getDefaultApp()).thenReturn(appInfo);
149 
150         mController.onPaymentAppsChanged();
151 
152         assertThat(mPreference.getSummary()).isEqualTo(appInfo.label);
153     }
154 }