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 private PhoneStateListenerHandler mPhoneStateListenerHandler; 32 33 private class PhoneStateListenerHandler extends HandlerThread { 34 private PhoneStateListenerHandler(String name) { 35 super(name); 36 } 37 @Override 38 public void onLooperPrepared() { 39 40 mPhoneStateListenerUT = new PhoneStateListener() { 41 @Override 42 public void onServiceStateChanged(ServiceState serviceState) { 43 logd("Service State Changed"); 44 mServiceState.setVoiceRegState(serviceState.getVoiceRegState()); 45 mServiceState.setDataRegState(serviceState.getDataRegState()); 46 setReady(true); 47 } 48 }; 49 setReady(true); 50 } 51 } 52 53 @Before 54 public void setUp() throws Exception { 55 this.setUp(this.getClass().getSimpleName()); 56 mPhoneStateListenerHandler = new PhoneStateListenerHandler(TAG); 57 mPhoneStateListenerHandler.start(); 58 waitUntilReady(); 59 } 60 61 @After 62 public void tearDown() throws Exception { 63 mPhoneStateListenerHandler.quit(); 64 super.tearDown(); 65 } 66 67 @Test @SmallTest 68 public void testTriggerServiceStateChanged() throws Exception { 69 Field field = PhoneStateListener.class.getDeclaredField("callback"); 70 field.setAccessible(true); 71 72 ServiceState ss = new ServiceState(); 73 ss.setDataRegState(ServiceState.STATE_IN_SERVICE); 74 ss.setVoiceRegState(ServiceState.STATE_EMERGENCY_ONLY); 75 76 setReady(false); 77 ((IPhoneStateListener) field.get(mPhoneStateListenerUT)).onServiceStateChanged(ss); 78 waitUntilReady(); 79 80 verify(mServiceState).setDataRegState(ServiceState.STATE_IN_SERVICE); 81 verify(mServiceState).setVoiceRegState(ServiceState.STATE_EMERGENCY_ONLY); 82 } 83 84} 85