SettingsLib logic for updated ActionDisabledByAdminDialogHelper

Fixes: 184107103
Test: manual
Test: atest FinancedDeviceActionDisabledByAdminControllerTest
Test: atest ManagedDeviceActionDisabledByAdminControllerTest
Change-Id: I894808fbde2bc325e7a0f5697dca760b631f210f
This commit is contained in:
arangelov
2021-05-14 18:05:54 +01:00
parent 0d88f8065d
commit 51fd49accb
11 changed files with 797 additions and 0 deletions

View File

@@ -0,0 +1,53 @@
/*
* 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 android.app.Activity;
import android.content.Context;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog.Builder;
import com.android.settingslib.RestrictedLockUtils;
/**
* A controller used to customize the action disabled by admin dialog.
*/
public interface ActionDisabledByAdminController {
/**
* Handles the adding and setting up of the learn more button. If button is not needed, then
* this method can be left empty.
*/
void setupLearnMoreButton(Activity activity, Builder builder);
/**
* Returns the admin support dialog's title resource id.
*/
String getAdminSupportTitle(@Nullable String restriction);
/**
* Returns the admin support dialog's content string.
*/
CharSequence getAdminSupportContentString(
Context context, @Nullable CharSequence supportMessage);
/**
* Updates the enforced admin
*/
void updateEnforcedAdmin(RestrictedLockUtils.EnforcedAdmin admin, int adminUserId);
}

View File

@@ -0,0 +1,47 @@
/*
* 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 android.app.admin.DevicePolicyManager.DEVICE_OWNER_TYPE_FINANCED;
import android.app.admin.DevicePolicyManager;
/**
* A factory that returns the relevant instance of {@link ActionDisabledByAdminController}.
*/
public class ActionDisabledByAdminControllerFactory {
/**
* Returns the relevant instance of {@link ActionDisabledByAdminController}.
*/
public static ActionDisabledByAdminController createInstance(
DevicePolicyManager dpm,
ActionDisabledLearnMoreButtonLauncher helper,
DeviceAdminStringProvider deviceAdminStringProvider) {
if (isFinancedDevice(dpm)) {
return new FinancedDeviceActionDisabledByAdminController(
helper, deviceAdminStringProvider);
}
return new ManagedDeviceActionDisabledByAdminController(
helper, deviceAdminStringProvider);
}
private static boolean isFinancedDevice(DevicePolicyManager dpm) {
return dpm.isDeviceManaged() && dpm.getDeviceOwnerType(
dpm.getDeviceOwnerComponentOnAnyUser()) == DEVICE_OWNER_TYPE_FINANCED;
}
}

View File

@@ -0,0 +1,46 @@
/*
* 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 android.app.Activity;
import androidx.appcompat.app.AlertDialog;
import com.android.settingslib.RestrictedLockUtils;
/**
* Helper interface meant to set up the "Learn more" button in the action disabled dialog.
*/
public interface ActionDisabledLearnMoreButtonLauncher {
/**
* Sets up a "learn more" button which shows a screen with device policy settings
*/
void setupLearnMoreButtonToShowAdminPolicies(
Activity activity,
AlertDialog.Builder builder,
int enforcementAdminUserId,
RestrictedLockUtils.EnforcedAdmin enforcedAdmin);
/**
* Sets up a "learn more" button which launches a help page
*/
void setupLearnMoreButtonToLaunchHelpPage(
Activity activity,
AlertDialog.Builder builder,
String url);
}

View File

