SettingsLib logic for updated ActionDisabledByAdminDialogHelper

Fixes: 184107103
Test: manual
Test: atest FinancedDeviceActionDisabledByAdminControllerTest
Test: atest ManagedDeviceActionDisabledByAdminControllerTest
Change-Id: I894808fbde2bc325e7a0f5697dca760b631f210f
diff --git a/packages/SettingsLib/tests/robotests/Android.bp b/packages/SettingsLib/tests/robotests/Android.bp
index dc661c2..63cfe59 100644
--- a/packages/SettingsLib/tests/robotests/Android.bp
+++ b/packages/SettingsLib/tests/robotests/Android.bp
@@ -43,6 +43,8 @@
     srcs: ["src/**/*.java"],
     static_libs: [
         "SettingsLib-robo-testutils",
+        "androidx.test.core",
+        "androidx.core_core",
     ],
     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
new file mode 100644
index 0000000..e0c9424
--- /dev/null
+++ b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/enterprise/ActionDisabledByAdminControllerTestUtils.java
@@ -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;
+    }
+}
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
new file mode 100644
index 0000000..8c07d75
--- /dev/null
+++ b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/enterprise/FakeDeviceAdminStringProvider.java
@@ -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;
+    }
+}
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
new file mode 100644
index 0000000..2fe2262
--- /dev/null
+++ b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/enterprise/FinancedDeviceActionDisabledByAdminControllerTest.java
@@ -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;
+    }
+}
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
new file mode 100644
index 0000000..eb1dc96
--- /dev/null
+++ b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/enterprise/ManagedDeviceActionDisabledByAdminControllerTest.java
@@ -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;
+    }
+}