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 import static org.mockito.Mockito.mock; 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.support.v7.preference.PreferenceManager; 27 import android.support.v7.preference.PreferenceScreen; 28 29 import com.android.settings.R; 30 import com.android.settings.testutils.SettingsRobolectricTestRunner; 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.RuntimeEnvironment; 38 39 @RunWith(SettingsRobolectricTestRunner.class) 40 public class NfcForegroundPreferenceTest { 41 @Mock 42 private PaymentBackend mPaymentBackend; 43 44 private Context mContext; 45 private PreferenceScreen mScreen; 46 private NfcForegroundPreference mPreference; 47 48 @Before 49 public void setUp() { 50 MockitoAnnotations.initMocks(this); 51 mContext = RuntimeEnvironment.application; 52 mScreen = spy(new PreferenceScreen(mContext, null)); 53 when(mScreen.getPreferenceManager()).thenReturn(mock(PreferenceManager.class)); 54 when(mPaymentBackend.isForegroundMode()).thenReturn(false); 55 mPreference = new NfcForegroundPreference(mContext, mPaymentBackend); 56 mScreen.addPreference(mPreference); 57 } 58 59 @Test 60 public void testTogglingMode() { 61 String nfc_payment_favor_default = mContext.getString(R.string.nfc_payment_favor_default); 62 String nfc_payment_favor_open = mContext.getString(R.string.nfc_payment_favor_open); 63 64 assertThat(mPreference.getEntry()).isEqualTo(nfc_payment_favor_default); 65 assertThat(mPreference.getSummary()).isEqualTo(nfc_payment_favor_default); 66 67 mPreference.setValueIndex(0); 68 mPreference.callChangeListener(mPreference.getEntryValues()[0]); 69 verify(mPaymentBackend).setForegroundMode(true); 70 assertThat(mPreference.getEntry()).isEqualTo(nfc_payment_favor_open); 71 assertThat(mPreference.getSummary()).isEqualTo(nfc_payment_favor_open); 72 73 mPreference.setValueIndex(1); 74 mPreference.callChangeListener(mPreference.getEntryValues()[1]); 75 verify(mPaymentBackend).setForegroundMode(false); 76 assertThat(mPreference.getEntry()).isEqualTo(nfc_payment_favor_default); 77 assertThat(mPreference.getSummary()).isEqualTo(nfc_payment_favor_default); 78 } 79 } 80