Refactor to use the utils class.
Bug: 298076922 Change-Id: I0085fdf7594d0ba9243a7b44600252d8c5b21608
This commit is contained in:
@@ -36,32 +36,8 @@ public class ContentProtectionPreferenceController extends BasePreferenceControl
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getAvailabilityStatus() {
|
public int getAvailabilityStatus() {
|
||||||
if (!settingUiEnabled() || getContentProtectionServiceComponentName() == null) {
|
return ContentProtectionPreferenceUtils.isAvailable(mContext)
|
||||||
return UNSUPPORTED_ON_DEVICE;
|
? AVAILABLE
|
||||||
}
|
: 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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -16,23 +16,35 @@
|
|||||||
|
|
||||||
package com.android.settings.security;
|
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 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.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.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.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.robolectric.RobolectricTestRunner;
|
import org.robolectric.RobolectricTestRunner;
|
||||||
|
import org.robolectric.RuntimeEnvironment;
|
||||||
|
import org.robolectric.annotation.Config;
|
||||||
|
|
||||||
@RunWith(RobolectricTestRunner.class)
|
@RunWith(RobolectricTestRunner.class)
|
||||||
|
@Config(
|
||||||
|
shadows = {
|
||||||
|
ShadowDeviceConfig.class,
|
||||||
|
})
|
||||||
public class ContentProtectionPreferenceControllerTest {
|
public class ContentProtectionPreferenceControllerTest {
|
||||||
|
|
||||||
private static final String PACKAGE_NAME = "com.test.package";
|
private static final String PACKAGE_NAME = "com.test.package";
|
||||||
@@ -42,84 +54,40 @@ public class ContentProtectionPreferenceControllerTest {
|
|||||||
|
|
||||||
@Rule public final SetFlagsRule mSetFlagsRule = new SetFlagsRule();
|
@Rule public final SetFlagsRule mSetFlagsRule = new SetFlagsRule();
|
||||||
|
|
||||||
private final Context mContext = ApplicationProvider.getApplicationContext();
|
private Context mContext;
|
||||||
|
|
||||||
private String mConfigDefaultContentProtectionService = COMPONENT_NAME.flattenToString();
|
private String mConfigDefaultContentProtectionService = COMPONENT_NAME.flattenToString();
|
||||||
|
|
||||||
private ContentProtectionPreferenceController mController;
|
private ContentProtectionPreferenceController mController;
|
||||||
|
|
||||||
private boolean mSettingUiEnabled;
|
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() {
|
public void setUp() {
|
||||||
mController = new TestContentProtectionPreferenceController();
|
mContext = spy(RuntimeEnvironment.application);
|
||||||
|
mController = new ContentProtectionPreferenceController(mContext, "key");
|
||||||
|
}
|
||||||
|
|
||||||
|
@After
|
||||||
|
public void tearDown() {
|
||||||
|
ShadowDeviceConfig.reset();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void isAvailable_flagSettingUiDisabled_isFalse() {
|
public void isAvailable_isFalse() {
|
||||||
mSettingUiEnabled = false;
|
|
||||||
|
|
||||||
assertThat(mController.isAvailable()).isFalse();
|
assertThat(mController.isAvailable()).isFalse();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void isAvailable_componentNameNull_isFalse() {
|
public void isAvailable_isTrue() {
|
||||||
mConfigDefaultContentProtectionService = null;
|
doReturn(COMPONENT_NAME.flattenToString())
|
||||||
mSetFlagsRule.enableFlags(FLAG_SETTING_UI_ENABLED);
|
.when(mContext)
|
||||||
mController = new TestContentProtectionPreferenceController();
|
.getString(config_defaultContentProtectionService);
|
||||||
|
|
||||||
assertThat(mController.isAvailable()).isFalse();
|
DeviceConfig.setProperty(
|
||||||
}
|
DeviceConfig.NAMESPACE_CONTENT_CAPTURE,
|
||||||
|
ContentCaptureManager.DEVICE_CONFIG_PROPERTY_ENABLE_CONTENT_PROTECTION_RECEIVER,
|
||||||
@Test
|
"true",
|
||||||
public void isAvailable_componentNameEmpty_isFalse() {
|
/* makeDefault= */ false);
|
||||||
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;
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected boolean settingUiEnabled() {
|
|
||||||
return mSettingUiEnabled;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user