Refactor to use the utils class.

Bug: 298076922

Change-Id: I0085fdf7594d0ba9243a7b44600252d8c5b21608
diff --git a/src/com/android/settings/security/ContentProtectionPreferenceController.java b/src/com/android/settings/security/ContentProtectionPreferenceController.java
index 5ff2712..ff472dd 100644
--- a/src/com/android/settings/security/ContentProtectionPreferenceController.java
+++ b/src/com/android/settings/security/ContentProtectionPreferenceController.java
@@ -36,32 +36,8 @@
 
     @Override
     public int getAvailabilityStatus() {
-        if (!settingUiEnabled() || getContentProtectionServiceComponentName() == null) {
-            return UNSUPPORTED_ON_DEVICE;
-        }
-        return AVAILABLE;
-    }
-
-    @VisibleForTesting
-    @Nullable
-    protected String getContentProtectionServiceFlatComponentName() {
-        return mContext.getString(config_defaultContentProtectionService);
-    }
-
-    @Nullable
-    private ComponentName getContentProtectionServiceComponentName() {
-        String flatComponentName = getContentProtectionServiceFlatComponentName();
-        if (flatComponentName == null) {
-            return null;
-        }
-        return ComponentName.unflattenFromString(flatComponentName);
-    }
-
-    @VisibleForTesting
-    protected boolean settingUiEnabled() {
-        return DeviceConfig.getBoolean(
-                DeviceConfig.NAMESPACE_CONTENT_CAPTURE,
-                ContentCaptureManager.DEVICE_CONFIG_PROPERTY_ENABLE_CONTENT_PROTECTION_RECEIVER,
-                ContentCaptureManager.DEFAULT_ENABLE_CONTENT_PROTECTION_RECEIVER);
+        return ContentProtectionPreferenceUtils.isAvailable(mContext)
+                ? AVAILABLE
+                : UNSUPPORTED_ON_DEVICE;
     }
 }
diff --git a/tests/robotests/src/com/android/settings/security/ContentProtectionPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/security/ContentProtectionPreferenceControllerTest.java
index a7990d4..cd2e266 100644
--- a/tests/robotests/src/com/android/settings/security/ContentProtectionPreferenceControllerTest.java
+++ b/tests/robotests/src/com/android/settings/security/ContentProtectionPreferenceControllerTest.java
@@ -16,23 +16,35 @@
 
 package com.android.settings.security;
 
-import static android.view.contentprotection.flags.Flags.FLAG_SETTING_UI_ENABLED;
+import static com.android.internal.R.string.config_defaultContentProtectionService;
 
 import static com.google.common.truth.Truth.assertThat;
 
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.spy;
+
 import android.content.ComponentName;
 import android.content.Context;
 import android.platform.test.flag.junit.SetFlagsRule;
+import android.provider.DeviceConfig;
+import android.view.contentcapture.ContentCaptureManager;
 
-import androidx.test.core.app.ApplicationProvider;
+import com.android.settings.testutils.shadow.ShadowDeviceConfig;
 
+import org.junit.After;
 import org.junit.Before;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.robolectric.RobolectricTestRunner;
+import org.robolectric.RuntimeEnvironment;
+import org.robolectric.annotation.Config;
 
 @RunWith(RobolectricTestRunner.class)
+@Config(
+        shadows = {
+            ShadowDeviceConfig.class,
+        })
 public class ContentProtectionPreferenceControllerTest {
 
     private static final String PACKAGE_NAME = "com.test.package";
@@ -42,84 +54,40 @@
 
     @Rule public final SetFlagsRule mSetFlagsRule = new SetFlagsRule();
 
-    private final Context mContext = ApplicationProvider.getApplicationContext();
+    private Context mContext;
 
     private String mConfigDefaultContentProtectionService = COMPONENT_NAME.flattenToString();
 
     private ContentProtectionPreferenceController mController;
 
-    private boolean mSettingUiEnabled;
-
     @Before
     public void setUp() {
-        mController = new TestContentProtectionPreferenceController();
+        mContext = spy(RuntimeEnvironment.application);
+        mController = new ContentProtectionPreferenceController(mContext, "key");
+    }
+
+    @After
+    public void tearDown() {
+        ShadowDeviceConfig.reset();
     }
 
     @Test
-    public void isAvailable_flagSettingUiDisabled_isFalse() {
-        mSettingUiEnabled = false;
-
+    public void isAvailable_isFalse() {
         assertThat(mController.isAvailable()).isFalse();
     }
 
     @Test
-    public void isAvailable_componentNameNull_isFalse() {
-        mConfigDefaultContentProtectionService = null;
-        mSetFlagsRule.enableFlags(FLAG_SETTING_UI_ENABLED);
-        mController = new TestContentProtectionPreferenceController();
+    public void isAvailable_isTrue() {
+        doReturn(COMPONENT_NAME.flattenToString())
+                .when(mContext)
+                .getString(config_defaultContentProtectionService);
 
-        assertThat(mController.isAvailable()).isFalse();
-    }
-
-    @Test
-    public void isAvailable_componentNameEmpty_isFalse() {
-        mConfigDefaultContentProtectionService = "";
-        mSetFlagsRule.enableFlags(FLAG_SETTING_UI_ENABLED);
-        mController = new TestContentProtectionPreferenceController();
-
-        assertThat(mController.isAvailable()).isFalse();
-    }
-
-    @Test
-    public void isAvailable_componentNameBlank_isFalse() {
-        mConfigDefaultContentProtectionService = "    ";
-        mSetFlagsRule.enableFlags(FLAG_SETTING_UI_ENABLED);
-        mController = new TestContentProtectionPreferenceController();
-
-        assertThat(mController.isAvailable()).isFalse();
-    }
-
-    @Test
-    public void isAvailable_componentNameInvalid_isFalse() {
-        mConfigDefaultContentProtectionService = "invalid";
-        mSetFlagsRule.enableFlags(FLAG_SETTING_UI_ENABLED);
-        mController = new TestContentProtectionPreferenceController();
-
-        assertThat(mController.isAvailable()).isFalse();
-    }
-
-    @Test
-    public void isAvailable_flagSettingUiEnabled_componentNameValid_isTrue() {
-        mSettingUiEnabled = true;
+        DeviceConfig.setProperty(
+                DeviceConfig.NAMESPACE_CONTENT_CAPTURE,
+                ContentCaptureManager.DEVICE_CONFIG_PROPERTY_ENABLE_CONTENT_PROTECTION_RECEIVER,
+                "true",
+                /* makeDefault= */ false);
 
         assertThat(mController.isAvailable()).isTrue();
     }
-
-    private class TestContentProtectionPreferenceController
-            extends ContentProtectionPreferenceController {
-
-        TestContentProtectionPreferenceController() {
-            super(ContentProtectionPreferenceControllerTest.this.mContext, "key");
-        }
-
-        @Override
-        protected String getContentProtectionServiceFlatComponentName() {
-            return mConfigDefaultContentProtectionService;
-        }
-
-        @Override
-        protected boolean settingUiEnabled() {
-            return mSettingUiEnabled;
-        }
-    }
 }