TelecomManager#getSelfManagedPA's CTS (2/3)

bug: 209839019
Test: CTS
Change-Id: I3633f9415e327ed30b3e3283b3d6a4634632e2c5
diff --git a/tests/tests/telecom/Android.bp b/tests/tests/telecom/Android.bp
index 437743a..ecd4d30 100644
--- a/tests/tests/telecom/Android.bp
+++ b/tests/tests/telecom/Android.bp
@@ -32,6 +32,7 @@
     ],
     path: "aidl/",
 }
+
 filegroup {
     name: "third-pty-dialer-app-srcs",
     srcs: [
@@ -41,6 +42,7 @@
     ],
     path: "ThirdPtyInCallServiceTestApp/",
 }
+
 filegroup {
     name: "third-pty-dialer-app-aidl",
     srcs: [
@@ -76,6 +78,7 @@
         "Api29InCallServiceTestApp/**/I*.aidl",
         "ThirdPtyDialerTestApp/**/*.java",
         "ThirdPtyDialerTestAppTwo/**/*.java",
+        "CarModeTestAppSelfManaged/**/*.java",
         "CarModeTestAppTwo/**/*.java",
     ],
     exclude_srcs: [
diff --git a/tests/tests/telecom/AndroidTest.xml b/tests/tests/telecom/AndroidTest.xml
index 8f17802..8ff4752 100644
--- a/tests/tests/telecom/AndroidTest.xml
+++ b/tests/tests/telecom/AndroidTest.xml
@@ -30,6 +30,7 @@
         <option name="test-file-name" value="CallScreeningServiceTestApp.apk" />
         <option name="test-file-name" value="CarModeTestApp.apk" />
         <option name="test-file-name" value="CarModeTestAppTwo.apk" />
+        <option name="test-file-name" value="CarModeTestAppSelfManaged.apk" />
         <option name="test-file-name" value="ThirdPtyDialerTestApp.apk" />
         <option name="test-file-name" value="ThirdPtyDialerTestAppTwo.apk" />
     </target_preparer>
diff --git a/tests/tests/telecom/CarModeTestAppSelfManaged/Android.bp b/tests/tests/telecom/CarModeTestAppSelfManaged/Android.bp
new file mode 100644
index 0000000..edc9f63
--- /dev/null
+++ b/tests/tests/telecom/CarModeTestAppSelfManaged/Android.bp
@@ -0,0 +1,38 @@
+// Copyright (C) 2019 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 {
+    default_applicable_licenses: ["Android-Apache-2.0"],
+}
+
+android_test_helper_app {
+    name: "CarModeTestAppSelfManaged",
+    defaults: ["cts_defaults"],
+    srcs: [
+        "src/**/*.java",
+        ":car-mode-app-srcs",
+        ":car-mode-app-aidl",
+    ],
+    static_libs: [
+        "compatibility-device-util-axt",
+        "ctstestrunner-axt",
+        "androidx.test.rules",
+        "CtsTelecomMockLib",
+    ],
+    sdk_version: "test_current",
+    test_suites: [
+        "cts",
+        "general-tests",
+    ],
+}
diff --git a/tests/tests/telecom/CarModeTestAppSelfManaged/AndroidManifest.xml b/tests/tests/telecom/CarModeTestAppSelfManaged/AndroidManifest.xml
new file mode 100644
index 0000000..c792dfd
--- /dev/null
+++ b/tests/tests/telecom/CarModeTestAppSelfManaged/AndroidManifest.xml
@@ -0,0 +1,59 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2019 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.telecom.cts.carmodetestappselfmanaged"
+          android:versionCode="1"
+          android:versionName="1.0">
+
+    <uses-permission android:name="android.permission.MANAGE_OWN_CALLS" />
+    <uses-permission android:name="android.permission.BIND_TELECOM_CONNECTION_SERVICE" />
+
+    <application android:label="CarModeTestAppSelfManaged">
+        <service android:name=".CtsCarModeInCallServiceSelfManaged"
+                 android:permission="android.permission.BIND_INCALL_SERVICE"
+                 android:launchMode="singleInstance"
+                 android:exported="true">
+            <meta-data android:name="android.telecom.IN_CALL_SERVICE_CAR_MODE_UI"
+                       android:value="true"/>
+            <meta-data android:name="android.telecom.INCLUDE_EXTERNAL_CALLS"
+                       android:value="true" />
+            <meta-data android:name="android.telecom.INCLUDE_SELF_MANAGED_CALLS"
+                       android:value="true" />
+            <intent-filter>
+                <action android:name="android.telecom.InCallService"/>
+            </intent-filter>
+        </service>
+
+        <service android:name=".CtsCarModeInCallServiceControlSelfManaged"
+                 android:launchMode="singleInstance"
+                 android:exported="true">
+            <intent-filter>
+                <action
+                    android:name="android.telecom.cts.carmodetestapp.ACTION_CAR_MODE_CONTROL"/>
+            </intent-filter>
+        </service>
+
+        <service android:name="android.telecom.cts.CtsSelfManagedConnectionService"
+                 android:permission="android.permission.BIND_TELECOM_CONNECTION_SERVICE"
+                 android:exported="true">
+            <intent-filter>
+                <action android:name="android.telecom.ConnectionService"/>
+            </intent-filter>
+        </service>
+
+    </application>
+</manifest>
\ No newline at end of file
diff --git a/tests/tests/telecom/CarModeTestAppSelfManaged/src/android/telecom/cts/carmodetestappselfmanaged/CtsCarModeInCallServiceControlSelfManaged.java b/tests/tests/telecom/CarModeTestAppSelfManaged/src/android/telecom/cts/carmodetestappselfmanaged/CtsCarModeInCallServiceControlSelfManaged.java
new file mode 100644
index 0000000..b6d5c10
--- /dev/null
+++ b/tests/tests/telecom/CarModeTestAppSelfManaged/src/android/telecom/cts/carmodetestappselfmanaged/CtsCarModeInCallServiceControlSelfManaged.java
@@ -0,0 +1,22 @@
+/*
+ * Copyright (C) 2019 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.telecom.cts.carmodetestappselfmanaged;
+
+import android.telecom.cts.carmodetestapp.CtsCarModeInCallServiceControl;
+
+public class CtsCarModeInCallServiceControlSelfManaged extends CtsCarModeInCallServiceControl {
+}
diff --git a/tests/tests/telecom/CarModeTestAppSelfManaged/src/android/telecom/cts/carmodetestappselfmanaged/CtsCarModeInCallServiceSelfManaged.java b/tests/tests/telecom/CarModeTestAppSelfManaged/src/android/telecom/cts/carmodetestappselfmanaged/CtsCarModeInCallServiceSelfManaged.java
new file mode 100644
index 0000000..e7b29e3
--- /dev/null
+++ b/tests/tests/telecom/CarModeTestAppSelfManaged/src/android/telecom/cts/carmodetestappselfmanaged/CtsCarModeInCallServiceSelfManaged.java
@@ -0,0 +1,22 @@
+/*
+ * Copyright (C) 2019 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.telecom.cts.carmodetestappselfmanaged;
+
+import android.telecom.cts.carmodetestapp.CtsCarModeInCallService;
+
+public class CtsCarModeInCallServiceSelfManaged extends CtsCarModeInCallService {
+}
diff --git a/tests/tests/telecom/aidl/android/telecom/cts/carmodetestapp/ICtsCarModeInCallServiceControl.aidl b/tests/tests/telecom/aidl/android/telecom/cts/carmodetestapp/ICtsCarModeInCallServiceControl.aidl
index 5357afb..d76e491 100644
--- a/tests/tests/telecom/aidl/android/telecom/cts/carmodetestapp/ICtsCarModeInCallServiceControl.aidl
+++ b/tests/tests/telecom/aidl/android/telecom/cts/carmodetestapp/ICtsCarModeInCallServiceControl.aidl
@@ -15,6 +15,8 @@
  */
 
 package android.telecom.cts.carmodetestapp;
+import android.telecom.PhoneAccountHandle;
+import android.telecom.PhoneAccount;
 
 interface ICtsCarModeInCallServiceControl {
     boolean isBound();
@@ -27,4 +29,7 @@
     boolean requestAutomotiveProjection();
     void releaseAutomotiveProjection();
     boolean checkBindStatus(boolean bind);
-}
+    List<PhoneAccountHandle> getSelfManagedPhoneAccounts();
+    void registerPhoneAccount(in PhoneAccount phoneAccount);
+    void unregisterPhoneAccount(in PhoneAccountHandle phoneAccountHandle);
+}
\ No newline at end of file
diff --git a/tests/tests/telecom/src/android/telecom/cts/SelfManagedConnectionTest.java b/tests/tests/telecom/src/android/telecom/cts/SelfManagedConnectionTest.java
index 29beb99..ae0554a 100644
--- a/tests/tests/telecom/src/android/telecom/cts/SelfManagedConnectionTest.java
+++ b/tests/tests/telecom/src/android/telecom/cts/SelfManagedConnectionTest.java
@@ -24,13 +24,17 @@
 import android.content.Intent;
 import android.content.ServiceConnection;
 import android.content.res.Configuration;
