diff options
-rw-r--r-- | core/java/android/content/pm/PackageBackwardCompatibility.java | 78 | ||||
-rw-r--r-- | core/java/android/content/pm/PackageParser.java | 14 | ||||
-rw-r--r-- | core/tests/coretests/README | 2 | ||||
-rw-r--r-- | core/tests/coretests/src/android/content/pm/PackageBackwardCompatibilityTest.java | 114 | ||||
-rw-r--r-- | test-runner/Android.mk | 10 |
5 files changed, 202 insertions, 16 deletions
diff --git a/core/java/android/content/pm/PackageBackwardCompatibility.java b/core/java/android/content/pm/PackageBackwardCompatibility.java new file mode 100644 index 000000000000..4de160b0bf88 --- /dev/null +++ b/core/java/android/content/pm/PackageBackwardCompatibility.java @@ -0,0 +1,78 @@ +/* + * Copyright (C) 2017 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.pm; + +import android.annotation.Nullable; +import android.content.pm.PackageParser.Package; + +import com.android.internal.annotations.VisibleForTesting; +import com.android.internal.util.ArrayUtils; + +import java.util.ArrayList; + +/** + * Modifies {@link Package} in order to maintain backwards compatibility. + * + * @hide + */ +@VisibleForTesting +public class PackageBackwardCompatibility { + + private static final String ANDROID_TEST_MOCK = "android.test.mock"; + + private static final String ANDROID_TEST_RUNNER = "android.test.runner"; + + /** + * Modify the shared libraries in the supplied {@link Package} to maintain backwards + * compatibility. + * + * @param pkg the {@link Package} to modify. + */ + @VisibleForTesting + public static void modifySharedLibraries(Package pkg) { + ArrayList<String> usesLibraries = pkg.usesLibraries; + ArrayList<String> usesOptionalLibraries = pkg.usesOptionalLibraries; + + usesLibraries = orgApacheHttpLegacy(usesLibraries); + usesOptionalLibraries = orgApacheHttpLegacy(usesOptionalLibraries); + + // android.test.runner has a dependency on android.test.mock so if android.test.runner + // is present but android.test.mock is not then add android.test.mock. + boolean androidTestMockPresent = ArrayUtils.contains(usesLibraries, ANDROID_TEST_MOCK) + || ArrayUtils.contains(usesOptionalLibraries, ANDROID_TEST_MOCK); + if (ArrayUtils.contains(usesLibraries, ANDROID_TEST_RUNNER) && !androidTestMockPresent) { + usesLibraries.add(ANDROID_TEST_MOCK); + } + if (ArrayUtils.contains(usesOptionalLibraries, ANDROID_TEST_RUNNER) + && !androidTestMockPresent) { + usesOptionalLibraries.add(ANDROID_TEST_MOCK); + } + + pkg.usesLibraries = usesLibraries; + pkg.usesOptionalLibraries = usesOptionalLibraries; + } + + private static ArrayList<String> orgApacheHttpLegacy(@Nullable ArrayList<String> libraries) { + // "org.apache.http.legacy" is now a part of the boot classpath so it doesn't need + // to be an explicit dependency. + // + // A future change will remove this library from the boot classpath, at which point + // all apps that target SDK 21 and earlier will have it automatically added to their + // dependency lists. + return ArrayUtils.remove(libraries, "org.apache.http.legacy"); + } +} diff --git a/core/java/android/content/pm/PackageParser.java b/core/java/android/content/pm/PackageParser.java index 426c4f26945c..a37e89ebcad1 100644 --- a/core/java/android/content/pm/PackageParser.java +++ b/core/java/android/content/pm/PackageParser.java @@ -3846,7 +3846,7 @@ public class PackageParser { // every activity info has had a chance to set it from its attributes. setMaxAspectRatio(owner); - modifySharedLibrariesForBackwardCompatibility(owner); + PackageBackwardCompatibility.modifySharedLibraries(owner); if (hasDomainURLs(owner)) { owner.applicationInfo.privateFlags |= ApplicationInfo.PRIVATE_FLAG_HAS_DOMAIN_URLS; @@ -3857,18 +3857,6 @@ public class PackageParser { return true; } - private static void modifySharedLibrariesForBackwardCompatibility(Package owner) { - // "org.apache.http.legacy" is now a part of the boot classpath so it doesn't need - // to be an explicit dependency. - // - // A future change will remove this library from the boot classpath, at which point - // all apps that target SDK 21 and earlier will have it automatically added to their - // dependency lists. - owner.usesLibraries = ArrayUtils.remove(owner.usesLibraries, "org.apache.http.legacy"); - owner.usesOptionalLibraries = ArrayUtils.remove(owner.usesOptionalLibraries, - "org.apache.http.legacy"); - } - /** * Check if one of the IntentFilter as both actions DEFAULT / VIEW and a HTTP/HTTPS data URI */ diff --git a/core/tests/coretests/README b/core/tests/coretests/README index aced4417ba02..ea282a0ebb3a 100644 --- a/core/tests/coretests/README +++ b/core/tests/coretests/README @@ -28,7 +28,7 @@ To run a test or set of tests, first build the FrameworksCoreTests package: Next, install the resulting APK and run tests as you would normal JUnit tests: - adb install out/target/product/.../data/app/FrameworksCoreTests/FrameworksCoreTests.apk + adb install -r ${ANDROID_PRODUCT_OUT}/data/app/FrameworksCoreTests/FrameworksCoreTests.apk adb shell am instrument -w \ com.android.frameworks.coretests/android.support.test.runner.AndroidJUnitRunner diff --git a/core/tests/coretests/src/android/content/pm/PackageBackwardCompatibilityTest.java b/core/tests/coretests/src/android/content/pm/PackageBackwardCompatibilityTest.java new file mode 100644 index 000000000000..4d2a0470b2ea --- /dev/null +++ b/core/tests/coretests/src/android/content/pm/PackageBackwardCompatibilityTest.java @@ -0,0 +1,114 @@ +/* + * Copyright (C) 2017 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.pm; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +import android.content.pm.PackageParser.Package; +import android.os.Build; +import android.support.test.filters.SmallTest; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +import java.util.ArrayList; +import java.util.Collections; + +@SmallTest +@RunWith(JUnit4.class) +public class PackageBackwardCompatibilityTest { + + private static final String ORG_APACHE_HTTP_LEGACY = "org.apache.http.legacy"; + + private static final String ANDROID_TEST_RUNNER = "android.test.runner"; + + private static final String ANDROID_TEST_MOCK = "android.test.mock"; + + private Package mPackage; + + private static ArrayList<String> arrayList(String... strings) { + ArrayList<String> list = new ArrayList<>(); + Collections.addAll(list, strings); + return list; + } + + @Before + public void setUp() { + mPackage = new Package("org.package.name"); + mPackage.applicationInfo.targetSdkVersion = Build.VERSION_CODES.CUR_DEVELOPMENT; + } + + @Test + public void null_usesLibraries() { + PackageBackwardCompatibility.modifySharedLibraries(mPackage); + assertNull("usesLibraries not updated correctly", mPackage.usesLibraries); + } + + @Test + public void null_usesOptionalLibraries() { + PackageBackwardCompatibility.modifySharedLibraries(mPackage); + assertNull("usesOptionalLibraries not updated correctly", mPackage.usesOptionalLibraries); + } + + @Test + public void remove_org_apache_http_legacy_from_usesLibraries() { + mPackage.usesLibraries = arrayList(ORG_APACHE_HTTP_LEGACY); + PackageBackwardCompatibility.modifySharedLibraries(mPackage); + assertNull("usesLibraries not updated correctly", mPackage.usesLibraries); + } + + @Test + public void remove_org_apache_http_legacy_from_usesOptionalLibraries() { + mPackage.usesOptionalLibraries = arrayList(ORG_APACHE_HTTP_LEGACY); + PackageBackwardCompatibility.modifySharedLibraries(mPackage); + assertNull("usesOptionalLibraries not updated correctly", mPackage.usesOptionalLibraries); + } + + @Test + public void android_test_runner_in_usesLibraries() { + mPackage.usesLibraries = arrayList(ANDROID_TEST_RUNNER); + PackageBackwardCompatibility.modifySharedLibraries(mPackage); + assertEquals("usesLibraries not updated correctly", + arrayList(ANDROID_TEST_RUNNER, ANDROID_TEST_MOCK), + mPackage.usesLibraries); + } + + @Test + public void android_test_runner_in_usesOptionalLibraries() { + mPackage.usesOptionalLibraries = arrayList(ANDROID_TEST_RUNNER); + PackageBackwardCompatibility.modifySharedLibraries(mPackage); + assertEquals("usesOptionalLibraries not updated correctly", + arrayList(ANDROID_TEST_RUNNER, ANDROID_TEST_MOCK), + mPackage.usesOptionalLibraries); + } + + @Test + public void android_test_runner_in_usesLibraries_android_test_mock_in_usesOptionalLibraries() { + mPackage.usesLibraries = arrayList(ANDROID_TEST_RUNNER); + mPackage.usesOptionalLibraries = arrayList(ANDROID_TEST_MOCK); + PackageBackwardCompatibility.modifySharedLibraries(mPackage); + assertEquals("usesLibraries not updated correctly", + arrayList(ANDROID_TEST_RUNNER), + mPackage.usesLibraries); + assertEquals("usesOptionalLibraries not updated correctly", + arrayList(ANDROID_TEST_MOCK), + mPackage.usesOptionalLibraries); + } +} diff --git a/test-runner/Android.mk b/test-runner/Android.mk index 9053b23cfe9a..3c3718adff0e 100644 --- a/test-runner/Android.mk +++ b/test-runner/Android.mk @@ -22,9 +22,15 @@ android_test_mock_source_files := $(call all-java-files-under, src/android/test/ # ===================================== include $(CLEAR_VARS) -LOCAL_SRC_FILES := $(call all-java-files-under, src) +LOCAL_SRC_FILES := \ + $(filter-out $(android_test_mock_source_files), $(call all-java-files-under, src)) -LOCAL_JAVA_LIBRARIES := core-oj core-libart framework legacy-test +LOCAL_JAVA_LIBRARIES := \ + core-oj \ + core-libart \ + framework \ + legacy-test \ + android.test.mock \ LOCAL_MODULE:= android.test.runner |