OSDN Git Service

Merge "Require unlocked work profile to change notification settings" into pi-dev
[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 @RunWith(SettingsRobolectricTestRunner.class)
45 public class AndroidBeamPreferenceControllerTest {
46
47     Context mContext;
48     @Mock
49     private NfcAdapter mNfcAdapter;
50     @Mock
51     NfcManager mManager;
52     @Mock
53     private UserManager mUserManager;
54     @Mock
55     private PreferenceScreen mScreen;
56
57     private RestrictedPreference mAndroidBeamPreference;
58     private AndroidBeamPreferenceController mAndroidBeamController;
59
60     @Before
61     public void setUp() {
62         MockitoAnnotations.initMocks(this);
63         mContext = spy(RuntimeEnvironment.application);
64
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);
71
72         mAndroidBeamController = new AndroidBeamPreferenceController(mContext);
73         mAndroidBeamPreference = new RestrictedPreference(RuntimeEnvironment.application);
74         when(mScreen.findPreference(mAndroidBeamController.getPreferenceKey())).thenReturn(
75                 mAndroidBeamPreference);
76
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,
82                 0);
83         mAndroidBeamController.displayPreference(mScreen);
84     }
85
86     @Test
87     public void isAvailable_hasNfc_shouldReturnTrue() {
88         when(mNfcAdapter.isEnabled()).thenReturn(true);
89         assertThat(mAndroidBeamController.isAvailable()).isTrue();
90     }
91
92     @Test
93     public void isAvailable_noNfcAdapter_shouldReturnFalse() {
94         ReflectionHelpers.setField(mAndroidBeamController, "mNfcAdapter", null);
95         assertThat(mAndroidBeamController.isAvailable()).isFalse();
96     }
97
98     @Test
99     public void isBeamEnable_disAllowBeam_shouldReturnFalse() {
100         when(mNfcAdapter.getAdapterState()).thenReturn(NfcAdapter.STATE_OFF);
101
102         when(RestrictedLockUtils.hasBaseUserRestriction(mContext,
103                 UserManager.DISALLOW_OUTGOING_BEAM, UserHandle.myUserId())).thenReturn(true);
104         mAndroidBeamController.displayPreference(mScreen);
105
106         assertThat(mAndroidBeamPreference.isEnabled()).isFalse();
107     }
108
109     @Test
110     public void isBeamEnable_nfcStateOn_shouldReturnTrue() {
111         when(mNfcAdapter.getAdapterState()).thenReturn(NfcAdapter.STATE_ON);
112         try {
113             mAndroidBeamController.onResume();
114         } catch (NullPointerException e) {
115             // skip because it's just test
116             // it will meet NullPointerException in checkRestrictionAndSetDisabled
117         }
118         assertThat(mAndroidBeamPreference.isEnabled()).isTrue();
119     }
120
121     @Test
122     public void isBeamEnable_nfcStateNotOn_shouldReturnFalse() {
123         when(mNfcAdapter.getAdapterState()).thenReturn(NfcAdapter.STATE_OFF);
124         mAndroidBeamController.onResume();
125         assertThat(mAndroidBeamPreference.isEnabled()).isFalse();
126
127         when(mNfcAdapter.getAdapterState()).thenReturn(NfcAdapter.STATE_TURNING_ON);
128         mAndroidBeamController.onResume();
129         assertThat(mAndroidBeamPreference.isEnabled()).isFalse();
130
131         when(mNfcAdapter.getAdapterState()).thenReturn(NfcAdapter.STATE_TURNING_OFF);
132         mAndroidBeamController.onResume();
133         assertThat(mAndroidBeamPreference.isEnabled()).isFalse();
134     }
135 }