+import android.graphics.Color;
 import android.net.Uri;
 import android.os.AsyncTask;
 import android.os.IBinder;
-import android.os.RemoteException;
 import android.os.UserHandle;
+import android.telecom.PhoneAccount;
+import android.telecom.PhoneAccountHandle;
+import android.telecom.TelecomManager;
 import android.telecom.cts.carmodetestapp.CtsCarModeInCallServiceControl;
 import android.telecom.cts.carmodetestapp.ICtsCarModeInCallServiceControl;
+import android.telecom.cts.carmodetestappselfmanaged.CtsCarModeInCallServiceControlSelfManaged;
 import android.telecom.cts.carmodetestapptwo.CtsCarModeInCallServiceControlTwo;
 import android.telecom.cts.thirdptydialer.CtsThirdPtyDialerInCallServiceControl;
 import android.telecom.cts.thirdptydialertwo.CtsThirdPtyDialerInCallServiceControlTwo;
@@ -38,6 +42,7 @@
 import android.telecom.cts.thirdptyincallservice.ICtsThirdPartyInCallServiceControl;
 import android.util.Log;
 
+import java.util.List;
 import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.ExecutionException;
@@ -73,16 +78,41 @@
             CAR_DIALER_PKG_1, CtsCarModeInCallServiceControl.class.getName());
     private static final String CAR_DIALER_PKG_2 = CtsCarModeInCallServiceControlTwo.class
             .getPackage().getName();
