1 /* 2 * Copyright (C) 2019 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; 18 19 import static org.mockito.Mockito.times; 20 import static org.mockito.Mockito.verify; 21 22 import android.content.Context; 23 import android.telephony.TelephonyManager; 24 25 import androidx.test.ext.junit.runners.AndroidJUnit4; 26 27 import org.junit.Before; 28 import org.junit.Test; 29 import org.junit.runner.RunWith; 30 import org.mockito.Mock; 31 import org.mockito.MockitoAnnotations; 32 import org.robolectric.RuntimeEnvironment; 33 import org.robolectric.shadows.ShadowSettings; 34 35 36 @RunWith(AndroidJUnit4.class) 37 public final class AirplaneModeEnablerTest { 38 39 private Context mContext; 40 41 @Mock 42 private AirplaneModeChangedListener mAirplaneModeChangedListener; 43 private AirplaneModeEnabler mAirplaneModeEnabler; 44 45 @Before setUp()46 public void setUp() { 47 MockitoAnnotations.initMocks(this); 48 49 mContext = RuntimeEnvironment.application.getBaseContext(); 50 mAirplaneModeEnabler = new AirplaneModeEnabler(mContext, 51 mAirplaneModeChangedListener); 52 } 53 54 @Test onRadioPowerStateChanged_beenInvoke_invokeOnAirplaneModeChanged()55 public void onRadioPowerStateChanged_beenInvoke_invokeOnAirplaneModeChanged() { 56 mAirplaneModeEnabler.resume(); 57 58 ShadowSettings.setAirplaneMode(true); 59 60 mAirplaneModeEnabler.mPhoneStateListener.onRadioPowerStateChanged( 61 TelephonyManager.RADIO_POWER_OFF); 62 63 verify(mAirplaneModeChangedListener, times(1)).onAirplaneModeChanged(true); 64 } 65 66 private class AirplaneModeChangedListener 67 implements AirplaneModeEnabler.OnAirplaneModeChangedListener { onAirplaneModeChanged(boolean isAirplaneModeOn)68 public void onAirplaneModeChanged(boolean isAirplaneModeOn) {} 69 } 70 } 71