@@ -0,0 +1,75 @@
/*
* 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;
/**
* A {@code String} provider for the action disabled by admin dialog.
*/
public interface DeviceAdminStringProvider {
/**
* Returns the default dialog title for the case when an action is disabled by policy on a
* managed device.
*/
String getDefaultDisabledByPolicyTitle();
/**
* Returns the dialog title for the case when volume adjusting is disabled.
*/
String getDisallowAdjustVolumeTitle();
/**
* Returns the dialog title for the case when outgoing calls are disabled.
*/
String getDisallowOutgoingCallsTitle();
/**
* Returns the dialog title for the case when sending SMS is disabled.
*/
String getDisallowSmsTitle();
/**
* Returns the dialog title for the case when the camera is disabled.
*/
String getDisableCameraTitle();
/**
* Returns the dialog title for the case when screen capturing is disabled.
*/
String getDisableScreenCaptureTitle();
/**
* Returns the dialog title for the case when suspending apps is disabled.
*/
String getSuspendPackagesTitle();
/**
* Returns the default dialog content for the case when an action is disabled by policy.
*/
String getDefaultDisabledByPolicyContent();
/**
* Returns the URL for the page to be shown when the learn more button is chosen.
*/
String getLearnMoreHelpPageUrl();
/**
* Returns the default dialog title for the case when an action is disabled by policy on
* a financed device.
*/
String getDisabledByPolicyTitleForFinancedDevice();
}

View File

@@ -0,0 +1,72 @@
/*
* 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 android.app.Activity;
import android.content.Context;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog.Builder;
import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
/**
* An {@link ActionDisabledByAdminController} to be used with financed devices.
*/
public class FinancedDeviceActionDisabledByAdminController
implements ActionDisabledByAdminController {
private @UserIdInt int mEnforcementAdminUserId;
private EnforcedAdmin mEnforcedAdmin;
private final ActionDisabledLearnMoreButtonLauncher mHelper;
private final DeviceAdminStringProvider mDeviceAdminStringProvider;
FinancedDeviceActionDisabledByAdminController(
ActionDisabledLearnMoreButtonLauncher helper,
DeviceAdminStringProvider deviceAdminStringProvider) {
mHelper = requireNonNull(helper);
mDeviceAdminStringProvider = requireNonNull(deviceAdminStringProvider);
}
@Override
public void updateEnforcedAdmin(EnforcedAdmin admin, int adminUserId) {
mEnforcementAdminUserId = adminUserId;
mEnforcedAdmin = requireNonNull(admin);
}
@Override
public void setupLearnMoreButton(Activity activity, Builder builder) {
mHelper.setupLearnMoreButtonToShowAdminPolicies(
activity,
builder,
mEnforcementAdminUserId,
mEnforcedAdmin);
}
@Override
public String getAdminSupportTitle(@Nullable String restriction) {
return mDeviceAdminStringProvider.getDisabledByPolicyTitleForFinancedDevice();
}
@Override
public CharSequence getAdminSupportContentString(Context context, CharSequence supportMessage) {
return supportMessage;
}
}

View File

@@ -0,0 +1,99 @@
/*
* 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 android.app.Activity;
import android.app.admin.DevicePolicyManager;
import android.content.Context;
import android.os.UserManager;
import android.text.TextUtils;
import androidx.appcompat.app.AlertDialog.Builder;
import com.android.settingslib.RestrictedLockUtils;
/**
* An {@link ActionDisabledByAdminController} to be used with managed devices.
*/
class ManagedDeviceActionDisabledByAdminController implements
ActionDisabledByAdminController {
private @UserIdInt int mEnforcementAdminUserId;
private RestrictedLockUtils.EnforcedAdmin mEnforcedAdmin;
private final ActionDisabledLearnMoreButtonLauncher mHelper;
private final DeviceAdminStringProvider mStringProvider;
ManagedDeviceActionDisabledByAdminController(
ActionDisabledLearnMoreButtonLauncher helper,
DeviceAdminStringProvider stringProvider) {
mHelper = requireNonNull(helper);
mStringProvider = requireNonNull(stringProvider);
}
@Override
public void updateEnforcedAdmin(RestrictedLockUtils.EnforcedAdmin admin, int adminUserId) {
mEnforcementAdminUserId = adminUserId;
mEnforcedAdmin = requireNonNull(admin);
}
@Override
public void setupLearnMoreButton(Activity activity, Builder builder) {
String url = mStringProvider.getLearnMoreHelpPageUrl();
if (TextUtils.isEmpty(url)) {
mHelper.setupLearnMoreButtonToShowAdminPolicies(
activity,
builder,
mEnforcementAdminUserId,
mEnforcedAdmin);
} else {
mHelper.setupLearnMoreButtonToLaunchHelpPage(activity, builder, url);
}
}
@Override
public String getAdminSupportTitle(String restriction) {
if (restriction == null) {
return mStringProvider.getDefaultDisabledByPolicyTitle();
}
switch (restriction) {
case UserManager.DISALLOW_ADJUST_VOLUME:
return mStringProvider.getDisallowAdjustVolumeTitle();
case UserManager.DISALLOW_OUTGOING_CALLS:
return mStringProvider.getDisallowOutgoingCallsTitle();
case UserManager.DISALLOW_SMS:
return mStringProvider.getDisallowSmsTitle();
case DevicePolicyManager.POLICY_DISABLE_CAMERA:
return mStringProvider.getDisableCameraTitle();
case DevicePolicyManager.POLICY_DISABLE_SCREEN_CAPTURE:
return mStringProvider.getDisableScreenCaptureTitle();
case DevicePolicyManager.POLICY_SUSPEND_PACKAGES:
return mStringProvider.getSuspendPackagesTitle();
default:
return mStringProvider.getDefaultDisabledByPolicyTitle();
}
}
@Override
public CharSequence getAdminSupportContentString(Context context, CharSequence supportMessage) {
if (supportMessage != null) {
return supportMessage;
}
return mStringProvider.getDefaultDisabledByPolicyContent();
}
}

