• 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.verify;
23 
24 import android.content.ContentResolver;
25 import android.content.Context;
26 import android.nfc.NfcAdapter;
27 import android.provider.Settings;
28 
29 import androidx.preference.SwitchPreference;
30 
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 import org.mockito.Mock;
35 import org.mockito.MockitoAnnotations;
36 import org.robolectric.RobolectricTestRunner;
37 import org.robolectric.RuntimeEnvironment;
38 
39 @RunWith(RobolectricTestRunner.class)
40 public class NfcEnablerTest {
41 
42     @Mock
43     private SwitchPreference mNfcPreference;
44 
45     private Context mContext;
46     private NfcEnabler mNfcEnabler;
47 
48     @Before
setUp()49     public void setUp() {
50         MockitoAnnotations.initMocks(this);
51         mContext = RuntimeEnvironment.application;
52         mNfcEnabler = spy(new NfcEnabler(mContext, mNfcPreference));
53     }
54 
55     @Test
isToggleable_AirplaneModeOff_shouldReturnTrue()56     public void isToggleable_AirplaneModeOff_shouldReturnTrue() {
57         final ContentResolver contentResolver = mContext.getContentResolver();
58         Settings.Global.putInt(contentResolver, Settings.Global.AIRPLANE_MODE_ON, 0);
59         Settings.Global.putString(contentResolver,
60             Settings.Global.AIRPLANE_MODE_RADIOS, Settings.Global.RADIO_NFC);
61         Settings.Global.putString(contentResolver,
62             Settings.Global.AIRPLANE_MODE_TOGGLEABLE_RADIOS, Settings.Global.RADIO_NFC);
63 
64         assertThat(mNfcEnabler.isToggleable()).isTrue();
65     }
66 
67     @Test
isToggleable_AirplaneModeOnNfcNotInAirplaneModeRadio_shouldReturnTrue()68     public void isToggleable_AirplaneModeOnNfcNotInAirplaneModeRadio_shouldReturnTrue() {
69         final ContentResolver contentResolver = mContext.getContentResolver();
70         Settings.Global.putInt(contentResolver, Settings.Global.AIRPLANE_MODE_ON, 1);
71         Settings.Global.putString(contentResolver, Settings.Global.AIRPLANE_MODE_RADIOS, "");
72 
73         assertThat(mNfcEnabler.isToggleable()).isTrue();
74     }
75 
76     @Test
isToggleable_AirplaneModeOnNfcToggleable_shouldReturnTrue()77     public void isToggleable_AirplaneModeOnNfcToggleable_shouldReturnTrue() {
78         final ContentResolver contentResolver = mContext.getContentResolver();
79         Settings.Global.putInt(contentResolver, Settings.Global.AIRPLANE_MODE_ON, 1);
80         Settings.Global.putString(contentResolver,
81             Settings.Global.AIRPLANE_MODE_RADIOS, Settings.Global.RADIO_NFC);
82         Settings.Global.putString(contentResolver,
83             Settings.Global.AIRPLANE_MODE_TOGGLEABLE_RADIOS, Settings.Global.RADIO_NFC);
84 
85         assertThat(mNfcEnabler.isToggleable()).isTrue();
86     }
87 
88     @Test
isToggleable_AirplaneModeOnNfcNotToggleable_shouldReturnFalse()89     public void isToggleable_AirplaneModeOnNfcNotToggleable_shouldReturnFalse() {
90         final ContentResolver contentResolver = mContext.getContentResolver();
91         Settings.Global.putInt(contentResolver, Settings.Global.AIRPLANE_MODE_ON, 1);
92         Settings.Global.putString(contentResolver,
93             Settings.Global.AIRPLANE_MODE_RADIOS, Settings.Global.RADIO_NFC);
94         Settings.Global.putString(contentResolver,
95                 Settings.Global.AIRPLANE_MODE_TOGGLEABLE_RADIOS, "");
96 
97         assertThat(mNfcEnabler.isToggleable()).isFalse();
98     }
99 
100     @Test
handleNfcStateChanged_stateOff_shouldCheckIfPreferenceEnableState()101     public void handleNfcStateChanged_stateOff_shouldCheckIfPreferenceEnableState() {
102         mNfcEnabler.handleNfcStateChanged(NfcAdapter.STATE_OFF);
103 
104         verify(mNfcEnabler).isToggleable();
105     }
106 }
107