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

Commit c7628879 authored by Nino Jagar's avatar Nino Jagar
Browse files

Disable content protection settings if no service

Bug: 308613762
Test: Unit tests and manual end-to-end
Change-Id: I0cc1ff1a260a7e14537036497a155135b7e2f0a9
parent af9f60f6
Loading
Loading
Loading
Loading
+24 −2
Original line number Diff line number Diff line
@@ -17,9 +17,14 @@ package com.android.settings.security;

import static android.view.contentprotection.flags.Flags.settingUiEnabled;

import static com.android.internal.R.string.config_defaultContentProtectionService;

import android.content.ComponentName;
import android.content.Context;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;

import com.android.settings.core.BasePreferenceController;

@@ -31,7 +36,24 @@ public class ContentProtectionPreferenceController extends BasePreferenceControl

    @Override
    public int getAvailabilityStatus() {
      // TODO(b/306565942): Add a resource value check.
      return settingUiEnabled() ? AVAILABLE : UNSUPPORTED_ON_DEVICE;
        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);
    }
}
+65 −17
Original line number Diff line number Diff line
@@ -17,54 +17,102 @@
package com.android.settings.security;

import static android.view.contentprotection.flags.Flags.FLAG_SETTING_UI_ENABLED;

import static com.google.common.truth.Truth.assertThat;

import android.content.ContentResolver;
import android.content.ComponentName;
import android.content.Context;
import android.platform.test.flag.junit.SetFlagsRule;
import android.provider.Settings;

import androidx.preference.Preference;

import com.android.settings.R;
import androidx.test.core.app.ApplicationProvider;

import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;

@RunWith(RobolectricTestRunner.class)
public class ContentProtectionPreferenceControllerTest {

    private static final String PACKAGE_NAME = "com.test.package";

    private static final ComponentName COMPONENT_NAME =
            new ComponentName(PACKAGE_NAME, "TestClass");

    @Rule public final SetFlagsRule mSetFlagsRule = new SetFlagsRule();

    private Context mContext;
    private final Context mContext = ApplicationProvider.getApplicationContext();

    private String mConfigDefaultContentProtectionService = COMPONENT_NAME.flattenToString();

    private ContentProtectionPreferenceController mController;
    private Preference mPreference;

    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);
        mContext = RuntimeEnvironment.application;
        mController = new ContentProtectionPreferenceController(mContext, "key");
        mPreference = new Preference(mContext);
        mPreference.setKey(mController.getPreferenceKey());
        mController = new TestContentProtectionPreferenceController();
    }

    @Test
    public void isAvailable_flagSettingUiDisabled_isFalse() {
        mSetFlagsRule.disableFlags(FLAG_SETTING_UI_ENABLED);

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

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

    @Test
    public void isAvailable_flagSettingUiEnabled_isTrue() {
    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() {
        mSetFlagsRule.enableFlags(FLAG_SETTING_UI_ENABLED);

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

    private class TestContentProtectionPreferenceController
            extends ContentProtectionPreferenceController {

        TestContentProtectionPreferenceController() {
            super(ContentProtectionPreferenceControllerTest.this.mContext, "key");
        }

        @Override
        protected String getContentProtectionServiceFlatComponentName() {
            return mConfigDefaultContentProtectionService;
        }
    }
}