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

Commit 86b6e303 authored by Joanne Chung's avatar Joanne Chung
Browse files

Add test to verify PackageMonitor implementation respects app visibility

Because the coretests shareUserId makes the test hard to verify the
app visibility, this change moves PackageMonitor tests to a specific
module. The module doesn' have the shareUserId, we can verify app
visibity with the install parameter force-queryable.

Bug: 289567218
Test: atest FrameworksCorePackageMonitorTests
Change-Id: Ib9472b407680c26501d662bc4f235f442e213414
parent 3a651337
Loading
Loading
Loading
Loading
+41 −0
Original line number Diff line number Diff line
// Copyright (C) 2023 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 {
    // See: http://go/android-license-faq
    // A large-scale-change added 'default_applicable_licenses' to import
    // all of the 'license_kinds' from "frameworks_base_license"
    // to get the below license kinds:
    //   SPDX-license-identifier-Apache-2.0
    default_applicable_licenses: ["frameworks_base_license"],
}

android_test {
    name: "FrameworksCorePackageMonitorTests",
    srcs: ["src/**/*.java"],
    static_libs: [
        "androidx.test.runner",
        "compatibility-device-util-axt",
        "frameworks-base-testutils",
        "mockito-target-minus-junit4",
        "truth-prebuilt",
    ],
    libs: ["android.test.runner"],
    platform_apis: true,
    certificate: "platform",
    test_suites: ["device-tests"],
    data: [
        ":TestVisibilityApp",
    ],
}
+30 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2023 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="com.android.frameworks.packagemonitor">

    <uses-permission android:name="android.permission.INTERACT_ACROSS_USERS_FULL" />

    <application>
        <uses-library android:name="android.test.runner" />
    </application>

    <instrumentation
            android:name="androidx.test.runner.AndroidJUnitRunner"
            android:targetPackage="com.android.frameworks.packagemonitor"
            android:label="Frameworks PackageMonitor Core Tests" />
</manifest>
+44 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<!--
  ~ Copyright (C) 2023 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.
  -->

<configuration description="Runs Frameworks Core Tests.">
    <option name="test-suite-tag" value="apct" />
    <option name="test-suite-tag" value="apct-instrumentation" />

    <target_preparer class="com.android.tradefed.targetprep.suite.SuiteApkInstaller">
        <option name="cleanup-apks" value="true" />
        <option name="test-file-name" value="FrameworksCorePackageMonitorTests.apk" />
    </target_preparer>

    <!-- Create place to store apks -->
    <target_preparer class="com.android.tradefed.targetprep.RunCommandTargetPreparer">
        <option name="run-command" value="mkdir -p /data/local/tmp/contenttests" />
        <option name="teardown-command" value="rm -rf /data/local/tmp/contenttests"/>
    </target_preparer>
    <!-- Load additional APKs onto device -->
    <target_preparer class="com.android.tradefed.targetprep.PushFilePreparer">
        <option name="push" value="TestVisibilityApp.apk->/data/local/tmp/contenttests/TestVisibilityApp.apk" />
        <option name="cleanup" value="true" />
    </target_preparer>

    <option name="test-tag" value="FrameworksCorePackageMonitorTests" />
    <test class="com.android.tradefed.testtype.AndroidJUnitTest" >
        <option name="package" value="com.android.frameworks.packagemonitor" />
        <option name="runner" value="androidx.test.runner.AndroidJUnitRunner" />
        <option name="hidden-api-checks" value="false"/>
    </test>
</configuration>
+1 −1
Original line number Diff line number Diff line
@@ -44,10 +44,10 @@ import org.mockito.MockitoAnnotations;
 */
@RunWith(AndroidJUnit4.class)
public class PackageMonitorTest {

    private static final String FAKE_PACKAGE_NAME = "com.android.internal.content.fakeapp";
    private static final int FAKE_PACKAGE_UID = 123;
    private static final int FAKE_USER_ID = 0;
    private static final int WAIT_CALLBACK_CALLED_IN_MS = 300;

