summaryrefslogtreecommitdiff
path: root/hostsidetests/devicepolicy/app/DeviceOwner/src/com/android/cts/deviceowner/UserControlDisabledPackagesTest.java
blob: 4da5fff7f0b0b75edb673bcedadda37c68cbc345 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
 * Copyright (C) 2020 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.cts.deviceowner;

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

import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.util.Log;

import java.util.ArrayList;

/**
 * Test {@link DevicePolicyManager#setUserControlDisabledPackages} and
 * {@link DevicePolicyManager#getUserControlDisabledPackages}
 * Hostside test uses "am force-stop" and verifies that app is not stopped.
 */
public class UserControlDisabledPackagesTest extends BaseDeviceOwnerTest {
    private static final String TAG = "UserControlDisabledPackagesTest";

    private static final String TEST_APP_APK = "CtsEmptyTestApp.apk";
    private static final String TEST_APP_PKG = "android.packageinstaller.emptytestapp.cts";
    private static final String SIMPLE_APP_APK = "CtsSimpleApp.apk";
    private static final String SIMPLE_APP_PKG = "com.android.cts.launcherapps.simpleapp";
    private static final String SIMPLE_APP_ACTIVITY =
            "com.android.cts.launcherapps.simpleapp.SimpleActivityImmediateExit";

    public void testSetUserControlDisabledPackages() throws Exception {
        ArrayList<String> protectedPackages = new ArrayList<>();
        protectedPackages.add(SIMPLE_APP_PKG);
        mDevicePolicyManager.setUserControlDisabledPackages(getWho(), protectedPackages);
    }

    public void testLaunchActivity() throws Exception {
        // Launch an activity so that the app exits stopped state.
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.setClassName(SIMPLE_APP_PKG, SIMPLE_APP_ACTIVITY);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        Log.d(TAG, "Starting " + intent + " on user " + getCurrentUser().getIdentifier());
        mContext.startActivityAsUser(intent, getCurrentUser());
    }

    public void testForceStopWithUserControlDisabled() throws Exception {
        final ArrayList<String> pkgs = new ArrayList<>();
        pkgs.add(SIMPLE_APP_PKG);
        // Check if package is part of UserControlDisabledPackages before checking if
        // package is stopped since it is a necessary condition to prevent stopping of
        // package

        assertThat(mDevicePolicyManager.getUserControlDisabledPackages(getWho()))
                .containsExactly(SIMPLE_APP_PKG);
        assertPackageStopped(/* stopped= */ false);
    }

    public void testClearSetUserControlDisabledPackages() throws Exception {
        final ArrayList<String> pkgs = new ArrayList<>();
        mDevicePolicyManager.setUserControlDisabledPackages(getWho(), pkgs);
        assertThat(mDevicePolicyManager.getUserControlDisabledPackages(getWho())).isEmpty();
    }

    public void testForceStopWithUserControlEnabled() throws Exception {
        assertPackageStopped(/* stopped= */ true);
        assertThat(mDevicePolicyManager.getUserControlDisabledPackages(getWho())).isEmpty();
    }

    public void testFgsStopWithUserControlDisabled() throws Exception {
        final ArrayList<String> pkgs = new ArrayList<>();
        pkgs.add(SIMPLE_APP_PKG);
        // Check if package is part of UserControlDisabledPackages before checking if
        // package is stopped since it is a necessary condition to prevent stopping of
        // package

        assertThat(mDevicePolicyManager.getUserControlDisabledPackages(getWho()))
                .containsExactly(SIMPLE_APP_PKG);
        assertPackageRunningState(/* running= */ true);
    }

    public void testFgsStopWithUserControlEnabled() throws Exception {
        assertPackageRunningState(/* running= */ false);
        assertThat(mDevicePolicyManager.getUserControlDisabledPackages(getWho())).isEmpty();
    }

    private boolean isPackageStopped(String packageName) throws Exception {
        PackageInfo packageInfo = mContext.getPackageManager()
                .getPackageInfoAsUser(packageName, PackageManager.GET_META_DATA,
                        getCurrentUser().getIdentifier());
        boolean stopped = (packageInfo.applicationInfo.flags & ApplicationInfo.FLAG_STOPPED)
                == ApplicationInfo.FLAG_STOPPED;
        Log.d(TAG, "Application flags for " + packageName + " on user "
                + getCurrentUser().getIdentifier() + " = "
                + Integer.toHexString(packageInfo.applicationInfo.flags) + ". Stopped: " + stopped);
        return stopped;
    }

    private void assertPackageStopped(boolean stopped) throws Exception {
        assertWithMessage("Package %s stopped for user %s", SIMPLE_APP_PKG,
                getCurrentUser().getIdentifier())
                .that(isPackageStopped(SIMPLE_APP_PKG)).isEqualTo(stopped);
    }

    private boolean isPackageRunning(String packageName) throws Exception {
        String pid = executeShellCommand(String.format("pidof %s", packageName)).trim();
        return pid.length() > 0;
    }

    private void assertPackageRunningState(boolean shouldBeRunning) throws Exception {
        assertWithMessage("Package %s running for user %s", SIMPLE_APP_PKG,
                getCurrentUser().getIdentifier())
                .that(isPackageRunning(SIMPLE_APP_PKG)).isEqualTo(shouldBeRunning);
    }
}