Merge "Further refactoring on ActionDisabledByAdminDialog classes." into sc-dev

This commit is contained in:
TreeHugger Robot
2021-06-01 20:13:24 +00:00
committed by Android (Google) Code Review
12 changed files with 449 additions and 185 deletions

View File

@@ -16,6 +16,7 @@
package com.android.settingslib.enterprise; package com.android.settingslib.enterprise;
import android.annotation.UserIdInt;
import android.content.Context; import android.content.Context;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
@@ -27,11 +28,16 @@ import com.android.settingslib.RestrictedLockUtils;
*/ */
public interface ActionDisabledByAdminController { public interface ActionDisabledByAdminController {
/**
* Sets the {@link ActionDisabledLearnMoreButtonLauncher}.
*/
void initialize(ActionDisabledLearnMoreButtonLauncher launcher);
/** /**
* Handles the adding and setting up of the learn more button. If button is not needed, then * Handles the adding and setting up of the learn more button. If button is not needed, then
* this method can be left empty. * this method can be left empty.
*/ */
void setupLearnMoreButton(Context context, Object alertDialogBuilder); void setupLearnMoreButton(Context context);
/** /**
* Returns the admin support dialog's title resource id. * Returns the admin support dialog's title resource id.
@@ -41,11 +47,11 @@ public interface ActionDisabledByAdminController {
/** /**
* Returns the admin support dialog's content string. * Returns the admin support dialog's content string.
*/ */
CharSequence getAdminSupportContentString( CharSequence getAdminSupportContentString(Context context,
Context context, @Nullable CharSequence supportMessage); @Nullable CharSequence supportMessage);
/** /**
* Updates the enforced admin * Updates the enforced admin
*/ */
void updateEnforcedAdmin(RestrictedLockUtils.EnforcedAdmin admin, int adminUserId); void updateEnforcedAdmin(RestrictedLockUtils.EnforcedAdmin admin, @UserIdInt int adminUserId);
} }

View File

@@ -19,29 +19,30 @@ package com.android.settingslib.enterprise;
import static android.app.admin.DevicePolicyManager.DEVICE_OWNER_TYPE_FINANCED; import static android.app.admin.DevicePolicyManager.DEVICE_OWNER_TYPE_FINANCED;
import android.app.admin.DevicePolicyManager; import android.app.admin.DevicePolicyManager;
import android.content.Context;
/** /**
* A factory that returns the relevant instance of {@link ActionDisabledByAdminController}. * A factory that returns the relevant instance of {@link ActionDisabledByAdminController}.
*/ */
public class ActionDisabledByAdminControllerFactory { public final class ActionDisabledByAdminControllerFactory {
/** /**
* Returns the relevant instance of {@link ActionDisabledByAdminController}. * Returns the relevant instance of {@link ActionDisabledByAdminController}.
*/ */
public static ActionDisabledByAdminController createInstance( public static ActionDisabledByAdminController createInstance(Context context,
DevicePolicyManager dpm, DeviceAdminStringProvider stringProvider) {
ActionDisabledLearnMoreButtonLauncher helper, return isFinancedDevice(context)
DeviceAdminStringProvider deviceAdminStringProvider) { ? new FinancedDeviceActionDisabledByAdminController(stringProvider)
if (isFinancedDevice(dpm)) { : new ManagedDeviceActionDisabledByAdminController(stringProvider);
return new FinancedDeviceActionDisabledByAdminController(
helper, deviceAdminStringProvider);
}
return new ManagedDeviceActionDisabledByAdminController(
helper, deviceAdminStringProvider);
} }
private static boolean isFinancedDevice(DevicePolicyManager dpm) { private static boolean isFinancedDevice(Context context) {
DevicePolicyManager dpm = context.getSystemService(DevicePolicyManager.class);
return dpm.isDeviceManaged() && dpm.getDeviceOwnerType( return dpm.isDeviceManaged() && dpm.getDeviceOwnerType(
dpm.getDeviceOwnerComponentOnAnyUser()) == DEVICE_OWNER_TYPE_FINANCED; dpm.getDeviceOwnerComponentOnAnyUser()) == DEVICE_OWNER_TYPE_FINANCED;
} }
private ActionDisabledByAdminControllerFactory() {
throw new UnsupportedOperationException("provides only static methods");
}
} }

View File

