1 /* 2 * Copyright (C) 2013 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 android.telephony.device.cts; 18 19 import static org.junit.Assert.assertTrue; 20 import static org.junit.Assert.fail; 21 22 import android.os.Handler; 23 import android.os.HandlerExecutor; 24 import android.os.HandlerThread; 25 import android.provider.DeviceConfig; 26 import android.telephony.PhoneStateListener; 27 import android.telephony.TelephonyCallback; 28 import android.telephony.TelephonyManager; 29 import android.telephony.TelephonyRegistryManager; 30 31 import androidx.test.InstrumentationRegistry; 32 import androidx.test.runner.AndroidJUnit4; 33 34 import com.android.compatibility.common.util.ShellIdentityUtils; 35 36 import org.junit.After; 37 import org.junit.Before; 38 import org.junit.Test; 39 import org.junit.runner.RunWith; 40 41 import java.util.List; 42 import java.util.concurrent.LinkedBlockingQueue; 43 import java.util.concurrent.TimeUnit; 44 import java.util.stream.Collectors; 45 import java.util.stream.Stream; 46 47 @RunWith(AndroidJUnit4.class) 48 public class TelephonyTest { 49 private static final int TEST_TIMEOUT_MILLIS = 1000; 50 51 private Handler mHandler; 52 private HandlerThread mHandlerThread; 53 private TelephonyManager mTelephonyManager; 54 55 @Before setUp()56 public void setUp() { 57 mHandlerThread = new HandlerThread(TelephonyTest.class.getSimpleName()); 58 mHandlerThread.start(); 59 mHandler = new Handler(mHandlerThread.getLooper()); 60 mTelephonyManager = InstrumentationRegistry.getContext() 61 .getSystemService(TelephonyManager.class); 62 } 63 64 @After tearDown()65 public void tearDown() { 66 mHandlerThread.quitSafely(); 67 } 68 69 @Test testListenerRegistrationWithChangeEnabled()70 public void testListenerRegistrationWithChangeEnabled() throws Throwable { 71 // Register a bunch of filler listeners first so that we hit the cap 72 List<PhoneStateListener> fillerListeners = registerFillerListeners(); 73 if (fillerListeners == null) { 74 return; 75 } 76 77 TelephonyRegistryManager telephonyRegistryManager = InstrumentationRegistry.getContext() 78 .getSystemService(TelephonyRegistryManager.class); 79 80 LinkedBlockingQueue<Boolean> queue = new LinkedBlockingQueue<>(1); 81 PhoneStateListener testListener = new PhoneStateListener(new HandlerExecutor(mHandler)) { 82 @Override 83 public void onCallStateChanged(int state, String number) { 84 queue.offer(true); 85 } 86 }; 87 88 try { 89 mTelephonyManager.listen(testListener, PhoneStateListener.LISTEN_CALL_STATE); 90 fail("Expected an IllegalStateException"); 91 } catch (IllegalStateException e) { 92 // expected 93 } 94 95 // Now, deregister one of the fillers and try that again. This time it should succeed 96 mTelephonyManager.listen(fillerListeners.get(0), PhoneStateListener.LISTEN_NONE); 97 98 mTelephonyManager.listen(testListener, PhoneStateListener.LISTEN_CALL_STATE); 99 ShellIdentityUtils.invokeMethodWithShellPermissionsNoReturn(telephonyRegistryManager, 100 (trm) -> trm.notifyCallStateChangedForAllSubscriptions( 101 TelephonyManager.CALL_STATE_IDLE, "12345678")); 102 103 Boolean result = queue.poll(TEST_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS); 104 assertTrue("Never got anything on the extra listener", result); 105 } 106 107 @Test testListenerRegistrationWithChangeDisabled()108 public void testListenerRegistrationWithChangeDisabled() throws Throwable { 109 if (registerFillerListeners() == null) { 110 return; 111 } 112 113 TelephonyRegistryManager telephonyRegistryManager = InstrumentationRegistry.getContext() 114 .getSystemService(TelephonyRegistryManager.class); 115 116 LinkedBlockingQueue<Boolean> queue = new LinkedBlockingQueue<>(1); 117 PhoneStateListener testListener = new PhoneStateListener(new HandlerExecutor(mHandler)) { 118 @Override 119 public void onCallStateChanged(int state, String number) { 120 queue.offer(true); 121 } 122 }; 123 124 mTelephonyManager.listen(testListener, PhoneStateListener.LISTEN_CALL_STATE); 125 ShellIdentityUtils.invokeMethodWithShellPermissionsNoReturn(telephonyRegistryManager, 126 (trm) -> trm.notifyCallStateChangedForAllSubscriptions( 127 TelephonyManager.CALL_STATE_IDLE, "12345678")); 128 129 Boolean result = queue.poll(TEST_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS); 130 assertTrue("Never got anything on the extra listener", result); 131 } 132 registerFillerListeners()133 private List<PhoneStateListener> registerFillerListeners() { 134 int registrationLimit = ShellIdentityUtils.invokeStaticMethodWithShellPermissions(() -> 135 DeviceConfig.getInt(DeviceConfig.NAMESPACE_TELEPHONY, 136 TelephonyCallback.FLAG_PER_PID_REGISTRATION_LIMIT, 137 TelephonyCallback.DEFAULT_PER_PID_REGISTRATION_LIMIT)); 138 if (registrationLimit < 1) { 139 // Don't test anything if the limit is too small 140 return null; 141 } 142 List<PhoneStateListener> fillerListeners = Stream.generate( 143 () -> new PhoneStateListener(new HandlerExecutor(mHandler))) 144 .limit(registrationLimit) 145 .collect(Collectors.toList()); 146 fillerListeners.forEach((l) -> mTelephonyManager.listen( 147 l, PhoneStateListener.LISTEN_SERVICE_STATE)); 148 149 return fillerListeners; 150 } 151 } 152