| /* |
| * Copyright (C) 2016 The Android Open Source Project |
| * |
| * Licensed under the Apache License, Version 2.0 (the "License"); |
| * you may not use this file except in compliance with the License. |
| * You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License |
| */ |
| |
| package com.android.services.telephony; |
| |
| import android.os.AsyncResult; |
| import android.os.Handler; |
| import android.telephony.ServiceState; |
| import android.support.test.runner.AndroidJUnit4; |
| |
| import com.android.TelephonyTestBase; |
| import com.android.internal.telephony.Phone; |
| import com.android.internal.telephony.PhoneConstants; |
| |
| import org.junit.After; |
| import org.junit.Before; |
| import org.junit.Test; |
| import org.junit.runner.RunWith; |
| import org.mockito.Mock; |
| |
| import static org.mockito.Matchers.anyBoolean; |
| import static org.mockito.Matchers.isNull; |
| import static org.mockito.Mockito.never; |
| import static org.mockito.Mockito.times; |
| import static org.mockito.Mockito.verify; |
| import static org.mockito.Mockito.any; |
| import static org.mockito.Mockito.eq; |
| import static org.mockito.Mockito.when; |
| |
| /** |
| * Tests the EmergencyCallStateListener, which listens to one Phone and waits until its service |
| * state changes to accepting emergency calls or in service. If it can not find a tower to camp onto |
| * for emergency calls, then it will fail after a timeout period. |
| */ |
| @RunWith(AndroidJUnit4.class) |
| public class EmergencyCallStateListenerTest extends TelephonyTestBase { |
| |
| private static final long TIMEOUT_MS = 100; |
| |
| @Mock Phone mMockPhone; |
| @Mock EmergencyCallStateListener.Callback mCallback; |
| EmergencyCallStateListener mListener; |
| |
| @Before |
| public void setUp() throws Exception { |
| super.setUp(); |
| mListener = new EmergencyCallStateListener(); |
| } |
| |
| @After |
| public void tearDown() throws Exception { |
| mListener.getHandler().removeCallbacksAndMessages(null); |
| super.tearDown(); |
| } |
| |
| @Test |
| public void testRegisterForCallback() { |
| mListener.waitForRadioOn(mMockPhone, mCallback); |
| |
| waitForHandlerAction(mListener.getHandler(), TIMEOUT_MS); |
| |
| verify(mMockPhone).unregisterForServiceStateChanged(any(Handler.class)); |
| verify(mMockPhone).registerForServiceStateChanged(any(Handler.class), |
| eq(EmergencyCallStateListener.MSG_SERVICE_STATE_CHANGED), isNull()); |
| } |
| |
| @Test |
| public void testPhoneChangeState_InService() { |
| ServiceState state = new ServiceState(); |
| state.setState(ServiceState.STATE_IN_SERVICE); |
| when(mMockPhone.getState()).thenReturn(PhoneConstants.State.IDLE); |
| mListener.waitForRadioOn(mMockPhone, mCallback); |
| waitForHandlerAction(mListener.getHandler(), TIMEOUT_MS); |
| |
| mListener.getHandler().obtainMessage(EmergencyCallStateListener.MSG_SERVICE_STATE_CHANGED, |
| new AsyncResult(null, state, null)).sendToTarget(); |
| |
| waitForHandlerAction(mListener.getHandler(), TIMEOUT_MS); |
| verify(mCallback).onComplete(eq(mListener), eq(true)); |
| } |
| |
| @Test |
| public void testPhoneChangeState_EmergencyCalls() { |
| ServiceState state = new ServiceState(); |
| state.setState(ServiceState.STATE_OUT_OF_SERVICE); |
| state.setEmergencyOnly(true); |
| when(mMockPhone.getState()).thenReturn(PhoneConstants.State.IDLE); |
| when(mMockPhone.getServiceState()).thenReturn(state); |
| mListener.waitForRadioOn(mMockPhone, mCallback); |
| waitForHandlerAction(mListener.getHandler(), TIMEOUT_MS); |
| |
| mListener.getHandler().obtainMessage(EmergencyCallStateListener.MSG_SERVICE_STATE_CHANGED, |
| new AsyncResult(null, state, null)).sendToTarget(); |
| |
| waitForHandlerAction(mListener.getHandler(), TIMEOUT_MS); |
| verify(mCallback).onComplete(eq(mListener), eq(true)); |
| } |
| |
| @Test |
| public void testPhoneChangeState_OutOfService() { |
| ServiceState state = new ServiceState(); |
| state.setState(ServiceState.STATE_OUT_OF_SERVICE); |
| when(mMockPhone.getState()).thenReturn(PhoneConstants.State.IDLE); |
| when(mMockPhone.getServiceState()).thenReturn(state); |
| mListener.waitForRadioOn(mMockPhone, mCallback); |
| waitForHandlerAction(mListener.getHandler(), TIMEOUT_MS); |
| |
| // Don't expect any answer, since it is not the one that we want and the timeout for giving |
| // up hasn't expired yet. |
| mListener.getHandler().obtainMessage(EmergencyCallStateListener.MSG_SERVICE_STATE_CHANGED, |
| new AsyncResult(null, state, null)).sendToTarget(); |
| |
| waitForHandlerAction(mListener.getHandler(), TIMEOUT_MS); |
| verify(mCallback, never()).onComplete(any(EmergencyCallStateListener.class), anyBoolean()); |
| } |
| |
| @Test |
| public void testTimeout_EmergencyCalls() { |
| ServiceState state = new ServiceState(); |
| state.setState(ServiceState.STATE_OUT_OF_SERVICE); |
| state.setEmergencyOnly(true); |
| when(mMockPhone.getState()).thenReturn(PhoneConstants.State.IDLE); |
| when(mMockPhone.getServiceState()).thenReturn(state); |
| mListener.waitForRadioOn(mMockPhone, mCallback); |
| mListener.setTimeBetweenRetriesMillis(500); |
| |
| // Wait for the timer to expire and check state manually in onRetryTimeout |
| waitForHandlerActionDelayed(mListener.getHandler(), TIMEOUT_MS, 600); |
| |
| verify(mCallback).onComplete(eq(mListener), eq(true)); |
| } |
| |
| @Test |
| public void testTimeout_RetryFailure() { |
| ServiceState state = new ServiceState(); |
| state.setState(ServiceState.STATE_POWER_OFF); |
| when(mMockPhone.getState()).thenReturn(PhoneConstants.State.IDLE); |
| when(mMockPhone.getServiceState()).thenReturn(state); |
| mListener.waitForRadioOn(mMockPhone, mCallback); |
| mListener.setTimeBetweenRetriesMillis(100); |
| mListener.setMaxNumRetries(2); |
| |
| // Wait for the timer to expire and check state manually in onRetryTimeout |
| waitForHandlerActionDelayed(mListener.getHandler(), TIMEOUT_MS, 600); |
| |
| verify(mCallback).onComplete(eq(mListener), eq(false)); |
| verify(mMockPhone, times(2)).setRadioPower(eq(true)); |
| } |
| |
| } |