Add unit tests for verifying title
Bug: 236259020 Test: m RunSettingsRoboTests ROBOTEST_FILTER=EnterprisePrivacySettingsTest Change-Id: I520c94818951acce25c5d8a090b6d59feff93a1c
This commit is contained in:
@@ -17,18 +17,21 @@
|
|||||||
package com.android.settings.enterprise;
|
package com.android.settings.enterprise;
|
||||||
|
|
||||||
import static android.app.admin.DevicePolicyManager.DEVICE_OWNER_TYPE_DEFAULT;
|
import static android.app.admin.DevicePolicyManager.DEVICE_OWNER_TYPE_DEFAULT;
|
||||||
|
import static android.app.admin.DevicePolicyManager.DEVICE_OWNER_TYPE_FINANCED;
|
||||||
|
|
||||||
import static com.google.common.truth.Truth.assertThat;
|
import static com.google.common.truth.Truth.assertThat;
|
||||||
|
|
||||||
import static org.mockito.ArgumentMatchers.anyBoolean;
|
|
||||||
import static org.mockito.Mockito.spy;
|
import static org.mockito.Mockito.spy;
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
import android.app.admin.DevicePolicyManager;
|
import android.app.admin.DevicePolicyManager;
|
||||||
import android.content.ComponentName;
|
import android.content.ComponentName;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
import android.os.Bundle;
|
||||||
import android.provider.SearchIndexableResource;
|
import android.provider.SearchIndexableResource;
|
||||||
|
import android.widget.FrameLayout;
|
||||||
|
|
||||||
|
import androidx.appcompat.app.AppCompatActivity;
|
||||||
import androidx.test.core.app.ApplicationProvider;
|
import androidx.test.core.app.ApplicationProvider;
|
||||||
|
|
||||||
import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
|
import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
|
||||||
@@ -42,9 +45,10 @@ import org.junit.Test;
|
|||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.mockito.Mock;
|
import org.mockito.Mock;
|
||||||
import org.mockito.MockitoAnnotations;
|
import org.mockito.MockitoAnnotations;
|
||||||
|
import org.robolectric.Robolectric;
|
||||||
import org.robolectric.RobolectricTestRunner;
|
import org.robolectric.RobolectricTestRunner;
|
||||||
|
import org.robolectric.android.controller.ActivityController;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@RunWith(RobolectricTestRunner.class)
|
@RunWith(RobolectricTestRunner.class)
|
||||||
@@ -54,33 +58,33 @@ public class EnterprisePrivacySettingsTest extends AbsBasePrivacySettingsPrefere
|
|||||||
|
|
||||||
@Mock
|
@Mock
|
||||||
private DevicePolicyManager mDevicePolicyManager;
|
private DevicePolicyManager mDevicePolicyManager;
|
||||||
@Mock
|
|
||||||
private PrivacySettingsPreference mPrivacySettingsPreference;
|
|
||||||
private FakeFeatureFactory mFeatureFactory;
|
private FakeFeatureFactory mFeatureFactory;
|
||||||
private EnterprisePrivacySettings mSettings;
|
private EnterprisePrivacySettings mSettings;
|
||||||
private Context mContext;
|
private Context mContext;
|
||||||
|
private TestActivity mActivity;
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() {
|
public void setUp() {
|
||||||
MockitoAnnotations.initMocks(this);
|
MockitoAnnotations.initMocks(this);
|
||||||
mContext = spy(ApplicationProvider.getApplicationContext());
|
mContext = ApplicationProvider.getApplicationContext();
|
||||||
mFeatureFactory = FakeFeatureFactory.setupForTest();
|
mFeatureFactory = FakeFeatureFactory.setupForTest();
|
||||||
mSettings = new EnterprisePrivacySettings();
|
mSettings = new EnterprisePrivacySettings();
|
||||||
mSettings.mPrivacySettingsPreference = mPrivacySettingsPreference;
|
mSettings.mPrivacySettingsPreference = new PrivacySettingsEnterprisePreference(mContext);
|
||||||
|
|
||||||
when(mContext.getSystemService(DevicePolicyManager.class)).thenReturn(mDevicePolicyManager);
|
ActivityController<TestActivity> controller = Robolectric.buildActivity(
|
||||||
when(mDevicePolicyManager.isDeviceManaged()).thenReturn(true);
|
TestActivity.class).create();
|
||||||
when(mDevicePolicyManager.getDeviceOwnerComponentOnAnyUser())
|
mActivity = controller.get();
|
||||||
.thenReturn(DEVICE_OWNER_COMPONENT);
|
|
||||||
when(mDevicePolicyManager.getDeviceOwnerType(DEVICE_OWNER_COMPONENT))
|
mActivity
|
||||||
.thenReturn(DEVICE_OWNER_TYPE_DEFAULT);
|
.getSupportFragmentManager()
|
||||||
|
.beginTransaction()
|
||||||
|
.add(TestActivity.CONTAINER_VIEW_ID, mSettings)
|
||||||
|
.commit();
|
||||||
|
controller.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void verifyConstants() {
|
public void verifyConstants() {
|
||||||
when(mPrivacySettingsPreference.getPreferenceScreenResId())
|
|
||||||
.thenReturn(R.xml.enterprise_privacy_settings);
|
|
||||||
|
|
||||||
assertThat(mSettings.getMetricsCategory())
|
assertThat(mSettings.getMetricsCategory())
|
||||||
.isEqualTo(MetricsEvent.ENTERPRISE_PRIVACY_SETTINGS);
|
.isEqualTo(MetricsEvent.ENTERPRISE_PRIVACY_SETTINGS);
|
||||||
assertThat(mSettings.getLogTag()).isEqualTo("EnterprisePrivacySettings");
|
assertThat(mSettings.getLogTag()).isEqualTo("EnterprisePrivacySettings");
|
||||||
@@ -90,7 +94,7 @@ public class EnterprisePrivacySettingsTest extends AbsBasePrivacySettingsPrefere
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void isPageEnabled_hasDeviceOwner_shouldReturnTrue() {
|
public void isPageEnabled_hasDeviceOwner_returnsTrue() {
|
||||||
when(mFeatureFactory.enterprisePrivacyFeatureProvider.hasDeviceOwner())
|
when(mFeatureFactory.enterprisePrivacyFeatureProvider.hasDeviceOwner())
|
||||||
.thenReturn(true);
|
.thenReturn(true);
|
||||||
|
|
||||||
@@ -99,7 +103,7 @@ public class EnterprisePrivacySettingsTest extends AbsBasePrivacySettingsPrefere
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void isPageEnabled_noDeviceOwner_shouldReturnFalse() {
|
public void isPageEnabled_noDeviceOwner_returnsFalse() {
|
||||||
when(mDevicePolicyManager.isDeviceManaged()).thenReturn(false);
|
when(mDevicePolicyManager.isDeviceManaged()).thenReturn(false);
|
||||||
when(mFeatureFactory.enterprisePrivacyFeatureProvider.hasDeviceOwner())
|
when(mFeatureFactory.enterprisePrivacyFeatureProvider.hasDeviceOwner())
|
||||||
.thenReturn(false);
|
.thenReturn(false);
|
||||||
@@ -109,36 +113,105 @@ public class EnterprisePrivacySettingsTest extends AbsBasePrivacySettingsPrefere
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void getPreferenceControllers() {
|
public void getPreferenceControllers_returnsEnterprisePreferenceControllers() {
|
||||||
final List<AbstractPreferenceController> controllers = new ArrayList<>();
|
|
||||||
controllers.add(new NetworkLogsPreferenceController(mContext));
|
|
||||||
when(mPrivacySettingsPreference.createPreferenceControllers(anyBoolean()))
|
|
||||||
.thenReturn(controllers);
|
|
||||||
|
|
||||||
final List<AbstractPreferenceController> privacyControllers =
|
final List<AbstractPreferenceController> privacyControllers =
|
||||||
mSettings.createPreferenceControllers(mContext);
|
mSettings.createPreferenceControllers(mContext);
|
||||||
|
|
||||||
assertThat(privacyControllers).isNotNull();
|
verifyEnterprisePreferenceControllers(privacyControllers);
|
||||||
assertThat(privacyControllers.size()).isEqualTo(1);
|
|
||||||
assertThat(controllers.get(0)).isInstanceOf(NetworkLogsPreferenceController.class);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void
|
public void
|
||||||
getSearchIndexProviderPreferenceControllers_returnsEnterpriseSearchIndexPreferenceControllers() {
|
getSearchIndexProviderPreferenceControllers_returnsEnterpriseSearchIndexPreferenceControllers() {
|
||||||
|
Context context = spy(ApplicationProvider.getApplicationContext());
|
||||||
|
setupPrivacyPreference(context, DEVICE_OWNER_TYPE_DEFAULT);
|
||||||
|
|
||||||
final List<AbstractPreferenceController> controllers =
|
final List<AbstractPreferenceController> controllers =
|
||||||
EnterprisePrivacySettings.SEARCH_INDEX_DATA_PROVIDER
|
EnterprisePrivacySettings.SEARCH_INDEX_DATA_PROVIDER
|
||||||
.getPreferenceControllers(mContext);
|
.getPreferenceControllers(context);
|
||||||
|
|
||||||
verifyEnterprisePreferenceControllers(controllers);
|
verifyEnterprisePreferenceControllers(controllers);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void
|
||||||
|
getSearchIndexProviderPreferenceControllers_returnsFinancedSearchIndexPreferenceControllers() {
|
||||||
|
Context context = spy(ApplicationProvider.getApplicationContext());
|
||||||
|
setupPrivacyPreference(context, DEVICE_OWNER_TYPE_FINANCED);
|
||||||
|
|
||||||
|
final List<AbstractPreferenceController> controllers =
|
||||||
|
EnterprisePrivacySettings.SEARCH_INDEX_DATA_PROVIDER
|
||||||
|
.getPreferenceControllers(context);
|
||||||
|
|
||||||
|
verifyFinancedPreferenceControllers(controllers);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void getXmlResourcesToIndex_returnsEnterpriseXmlResources() {
|
public void getXmlResourcesToIndex_returnsEnterpriseXmlResources() {
|
||||||
|
Context context = spy(ApplicationProvider.getApplicationContext());
|
||||||
|
setupPrivacyPreference(context, DEVICE_OWNER_TYPE_DEFAULT);
|
||||||
|
|
||||||
final List<SearchIndexableResource> searchIndexableResources =
|
final List<SearchIndexableResource> searchIndexableResources =
|
||||||
EnterprisePrivacySettings.SEARCH_INDEX_DATA_PROVIDER
|
EnterprisePrivacySettings.SEARCH_INDEX_DATA_PROVIDER
|
||||||
.getXmlResourcesToIndex(mContext, true);
|
.getXmlResourcesToIndex(context, true);
|
||||||
|
|
||||||
verifyEnterpriseSearchIndexableResources(searchIndexableResources);
|
verifyEnterpriseSearchIndexableResources(searchIndexableResources);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void getXmlResourcesToIndex_returnsFinancedXmlResources() {
|
||||||
|
Context context = spy(ApplicationProvider.getApplicationContext());
|
||||||
|
setupPrivacyPreference(context, DEVICE_OWNER_TYPE_FINANCED);
|
||||||
|
|
||||||
|
final List<SearchIndexableResource> searchIndexableResources =
|
||||||
|
EnterprisePrivacySettings.SEARCH_INDEX_DATA_PROVIDER
|
||||||
|
.getXmlResourcesToIndex(context, true);
|
||||||
|
|
||||||
|
verifyFinancedSearchIndexableResources(searchIndexableResources);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void onCreate_enterprisePrivacyPreference_updatesTitle() {
|
||||||
|
mSettings.onCreate(new Bundle());
|
||||||
|
|
||||||
|
assertThat(mActivity.getTitle())
|
||||||
|
.isEqualTo(mContext.getText(R.string.enterprise_privacy_settings));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void onCreate_financedPrivacyPreference_doesNotUpdateTitle() {
|
||||||
|
mSettings.mPrivacySettingsPreference = new PrivacySettingsFinancedPreference(mContext);
|
||||||
|
|
||||||
|
mSettings.onCreate(new Bundle());
|
||||||
|
|
||||||
|
assertThat(mActivity.getTitle())
|
||||||
|
.isEqualTo(mContext.getText(R.string.financed_privacy_settings));
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setupPrivacyPreference(Context context, int deviceOwnerType) {
|
||||||
|
when(context.getSystemService(DevicePolicyManager.class)).thenReturn(mDevicePolicyManager);
|
||||||
|
when(mDevicePolicyManager.isDeviceManaged()).thenReturn(true);
|
||||||
|
when(mDevicePolicyManager.getDeviceOwnerComponentOnAnyUser())
|
||||||
|
.thenReturn(DEVICE_OWNER_COMPONENT);
|
||||||
|
when(mDevicePolicyManager.getDeviceOwnerType(DEVICE_OWNER_COMPONENT))
|
||||||
|
.thenReturn(deviceOwnerType);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static final class TestActivity extends AppCompatActivity {
|
||||||
|
|
||||||
|
private static final int CONTAINER_VIEW_ID = 1234;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onCreate(Bundle bundle) {
|
||||||
|
super.onCreate(bundle);
|
||||||
|
|
||||||
|
FrameLayout frameLayout = new FrameLayout(this);
|
||||||
|
frameLayout.setId(CONTAINER_VIEW_ID);
|
||||||
|
|
||||||
|
// Need to set the Theme.AppCompat theme (or descendant) with this activity, otherwise
|
||||||
|
// a {@link IllegalStateException} is thrown when setting the content view.
|
||||||
|
setTheme(R.style.Theme_AppCompat_Light);
|
||||||
|
setContentView(frameLayout);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user