    @Mock
    Context mMockContext;
+121 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 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 com.android.internal.content;

import static com.android.compatibility.common.util.ShellUtils.runShellCommand;

import static com.google.common.truth.Truth.assertThat;

import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Handler;
import android.os.Looper;
import android.os.UserHandle;

import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.runner.AndroidJUnit4;

import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

/**
 * A test to verify PackageMonitor implementation respects the app visibility.
 */
@RunWith(AndroidJUnit4.class)
public class PackageMonitorVisibilityTest {
    private static final String TEST_DATA_PATH = "/data/local/tmp/contenttests/";
    private static final String TEAT_APK_PATH =
            TEST_DATA_PATH + "TestVisibilityApp.apk";
    private static final String TEAT_APK_PACKAGE_NAME = "com.example.android.testvisibilityapp";
    private static final int WAIT_CALLBACK_CALLED_IN_SECONDS = 1;
    @Test
    public void testPackageMonitorPackageVisible() throws Exception {
        TestVisibilityPackageMonitor testPackageMonitor = new TestVisibilityPackageMonitor();

        try {
            Context context = InstrumentationRegistry.getInstrumentation().getContext();
            testPackageMonitor.register(context, UserHandle.ALL,
                    new Handler(Looper.getMainLooper()));

            installTestPackage(true /* forceQueryable */);
            boolean result = testPackageMonitor.mCallbackCountDownLatch.await(
                    WAIT_CALLBACK_CALLED_IN_SECONDS, TimeUnit.SECONDS);

            int expectedUid = context.getPackageManager().getPackageUid(TEAT_APK_PACKAGE_NAME,
                    PackageManager.PackageInfoFlags.of(0));
            assertThat(result).isTrue();
            assertThat(testPackageMonitor.mAddedPackageName).isEqualTo(TEAT_APK_PACKAGE_NAME);
            assertThat(testPackageMonitor.mAddedPackageUid).isEqualTo(expectedUid);
        } finally {
            testPackageMonitor.unregister();
            uninstallTestPackage();
        }
    }

    @Test
    public void testPackageMonitorPackageNotVisible() throws Exception {
        TestVisibilityPackageMonitor testPackageMonitor = new TestVisibilityPackageMonitor();

        try {
            Context context = InstrumentationRegistry.getInstrumentation().getContext();
            testPackageMonitor.register(context, UserHandle.ALL,
                    new Handler(Looper.getMainLooper()));

            installTestPackage(false /* forceQueryable */);
            boolean result = testPackageMonitor.mCallbackCountDownLatch.await(
                    WAIT_CALLBACK_CALLED_IN_SECONDS, TimeUnit.SECONDS);

            assertThat(result).isFalse();
        } finally {
            testPackageMonitor.unregister();
            uninstallTestPackage();
        }
    }

    private static void installTestPackage(boolean forceQueryable) {
        final StringBuilder cmd = new StringBuilder("pm install ");
        if (forceQueryable) {
            cmd.append("--force-queryable ");
        }
        cmd.append(TEAT_APK_PATH);
        final String result = runShellCommand(cmd.toString());
        assertThat(result.trim()).contains("Success");
    }

    private static void uninstallTestPackage() {
        runShellCommand("pm uninstall " + TEAT_APK_PACKAGE_NAME);
    }

    private static class TestVisibilityPackageMonitor extends PackageMonitor {
        String mAddedPackageName;
        int mAddedPackageUid;
        CountDownLatch mCallbackCountDownLatch = new CountDownLatch(1);

        @Override
        public void onPackageAdded(String packageName, int uid) {
            if (!TEAT_APK_PACKAGE_NAME.equals(packageName)) {
                return;
            }
            mAddedPackageName = packageName;
            mAddedPackageUid = uid;
            mCallbackCountDownLatch.countDown();
        }
    }
}
Loading