Update ActionDisabledByAdminDialogHelper

- For managed devices, "Learn more" takes the user to a support
page
- Put back the "If you have questions, contact your IT admin"
dialog content text
- Update header to 'Blocked by your IT admin' for managed devices

Fixes: 184107103
Test: manual
Test: atest ActionDisabledByAdminDialogHelperTest
Test: atest ActionDisabledLearnMoreButtonLauncherImplTest
Change-Id: Ie5b8a21b9ba295435d5709f9b0374035141d319a
diff --git a/tests/robotests/Android.bp b/tests/robotests/Android.bp
index 337bad7..9b2e97f 100644
--- a/tests/robotests/Android.bp
+++ b/tests/robotests/Android.bp
@@ -72,10 +72,11 @@
 
     static_libs: [
         "SettingsLib-robo-testutils",
-	"android-support-annotations",
+        "android-support-annotations",
         "androidx.test.core",
         "androidx.test.runner",
         "androidx.test.ext.junit",
+        "androidx.test.espresso.core",
     ],
 
     libs: [
diff --git a/tests/robotests/src/com/android/settings/enterprise/ActionDisabledByAdminDialogHelperTest.java b/tests/robotests/src/com/android/settings/enterprise/ActionDisabledByAdminDialogHelperTest.java
index 5c3dacd..6acc72f 100644
--- a/tests/robotests/src/com/android/settings/enterprise/ActionDisabledByAdminDialogHelperTest.java
+++ b/tests/robotests/src/com/android/settings/enterprise/ActionDisabledByAdminDialogHelperTest.java
@@ -21,7 +21,6 @@
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
-import static org.junit.Assert.assertTrue;
 import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.ArgumentMatchers.anyInt;
 import static org.mockito.Mockito.mock;
@@ -31,7 +30,6 @@
 import android.app.Activity;
 import android.app.admin.DevicePolicyManager;
 import android.content.ComponentName;
-import android.content.Intent;
 import android.content.pm.UserInfo;
 import android.os.Process;
 import android.os.UserHandle;
@@ -43,8 +41,6 @@
 import androidx.appcompat.app.AlertDialog;
 
 import com.android.settings.R;
-import com.android.settings.Settings;
-import com.android.settings.applications.specialaccess.deviceadmin.DeviceAdminAdd;
 import com.android.settings.testutils.CustomActivity;
 import com.android.settings.testutils.shadow.ShadowActivity;
 import com.android.settings.testutils.shadow.ShadowDevicePolicyManager;
@@ -69,6 +65,11 @@
         ShadowActivity.class
 })
 public class ActionDisabledByAdminDialogHelperTest {
+    private static final ComponentName ADMIN_COMPONENT =
+            new ComponentName("some.package.name", "some.package.name.SomeClass");
+    private static final int USER_ID = 123;
+    private static final EnforcedAdmin ENFORCED_ADMIN =
+            new EnforcedAdmin(ADMIN_COMPONENT, UserHandle.of(USER_ID));
     private ActionDisabledByAdminDialogHelper mHelper;
     private Activity mActivity;
     private org.robolectric.shadows.ShadowActivity mActivityShadow;
@@ -81,32 +82,6 @@
     }
 
     @Test
-    public void testShowAdminPoliciesWithComponent() {
-        final int userId = 123;
-        final ComponentName component = new ComponentName("some.package.name",
-                "some.package.name.SomeClass");
-        final EnforcedAdmin admin = new EnforcedAdmin(component, UserHandle.of(userId));
-
-        mHelper.showAdminPolicies(admin, mActivity);
-
-        final Intent intent = mActivityShadow.getNextStartedActivity();
-        assertTrue(
-                intent.getBooleanExtra(DeviceAdminAdd.EXTRA_CALLED_FROM_SUPPORT_DIALOG, false));
-        assertEquals(component,
-                intent.getParcelableExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN));
-    }
-
-    @Test
-    public void testShowAdminPoliciesWithoutComponent() {
-        final int userId = 123;
-        final EnforcedAdmin admin = new EnforcedAdmin(null, UserHandle.of(userId));
-        mHelper.showAdminPolicies(admin, mActivity);
-        final Intent intent = mActivityShadow.getNextStartedActivity();
-        assertEquals(intent.getComponent(), new ComponentName(mActivity,
-                Settings.DeviceAdminSettingsActivity.class.getName()));
-    }
-
-    @Test
     public void testSetAdminSupportTitle() {
         final ViewGroup view = new FrameLayout(mActivity);
         final TextView textView = createAdminSupportDialogTitleTextView(view, mActivity);
@@ -150,6 +125,7 @@
         final ViewGroup view = new FrameLayout(mActivity);
         final TextView textView = createAdminSupportDialogTitleTextView(view, mActivity);
         setupFinancedDevice(dpmShadow);
+        mHelper = new ActionDisabledByAdminDialogHelper(mActivity);
 
         mHelper.setAdminSupportTitle(view, null);
 
@@ -196,7 +172,8 @@
         mHelper.setAdminSupportDetails(mActivity, view, admin);
 
         assertNotNull(admin.component);
-        assertEquals("", Shadows.shadowOf(textView).innerText());
+        assertEquals(mActivity.getString(R.string.default_admin_support_msg),
+                Shadows.shadowOf(textView).innerText());
     }
 
     @Test
@@ -217,7 +194,8 @@
         mHelper.setAdminSupportDetails(mActivity, textView, admin);
 
         assertNull(admin.component);
-        assertEquals("", Shadows.shadowOf(textView).innerText());
+        assertEquals(mActivity.getString(R.string.default_admin_support_msg),
+                Shadows.shadowOf(textView).innerText());
     }
 
     @Test
