From 54cb55b87e9ec535620f02881245d2c5eae7033b Mon Sep 17 00:00:00 2001 From: Salud Lemus Date: Wed, 22 Jun 2022 16:11:00 -0700 Subject: [PATCH] Add unit tests for verifying title Bug: 236259020 Test: m RunSettingsRoboTests ROBOTEST_FILTER=EnterprisePrivacySettingsTest Change-Id: I520c94818951acce25c5d8a090b6d59feff93a1c --- .../EnterprisePrivacySettingsTest.java | 129 ++++++++++++++---- 1 file changed, 101 insertions(+), 28 deletions(-) diff --git a/tests/robotests/src/com/android/settings/enterprise/EnterprisePrivacySettingsTest.java b/tests/robotests/src/com/android/settings/enterprise/EnterprisePrivacySettingsTest.java index e028c629dbe..b3de1841989 100644 --- a/tests/robotests/src/com/android/settings/enterprise/EnterprisePrivacySettingsTest.java +++ b/tests/robotests/src/com/android/settings/enterprise/EnterprisePrivacySettingsTest.java @@ -17,18 +17,21 @@ package com.android.settings.enterprise; 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 org.mockito.ArgumentMatchers.anyBoolean; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.when; import android.app.admin.DevicePolicyManager; import android.content.ComponentName; import android.content.Context; +import android.os.Bundle; import android.provider.SearchIndexableResource; +import android.widget.FrameLayout; +import androidx.appcompat.app.AppCompatActivity; import androidx.test.core.app.ApplicationProvider; import com.android.internal.logging.nano.MetricsProto.MetricsEvent; @@ -42,9 +45,10 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.MockitoAnnotations; +import org.robolectric.Robolectric; import org.robolectric.RobolectricTestRunner; +import org.robolectric.android.controller.ActivityController; -import java.util.ArrayList; import java.util.List; @RunWith(RobolectricTestRunner.class) @@ -54,33 +58,33 @@ public class EnterprisePrivacySettingsTest extends AbsBasePrivacySettingsPrefere @Mock private DevicePolicyManager mDevicePolicyManager; - @Mock - private PrivacySettingsPreference mPrivacySettingsPreference; private FakeFeatureFactory mFeatureFactory; private EnterprisePrivacySettings mSettings; private Context mContext; + private TestActivity mActivity; @Before public void setUp() { MockitoAnnotations.initMocks(this); - mContext = spy(ApplicationProvider.getApplicationContext()); + mContext = ApplicationProvider.getApplicationContext(); mFeatureFactory = FakeFeatureFactory.setupForTest(); mSettings = new EnterprisePrivacySettings(); - mSettings.mPrivacySettingsPreference = mPrivacySettingsPreference; + mSettings.mPrivacySettingsPreference = new PrivacySettingsEnterprisePreference(mContext); - when(mContext.getSystemService(DevicePolicyManager.class)).thenReturn(mDevicePolicyManager); - when(mDevicePolicyManager.isDeviceManaged()).thenReturn(true); - when(mDevicePolicyManager.getDeviceOwnerComponentOnAnyUser()) - .thenReturn(DEVICE_OWNER_COMPONENT); - when(mDevicePolicyManager.getDeviceOwnerType(DEVICE_OWNER_COMPONENT)) - .thenReturn(DEVICE_OWNER_TYPE_DEFAULT); + ActivityController controller = Robolectric.buildActivity( + TestActivity.class).create(); + mActivity = controller.get(); + + mActivity + .getSupportFragmentManager() + .beginTransaction() + .add(TestActivity.CONTAINER_VIEW_ID, mSettings) + .commit(); + controller.start(); } @Test public void verifyConstants() { - when(mPrivacySettingsPreference.getPreferenceScreenResId()) - .thenReturn(R.xml.enterprise_privacy_settings); - assertThat(mSettings.getMetricsCategory()) .isEqualTo(MetricsEvent.ENTERPRISE_PRIVACY_SETTINGS); assertThat(mSettings.getLogTag()).isEqualTo("EnterprisePrivacySettings"); @@ -90,7 +94,7 @@ public class EnterprisePrivacySettingsTest extends AbsBasePrivacySettingsPrefere } @Test - public void isPageEnabled_hasDeviceOwner_shouldReturnTrue() { + public void isPageEnabled_hasDeviceOwner_returnsTrue() { when(mFeatureFactory.enterprisePrivacyFeatureProvider.hasDeviceOwner()) .thenReturn(true); @@ -99,7 +103,7 @@ public class EnterprisePrivacySettingsTest extends AbsBasePrivacySettingsPrefere } @Test - public void isPageEnabled_noDeviceOwner_shouldReturnFalse() { + public void isPageEnabled_noDeviceOwner_returnsFalse() { when(mDevicePolicyManager.isDeviceManaged()).thenReturn(false); when(mFeatureFactory.enterprisePrivacyFeatureProvider.hasDeviceOwner()) .thenReturn(false); @@ -109,36 +113,105 @@ public class EnterprisePrivacySettingsTest extends AbsBasePrivacySettingsPrefere } @Test - public void getPreferenceControllers() { - final List controllers = new ArrayList<>(); - controllers.add(new NetworkLogsPreferenceController(mContext)); - when(mPrivacySettingsPreference.createPreferenceControllers(anyBoolean())) - .thenReturn(controllers); - + public void getPreferenceControllers_returnsEnterprisePreferenceControllers() { final List privacyControllers = mSettings.createPreferenceControllers(mContext); - assertThat(privacyControllers).isNotNull(); - assertThat(privacyControllers.size()).isEqualTo(1); - assertThat(controllers.get(0)).isInstanceOf(NetworkLogsPreferenceController.class); + verifyEnterprisePreferenceControllers(privacyControllers); } @Test public void getSearchIndexProviderPreferenceControllers_returnsEnterpriseSearchIndexPreferenceControllers() { + Context context = spy(ApplicationProvider.getApplicationContext()); + setupPrivacyPreference(context, DEVICE_OWNER_TYPE_DEFAULT); + final List controllers = EnterprisePrivacySettings.SEARCH_INDEX_DATA_PROVIDER - .getPreferenceControllers(mContext); + .getPreferenceControllers(context); verifyEnterprisePreferenceControllers(controllers); } + @Test + public void + getSearchIndexProviderPreferenceControllers_returnsFinancedSearchIndexPreferenceControllers() { + Context context = spy(ApplicationProvider.getApplicationContext()); + setupPrivacyPreference(context, DEVICE_OWNER_TYPE_FINANCED); + + final List controllers = + EnterprisePrivacySettings.SEARCH_INDEX_DATA_PROVIDER + .getPreferenceControllers(context); + + verifyFinancedPreferenceControllers(controllers); + } + @Test public void getXmlResourcesToIndex_returnsEnterpriseXmlResources() { + Context context = spy(ApplicationProvider.getApplicationContext()); + setupPrivacyPreference(context, DEVICE_OWNER_TYPE_DEFAULT); + final List searchIndexableResources = EnterprisePrivacySettings.SEARCH_INDEX_DATA_PROVIDER - .getXmlResourcesToIndex(mContext, true); + .getXmlResourcesToIndex(context, true); verifyEnterpriseSearchIndexableResources(searchIndexableResources); } + + @Test + public void getXmlResourcesToIndex_returnsFinancedXmlResources() { + Context context = spy(ApplicationProvider.getApplicationContext()); + setupPrivacyPreference(context, DEVICE_OWNER_TYPE_FINANCED); + + final List 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); + } + } }