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

Commit 57327700 authored by Kholoud Mohamed's avatar Kholoud Mohamed Committed by Android (Google) Code Review
Browse files

Merge "Fixed and reenabled failing DevicePolicyManagerTest"

parents 192910e9 b73f3bef
Loading
Loading
Loading
Loading
+16 −16
Original line number Original line Diff line number Diff line
@@ -16,15 +16,12 @@


package com.android.server.devicepolicy;
package com.android.server.devicepolicy;


import static android.app.admin.DevicePolicyResources.Drawables.Style;

import static java.util.Objects.requireNonNull;
import static java.util.Objects.requireNonNull;


import android.annotation.NonNull;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.Nullable;
import android.app.admin.DevicePolicyDrawableResource;
import android.app.admin.DevicePolicyDrawableResource;
import android.app.admin.DevicePolicyResources;
import android.app.admin.DevicePolicyResources;
import android.app.admin.DevicePolicyResources.Drawables;
import android.app.admin.DevicePolicyStringResource;
import android.app.admin.DevicePolicyStringResource;
import android.app.admin.ParcelableResource;
import android.app.admin.ParcelableResource;
import android.os.Environment;
import android.os.Environment;
@@ -180,6 +177,7 @@ class DeviceManagementResourcesProvider {
    @Nullable
    @Nullable
    ParcelableResource getDrawable(
    ParcelableResource getDrawable(
            String drawableId, String drawableStyle, String drawableSource) {
            String drawableId, String drawableStyle, String drawableSource) {
        synchronized (mLock) {
            if (mUpdatedDrawablesForSource.containsKey(drawableId)
            if (mUpdatedDrawablesForSource.containsKey(drawableId)
                    && mUpdatedDrawablesForSource.get(drawableId).containsKey(drawableSource)) {
                    && mUpdatedDrawablesForSource.get(drawableId).containsKey(drawableSource)) {
                return mUpdatedDrawablesForSource.get(drawableId).get(drawableSource);
                return mUpdatedDrawablesForSource.get(drawableId).get(drawableSource);
@@ -191,6 +189,7 @@ class DeviceManagementResourcesProvider {
            if (mUpdatedDrawablesForStyle.get(drawableId).containsKey(drawableStyle)) {
            if (mUpdatedDrawablesForStyle.get(drawableId).containsKey(drawableStyle)) {
                return mUpdatedDrawablesForStyle.get(drawableId).get(drawableStyle);
                return mUpdatedDrawablesForStyle.get(drawableId).get(drawableStyle);
            }
            }
        }
        Log.d(TAG, "No updated drawable found for drawable id " + drawableId);
        Log.d(TAG, "No updated drawable found for drawable id " + drawableId);
        return null;
        return null;
    }
    }
@@ -249,10 +248,11 @@ class DeviceManagementResourcesProvider {


    @Nullable
    @Nullable
    ParcelableResource getString(String stringId) {
    ParcelableResource getString(String stringId) {
        synchronized (mLock) {
            if (mUpdatedStrings.containsKey(stringId)) {
            if (mUpdatedStrings.containsKey(stringId)) {
                return mUpdatedStrings.get(stringId);
                return mUpdatedStrings.get(stringId);
            }
            }

        }
        Log.d(TAG, "No updated string found for string id " + stringId);
        Log.d(TAG, "No updated string found for string id " + stringId);
        return null;
        return null;
    }
    }
+46 −51
Original line number Original line Diff line number Diff line
@@ -114,6 +114,8 @@ import static android.app.admin.DevicePolicyResources.Strings.Core.LOCATION_CHAN
import static android.app.admin.DevicePolicyResources.Strings.Core.NETWORK_LOGGING_MESSAGE;
import static android.app.admin.DevicePolicyResources.Strings.Core.NETWORK_LOGGING_MESSAGE;
import static android.app.admin.DevicePolicyResources.Strings.Core.NETWORK_LOGGING_TITLE;
import static android.app.admin.DevicePolicyResources.Strings.Core.NETWORK_LOGGING_TITLE;
import static android.app.admin.DevicePolicyResources.Strings.Core.NOTIFICATION_WORK_PROFILE_CONTENT_DESCRIPTION;
import static android.app.admin.DevicePolicyResources.Strings.Core.NOTIFICATION_WORK_PROFILE_CONTENT_DESCRIPTION;
import static android.app.admin.DevicePolicyResources.Strings.Core.PERSONAL_APP_SUSPENSION_MESSAGE;
import static android.app.admin.DevicePolicyResources.Strings.Core.PERSONAL_APP_SUSPENSION_SOON_MESSAGE;
import static android.app.admin.DevicePolicyResources.Strings.Core.PERSONAL_APP_SUSPENSION_TITLE;
import static android.app.admin.DevicePolicyResources.Strings.Core.PERSONAL_APP_SUSPENSION_TITLE;
import static android.app.admin.DevicePolicyResources.Strings.Core.PERSONAL_APP_SUSPENSION_TURN_ON_PROFILE;
import static android.app.admin.DevicePolicyResources.Strings.Core.PERSONAL_APP_SUSPENSION_TURN_ON_PROFILE;
import static android.app.admin.DevicePolicyResources.Strings.Core.PRINTING_DISABLED_NAMED_ADMIN;
import static android.app.admin.DevicePolicyResources.Strings.Core.PRINTING_DISABLED_NAMED_ADMIN;
@@ -173,7 +175,6 @@ import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager;
import android.app.ActivityManagerInternal;
import android.app.ActivityManagerInternal;
import android.app.ActivityTaskManager;
import android.app.ActivityTaskManager;
import android.app.ActivityThread;
import android.app.AlarmManager;
import android.app.AlarmManager;
import android.app.AppGlobals;
import android.app.AppGlobals;
import android.app.AppOpsManager;
import android.app.AppOpsManager;
@@ -7040,14 +7041,13 @@ public class DevicePolicyManagerService extends BaseIDevicePolicyManager {
    private String getGenericWipeReason(
    private String getGenericWipeReason(
            boolean calledByProfileOwnerOnOrgOwnedDevice, boolean calledOnParentInstance) {
            boolean calledByProfileOwnerOnOrgOwnedDevice, boolean calledOnParentInstance) {
        DevicePolicyManager dpm = mContext.getSystemService(DevicePolicyManager.class);
        return calledByProfileOwnerOnOrgOwnedDevice && !calledOnParentInstance
        return calledByProfileOwnerOnOrgOwnedDevice && !calledOnParentInstance
                ? dpm.getResources().getString(WORK_PROFILE_DELETED_ORG_OWNED_MESSAGE,
                ? getUpdatableString(
                        () -> mContext.getString(
                        WORK_PROFILE_DELETED_ORG_OWNED_MESSAGE,
                                R.string.device_ownership_relinquished))
                        R.string.device_ownership_relinquished)
                : dpm.getResources().getString(WORK_PROFILE_DELETED_GENERIC_MESSAGE,
                : getUpdatableString(
                        () -> mContext.getString(
                        WORK_PROFILE_DELETED_GENERIC_MESSAGE,
                                R.string.work_profile_deleted_description_dpm_wipe));
                        R.string.work_profile_deleted_description_dpm_wipe);
    }
    }
    /**
    /**
@@ -7143,9 +7143,7 @@ public class DevicePolicyManagerService extends BaseIDevicePolicyManager {
    }
    }
    private String getWorkProfileDeletedTitle() {
    private String getWorkProfileDeletedTitle() {
        DevicePolicyManager dpm = mContext.getSystemService(DevicePolicyManager.class);
        return getUpdatableString(WORK_PROFILE_DELETED_TITLE, R.string.work_profile_deleted);
        return dpm.getResources().getString(WORK_PROFILE_DELETED_TITLE,
                () -> mContext.getString(R.string.work_profile_deleted));
    }
    }
    private void clearWipeProfileNotification() {
    private void clearWipeProfileNotification() {
@@ -7457,10 +7455,9 @@ public class DevicePolicyManagerService extends BaseIDevicePolicyManager {
    }
    }
    private String getFailedPasswordAttemptWipeMessage() {
    private String getFailedPasswordAttemptWipeMessage() {
        DevicePolicyManager dpm = mContext.getSystemService(DevicePolicyManager.class);
        return getUpdatableString(
        return dpm.getResources().getString(WORK_PROFILE_DELETED_FAILED_PASSWORD_ATTEMPTS_MESSAGE,
                WORK_PROFILE_DELETED_FAILED_PASSWORD_ATTEMPTS_MESSAGE,
                () -> mContext.getString(
               R.string.work_profile_deleted_reason_maximum_password_failure);
                        R.string.work_profile_deleted_reason_maximum_password_failure));
    }
    }
    /**
    /**
@@ -12669,15 +12666,13 @@ public class DevicePolicyManagerService extends BaseIDevicePolicyManager {
    }
    }
    private String getLocationChangedTitle() {
    private String getLocationChangedTitle() {
        DevicePolicyManager dpm = mContext.getSystemService(DevicePolicyManager.class);
        return getUpdatableString(
        return dpm.getResources().getString(LOCATION_CHANGED_TITLE,
                LOCATION_CHANGED_TITLE, R.string.location_changed_notification_title);
                () -> mContext.getString(R.string.location_changed_notification_title));
    }
    }
    private String getLocationChangedText() {
    private String getLocationChangedText() {
        DevicePolicyManager dpm = mContext.getSystemService(DevicePolicyManager.class);
        return getUpdatableString(
        return dpm.getResources().getString(LOCATION_CHANGED_MESSAGE,
                LOCATION_CHANGED_MESSAGE, R.string.location_changed_notification_text);
                () -> mContext.getString(R.string.location_changed_notification_text));
    }
    }
    @Override
    @Override
@@ -13278,19 +13273,13 @@ public class DevicePolicyManagerService extends BaseIDevicePolicyManager {
                    Slogf.e(LOG_TAG, "appLabel is inexplicably null");
                    Slogf.e(LOG_TAG, "appLabel is inexplicably null");
                    return null;
                    return null;
                }
                }
                DevicePolicyManager dpm = mContext.getSystemService(DevicePolicyManager.class);
                return getUpdatableString(
                return dpm.getResources().getString(
                        PRINTING_DISABLED_NAMED_ADMIN,
                        PRINTING_DISABLED_NAMED_ADMIN,
                        () -> getDefaultPrintingDisabledMsg(appLabel),
                        R.string.printing_disabled_by,
                        appLabel);
                        appLabel);
            }
            }
        }
        }
        private String getDefaultPrintingDisabledMsg(CharSequence appLabel) {
            return ((Context) ActivityThread.currentActivityThread().getSystemUiContext())
                        .getResources().getString(R.string.printing_disabled_by, appLabel);
        }
        @Override
        @Override
        protected DevicePolicyCache getDevicePolicyCache() {
        protected DevicePolicyCache getDevicePolicyCache() {
            return mPolicyCache;
            return mPolicyCache;
@@ -15905,15 +15894,13 @@ public class DevicePolicyManagerService extends BaseIDevicePolicyManager {
    }
    }
    private String getNetworkLoggingTitle() {
    private String getNetworkLoggingTitle() {
        DevicePolicyManager dpm = mContext.getSystemService(DevicePolicyManager.class);
        return getUpdatableString(
        return dpm.getResources().getString(NETWORK_LOGGING_TITLE,
                NETWORK_LOGGING_TITLE, R.string.network_logging_notification_title);
                () -> mContext.getString(R.string.network_logging_notification_title));
    }
    }
    private String getNetworkLoggingText() {
    private String getNetworkLoggingText() {
        DevicePolicyManager dpm = mContext.getSystemService(DevicePolicyManager.class);
        return getUpdatableString(
        return dpm.getResources().getString(NETWORK_LOGGING_MESSAGE,
                NETWORK_LOGGING_MESSAGE, R.string.network_logging_notification_text);
                () -> mContext.getString(R.string.network_logging_notification_text));
    }
    }
    private void handleCancelNetworkLoggingNotification() {
    private void handleCancelNetworkLoggingNotification() {
@@ -17500,35 +17487,31 @@ public class DevicePolicyManagerService extends BaseIDevicePolicyManager {
    }
    }
    private String getPersonalAppSuspensionButtonText() {
    private String getPersonalAppSuspensionButtonText() {
        DevicePolicyManager dpm = mContext.getSystemService(DevicePolicyManager.class);
        return getUpdatableString(
        return dpm.getResources().getString(PERSONAL_APP_SUSPENSION_TURN_ON_PROFILE,
                PERSONAL_APP_SUSPENSION_TURN_ON_PROFILE,
                () -> mContext.getString(R.string.personal_apps_suspended_turn_profile_on));
                R.string.personal_apps_suspended_turn_profile_on);
    }
    }
    private String getPersonalAppSuspensionTitle() {
    private String getPersonalAppSuspensionTitle() {
        DevicePolicyManager dpm = mContext.getSystemService(DevicePolicyManager.class);
        return getUpdatableString(
        return dpm.getResources().getString(PERSONAL_APP_SUSPENSION_TITLE,
                PERSONAL_APP_SUSPENSION_TITLE, R.string.personal_apps_suspension_title);
                () -> mContext.getString(R.string.personal_apps_suspension_title));
    }
    }
    private String getPersonalAppSuspensionText() {
    private String getPersonalAppSuspensionText() {
        DevicePolicyManager dpm = mContext.getSystemService(DevicePolicyManager.class);
        return getUpdatableString(
        return dpm.getResources().getString(PERSONAL_APP_SUSPENSION_TITLE,
                PERSONAL_APP_SUSPENSION_MESSAGE, R.string.personal_apps_suspension_text);
                () -> mContext.getString(R.string.personal_apps_suspension_text));
    }
    }
    private String getPersonalAppSuspensionSoonText(String date, String time, int maxDays) {
    private String getPersonalAppSuspensionSoonText(String date, String time, int maxDays) {
        DevicePolicyManager dpm = mContext.getSystemService(DevicePolicyManager.class);
        return getUpdatableString(
        return dpm.getResources().getString(PERSONAL_APP_SUSPENSION_TITLE,
                PERSONAL_APP_SUSPENSION_SOON_MESSAGE, R.string.personal_apps_suspension_soon_text,
                () -> mContext.getString(
                        R.string.personal_apps_suspension_soon_text, date, time, maxDays),
                date, time, maxDays);
                date, time, maxDays);
    }
    }
    private String getWorkProfileContentDescription() {
    private String getWorkProfileContentDescription() {
        DevicePolicyManager dpm = mContext.getSystemService(DevicePolicyManager.class);
        return getUpdatableString(
        return dpm.getResources().getString(NOTIFICATION_WORK_PROFILE_CONTENT_DESCRIPTION,
                NOTIFICATION_WORK_PROFILE_CONTENT_DESCRIPTION,
                () -> mContext.getString(R.string.notification_work_profile_content_description));
                R.string.notification_work_profile_content_description);
    }
    }
    @Override
    @Override
@@ -18786,6 +18769,18 @@ public class DevicePolicyManagerService extends BaseIDevicePolicyManager {
        }
        }
    }
    }
    private String getUpdatableString(
            String updatableStringId, int defaultStringId, Object... formatArgs) {
        ParcelableResource resource = mDeviceManagementResourcesProvider.getString(
                updatableStringId);
        if (resource == null) {
            return ParcelableResource.loadDefaultString(() ->
                    mContext.getString(defaultStringId, formatArgs));
        }
        return resource.getString(
                mContext, () -> mContext.getString(defaultStringId, formatArgs), formatArgs);
    }
    public boolean isDpcDownloaded() {
    public boolean isDpcDownloaded() {
        Preconditions.checkCallAuthorization(hasCallingOrSelfPermission(
        Preconditions.checkCallAuthorization(hasCallingOrSelfPermission(
                android.Manifest.permission.MANAGE_PROFILE_AND_DEVICE_OWNERS));
                android.Manifest.permission.MANAGE_PROFILE_AND_DEVICE_OWNERS));
+5 −0
Original line number Original line Diff line number Diff line
@@ -530,5 +530,10 @@ public class DevicePolicyManagerServiceTestable extends DevicePolicyManagerServi


            return true;
            return true;
        }
        }

        @Override
        public Context createContextAsUser(UserHandle user) {
            return context;
        }
    }
    }
}
}
+6 −6
Original line number Original line Diff line number Diff line
@@ -162,7 +162,6 @@ import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.Matcher;
import org.junit.After;
import org.junit.After;
import org.junit.Before;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.Test;
import org.mockito.Mockito;
import org.mockito.Mockito;
import org.mockito.internal.util.collections.Sets;
import org.mockito.internal.util.collections.Sets;
@@ -191,7 +190,6 @@ import java.util.concurrent.TimeUnit;
 */
 */
@SmallTest
@SmallTest
@Presubmit
@Presubmit
@Ignore("b/225415867")
public class DevicePolicyManagerTest extends DpmTestBase {
public class DevicePolicyManagerTest extends DpmTestBase {


    private static final String TAG = DevicePolicyManagerTest.class.getSimpleName();
    private static final String TAG = DevicePolicyManagerTest.class.getSimpleName();
@@ -1794,10 +1792,9 @@ public class DevicePolicyManagerTest extends DpmTestBase {
        final int userId = CALLER_USER_HANDLE;
        final int userId = CALLER_USER_HANDLE;
        final UserHandle user = UserHandle.of(userId);
        final UserHandle user = UserHandle.of(userId);


        mContext.applicationInfo = new ApplicationInfo();
        mServiceContext.packageName = mRealTestContext.getPackageName();
        mContext.callerPermissions.add(permission.MANAGE_USERS);
        mServiceContext.applicationInfo = new ApplicationInfo();
        mContext.packageName = "com.android.frameworks.servicestests";
        mServiceContext.binder.callingUid = DpmMockContext.SYSTEM_UID;
        getServices().addPackageContext(user, mContext);
        when(mContext.resources.getColor(anyInt(), anyObject())).thenReturn(Color.WHITE);
        when(mContext.resources.getColor(anyInt(), anyObject())).thenReturn(Color.WHITE);


        StringParceledListSlice oneCert = asSlice(new String[] {"1"});
        StringParceledListSlice oneCert = asSlice(new String[] {"1"});
@@ -6289,6 +6286,7 @@ public class DevicePolicyManagerTest extends DpmTestBase {
    @Test
    @Test
    public void testGetOwnerInstalledCaCertsForDeviceOwner() throws Exception {
    public void testGetOwnerInstalledCaCertsForDeviceOwner() throws Exception {
        mServiceContext.packageName = mRealTestContext.getPackageName();
        mServiceContext.packageName = mRealTestContext.getPackageName();
        mServiceContext.applicationInfo = new ApplicationInfo();
        mServiceContext.binder.callingUid = DpmMockContext.SYSTEM_UID;
        mServiceContext.binder.callingUid = DpmMockContext.SYSTEM_UID;
        mAdmin1Context.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID;
        mAdmin1Context.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID;
        setDeviceOwner();
        setDeviceOwner();
@@ -6299,6 +6297,7 @@ public class DevicePolicyManagerTest extends DpmTestBase {
    @Test
    @Test
    public void testGetOwnerInstalledCaCertsForProfileOwner() throws Exception {
    public void testGetOwnerInstalledCaCertsForProfileOwner() throws Exception {
        mServiceContext.packageName = mRealTestContext.getPackageName();
        mServiceContext.packageName = mRealTestContext.getPackageName();
        mServiceContext.applicationInfo = new ApplicationInfo();
        mServiceContext.binder.callingUid = DpmMockContext.SYSTEM_UID;
        mServiceContext.binder.callingUid = DpmMockContext.SYSTEM_UID;
        mAdmin1Context.binder.callingUid = DpmMockContext.CALLER_UID;
        mAdmin1Context.binder.callingUid = DpmMockContext.CALLER_UID;
        setAsProfileOwner(admin1);
        setAsProfileOwner(admin1);
@@ -6310,6 +6309,7 @@ public class DevicePolicyManagerTest extends DpmTestBase {
    @Test
    @Test
    public void testGetOwnerInstalledCaCertsForDelegate() throws Exception {
    public void testGetOwnerInstalledCaCertsForDelegate() throws Exception {
        mServiceContext.packageName = mRealTestContext.getPackageName();
        mServiceContext.packageName = mRealTestContext.getPackageName();
        mServiceContext.applicationInfo = new ApplicationInfo();
        mServiceContext.binder.callingUid = DpmMockContext.SYSTEM_UID;
        mServiceContext.binder.callingUid = DpmMockContext.SYSTEM_UID;
        mAdmin1Context.binder.callingUid = DpmMockContext.CALLER_UID;
        mAdmin1Context.binder.callingUid = DpmMockContext.CALLER_UID;
        setAsProfileOwner(admin1);
        setAsProfileOwner(admin1);