Home | History | Annotate | Download | only in nfc
      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 import static org.mockito.Mockito.when;
     24 
     25 import android.content.Context;
     26 import android.nfc.NfcAdapter;
     27 import android.nfc.NfcManager;
     28 import android.os.UserManager;
     29 import android.provider.Settings;
     30 import android.support.v7.preference.PreferenceScreen;
     31 import android.support.v14.preference.SwitchPreference;
     32 
     33 import com.android.settings.testutils.SettingsRobolectricTestRunner;
     34 
     35 import org.junit.Before;
     36 import org.junit.Test;
     37 import org.junit.runner.RunWith;
     38 import org.mockito.Mock;
     39 import org.mockito.MockitoAnnotations;
     40 import org.robolectric.RuntimeEnvironment;
     41 import org.robolectric.util.ReflectionHelpers;
     42 
     43 import java.util.ArrayList;
     44 import java.util.List;
     45 
     46 @RunWith(SettingsRobolectricTestRunner.class)
     47 public class NfcPreferenceControllerTest {
     48 
     49     Context mContext;
     50     @Mock
     51     private NfcAdapter mNfcAdapter;
     52     @Mock
     53     NfcManager mManager;
     54     @Mock
     55     private UserManager mUserManager;
     56     @Mock
     57     private PreferenceScreen mScreen;
     58 
     59     private SwitchPreference mNfcPreference;
     60     private NfcPreferenceController mNfcController;
     61 
     62     @Before
     63     public void setUp() {
     64         MockitoAnnotations.initMocks(this);
     65         mContext = spy(RuntimeEnvironment.application);
     66 
     67         when(mContext.getApplicationContext()).thenReturn(mContext);
     68         when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager);
     69         when(mContext.getSystemService(Context.NFC_SERVICE)).thenReturn(mManager);
     70         when(NfcAdapter.getDefaultAdapter(mContext)).thenReturn(mNfcAdapter);
     71 
     72         mNfcController = new NfcPreferenceController(mContext,
     73                 NfcPreferenceController.KEY_TOGGLE_NFC);
     74         mNfcPreference = new SwitchPreference(RuntimeEnvironment.application);
     75 
     76         when(mScreen.findPreference(mNfcController.getPreferenceKey())).thenReturn(mNfcPreference);
     77 
     78         Settings.Global.putString(mContext.getContentResolver(),
     79                 Settings.Global.AIRPLANE_MODE_TOGGLEABLE_RADIOS,
     80                 Settings.Global.RADIO_NFC);
     81 
     82         Settings.Global.putInt(mContext.getContentResolver(),
     83                 Settings.Global.AIRPLANE_MODE_ON,
     84                 0);
     85         mNfcController.displayPreference(mScreen);
     86     }
     87 
     88     @Test
     89     public void getAvailabilityStatus_hasNfc_shouldReturnAvailable() {
     90         when(mNfcAdapter.isEnabled()).thenReturn(true);
     91         assertThat(mNfcController.getAvailabilityStatus())
     92                 .isEqualTo(NfcPreferenceController.AVAILABLE);
     93     }
     94 
     95     @Test
     96     public void getAvailabilityStatus_noNfcAdapter_shouldReturnDisabledUnsupported() {
     97         ReflectionHelpers.setField(mNfcController, "mNfcAdapter", null);
     98         assertThat(mNfcController.getAvailabilityStatus())
     99                 .isEqualTo(NfcPreferenceController.UNSUPPORTED_ON_DEVICE);
    100     }
    101 
    102     @Test
    103     public void isNfcEnable_nfcStateNotTurning_shouldReturnTrue() {
    104         when(mNfcAdapter.getAdapterState()).thenReturn(NfcAdapter.STATE_ON);
    105         mNfcController.onResume();
    106         assertThat(mNfcPreference.isEnabled()).isTrue();
    107 
    108         when(mNfcAdapter.getAdapterState()).thenReturn(NfcAdapter.STATE_OFF);
    109         mNfcController.onResume();
    110         assertThat(mNfcPreference.isEnabled()).isTrue();
    111     }
    112 
    113     @Test
    114     public void isNfcEnable_nfcStateTurning_shouldReturnFalse() {
    115         when(mNfcAdapter.getAdapterState()).thenReturn(NfcAdapter.STATE_TURNING_ON);
    116         mNfcController.onResume();
    117         assertThat(mNfcPreference.isEnabled()).isFalse();
    118 
    119         when(mNfcAdapter.getAdapterState()).thenReturn(NfcAdapter.STATE_TURNING_OFF);
    120         mNfcController.onResume();
    121         assertThat(mNfcPreference.isEnabled()).isFalse();
    122     }
    123 
    124     @Test
    125     public void isNfcChecked_nfcStateOn_shouldReturnTrue() {
    126         when(mNfcAdapter.getAdapterState()).thenReturn(NfcAdapter.STATE_ON);
    127         mNfcController.onResume();
    128         assertThat(mNfcPreference.isChecked()).isTrue();
    129 
    130         when(mNfcAdapter.getAdapterState()).thenReturn(NfcAdapter.STATE_TURNING_ON);
    131         mNfcController.onResume();
    132         assertThat(mNfcPreference.isChecked()).isTrue();
    133     }
    134 
    135     @Test
    136     public void isNfcChecked_nfcStateOff_shouldReturnFalse() {
    137         when(mNfcAdapter.getAdapterState()).thenReturn(NfcAdapter.STATE_OFF);
    138         mNfcController.onResume();
    139         assertThat(mNfcPreference.isChecked()).isFalse();
    140 
    141         when(mNfcAdapter.getAdapterState()).thenReturn(NfcAdapter.STATE_TURNING_OFF);
    142         mNfcController.onResume();
    143         assertThat(mNfcPreference.isChecked()).isFalse();
    144     }
    145 
    146     @Test
    147     public void updateNonIndexableKeys_available_shouldNotUpdate() {
    148         when(mNfcAdapter.isEnabled()).thenReturn(true);
    149         final List<String> keys = new ArrayList<>();
    150 
    151         mNfcController.updateNonIndexableKeys(keys);
    152 
    153         assertThat(keys).isEmpty();
    154     }
    155 
    156     @Test
    157     public void updateNonIndexableKeys_notAvailable_shouldUpdate() {
    158         ReflectionHelpers.setField(mNfcController, "mNfcAdapter", null);
    159         final List<String> keys = new ArrayList<>();
    160 
    161         mNfcController.updateNonIndexableKeys(keys);
    162 
    163         assertThat(keys).hasSize(1);
    164     }
    165     @Test
    166     public void setChecked_True_nfcShouldEnable() {
    167         mNfcController.setChecked(true);
    168         mNfcController.onResume();
    169 
    170         verify(mNfcAdapter).enable();
    171     }
    172 
    173     @Test
    174     public void setChecked_False_nfcShouldDisable() {
    175         mNfcController.setChecked(false);
    176         mNfcController.onResume();
    177 
    178         verify(mNfcAdapter).disable();
    179     }
    180 
    181     @Test
    182     public void hasAsyncUpdate_shouldReturnTrue() {
    183         assertThat(mNfcController.hasAsyncUpdate()).isTrue();
    184     }
    185 
    186     @Test
    187     public void isToggleableInAirplaneMode_containNfc_shouldReturnTrue() {
    188         Settings.Global.putString(mContext.getContentResolver(),
    189                 Settings.Global.AIRPLANE_MODE_TOGGLEABLE_RADIOS,
    190                 Settings.Global.RADIO_NFC);
    191         Settings.Global.putInt(mContext.getContentResolver(),
    192                 Settings.Global.AIRPLANE_MODE_ON, 1);
    193 
    194         assertThat(NfcPreferenceController.isToggleableInAirplaneMode(mContext)).isTrue();
    195     }
    196 
    197     @Test
    198     public void isToggleableInAirplaneMode_withoutNfc_shouldReturnFalse() {
    199         Settings.Global.putString(mContext.getContentResolver(),
    200                 Settings.Global.AIRPLANE_MODE_TOGGLEABLE_RADIOS,
    201                 "null");
    202         Settings.Global.putInt(mContext.getContentResolver(),
    203                 Settings.Global.AIRPLANE_MODE_ON, 1);
    204 
    205         assertThat(NfcPreferenceController.isToggleableInAirplaneMode(mContext)).isFalse();
    206     }
    207 }
    208