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