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

Commit b73f3bef authored by kholoud mohamed's avatar kholoud mohamed Committed by Kholoud Mohamed
Browse files

Fixed and reenabled failing DevicePolicyManagerTest

Also refactored DPMS to stop calling DPM#getString

Fixes: 225415867
Test: atest com.android.server.devicepolicy.DevicePolicyManagerTest
Change-Id: Id53bd4c6b3f57d8ac98a31d28eaa27cb98547852
(cherry picked from commit 14913d1d)
Merged-In: Id53bd4c6b3f57d8ac98a31d28eaa27cb98547852
parent 349e0535
Loading
Loading
Loading
Loading
+16 −16
Original line number Diff line number Diff line
@@ -16,15 +16,12 @@

package com.android.server.devicepolicy;

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

import static java.util.Objects.requireNonNull;

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

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

        }
        Log.d(TAG, "No updated string found for string id " + stringId);
        return null;
    }
+46 −51
Original line number 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_TITLE;
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_TURN_ON_PROFILE;
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.ActivityManagerInternal;
import android.app.ActivityTaskManager;
import android.app.ActivityThread;
import android.app.AlarmManager;
import android.app.AppGlobals;
import android.app.AppOpsManager;
@@ -7023,14 +7024,13 @@ public class DevicePolicyManagerService extends BaseIDevicePolicyManager {
    private String getGenericWipeReason(
            boolean calledByProfileOwnerOnOrgOwnedDevice, boolean calledOnParentInstance) {
        DevicePolicyManager dpm = mContext.getSystemService(DevicePolicyManager.class);
        return calledByProfileOwnerOnOrgOwnedDevice && !calledOnParentInstance
                ? dpm.getResources().getString(WORK_PROFILE_DELETED_ORG_OWNED_MESSAGE,
                        () -> mContext.getString(
                                R.string.device_ownership_relinquished))
                : dpm.getResources().getString(WORK_PROFILE_DELETED_GENERIC_MESSAGE,
                        () -> mContext.getString(
                                R.string.work_profile_deleted_description_dpm_wipe));
                ? getUpdatableString(
                        WORK_PROFILE_DELETED_ORG_OWNED_MESSAGE,
                        R.string.device_ownership_relinquished)
                : getUpdatableString(
                        WORK_PROFILE_DELETED_GENERIC_MESSAGE,
                        R.string.work_profile_deleted_description_dpm_wipe);
    }
    /**
@@ -7126,9 +7126,7 @@ public class DevicePolicyManagerService extends BaseIDevicePolicyManager {
    }
    private String getWorkProfileDeletedTitle() {
        DevicePolicyManager dpm = mContext.getSystemService(DevicePolicyManager.class);
        return dpm.getResources().getString(WORK_PROFILE_DELETED_TITLE,
                () -> mContext.getString(R.string.work_profile_deleted));
        return getUpdatableString(WORK_PROFILE_DELETED_TITLE, R.string.work_profile_deleted);
    }
    private void clearWipeProfileNotification() {
@@ -7440,10 +7438,9 @@ public class DevicePolicyManagerService extends BaseIDevicePolicyManager {
    }
    private String getFailedPasswordAttemptWipeMessage() {
        DevicePolicyManager dpm = mContext.getSystemService(DevicePolicyManager.class);
        return dpm.getResources().getString(WORK_PROFILE_DELETED_FAILED_PASSWORD_ATTEMPTS_MESSAGE,
                () -> mContext.getString(
                        R.string.work_profile_deleted_reason_maximum_password_failure));
        return getUpdatableString(
                WORK_PROFILE_DELETED_FAILED_PASSWORD_ATTEMPTS_MESSAGE,
               R.string.work_profile_deleted_reason_maximum_password_failure);
    }
    /**
@@ -12642,15 +12639,13 @@ public class DevicePolicyManagerService extends BaseIDevicePolicyManager {
    }
    private String getLocationChangedTitle() {
        DevicePolicyManager dpm = mContext.getSystemService(DevicePolicyManager.class);
        return dpm.getResources().getString(LOCATION_CHANGED_TITLE,
                () -> mContext.getString(R.string.location_changed_notification_title));
        return getUpdatableString(
                LOCATION_CHANGED_TITLE, R.string.location_changed_notification_title);
    }
    private String getLocationChangedText() {
        DevicePolicyManager dpm = mContext.getSystemService(DevicePolicyManager.class);
        return dpm.getResources().getString(LOCATION_CHANGED_MESSAGE,
                () -> mContext.getString(R.string.location_changed_notification_text));
        return getUpdatableString(
                LOCATION_CHANGED_MESSAGE, R.string.location_changed_notification_text);
    }
    @Override
@@ -13249,19 +13244,13 @@ public class DevicePolicyManagerService extends BaseIDevicePolicyManager {
                    Slogf.e(LOG_TAG, "appLabel is inexplicably null");
                    return null;
                }
                DevicePolicyManager dpm = mContext.getSystemService(DevicePolicyManager.class);
                return dpm.getResources().getString(
                return getUpdatableString(
                        PRINTING_DISABLED_NAMED_ADMIN,
                        () -> getDefaultPrintingDisabledMsg(appLabel),
                        R.string.printing_disabled_by,
                        appLabel);
            }
        }
        private String getDefaultPrintingDisabledMsg(CharSequence appLabel) {
            return ((Context) ActivityThread.currentActivityThread().getSystemUiContext())
                        .getResources().getString(R.string.printing_disabled_by, appLabel);
        }
        @Override
        protected DevicePolicyCache getDevicePolicyCache() {
            return mPolicyCache;
@@ -15867,15 +15856,13 @@ public class DevicePolicyManagerService extends BaseIDevicePolicyManager {
    }
    private String getNetworkLoggingTitle() {
        DevicePolicyManager dpm = mContext.getSystemService(DevicePolicyManager.class);
        return dpm.getResources().getString(NETWORK_LOGGING_TITLE,
                () -> mContext.getString(R.string.network_logging_notification_title));
        return getUpdatableString(
                NETWORK_LOGGING_TITLE, R.string.network_logging_notification_title);
    }
    private String getNetworkLoggingText() {
        DevicePolicyManager dpm = mContext.getSystemService(DevicePolicyManager.class);
        return dpm.getResources().getString(NETWORK_LOGGING_MESSAGE,
                () -> mContext.getString(R.string.network_logging_notification_text));
        return getUpdatableString(
                NETWORK_LOGGING_MESSAGE, R.string.network_logging_notification_text);
    }
    private void handleCancelNetworkLoggingNotification() {
@@ -17462,35 +17449,31 @@ public class DevicePolicyManagerService extends BaseIDevicePolicyManager {
    }
    private String getPersonalAppSuspensionButtonText() {
        DevicePolicyManager dpm = mContext.getSystemService(DevicePolicyManager.class);
        return dpm.getResources().getString(PERSONAL_APP_SUSPENSION_TURN_ON_PROFILE,
                () -> mContext.getString(R.string.personal_apps_suspended_turn_profile_on));
        return getUpdatableString(
                PERSONAL_APP_SUSPENSION_TURN_ON_PROFILE,
                R.string.personal_apps_suspended_turn_profile_on);
    }
    private String getPersonalAppSuspensionTitle() {
        DevicePolicyManager dpm = mContext.getSystemService(DevicePolicyManager.class);
        return dpm.getResources().getString(PERSONAL_APP_SUSPENSION_TITLE,
                () -> mContext.getString(R.string.personal_apps_suspension_title));
        return getUpdatableString(
                PERSONAL_APP_SUSPENSION_TITLE, R.string.personal_apps_suspension_title);
    }
    private String getPersonalAppSuspensionText() {
        DevicePolicyManager dpm = mContext.getSystemService(DevicePolicyManager.class);
        return dpm.getResources().getString(PERSONAL_APP_SUSPENSION_TITLE,
                () -> mContext.getString(R.string.personal_apps_suspension_text));
        return getUpdatableString(
                PERSONAL_APP_SUSPENSION_MESSAGE, R.string.personal_apps_suspension_text);
    }
    private String getPersonalAppSuspensionSoonText(String date, String time, int maxDays) {
        DevicePolicyManager dpm = mContext.getSystemService(DevicePolicyManager.class);
        return dpm.getResources().getString(PERSONAL_APP_SUSPENSION_TITLE,
                () -> mContext.getString(
                        R.string.personal_apps_suspension_soon_text, date, time, maxDays),
        return getUpdatableString(
                PERSONAL_APP_SUSPENSION_SOON_MESSAGE, R.string.personal_apps_suspension_soon_text,
                date, time, maxDays);
    }
    private String getWorkProfileContentDescription() {
        DevicePolicyManager dpm = mContext.getSystemService(DevicePolicyManager.class);
        return dpm.getResources().getString(NOTIFICATION_WORK_PROFILE_CONTENT_DESCRIPTION,
                () -> mContext.getString(R.string.notification_work_profile_content_description));
        return getUpdatableString(
                NOTIFICATION_WORK_PROFILE_CONTENT_DESCRIPTION,
                R.string.notification_work_profile_content_description);
    }
    @Override
@@ -18748,6 +18731,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() {
        Preconditions.checkCallAuthorization(hasCallingOrSelfPermission(
                android.Manifest.permission.MANAGE_PROFILE_AND_DEVICE_OWNERS));
+5 −0
Original line number Diff line number Diff line
@@ -530,5 +530,10 @@ public class DevicePolicyManagerServiceTestable extends DevicePolicyManagerServi

            return true;
        }

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

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

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

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