Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit b9835cea authored by Wa Gao's avatar Wa Gao
Browse files

Refactor to use the utils class.

Bug: 298076922

Change-Id: I0085fdf7594d0ba9243a7b44600252d8c5b21608
parent bd073bc1
Loading
Loading
Loading
Loading
+3 −27
Original line number Diff line number Diff line
@@ -36,32 +36,8 @@ public class ContentProtectionPreferenceController extends BasePreferenceControl

    @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;
    }
}
+30 −62
Original line number Diff line number Diff line
@@ -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 @@ public class ContentProtectionPreferenceControllerTest {

    @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();
    }

    @Test
    public void isAvailable_flagSettingUiDisabled_isFalse() {
        mSettingUiEnabled = false;

        assertThat(mController.isAvailable()).isFalse();
        mContext = spy(RuntimeEnvironment.application);
        mController = new ContentProtectionPreferenceController(mContext, "key");
    }

    @Test
    public void isAvailable_componentNameNull_isFalse() {
        mConfigDefaultContentProtectionService = null;
        mSetFlagsRule.enableFlags(FLAG_SETTING_UI_ENABLED);
        mController = new TestContentProtectionPreferenceController();

        assertThat(mController.isAvailable()).isFalse();
    }

    @Test
    public void isAvailable_componentNameEmpty_isFalse() {
        mConfigDefaultContentProtectionService = "";
        mSetFlagsRule.enableFlags(FLAG_SETTING_UI_ENABLED);
        mController = new TestContentProtectionPreferenceController();

        assertThat(mController.isAvailable()).isFalse();
    @After
    public void tearDown() {
        ShadowDeviceConfig.reset();
    }

    @Test
    public void isAvailable_componentNameBlank_isFalse() {
        mConfigDefaultContentProtectionService = "    ";
        mSetFlagsRule.enableFlags(FLAG_SETTING_UI_ENABLED);
        mController = new TestContentProtectionPreferenceController();

    public void isAvailable_isFalse() {
        assertThat(mController.isAvailable()).isFalse();
    }

    @Test
    public void isAvailable_componentNameInvalid_isFalse() {
        mConfigDefaultContentProtectionService = "invalid";
        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_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;
        }
    }
}