1 /* 2 * Copyright (C) 2021 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.display; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import static org.mockito.ArgumentMatchers.anyBoolean; 22 import static org.mockito.ArgumentMatchers.anyInt; 23 import static org.mockito.Mockito.atLeastOnce; 24 import static org.mockito.Mockito.verify; 25 import static org.mockito.Mockito.when; 26 27 import android.content.ContentResolver; 28 import android.content.Context; 29 import android.provider.Settings; 30 import android.service.quickaccesswallet.QuickAccessWalletClient; 31 32 import androidx.preference.Preference; 33 34 import com.android.internal.widget.LockPatternUtils; 35 import com.android.settings.R; 36 import com.android.settings.core.BasePreferenceController; 37 import com.android.settings.testutils.FakeFeatureFactory; 38 39 import org.junit.Before; 40 import org.junit.Test; 41 import org.junit.runner.RunWith; 42 import org.mockito.Mock; 43 import org.mockito.MockitoAnnotations; 44 import org.robolectric.RobolectricTestRunner; 45 import org.robolectric.RuntimeEnvironment; 46 47 @RunWith(RobolectricTestRunner.class) 48 public class WalletPrivacyPreferenceControllerTest { 49 50 private static final String TEST_KEY = "test_key"; 51 private static final String SETTING_KEY = Settings.Secure.LOCKSCREEN_SHOW_WALLET; 52 53 private Context mContext; 54 private ContentResolver mContentResolver; 55 private WalletPrivacyPreferenceController mController; 56 57 @Mock 58 private Preference mPreference; 59 @Mock 60 private LockPatternUtils mLockPatternUtils; 61 @Mock 62 private QuickAccessWalletClient mClient; 63 64 @Before setUp()65 public void setUp() { 66 MockitoAnnotations.initMocks(this); 67 mContext = RuntimeEnvironment.application; 68 69 mContentResolver = mContext.getContentResolver(); 70 FakeFeatureFactory featureFactory = FakeFeatureFactory.setupForTest(); 71 when(featureFactory.securityFeatureProvider.getLockPatternUtils(mContext)) 72 .thenReturn(mLockPatternUtils); 73 when(mLockPatternUtils.isSecure(anyInt())).thenReturn(true); 74 75 when(mClient.isWalletServiceAvailable()).thenReturn(true); 76 mController = new WalletPrivacyPreferenceController(mContext, TEST_KEY) { 77 @Override 78 QuickAccessWalletClient initWalletClient() { 79 return mClient; 80 } 81 }; 82 } 83 84 @Test isChecked_SettingIs1_returnTrue()85 public void isChecked_SettingIs1_returnTrue() { 86 Settings.Secure.putInt(mContentResolver, SETTING_KEY, 1); 87 88 assertThat(mController.isChecked()).isTrue(); 89 } 90 91 @Test isChecked_SettingIs0_returnFalse()92 public void isChecked_SettingIs0_returnFalse() { 93 Settings.Secure.putInt(mContentResolver, SETTING_KEY, 0); 94 95 assertThat(mController.isChecked()).isFalse(); 96 } 97 98 @Test isChecked_SettingIsNotSet_returnFalse()99 public void isChecked_SettingIsNotSet_returnFalse() { 100 Settings.Secure.putString(mContentResolver, SETTING_KEY, null); 101 102 assertThat(mController.isChecked()).isFalse(); 103 } 104 105 @Test setChecked_true_SettingIsNot0()106 public void setChecked_true_SettingIsNot0() { 107 mController.setChecked(true); 108 109 assertThat(Settings.Secure.getInt(mContentResolver, SETTING_KEY, 0)).isNotEqualTo(0); 110 } 111 112 @Test setChecked_false_SettingIs0()113 public void setChecked_false_SettingIs0() { 114 mController.setChecked(false); 115 116 assertThat(Settings.Secure.getInt(mContentResolver, SETTING_KEY, 0)).isEqualTo(0); 117 } 118 119 @Test getSummary_notSecureLock_lockscreen_privacy_not_secureString()120 public void getSummary_notSecureLock_lockscreen_privacy_not_secureString() { 121 when(mLockPatternUtils.isSecure(anyInt())).thenReturn(false); 122 123 assertThat(mController.getSummary()).isEqualTo( 124 mContext.getText(R.string.lockscreen_privacy_not_secure)); 125 } 126 127 @Test getSummary_isSecure_lockscreen_privacy_showString()128 public void getSummary_isSecure_lockscreen_privacy_showString() { 129 when(mLockPatternUtils.isSecure(anyInt())).thenReturn(true); 130 131 assertThat(mController.getSummary()).isEqualTo( 132 mContext.getText(R.string.lockscreen_privacy_wallet_summary)); 133 } 134 135 @Test updateState_onPreferenceRefreshed_preferenceEnabledAndSummaryChanged()136 public void updateState_onPreferenceRefreshed_preferenceEnabledAndSummaryChanged() { 137 mController.updateState(mPreference); 138 139 verify(mPreference).setEnabled(anyBoolean()); 140 verify(mPreference, atLeastOnce()).setSummary(mController.getSummary()); 141 } 142 143 @Test getAvailabilityStatus_noService_returnsDisabled()144 public void getAvailabilityStatus_noService_returnsDisabled() { 145 when(mClient.isWalletServiceAvailable()).thenReturn(false); 146 when(mLockPatternUtils.isSecure(anyInt())).thenReturn(true); 147 148 assertThat(mController.getAvailabilityStatus()).isEqualTo( 149 BasePreferenceController.DISABLED_DEPENDENT_SETTING); 150 } 151 } 152