View File

@@ -43,6 +43,8 @@ android_robolectric_test {
srcs: ["src/**/*.java"],
static_libs: [
"SettingsLib-robo-testutils",
"androidx.test.core",
"androidx.core_core",
],
java_resource_dirs: ["config"],
instrumentation_for: "SettingsLibShell",

View File

@@ -0,0 +1,65 @@
/*
* 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.google.common.truth.Truth.assertThat;
import android.app.Activity;
import androidx.appcompat.app.AlertDialog;
import com.android.settingslib.RestrictedLockUtils;
/**
* Utils related to the action disabled by admin dialogs.
*/
class ActionDisabledByAdminControllerTestUtils {
static final int LEARN_MORE_ACTION_NONE = 0;
static final int LEARN_MORE_ACTION_SHOW_ADMIN_POLICIES = 1;
static final int LEARN_MORE_ACTION_LAUNCH_HELP_PAGE = 2;
private int mLearnMoreButtonAction = LEARN_MORE_ACTION_NONE;
ActionDisabledLearnMoreButtonLauncher createLearnMoreButtonLauncher() {
return new ActionDisabledLearnMoreButtonLauncher() {
@Override
public void setupLearnMoreButtonToShowAdminPolicies(Activity activity,
AlertDialog.Builder builder, int enforcementAdminUserId,
RestrictedLockUtils.EnforcedAdmin enforcedAdmin) {
mLearnMoreButtonAction = LEARN_MORE_ACTION_SHOW_ADMIN_POLICIES;
}
@Override
public void setupLearnMoreButtonToLaunchHelpPage(Activity activity,
AlertDialog.Builder builder, String url) {
mLearnMoreButtonAction = LEARN_MORE_ACTION_LAUNCH_HELP_PAGE;
}
};
}
void assertLearnMoreAction(int learnMoreActionShowAdminPolicies) {
assertThat(learnMoreActionShowAdminPolicies).isEqualTo(mLearnMoreButtonAction);
}
AlertDialog createAlertDialog(ActionDisabledByAdminController mController, Activity activity) {
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
mController.setupLearnMoreButton(activity, builder);
AlertDialog alertDialog = builder.create();
alertDialog.show();
return alertDialog;
}
}

View File

@@ -0,0 +1,89 @@
/*
* 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 android.annotation.Nullable;
class FakeDeviceAdminStringProvider implements DeviceAdminStringProvider {
static final String DEFAULT_DISABLED_BY_POLICY_TITLE = "default_disabled_by_policy_title";
static final String DISALLOW_ADJUST_VOLUME_TITLE = "disallow_adjust_volume_title";
static final String DISALLOW_OUTGOING_CALLS_TITLE = "disallow_outgoing_calls_title";
static final String DISALLOW_SMS_TITLE = "disallow_sms_title";
static final String DISABLE_CAMERA_TITLE = "disable_camera_title";
static final String DISABLE_SCREEN_CAPTURE_TITLE = "disable_screen_capture_title";
static final String SUSPENDED_PACKAGES_TITLE = "suspended_packages_title";
static final String DEFAULT_DISABLED_BY_POLICY_CONTENT = "default_disabled_by_policy_content";
static final String DEFAULT_DISABLED_BY_POLICY_TITLE_FINANCED_DEVICE =
"default_disabled_by_policy_title_financed_device";
private final String mUrl;
FakeDeviceAdminStringProvider(@Nullable String url) {
mUrl = url;
}
@Override
public String getDefaultDisabledByPolicyTitle() {
return DEFAULT_DISABLED_BY_POLICY_TITLE;
}
@Override
public String getDisallowAdjustVolumeTitle() {
return DISALLOW_ADJUST_VOLUME_TITLE;
}
@Override
public String getDisallowOutgoingCallsTitle() {
return DISALLOW_OUTGOING_CALLS_TITLE;
}
@Override
public String getDisallowSmsTitle() {
return DISALLOW_SMS_TITLE;
}
@Override
public String getDisableCameraTitle() {
return DISABLE_CAMERA_TITLE;
}
@Override
public String getDisableScreenCaptureTitle() {
return DISABLE_SCREEN_CAPTURE_TITLE;
}
@Override
public String getSuspendPackagesTitle() {
return SUSPENDED_PACKAGES_TITLE;
}
@Override
public String getDefaultDisabledByPolicyContent() {
return DEFAULT_DISABLED_BY_POLICY_CONTENT;
}
@Override
public String getLearnMoreHelpPageUrl() {
return mUrl;
}
@Override
public String getDisabledByPolicyTitleForFinancedDevice() {
return DEFAULT_DISABLED_BY_POLICY_TITLE_FINANCED_DEVICE;
}
}

View File

@@ -0,0 +1,113 @@
/*
* 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.LEARN_MORE_ACTION_SHOW_ADMIN_POLICIES;
import static com.android.settingslib.enterprise.FakeDeviceAdminStringProvider.DEFAULT_DISABLED_BY_POLICY_TITLE_FINANCED_DEVICE;
import static com.google.common.truth.Truth.assertThat;
import android.app.Activity;
import android.app.Dialog;
import android.content.ComponentName;
import android.content.Context;
import android.os.UserHandle;
import androidx.appcompat.app.AlertDialog;
import androidx.test.core.app.ApplicationProvider;
import com.android.settingslib.R;
import com.android.settingslib.RestrictedLockUtils;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.android.controller.ActivityController;
@RunWith(RobolectricTestRunner.class)
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 Activity mActivity = ActivityController.of(new Activity()).get();
private final ActionDisabledByAdminControllerTestUtils mTestUtils =
new ActionDisabledByAdminControllerTestUtils();
private final ActionDisabledLearnMoreButtonLauncher mLauncher =
mTestUtils.createLearnMoreButtonLauncher();
@Before
public void setUp() {
mActivity.setTheme(R.style.Theme_AppCompat_DayNight);
}
@Test
public void setupLearnMoreButton_negativeButtonSet() {
FinancedDeviceActionDisabledByAdminController mController = createController(mLauncher);
AlertDialog alertDialog = mTestUtils.createAlertDialog(mController, mActivity);
alertDialog.getButton(Dialog.BUTTON_NEUTRAL).performClick();
mTestUtils.assertLearnMoreAction(LEARN_MORE_ACTION_SHOW_ADMIN_POLICIES);
}
@Test
public void getAdminSupportTitleResource_works() {
FinancedDeviceActionDisabledByAdminController mController = createController();
assertThat(mController.getAdminSupportTitle(null))
.isEqualTo(DEFAULT_DISABLED_BY_POLICY_TITLE_FINANCED_DEVICE);
}
@Test
public void getAdminSupportContentString_withSupportMessage_returnsSupportMessage() {
FinancedDeviceActionDisabledByAdminController mController = createController();
assertThat(mController.getAdminSupportContentString(mContext, SUPPORT_MESSAGE))
.isEqualTo(SUPPORT_MESSAGE);
}
@Test
public void getAdminSupportContentString_noSupportMessage_returnsNull() {
FinancedDeviceActionDisabledByAdminController mController = createController();
assertThat(mController.getAdminSupportContentString(mContext, /* supportMessage= */ null))
.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