+    private static final String CAR_SELF_MANAGED_PKG =
+            CtsCarModeInCallServiceControlSelfManaged.class
+                    .getPackage().getName();
     private static final ComponentName CAR_DIALER_2 = ComponentName.createRelative(
             CAR_DIALER_PKG_2, CtsCarModeInCallServiceControlTwo.class.getName());
+    private static final ComponentName CAR_SELF_MANAGED_COMPONENT = ComponentName.createRelative(
+            CAR_SELF_MANAGED_PKG, CtsCarModeInCallServiceControlSelfManaged.class.getName());
 
     private Uri TEST_ADDRESS = Uri.fromParts("tel", "6505551213", null);
 
+    private static final PhoneAccountHandle TEST_CAR_SELF_MANAGED_HANDLE =
+            new PhoneAccountHandle(
+                    new ComponentName(CAR_SELF_MANAGED_PKG, TestUtils.SELF_MANAGED_COMPONENT),
+                    TestUtils.SELF_MANAGED_ACCOUNT_ID_1);
+
+    private static final PhoneAccount TEST_SELF_MANAGED_PHONE_ACCOUNT = PhoneAccount.builder(
+                    TEST_CAR_SELF_MANAGED_HANDLE, TestUtils.SELF_MANAGED_ACCOUNT_LABEL)
+            .setAddress(Uri.parse("sip:test@test.com"))
+            .setSubscriptionAddress(Uri.parse("sip:test@test.com"))
+            .setCapabilities(PhoneAccount.CAPABILITY_SELF_MANAGED
+                    | PhoneAccount.CAPABILITY_SUPPORTS_VIDEO_CALLING
+                    | PhoneAccount.CAPABILITY_VIDEO_CALLING)
+            .setHighlightColor(Color.BLUE)
+            .setShortDescription(TestUtils.SELF_MANAGED_ACCOUNT_LABEL)
+            .addSupportedUriScheme(PhoneAccount.SCHEME_TEL)
+            .addSupportedUriScheme(PhoneAccount.SCHEME_SIP)
+            .setExtras(TestUtils.SELF_MANAGED_ACCOUNT_1_EXTRAS)
+            .build();
+
     private RoleManager mRoleManager;
     private String mDefaultDialer;
     private UiAutomation mUiAutomation;
     private ICtsCarModeInCallServiceControl mCarModeIncallServiceControlOne;
     private ICtsCarModeInCallServiceControl mCarModeIncallServiceControlTwo;
