• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.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;
31 
32 import com.android.settings.testutils.SettingsRobolectricTestRunner;
33 import com.android.settingslib.RestrictedLockUtils;
34 import com.android.settingslib.RestrictedPreference;
35 
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;
43 
44 import java.util.ArrayList;
45 import java.util.List;
46 
47 @RunWith(SettingsRobolectricTestRunner.class)
48 public class AndroidBeamPreferenceControllerTest {
49 
50     Context mContext;
51     @Mock
52     private NfcAdapter mNfcAdapter;
53     @Mock
54     NfcManager mManager;
55     @Mock
56     private UserManager mUserManager;
57     @Mock
58     private PreferenceScreen mScreen;
59 
60     private RestrictedPreference mAndroidBeamPreference;
61     private AndroidBeamPreferenceController mAndroidBeamController;
62 
63     @Before
setUp()64     public void setUp() {
65         MockitoAnnotations.initMocks(this);
66         mContext = spy(RuntimeEnvironment.application);
67 
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);
74 
75         mAndroidBeamController = new AndroidBeamPreferenceController(mContext,
76                 AndroidBeamPreferenceController.KEY_ANDROID_BEAM_SETTINGS);
77         mAndroidBeamPreference = new RestrictedPreference(RuntimeEnvironment.application);
78         when(mScreen.findPreference(mAndroidBeamController.getPreferenceKey())).thenReturn(
79                 mAndroidBeamPreference);
80 
81         Settings.Global.putString(mContext.getContentResolver(),
82                 Settings.Global.AIRPLANE_MODE_TOGGLEABLE_RADIOS,
83                 Settings.Global.RADIO_NFC);
84         Settings.Global.putInt(mContext.getContentResolver(),
85                 Settings.Global.AIRPLANE_MODE_ON,
86                 0);
87         mAndroidBeamController.displayPreference(mScreen);
88     }
89 
90     @Test
isAvailable_hasNfc_shouldReturnTrue()91     public void isAvailable_hasNfc_shouldReturnTrue() {
92         when(mNfcAdapter.isEnabled()).thenReturn(true);
93         assertThat(mAndroidBeamController.isAvailable()).isTrue();
94     }
95 
96     @Test
isAvailable_noNfcAdapter_shouldReturnFalse()97     public void isAvailable_noNfcAdapter_shouldReturnFalse() {
98         ReflectionHelpers.setField(mAndroidBeamController, "mNfcAdapter", null);
99         assertThat(mAndroidBeamController.isAvailable()).isFalse();
100     }
101 
102     @Test
isBeamEnable_disAllowBeam_shouldReturnFalse()103     public void isBeamEnable_disAllowBeam_shouldReturnFalse() {
104         when(mNfcAdapter.getAdapterState()).thenReturn(NfcAdapter.STATE_OFF);
105 
106         when(RestrictedLockUtils.hasBaseUserRestriction(mContext,
107                 UserManager.DISALLOW_OUTGOING_BEAM, UserHandle.myUserId())).thenReturn(true);
108         mAndroidBeamController.displayPreference(mScreen);
109 
110         assertThat(mAndroidBeamPreference.isEnabled()).isFalse();
111     }
112 
113     @Test
isBeamEnable_nfcStateOn_shouldReturnTrue()114     public void isBeamEnable_nfcStateOn_shouldReturnTrue() {
115         when(mNfcAdapter.getAdapterState()).thenReturn(NfcAdapter.STATE_ON);
116         try {
117             mAndroidBeamController.onResume();
118         } catch (NullPointerException e) {
119             // skip because it's just test
120             // it will meet NullPointerException in checkRestrictionAndSetDisabled
121         }
122         assertThat(mAndroidBeamPreference.isEnabled()).isTrue();
123     }
124 
125     @Test
isBeamEnable_nfcStateNotOn_shouldReturnFalse()126     public void isBeamEnable_nfcStateNotOn_shouldReturnFalse() {
127         when(mNfcAdapter.getAdapterState()).thenReturn(NfcAdapter.STATE_OFF);
128         mAndroidBeamController.onResume();
129         assertThat(mAndroidBeamPreference.isEnabled()).isFalse();
130 
131         when(mNfcAdapter.getAdapterState()).thenReturn(NfcAdapter.STATE_TURNING_ON);
132         mAndroidBeamController.onResume();
133         assertThat(mAndroidBeamPreference.isEnabled()).isFalse();
134 
135         when(mNfcAdapter.getAdapterState()).thenReturn(NfcAdapter.STATE_TURNING_OFF);
136         mAndroidBeamController.onResume();
137         assertThat(mAndroidBeamPreference.isEnabled()).isFalse();
138     }
139 
140     @Test
updateNonIndexableKeys_available_shouldNotUpdate()141     public void updateNonIndexableKeys_available_shouldNotUpdate() {
142         when(mNfcAdapter.isEnabled()).thenReturn(true);
143         final List<String> keys = new ArrayList<>();
144 
145         mAndroidBeamController.updateNonIndexableKeys(keys);
146 
147         assertThat(keys).isEmpty();
148     }
149 
150     @Test
updateNonIndexableKeys_notAvailable_shouldUpdate()151     public void updateNonIndexableKeys_notAvailable_shouldUpdate() {
152         ReflectionHelpers.setField(mAndroidBeamController, "mNfcAdapter", null);
153         final List<String> keys = new ArrayList<>();
154 
155         mAndroidBeamController.updateNonIndexableKeys(keys);
156 
157         assertThat(keys).hasSize(1);
158     }
159 }
160