Add CTS checking the limit for PhoneStateListener
Test: atest CtsTelephonyTestCases:PhoneStateListenerTest#testListenerLimit
Bug: 151835251
Change-Id: I79b9cdc0af87b9d75234f784e38ee6b9e9070335
Merged-In: I79b9cdc0af87b9d75234f784e38ee6b9e9070335
diff --git a/hostsidetests/telephony/Android.bp b/hostsidetests/telephony/Android.bp
new file mode 100644
index 0000000..b87132f
--- /dev/null
+++ b/hostsidetests/telephony/Android.bp
@@ -0,0 +1,31 @@
+// Copyright (C) 2020 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.
+
+java_test_host {
+ name: "CtsTelephonyHostCases",
+ defaults: ["cts_defaults"],
+ srcs: ["src/**/*.java"],
+ static_libs:["CompatChangeGatingTestBase"],
+ // tag this module as a cts test artifact
+ test_suites: [
+ "cts",
+ "vts",
+ "general-tests",
+ ],
+ libs: [
+ "cts-tradefed",
+ "tradefed",
+ "compatibility-host-util",
+ ],
+}
diff --git a/hostsidetests/telephony/AndroidTest.xml b/hostsidetests/telephony/AndroidTest.xml
new file mode 100644
index 0000000..388d07d
--- /dev/null
+++ b/hostsidetests/telephony/AndroidTest.xml
@@ -0,0 +1,29 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2020 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.
+-->
+<configuration description="Config for Telephony host test cases">
+ <option name="test-suite-tag" value="cts" />
+ <option name="config-descriptor:metadata" key="component" value="telecom" />
+ <option name="config-descriptor:metadata" key="parameter" value="not_instant_app" />
+ <option name="config-descriptor:metadata" key="parameter" value="not_multi_abi" />
+ <option name="config-descriptor:metadata" key="parameter" value="secondary_user" />
+ <target_preparer class="com.android.tradefed.targetprep.suite.SuiteApkInstaller">
+ <option name="cleanup-apks" value="true" />
+ <option name="test-file-name" value="TelephonyDeviceTest.apk" />
+ </target_preparer>
+ <test class="com.android.compatibility.common.tradefed.testtype.JarHostTest" >
+ <option name="jar" value="CtsTelephonyHostCases.jar" />
+ </test>
+</configuration>
diff --git a/hostsidetests/telephony/OWNERS b/hostsidetests/telephony/OWNERS
new file mode 100644
index 0000000..fbb2d02
--- /dev/null
+++ b/hostsidetests/telephony/OWNERS
@@ -0,0 +1,2 @@
+# Bug component: 20868
+include ../../tests/tests/telephony/OWNERS
diff --git a/hostsidetests/telephony/devicetest/Android.bp b/hostsidetests/telephony/devicetest/Android.bp
new file mode 100644
index 0000000..a732b22
--- /dev/null
+++ b/hostsidetests/telephony/devicetest/Android.bp
@@ -0,0 +1,37 @@
+//
+// Copyright (C) 2020 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.
+//
+
+android_test_helper_app {
+ name: "TelephonyDeviceTest",
+ defaults: ["cts_support_defaults"],
+ platform_apis: true,
+ srcs: ["src/**/*.java"],
+ libs: [
+ "junit",
+ ],
+ static_libs: [
+ "compatibility-device-util-axt",
+ "ctstestrunner-axt",
+ "truth-prebuilt",
+ ],
+
+ // Tag this module as a cts test artifact
+ test_suites: [
+ "cts",
+ "vts",
+ "general-tests",
+ ],
+}
diff --git a/hostsidetests/telephony/devicetest/AndroidManifest.xml b/hostsidetests/telephony/devicetest/AndroidManifest.xml
new file mode 100644
index 0000000..2887335
--- /dev/null
+++ b/hostsidetests/telephony/devicetest/AndroidManifest.xml
@@ -0,0 +1,28 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2020 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.
+-->
+
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+ package="android.telephony.device.cts">
+ <application
+ android:debuggable="true">
+ <uses-library android:name="android.test.runner" />
+ </application>
+
+ <instrumentation
+ android:name="androidx.test.runner.AndroidJUnitRunner"
+ android:targetPackage="android.telephony.device.cts" />
+
+</manifest>
diff --git a/hostsidetests/telephony/devicetest/src/android/telephony/device/cts/TelephonyTest.java b/hostsidetests/telephony/devicetest/src/android/telephony/device/cts/TelephonyTest.java
new file mode 100644
index 0000000..fdcd338
--- /dev/null
+++ b/hostsidetests/telephony/devicetest/src/android/telephony/device/cts/TelephonyTest.java
@@ -0,0 +1,150 @@
+/*
+ * Copyright (C) 2013 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 android.telephony.device.cts;
+
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+import android.os.Handler;
+import android.os.HandlerExecutor;
+import android.os.HandlerThread;
+import android.provider.DeviceConfig;
+import android.telephony.PhoneStateListener;
+import android.telephony.TelephonyManager;
+import android.telephony.TelephonyRegistryManager;
+
+import androidx.test.InstrumentationRegistry;
+import androidx.test.runner.AndroidJUnit4;
+
+import com.android.compatibility.common.util.ShellIdentityUtils;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import java.util.List;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.TimeUnit;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+@RunWith(AndroidJUnit4.class)
+public class TelephonyTest {
+ private static final int TEST_TIMEOUT_MILLIS = 1000;
+
+ private Handler mHandler;
+ private HandlerThread mHandlerThread;
+ private TelephonyManager mTelephonyManager;
+
+ @Before
+ public void setUp() {
+ mHandlerThread = new HandlerThread(TelephonyTest.class.getSimpleName());
+ mHandlerThread.start();
+ mHandler = new Handler(mHandlerThread.getLooper());
+ mTelephonyManager = InstrumentationRegistry.getContext()
+ .getSystemService(TelephonyManager.class);
+ }
+
+ @After
+ public void tearDown() {
+ mHandlerThread.quitSafely();
+ }
+
+ @Test
+ public void testListenerRegistrationWithChangeEnabled() throws Throwable {
+ // Register a bunch of filler listeners first so that we hit the cap
+ List<PhoneStateListener> fillerListeners = registerFillerListeners();
+ if (fillerListeners == null) {
+ return;
+ }
+
+ TelephonyRegistryManager telephonyRegistryManager = InstrumentationRegistry.getContext()
+ .getSystemService(TelephonyRegistryManager.class);
+
+ LinkedBlockingQueue<Boolean> queue = new LinkedBlockingQueue<>(1);
+ PhoneStateListener testListener = new PhoneStateListener(new HandlerExecutor(mHandler)) {
+ @Override
+ public void onCallStateChanged(int state, String number) {
+ queue.offer(true);
+ }
+ };
+
+ try {
+ mTelephonyManager.listen(testListener, PhoneStateListener.LISTEN_CALL_STATE);
+ fail("Expected an IllegalStateException");
+ } catch (IllegalStateException e) {
+ // expected
+ }
+
+ // Now, deregister one of the fillers and try that again. This time it should succeed
+ mTelephonyManager.listen(fillerListeners.get(0), PhoneStateListener.LISTEN_NONE);
+
+ mTelephonyManager.listen(testListener, PhoneStateListener.LISTEN_CALL_STATE);
+ ShellIdentityUtils.invokeMethodWithShellPermissionsNoReturn(telephonyRegistryManager,
+ (trm) -> trm.notifyCallStateChangedForAllSubscriptions(
+ TelephonyManager.CALL_STATE_IDLE, "12345678"));
+
+ Boolean result = queue.poll(TEST_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
+ assertTrue("Never got anything on the extra listener", result);
+ }
+
+ @Test
+ public void testListenerRegistrationWithChangeDisabled() throws Throwable {
+ if (registerFillerListeners() == null) {
+ return;
+ }
+
+ TelephonyRegistryManager telephonyRegistryManager = InstrumentationRegistry.getContext()
+ .getSystemService(TelephonyRegistryManager.class);
+
+ LinkedBlockingQueue<Boolean> queue = new LinkedBlockingQueue<>(1);
+ PhoneStateListener testListener = new PhoneStateListener(new HandlerExecutor(mHandler)) {
+ @Override
+ public void onCallStateChanged(int state, String number) {
+ queue.offer(true);
+ }
+ };
+
+ mTelephonyManager.listen(testListener, PhoneStateListener.LISTEN_CALL_STATE);
+ ShellIdentityUtils.invokeMethodWithShellPermissionsNoReturn(telephonyRegistryManager,
+ (trm) -> trm.notifyCallStateChangedForAllSubscriptions(
+ TelephonyManager.CALL_STATE_IDLE, "12345678"));
+
+ Boolean result = queue.poll(TEST_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
+ assertTrue("Never got anything on the extra listener", result);
+ }
+
+ private List<PhoneStateListener> registerFillerListeners() {
+ int registrationLimit = ShellIdentityUtils.invokeStaticMethodWithShellPermissions(() ->
+ DeviceConfig.getInt(DeviceConfig.NAMESPACE_TELEPHONY,
+ PhoneStateListener.FLAG_PER_PID_REGISTRATION_LIMIT,
+ PhoneStateListener.DEFAULT_PER_PID_REGISTRATION_LIMIT));
+ if (registrationLimit < 1) {
+ // Don't test anything if the limit is too small
+ return null;
+ }
+ List<PhoneStateListener> fillerListeners = Stream.generate(
+ () -> new PhoneStateListener(new HandlerExecutor(mHandler)))
+ .limit(registrationLimit)
+ .collect(Collectors.toList());
+ fillerListeners.forEach((l) -> mTelephonyManager.listen(
+ l, PhoneStateListener.LISTEN_SERVICE_STATE));
+
+ return fillerListeners;
+ }
+}
diff --git a/hostsidetests/telephony/src/android/telephony/cts/TelephonyHostTest.java b/hostsidetests/telephony/src/android/telephony/cts/TelephonyHostTest.java
new file mode 100644
index 0000000..c814a64
--- /dev/null
+++ b/hostsidetests/telephony/src/android/telephony/cts/TelephonyHostTest.java
@@ -0,0 +1,51 @@
+/*
+ * Copyright (C) 2020 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 android.telephony.cts;
+
+import android.compat.cts.CompatChangeGatingTestCase;
+
+import com.google.common.collect.ImmutableSet;
+
+/**
+ * Tests for {@link android.telephony.TelephonyManager} and related APIs
+ */
+
+public class TelephonyHostTest extends CompatChangeGatingTestCase {
+
+ protected static final String TEST_APK = "TelephonyDeviceTest.apk";
+ protected static final String TEST_PKG = "android.telephony.device.cts";
+
+ public static final long PHONE_STATE_LISTENER_LIMIT_CHANGE_ID = 150880553L;
+
+ @Override
+ protected void setUp() throws Exception {
+ installPackage(TEST_APK, true);
+ }
+
+ public void testWithChangeEnabled() throws Exception {
+ runDeviceCompatTest(TEST_PKG, ".TelephonyTest", "testListenerRegistrationWithChangeEnabled",
+ /*enabledChanges*/ImmutableSet.of(PHONE_STATE_LISTENER_LIMIT_CHANGE_ID),
+ /*disabledChanges*/ ImmutableSet.of());
+ }
+
+ public void testWithChangeDisabled() throws Exception {
+ runDeviceCompatTest(TEST_PKG, ".TelephonyTest",
+ "testListenerRegistrationWithChangeDisabled",
+ /*enabledChanges*/ImmutableSet.of(),
+ /*disabledChanges*/ ImmutableSet.of(PHONE_STATE_LISTENER_LIMIT_CHANGE_ID));
+ }
+}