@@ -0,0 +1,136 @@
/*
* 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.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.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.DISALLOW_ADJUST_VOLUME_TITLE;
import static com.google.common.truth.Truth.assertThat;
import android.app.Activity;
import android.app.Dialog;
import android.content.ComponentName;
import android.os.UserHandle;
import android.os.UserManager;
import androidx.appcompat.app.AlertDialog;
import com.android.settingslib.R;
import com.android.settingslib.RestrictedLockUtils;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.android.controller.ActivityController;
@RunWith(RobolectricTestRunner.class)
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 URL = "https://testexample.com";
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 final Activity mActivity = ActivityController.of(new Activity()).get();
private final ActionDisabledByAdminControllerTestUtils mTestUtils =
new ActionDisabledByAdminControllerTestUtils();
private final ActionDisabledLearnMoreButtonLauncher mLauncher =
mTestUtils.createLearnMoreButtonLauncher();
@Before
public void setUp() {
mActivity.setTheme(R.style.Theme_AppCompat_DayNight);
}
@Test
public void setupLearnMoreButton_validUrl_negativeButtonSet() {
ManagedDeviceActionDisabledByAdminController mController =
createController(mLauncher, URL);
AlertDialog alertDialog = mTestUtils.createAlertDialog(mController, mActivity);
alertDialog.getButton(Dialog.BUTTON_NEUTRAL).performClick();
mTestUtils.assertLearnMoreAction(LEARN_MORE_ACTION_LAUNCH_HELP_PAGE);
}
@Test
public void setupLearnMoreButton_noUrl_negativeButtonSet() {
ManagedDeviceActionDisabledByAdminController mController =
createController(mLauncher, EMPTY_URL);
AlertDialog alertDialog = mTestUtils.createAlertDialog(mController, mActivity);
alertDialog.getButton(Dialog.BUTTON_NEUTRAL).performClick();
mTestUtils.assertLearnMoreAction(LEARN_MORE_ACTION_SHOW_ADMIN_POLICIES);
}
@Test
public void getAdminSupportTitleResource_noRestriction_works() {
ManagedDeviceActionDisabledByAdminController mController = createController();
assertThat(mController.getAdminSupportTitle(null))
.isEqualTo(DEFAULT_DISABLED_BY_POLICY_TITLE);
}
@Test
public void getAdminSupportTitleResource_withRestriction_works() {
ManagedDeviceActionDisabledByAdminController mController = createController();
assertThat(mController.getAdminSupportTitle(RESTRICTION))
.isEqualTo(SUPPORT_TITLE_FOR_RESTRICTION);
}
@Test
public void getAdminSupportContentString_withSupportMessage_returnsSupportMessage() {
ManagedDeviceActionDisabledByAdminController mController = createController();
assertThat(mController.getAdminSupportContentString(mActivity, SUPPORT_MESSAGE))
.isEqualTo(SUPPORT_MESSAGE);
}
@Test
public void getAdminSupportContentString_noSupportMessage_returnsDefault() {
ManagedDeviceActionDisabledByAdminController mController = createController();
assertThat(mController.getAdminSupportContentString(mActivity, /* supportMessage= */ null))
.isEqualTo(DEFAULT_DISABLED_BY_POLICY_CONTENT);
}
private ManagedDeviceActionDisabledByAdminController createController() {
return createController(mLauncher, /* url= */ null);
}
private ManagedDeviceActionDisabledByAdminController createController(
ActionDisabledLearnMoreButtonLauncher buttonHelper, String url) {
ManagedDeviceActionDisabledByAdminController controller =
new ManagedDeviceActionDisabledByAdminController(
buttonHelper,
new FakeDeviceAdminStringProvider(url));
controller.updateEnforcedAdmin(ENFORCED_ADMIN, ENFORCEMENT_ADMIN_USER_ID);
return controller;
}
}