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 android.content.Context;
     22 import android.nfc.NfcAdapter;
     23 import android.provider.Settings;
     24 import android.support.v7.preference.Preference;
     25 import android.support.v14.preference.SwitchPreference;
     26 
     27 import com.android.settings.testutils.SettingsRobolectricTestRunner;
     28 import com.android.settings.testutils.shadow.ShadowNfcAdapter;
     29 
     30 import org.junit.Before;
     31 import org.junit.Test;
     32 import org.junit.runner.RunWith;
     33 import org.robolectric.RuntimeEnvironment;
     34 import org.robolectric.annotation.Config;
     35 import org.robolectric.shadows.ShadowApplication;
     36 import org.robolectric.util.ReflectionHelpers;
     37 
     38 @RunWith(SettingsRobolectricTestRunner.class)
     39 @Config(shadows = {ShadowNfcAdapter.class})
     40 public class NfcAirplaneModeObserverTest {
     41 
     42     Context mContext;
     43     private NfcAdapter mNfcAdapter;
     44     private SwitchPreference mNfcPreference;
     45     private NfcAirplaneModeObserver mNfcAirplaneModeObserver;
     46 
     47     @Before
     48     public void setUp() {
     49         mContext = ShadowApplication.getInstance().getApplicationContext();
     50         mNfcAdapter = NfcAdapter.getDefaultAdapter(mContext);
     51 
     52         mNfcPreference = new SwitchPreference(RuntimeEnvironment.application);
     53 
     54         mNfcAirplaneModeObserver = new NfcAirplaneModeObserver(mContext, mNfcAdapter,
     55                 (Preference) mNfcPreference);
     56     }
     57 
     58     @Test
     59     public void NfcAirplaneModeObserver_airplaneOn_shouldDisableNfc() {
     60         ReflectionHelpers.setField(mNfcAirplaneModeObserver,
     61                 "mAirplaneMode", 0);
     62         Settings.Global.putInt(mContext.getContentResolver(),
     63                 Settings.Global.AIRPLANE_MODE_ON, 1);
     64 
     65         mNfcAirplaneModeObserver.onChange(false,
     66                 NfcAirplaneModeObserver.AIRPLANE_MODE_URI);
     67 
     68         assertThat(mNfcAdapter.isEnabled()).isFalse();
     69         assertThat(mNfcPreference.isEnabled()).isFalse();
     70     }
     71 
     72     @Test
     73     public void NfcAirplaneModeObserver_airplaneOff_shouldEnableNfc() {
     74         ReflectionHelpers.setField(mNfcAirplaneModeObserver,
     75                 "mAirplaneMode", 1);
     76         Settings.Global.putInt(mContext.getContentResolver(),
     77                 Settings.Global.AIRPLANE_MODE_ON, 0);
     78 
     79         mNfcAirplaneModeObserver.onChange(false,
     80                 NfcAirplaneModeObserver.AIRPLANE_MODE_URI);
     81 
     82         assertThat(mNfcAdapter.isEnabled()).isTrue();
     83         assertThat(mNfcPreference.isEnabled()).isTrue();
     84     }
     85 }
     86