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 @RunWith(SettingsRobolectricTestRunner.class)
45 public class AndroidBeamPreferenceControllerTest {
49 private NfcAdapter mNfcAdapter;
53 private UserManager mUserManager;
55 private PreferenceScreen mScreen;
57 private RestrictedPreference mAndroidBeamPreference;
58 private AndroidBeamPreferenceController mAndroidBeamController;
62 MockitoAnnotations.initMocks(this);
63 mContext = spy(RuntimeEnvironment.application);
65 when(mContext.getApplicationContext()).thenReturn(mContext);
66 when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager);
67 when(mContext.getSystemService(Context.NFC_SERVICE)).thenReturn(mManager);
68 when(RestrictedLockUtils.hasBaseUserRestriction(mContext,
69 UserManager.DISALLOW_OUTGOING_BEAM, UserHandle.myUserId())).thenReturn(false);
70 when(NfcAdapter.getDefaultAdapter(mContext)).thenReturn(mNfcAdapter);
72 mAndroidBeamController = new AndroidBeamPreferenceController(mContext);
73 mAndroidBeamPreference = new RestrictedPreference(RuntimeEnvironment.application);
74 when(mScreen.findPreference(mAndroidBeamController.getPreferenceKey())).thenReturn(
75 mAndroidBeamPreference);
77 Settings.Global.putString(mContext.getContentResolver(),
78 Settings.Global.AIRPLANE_MODE_TOGGLEABLE_RADIOS,
79 Settings.Global.RADIO_NFC);
80 Settings.Global.putInt(mContext.getContentResolver(),
81 Settings.Global.AIRPLANE_MODE_ON,
83 mAndroidBeamController.displayPreference(mScreen);
87 public void isAvailable_hasNfc_shouldReturnTrue() {
88 when(mNfcAdapter.isEnabled()).thenReturn(true);
89 assertThat(mAndroidBeamController.isAvailable()).isTrue();
93 public void isAvailable_noNfcAdapter_shouldReturnFalse() {
94 ReflectionHelpers.setField(mAndroidBeamController, "mNfcAdapter", null);
95 assertThat(mAndroidBeamController.isAvailable()).isFalse();
99 public void isBeamEnable_disAllowBeam_shouldReturnFalse() {
100 when(mNfcAdapter.getAdapterState()).thenReturn(NfcAdapter.STATE_OFF);
102 when(RestrictedLockUtils.hasBaseUserRestriction(mContext,
103 UserManager.DISALLOW_OUTGOING_BEAM, UserHandle.myUserId())).thenReturn(true);
104 mAndroidBeamController.displayPreference(mScreen);
106 assertThat(mAndroidBeamPreference.isEnabled()).isFalse();
110 public void isBeamEnable_nfcStateOn_shouldReturnTrue() {
111 when(mNfcAdapter.getAdapterState()).thenReturn(NfcAdapter.STATE_ON);
113 mAndroidBeamController.onResume();
114 } catch (NullPointerException e) {
115 // skip because it's just test
116 // it will meet NullPointerException in checkRestrictionAndSetDisabled
118 assertThat(mAndroidBeamPreference.isEnabled()).isTrue();
122 public void isBeamEnable_nfcStateNotOn_shouldReturnFalse() {
123 when(mNfcAdapter.getAdapterState()).thenReturn(NfcAdapter.STATE_OFF);
124 mAndroidBeamController.onResume();
125 assertThat(mAndroidBeamPreference.isEnabled()).isFalse();
127 when(mNfcAdapter.getAdapterState()).thenReturn(NfcAdapter.STATE_TURNING_ON);
128 mAndroidBeamController.onResume();
129 assertThat(mAndroidBeamPreference.isEnabled()).isFalse();
131 when(mNfcAdapter.getAdapterState()).thenReturn(NfcAdapter.STATE_TURNING_OFF);
132 mAndroidBeamController.onResume();
133 assertThat(mAndroidBeamPreference.isEnabled()).isFalse();