Enhance component alias test.
- We no longer use the helper APKs. Removed the "apps" directory.
- Now, we create 3 instrumentation test APKs from the same source files.
They all contain aliases and targets aross each other.
And we run all the 3 test APKs.
This allows us to cover various combination, such as:
- Caller in package A -> Alias in package A -> Target in Package A
- Caller in package A -> Alias in package A -> Target in Package B
- Caller in package A -> Alias in package A -> Target in Package C
- Caller in package A -> Alias in package B -> Target in Package A
- Caller in package A -> Alias in package B -> Target in Package B
- Caller in package A -> Alias in package B -> Target in Package C
....
BYPASS_INCLUSIVE_LANGUAGE_REASON=legacy command name
Bug: 196254758
Test: atest frameworks/base/tests/componentalias
Change-Id: I8e00bfd1a601395641cc5d715a16db11134e81d7
diff --git a/tests/componentalias/Android.bp b/tests/componentalias/Android.bp
index 15a680d..4e2009d 100644
--- a/tests/componentalias/Android.bp
+++ b/tests/componentalias/Android.bp
@@ -16,26 +16,14 @@
default_applicable_licenses: ["Android-Apache-2.0"],
}
-android_library {
- name: "ComponentAliasTestsCommon",
- srcs: [
- "common/**/*.java",
- ],
- plugins: [
- "staledataclass-annotation-processor",
- ],
- platform_apis: true, // We use hidden APIs in the test.
-}
-
-android_test {
- name: "ComponentAliasTests",
+java_defaults {
+ name: "ComponentAliasTests_defaults",
static_libs: [
"androidx.test.rules",
"compatibility-device-util-axt",
"mockito-target-extended-minus-junit4",
"truth-prebuilt",
"ub-uiautomator",
- "ComponentAliasTestsCommon",
],
libs: ["android.test.base"],
srcs: [
@@ -46,3 +34,53 @@
],
platform_apis: true, // We use hidden APIs in the test.
}
+
+// We build three APKs from the exact same source files, so these APKs contain the exact same tests.
+// And we run the tests on each APK, so that we can test various situations:
+// - When the alias is in the same package, target in the same package.
+// - When the alias is in the same package, target in another package.
+// - When the alias is in another package, which also contains the target.
+// - When the alias is in another package, and the target is in yet another package.
+// etc etc...
+
+android_test {
+ name: "ComponentAliasTests",
+ defaults: [
+ "ComponentAliasTests_defaults",
+ ],
+ package_name: "android.content.componentalias.tests",
+ manifest: "AndroidManifest.xml",
+ additional_manifests: [
+ "AndroidManifest_service_aliases.xml",
+ "AndroidManifest_service_targets.xml",
+ ],
+ test_config_template: "AndroidTest-template.xml",
+}
+
+android_test {
+ name: "ComponentAliasTests1",
+ defaults: [
+ "ComponentAliasTests_defaults",
+ ],
+ package_name: "android.content.componentalias.tests.sub1",
+ manifest: "AndroidManifest.xml",
+ additional_manifests: [
+ "AndroidManifest_service_aliases.xml",
+ "AndroidManifest_service_targets.xml",
+ ],
+ test_config_template: "AndroidTest-template.xml",
+}
+
+android_test {
+ name: "ComponentAliasTests2",
+ defaults: [
+ "ComponentAliasTests_defaults",
+ ],
+ package_name: "android.content.componentalias.tests.sub2",
+ manifest: "AndroidManifest.xml",
+ additional_manifests: [
+ "AndroidManifest_service_aliases.xml",
+ "AndroidManifest_service_targets.xml",
+ ],
+ test_config_template: "AndroidTest-template.xml",
+}
diff --git a/tests/componentalias/AndroidManifest_service_aliases.xml b/tests/componentalias/AndroidManifest_service_aliases.xml
new file mode 100644
index 0000000..28ac2a5
--- /dev/null
+++ b/tests/componentalias/AndroidManifest_service_aliases.xml
@@ -0,0 +1,50 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+ * Copyright (C) 2021 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.content.componentalias.tests" >
+ <application>
+ <!--
+ Note the alias components are essentially just placeholders, so the APKs don't have to
+ have the implementation classes.
+ -->
+ <service android:name=".s.Alias00" android:exported="true" android:enabled="true" >
+ <meta-data android:name="alias_target" android:value="android.content.componentalias.tests/android.content.componentalias.tests.s.Target00" />
+ <intent-filter><action android:name="android.intent.action.EXPERIMENTAL_IS_ALIAS" /></intent-filter>
+ <intent-filter><action android:name="android.content.componentalias.tests.IS_ALIAS_00" /></intent-filter>
+ </service>
+ <service android:name=".s.Alias01" android:exported="true" android:enabled="true" >
+ <meta-data android:name="alias_target" android:value="android.content.componentalias.tests.sub1/android.content.componentalias.tests.s.Target01" />
+ <intent-filter><action android:name="android.intent.action.EXPERIMENTAL_IS_ALIAS" /></intent-filter>
+ <intent-filter><action android:name="android.content.componentalias.tests.IS_ALIAS_01" /></intent-filter>
+ </service>
+ <service android:name=".s.Alias02" android:exported="true" android:enabled="true" >
+ <meta-data android:name="alias_target" android:value="android.content.componentalias.tests.sub2/android.content.componentalias.tests.s.Target02" />
+ <intent-filter><action android:name="android.intent.action.EXPERIMENTAL_IS_ALIAS" /></intent-filter>
+ <intent-filter><action android:name="android.content.componentalias.tests.IS_ALIAS_02" /></intent-filter>
+ </service>
+ <service android:name=".s.Alias03" android:exported="true" android:enabled="true" >
+ <meta-data android:name="alias_target" android:value="android.content.componentalias.tests.sub1/android.content.componentalias.tests.s.Target03" />
+ <intent-filter><action android:name="android.intent.action.EXPERIMENTAL_IS_ALIAS" /></intent-filter>
+ <intent-filter><action android:name="android.content.componentalias.tests.IS_ALIAS_03" /></intent-filter>
+ </service>
+ <service android:name=".s.Alias04" android:exported="true" android:enabled="true" >
+ <meta-data android:name="alias_target" android:value="android.content.componentalias.tests.sub2/android.content.componentalias.tests.s.Target04" />
+ <intent-filter><action android:name="android.intent.action.EXPERIMENTAL_IS_ALIAS" /></intent-filter>
+ <intent-filter><action android:name="android.content.componentalias.tests.IS_ALIAS_04" /></intent-filter>
+ </service>
+ </application>
+</manifest>
diff --git a/tests/componentalias/apps/AndroidManifest_sub.xml b/tests/componentalias/AndroidManifest_service_targets.xml
old mode 100755
new mode 100644
similarity index 87%
rename from tests/componentalias/apps/AndroidManifest_sub.xml
rename to tests/componentalias/AndroidManifest_service_targets.xml
index 2ddd965..6e7228c
--- a/tests/componentalias/apps/AndroidManifest_sub.xml
+++ b/tests/componentalias/AndroidManifest_service_targets.xml
@@ -14,13 +14,11 @@
* 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.content.componentalias.tests.app" >
-
+ package="android.content.componentalias.tests" >
<application>
- <!-- Only contain the target components -->
-
+ <service android:name=".s.Target00" android:exported="true" android:enabled="true" >
+ </service>
<service android:name=".s.Target01" android:exported="true" android:enabled="true" >
</service>
<service android:name=".s.Target02" android:exported="true" android:enabled="true" >
@@ -29,7 +27,5 @@
</service>
<service android:name=".s.Target04" android:exported="true" android:enabled="true" >
</service>
- <service android:name=".s.Target05" android:exported="true" android:enabled="true" >
- </service>
</application>
</manifest>
diff --git a/tests/componentalias/AndroidTest.xml b/tests/componentalias/AndroidTest-template.xml
similarity index 68%
rename from tests/componentalias/AndroidTest.xml
rename to tests/componentalias/AndroidTest-template.xml
index e2c37d2..afdfe79 100644
--- a/tests/componentalias/AndroidTest.xml
+++ b/tests/componentalias/AndroidTest-template.xml
@@ -17,18 +17,21 @@
<target_preparer class="com.android.tradefed.targetprep.suite.SuiteApkInstaller">
<option name="cleanup-apks" value="true" />
<option name="test-file-name" value="ComponentAliasTests.apk" />
- <option name="test-file-name" value="ComponentAliasAppMain.apk" />
- <option name="test-file-name" value="ComponentAliasAppSub1.apk" />
- <option name="test-file-name" value="ComponentAliasAppSub2.apk" />
+ <option name="test-file-name" value="ComponentAliasTests1.apk" />
+ <option name="test-file-name" value="ComponentAliasTests2.apk" />
</target_preparer>
<target_preparer class="com.android.tradefed.targetprep.RunCommandTargetPreparer">
<!-- Exempt the helper APKs from the BG restriction, so they can start BG services. -->
- <option name="run-command" value="cmd deviceidle whitelist +android.content.componentalias.tests.app" />
- <option name="run-command" value="cmd deviceidle whitelist +android.content.componentalias.tests.app.sub1" />
- <option name="run-command" value="cmd deviceidle whitelist +android.content.componentalias.tests.app.sub2" />
+ <option name="run-command" value="cmd deviceidle whitelist +android.content.componentalias.tests" />
+ <option name="run-command" value="cmd deviceidle whitelist +android.content.componentalias.tests.sub1" />
+ <option name="run-command" value="cmd deviceidle whitelist +android.content.componentalias.tests.sub2" />
+
+ <option name="teardown-command" value="cmd deviceidle whitelist -android.content.componentalias.tests" />
+ <option name="teardown-command" value="cmd deviceidle whitelist -android.content.componentalias.tests.sub1" />
+ <option name="teardown-command" value="cmd deviceidle whitelist -android.content.componentalias.tests.sub2" />
</target_preparer>
<test class="com.android.tradefed.testtype.AndroidJUnitTest" >
- <option name="package" value="android.content.componentalias.tests" />
+ <option name="package" value="{PACKAGE}" />
<option name="runtime-hint" value="2m" />
<option name="isolated-storage" value="false" />
</test>
diff --git a/tests/componentalias/apps/Android.bp b/tests/componentalias/apps/Android.bp
deleted file mode 100644
index b2cb0f7..0000000
--- a/tests/componentalias/apps/Android.bp
+++ /dev/null
@@ -1,76 +0,0 @@
-// Copyright (C) 2021 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.
-// Copyright (C) 2018 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"],
-}
-
-// We build three helper APKs from the same source.
-// - The main APK (ComponentAliasAppMain), which contains both the alias and the target components.
-// - The sub APKs (ComponentAliasAppSub*), which only contains the target components.
-
-java_defaults {
- name: "component_alias_app_defaults",
- libs: ["android.test.base"],
- sdk_version: "test_current",
- srcs: [
- "src/**/*.java",
- ],
- static_libs: [
- "compatibility-device-util-axt",
- "androidx.legacy_legacy-support-v4",
- "androidx.test.rules",
- "ComponentAliasTestsCommon",
- ],
-}
-
-android_test_helper_app {
- name: "ComponentAliasAppMain",
- defaults: [
- "component_alias_app_defaults",
- ],
- package_name: "android.content.componentalias.tests.app",
- manifest: "AndroidManifest_main.xml",
-}
-
-android_test_helper_app {
- name: "ComponentAliasAppSub1",
- defaults: [
- "component_alias_app_defaults",
- ],
- package_name: "android.content.componentalias.tests.app.sub1",
- manifest: "AndroidManifest_sub.xml",
-}
-
-android_test_helper_app {
- name: "ComponentAliasAppSub2",
- defaults: [
- "component_alias_app_defaults",
- ],
- package_name: "android.content.componentalias.tests.app.sub2",
- manifest: "AndroidManifest_sub.xml",
-}
diff --git a/tests/componentalias/apps/AndroidManifest_main.xml b/tests/componentalias/apps/AndroidManifest_main.xml
deleted file mode 100755
index 2caef3e..0000000
--- a/tests/componentalias/apps/AndroidManifest_main.xml
+++ /dev/null
@@ -1,67 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<!--
- * Copyright (C) 2021 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.content.componentalias.tests.app" >
-
- <application>
- <!--
- Alias components.
- Note aliases do not have the actual implementation, because they're never called
- directly.
- -->
-
- <service android:name=".s.Alias01" android:exported="true" android:enabled="true" >
- <meta-data android:name="alias_target" android:value="android.content.componentalias.tests.app.sub1/android.content.componentalias.tests.app.s.Target01" />
- <intent-filter><action android:name="android.intent.action.EXPERIMENTAL_IS_ALIAS" /></intent-filter>
- <intent-filter><action android:name="android.content.componentalias.tests.app.IS_ALIAS_01" /></intent-filter>
- </service>
- <service android:name=".s.Alias02" android:exported="true" android:enabled="true" >
- <meta-data android:name="alias_target" android:value="android.content.componentalias.tests.app.sub2/android.content.componentalias.tests.app.s.Target02" />
- <intent-filter><action android:name="android.intent.action.EXPERIMENTAL_IS_ALIAS" /></intent-filter>
- <intent-filter><action android:name="android.content.componentalias.tests.app.IS_ALIAS_02" /></intent-filter>
- </service>
- <service android:name=".s.Alias03" android:exported="true" android:enabled="true" >
- <meta-data android:name="alias_target" android:value="android.content.componentalias.tests.app.sub1/android.content.componentalias.tests.app.s.Target03" />
- <intent-filter><action android:name="android.intent.action.EXPERIMENTAL_IS_ALIAS" /></intent-filter>
- <intent-filter><action android:name="android.content.componentalias.tests.app.IS_ALIAS_03" /></intent-filter>
- </service>
- <service android:name=".s.Alias04" android:exported="true" android:enabled="true" >
- <meta-data android:name="alias_target" android:value="android.content.componentalias.tests.app.sub2/android.content.componentalias.tests.app.s.Target04" />
- <intent-filter><action android:name="android.intent.action.EXPERIMENTAL_IS_ALIAS" /></intent-filter>
- <intent-filter><action android:name="android.content.componentalias.tests.app.IS_ALIAS_04" /></intent-filter>
- </service>
- <service android:name=".s.Alias05" android:exported="true" android:enabled="true" >
- <meta-data android:name="alias_target" android:value="android.content.componentalias.tests.app.sub1/android.content.componentalias.tests.app.s.Target05" />
- <intent-filter><action android:name="android.intent.action.EXPERIMENTAL_IS_ALIAS" /></intent-filter>
- <intent-filter><action android:name="android.content.componentalias.tests.app.IS_ALIAS_05" /></intent-filter>
- </service>
-
- <!-- Target components -->
-
- <service android:name=".s.Target01" android:exported="true" android:enabled="true" >
- </service>
- <service android:name=".s.Target02" android:exported="true" android:enabled="true" >
- </service>
- <service android:name=".s.Target03" android:exported="true" android:enabled="true" >
- </service>
- <service android:name=".s.Target04" android:exported="true" android:enabled="true" >
- </service>
- <service android:name=".s.Target05" android:exported="true" android:enabled="true" >
- </service>
- </application>
-</manifest>
diff --git a/tests/componentalias/apps/src/android/content/componentalias/tests/app/s/Target05.java b/tests/componentalias/apps/src/android/content/componentalias/tests/app/s/Target05.java
deleted file mode 100644
index 77060cd..0000000
--- a/tests/componentalias/apps/src/android/content/componentalias/tests/app/s/Target05.java
+++ /dev/null
@@ -1,19 +0,0 @@
-/*
- * Copyright (C) 2021 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.content.componentalias.tests.app.s;
-
-public class Target05 extends BaseService {
-}
diff --git a/tests/componentalias/common/android/content/componentalias/tests/common/ComponentAliasMessage.java b/tests/componentalias/src/android/content/componentalias/tests/ComponentAliasMessage.java
similarity index 94%
rename from tests/componentalias/common/android/content/componentalias/tests/common/ComponentAliasMessage.java
rename to tests/componentalias/src/android/content/componentalias/tests/ComponentAliasMessage.java
index 399c070..d41696f 100644
--- a/tests/componentalias/common/android/content/componentalias/tests/common/ComponentAliasMessage.java
+++ b/tests/componentalias/src/android/content/componentalias/tests/ComponentAliasMessage.java
@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package android.content.componentalias.tests.common;
+package android.content.componentalias.tests;
import android.annotation.NonNull;
import android.annotation.Nullable;
@@ -28,7 +28,7 @@
* Parcelabe containing a "message" that's meant to be delivered via BroadcastMessenger.
*
* To add a new field, just add a private member field, and run:
- * codegen $ANDROID_BUILD_TOP/frameworks/base/tests/componentalias/common/android/content/componentalias/tests/common/ComponentAliasMessage.java
+ * codegen $ANDROID_BUILD_TOP/frameworks/base/tests/componentalias/src/android/content/componentalias/tests/ComponentAliasMessage.java
*/
@DataClass(
genConstructor = false,
@@ -62,7 +62,7 @@
// CHECKSTYLE:OFF Generated code
//
// To regenerate run:
- // $ codegen $ANDROID_BUILD_TOP/frameworks/base/tests/componentalias/common/android/content/componentalias/tests/common/ComponentAliasMessage.java
+ // $ codegen $ANDROID_BUILD_TOP/frameworks/base/tests/componentalias/src/android/content/componentalias/tests/ComponentAliasMessage.java
//
// To exclude the generated code from IntelliJ auto-formatting enable (one-time):
// Settings > Editor > Code Style > Formatter Control
@@ -201,9 +201,9 @@
};
@DataClass.Generated(
- time = 1629137098129L,
+ time = 1630098801203L,
codegenVersion = "1.0.23",
- sourceFile = "frameworks/base/tests/componentalias/common/android/content/componentalias/tests/common/ComponentAliasMessage.java",
+ sourceFile = "frameworks/base/tests/componentalias/src/android/content/componentalias/tests/ComponentAliasMessage.java",
inputSignatures = "private @android.annotation.Nullable java.lang.String mMessage\nprivate @android.annotation.Nullable java.lang.String mMethodName\nprivate @android.annotation.Nullable java.lang.String mSenderIdentity\nprivate @android.annotation.Nullable android.content.Intent mIntent\nprivate @android.annotation.Nullable android.content.ComponentName mComponent\nclass ComponentAliasMessage extends java.lang.Object implements [android.os.Parcelable]\n@com.android.internal.util.DataClass(genConstructor=false, genSetters=true, genToString=true, genAidl=false)")
@Deprecated
private void __metadata() {}
diff --git a/tests/componentalias/src/android/content/componentalias/tests/ComponentAliasServiceTest.java b/tests/componentalias/src/android/content/componentalias/tests/ComponentAliasServiceTest.java
index 81fc9bf..af51c32 100644
--- a/tests/componentalias/src/android/content/componentalias/tests/ComponentAliasServiceTest.java
+++ b/tests/componentalias/src/android/content/componentalias/tests/ComponentAliasServiceTest.java
@@ -17,18 +17,19 @@
package android.content.componentalias.tests;
import static android.content.Context.BIND_AUTO_CREATE;
-import static android.content.componentalias.tests.common.ComponentAliasTestCommon.APP_PACKAGE;
-import static android.content.componentalias.tests.common.ComponentAliasTestCommon.SUB1_PACKAGE;
-import static android.content.componentalias.tests.common.ComponentAliasTestCommon.SUB2_PACKAGE;
-import static android.content.componentalias.tests.common.ComponentAliasTestCommon.TAG;
+import static android.content.componentalias.tests.ComponentAliasTestCommon.MAIN_PACKAGE;
+import static android.content.componentalias.tests.ComponentAliasTestCommon.SUB1_PACKAGE;
+import static android.content.componentalias.tests.ComponentAliasTestCommon.SUB2_PACKAGE;
+import static android.content.componentalias.tests.ComponentAliasTestCommon.TAG;
import static com.google.common.truth.Truth.assertThat;
+import static org.hamcrest.core.IsNot.not;
+
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
-import android.content.componentalias.tests.common.ComponentAliasMessage;
import android.os.IBinder;
import android.provider.DeviceConfig;
import android.util.Log;
@@ -42,9 +43,12 @@
import com.android.compatibility.common.util.TestUtils;
import org.junit.AfterClass;
+import org.junit.Assume;
import org.junit.Before;
import org.junit.Test;
+import java.util.function.Consumer;
+
/**
* Test for the experimental "Component alias" feature.
*
@@ -92,7 +96,7 @@
.setMethodName("onServiceConnected")
.setComponent(name);
- BroadcastMessenger.send(sContext, sContext.getPackageName(), m);
+ BroadcastMessenger.send(sContext, TAG, m);
}
@Override
@@ -104,7 +108,7 @@
.setMethodName("onServiceDisconnected")
.setComponent(name);
- BroadcastMessenger.send(sContext, sContext.getPackageName(), m);
+ BroadcastMessenger.send(sContext, TAG, m);
}
@Override
@@ -115,7 +119,7 @@
.setSenderIdentity("sServiceConnection")
.setMethodName("onBindingDied");
- BroadcastMessenger.send(sContext, sContext.getPackageName(), m);
+ BroadcastMessenger.send(sContext, TAG, m);
}
@Override
@@ -126,18 +130,18 @@
.setSenderIdentity("sServiceConnection")
.setMethodName("onNullBinding");
- BroadcastMessenger.send(sContext, sContext.getPackageName(), m);
+ BroadcastMessenger.send(sContext, TAG, m);
}
};
private void testStartAndStopService_common(
Intent originalIntent,
ComponentName componentNameForClient,
- ComponentName componentNameForTarget) throws Exception {
+ ComponentName componentNameForTarget) {
ComponentAliasMessage m;
- try (Receiver<ComponentAliasMessage> receiver = new Receiver<>(sContext)) {
+ try (Receiver<ComponentAliasMessage> receiver = new Receiver<>(sContext, TAG)) {
// Start the service.
ComponentName result = sContext.startService(originalIntent);
assertThat(result).isEqualTo(componentNameForClient);
@@ -167,40 +171,64 @@
}
}
+ private static class Combo {
+ public final ComponentName alias;
+ public final ComponentName target;
+ public final String action;
+
+ private Combo(ComponentName alias, ComponentName target, String action) {
+ this.alias = alias;
+ this.target = target;
+ this.action = action;
+ }
+ }
+
+ private void forEachCombo(Consumer<Combo> callback) {
+ callback.accept(new Combo(
+ new ComponentName(MAIN_PACKAGE, MAIN_PACKAGE + ".s.Alias00"),
+ new ComponentName(MAIN_PACKAGE, MAIN_PACKAGE + ".s.Target00"),
+ MAIN_PACKAGE + ".IS_ALIAS_00"));
+ callback.accept(new Combo(
+ new ComponentName(MAIN_PACKAGE, MAIN_PACKAGE + ".s.Alias01"),
+ new ComponentName(SUB1_PACKAGE, MAIN_PACKAGE + ".s.Target01"),
+ MAIN_PACKAGE + ".IS_ALIAS_01"));
+ callback.accept(new Combo(
+ new ComponentName(MAIN_PACKAGE, MAIN_PACKAGE + ".s.Alias02"),
+ new ComponentName(SUB2_PACKAGE, MAIN_PACKAGE + ".s.Target02"),
+ MAIN_PACKAGE + ".IS_ALIAS_02"));
+ }
+
+
@Test
public void testStartAndStopService_explicitComponentName() throws Exception {
- Intent i = new Intent().setComponent(
- new ComponentName(APP_PACKAGE, APP_PACKAGE + ".s.Alias01"));
-
- ComponentName alias = new ComponentName(APP_PACKAGE, APP_PACKAGE + ".s.Alias01");
- ComponentName target = new ComponentName(SUB1_PACKAGE, APP_PACKAGE + ".s.Target01");
-
- testStartAndStopService_common(i, alias, target);
+ forEachCombo((c) -> {
+ Intent i = new Intent().setComponent(c.alias);
+ testStartAndStopService_common(i, c.alias, c.target);
+ });
}
@Test
public void testStartAndStopService_explicitPackageName() throws Exception {
- Intent i = new Intent().setPackage(APP_PACKAGE);
- i.setAction(APP_PACKAGE + ".IS_ALIAS_02");
+ forEachCombo((c) -> {
+ Intent i = new Intent().setPackage(c.alias.getPackageName());
+ i.setAction(c.action);
- ComponentName alias = new ComponentName(APP_PACKAGE, APP_PACKAGE + ".s.Alias02");
- ComponentName target = new ComponentName(SUB2_PACKAGE, APP_PACKAGE + ".s.Target02");
-
- testStartAndStopService_common(i, alias, target);
+ testStartAndStopService_common(i, c.alias, c.target);
+ });
}
@Test
public void testStartAndStopService_override() throws Exception {
- Intent i = new Intent().setPackage(APP_PACKAGE);
- i.setAction(APP_PACKAGE + ".IS_ALIAS_01");
+ Intent i = new Intent().setPackage(MAIN_PACKAGE);
+ i.setAction(MAIN_PACKAGE + ".IS_ALIAS_01");
// Change some of the aliases from what's defined in <meta-data>.
- ComponentName aliasA = new ComponentName(APP_PACKAGE, APP_PACKAGE + ".s.Alias01");
- ComponentName targetA = new ComponentName(APP_PACKAGE, APP_PACKAGE + ".s.Target02");
+ ComponentName aliasA = new ComponentName(MAIN_PACKAGE, MAIN_PACKAGE + ".s.Alias01");
+ ComponentName targetA = new ComponentName(MAIN_PACKAGE, MAIN_PACKAGE + ".s.Target02");
- ComponentName aliasB = new ComponentName(APP_PACKAGE, APP_PACKAGE + ".s.Alias02");
- ComponentName targetB = new ComponentName(APP_PACKAGE, APP_PACKAGE + ".s.Target01");
+ ComponentName aliasB = new ComponentName(MAIN_PACKAGE, MAIN_PACKAGE + ".s.Alias02");
+ ComponentName targetB = new ComponentName(MAIN_PACKAGE, MAIN_PACKAGE + ".s.Target01");
sDeviceConfig.set("component_alias_overrides",
aliasA.flattenToShortString() + ":" + targetA.flattenToShortString()
@@ -220,10 +248,10 @@
private void testBindAndUnbindService_common(
Intent originalIntent,
ComponentName componentNameForClient,
- ComponentName componentNameForTarget) throws Exception {
+ ComponentName componentNameForTarget) {
ComponentAliasMessage m;
- try (Receiver<ComponentAliasMessage> receiver = new Receiver<>(sContext)) {
+ try (Receiver<ComponentAliasMessage> receiver = new Receiver<>(sContext, TAG)) {
// Bind to the service.
assertThat(sContext.bindService(
originalIntent, sServiceConnection, BIND_AUTO_CREATE)).isTrue();
@@ -263,37 +291,45 @@
@Test
public void testBindService_explicitComponentName() throws Exception {
- Intent i = new Intent().setComponent(
- new ComponentName(APP_PACKAGE, APP_PACKAGE + ".s.Alias01"));
+ forEachCombo((c) -> {
+ Intent i = new Intent().setComponent(c.alias);
- testBindAndUnbindService_common(i,
- new ComponentName(APP_PACKAGE, APP_PACKAGE + ".s.Alias01"),
- new ComponentName(SUB1_PACKAGE, APP_PACKAGE + ".s.Target01"));
+ testBindAndUnbindService_common(i, c.alias, c.target);
+ });
+
}
@Test
public void testBindService_explicitPackageName() throws Exception {
- Intent i = new Intent().setPackage(APP_PACKAGE);
- i.setAction(APP_PACKAGE + ".IS_ALIAS_02");
+ forEachCombo((c) -> {
+ Intent i = new Intent().setPackage(c.alias.getPackageName());
+ i.setAction(c.action);
- testBindAndUnbindService_common(i,
- new ComponentName(APP_PACKAGE, APP_PACKAGE + ".s.Alias02"),
- new ComponentName(SUB2_PACKAGE, APP_PACKAGE + ".s.Target02"));
+ testBindAndUnbindService_common(i, c.alias, c.target);
+ });
}
+ /**
+ * Make sure, when the service process is killed, the client will get a callback with the
+ * right component name.
+ */
@Test
public void testBindService_serviceKilled() throws Exception {
- Intent originalIntent = new Intent().setPackage(APP_PACKAGE);
- originalIntent.setAction(APP_PACKAGE + ".IS_ALIAS_02");
+
+ // We need to kill SUB2_PACKAGE, don't run it for this package.
+ Assume.assumeThat(sContext.getPackageName(), not(SUB2_PACKAGE));
+
+ Intent originalIntent = new Intent().setPackage(MAIN_PACKAGE);
+ originalIntent.setAction(MAIN_PACKAGE + ".IS_ALIAS_02");
final ComponentName componentNameForClient =
- new ComponentName(APP_PACKAGE, APP_PACKAGE + ".s.Alias02");
+ new ComponentName(MAIN_PACKAGE, MAIN_PACKAGE + ".s.Alias02");
final ComponentName componentNameForTarget =
- new ComponentName(SUB2_PACKAGE, APP_PACKAGE + ".s.Target02");
+ new ComponentName(SUB2_PACKAGE, MAIN_PACKAGE + ".s.Target02");
ComponentAliasMessage m;
- try (Receiver<ComponentAliasMessage> receiver = new Receiver<>(sContext)) {
+ try (Receiver<ComponentAliasMessage> receiver = new Receiver<>(sContext, TAG)) {
// Bind to the service.
assertThat(sContext.bindService(
originalIntent, sServiceConnection, BIND_AUTO_CREATE)).isTrue();
@@ -306,6 +342,7 @@
m = receiver.waitForNextMessage();
assertThat(m.getMethodName()).isEqualTo("onServiceConnected");
assertThat(m.getComponent()).isEqualTo(componentNameForClient);
+ // We don't need to check all the fields because these are tested else where.
// Now kill the service process.
ShellUtils.runShellCommand("su 0 killall %s", SUB2_PACKAGE);
diff --git a/tests/componentalias/common/android/content/componentalias/tests/common/ComponentAliasTestCommon.java b/tests/componentalias/src/android/content/componentalias/tests/ComponentAliasTestCommon.java
similarity index 77%
rename from tests/componentalias/common/android/content/componentalias/tests/common/ComponentAliasTestCommon.java
rename to tests/componentalias/src/android/content/componentalias/tests/ComponentAliasTestCommon.java
index f306072..165d728 100644
--- a/tests/componentalias/common/android/content/componentalias/tests/common/ComponentAliasTestCommon.java
+++ b/tests/componentalias/src/android/content/componentalias/tests/ComponentAliasTestCommon.java
@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package android.content.componentalias.tests.common;
+package android.content.componentalias.tests;
public final class ComponentAliasTestCommon {
private ComponentAliasTestCommon() {
@@ -21,9 +21,8 @@
public static final String TAG = "ComponentAliasTest";
- public static final String TEST_PACKAGE = "android.content.componentalias.tests";
+ public static final String MAIN_PACKAGE = "android.content.componentalias.tests";
- public static final String APP_PACKAGE = "android.content.componentalias.tests.app";
- public static final String SUB1_PACKAGE = "android.content.componentalias.tests.app.sub1";
- public static final String SUB2_PACKAGE = "android.content.componentalias.tests.app.sub2";
+ public static final String SUB1_PACKAGE = "android.content.componentalias.tests.sub1";
+ public static final String SUB2_PACKAGE = "android.content.componentalias.tests.sub2";
}
diff --git a/tests/componentalias/apps/src/android/content/componentalias/tests/app/s/BaseService.java b/tests/componentalias/src/android/content/componentalias/tests/s/BaseService.java
similarity index 81%
rename from tests/componentalias/apps/src/android/content/componentalias/tests/app/s/BaseService.java
rename to tests/componentalias/src/android/content/componentalias/tests/s/BaseService.java
index fb67829..535d9b8 100644
--- a/tests/componentalias/apps/src/android/content/componentalias/tests/app/s/BaseService.java
+++ b/tests/componentalias/src/android/content/componentalias/tests/s/BaseService.java
@@ -13,15 +13,14 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package android.content.componentalias.tests.app.s;
+package android.content.componentalias.tests.s;
-import static android.content.componentalias.tests.common.ComponentAliasTestCommon.TAG;
-import static android.content.componentalias.tests.common.ComponentAliasTestCommon.TEST_PACKAGE;
+import static android.content.componentalias.tests.ComponentAliasTestCommon.TAG;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
-import android.content.componentalias.tests.common.ComponentAliasMessage;
+import android.content.componentalias.tests.ComponentAliasMessage;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
@@ -41,7 +40,7 @@
.setSenderIdentity(getMyIdentity())
.setMethodName("onStartCommand")
.setIntent(intent);
- BroadcastMessenger.send(this, TEST_PACKAGE, m);
+ BroadcastMessenger.send(this, TAG, m);
return START_NOT_STICKY;
}
@@ -53,7 +52,7 @@
ComponentAliasMessage m = new ComponentAliasMessage()
.setSenderIdentity(getMyIdentity())
.setMethodName("onDestroy");
- BroadcastMessenger.send(this, TEST_PACKAGE, m);
+ BroadcastMessenger.send(this, TAG, m);
}
@Override
@@ -64,7 +63,7 @@
.setSenderIdentity(getMyIdentity())
.setMethodName("onBind")
.setIntent(intent);
- BroadcastMessenger.send(this, TEST_PACKAGE, m);
+ BroadcastMessenger.send(this, TAG, m);
return new Binder();
}
diff --git a/tests/componentalias/apps/src/android/content/componentalias/tests/app/s/Target04.java b/tests/componentalias/src/android/content/componentalias/tests/s/Target00.java
similarity index 86%
copy from tests/componentalias/apps/src/android/content/componentalias/tests/app/s/Target04.java
copy to tests/componentalias/src/android/content/componentalias/tests/s/Target00.java
index 0e51a6b..64b91f5 100644
--- a/tests/componentalias/apps/src/android/content/componentalias/tests/app/s/Target04.java
+++ b/tests/componentalias/src/android/content/componentalias/tests/s/Target00.java
@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package android.content.componentalias.tests.app.s;
+package android.content.componentalias.tests.s;
-public class Target04 extends BaseService {
+public class Target00 extends BaseService {
}
diff --git a/tests/componentalias/apps/src/android/content/componentalias/tests/app/s/Target01.java b/tests/componentalias/src/android/content/componentalias/tests/s/Target01.java
similarity index 92%
rename from tests/componentalias/apps/src/android/content/componentalias/tests/app/s/Target01.java
rename to tests/componentalias/src/android/content/componentalias/tests/s/Target01.java
index 87e48cb..bd58999 100644
--- a/tests/componentalias/apps/src/android/content/componentalias/tests/app/s/Target01.java
+++ b/tests/componentalias/src/android/content/componentalias/tests/s/Target01.java
@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package android.content.componentalias.tests.app.s;
+package android.content.componentalias.tests.s;
public class Target01 extends BaseService {
}
diff --git a/tests/componentalias/apps/src/android/content/componentalias/tests/app/s/Target02.java b/tests/componentalias/src/android/content/componentalias/tests/s/Target02.java
similarity index 92%
rename from tests/componentalias/apps/src/android/content/componentalias/tests/app/s/Target02.java
rename to tests/componentalias/src/android/content/componentalias/tests/s/Target02.java
index 0e8a6a8..0ddf818 100644
--- a/tests/componentalias/apps/src/android/content/componentalias/tests/app/s/Target02.java
+++ b/tests/componentalias/src/android/content/componentalias/tests/s/Target02.java
@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package android.content.componentalias.tests.app.s;
+package android.content.componentalias.tests.s;
public class Target02 extends BaseService {
}
diff --git a/tests/componentalias/apps/src/android/content/componentalias/tests/app/s/Target03.java b/tests/componentalias/src/android/content/componentalias/tests/s/Target03.java
similarity index 92%
rename from tests/componentalias/apps/src/android/content/componentalias/tests/app/s/Target03.java
rename to tests/componentalias/src/android/content/componentalias/tests/s/Target03.java
index b7990bb..0dbc050 100644
--- a/tests/componentalias/apps/src/android/content/componentalias/tests/app/s/Target03.java
+++ b/tests/componentalias/src/android/content/componentalias/tests/s/Target03.java
@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package android.content.componentalias.tests.app.s;
+package android.content.componentalias.tests.s;
public class Target03 extends BaseService {
}
diff --git a/tests/componentalias/apps/src/android/content/componentalias/tests/app/s/Target04.java b/tests/componentalias/src/android/content/componentalias/tests/s/Target04.java
similarity index 92%
rename from tests/componentalias/apps/src/android/content/componentalias/tests/app/s/Target04.java
rename to tests/componentalias/src/android/content/componentalias/tests/s/Target04.java
index 0e51a6b..0994258 100644
--- a/tests/componentalias/apps/src/android/content/componentalias/tests/app/s/Target04.java
+++ b/tests/componentalias/src/android/content/componentalias/tests/s/Target04.java
@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package android.content.componentalias.tests.app.s;
+package android.content.componentalias.tests.s;
public class Target04 extends BaseService {
}
diff --git a/tests/componentalias/common/com/android/compatibility/common/util/BroadcastMessenger.java b/tests/componentalias/src/com/android/compatibility/common/util/BroadcastMessenger.java
similarity index 68%
rename from tests/componentalias/common/com/android/compatibility/common/util/BroadcastMessenger.java
rename to tests/componentalias/src/com/android/compatibility/common/util/BroadcastMessenger.java
index 175082e..e01c2ef 100644
--- a/tests/componentalias/common/com/android/compatibility/common/util/BroadcastMessenger.java
+++ b/tests/componentalias/src/com/android/compatibility/common/util/BroadcastMessenger.java
@@ -16,6 +16,7 @@
package com.android.compatibility.common.util;
import android.annotation.NonNull;
+import android.annotation.Nullable;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
@@ -25,23 +26,27 @@
import android.os.SystemClock;
import android.util.Log;
-import com.android.internal.annotations.GuardedBy;
-import com.android.internal.util.Preconditions;
-
import java.util.ArrayList;
+import java.util.Objects;
/**
* Provides a one-way communication mechanism using a Parcelable as a payload, via broadcasts.
*
+ * Use {@link #send(Context, String, Parcelable)} to send a message.
+ * USe {@link Receiver} to receive a message.
+ *
+ * Pick a unique "suffix" for your test, and use it with both the sender and receiver, in order
+ * to avoid "cross-talks" between different tests. (if they ever run at the same time.)
+ *
* TODO: Move it to compatibility-device-util-axt.
*/
public final class BroadcastMessenger {
private static final String TAG = "BroadcastMessenger";
private static final String ACTION_MESSAGE =
- "com.android.compatibility.common.util.BroadcastMessenger.ACTION_MESSAGE";
+ "com.android.compatibility.common.util.BroadcastMessenger.ACTION_MESSAGE_";
private static final String ACTION_PING =
- "com.android.compatibility.common.util.BroadcastMessenger.ACTION_PING";
+ "com.android.compatibility.common.util.BroadcastMessenger.ACTION_PING_";
private static final String EXTRA_MESSAGE =
"com.android.compatibility.common.util.BroadcastMessenger.EXTRA_MESSAGE";
@@ -56,8 +61,8 @@
return SystemClock.uptimeMillis();
}
- private static void sendBroadcast(@NonNull Intent i,
- @NonNull Context context, @NonNull String receiverPackage) {
+ private static void sendBroadcast(@NonNull Intent i, @NonNull Context context,
+ @NonNull String broadcastSuffix, @Nullable String receiverPackage) {
i.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
i.setPackage(receiverPackage);
i.putExtra(EXTRA_SENT_TIME, getCurrentTime());
@@ -65,21 +70,22 @@
context.sendBroadcast(i);
}
- /** Send a message to the {@link Receiver} in a given package. */
+ /** Send a message to the {@link Receiver} expecting a given "suffix". */
public static <T extends Parcelable> void send(@NonNull Context context,
- @NonNull String receiverPackage, @NonNull T message) {
- final Intent i = new Intent(ACTION_MESSAGE);
- i.putExtra(EXTRA_MESSAGE, Preconditions.checkNotNull(message));
+ @NonNull String broadcastSuffix, @NonNull T message) {
+ final Intent i = new Intent(ACTION_MESSAGE + Objects.requireNonNull(broadcastSuffix));
+ i.putExtra(EXTRA_MESSAGE, Objects.requireNonNull(message));
Log.i(TAG, "Sending: " + message);
- sendBroadcast(i, context, receiverPackage);
+ sendBroadcast(i, context, broadcastSuffix, /*receiverPackage=*/ null);
}
- private static void sendPing(@NonNull Context context, @NonNull String receiverPackage) {
- final Intent i = new Intent(ACTION_PING);
+ private static void sendPing(@NonNull Context context,@NonNull String broadcastSuffix,
+ @NonNull String receiverPackage) {
+ final Intent i = new Intent(ACTION_PING + Objects.requireNonNull(broadcastSuffix));
Log.i(TAG, "Sending a ping");
- sendBroadcast(i, context, receiverPackage);
+ sendBroadcast(i, context, broadcastSuffix, receiverPackage);
}
/**
@@ -88,9 +94,10 @@
*/
public static final class Receiver<T extends Parcelable> implements AutoCloseable {
private final Context mContext;
+ private final String mBroadcastSuffix;
private final HandlerThread mReceiverThread = new HandlerThread(TAG);
- @GuardedBy("mMessages")
+ // @GuardedBy("mMessages")
private final ArrayList<T> mMessages = new ArrayList<>();
private final long mCreatedTime = getCurrentTime();
private boolean mRegistered;
@@ -99,12 +106,11 @@
@Override
public void onReceive(Context context, Intent intent) {
// Log.d(TAG, "Received intent: " + intent);
- switch (intent.getAction()) {
- case ACTION_MESSAGE:
- case ACTION_PING:
- break;
- default:
- throw new RuntimeException("Unknown broadcast received: " + intent);
+ if (intent.getAction().equals(ACTION_MESSAGE + mBroadcastSuffix)
+ || intent.getAction().equals(ACTION_PING + mBroadcastSuffix)) {
+ // OK
+ } else {
+ throw new RuntimeException("Unknown broadcast received: " + intent);
}
if (intent.getLongExtra(EXTRA_SENT_TIME, 0) < mCreatedTime) {
Log.i(TAG, "Dropping stale broadcast: " + intent);
@@ -127,16 +133,17 @@
/**
* Constructor.
*/
- public Receiver(@NonNull Context context) {
+ public Receiver(@NonNull Context context, @NonNull String broadcastSuffix) {
mContext = context;
+ mBroadcastSuffix = Objects.requireNonNull(broadcastSuffix);
mReceiverThread.start();
- final IntentFilter fi = new IntentFilter(ACTION_MESSAGE);
- fi.addAction(ACTION_PING);
+ final IntentFilter fi = new IntentFilter(ACTION_MESSAGE + mBroadcastSuffix);
+ fi.addAction(ACTION_PING + mBroadcastSuffix);
- context.registerReceiver(mReceiver, fi, /** permission=*/ null,
- mReceiverThread.getThreadHandler());
+ context.registerReceiver(mReceiver, fi, /* permission=*/ null,
+ mReceiverThread.getThreadHandler(), Context.RECEIVER_EXPORTED);
mRegistered = true;
}
@@ -153,7 +160,7 @@
* Receive the next message with a 60 second timeout.
*/
@NonNull
- public T waitForNextMessage() throws Exception {
+ public T waitForNextMessage() {
return waitForNextMessage(60_000);
}
@@ -161,7 +168,7 @@
* Receive the next message.
*/
@NonNull
- public T waitForNextMessage(long timeoutMillis) throws Exception {
+ public T waitForNextMessage(long timeoutMillis) {
synchronized (mMessages) {
final long timeout = System.currentTimeMillis() + timeoutMillis;
while (mMessages.size() == 0) {
@@ -169,7 +176,11 @@
if (wait <= 0) {
throw new RuntimeException("Timeout waiting for the next message");
}
- mMessages.wait(wait);
+ try {
+ mMessages.wait(wait);
+ } catch (InterruptedException e) {
+ throw new RuntimeException(e);
+ }
}
return mMessages.remove(0);
}
@@ -180,9 +191,9 @@
*
* Call it before {@link #close()}.
*/
- public void ensureNoMoreMessages() throws Exception {
+ public void ensureNoMoreMessages() {
// Send a ping to myself.
- sendPing(mContext, mContext.getPackageName());
+ sendPing(mContext, mBroadcastSuffix, mContext.getPackageName());
final T m = waitForNextMessage();
if (m == null) {