@@ -16,29 +16,100 @@
package com.android.settingslib.enterprise; package com.android.settingslib.enterprise;
import android.content.Context; import static java.util.Objects.requireNonNull;
import com.android.settingslib.RestrictedLockUtils; import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.UserHandle;
import android.os.UserManager;
import com.android.internal.annotations.VisibleForTesting;
import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
/** /**
* Helper interface meant to set up the "Learn more" button in the action disabled dialog. * Helper class meant to set up the "Learn more" button in the action disabled dialog.
*/ */
public interface ActionDisabledLearnMoreButtonLauncher { public abstract class ActionDisabledLearnMoreButtonLauncher {
/** /**
* Sets up a "learn more" button which shows a screen with device policy settings * Sets up a "learn more" button which shows a screen with device policy settings
*/ */
void setupLearnMoreButtonToShowAdminPolicies( public final void setupLearnMoreButtonToShowAdminPolicies(Context context,
Context context, int enforcementAdminUserId, EnforcedAdmin enforcedAdmin) {
Object alertDialogBuilder, requireNonNull(context, "context cannot be null");
int enforcementAdminUserId, requireNonNull(enforcedAdmin, "enforcedAdmin cannot be null");
RestrictedLockUtils.EnforcedAdmin enforcedAdmin);
// The "Learn more" button appears only if the restriction is enforced by an admin in the
// same profile group. Otherwise the admin package and its policies are not accessible to
// the current user.
if (isSameProfileGroup(context, enforcementAdminUserId)) {
setLearnMoreButton(() -> showAdminPolicies(context, enforcedAdmin));
}
}
/** /**
* Sets up a "learn more" button which launches a help page * Sets up a "learn more" button which launches a help page
*/ */
void setupLearnMoreButtonToLaunchHelpPage( public final void setupLearnMoreButtonToLaunchHelpPage(Context context, String url) {
Context context, requireNonNull(context, "context cannot be null");
Object alertDialogBuilder, requireNonNull(url, "url cannot be null");
String url);
setLearnMoreButton(() -> showHelpPage(context, url));
}
/**
* Sets the "learning more" button.
*
* @param action action to be run when the button is tapped.
*/
public abstract void setLearnMoreButton(Runnable action);
/**
* Launches the settings page with info about the given admin.
*/
protected abstract void launchShowAdminPolicies(Context context, UserHandle user,
ComponentName admin);
/**
* Launches the settings page that shows all admins.
*/
protected abstract void launchShowAdminSettings(Context context);
/**
* Callback to finish the activity associated with the launcher.
*/
protected void finishSelf() {
}
@VisibleForTesting
protected boolean isSameProfileGroup(Context context, int enforcementAdminUserId) {
UserManager um = context.getSystemService(UserManager.class);
return um.isSameProfileGroup(enforcementAdminUserId, um.getUserHandle());
}
/**
* Shows the help page using the given {@code url}.
*/
@VisibleForTesting
public void showHelpPage(Context context, String url) {
context.startActivityAsUser(createLearnMoreIntent(url), UserHandle.of(context.getUserId()));
finishSelf();
}
private void showAdminPolicies(Context context, EnforcedAdmin enforcedAdmin) {
if (enforcedAdmin.component != null) {
launchShowAdminPolicies(context, enforcedAdmin.user, enforcedAdmin.component);
} else {
launchShowAdminSettings(context);
}
finishSelf();
}
private static Intent createLearnMoreIntent(String url) {
return new Intent(Intent.ACTION_VIEW, Uri.parse(url)).setFlags(
Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
}
} }

View File

@@ -0,0 +1,56 @@
/*
* Copyright (C) 2021 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.settingslib.enterprise;
import static java.util.Objects.requireNonNull;
import android.annotation.UserIdInt;
import com.android.internal.util.Preconditions;
import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
/**
* Base class for {@link ActionDisabledByAdminController} implementations.
*/
abstract class BaseActionDisabledByAdminController
implements ActionDisabledByAdminController {
protected @UserIdInt int mEnforcementAdminUserId;
protected EnforcedAdmin mEnforcedAdmin;
protected ActionDisabledLearnMoreButtonLauncher mLauncher;
protected final DeviceAdminStringProvider mStringProvider;
BaseActionDisabledByAdminController(DeviceAdminStringProvider stringProvider) {
mStringProvider = stringProvider;
}
@Override
public final void initialize(ActionDisabledLearnMoreButtonLauncher launcher) {
mLauncher = requireNonNull(launcher, "launcher cannot be null");
}
@Override
public final void updateEnforcedAdmin(EnforcedAdmin admin, int adminUserId) {
assertInitialized();
mEnforcementAdminUserId = adminUserId;
mEnforcedAdmin = requireNonNull(admin, "admin cannot be null");
}
protected final void assertInitialized() {
Preconditions.checkState(mLauncher != null, "must call initialize() first");
}
}

View File

@@ -16,52 +16,31 @@
package com.android.settingslib.enterprise; package com.android.settingslib.enterprise;
import static java.util.Objects.requireNonNull;
import android.annotation.UserIdInt;
import android.content.Context; import android.content.Context;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
/** /**
* An {@link ActionDisabledByAdminController} to be used with financed devices. * An {@link ActionDisabledByAdminController} to be used with financed devices.
*/ */
public class FinancedDeviceActionDisabledByAdminController final class FinancedDeviceActionDisabledByAdminController
implements ActionDisabledByAdminController { extends BaseActionDisabledByAdminController {
private @UserIdInt int mEnforcementAdminUserId; FinancedDeviceActionDisabledByAdminController(DeviceAdminStringProvider stringProvider) {
private EnforcedAdmin mEnforcedAdmin; super(stringProvider);
private final ActionDisabledLearnMoreButtonLauncher mHelper;
private final DeviceAdminStringProvider mDeviceAdminStringProvider;
FinancedDeviceActionDisabledByAdminController(
ActionDisabledLearnMoreButtonLauncher helper,
DeviceAdminStringProvider deviceAdminStringProvider) {
mHelper = requireNonNull(helper, "helper cannot be null");
mDeviceAdminStringProvider = requireNonNull(deviceAdminStringProvider,
"deviceAdminStringProvider cannot be null");
} }
@Override @Override
public void updateEnforcedAdmin(EnforcedAdmin admin, int adminUserId) { public void setupLearnMoreButton(Context context) {
mEnforcementAdminUserId = adminUserId; assertInitialized();
mEnforcedAdmin = requireNonNull(admin, "admin cannot be null");
}
@Override mLauncher.setupLearnMoreButtonToShowAdminPolicies(context, mEnforcementAdminUserId,
public void setupLearnMoreButton(Context context, Object alertDialogBuilder) {
mHelper.setupLearnMoreButtonToShowAdminPolicies(
context,
alertDialogBuilder,
mEnforcementAdminUserId,
mEnforcedAdmin); mEnforcedAdmin);
} }
@Override @Override
public String getAdminSupportTitle(@Nullable String restriction) { public String getAdminSupportTitle(@Nullable String restriction) {
return mDeviceAdminStringProvider.getDisabledByPolicyTitleForFinancedDevice(); return mStringProvider.getDisabledByPolicyTitleForFinancedDevice();
} }
@Override @Override

View File

@@ -16,55 +16,39 @@
package com.android.settingslib.enterprise; package com.android.settingslib.enterprise;
import static java.util.Objects.requireNonNull;
import android.annotation.UserIdInt;
import android.app.admin.DevicePolicyManager; import android.app.admin.DevicePolicyManager;
import android.content.Context; import android.content.Context;
import android.os.UserManager; import android.os.UserManager;
import android.text.TextUtils; import android.text.TextUtils;
import com.android.settingslib.RestrictedLockUtils; import androidx.annotation.Nullable;
/** /**
* An {@link ActionDisabledByAdminController} to be used with managed devices. * An {@link ActionDisabledByAdminController} to be used with managed devices.
*/ */
class ManagedDeviceActionDisabledByAdminController implements final class ManagedDeviceActionDisabledByAdminController
ActionDisabledByAdminController { extends BaseActionDisabledByAdminController {
private @UserIdInt int mEnforcementAdminUserId;
private RestrictedLockUtils.EnforcedAdmin mEnforcedAdmin;
private final ActionDisabledLearnMoreButtonLauncher mHelper;
private final DeviceAdminStringProvider mStringProvider;
ManagedDeviceActionDisabledByAdminController( ManagedDeviceActionDisabledByAdminController(DeviceAdminStringProvider stringProvider) {
ActionDisabledLearnMoreButtonLauncher helper, super(stringProvider);
DeviceAdminStringProvider stringProvider) {
mHelper = requireNonNull(helper);
mStringProvider = requireNonNull(stringProvider);
} }
@Override @Override
public void updateEnforcedAdmin(RestrictedLockUtils.EnforcedAdmin admin, int adminUserId) { public void setupLearnMoreButton(Context context) {
mEnforcementAdminUserId = adminUserId; assertInitialized();
mEnforcedAdmin = requireNonNull(admin, "admin cannot be null");
}
@Override
public void setupLearnMoreButton(Context context, Object alertDialogBuilder) {
String url = mStringProvider.getLearnMoreHelpPageUrl(); String url = mStringProvider.getLearnMoreHelpPageUrl();
if (TextUtils.isEmpty(url)) { if (TextUtils.isEmpty(url)) {
mHelper.setupLearnMoreButtonToShowAdminPolicies( mLauncher.setupLearnMoreButtonToShowAdminPolicies(context, mEnforcementAdminUserId,
context,
alertDialogBuilder,
mEnforcementAdminUserId,
mEnforcedAdmin); mEnforcedAdmin);
} else { } else {
mHelper.setupLearnMoreButtonToLaunchHelpPage(context, alertDialogBuilder, url); mLauncher.setupLearnMoreButtonToLaunchHelpPage(context, url);
} }
} }
@Override @Override
public String getAdminSupportTitle(String restriction) { public String getAdminSupportTitle(@Nullable String restriction) {
if (restriction == null) { if (restriction == null) {
return mStringProvider.getDefaultDisabledByPolicyTitle(); return mStringProvider.getDefaultDisabledByPolicyTitle();
} }
@@ -88,9 +72,8 @@ class ManagedDeviceActionDisabledByAdminController implements
@Override @Override
public CharSequence getAdminSupportContentString(Context context, CharSequence supportMessage) { public CharSequence getAdminSupportContentString(Context context, CharSequence supportMessage) {
if (supportMessage != null) { return supportMessage != null
return supportMessage; ? supportMessage
} : mStringProvider.getDefaultDisabledByPolicyContent();
return mStringProvider.getDefaultDisabledByPolicyContent();
} }
} }

View File

@@ -45,6 +45,7 @@ android_robolectric_test {
"SettingsLib-robo-testutils", "SettingsLib-robo-testutils",
"androidx.test.core", "androidx.test.core",
"androidx.core_core", "androidx.core_core",
"testng", // TODO: remove once JUnit on Android provides assertThrows
], ],
java_resource_dirs: ["config"], java_resource_dirs: ["config"],
instrumentation_for: "SettingsLibShell", instrumentation_for: "SettingsLibShell",

View File

@@ -16,51 +16,81 @@
package com.android.settingslib.enterprise; package com.android.settingslib.enterprise;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertWithMessage;
import android.app.Activity; import android.content.ComponentName;
import android.content.Context; import android.content.Context;
import android.os.UserHandle;
import android.util.DebugUtils;
import androidx.appcompat.app.AlertDialog; import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
import com.android.settingslib.RestrictedLockUtils;
/** /**
* Utils related to the action disabled by admin dialogs. * Utils related to the action disabled by admin dialogs.
*/ */
class ActionDisabledByAdminControllerTestUtils { // NOTE: must be public because of DebugUtils.constantToString() call
static final int LEARN_MORE_ACTION_NONE = 0; public final class ActionDisabledByAdminControllerTestUtils {
static final int LEARN_MORE_ACTION_SHOW_ADMIN_POLICIES = 1;
static final int LEARN_MORE_ACTION_LAUNCH_HELP_PAGE = 2; static final int ENFORCEMENT_ADMIN_USER_ID = 123;
static final UserHandle ENFORCEMENT_ADMIN_USER = UserHandle.of(ENFORCEMENT_ADMIN_USER_ID);
static final String SUPPORT_MESSAGE = "support message";
static final ComponentName ADMIN_COMPONENT =
new ComponentName("some.package.name", "some.package.name.SomeClass");
static final EnforcedAdmin ENFORCED_ADMIN = new EnforcedAdmin(
ADMIN_COMPONENT, UserHandle.of(ENFORCEMENT_ADMIN_USER_ID));
static final EnforcedAdmin ENFORCED_ADMIN_WITHOUT_COMPONENT = new EnforcedAdmin(
/* component= */ null, UserHandle.of(ENFORCEMENT_ADMIN_USER_ID));
static final String URL = "https://testexample.com";
// NOTE: fields below must be public because of DebugUtils.constantToString() call
public static final int LEARN_MORE_ACTION_NONE = 0;
public static final int LEARN_MORE_ACTION_SHOW_ADMIN_POLICIES = 1;
public static final int LEARN_MORE_ACTION_SHOW_ADMIN_SETTINGS = 2;
public static final int LEARN_MORE_ACTION_LAUNCH_HELP_PAGE = 3;
private int mLearnMoreButtonAction = LEARN_MORE_ACTION_NONE; private int mLearnMoreButtonAction = LEARN_MORE_ACTION_NONE;
ActionDisabledLearnMoreButtonLauncher createLearnMoreButtonLauncher() { ActionDisabledLearnMoreButtonLauncher createLearnMoreButtonLauncher() {
return new ActionDisabledLearnMoreButtonLauncher() { return new ActionDisabledLearnMoreButtonLauncher() {
@Override @Override
public void setupLearnMoreButtonToShowAdminPolicies(Context context, public void setLearnMoreButton(Runnable action) {
Object alertDialogBuilder, int enforcementAdminUserId, action.run();
RestrictedLockUtils.EnforcedAdmin enforcedAdmin) { }
@Override
protected void launchShowAdminPolicies(Context context, UserHandle user,
ComponentName admin) {
mLearnMoreButtonAction = LEARN_MORE_ACTION_SHOW_ADMIN_POLICIES; mLearnMoreButtonAction = LEARN_MORE_ACTION_SHOW_ADMIN_POLICIES;
} }
@Override @Override
public void setupLearnMoreButtonToLaunchHelpPage(Context context, protected void launchShowAdminSettings(Context context) {
Object alertDialogBuilder, String url) { mLearnMoreButtonAction = LEARN_MORE_ACTION_SHOW_ADMIN_SETTINGS;
}
@Override
public void showHelpPage(Context context, String url) {
mLearnMoreButtonAction = LEARN_MORE_ACTION_LAUNCH_HELP_PAGE; mLearnMoreButtonAction = LEARN_MORE_ACTION_LAUNCH_HELP_PAGE;
} }
@Override
protected boolean isSameProfileGroup(Context context, int enforcementAdminUserId) {
return true;
}
}; };
} }
void assertLearnMoreAction(int learnMoreActionShowAdminPolicies) { void assertLearnMoreAction(int learnMoreActionShowAdminPolicies) {
assertThat(learnMoreActionShowAdminPolicies).isEqualTo(mLearnMoreButtonAction); assertWithMessage("action").that(actionToString(mLearnMoreButtonAction))
.isEqualTo(actionToString(learnMoreActionShowAdminPolicies));
} }
AlertDialog createAlertDialog(ActionDisabledByAdminController mController, Activity activity) { private static String actionToString(int action) {
AlertDialog.Builder builder = new AlertDialog.Builder(activity); return DebugUtils.constantToString(ActionDisabledByAdminControllerTestUtils.class,
mController.setupLearnMoreButton(activity, builder); "LEARN_MORE_ACTION_", action);
AlertDialog alertDialog = builder.create();
alertDialog.show();
return alertDialog;
} }
} }

View File

@@ -0,0 +1,175 @@
/*
* Copyright (C) 2021 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.settingslib.enterprise;
import static com.android.settingslib.enterprise.ActionDisabledByAdminControllerTestUtils.ADMIN_COMPONENT;
import static com.android.settingslib.enterprise.ActionDisabledByAdminControllerTestUtils.ENFORCED_ADMIN;
import static com.android.settingslib.enterprise.ActionDisabledByAdminControllerTestUtils.ENFORCED_ADMIN_WITHOUT_COMPONENT;
import static com.android.settingslib.enterprise.ActionDisabledByAdminControllerTestUtils.ENFORCEMENT_ADMIN_USER;
import static com.android.settingslib.enterprise.ActionDisabledByAdminControllerTestUtils.ENFORCEMENT_ADMIN_USER_ID;
import static com.android.settingslib.enterprise.ActionDisabledByAdminControllerTestUtils.URL;
import static com.google.common.truth.Truth.assertWithMessage;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.testng.Assert.assertThrows;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.UserHandle;
import android.os.UserManager;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.Spy;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
import org.robolectric.RobolectricTestRunner;
@RunWith(RobolectricTestRunner.class)// NOTE: this test doesn't need RoboElectric...
public final class ActionDisabledLearnMoreButtonLauncherTest {
private static final int CONTEXT_USER_ID = -ENFORCEMENT_ADMIN_USER_ID;
private static final UserHandle CONTEXT_USER = UserHandle.of(CONTEXT_USER_ID);
@Rule
public final MockitoRule mMockitoRule = MockitoJUnit.rule();
@Mock
private Context mContext;
@Mock
private UserManager mUserManager;
@Spy
private ActionDisabledLearnMoreButtonLauncher mLauncher;
@Captor
private ArgumentCaptor<Runnable> mLearnMoreActionCaptor;
@Captor
private ArgumentCaptor<Intent> mIntentCaptor;
@Before
public void setUp() {
when(mContext.getUserId()).thenReturn(CONTEXT_USER_ID);
when(mUserManager.getUserHandle()).thenReturn(CONTEXT_USER_ID);
when(mContext.getSystemService(UserManager.class)).thenReturn(mUserManager);
}
@Test
public void testSetupLearnMoreButtonToShowAdminPolicies_nullContext() {
assertThrows(NullPointerException.class,
() -> mLauncher.setupLearnMoreButtonToShowAdminPolicies(/* context= */ null,
ENFORCEMENT_ADMIN_USER_ID, ENFORCED_ADMIN));
}
@Test
public void testSetupLearnMoreButtonToShowAdminPolicies_nullEnforcedAdmin() {
assertThrows(NullPointerException.class,
() -> mLauncher.setupLearnMoreButtonToShowAdminPolicies(/* context= */ null,
ENFORCEMENT_ADMIN_USER_ID, /* enforcedAdmin= */ null));
}
@Test
public void testSetupLearnMoreButtonToShowAdminPolicies_differentProfileGroup() {
mockDifferentProfileGroup();
mLauncher.setupLearnMoreButtonToShowAdminPolicies(mContext, ENFORCEMENT_ADMIN_USER_ID,
ENFORCED_ADMIN);
verify(mLauncher, never()).setLearnMoreButton(any());
}
@Test
public void testSetupLearnMoreButtonToShowAdminPolicies_sameProfileGroup_noComponent() {
mockSameProfileGroup();
mLauncher.setupLearnMoreButtonToShowAdminPolicies(mContext, ENFORCEMENT_ADMIN_USER_ID,
ENFORCED_ADMIN_WITHOUT_COMPONENT);
tapLearnMore();
verify(mLauncher, never()).launchShowAdminPolicies(any(), any(), any());
verify(mLauncher).launchShowAdminSettings(mContext);
verifyFinishSelf();
}
@Test
public void testSetupLearnMoreButtonToShowAdminPolicies_sameProfileGroup_withComponent() {
mockSameProfileGroup();
mLauncher.setupLearnMoreButtonToShowAdminPolicies(mContext, ENFORCEMENT_ADMIN_USER_ID,
ENFORCED_ADMIN);
tapLearnMore();
verify(mLauncher).launchShowAdminPolicies(mContext, ENFORCEMENT_ADMIN_USER,
ADMIN_COMPONENT);
verify(mLauncher, never()).launchShowAdminSettings(any());
verifyFinishSelf();
}
@Test
public void testSetupLearnMoreButtonToLaunchHelpPage_nullContext() {
assertThrows(NullPointerException.class,
() -> mLauncher.setupLearnMoreButtonToLaunchHelpPage(/* context= */ null, URL));
}
@Test
public void testSetupLearnMoreButtonToLaunchHelpPage_nullUrl() {
assertThrows(NullPointerException.class,
() -> mLauncher.setupLearnMoreButtonToLaunchHelpPage(mContext, /* url= */ null));
}
@Test
public void testSetupLearnMoreButtonToLaunchHelpPage() {
mLauncher.setupLearnMoreButtonToLaunchHelpPage(mContext, URL);
tapLearnMore();
verify(mContext).startActivityAsUser(mIntentCaptor.capture(), eq(CONTEXT_USER));
Intent intent = mIntentCaptor.getValue();
assertWithMessage("wrong url on intent %s", intent).that(intent.getData())
.isEqualTo(Uri.parse(URL));
verifyFinishSelf();
}
private void mockDifferentProfileGroup() {
// No need to mock anything - isSameProfileGroup() will return false by default
}
private void mockSameProfileGroup() {
when(mUserManager.isSameProfileGroup(ENFORCEMENT_ADMIN_USER_ID, CONTEXT_USER_ID))
.thenReturn(true);
}
private void tapLearnMore() {
verify(mLauncher).setLearnMoreButton(mLearnMoreActionCaptor.capture());
mLearnMoreActionCaptor.getValue().run();
}
private void verifyFinishSelf() {
verify(mLauncher).finishSelf();
}
}

