/* * Copyright (C) 2017 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ package com.android.settings.nfc; import static com.google.common.truth.Truth.assertThat; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.when; import android.content.Context; import android.content.pm.PackageManager; import android.content.pm.UserInfo; import android.os.UserHandle; import android.os.UserManager; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.MockitoAnnotations; import org.robolectric.RobolectricTestRunner; import org.robolectric.RuntimeEnvironment; import org.robolectric.annotation.Config; import org.robolectric.annotation.Implementation; import org.robolectric.annotation.Implements; import java.util.ArrayList; import java.util.List; @RunWith(RobolectricTestRunner.class) @Config(shadows = PaymentSettingsTest.ShadowPaymentBackend.class) public class PaymentSettingsTest { static final String PAYMENT_KEY = "nfc_payment"; static final String FOREGROUND_KEY = "nfc_foreground"; static final String PAYMENT_SCREEN_KEY = "nfc_payment_settings_screen"; private Context mContext; @Mock private PackageManager mPackageManager; @Mock private UserManager mUserManager; @Mock private UserInfo mUserInfo; @Before public void setUp() { MockitoAnnotations.initMocks(this); mContext = spy(RuntimeEnvironment.application); when(mContext.getPackageManager()).thenReturn(mPackageManager); doReturn(mUserManager).when(mContext).getSystemService(UserManager.class); when(mUserManager.getUserInfo(UserHandle.myUserId())).thenReturn(mUserInfo); } @Test public void getNonIndexableKey_noNFC_allKeysAdded() { when(mPackageManager.hasSystemFeature(PackageManager.FEATURE_NFC)).thenReturn(false); final List niks = PaymentSettings.SEARCH_INDEX_DATA_PROVIDER.getNonIndexableKeys(mContext); assertThat(niks).contains(PAYMENT_KEY); assertThat(niks).contains(FOREGROUND_KEY); } @Test public void getNonIndexableKey_NFC_foregroundKeyAdded() { when(mPackageManager.hasSystemFeature(PackageManager.FEATURE_NFC)).thenReturn(true); final List niks = PaymentSettings.SEARCH_INDEX_DATA_PROVIDER.getNonIndexableKeys(mContext); assertThat(niks).contains(FOREGROUND_KEY); } @Test public void getNonIndexableKey_primaryUser_returnsTrue() { when(mPackageManager.hasSystemFeature(PackageManager.FEATURE_NFC)).thenReturn(true); final List niks = PaymentSettings.SEARCH_INDEX_DATA_PROVIDER.getNonIndexableKeys(mContext); assertThat(niks).containsExactly(FOREGROUND_KEY); } @Test public void getNonIndexabkeKey_guestUser_returnsFalse() { when(mPackageManager.hasSystemFeature(PackageManager.FEATURE_NFC)).thenReturn(true); when(mUserInfo.isGuest()).thenReturn(true); final List niks = PaymentSettings.SEARCH_INDEX_DATA_PROVIDER.getNonIndexableKeys(mContext); assertThat(niks).containsAllOf(FOREGROUND_KEY, PAYMENT_KEY, PAYMENT_SCREEN_KEY); } @Implements(PaymentBackend.class) public static class ShadowPaymentBackend { private ArrayList mAppInfos; public void __constructor__(Context context) { mAppInfos = new ArrayList<>(); mAppInfos.add(new PaymentBackend.PaymentAppInfo()); } @Implementation protected List getPaymentAppInfos() { return mAppInfos; } } }