• 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 
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 import org.mockito.Mock;
36 import org.mockito.MockitoAnnotations;
37 import org.robolectric.RobolectricTestRunner;
38 import org.robolectric.RuntimeEnvironment;
39 import org.robolectric.annotation.Config;
40 import org.robolectric.annotation.Implementation;
41 import org.robolectric.annotation.Implements;
42 
43 import java.util.ArrayList;
44 import java.util.List;
45 
46 @RunWith(RobolectricTestRunner.class)
47 @Config(shadows = PaymentSettingsTest.ShadowPaymentBackend.class)
48 public class PaymentSettingsTest {
49 
50     static final String PAYMENT_KEY = "nfc_payment";
51     static final String FOREGROUND_KEY = "nfc_foreground";
52     static final String PAYMENT_SCREEN_KEY = "nfc_payment_settings_screen";
53 
54     private Context mContext;
55 
56     @Mock
57     private PackageManager mPackageManager;
58 
59     @Mock
60     private UserManager mUserManager;
61 
62     @Mock
63     private UserInfo mUserInfo;
64 
65     @Before
setUp()66     public void setUp() {
67         MockitoAnnotations.initMocks(this);
68         mContext = spy(RuntimeEnvironment.application);
69         when(mContext.getPackageManager()).thenReturn(mPackageManager);
70         doReturn(mUserManager).when(mContext).getSystemService(UserManager.class);
71         when(mUserManager.getUserInfo(UserHandle.myUserId())).thenReturn(mUserInfo);
72     }
73 
74     @Test
getNonIndexableKey_noNFC_allKeysAdded()75     public void getNonIndexableKey_noNFC_allKeysAdded() {
76         when(mPackageManager.hasSystemFeature(PackageManager.FEATURE_NFC)).thenReturn(false);
77 
78         final List<String> niks =
79                 PaymentSettings.SEARCH_INDEX_DATA_PROVIDER.getNonIndexableKeys(mContext);
80 
81         assertThat(niks).contains(PAYMENT_KEY);
82         assertThat(niks).contains(FOREGROUND_KEY);
83     }
84 
85     @Test
getNonIndexableKey_NFC_foregroundKeyAdded()86     public void getNonIndexableKey_NFC_foregroundKeyAdded() {
87         when(mPackageManager.hasSystemFeature(PackageManager.FEATURE_NFC)).thenReturn(true);
88 
89         final List<String> niks =
90                 PaymentSettings.SEARCH_INDEX_DATA_PROVIDER.getNonIndexableKeys(mContext);
91 
92         assertThat(niks).contains(FOREGROUND_KEY);
93     }
94 
95     @Test
getNonIndexableKey_primaryUser_returnsTrue()96     public void getNonIndexableKey_primaryUser_returnsTrue() {
97         when(mPackageManager.hasSystemFeature(PackageManager.FEATURE_NFC)).thenReturn(true);
98 
99         final List<String> niks =
100                 PaymentSettings.SEARCH_INDEX_DATA_PROVIDER.getNonIndexableKeys(mContext);
101 
102         assertThat(niks).containsExactly(FOREGROUND_KEY);
103     }
104 
105     @Test
getNonIndexabkeKey_guestUser_returnsFalse()106     public void getNonIndexabkeKey_guestUser_returnsFalse() {
107         when(mPackageManager.hasSystemFeature(PackageManager.FEATURE_NFC)).thenReturn(true);
108         when(mUserInfo.isGuest()).thenReturn(true);
109 
110         final List<String> niks =
111                 PaymentSettings.SEARCH_INDEX_DATA_PROVIDER.getNonIndexableKeys(mContext);
112 
113         assertThat(niks).containsAllOf(FOREGROUND_KEY, PAYMENT_KEY, PAYMENT_SCREEN_KEY);
114     }
115 
116     @Implements(PaymentBackend.class)
117     public static class ShadowPaymentBackend {
118         private ArrayList<PaymentBackend.PaymentAppInfo> mAppInfos;
119 
__constructor__(Context context)120         public void __constructor__(Context context) {
121             mAppInfos = new ArrayList<>();
122             mAppInfos.add(new PaymentBackend.PaymentAppInfo());
123         }
124 
125         @Implementation
getPaymentAppInfos()126         protected List<PaymentBackend.PaymentAppInfo> getPaymentAppInfos() {
127             return mAppInfos;
128         }
129     }
130 }