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 androidx.preference.Preference; 33 import androidx.preference.PreferenceManager; 34 import androidx.preference.PreferenceScreen; 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 import org.robolectric.annotation.Implementation; 45 import org.robolectric.annotation.Implements; 46 47 import java.util.ArrayList; 48 import java.util.List; 49 50 @RunWith(RobolectricTestRunner.class) 51 @Config(shadows = PaymentSettingsTest.ShadowPaymentBackend.class) 52 public class PaymentSettingsTest { 53 54 static final String PAYMENT_KEY = "nfc_payment"; 55 static final String FOREGROUND_KEY = "nfc_foreground"; 56 57 private Context mContext; 58 59 @Mock 60 private PackageManager mPackageManager; 61 62 @Mock 63 private UserManager mUserManager; 64 65 @Mock 66 private UserInfo mUserInfo; 67 68 @Before setUp()69 public void setUp() { 70 MockitoAnnotations.initMocks(this); 71 mContext = spy(RuntimeEnvironment.application); 72 when(mContext.getPackageManager()).thenReturn(mPackageManager); 73 doReturn(mUserManager).when(mContext).getSystemService(UserManager.class); 74 when(mUserManager.getUserInfo(UserHandle.myUserId())).thenReturn(mUserInfo); 75 } 76 77 @Test getNonIndexableKey_noNFC_allKeysAdded()78 public void getNonIndexableKey_noNFC_allKeysAdded() { 79 when(mPackageManager.hasSystemFeature(PackageManager.FEATURE_NFC)).thenReturn(false); 80 81 final List<String> niks = 82 PaymentSettings.SEARCH_INDEX_DATA_PROVIDER.getNonIndexableKeys(mContext); 83 84 assertThat(niks).contains(PAYMENT_KEY); 85 assertThat(niks).contains(FOREGROUND_KEY); 86 } 87 88 @Test getNonIndexableKey_NFC_foregroundKeyAdded()89 public void getNonIndexableKey_NFC_foregroundKeyAdded() { 90 when(mPackageManager.hasSystemFeature(PackageManager.FEATURE_NFC)).thenReturn(true); 91 92 final List<String> niks = 93 PaymentSettings.SEARCH_INDEX_DATA_PROVIDER.getNonIndexableKeys(mContext); 94 95 assertThat(niks).contains(FOREGROUND_KEY); 96 } 97 98 @Test getNonIndexableKey_primaryUser_returnsTrue()99 public void getNonIndexableKey_primaryUser_returnsTrue() { 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).containsExactly(FOREGROUND_KEY); 106 } 107 108 @Test getNonIndexableKey_guestUser_returnsFalse()109 public void getNonIndexableKey_guestUser_returnsFalse() { 110 when(mPackageManager.hasSystemFeature(PackageManager.FEATURE_NFC)).thenReturn(true); 111 when(mUserInfo.isGuest()).thenReturn(true); 112 113 final List<String> niks = 114 PaymentSettings.SEARCH_INDEX_DATA_PROVIDER.getNonIndexableKeys(mContext); 115 116 assertThat(niks).containsAllOf(FOREGROUND_KEY, PAYMENT_KEY); 117 } 118 119 @Test isShowEmptyImage_hasVisiblePreference_returnFalse()120 public void isShowEmptyImage_hasVisiblePreference_returnFalse() { 121 final PaymentSettings paymentSettings = new PaymentSettings(); 122 final PreferenceManager preferenceManager = new PreferenceManager(mContext); 123 final PreferenceScreen screen = preferenceManager.createPreferenceScreen(mContext); 124 final Preference preference1 = new Preference(mContext); 125 screen.addPreference(preference1); 126 final Preference preference2 = new Preference(mContext); 127 screen.addPreference(preference2); 128 129 assertThat(paymentSettings.isShowEmptyImage(screen)).isFalse(); 130 } 131 132 @Test isShowEmptyImage_hasNoVisiblePreference_returnTrue()133 public void isShowEmptyImage_hasNoVisiblePreference_returnTrue() { 134 final PaymentSettings paymentSettings = new PaymentSettings(); 135 final PreferenceManager preferenceManager = new PreferenceManager(mContext); 136 final PreferenceScreen screen = preferenceManager.createPreferenceScreen(mContext); 137 final Preference preference1 = new Preference(mContext); 138 preference1.setVisible(false); 139 screen.addPreference(preference1); 140 final Preference preference2 = new Preference(mContext); 141 screen.addPreference(preference2); 142 preference2.setVisible(false); 143 144 assertThat(paymentSettings.isShowEmptyImage(screen)).isTrue(); 145 } 146 147 @Implements(PaymentBackend.class) 148 public static class ShadowPaymentBackend { 149 private ArrayList<PaymentBackend.PaymentAppInfo> mAppInfos; 150 __constructor__(Context context)151 public void __constructor__(Context context) { 152 mAppInfos = new ArrayList<>(); 153 mAppInfos.add(new PaymentBackend.PaymentAppInfo()); 154 } 155 156 @Implementation getPaymentAppInfos()157 protected List<PaymentBackend.PaymentAppInfo> getPaymentAppInfos() { 158 return mAppInfos; 159 } 160 } 161 }