Further refactoring on ActionDisabledByAdminDialog classes.
Bug: 184107103
Bug: 188836559
Test: m -j RunSettingsLibRoboTests ROBOTEST_FILTER=FinancedDeviceActionDisabledByAdminControllerTest
Test: m -j RunSettingsLibRoboTests ROBOTEST_FILTER=ManagedDeviceActionDisabledByAdminControllerTest
Test: m -j RunSettingsLibRoboTests ROBOTEST_FILTER=ActionDisabledLearnMoreButtonLauncherTest
Change-Id: I3c4056d82834b5e2a4afbd958be7c9d0b9af543e
diff --git a/packages/SettingsLib/src/com/android/settingslib/enterprise/ActionDisabledByAdminController.java b/packages/SettingsLib/src/com/android/settingslib/enterprise/ActionDisabledByAdminController.java
index 6d7f8df..a537394 100644
--- a/packages/SettingsLib/src/com/android/settingslib/enterprise/ActionDisabledByAdminController.java
+++ b/packages/SettingsLib/src/com/android/settingslib/enterprise/ActionDisabledByAdminController.java
@@ -16,6 +16,7 @@
package com.android.settingslib.enterprise;
+import android.annotation.UserIdInt;
import android.content.Context;
import androidx.annotation.Nullable;
@@ -28,10 +29,15 @@
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
* 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.
@@ -41,11 +47,11 @@
/**
* Returns the admin support dialog's content string.
*/
- CharSequence getAdminSupportContentString(
- Context context, @Nullable CharSequence supportMessage);
+ CharSequence getAdminSupportContentString(Context context,
+ @Nullable CharSequence supportMessage);
/**
* Updates the enforced admin
*/
- void updateEnforcedAdmin(RestrictedLockUtils.EnforcedAdmin admin, int adminUserId);
+ void updateEnforcedAdmin(RestrictedLockUtils.EnforcedAdmin admin, @UserIdInt int adminUserId);
}
diff --git a/packages/SettingsLib/src/com/android/settingslib/enterprise/ActionDisabledByAdminControllerFactory.java b/packages/SettingsLib/src/com/android/settingslib/enterprise/ActionDisabledByAdminControllerFactory.java
index 7eecd19..da42e33 100644
--- a/packages/SettingsLib/src/com/android/settingslib/enterprise/ActionDisabledByAdminControllerFactory.java
+++ b/packages/SettingsLib/src/com/android/settingslib/enterprise/ActionDisabledByAdminControllerFactory.java
@@ -19,29 +19,30 @@
import static android.app.admin.DevicePolicyManager.DEVICE_OWNER_TYPE_FINANCED;
import android.app.admin.DevicePolicyManager;
+import android.content.Context;
/**
* A factory that returns the relevant instance of {@link ActionDisabledByAdminController}.
*/
-public class ActionDisabledByAdminControllerFactory {
+public final 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);
+ public static ActionDisabledByAdminController createInstance(Context context,
+ DeviceAdminStringProvider stringProvider) {
+ return isFinancedDevice(context)
+ ? new FinancedDeviceActionDisabledByAdminController(stringProvider)
+ : new ManagedDeviceActionDisabledByAdminController(stringProvider);
}
- private static boolean isFinancedDevice(DevicePolicyManager dpm) {
+ private static boolean isFinancedDevice(Context context) {
+ DevicePolicyManager dpm = context.getSystemService(DevicePolicyManager.class);
return dpm.isDeviceManaged() && dpm.getDeviceOwnerType(
dpm.getDeviceOwnerComponentOnAnyUser()) == DEVICE_OWNER_TYPE_FINANCED;
}
+
+ private ActionDisabledByAdminControllerFactory() {
+ throw new UnsupportedOperationException("provides only static methods");
+ }
}
diff --git a/packages/SettingsLib/src/com/android/settingslib/enterprise/ActionDisabledLearnMoreButtonLauncher.java b/packages/SettingsLib/src/com/android/settingslib/enterprise/ActionDisabledLearnMoreButtonLauncher.java
index 65b91f1..78a42be 100644
--- a/packages/SettingsLib/src/com/android/settingslib/enterprise/ActionDisabledLearnMoreButtonLauncher.java
+++ b/packages/SettingsLib/src/com/android/settingslib/enterprise/ActionDisabledLearnMoreButtonLauncher.java
@@ -16,29 +16,100 @@
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
*/
- void setupLearnMoreButtonToShowAdminPolicies(
- Context context,
- Object alertDialogBuilder,
- int enforcementAdminUserId,
- RestrictedLockUtils.EnforcedAdmin enforcedAdmin);
+ public final void setupLearnMoreButtonToShowAdminPolicies(Context context,
+ int enforcementAdminUserId, EnforcedAdmin enforcedAdmin) {
+ requireNonNull(context, "context cannot be null");
+ requireNonNull(enforcedAdmin, "enforcedAdmin cannot be null");
+
+ // 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
*/
- void setupLearnMoreButtonToLaunchHelpPage(
- Context context,
- Object alertDialogBuilder,
- String url);
+ public final void setupLearnMoreButtonToLaunchHelpPage(Context context, String url) {
+ requireNonNull(context, "context cannot be null");
+ requireNonNull(url, "url cannot be null");
+
+ 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);
+ }
}
diff --git a/packages/SettingsLib/src/com/android/settingslib/enterprise/BaseActionDisabledByAdminController.java b/packages/SettingsLib/src/com/android/settingslib/enterprise/BaseActionDisabledByAdminController.java
new file mode 100644
index 0000000..dd71557
--- /dev/null
+++ b/packages/SettingsLib/src/com/android/settingslib/enterprise/BaseActionDisabledByAdminController.java
@@ -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");
+ }
+}
diff --git a/packages/SettingsLib/src/com/android/settingslib/enterprise/FinancedDeviceActionDisabledByAdminController.java b/packages/SettingsLib/src/com/android/settingslib/enterprise/FinancedDeviceActionDisabledByAdminController.java
index cd816e88..2ed0dc4 100644
--- a/packages/SettingsLib/src/com/android/settingslib/enterprise/FinancedDeviceActionDisabledByAdminController.java
+++ b/packages/SettingsLib/src/com/android/settingslib/enterprise/FinancedDeviceActionDisabledByAdminController.java
@@ -16,52 +16,31 @@
package com.android.settingslib.enterprise;
-import static java.util.Objects.requireNonNull;
-
-import android.annotation.UserIdInt;
import android.content.Context;
import androidx.annotation.Nullable;
-import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
-
/**
* An {@link ActionDisabledByAdminController} to be used with financed devices.
*/
-public class FinancedDeviceActionDisabledByAdminController
- implements ActionDisabledByAdminController {
+final class FinancedDeviceActionDisabledByAdminController
+ extends BaseActionDisabledByAdminController {
- private @UserIdInt int mEnforcementAdminUserId;
- private EnforcedAdmin mEnforcedAdmin;
- 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");
+ FinancedDeviceActionDisabledByAdminController(DeviceAdminStringProvider stringProvider) {
+ super(stringProvider);
}
@Override
- public void updateEnforcedAdmin(EnforcedAdmin admin, int adminUserId) {
- mEnforcementAdminUserId = adminUserId;
- mEnforcedAdmin = requireNonNull(admin, "admin cannot be null");
- }
+ public void setupLearnMoreButton(Context context) {
+ assertInitialized();
- @Override
- public void setupLearnMoreButton(Context context, Object alertDialogBuilder) {
- mHelper.setupLearnMoreButtonToShowAdminPolicies(
- context,
- alertDialogBuilder,
- mEnforcementAdminUserId,
+ mLauncher.setupLearnMoreButtonToShowAdminPolicies(context, mEnforcementAdminUserId,
mEnforcedAdmin);
}
@Override
public String getAdminSupportTitle(@Nullable String restriction) {
- return mDeviceAdminStringProvider.getDisabledByPolicyTitleForFinancedDevice();
+ return mStringProvider.getDisabledByPolicyTitleForFinancedDevice();
}
@Override
diff --git a/packages/SettingsLib/src/com/android/settingslib/enterprise/ManagedDeviceActionDisabledByAdminController.java b/packages/SettingsLib/src/com/android/settingslib/enterprise/ManagedDeviceActionDisabledByAdminController.java
index 70e19f9..df6bab7 100644
--- a/packages/SettingsLib/src/com/android/settingslib/enterprise/ManagedDeviceActionDisabledByAdminController.java
+++ b/packages/SettingsLib/src/com/android/settingslib/enterprise/ManagedDeviceActionDisabledByAdminController.java
@@ -16,55 +16,39 @@
package com.android.settingslib.enterprise;
-import static java.util.Objects.requireNonNull;
-
-import android.annotation.UserIdInt;
import android.app.admin.DevicePolicyManager;
import android.content.Context;
import android.os.UserManager;
import android.text.TextUtils;
-import com.android.settingslib.RestrictedLockUtils;
+import androidx.annotation.Nullable;
+
/**
* 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;
+final class ManagedDeviceActionDisabledByAdminController
+ extends BaseActionDisabledByAdminController {
- ManagedDeviceActionDisabledByAdminController(
- ActionDisabledLearnMoreButtonLauncher helper,
- DeviceAdminStringProvider stringProvider) {
- mHelper = requireNonNull(helper);
- mStringProvider = requireNonNull(stringProvider);
+ ManagedDeviceActionDisabledByAdminController(DeviceAdminStringProvider stringProvider) {
+ super(stringProvider);
}
@Override
- public void updateEnforcedAdmin(RestrictedLockUtils.EnforcedAdmin admin, int adminUserId) {
- mEnforcementAdminUserId = adminUserId;
- mEnforcedAdmin = requireNonNull(admin, "admin cannot be null");
- }
+ public void setupLearnMoreButton(Context context) {
+ assertInitialized();
- @Override
- public void setupLearnMoreButton(Context context, Object alertDialogBuilder) {
String url = mStringProvider.getLearnMoreHelpPageUrl();
if (TextUtils.isEmpty(url)) {
- mHelper.setupLearnMoreButtonToShowAdminPolicies(
- context,
- alertDialogBuilder,
- mEnforcementAdminUserId,
+ mLauncher.setupLearnMoreButtonToShowAdminPolicies(context, mEnforcementAdminUserId,
mEnforcedAdmin);
} else {
- mHelper.setupLearnMoreButtonToLaunchHelpPage(context, alertDialogBuilder, url);
+ mLauncher.setupLearnMoreButtonToLaunchHelpPage(context, url);
}
}
@Override
- public String getAdminSupportTitle(String restriction) {
+ public String getAdminSupportTitle(@Nullable String restriction) {
if (restriction == null) {
return mStringProvider.getDefaultDisabledByPolicyTitle();
}
@@ -88,9 +72,8 @@
@Override
public CharSequence getAdminSupportContentString(Context context, CharSequence supportMessage) {
- if (supportMessage != null) {
- return supportMessage;
- }
- return mStringProvider.getDefaultDisabledByPolicyContent();
+ return supportMessage != null
+ ? supportMessage
+ : mStringProvider.getDefaultDisabledByPolicyContent();
}
}
diff --git a/packages/SettingsLib/tests/robotests/Android.bp b/packages/SettingsLib/tests/robotests/Android.bp
index 63cfe59..2d1a516 100644
--- a/packages/SettingsLib/tests/robotests/Android.bp
+++ b/packages/SettingsLib/tests/robotests/Android.bp
@@ -45,6 +45,7 @@
"SettingsLib-robo-testutils",
"androidx.test.core",
"androidx.core_core",
+ "testng", // TODO: remove once JUnit on Android provides assertThrows
],
java_resource_dirs: ["config"],
instrumentation_for: "SettingsLibShell",
diff --git a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/enterprise/ActionDisabledByAdminControllerTestUtils.java b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/enterprise/ActionDisabledByAdminControllerTestUtils.java
index 4b51790..e57335f 100644
--- a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/enterprise/ActionDisabledByAdminControllerTestUtils.java
+++ b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/enterprise/ActionDisabledByAdminControllerTestUtils.java
@@ -16,51 +16,81 @@
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.os.UserHandle;
+import android.util.DebugUtils;
-import androidx.appcompat.app.AlertDialog;
-
-import com.android.settingslib.RestrictedLockUtils;
+import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
/**
* 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;
+// NOTE: must be public because of DebugUtils.constantToString() call
+public final class ActionDisabledByAdminControllerTestUtils {
+
+ 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;
ActionDisabledLearnMoreButtonLauncher createLearnMoreButtonLauncher() {
return new ActionDisabledLearnMoreButtonLauncher() {
+
@Override
- public void setupLearnMoreButtonToShowAdminPolicies(Context context,
- Object alertDialogBuilder, int enforcementAdminUserId,
- RestrictedLockUtils.EnforcedAdmin enforcedAdmin) {
+ public void setLearnMoreButton(Runnable action) {
+ action.run();
+ }
+
+ @Override
+ protected void launchShowAdminPolicies(Context context, UserHandle user,
+ ComponentName admin) {
mLearnMoreButtonAction = LEARN_MORE_ACTION_SHOW_ADMIN_POLICIES;
}
@Override
- public void setupLearnMoreButtonToLaunchHelpPage(Context context,
- Object alertDialogBuilder, String url) {
+ protected void launchShowAdminSettings(Context context) {
+ mLearnMoreButtonAction = LEARN_MORE_ACTION_SHOW_ADMIN_SETTINGS;
+ }
+
+ @Override
+ public void showHelpPage(Context context, String url) {
mLearnMoreButtonAction = LEARN_MORE_ACTION_LAUNCH_HELP_PAGE;
}
+
+ @Override
+ protected boolean isSameProfileGroup(Context context, int enforcementAdminUserId) {
+ return true;
+ }
};
}
void assertLearnMoreAction(int learnMoreActionShowAdminPolicies) {
- assertThat(learnMoreActionShowAdminPolicies).isEqualTo(mLearnMoreButtonAction);
+ assertWithMessage("action").that(actionToString(mLearnMoreButtonAction))
+ .isEqualTo(actionToString(learnMoreActionShowAdminPolicies));
}
- 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;
+ private static String actionToString(int action) {
+ return DebugUtils.constantToString(ActionDisabledByAdminControllerTestUtils.class,
+ "LEARN_MORE_ACTION_", action);
}
}
diff --git a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/enterprise/ActionDisabledLearnMoreButtonLauncherTest.java b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/enterprise/ActionDisabledLearnMoreButtonLauncherTest.java
new file mode 100644
index 0000000..7014da0
--- /dev/null
+++ b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/enterprise/ActionDisabledLearnMoreButtonLauncherTest.java
@@ -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();
+ }
+}
diff --git a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/enterprise/FakeDeviceAdminStringProvider.java b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/enterprise/FakeDeviceAdminStringProvider.java
index 8c07d75..be3e9fc 100644
--- a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/enterprise/FakeDeviceAdminStringProvider.java
+++ b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/enterprise/FakeDeviceAdminStringProvider.java
@@ -30,6 +30,8 @@
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";
+ static final DeviceAdminStringProvider DEFAULT_DEVICE_ADMIN_STRING_PROVIDER =
+ new FakeDeviceAdminStringProvider(/* url = */ null);
private final String mUrl;
diff --git a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/enterprise/FinancedDeviceActionDisabledByAdminControllerTest.java b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/enterprise/FinancedDeviceActionDisabledByAdminControllerTest.java
index 2fe2262..7b08fee 100644
--- a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/enterprise/FinancedDeviceActionDisabledByAdminControllerTest.java
+++ b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/enterprise/FinancedDeviceActionDisabledByAdminControllerTest.java
@@ -16,22 +16,21 @@
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.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.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;
@@ -41,73 +40,46 @@
@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();
+ private final FinancedDeviceActionDisabledByAdminController mController =
+ new FinancedDeviceActionDisabledByAdminController(
+ DEFAULT_DEVICE_ADMIN_STRING_PROVIDER);
@Before
public void setUp() {
mActivity.setTheme(R.style.Theme_AppCompat_DayNight);
+
+ mController.initialize(mTestUtils.createLearnMoreButtonLauncher());
+ mController.updateEnforcedAdmin(ENFORCED_ADMIN, ENFORCEMENT_ADMIN_USER_ID);
+
}
@Test
public void setupLearnMoreButton_negativeButtonSet() {
- FinancedDeviceActionDisabledByAdminController mController = createController(mLauncher);
- AlertDialog alertDialog = mTestUtils.createAlertDialog(mController, mActivity);
-
- alertDialog.getButton(Dialog.BUTTON_NEUTRAL).performClick();
+ mController.setupLearnMoreButton(mContext);
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;
- }
}
diff --git a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/enterprise/ManagedDeviceActionDisabledByAdminControllerTest.java b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/enterprise/ManagedDeviceActionDisabledByAdminControllerTest.java
index eb1dc96..19f6aa1 100644
--- a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/enterprise/ManagedDeviceActionDisabledByAdminControllerTest.java
+++ b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/enterprise/ManagedDeviceActionDisabledByAdminControllerTest.java
@@ -16,8 +16,12 @@
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_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_TITLE;
import static com.android.settingslib.enterprise.FakeDeviceAdminStringProvider.DISALLOW_ADJUST_VOLUME_TITLE;
@@ -25,15 +29,12 @@
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.content.Context;
import android.os.UserManager;
-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;
@@ -43,23 +44,15 @@
@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 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() {
@@ -68,68 +61,63 @@
@Test
public void setupLearnMoreButton_validUrl_negativeButtonSet() {
- ManagedDeviceActionDisabledByAdminController mController =
- createController(mLauncher, URL);
- AlertDialog alertDialog = mTestUtils.createAlertDialog(mController, mActivity);
+ ManagedDeviceActionDisabledByAdminController controller = createController(URL);
- alertDialog.getButton(Dialog.BUTTON_NEUTRAL).performClick();
+ controller.setupLearnMoreButton(mContext);
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);
+ ManagedDeviceActionDisabledByAdminController controller = createController(EMPTY_URL);
- alertDialog.getButton(Dialog.BUTTON_NEUTRAL).performClick();
+ controller.setupLearnMoreButton(mContext);
mTestUtils.assertLearnMoreAction(LEARN_MORE_ACTION_SHOW_ADMIN_POLICIES);
}
@Test
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);
}
@Test
public void getAdminSupportTitleResource_withRestriction_works() {
- ManagedDeviceActionDisabledByAdminController mController = createController();
+ ManagedDeviceActionDisabledByAdminController controller = createController();
- assertThat(mController.getAdminSupportTitle(RESTRICTION))
+ assertThat(controller.getAdminSupportTitle(RESTRICTION))
.isEqualTo(SUPPORT_TITLE_FOR_RESTRICTION);
}
@Test
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);
}
@Test
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);
}
private ManagedDeviceActionDisabledByAdminController createController() {
- return createController(mLauncher, /* url= */ null);
+ return createController(/* url= */ null);
}
- private ManagedDeviceActionDisabledByAdminController createController(
- ActionDisabledLearnMoreButtonLauncher buttonHelper, String url) {
+ private ManagedDeviceActionDisabledByAdminController createController(String url) {
ManagedDeviceActionDisabledByAdminController controller =
new ManagedDeviceActionDisabledByAdminController(
- buttonHelper,
new FakeDeviceAdminStringProvider(url));
+ controller.initialize(mTestUtils.createLearnMoreButtonLauncher());
controller.updateEnforcedAdmin(ENFORCED_ADMIN, ENFORCEMENT_ADMIN_USER_ID);
return controller;
}