OSDN Git Service

Make Nfc/Beam searchable based on feature availability
[android-x86/packages-apps-Settings.git] / tests / robotests / src / com / android / settings / nfc / AndroidBeamPreferenceControllerTest.java
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
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         mAndroidBeamPreference = new RestrictedPreference(RuntimeEnvironment.application);
77         when(mScreen.findPreference(mAndroidBeamController.getPreferenceKey())).thenReturn(
78                 mAndroidBeamPreference);
79
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,
85                 0);
86         mAndroidBeamController.displayPreference(mScreen);
87     }
88
89     @Test
90     public void isAvailable_hasNfc_shouldReturnTrue() {
91         when(mNfcAdapter.isEnabled()).thenReturn(true);
92         assertThat(mAndroidBeamController.isAvailable()).isTrue();
93     }
94
95     @Test
96     public void isAvailable_noNfcAdapter_shouldReturnFalse() {
97         ReflectionHelpers.setField(mAndroidBeamController, "mNfcAdapter", null);
98         assertThat(mAndroidBeamController.isAvailable()).isFalse();
99     }
100
101     @Test
102     public void isBeamEnable_disAllowBeam_shouldReturnFalse() {
103         when(mNfcAdapter.getAdapterState()).thenReturn(NfcAdapter.STATE_OFF);
104
105         when(RestrictedLockUtils.hasBaseUserRestriction(mContext,
106                 UserManager.DISALLOW_OUTGOING_BEAM, UserHandle.myUserId())).thenReturn(true);
107         mAndroidBeamController.displayPreference(mScreen);
108
109         assertThat(mAndroidBeamPreference.isEnabled()).isFalse();
110     }
111
112     @Test
113     public void isBeamEnable_nfcStateOn_shouldReturnTrue() {
114         when(mNfcAdapter.getAdapterState()).thenReturn(NfcAdapter.STATE_ON);
115         try {
116             mAndroidBeamController.onResume();
117         } catch (NullPointerException e) {
118             // skip because it's just test
119             // it will meet NullPointerException in checkRestrictionAndSetDisabled
120         }
121         assertThat(mAndroidBeamPreference.isEnabled()).isTrue();
122     }
123
124     @Test
125     public void isBeamEnable_nfcStateNotOn_shouldReturnFalse() {
126         when(mNfcAdapter.getAdapterState()).thenReturn(NfcAdapter.STATE_OFF);
127         mAndroidBeamController.onResume();
128         assertThat(mAndroidBeamPreference.isEnabled()).isFalse();
129
130         when(mNfcAdapter.getAdapterState()).thenReturn(NfcAdapter.STATE_TURNING_ON);
131         mAndroidBeamController.onResume();
132         assertThat(mAndroidBeamPreference.isEnabled()).isFalse();
133
134         when(mNfcAdapter.getAdapterState()).thenReturn(NfcAdapter.STATE_TURNING_OFF);
135         mAndroidBeamController.onResume();
136         assertThat(mAndroidBeamPreference.isEnabled()).isFalse();
137     }
138
139     @Test
140     public void updateNonIndexableKeys_available_shouldNotUpdate() {
141         when(mNfcAdapter.isEnabled()).thenReturn(true);
142         final List<String> keys = new ArrayList<>();
143
144         mAndroidBeamController.updateNonIndexableKeys(keys);
145
146         assertThat(keys).isEmpty();
147     }
148
149     @Test
150     public void updateNonIndexableKeys_notAvailable_shouldUpdate() {
151         ReflectionHelpers.setField(mAndroidBeamController, "mNfcAdapter", null);
152         final List<String> keys = new ArrayList<>();
153
154         mAndroidBeamController.updateNonIndexableKeys(keys);
155
156         assertThat(keys).hasSize(1);
157     }
158 }