2 * Copyright (C) 2018 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.settings.nfc;
19 import static com.google.common.truth.Truth.assertThat;
21 import static org.mockito.Mockito.spy;
22 import static org.mockito.Mockito.when;
24 import android.content.Context;
25 import android.nfc.NfcAdapter;
26 import android.nfc.NfcManager;
27 import android.os.UserHandle;
28 import android.os.UserManager;
29 import android.provider.Settings;
30 import android.support.v7.preference.PreferenceScreen;
32 import com.android.settings.testutils.SettingsRobolectricTestRunner;
33 import com.android.settingslib.RestrictedLockUtils;
34 import com.android.settingslib.RestrictedPreference;
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.RuntimeEnvironment;
42 import org.robolectric.util.ReflectionHelpers;
44 import java.util.ArrayList;
45 import java.util.List;
47 @RunWith(SettingsRobolectricTestRunner.class)
48 public class AndroidBeamPreferenceControllerTest {
52 private NfcAdapter mNfcAdapter;
56 private UserManager mUserManager;
58 private PreferenceScreen mScreen;
60 private RestrictedPreference mAndroidBeamPreference;
61 private AndroidBeamPreferenceController mAndroidBeamController;
65 MockitoAnnotations.initMocks(this);
66 mContext = spy(RuntimeEnvironment.application);
68 when(mContext.getApplicationContext()).thenReturn(mContext);
69 when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager);
70 when(mContext.getSystemService(Context.NFC_SERVICE)).thenReturn(mManager);
71 when(RestrictedLockUtils.hasBaseUserRestriction(mContext,
72 UserManager.DISALLOW_OUTGOING_BEAM, UserHandle.myUserId())).thenReturn(false);
73 when(NfcAdapter.getDefaultAdapter(mContext)).thenReturn(mNfcAdapter);
75 mAndroidBeamController = new AndroidBeamPreferenceController(mContext);
76 mAndroidBeamPreference = new RestrictedPreference(RuntimeEnvironment.application);
77 when(mScreen.findPreference(mAndroidBeamController.getPreferenceKey())).thenReturn(
78 mAndroidBeamPreference);
80 Settings.Global.putString(mContext.getContentResolver(),
81 Settings.Global.AIRPLANE_MODE_TOGGLEABLE_RADIOS,
82 Settings.Global.RADIO_NFC);
83 Settings.Global.putInt(mContext.getContentResolver(),
84 Settings.Global.AIRPLANE_MODE_ON,
86 mAndroidBeamController.displayPreference(mScreen);
90 public void isAvailable_hasNfc_shouldReturnTrue() {
91 when(mNfcAdapter.isEnabled()).thenReturn(true);
92 assertThat(mAndroidBeamController.isAvailable()).isTrue();
96 public void isAvailable_noNfcAdapter_shouldReturnFalse() {
97 ReflectionHelpers.setField(mAndroidBeamController, "mNfcAdapter", null);
98 assertThat(mAndroidBeamController.isAvailable()).isFalse();
102 public void isBeamEnable_disAllowBeam_shouldReturnFalse() {
103 when(mNfcAdapter.getAdapterState()).thenReturn(NfcAdapter.STATE_OFF);
105 when(RestrictedLockUtils.hasBaseUserRestriction(mContext,
106 UserManager.DISALLOW_OUTGOING_BEAM, UserHandle.myUserId())).thenReturn(true);
107 mAndroidBeamController.displayPreference(mScreen);
109 assertThat(mAndroidBeamPreference.isEnabled()).isFalse();
113 public void isBeamEnable_nfcStateOn_shouldReturnTrue() {
114 when(mNfcAdapter.getAdapterState()).thenReturn(NfcAdapter.STATE_ON);
116 mAndroidBeamController.onResume();
117 } catch (NullPointerException e) {
118 // skip because it's just test
119 // it will meet NullPointerException in checkRestrictionAndSetDisabled
121 assertThat(mAndroidBeamPreference.isEnabled()).isTrue();
125 public void isBeamEnable_nfcStateNotOn_shouldReturnFalse() {
126 when(mNfcAdapter.getAdapterState()).thenReturn(NfcAdapter.STATE_OFF);
127 mAndroidBeamController.onResume();
128 assertThat(mAndroidBeamPreference.isEnabled()).isFalse();
130 when(mNfcAdapter.getAdapterState()).thenReturn(NfcAdapter.STATE_TURNING_ON);
131 mAndroidBeamController.onResume();
132 assertThat(mAndroidBeamPreference.isEnabled()).isFalse();
134 when(mNfcAdapter.getAdapterState()).thenReturn(NfcAdapter.STATE_TURNING_OFF);
135 mAndroidBeamController.onResume();
136 assertThat(mAndroidBeamPreference.isEnabled()).isFalse();
140 public void updateNonIndexableKeys_available_shouldNotUpdate() {
141 when(mNfcAdapter.isEnabled()).thenReturn(true);
142 final List<String> keys = new ArrayList<>();
144 mAndroidBeamController.updateNonIndexableKeys(keys);
146 assertThat(keys).isEmpty();
150 public void updateNonIndexableKeys_notAvailable_shouldUpdate() {
151 ReflectionHelpers.setField(mAndroidBeamController, "mNfcAdapter", null);
152 final List<String> keys = new ArrayList<>();
154 mAndroidBeamController.updateNonIndexableKeys(keys);
156 assertThat(keys).hasSize(1);