View File

@@ -30,6 +30,8 @@ class FakeDeviceAdminStringProvider implements DeviceAdminStringProvider {
static final String DEFAULT_DISABLED_BY_POLICY_CONTENT = "default_disabled_by_policy_content"; static final String DEFAULT_DISABLED_BY_POLICY_CONTENT = "default_disabled_by_policy_content";
static final String DEFAULT_DISABLED_BY_POLICY_TITLE_FINANCED_DEVICE = static final String DEFAULT_DISABLED_BY_POLICY_TITLE_FINANCED_DEVICE =
"default_disabled_by_policy_title_financed_device"; "default_disabled_by_policy_title_financed_device";
static final DeviceAdminStringProvider DEFAULT_DEVICE_ADMIN_STRING_PROVIDER =
new FakeDeviceAdminStringProvider(/* url = */ null);
private final String mUrl; private final String mUrl;

View File

@@ -16,22 +16,21 @@
package com.android.settingslib.enterprise; package com.android.settingslib.enterprise;
import static com.android.settingslib.enterprise.ActionDisabledByAdminControllerTestUtils.ENFORCED_ADMIN;
import static com.android.settingslib.enterprise.ActionDisabledByAdminControllerTestUtils.ENFORCEMENT_ADMIN_USER_ID;
import static com.android.settingslib.enterprise.ActionDisabledByAdminControllerTestUtils.LEARN_MORE_ACTION_SHOW_ADMIN_POLICIES; import static com.android.settingslib.enterprise.ActionDisabledByAdminControllerTestUtils.LEARN_MORE_ACTION_SHOW_ADMIN_POLICIES;
import static com.android.settingslib.enterprise.ActionDisabledByAdminControllerTestUtils.SUPPORT_MESSAGE;
import static com.android.settingslib.enterprise.FakeDeviceAdminStringProvider.DEFAULT_DEVICE_ADMIN_STRING_PROVIDER;
import static com.android.settingslib.enterprise.FakeDeviceAdminStringProvider.DEFAULT_DISABLED_BY_POLICY_TITLE_FINANCED_DEVICE; import static com.android.settingslib.enterprise.FakeDeviceAdminStringProvider.DEFAULT_DISABLED_BY_POLICY_TITLE_FINANCED_DEVICE;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import android.app.Activity; import android.app.Activity;
import android.app.Dialog;
import android.content.ComponentName;
import android.content.Context; import android.content.Context;
import android.os.UserHandle;
import androidx.appcompat.app.AlertDialog;
import androidx.test.core.app.ApplicationProvider; import androidx.test.core.app.ApplicationProvider;
import com.android.settingslib.R; import com.android.settingslib.R;
import com.android.settingslib.RestrictedLockUtils;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@@ -41,73 +40,46 @@ import org.robolectric.android.controller.ActivityController;
@RunWith(RobolectricTestRunner.class) @RunWith(RobolectricTestRunner.class)
public class FinancedDeviceActionDisabledByAdminControllerTest { public class FinancedDeviceActionDisabledByAdminControllerTest {
private static final int ENFORCEMENT_ADMIN_USER_ID = 123;
private static final ComponentName ADMIN_COMPONENT =
new ComponentName("some.package.name", "some.package.name.SomeClass");
private static final String SUPPORT_MESSAGE = "support message";
private static final DeviceAdminStringProvider DEVICE_ADMIN_STRING_PROVIDER =
new FakeDeviceAdminStringProvider(/* url = */ null);
private static final RestrictedLockUtils.EnforcedAdmin ENFORCED_ADMIN =
new RestrictedLockUtils.EnforcedAdmin(
ADMIN_COMPONENT, UserHandle.of(ENFORCEMENT_ADMIN_USER_ID));
private final Context mContext = ApplicationProvider.getApplicationContext(); private final Context mContext = ApplicationProvider.getApplicationContext();
private final Activity mActivity = ActivityController.of(new Activity()).get(); private final Activity mActivity = ActivityController.of(new Activity()).get();
private final ActionDisabledByAdminControllerTestUtils mTestUtils = private final ActionDisabledByAdminControllerTestUtils mTestUtils =
new ActionDisabledByAdminControllerTestUtils(); new ActionDisabledByAdminControllerTestUtils();
private final ActionDisabledLearnMoreButtonLauncher mLauncher = private final FinancedDeviceActionDisabledByAdminController mController =
mTestUtils.createLearnMoreButtonLauncher(); new FinancedDeviceActionDisabledByAdminController(
DEFAULT_DEVICE_ADMIN_STRING_PROVIDER);
@Before @Before
public void setUp() { public void setUp() {
mActivity.setTheme(R.style.Theme_AppCompat_DayNight); mActivity.setTheme(R.style.Theme_AppCompat_DayNight);
mController.initialize(mTestUtils.createLearnMoreButtonLauncher());
mController.updateEnforcedAdmin(ENFORCED_ADMIN, ENFORCEMENT_ADMIN_USER_ID);
} }
@Test @Test
public void setupLearnMoreButton_negativeButtonSet() { public void setupLearnMoreButton_negativeButtonSet() {
FinancedDeviceActionDisabledByAdminController mController = createController(mLauncher); mController.setupLearnMoreButton(mContext);
AlertDialog alertDialog = mTestUtils.createAlertDialog(mController, mActivity);
alertDialog.getButton(Dialog.BUTTON_NEUTRAL).performClick();
mTestUtils.assertLearnMoreAction(LEARN_MORE_ACTION_SHOW_ADMIN_POLICIES); mTestUtils.assertLearnMoreAction(LEARN_MORE_ACTION_SHOW_ADMIN_POLICIES);
} }
@Test @Test
public void getAdminSupportTitleResource_works() { public void getAdminSupportTitleResource_works() {
FinancedDeviceActionDisabledByAdminController mController = createController();
assertThat(mController.getAdminSupportTitle(null)) assertThat(mController.getAdminSupportTitle(null))
.isEqualTo(DEFAULT_DISABLED_BY_POLICY_TITLE_FINANCED_DEVICE); .isEqualTo(DEFAULT_DISABLED_BY_POLICY_TITLE_FINANCED_DEVICE);
} }
@Test @Test
public void getAdminSupportContentString_withSupportMessage_returnsSupportMessage() { public void getAdminSupportContentString_withSupportMessage_returnsSupportMessage() {
FinancedDeviceActionDisabledByAdminController mController = createController();
assertThat(mController.getAdminSupportContentString(mContext, SUPPORT_MESSAGE)) assertThat(mController.getAdminSupportContentString(mContext, SUPPORT_MESSAGE))
.isEqualTo(SUPPORT_MESSAGE); .isEqualTo(SUPPORT_MESSAGE);
} }
@Test @Test
public void getAdminSupportContentString_noSupportMessage_returnsNull() { public void getAdminSupportContentString_noSupportMessage_returnsNull() {
FinancedDeviceActionDisabledByAdminController mController = createController();
assertThat(mController.getAdminSupportContentString(mContext, /* supportMessage= */ null)) assertThat(mController.getAdminSupportContentString(mContext, /* supportMessage= */ null))
.isNull(); .isNull();
} }
private FinancedDeviceActionDisabledByAdminController createController() {
return createController(mLauncher);
}
private FinancedDeviceActionDisabledByAdminController createController(
ActionDisabledLearnMoreButtonLauncher buttonHelper) {
FinancedDeviceActionDisabledByAdminController controller =
new FinancedDeviceActionDisabledByAdminController(
buttonHelper,
DEVICE_ADMIN_STRING_PROVIDER);
controller.updateEnforcedAdmin(ENFORCED_ADMIN, ENFORCEMENT_ADMIN_USER_ID);
return controller;
}
} }

View File

@@ -16,8 +16,12 @@
package com.android.settingslib.enterprise; package com.android.settingslib.enterprise;
import static com.android.settingslib.enterprise.ActionDisabledByAdminControllerTestUtils.ENFORCED_ADMIN;
import static com.android.settingslib.enterprise.ActionDisabledByAdminControllerTestUtils.ENFORCEMENT_ADMIN_USER_ID;
import static com.android.settingslib.enterprise.ActionDisabledByAdminControllerTestUtils.LEARN_MORE_ACTION_LAUNCH_HELP_PAGE; import static com.android.settingslib.enterprise.ActionDisabledByAdminControllerTestUtils.LEARN_MORE_ACTION_LAUNCH_HELP_PAGE;
import static com.android.settingslib.enterprise.ActionDisabledByAdminControllerTestUtils.LEARN_MORE_ACTION_SHOW_ADMIN_POLICIES; import static com.android.settingslib.enterprise.ActionDisabledByAdminControllerTestUtils.LEARN_MORE_ACTION_SHOW_ADMIN_POLICIES;
import static com.android.settingslib.enterprise.ActionDisabledByAdminControllerTestUtils.SUPPORT_MESSAGE;
import static com.android.settingslib.enterprise.ActionDisabledByAdminControllerTestUtils.URL;
import static com.android.settingslib.enterprise.FakeDeviceAdminStringProvider.DEFAULT_DISABLED_BY_POLICY_CONTENT; import static com.android.settingslib.enterprise.FakeDeviceAdminStringProvider.DEFAULT_DISABLED_BY_POLICY_CONTENT;
import static com.android.settingslib.enterprise.FakeDeviceAdminStringProvider.DEFAULT_DISABLED_BY_POLICY_TITLE; import static com.android.settingslib.enterprise.FakeDeviceAdminStringProvider.DEFAULT_DISABLED_BY_POLICY_TITLE;
import static com.android.settingslib.enterprise.FakeDeviceAdminStringProvider.DISALLOW_ADJUST_VOLUME_TITLE; import static com.android.settingslib.enterprise.FakeDeviceAdminStringProvider.DISALLOW_ADJUST_VOLUME_TITLE;
@@ -25,15 +29,12 @@ import static com.android.settingslib.enterprise.FakeDeviceAdminStringProvider.D
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import android.app.Activity; import android.app.Activity;
import android.app.Dialog; import android.content.Context;
import android.content.ComponentName;
import android.os.UserHandle;
import android.os.UserManager; import android.os.UserManager;
import androidx.appcompat.app.AlertDialog; import androidx.test.core.app.ApplicationProvider;
import com.android.settingslib.R; import com.android.settingslib.R;
import com.android.settingslib.RestrictedLockUtils;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@@ -43,23 +44,15 @@ import org.robolectric.android.controller.ActivityController;
@RunWith(RobolectricTestRunner.class) @RunWith(RobolectricTestRunner.class)
public class ManagedDeviceActionDisabledByAdminControllerTest { public class ManagedDeviceActionDisabledByAdminControllerTest {
private static final int ENFORCEMENT_ADMIN_USER_ID = 123;
private static final ComponentName ADMIN_COMPONENT =
new ComponentName("some.package.name", "some.package.name.SomeClass");
private static final String SUPPORT_MESSAGE = "support message";
private static final String RESTRICTION = UserManager.DISALLOW_ADJUST_VOLUME; private static final String RESTRICTION = UserManager.DISALLOW_ADJUST_VOLUME;
private static final String URL = "https://testexample.com";
private static final String EMPTY_URL = ""; private static final String EMPTY_URL = "";
private static final RestrictedLockUtils.EnforcedAdmin ENFORCED_ADMIN =
new RestrictedLockUtils.EnforcedAdmin(
ADMIN_COMPONENT, UserHandle.of(ENFORCEMENT_ADMIN_USER_ID));
private static final String SUPPORT_TITLE_FOR_RESTRICTION = DISALLOW_ADJUST_VOLUME_TITLE; private static final String SUPPORT_TITLE_FOR_RESTRICTION = DISALLOW_ADJUST_VOLUME_TITLE;
private final Context mContext = ApplicationProvider.getApplicationContext();
private final Activity mActivity = ActivityController.of(new Activity()).get(); private final Activity mActivity = ActivityController.of(new Activity()).get();
private final ActionDisabledByAdminControllerTestUtils mTestUtils = private final ActionDisabledByAdminControllerTestUtils mTestUtils =
new ActionDisabledByAdminControllerTestUtils(); new ActionDisabledByAdminControllerTestUtils();
private final ActionDisabledLearnMoreButtonLauncher mLauncher =
mTestUtils.createLearnMoreButtonLauncher();
@Before @Before
public void setUp() { public void setUp() {
@@ -68,68 +61,63 @@ public class ManagedDeviceActionDisabledByAdminControllerTest {
@Test @Test
public void setupLearnMoreButton_validUrl_negativeButtonSet() { public void setupLearnMoreButton_validUrl_negativeButtonSet() {
ManagedDeviceActionDisabledByAdminController mController = ManagedDeviceActionDisabledByAdminController controller = createController(URL);
createController(mLauncher, URL);
AlertDialog alertDialog = mTestUtils.createAlertDialog(mController, mActivity);
alertDialog.getButton(Dialog.BUTTON_NEUTRAL).performClick(); controller.setupLearnMoreButton(mContext);
mTestUtils.assertLearnMoreAction(LEARN_MORE_ACTION_LAUNCH_HELP_PAGE); mTestUtils.assertLearnMoreAction(LEARN_MORE_ACTION_LAUNCH_HELP_PAGE);
} }
@Test @Test
public void setupLearnMoreButton_noUrl_negativeButtonSet() { public void setupLearnMoreButton_noUrl_negativeButtonSet() {
ManagedDeviceActionDisabledByAdminController mController = ManagedDeviceActionDisabledByAdminController controller = createController(EMPTY_URL);
createController(mLauncher, EMPTY_URL);
AlertDialog alertDialog = mTestUtils.createAlertDialog(mController, mActivity);
alertDialog.getButton(Dialog.BUTTON_NEUTRAL).performClick(); controller.setupLearnMoreButton(mContext);
mTestUtils.assertLearnMoreAction(LEARN_MORE_ACTION_SHOW_ADMIN_POLICIES); mTestUtils.assertLearnMoreAction(LEARN_MORE_ACTION_SHOW_ADMIN_POLICIES);
} }
@Test @Test
public void getAdminSupportTitleResource_noRestriction_works() { public void getAdminSupportTitleResource_noRestriction_works() {
ManagedDeviceActionDisabledByAdminController mController = createController(); ManagedDeviceActionDisabledByAdminController controller = createController();
assertThat(mController.getAdminSupportTitle(null)) assertThat(controller.getAdminSupportTitle(null))
.isEqualTo(DEFAULT_DISABLED_BY_POLICY_TITLE); .isEqualTo(DEFAULT_DISABLED_BY_POLICY_TITLE);
} }
@Test @Test
public void getAdminSupportTitleResource_withRestriction_works() { public void getAdminSupportTitleResource_withRestriction_works() {
ManagedDeviceActionDisabledByAdminController mController = createController(); ManagedDeviceActionDisabledByAdminController controller = createController();
assertThat(mController.getAdminSupportTitle(RESTRICTION)) assertThat(controller.getAdminSupportTitle(RESTRICTION))
.isEqualTo(SUPPORT_TITLE_FOR_RESTRICTION); .isEqualTo(SUPPORT_TITLE_FOR_RESTRICTION);
} }
@Test @Test
public void getAdminSupportContentString_withSupportMessage_returnsSupportMessage() { public void getAdminSupportContentString_withSupportMessage_returnsSupportMessage() {
ManagedDeviceActionDisabledByAdminController mController = createController(); ManagedDeviceActionDisabledByAdminController controller = createController();
assertThat(mController.getAdminSupportContentString(mActivity, SUPPORT_MESSAGE)) assertThat(controller.getAdminSupportContentString(mActivity, SUPPORT_MESSAGE))
.isEqualTo(SUPPORT_MESSAGE); .isEqualTo(SUPPORT_MESSAGE);
} }
@Test @Test
public void getAdminSupportContentString_noSupportMessage_returnsDefault() { public void getAdminSupportContentString_noSupportMessage_returnsDefault() {
ManagedDeviceActionDisabledByAdminController mController = createController(); ManagedDeviceActionDisabledByAdminController controller = createController();
assertThat(mController.getAdminSupportContentString(mActivity, /* supportMessage= */ null)) assertThat(controller.getAdminSupportContentString(mActivity, /* supportMessage= */ null))
.isEqualTo(DEFAULT_DISABLED_BY_POLICY_CONTENT); .isEqualTo(DEFAULT_DISABLED_BY_POLICY_CONTENT);
} }
private ManagedDeviceActionDisabledByAdminController createController() { private ManagedDeviceActionDisabledByAdminController createController() {
return createController(mLauncher, /* url= */ null); return createController(/* url= */ null);
} }
private ManagedDeviceActionDisabledByAdminController createController( private ManagedDeviceActionDisabledByAdminController createController(String url) {
ActionDisabledLearnMoreButtonLauncher buttonHelper, String url) {
ManagedDeviceActionDisabledByAdminController controller = ManagedDeviceActionDisabledByAdminController controller =
new ManagedDeviceActionDisabledByAdminController( new ManagedDeviceActionDisabledByAdminController(
buttonHelper,
new FakeDeviceAdminStringProvider(url)); new FakeDeviceAdminStringProvider(url));
controller.initialize(mTestUtils.createLearnMoreButtonLauncher());
controller.updateEnforcedAdmin(ENFORCED_ADMIN, ENFORCEMENT_ADMIN_USER_ID); controller.updateEnforcedAdmin(ENFORCED_ADMIN, ENFORCEMENT_ADMIN_USER_ID);
return controller; return controller;
} }