Disable content protection settings if no service

Bug: 308613762
Test: Unit tests and manual end-to-end
Change-Id: I0cc1ff1a260a7e14537036497a155135b7e2f0a9
This commit is contained in:
Nino Jagar
2023-10-31 20:34:04 +00:00
parent af9f60f6c2
commit c7628879da
2 changed files with 89 additions and 19 deletions

View File

@@ -17,9 +17,14 @@ package com.android.settings.security;
import static android.view.contentprotection.flags.Flags.settingUiEnabled; 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 android.content.Context;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import com.android.settings.core.BasePreferenceController; import com.android.settings.core.BasePreferenceController;
@@ -31,7 +36,24 @@ public class ContentProtectionPreferenceController extends BasePreferenceControl
@Override @Override
public int getAvailabilityStatus() { public int getAvailabilityStatus() {
// TODO(b/306565942): Add a resource value check. if (!settingUiEnabled() || getContentProtectionServiceComponentName() == null) {
return settingUiEnabled() ? AVAILABLE : UNSUPPORTED_ON_DEVICE; 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);
} }
} }

View File

@@ -17,54 +17,102 @@
package com.android.settings.security; package com.android.settings.security;
import static android.view.contentprotection.flags.Flags.FLAG_SETTING_UI_ENABLED; import static android.view.contentprotection.flags.Flags.FLAG_SETTING_UI_ENABLED;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import android.content.ContentResolver; import android.content.ComponentName;
import android.content.Context; import android.content.Context;
import android.platform.test.flag.junit.SetFlagsRule; import android.platform.test.flag.junit.SetFlagsRule;
import android.provider.Settings;
import androidx.preference.Preference; import androidx.test.core.app.ApplicationProvider;
import com.android.settings.R;
import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner; import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
@RunWith(RobolectricTestRunner.class) @RunWith(RobolectricTestRunner.class)
public class ContentProtectionPreferenceControllerTest { 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(); @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 ContentProtectionPreferenceController mController;
private Preference mPreference;
@Before @Before
public void setUp() { public void setUp() {
MockitoAnnotations.initMocks(this); mController = new TestContentProtectionPreferenceController();
mContext = RuntimeEnvironment.application;
mController = new ContentProtectionPreferenceController(mContext, "key");
mPreference = new Preference(mContext);
mPreference.setKey(mController.getPreferenceKey());
} }
@Test @Test
public void isAvailable_flagSettingUiDisabled_isFalse() { public void isAvailable_flagSettingUiDisabled_isFalse() {
mSetFlagsRule.disableFlags(FLAG_SETTING_UI_ENABLED); mSetFlagsRule.disableFlags(FLAG_SETTING_UI_ENABLED);
assertThat(mController.isAvailable()).isFalse(); assertThat(mController.isAvailable()).isFalse();
} }
@Test @Test
public void isAvailable_flagSettingUiEnabled_isTrue() { public void isAvailable_componentNameNull_isFalse() {
mConfigDefaultContentProtectionService = null;
mSetFlagsRule.enableFlags(FLAG_SETTING_UI_ENABLED); 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_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(); assertThat(mController.isAvailable()).isTrue();
} }
private class TestContentProtectionPreferenceController
extends ContentProtectionPreferenceController {
TestContentProtectionPreferenceController() {
super(ContentProtectionPreferenceControllerTest.this.mContext, "key");
}
@Override
protected String getContentProtectionServiceFlatComponentName() {
return mConfigDefaultContentProtectionService;
}
}
} }