1/* 2 * Copyright (C) 2016 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 */ 16package com.android.internal.telephony; 17 18import android.os.HandlerThread; 19import android.telephony.PhoneStateListener; 20import android.telephony.ServiceState; 21import android.test.suitebuilder.annotation.SmallTest; 22import org.junit.After; 23import org.junit.Before; 24import org.junit.Test; 25import java.lang.reflect.Field; 26import static org.mockito.Mockito.verify; 27 28public class PhoneStateListenerTest extends TelephonyTest { 29 30 private PhoneStateListener mPhoneStateListenerUT; 31 32 private class PhoneStateListenerHandler extends HandlerThread { 33 private PhoneStateListenerHandler(String name) { 34 super(name); 35 } 36 @Override 37 public void onLooperPrepared() { 38 39 mPhoneStateListenerUT = new PhoneStateListener() { 40 @Override 41 public void onServiceStateChanged(ServiceState serviceState) { 42 logd("Service State Changed"); 43 mServiceState.setVoiceRegState(serviceState.getVoiceRegState()); 44 mServiceState.setDataRegState(serviceState.getDataRegState()); 45 setReady(true); 46 } 47 }; 48 setReady(true); 49 } 50 } 51 52 @Before 53 public void setUp() throws Exception { 54 this.setUp(this.getClass().getSimpleName()); 55 new PhoneStateListenerHandler(TAG).start(); 56 waitUntilReady(); 57 } 58 59 @After 60 public void tearDown() throws Exception { 61 super.tearDown(); 62 } 63 64 @Test @SmallTest 65 public void testTriggerServiceStateChanged() throws Exception { 66 Field field = PhoneStateListener.class.getDeclaredField("callback"); 67 field.setAccessible(true); 68 69 ServiceState ss = new ServiceState(); 70 ss.setDataRegState(ServiceState.STATE_IN_SERVICE); 71 ss.setVoiceRegState(ServiceState.STATE_EMERGENCY_ONLY); 72 73 setReady(false); 74 ((IPhoneStateListener) field.get(mPhoneStateListenerUT)).onServiceStateChanged(ss); 75 waitUntilReady(); 76 77 verify(mServiceState).setDataRegState(ServiceState.STATE_IN_SERVICE); 78 verify(mServiceState).setVoiceRegState(ServiceState.STATE_EMERGENCY_ONLY); 79 } 80 81} 82