• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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 
18 package com.android.settings.nfc;
19 
20 import static com.google.common.truth.Truth.assertThat;
21 
22 import static org.mockito.Mockito.doReturn;
23 import static org.mockito.Mockito.spy;
24 import static org.mockito.Mockito.when;
25 
26 import android.content.Context;
27 import android.content.pm.PackageManager;
28 import android.content.pm.UserInfo;
29 import android.os.UserHandle;
30 import android.os.UserManager;
31 import android.platform.test.annotations.RequiresFlagsDisabled;
32 import android.platform.test.annotations.RequiresFlagsEnabled;
33 import android.platform.test.flag.junit.CheckFlagsRule;
34 import android.platform.test.flag.junit.DeviceFlagsValueProvider;
35 
36 import androidx.preference.Preference;
37 import androidx.preference.PreferenceManager;
38 import androidx.preference.PreferenceScreen;
39 
40 import com.android.settings.testutils.shadow.ShadowNfcAdapter;
41 
42 import org.junit.Before;
43 import org.junit.Rule;
44 import org.junit.Test;
45 import org.junit.runner.RunWith;
46 import org.mockito.Mock;
47 import org.mockito.MockitoAnnotations;
48 import org.robolectric.RobolectricTestRunner;
49 import org.robolectric.RuntimeEnvironment;
50 import org.robolectric.annotation.Config;
51 import org.robolectric.annotation.Implementation;
52 import org.robolectric.annotation.Implements;
53 
54 import java.util.ArrayList;
55 import java.util.List;
56 
57 @RunWith(RobolectricTestRunner.class)
58 @Config(shadows = {PaymentSettingsTest.ShadowPaymentBackend.class, ShadowNfcAdapter.class})
59 public class PaymentSettingsTest {
60 
61     static final String PAYMENT_KEY = "nfc_payment_app";
62     static final String FOREGROUND_KEY = "nfc_foreground";
63 
64     private Context mContext;
65 
66     @Rule
67     public final CheckFlagsRule mCheckFlagsRule = DeviceFlagsValueProvider.createCheckFlagsRule();
68 
69     @Mock
70     private PackageManager mPackageManager;
71 
72     @Mock
73     private UserManager mUserManager;
74 
75     @Mock
76     private UserInfo mUserInfo;
77 
78     @Before
setUp()79     public void setUp() {
80         MockitoAnnotations.initMocks(this);
81         mContext = spy(RuntimeEnvironment.application);
82         when(mContext.getPackageManager()).thenReturn(mPackageManager);
83         doReturn(mUserManager).when(mContext).getSystemService(UserManager.class);
84         when(mUserManager.getUserInfo(UserHandle.myUserId())).thenReturn(mUserInfo);
85     }
86 
87     @Test
getNonIndexableKey_noNFC_allKeysAdded()88     public void getNonIndexableKey_noNFC_allKeysAdded() {
89         when(mPackageManager.hasSystemFeature(PackageManager.FEATURE_NFC)).thenReturn(false);
90 
91         final List<String> niks =
92                 PaymentSettings.SEARCH_INDEX_DATA_PROVIDER.getNonIndexableKeys(mContext);
93 
94         assertThat(niks).contains(PAYMENT_KEY);
95         assertThat(niks).contains(FOREGROUND_KEY);
96     }
97 
98     @Test
getNonIndexableKey_NFC_foregroundKeyAdded()99     public void getNonIndexableKey_NFC_foregroundKeyAdded() {
100         when(mPackageManager.hasSystemFeature(PackageManager.FEATURE_NFC)).thenReturn(true);
101 
102         final List<String> niks =
103                 PaymentSettings.SEARCH_INDEX_DATA_PROVIDER.getNonIndexableKeys(mContext);
104 
105         assertThat(niks).contains(FOREGROUND_KEY);
106     }
107 
108     @Test
109     @RequiresFlagsEnabled(android.permission.flags.Flags.FLAG_WALLET_ROLE_ENABLED)
getNonIndexableKey_primaryUser_returnsFalse_walletRoleEnabled()110     public void getNonIndexableKey_primaryUser_returnsFalse_walletRoleEnabled() {
111         when(mPackageManager.hasSystemFeature(PackageManager.FEATURE_NFC)).thenReturn(true);
112 
113         final List<String> niks =
114                 PaymentSettings.SEARCH_INDEX_DATA_PROVIDER.getNonIndexableKeys(mContext);
115 
116         assertThat(niks).containsAtLeast(FOREGROUND_KEY, PAYMENT_KEY);
117     }
118 
119     @Test
120     @RequiresFlagsDisabled(android.permission.flags.Flags.FLAG_WALLET_ROLE_ENABLED)
getNonIndexableKey_primaryUser_returnsTrue_walletRoleDisabled()121     public void getNonIndexableKey_primaryUser_returnsTrue_walletRoleDisabled() {
122         when(mPackageManager.hasSystemFeature(PackageManager.FEATURE_NFC)).thenReturn(true);
123 
124         final List<String> niks =
125                 PaymentSettings.SEARCH_INDEX_DATA_PROVIDER.getNonIndexableKeys(mContext);
126 
127         assertThat(niks).containsExactly(FOREGROUND_KEY);
128     }
129 
130     @Test
getNonIndexableKey_guestUser_returnsFalse()131     public void getNonIndexableKey_guestUser_returnsFalse() {
132         when(mPackageManager.hasSystemFeature(PackageManager.FEATURE_NFC)).thenReturn(true);
133         when(mUserInfo.isGuest()).thenReturn(true);
134 
135         final List<String> niks =
136                 PaymentSettings.SEARCH_INDEX_DATA_PROVIDER.getNonIndexableKeys(mContext);
137 
138         assertThat(niks).containsAtLeast(FOREGROUND_KEY, PAYMENT_KEY);
139     }
140 
141     @Test
isShowEmptyImage_hasVisiblePreference_returnFalse()142     public void isShowEmptyImage_hasVisiblePreference_returnFalse() {
143         final PaymentSettings paymentSettings = new PaymentSettings();
144         final PreferenceManager preferenceManager = new PreferenceManager(mContext);
145         final PreferenceScreen screen = preferenceManager.createPreferenceScreen(mContext);
146         final Preference preference1 = new Preference(mContext);
147         screen.addPreference(preference1);
148         final Preference preference2 = new Preference(mContext);
149         screen.addPreference(preference2);
150 
151         assertThat(paymentSettings.isShowEmptyImage(screen)).isFalse();
152     }
153 
154     @Test
isShowEmptyImage_hasNoVisiblePreference_returnTrue()155     public void isShowEmptyImage_hasNoVisiblePreference_returnTrue() {
156         final PaymentSettings paymentSettings = new PaymentSettings();
157         final PreferenceManager preferenceManager = new PreferenceManager(mContext);
158         final PreferenceScreen screen = preferenceManager.createPreferenceScreen(mContext);
159         final Preference preference1 = new Preference(mContext);
160         preference1.setVisible(false);
161         screen.addPreference(preference1);
162         final Preference preference2 = new Preference(mContext);
163         screen.addPreference(preference2);
164         preference2.setVisible(false);
165 
166         assertThat(paymentSettings.isShowEmptyImage(screen)).isTrue();
167     }
168 
169     @Implements(PaymentBackend.class)
170     public static class ShadowPaymentBackend {
171         private ArrayList<PaymentBackend.PaymentAppInfo> mAppInfos;
172 
__constructor__(Context context)173         public void __constructor__(Context context) {
174             mAppInfos = new ArrayList<>();
175             mAppInfos.add(new PaymentBackend.PaymentAppInfo());
176         }
177 
178         @Implementation
getPaymentAppInfos()179         protected List<PaymentBackend.PaymentAppInfo> getPaymentAppInfos() {
180             return mAppInfos;
181         }
182     }
183 }
184