@@ -225,12 +203,11 @@
         final UserManager userManager = RuntimeEnvironment.application.getSystemService(
                 UserManager.class);
         final ShadowUserManager userManagerShadow = Shadow.extract(userManager);
-        final ComponentName component = new ComponentName("some.package.name",
-                "some.package.name.SomeClass");
-        mHelper.mEnforcedAdmin = new EnforcedAdmin(component, UserHandle.of(123));
+        mHelper.prepareDialogBuilder(
+                /* restriction= */ null, ENFORCED_ADMIN);
 
         // Set up for shadow call.
-        userManagerShadow.getSameProfileGroupIds().put(123, 0);
+        userManagerShadow.getSameProfileGroupIds().put(USER_ID, 0);
 
         // Test that the button is shown when user IDs are in the same profile group
         AlertDialog.Builder builder = mock(AlertDialog.Builder.class);
diff --git a/tests/unit/src/com/android/settings/enterprise/ActionDisabledLearnMoreButtonLauncherImplTest.java b/tests/unit/src/com/android/settings/enterprise/ActionDisabledLearnMoreButtonLauncherImplTest.java
new file mode 100644
index 0000000..68468ed
--- /dev/null
+++ b/tests/unit/src/com/android/settings/enterprise/ActionDisabledLearnMoreButtonLauncherImplTest.java
@@ -0,0 +1,124 @@
+/*
+ * 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.settings.enterprise;
+
+import static android.app.admin.DevicePolicyManager.EXTRA_DEVICE_ADMIN;
+
+import static com.android.settings.applications.specialaccess.deviceadmin.DeviceAdminAdd.EXTRA_CALLED_FROM_SUPPORT_DIALOG;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.verify;
+
+import android.app.Activity;
+import android.content.ComponentName;
+import android.content.Intent;
+import android.net.Uri;
+import android.os.UserHandle;
+
+import androidx.test.runner.AndroidJUnit4;
+
+import com.android.settings.Settings;
+import com.android.settings.applications.specialaccess.deviceadmin.DeviceAdminAdd;
+import com.android.settingslib.RestrictedLockUtils;
+import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.ArgumentCaptor;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+
+
+@RunWith(AndroidJUnit4.class)
+public class ActionDisabledLearnMoreButtonLauncherImplTest {
+
+    private static final int ENFORCED_ADMIN_USER_ID = 123;
+    private static final ComponentName ADMIN_COMPONENT =
+            new ComponentName("some.package.name", "some.package.name.SomeClass");
+    private static final String URL = "https://testexample.com";
+    private static final Uri URI = Uri.parse(URL);
+
+    @Mock
+    private Activity mActivity;
+
+    @Before
+    public void setUp() {
+        MockitoAnnotations.initMocks(this);
+    }
+
+    @Test
+    public void showAdminPolicies_noComponent_works() {
+        final EnforcedAdmin enforcedAdmin = createEnforcedAdmin(/* component= */ null);
+
+        ActionDisabledLearnMoreButtonLauncherImpl.SHOW_ADMIN_POLICIES
+                .accept(mActivity, enforcedAdmin);
+
+        final ArgumentCaptor<Intent> captor = ArgumentCaptor.forClass(Intent.class);
+        verify(mActivity).startActivity(captor.capture());
+        assertThat(captor.getValue().getComponent().getClassName())
+                .isEqualTo(Settings.DeviceAdminSettingsActivity.class.getName());
+    }
+
+    @Test
+    public void showAdminPolicies_withComponent_works() {
+        final EnforcedAdmin enforcedAdmin = createEnforcedAdmin(ADMIN_COMPONENT);
+
+        ActionDisabledLearnMoreButtonLauncherImpl.SHOW_ADMIN_POLICIES
+                .accept(mActivity, enforcedAdmin);
+
+        final ArgumentCaptor<Intent> captor = ArgumentCaptor.forClass(Intent.class);
+        verify(mActivity).startActivityAsUser(
+                captor.capture(),
+                eq(UserHandle.of(ENFORCED_ADMIN_USER_ID)));
+        assertDeviceAdminAddIntent(captor.getValue());
+    }
+
+    @Test
+    public void launchHelpPage_works() {
+        ActionDisabledLearnMoreButtonLauncherImpl.LAUNCH_HELP_PAGE.accept(mActivity, URL);
+
+        final ArgumentCaptor<Intent> captor = ArgumentCaptor.forClass(Intent.class);
+        verify(mActivity).startActivityAsUser(captor.capture(), eq(UserHandle.SYSTEM));
+        assertActionViewIntent(captor.getValue());
+    }
+
+    private EnforcedAdmin createEnforcedAdmin(ComponentName component) {
+        return new RestrictedLockUtils.EnforcedAdmin(
+                component, UserHandle.of(ENFORCED_ADMIN_USER_ID));
+    }
+
+    private void assertDeviceAdminAddIntent(Intent intent) {
+        assertThat(intent.getComponent().getClassName())
+                .isEqualTo(DeviceAdminAdd.class.getName());
+        assertThat((ComponentName) intent.getParcelableExtra(EXTRA_DEVICE_ADMIN))
+                .isEqualTo(ADMIN_COMPONENT);
+        assertThat(intent.getBooleanExtra(
+                EXTRA_CALLED_FROM_SUPPORT_DIALOG,
+                /* defaultValue= */ false))
+                .isTrue();
+    }
+
+    private void assertActionViewIntent(Intent intent) {
+        assertThat(intent.getAction())
+                .isEqualTo(Intent.ACTION_VIEW);
+        assertThat(intent.getData())
+                .isEqualTo(URI);
+    }
+}