+    private ICtsCarModeInCallServiceControl mCarModeIncallServiceControlSelfManaged;
 
     private class TestServiceConnection implements ServiceConnection {
         private IBinder mService;
@@ -142,6 +172,7 @@
 
         disableAndVerifyCarMode(mCarModeIncallServiceControlOne, Configuration.UI_MODE_TYPE_NORMAL);
         disableAndVerifyCarMode(mCarModeIncallServiceControlTwo, Configuration.UI_MODE_TYPE_NORMAL);
+
         disconnectAllCallsAndVerify(mCarModeIncallServiceControlOne);
         disconnectAllCallsAndVerify(mCarModeIncallServiceControlTwo);
 
@@ -250,6 +281,43 @@
         mContext.unbindService(controlConn);
     }
 
+    /**
+     * Test {@link TelecomManager#getSelfManagedPhoneAccounts} works on packages with only the
+     * {@link android.Manifest.permission#MANAGE_OWN_CALLS} permission
+     */
+    public void testTelecomManagerGetSelfManagedPhoneAccountsForPackage() throws Exception {
+        if (!mShouldTestTelecom) {
+            return;
+        }
+        // bind to CarModeTestAppSelfManaged which only has the
+        // {@link android.Manifest.permission#MANAGE_OWN_CALLS} permission
+        TestServiceConnection control = setUpControl(CAR_MODE_CONTROL, CAR_SELF_MANAGED_COMPONENT);
+
+        mCarModeIncallServiceControlSelfManaged =
+                ICtsCarModeInCallServiceControl.Stub
+                        .asInterface(control.getService());
+
+        mCarModeIncallServiceControlSelfManaged.reset();
+
+        // register a self-managed phone account
+        mCarModeIncallServiceControlSelfManaged.registerPhoneAccount(
+                TEST_SELF_MANAGED_PHONE_ACCOUNT);
+
+        List<PhoneAccountHandle> pah =
+                mCarModeIncallServiceControlSelfManaged.getSelfManagedPhoneAccounts();
+
+        // assert that we can get all the self-managed phone accounts registered to
+        // CarModeTestAppSelfManaged
+        assertEquals(1, pah.size());
+        assertTrue(pah.contains(TEST_CAR_SELF_MANAGED_HANDLE));
+
+        mCarModeIncallServiceControlSelfManaged.unregisterPhoneAccount(
+                TEST_CAR_SELF_MANAGED_HANDLE);
+
+        // unbind to CarModeTestAppSelfManaged
+        mContext.unbindService(control);
+    }
+
     public void testChangeCarModeApp() throws Exception {
         if (!mShouldTestTelecom) {
             return;
diff --git a/tests/tests/telecom/src/android/telecom/cts/carmodetestapp/CtsCarModeInCallServiceControl.java b/tests/tests/telecom/src/android/telecom/cts/carmodetestapp/CtsCarModeInCallServiceControl.java
index fe38d3c..dc3ae44 100644
--- a/tests/tests/telecom/src/android/telecom/cts/carmodetestapp/CtsCarModeInCallServiceControl.java
+++ b/tests/tests/telecom/src/android/telecom/cts/carmodetestapp/CtsCarModeInCallServiceControl.java
@@ -21,8 +21,14 @@
 import android.content.ComponentName;
 import android.content.Intent;
 import android.os.IBinder;
+import android.telecom.PhoneAccount;
+import android.telecom.PhoneAccountHandle;
+import android.telecom.TelecomManager;
 import android.util.Log;
 
+import java.util.ArrayList;
+import java.util.List;
+
 /**
  * Control class for the car mode app; allows CTS tests to perform operations using the car mode
  * test app.
@@ -97,6 +103,29 @@
         public boolean checkBindStatus(boolean bind) {
             return CtsCarModeInCallService.checkBindStatus(bind);
         }
+
+        @Override
+        public List<PhoneAccountHandle> getSelfManagedPhoneAccounts() {
+            TelecomManager telecomManager = getSystemService(TelecomManager.class);
+            return (telecomManager != null) ? telecomManager.getSelfManagedPhoneAccounts()
+                    : new ArrayList<>();
+        }
+
+        @Override
+        public void registerPhoneAccount(PhoneAccount phoneAccount) {
+            TelecomManager telecomManager = getSystemService(TelecomManager.class);
+            if (telecomManager != null) {
+                telecomManager.registerPhoneAccount(phoneAccount);
+            }
+        }
+
+        @Override
+        public void unregisterPhoneAccount(PhoneAccountHandle phoneAccountHandle) {
+            TelecomManager telecomManager = getSystemService(TelecomManager.class);
+            if (telecomManager != null) {
+                telecomManager.unregisterPhoneAccount(phoneAccountHandle);
+            }
+        }
     };
 
     @Override