Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit e60bef09 authored by Paul Duffin's avatar Paul Duffin Committed by Android (Google) Code Review
Browse files

Merge "Separate android.test.mock classes from android.test.runner"

parents fed5d370 855d702c
Loading
Loading
Loading
Loading
+78 −0
Original line number Diff line number Diff line
/*
 * 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");
    }
}
+1 −13
Original line number Diff line number Diff line
@@ -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
     */
+1 −1
Original line number Diff line number Diff line
@@ -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

+114 −0
Original line number Diff line number Diff line
/*
 * 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);
    }
}
+8 −2
Original line number Diff line number Diff line
@@ -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