1 /* 2 * Copyright (C) 2017 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.content.pm; 18 19 import static org.junit.Assert.assertEquals; 20 import static org.junit.Assert.assertNull; 21 22 import android.content.pm.PackageParser.Package; 23 import android.os.Build; 24 import android.support.test.filters.SmallTest; 25 26 import org.junit.Before; 27 import org.junit.Test; 28 import org.junit.runner.RunWith; 29 import org.junit.runners.JUnit4; 30 31 import java.util.ArrayList; 32 import java.util.Collections; 33 34 @SmallTest 35 @RunWith(JUnit4.class) 36 public class PackageBackwardCompatibilityTest { 37 38 private static final String ORG_APACHE_HTTP_LEGACY = "org.apache.http.legacy"; 39 40 private static final String ANDROID_TEST_RUNNER = "android.test.runner"; 41 42 private static final String ANDROID_TEST_MOCK = "android.test.mock"; 43 44 private Package mPackage; 45 46 private static ArrayList<String> arrayList(String... strings) { 47 ArrayList<String> list = new ArrayList<>(); 48 Collections.addAll(list, strings); 49 return list; 50 } 51 52 @Before 53 public void setUp() { 54 mPackage = new Package("org.package.name"); 55 mPackage.applicationInfo.targetSdkVersion = Build.VERSION_CODES.CUR_DEVELOPMENT; 56 } 57 58 @Test 59 public void null_usesLibraries() { 60 PackageBackwardCompatibility.modifySharedLibraries(mPackage); 61 assertNull("usesLibraries not updated correctly", mPackage.usesLibraries); 62 } 63 64 @Test 65 public void null_usesOptionalLibraries() { 66 PackageBackwardCompatibility.modifySharedLibraries(mPackage); 67 assertNull("usesOptionalLibraries not updated correctly", mPackage.usesOptionalLibraries); 68 } 69 70 @Test 71 public void remove_org_apache_http_legacy_from_usesLibraries() { 72 mPackage.usesLibraries = arrayList(ORG_APACHE_HTTP_LEGACY); 73 PackageBackwardCompatibility.modifySharedLibraries(mPackage); 74 assertNull("usesLibraries not updated correctly", mPackage.usesLibraries); 75 } 76 77 @Test 78 public void remove_org_apache_http_legacy_from_usesOptionalLibraries() { 79 mPackage.usesOptionalLibraries = arrayList(ORG_APACHE_HTTP_LEGACY); 80 PackageBackwardCompatibility.modifySharedLibraries(mPackage); 81 assertNull("usesOptionalLibraries not updated correctly", mPackage.usesOptionalLibraries); 82 } 83 84 @Test 85 public void android_test_runner_in_usesLibraries() { 86 mPackage.usesLibraries = arrayList(ANDROID_TEST_RUNNER); 87 PackageBackwardCompatibility.modifySharedLibraries(mPackage); 88 assertEquals("usesLibraries not updated correctly", 89 arrayList(ANDROID_TEST_RUNNER, ANDROID_TEST_MOCK), 90 mPackage.usesLibraries); 91 } 92 93 @Test 94 public void android_test_runner_in_usesOptionalLibraries() { 95 mPackage.usesOptionalLibraries = arrayList(ANDROID_TEST_RUNNER); 96 PackageBackwardCompatibility.modifySharedLibraries(mPackage); 97 assertEquals("usesOptionalLibraries not updated correctly", 98 arrayList(ANDROID_TEST_RUNNER, ANDROID_TEST_MOCK), 99 mPackage.usesOptionalLibraries); 100 } 101 102 @Test 103 public void android_test_runner_in_usesLibraries_android_test_mock_in_usesOptionalLibraries() { 104 mPackage.usesLibraries = arrayList(ANDROID_TEST_RUNNER); 105 mPackage.usesOptionalLibraries = arrayList(ANDROID_TEST_MOCK); 106 PackageBackwardCompatibility.modifySharedLibraries(mPackage); 107 assertEquals("usesLibraries not updated correctly", 108 arrayList(ANDROID_TEST_RUNNER), 109 mPackage.usesLibraries); 110 assertEquals("usesOptionalLibraries not updated correctly", 111 arrayList(ANDROID_TEST_MOCK), 112 mPackage.usesOptionalLibraries); 113 } 114 } 115