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 21 import static org.mockito.Mockito.spy; 22 import static org.mockito.Mockito.when; 23 24 import android.content.Context; 25 import android.content.pm.PackageManager; 26 import android.nfc.NfcAdapter; 27 import android.nfc.NfcManager; 28 import android.os.UserHandle; 29 import android.os.UserManager; 30 import android.provider.Settings; 31 32 import androidx.preference.PreferenceScreen; 33 34 import com.android.settingslib.RestrictedLockUtilsInternal; 35 import com.android.settingslib.RestrictedPreference; 36 37 import org.junit.Before; 38 import org.junit.Test; 39 import org.junit.runner.RunWith; 40 import org.mockito.Mock; 41 import org.mockito.MockitoAnnotations; 42 import org.robolectric.RobolectricTestRunner; 43 import org.robolectric.RuntimeEnvironment; 44 import org.robolectric.util.ReflectionHelpers; 45 46 import java.util.ArrayList; 47 import java.util.List; 48 49 @RunWith(RobolectricTestRunner.class) 50 public class AndroidBeamPreferenceControllerTest { 51 52 Context mContext; 53 @Mock 54 private NfcAdapter mNfcAdapter; 55 @Mock 56 NfcManager mManager; 57 @Mock 58 private UserManager mUserManager; 59 @Mock 60 private PreferenceScreen mScreen; 61 @Mock 62 private PackageManager mPackageManager; 63 64 private RestrictedPreference mAndroidBeamPreference; 65 private AndroidBeamPreferenceController mAndroidBeamController; 66 67 @Before setUp()68 public void setUp() { 69 MockitoAnnotations.initMocks(this); 70 mContext = spy(RuntimeEnvironment.application); 71 72 when(mContext.getApplicationContext()).thenReturn(mContext); 73 when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager); 74 when(mContext.getSystemService(Context.NFC_SERVICE)).thenReturn(mManager); 75 when(RestrictedLockUtilsInternal.hasBaseUserRestriction(mContext, 76 UserManager.DISALLOW_OUTGOING_BEAM, UserHandle.myUserId())).thenReturn(false); 77 when(NfcAdapter.getDefaultAdapter(mContext)).thenReturn(mNfcAdapter); 78 79 mAndroidBeamController = new AndroidBeamPreferenceController(mContext, 80 AndroidBeamPreferenceController.KEY_ANDROID_BEAM_SETTINGS); 81 mAndroidBeamPreference = new RestrictedPreference(RuntimeEnvironment.application); 82 when(mScreen.findPreference(mAndroidBeamController.getPreferenceKey())).thenReturn( 83 mAndroidBeamPreference); 84 when(mContext.getPackageManager()).thenReturn(mPackageManager); 85 when(mPackageManager.hasSystemFeature(PackageManager.FEATURE_NFC_BEAM)).thenReturn(true); 86 87 Settings.Global.putString(mContext.getContentResolver(), 88 Settings.Global.AIRPLANE_MODE_TOGGLEABLE_RADIOS, 89 Settings.Global.RADIO_NFC); 90 Settings.Global.putInt(mContext.getContentResolver(), 91 Settings.Global.AIRPLANE_MODE_ON, 92 0); 93 mAndroidBeamController.displayPreference(mScreen); 94 } 95 96 @Test isAvailable_hasNfc_shouldReturnTrue()97 public void isAvailable_hasNfc_shouldReturnTrue() { 98 when(mNfcAdapter.isEnabled()).thenReturn(true); 99 assertThat(mAndroidBeamController.isAvailable()).isTrue(); 100 } 101 102 @Test isAvailable_noNfcFeature_shouldReturnFalse()103 public void isAvailable_noNfcFeature_shouldReturnFalse() { 104 when(mNfcAdapter.isEnabled()).thenReturn(true); 105 when(mPackageManager.hasSystemFeature(PackageManager.FEATURE_NFC_BEAM)).thenReturn(false); 106 assertThat(mAndroidBeamController.isAvailable()).isFalse(); 107 } 108 109 @Test isAvailable_noNfcAdapter_shouldReturnFalse()110 public void isAvailable_noNfcAdapter_shouldReturnFalse() { 111 ReflectionHelpers.setField(mAndroidBeamController, "mNfcAdapter", null); 112 assertThat(mAndroidBeamController.isAvailable()).isFalse(); 113 } 114 115 @Test isBeamEnable_disAllowBeam_shouldReturnFalse()116 public void isBeamEnable_disAllowBeam_shouldReturnFalse() { 117 when(mNfcAdapter.getAdapterState()).thenReturn(NfcAdapter.STATE_OFF); 118 119 when(RestrictedLockUtilsInternal.hasBaseUserRestriction(mContext, 120 UserManager.DISALLOW_OUTGOING_BEAM, UserHandle.myUserId())).thenReturn(true); 121 mAndroidBeamController.displayPreference(mScreen); 122 123 assertThat(mAndroidBeamPreference.isEnabled()).isFalse(); 124 } 125 126 @Test isBeamEnable_nfcStateOn_shouldReturnTrue()127 public void isBeamEnable_nfcStateOn_shouldReturnTrue() { 128 when(mNfcAdapter.getAdapterState()).thenReturn(NfcAdapter.STATE_ON); 129 try { 130 mAndroidBeamController.onResume(); 131 } catch (NullPointerException e) { 132 // skip because it's just test 133 // it will meet NullPointerException in checkRestrictionAndSetDisabled 134 } 135 assertThat(mAndroidBeamPreference.isEnabled()).isTrue(); 136 } 137 138 @Test isBeamEnable_nfcStateNotOn_shouldReturnFalse()139 public void isBeamEnable_nfcStateNotOn_shouldReturnFalse() { 140 when(mNfcAdapter.getAdapterState()).thenReturn(NfcAdapter.STATE_OFF); 141 mAndroidBeamController.onResume(); 142 assertThat(mAndroidBeamPreference.isEnabled()).isFalse(); 143 144 when(mNfcAdapter.getAdapterState()).thenReturn(NfcAdapter.STATE_TURNING_ON); 145 mAndroidBeamController.onResume(); 146 assertThat(mAndroidBeamPreference.isEnabled()).isFalse(); 147 148 when(mNfcAdapter.getAdapterState()).thenReturn(NfcAdapter.STATE_TURNING_OFF); 149 mAndroidBeamController.onResume(); 150 assertThat(mAndroidBeamPreference.isEnabled()).isFalse(); 151 } 152 153 @Test updateNonIndexableKeys_available_shouldNotUpdate()154 public void updateNonIndexableKeys_available_shouldNotUpdate() { 155 when(mNfcAdapter.isEnabled()).thenReturn(true); 156 final List<String> keys = new ArrayList<>(); 157 158 mAndroidBeamController.updateNonIndexableKeys(keys); 159 160 assertThat(keys).isEmpty(); 161 } 162 163 @Test updateNonIndexableKeys_notAvailable_shouldUpdate()164 public void updateNonIndexableKeys_notAvailable_shouldUpdate() { 165 ReflectionHelpers.setField(mAndroidBeamController, "mNfcAdapter", null); 166 final List<String> keys = new ArrayList<>(); 167 168 mAndroidBeamController.updateNonIndexableKeys(keys); 169 170 assertThat(keys).hasSize(1); 171 } 172 } 173