diff --git a/AndroidManifest.xml b/AndroidManifest.xml index 1e5964ed2f0..05c5075b42f 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -1920,7 +1920,8 @@ android:label="@string/development_settings_title" android:icon="@drawable/ic_settings_development" android:taskAffinity="com.android.settings" - android:parentActivityName="Settings"> + android:parentActivityName="Settings" + android:enabled="false"> @@ -1989,15 +1990,6 @@ android:value="com.android.settings.print.PrintJobSettingsFragment" /> - - - - - @@ -2006,15 +1998,6 @@ android:label="@string/select_webview_provider_dialog_title"> - - - - - 24dp 64dp + 32dp 24dp diff --git a/res/values/dimens.xml b/res/values/dimens.xml index 0d4289a83b9..2f00297dc43 100755 --- a/res/values/dimens.xml +++ b/res/values/dimens.xml @@ -121,6 +121,7 @@ -8dp 48dp + 24dp 16dp diff --git a/res/xml/date_time_prefs.xml b/res/xml/date_time_prefs.xml index d74e8c2cb84..5219caaa447 100644 --- a/res/xml/date_time_prefs.xml +++ b/res/xml/date_time_prefs.xml @@ -29,32 +29,37 @@ android:summaryOn="@string/date_time_auto_summaryOn" android:summaryOff="@string/date_time_auto_summaryOff" settings:useAdditionalSummary="true" - settings:restrictedSwitchSummary="@string/enabled_by_admin" /> + settings:restrictedSwitchSummary="@string/enabled_by_admin" + settings:userRestriction="no_config_date_time" /> - + android:summary="@string/summary_placeholder" + settings:userRestriction="no_config_date_time" /> - + android:summary="@string/summary_placeholder" + settings:userRestriction="no_config_date_time" /> - + android:summaryOff="@string/zone_auto_summaryOff" + settings:userRestriction="no_config_date_time" /> - + android:summary="GMT-8:00" + settings:userRestriction="no_config_date_time" /> getPreferenceControllers(Context context) { + if (Utils.isMonkeyRunning()) { + mPreferenceControllers = new ArrayList<>(); + return null; + } mPreferenceControllers = buildPreferenceControllers(context, getActivity(), getLifecycle(), this /* devOptionsDashboardFragment */, new BluetoothA2dpConfigStore()); @@ -326,6 +329,9 @@ public class DevelopmentSettingsDashboardFragment extends RestrictedDashboardFra } private void enableDeveloperOptions() { + if (Utils.isMonkeyRunning()) { + return; + } DevelopmentSettingsEnabler.setDevelopmentSettingsEnabled(getContext(), true); for (AbstractPreferenceController controller : mPreferenceControllers) { if (controller instanceof DeveloperOptionsPreferenceController) { @@ -335,6 +341,9 @@ public class DevelopmentSettingsDashboardFragment extends RestrictedDashboardFra } private void disableDeveloperOptions() { + if (Utils.isMonkeyRunning()) { + return; + } DevelopmentSettingsEnabler.setDevelopmentSettingsEnabled(getContext(), false); final SystemPropPoker poker = SystemPropPoker.getInstance(); poker.blockPokes(); diff --git a/tests/robotests/src/com/android/settings/SetupChooseLockPatternTest.java b/tests/robotests/src/com/android/settings/SetupChooseLockPatternTest.java index 206ba953a57..17d10218b8c 100644 --- a/tests/robotests/src/com/android/settings/SetupChooseLockPatternTest.java +++ b/tests/robotests/src/com/android/settings/SetupChooseLockPatternTest.java @@ -30,6 +30,7 @@ import com.android.settings.password.ChooseLockPattern.IntentBuilder; import com.android.settings.password.SetupChooseLockPattern; import com.android.settings.testutils.SettingsRobolectricTestRunner; import com.android.settings.testutils.shadow.SettingsShadowResources; +import com.android.settings.testutils.shadow.SettingsShadowResourcesImpl; import com.android.settings.testutils.shadow.ShadowEventLogWriter; import com.android.settings.testutils.shadow.ShadowUtils; @@ -44,9 +45,10 @@ import org.robolectric.shadows.ShadowPackageManager.ComponentState; @RunWith(SettingsRobolectricTestRunner.class) @Config( manifest = TestConfig.MANIFEST_PATH, - sdk = TestConfig.SDK_VERSION, + sdk = TestConfig.SDK_VERSION_O, shadows = { SettingsShadowResources.class, + SettingsShadowResourcesImpl.class, SettingsShadowResources.SettingsShadowTheme.class, ShadowEventLogWriter.class, ShadowUtils.class @@ -62,7 +64,7 @@ public class SetupChooseLockPatternTest { PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP); - mActivity = Robolectric.buildActivity( + mActivity = Robolectric.buildActivity( SetupChooseLockPattern.class, SetupChooseLockPattern.modifyIntentForSetup( application, diff --git a/tests/robotests/src/com/android/settings/accessibility/AccessibilitySettingsTest.java b/tests/robotests/src/com/android/settings/accessibility/AccessibilitySettingsTest.java index fb32da11ccd..66165374ad8 100644 --- a/tests/robotests/src/com/android/settings/accessibility/AccessibilitySettingsTest.java +++ b/tests/robotests/src/com/android/settings/accessibility/AccessibilitySettingsTest.java @@ -33,7 +33,7 @@ import java.util.List; import static com.google.common.truth.Truth.assertThat; @RunWith(SettingsRobolectricTestRunner.class) -@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) +@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION_O) public class AccessibilitySettingsTest { @Test diff --git a/tests/robotests/src/com/android/settings/accessibility/ShortcutServicePickerFragmentTest.java b/tests/robotests/src/com/android/settings/accessibility/ShortcutServicePickerFragmentTest.java index 66621a000bd..2e95c732b8a 100644 --- a/tests/robotests/src/com/android/settings/accessibility/ShortcutServicePickerFragmentTest.java +++ b/tests/robotests/src/com/android/settings/accessibility/ShortcutServicePickerFragmentTest.java @@ -41,7 +41,7 @@ import org.robolectric.RuntimeEnvironment; import org.robolectric.annotation.Config; @RunWith(SettingsRobolectricTestRunner.class) -@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) +@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION_O) public class ShortcutServicePickerFragmentTest { private static final String TEST_SERVICE_KEY_1 = "abc/123"; diff --git a/tests/robotests/src/com/android/settings/accessibility/ToggleFeaturePreferenceFragmentTest.java b/tests/robotests/src/com/android/settings/accessibility/ToggleFeaturePreferenceFragmentTest.java index 5ef5a12c36f..8d1d1743edb 100644 --- a/tests/robotests/src/com/android/settings/accessibility/ToggleFeaturePreferenceFragmentTest.java +++ b/tests/robotests/src/com/android/settings/accessibility/ToggleFeaturePreferenceFragmentTest.java @@ -38,7 +38,7 @@ import org.robolectric.annotation.Config; import org.robolectric.util.FragmentTestUtil; @RunWith(SettingsRobolectricTestRunner.class) -@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION, +@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION_O, shadows = { SettingsShadowResources.class, SettingsShadowResources.SettingsShadowTheme.class, diff --git a/tests/robotests/src/com/android/settings/accounts/AccountDetailDashboardFragmentTest.java b/tests/robotests/src/com/android/settings/accounts/AccountDetailDashboardFragmentTest.java index cfed83b790a..89245510d46 100644 --- a/tests/robotests/src/com/android/settings/accounts/AccountDetailDashboardFragmentTest.java +++ b/tests/robotests/src/com/android/settings/accounts/AccountDetailDashboardFragmentTest.java @@ -55,15 +55,13 @@ import static org.mockito.Mockito.when; import static org.robolectric.Shadows.shadowOf; @RunWith(SettingsRobolectricTestRunner.class) -@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) +@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION_O) public class AccountDetailDashboardFragmentTest { private static final String METADATA_CATEGORY = "com.android.settings.category"; private static final String METADATA_ACCOUNT_TYPE = "com.android.settings.ia.account"; private static final String METADATA_USER_HANDLE = "user_handle"; - @Mock(answer = RETURNS_DEEP_STUBS) - private Activity mActivity; @Mock(answer = RETURNS_DEEP_STUBS) private AccountManager mAccountManager; @Mock @@ -129,11 +127,11 @@ public class AccountDetailDashboardFragmentTest { @Test public void refreshDashboardTiles_HasAccountType_shouldAddAccountNameToIntent() { - FakeFeatureFactory.setupForTest(mActivity); + FakeFeatureFactory.setupForTest(mContext); final FakeFeatureFactory featureFactory = - (FakeFeatureFactory) FakeFeatureFactory.getFactory(mActivity); + (FakeFeatureFactory) FakeFeatureFactory.getFactory(mContext); final DashboardFeatureProviderImpl dashboardFeatureProvider = - new DashboardFeatureProviderImpl(mActivity); + new DashboardFeatureProviderImpl(mContext); final PackageManager packageManager = mock(PackageManager.class); ReflectionHelpers.setField(dashboardFeatureProvider, "mPackageManager", packageManager); when(packageManager.resolveActivity(any(Intent.class), anyInt())) diff --git a/tests/robotests/src/com/android/settings/accounts/AccountHeaderPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/accounts/AccountHeaderPreferenceControllerTest.java index 4380e5c0b81..1b20e96b064 100644 --- a/tests/robotests/src/com/android/settings/accounts/AccountHeaderPreferenceControllerTest.java +++ b/tests/robotests/src/com/android/settings/accounts/AccountHeaderPreferenceControllerTest.java @@ -56,7 +56,7 @@ import org.robolectric.annotation.Implements; @RunWith(SettingsRobolectricTestRunner.class) @Config( manifest = TestConfig.MANIFEST_PATH, - sdk = TestConfig.SDK_VERSION, + sdk = TestConfig.SDK_VERSION_O, shadows = AccountHeaderPreferenceControllerTest.ShadowAuthenticatorHelper.class ) public class AccountHeaderPreferenceControllerTest { diff --git a/tests/robotests/src/com/android/settings/accounts/AccountPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/accounts/AccountPreferenceControllerTest.java index 139bee79f6d..6daaeca4190 100644 --- a/tests/robotests/src/com/android/settings/accounts/AccountPreferenceControllerTest.java +++ b/tests/robotests/src/com/android/settings/accounts/AccountPreferenceControllerTest.java @@ -68,7 +68,7 @@ import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @RunWith(SettingsRobolectricTestRunner.class) -@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION, +@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION_O, shadows = {ShadowAccountManager.class, ShadowContentResolver.class}) public class AccountPreferenceControllerTest { diff --git a/tests/robotests/src/com/android/settings/accounts/AccountPreferenceTest.java b/tests/robotests/src/com/android/settings/accounts/AccountPreferenceTest.java index 07780432be8..43995806238 100644 --- a/tests/robotests/src/com/android/settings/accounts/AccountPreferenceTest.java +++ b/tests/robotests/src/com/android/settings/accounts/AccountPreferenceTest.java @@ -35,7 +35,7 @@ import static org.mockito.Mockito.spy; import static org.mockito.Mockito.verify; @RunWith(SettingsRobolectricTestRunner.class) -@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) +@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION_O) public class AccountPreferenceTest { private Context mContext; diff --git a/tests/robotests/src/com/android/settings/accounts/AccountSyncPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/accounts/AccountSyncPreferenceControllerTest.java index 5fbd3a706a4..dde68f54872 100644 --- a/tests/robotests/src/com/android/settings/accounts/AccountSyncPreferenceControllerTest.java +++ b/tests/robotests/src/com/android/settings/accounts/AccountSyncPreferenceControllerTest.java @@ -47,7 +47,7 @@ import org.robolectric.annotation.Config; import org.robolectric.shadows.ShadowApplication; @RunWith(SettingsRobolectricTestRunner.class) -@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION, +@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION_O, shadows = {ShadowAccountManager.class, ShadowContentResolver.class}) public class AccountSyncPreferenceControllerTest { diff --git a/tests/robotests/src/com/android/settings/accounts/AccountTypePreferenceLoaderTest.java b/tests/robotests/src/com/android/settings/accounts/AccountTypePreferenceLoaderTest.java index fff73b7f8e9..2e7f0fae0a6 100644 --- a/tests/robotests/src/com/android/settings/accounts/AccountTypePreferenceLoaderTest.java +++ b/tests/robotests/src/com/android/settings/accounts/AccountTypePreferenceLoaderTest.java @@ -50,7 +50,7 @@ import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @RunWith(SettingsRobolectricTestRunner.class) -@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) +@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION_O) public class AccountTypePreferenceLoaderTest { @Mock(answer = RETURNS_DEEP_STUBS) diff --git a/tests/robotests/src/com/android/settings/accounts/AddUserWhenLockedPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/accounts/AddUserWhenLockedPreferenceControllerTest.java index faaf7db9331..e00a0aefd00 100644 --- a/tests/robotests/src/com/android/settings/accounts/AddUserWhenLockedPreferenceControllerTest.java +++ b/tests/robotests/src/com/android/settings/accounts/AddUserWhenLockedPreferenceControllerTest.java @@ -41,7 +41,7 @@ import org.robolectric.annotation.Config; import org.robolectric.shadows.ShadowApplication; @RunWith(SettingsRobolectricTestRunner.class) -@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) +@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION_O) public class AddUserWhenLockedPreferenceControllerTest { @Mock(answer = RETURNS_DEEP_STUBS) diff --git a/tests/robotests/src/com/android/settings/accounts/AutoSyncDataPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/accounts/AutoSyncDataPreferenceControllerTest.java index 4715f88d06e..336de213691 100644 --- a/tests/robotests/src/com/android/settings/accounts/AutoSyncDataPreferenceControllerTest.java +++ b/tests/robotests/src/com/android/settings/accounts/AutoSyncDataPreferenceControllerTest.java @@ -44,14 +44,14 @@ import java.util.ArrayList; import java.util.List; @RunWith(SettingsRobolectricTestRunner.class) -@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) +@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION_O) public class AutoSyncDataPreferenceControllerTest { @Mock(answer = RETURNS_DEEP_STUBS) private PreferenceScreen mScreen; @Mock(answer = RETURNS_DEEP_STUBS) private UserManager mUserManager; - @Mock(answer = RETURNS_DEEP_STUBS) + @Mock private Fragment mFragment; private Preference mPreference; diff --git a/tests/robotests/src/com/android/settings/accounts/AutoSyncPersonalDataPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/accounts/AutoSyncPersonalDataPreferenceControllerTest.java index 597fbd479ef..50d4df883bf 100644 --- a/tests/robotests/src/com/android/settings/accounts/AutoSyncPersonalDataPreferenceControllerTest.java +++ b/tests/robotests/src/com/android/settings/accounts/AutoSyncPersonalDataPreferenceControllerTest.java @@ -42,7 +42,7 @@ import java.util.ArrayList; import java.util.List; @RunWith(SettingsRobolectricTestRunner.class) -@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) +@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION_O) public class AutoSyncPersonalDataPreferenceControllerTest { @Mock(answer = RETURNS_DEEP_STUBS) diff --git a/tests/robotests/src/com/android/settings/accounts/AutoSyncWorkDataPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/accounts/AutoSyncWorkDataPreferenceControllerTest.java index a25aa281a01..35a8bb6951d 100644 --- a/tests/robotests/src/com/android/settings/accounts/AutoSyncWorkDataPreferenceControllerTest.java +++ b/tests/robotests/src/com/android/settings/accounts/AutoSyncWorkDataPreferenceControllerTest.java @@ -42,7 +42,7 @@ import java.util.ArrayList; import java.util.List; @RunWith(SettingsRobolectricTestRunner.class) -@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) +@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION_O) public class AutoSyncWorkDataPreferenceControllerTest { private static int MANAGED_PROFILE_ID = 10; diff --git a/tests/robotests/src/com/android/settings/accounts/ProviderPreferenceTest.java b/tests/robotests/src/com/android/settings/accounts/ProviderPreferenceTest.java index 4e4bb4feeaf..4d1667f407c 100644 --- a/tests/robotests/src/com/android/settings/accounts/ProviderPreferenceTest.java +++ b/tests/robotests/src/com/android/settings/accounts/ProviderPreferenceTest.java @@ -32,7 +32,7 @@ import org.robolectric.annotation.Config; import org.robolectric.util.ReflectionHelpers; @RunWith(SettingsRobolectricTestRunner.class) -@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) +@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION_O) public class ProviderPreferenceTest { private Context mContext; diff --git a/tests/robotests/src/com/android/settings/accounts/RemoveAccountPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/accounts/RemoveAccountPreferenceControllerTest.java index d4e866f8044..8250340d5a2 100644 --- a/tests/robotests/src/com/android/settings/accounts/RemoveAccountPreferenceControllerTest.java +++ b/tests/robotests/src/com/android/settings/accounts/RemoveAccountPreferenceControllerTest.java @@ -40,6 +40,7 @@ import android.os.Handler; import android.os.UserHandle; import android.os.UserManager; import android.support.v14.preference.PreferenceFragment; +import android.support.v7.preference.PreferenceManager; import android.support.v7.preference.PreferenceScreen; import android.widget.Button; @@ -60,7 +61,7 @@ import org.robolectric.annotation.Config; import org.robolectric.shadows.ShadowApplication; @RunWith(SettingsRobolectricTestRunner.class) -@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) +@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION_O) public class RemoveAccountPreferenceControllerTest { private static final String KEY_REMOVE_ACCOUNT = "remove_account"; @@ -70,9 +71,11 @@ public class RemoveAccountPreferenceControllerTest { private AccountManager mAccountManager; @Mock private DevicePolicyManagerWrapper mDevicePolicyManager; - @Mock(answer = RETURNS_DEEP_STUBS) + @Mock private PreferenceFragment mFragment; @Mock + private PreferenceManager mPreferenceManager; + @Mock private PreferenceScreen mScreen; @Mock private FragmentManager mFragmentManager; @@ -92,7 +95,8 @@ public class RemoveAccountPreferenceControllerTest { mContext = spy(shadowContext.getApplicationContext()); when(mFragment.getPreferenceScreen()).thenReturn(mScreen); - when(mFragment.getPreferenceManager().getContext()).thenReturn(mContext); + when(mFragment.getPreferenceManager()).thenReturn(mPreferenceManager); + when(mPreferenceManager.getContext()).thenReturn(mContext); when(mFragment.getFragmentManager()).thenReturn(mFragmentManager); when(mFragmentManager.beginTransaction()).thenReturn(mFragmentTransaction); when(mAccountManager.getAuthenticatorTypesAsUser(anyInt())).thenReturn( diff --git a/tests/robotests/src/com/android/settings/accounts/RemoveUserFragmentTest.java b/tests/robotests/src/com/android/settings/accounts/RemoveUserFragmentTest.java index cd73068e194..ef23d22aca7 100644 --- a/tests/robotests/src/com/android/settings/accounts/RemoveUserFragmentTest.java +++ b/tests/robotests/src/com/android/settings/accounts/RemoveUserFragmentTest.java @@ -28,7 +28,7 @@ import java.lang.reflect.Modifier; import static com.google.common.truth.Truth.assertThat; @RunWith(SettingsRobolectricTestRunner.class) -@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) +@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION_O) public class RemoveUserFragmentTest { @Test diff --git a/tests/robotests/src/com/android/settings/accounts/UserAndAccountDashboardFragmentTest.java b/tests/robotests/src/com/android/settings/accounts/UserAndAccountDashboardFragmentTest.java index 8dba2f25371..2383ed14706 100644 --- a/tests/robotests/src/com/android/settings/accounts/UserAndAccountDashboardFragmentTest.java +++ b/tests/robotests/src/com/android/settings/accounts/UserAndAccountDashboardFragmentTest.java @@ -43,7 +43,7 @@ import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @RunWith(RobolectricTestRunner.class) -@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) +@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION_O) public class UserAndAccountDashboardFragmentTest { private static final String METADATA_CATEGORY = "com.android.settings.category"; diff --git a/tests/robotests/src/com/android/settings/datetime/DatePreferenceControllerTest.java b/tests/robotests/src/com/android/settings/datetime/DatePreferenceControllerTest.java index e6a6dd842e0..f1d34f8219a 100644 --- a/tests/robotests/src/com/android/settings/datetime/DatePreferenceControllerTest.java +++ b/tests/robotests/src/com/android/settings/datetime/DatePreferenceControllerTest.java @@ -22,6 +22,7 @@ import android.support.v7.preference.Preference; import com.android.settings.testutils.SettingsRobolectricTestRunner; import com.android.settings.TestConfig; +import com.android.settingslib.RestrictedPreference; import org.junit.Before; import org.junit.Test; @@ -48,14 +49,15 @@ public class DatePreferenceControllerTest { @Mock private AutoTimePreferenceController mAutoTimePreferenceController; - private Preference mPreference; + private RestrictedPreference mPreference; private DatePreferenceController mController; @Before public void setUp() { MockitoAnnotations.initMocks(this); when(mContext.getSystemService(Context.ALARM_SERVICE)).thenReturn(mAlarmManager); - mPreference = new Preference(ShadowApplication.getInstance().getApplicationContext()); + mPreference = new RestrictedPreference(ShadowApplication.getInstance(). + getApplicationContext()); mController = new DatePreferenceController(mContext, mHost, mAutoTimePreferenceController); } @@ -72,6 +74,9 @@ public class DatePreferenceControllerTest { @Test public void updateState_autoTimeEnabled_shouldDisablePref() { + // Make sure not disabled by admin. + mPreference.setDisabledByAdmin(null); + when(mAutoTimePreferenceController.isEnabled()).thenReturn(true); mController.updateState(mPreference); @@ -80,6 +85,9 @@ public class DatePreferenceControllerTest { @Test public void updateState_autoTimeDisabled_shouldEnablePref() { + // Make sure not disabled by admin. + mPreference.setDisabledByAdmin(null); + when(mAutoTimePreferenceController.isEnabled()).thenReturn(false); mController.updateState(mPreference); diff --git a/tests/robotests/src/com/android/settings/datetime/TimePreferenceControllerTest.java b/tests/robotests/src/com/android/settings/datetime/TimePreferenceControllerTest.java index 15877e0dc3e..89c5f47e935 100644 --- a/tests/robotests/src/com/android/settings/datetime/TimePreferenceControllerTest.java +++ b/tests/robotests/src/com/android/settings/datetime/TimePreferenceControllerTest.java @@ -17,10 +17,11 @@ package com.android.settings.datetime; import android.content.Context; -import android.support.v7.preference.Preference; +import android.os.UserManager; import com.android.settings.testutils.SettingsRobolectricTestRunner; import com.android.settings.TestConfig; +import com.android.settingslib.RestrictedPreference; import org.junit.Before; import org.junit.Test; @@ -31,6 +32,7 @@ import org.robolectric.annotation.Config; import org.robolectric.shadows.ShadowApplication; import static com.google.common.truth.Truth.assertThat; + import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -46,12 +48,13 @@ public class TimePreferenceControllerTest { private AutoTimePreferenceController mAutoTimePreferenceController; private TimePreferenceController mController; - private Preference mPreference; + private RestrictedPreference mPreference; @Before public void setUp() { MockitoAnnotations.initMocks(this); - mPreference = new Preference(ShadowApplication.getInstance().getApplicationContext()); + mPreference = new RestrictedPreference( + ShadowApplication.getInstance().getApplicationContext()); mController = new TimePreferenceController(mContext, mHost, mAutoTimePreferenceController); } @@ -62,6 +65,9 @@ public class TimePreferenceControllerTest { @Test public void updateState_autoTimeEnabled_shouldDisablePref() { + // Make sure not disabled by admin. + mPreference.setDisabledByAdmin(null); + when(mAutoTimePreferenceController.isEnabled()).thenReturn(true); mController.updateState(mPreference); @@ -70,6 +76,9 @@ public class TimePreferenceControllerTest { @Test public void updateState_autoTimeDisabled_shouldEnablePref() { + // Make sure not disabled by admin. + mPreference.setDisabledByAdmin(null); + when(mAutoTimePreferenceController.isEnabled()).thenReturn(false); mController.updateState(mPreference); diff --git a/tests/robotests/src/com/android/settings/datetime/TimeZonePreferenceControllerTest.java b/tests/robotests/src/com/android/settings/datetime/TimeZonePreferenceControllerTest.java index 2f96d65e26d..ec26448f0ca 100644 --- a/tests/robotests/src/com/android/settings/datetime/TimeZonePreferenceControllerTest.java +++ b/tests/robotests/src/com/android/settings/datetime/TimeZonePreferenceControllerTest.java @@ -21,6 +21,7 @@ import android.support.v7.preference.Preference; import com.android.settings.testutils.SettingsRobolectricTestRunner; import com.android.settings.TestConfig; +import com.android.settingslib.RestrictedPreference; import org.junit.Before; import org.junit.Test; @@ -41,15 +42,16 @@ public class TimeZonePreferenceControllerTest { @Mock private AutoTimeZonePreferenceController mAutoTimeZonePreferenceController; + private Context mContext; private TimeZonePreferenceController mController; - private Preference mPreference; + private RestrictedPreference mPreference; @Before public void setUp() { MockitoAnnotations.initMocks(this); mContext = ShadowApplication.getInstance().getApplicationContext(); - mPreference = new Preference(mContext); + mPreference = new RestrictedPreference(mContext); mController = spy(new TimeZonePreferenceController(mContext, mAutoTimeZonePreferenceController)); } @@ -61,6 +63,9 @@ public class TimeZonePreferenceControllerTest { @Test public void updateState_autoTimeZoneEnabled_shouldDisablePref() { + // Make sure not disabled by admin. + mPreference.setDisabledByAdmin(null); + doReturn("test timezone").when(mController).getTimeZoneOffsetAndName(); when(mAutoTimeZonePreferenceController.isEnabled()).thenReturn(true); mController.updateState(mPreference); @@ -70,6 +75,9 @@ public class TimeZonePreferenceControllerTest { @Test public void updateState_autoTimeZoneDisabled_shouldEnablePref() { + // Make sure not disabled by admin. + mPreference.setDisabledByAdmin(null); + doReturn("test timezone").when(mController).getTimeZoneOffsetAndName(); when(mAutoTimeZonePreferenceController.isEnabled()).thenReturn(false); mController.updateState(mPreference); diff --git a/tests/robotests/src/com/android/settings/testutils/shadow/SettingsShadowResources.java b/tests/robotests/src/com/android/settings/testutils/shadow/SettingsShadowResources.java index 183f5ad9c70..b0cd484dd2e 100644 --- a/tests/robotests/src/com/android/settings/testutils/shadow/SettingsShadowResources.java +++ b/tests/robotests/src/com/android/settings/testutils/shadow/SettingsShadowResources.java @@ -101,6 +101,13 @@ public class SettingsShadowResources extends ShadowResources { return directlyOn(realResources, Resources.class).getColorStateList(id, theme); } + /** + * Deprecated because SDK 24+ uses + * {@link SettingsShadowResourcesImpl#loadDrawable(Resources, TypedValue, int, int, Theme)} + * + * TODO: Delete when all tests have been migrated to sdk 26 + */ + @Deprecated @Implementation public Drawable loadDrawable(TypedValue value, int id, Theme theme) throws NotFoundException { diff --git a/tests/robotests/src/com/android/settings/testutils/shadow/SettingsShadowResourcesImpl.java b/tests/robotests/src/com/android/settings/testutils/shadow/SettingsShadowResourcesImpl.java new file mode 100644 index 00000000000..42b02f3c23d --- /dev/null +++ b/tests/robotests/src/com/android/settings/testutils/shadow/SettingsShadowResourcesImpl.java @@ -0,0 +1,57 @@ +/* + * Copyright (C) 2017 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.settings.testutils.shadow; + +import android.content.res.Resources; +import android.content.res.ResourcesImpl; +import android.graphics.drawable.ColorDrawable; +import android.graphics.drawable.Drawable; +import android.util.TypedValue; + +import com.android.settings.R; + +import org.robolectric.annotation.Implementation; +import org.robolectric.annotation.Implements; +import org.robolectric.shadows.ShadowResourcesImpl; + +@Implements( + value = ResourcesImpl.class, + isInAndroidSdk = false, + minSdk = 26 +) +public class SettingsShadowResourcesImpl extends ShadowResourcesImpl { + + @Implementation + public Drawable loadDrawable(Resources wrapper, TypedValue value, int id, int density, + Resources.Theme theme) { + // The drawable item in switchbar_background.xml refers to a very recent color attribute + // that Robolectric isn't yet aware of. + // TODO: Remove this once Robolectric is updated. + if (id == R.drawable.switchbar_background) { + return new ColorDrawable(); + } else if (id == R.drawable.ic_launcher_settings) { + // ic_launcher_settings uses adaptive-icon, which is not supported by robolectric, + // change it to a normal drawable. + id = R.drawable.ic_settings_wireless; + } else if (id == R.drawable.app_filter_spinner_background) { + id = R.drawable.ic_expand_more_inverse; + } else if (id == R.drawable.selectable_card_grey) { + id = R.drawable.ic_expand_more_inverse; + } + return super.loadDrawable(wrapper, value, id, density, theme); + } +} diff --git a/tests/robotests/src/com/android/settings/widget/ActionBarShadowControllerTest.java b/tests/robotests/src/com/android/settings/widget/ActionBarShadowControllerTest.java index 0f3eb31bb4f..f69012e9f7f 100644 --- a/tests/robotests/src/com/android/settings/widget/ActionBarShadowControllerTest.java +++ b/tests/robotests/src/com/android/settings/widget/ActionBarShadowControllerTest.java @@ -45,7 +45,7 @@ import org.robolectric.RuntimeEnvironment; import org.robolectric.annotation.Config; @RunWith(SettingsRobolectricTestRunner.class) -@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) +@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION_O) public class ActionBarShadowControllerTest { @Mock diff --git a/tests/robotests/src/com/android/settings/widget/ActionButtonPreferenceTest.java b/tests/robotests/src/com/android/settings/widget/ActionButtonPreferenceTest.java index a080bc6929a..a2159676c91 100644 --- a/tests/robotests/src/com/android/settings/widget/ActionButtonPreferenceTest.java +++ b/tests/robotests/src/com/android/settings/widget/ActionButtonPreferenceTest.java @@ -40,7 +40,7 @@ import org.robolectric.RuntimeEnvironment; import org.robolectric.annotation.Config; @RunWith(SettingsRobolectricTestRunner.class) -@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) +@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION_O) public class ActionButtonPreferenceTest { private Context mContext; diff --git a/tests/robotests/src/com/android/settings/widget/AppPreferenceTest.java b/tests/robotests/src/com/android/settings/widget/AppPreferenceTest.java index d4890942c6d..6481f8a57f6 100644 --- a/tests/robotests/src/com/android/settings/widget/AppPreferenceTest.java +++ b/tests/robotests/src/com/android/settings/widget/AppPreferenceTest.java @@ -33,7 +33,7 @@ import org.robolectric.RuntimeEnvironment; import org.robolectric.annotation.Config; @RunWith(SettingsRobolectricTestRunner.class) -@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) +@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION_O) public class AppPreferenceTest { private Context mContext; diff --git a/tests/robotests/src/com/android/settings/widget/AppSwitchPreferenceTest.java b/tests/robotests/src/com/android/settings/widget/AppSwitchPreferenceTest.java index a7c8d7cdd90..81537a8c650 100644 --- a/tests/robotests/src/com/android/settings/widget/AppSwitchPreferenceTest.java +++ b/tests/robotests/src/com/android/settings/widget/AppSwitchPreferenceTest.java @@ -33,7 +33,7 @@ import org.robolectric.RuntimeEnvironment; import org.robolectric.annotation.Config; @RunWith(SettingsRobolectricTestRunner.class) -@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) +@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION_O) public class AppSwitchPreferenceTest { private Context mContext; diff --git a/tests/robotests/src/com/android/settings/widget/AspectRatioFrameLayoutTest.java b/tests/robotests/src/com/android/settings/widget/AspectRatioFrameLayoutTest.java index dd6138cea50..4a192584e39 100644 --- a/tests/robotests/src/com/android/settings/widget/AspectRatioFrameLayoutTest.java +++ b/tests/robotests/src/com/android/settings/widget/AspectRatioFrameLayoutTest.java @@ -32,7 +32,7 @@ import org.robolectric.annotation.Config; import org.robolectric.util.ReflectionHelpers; @RunWith(SettingsRobolectricTestRunner.class) -@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) +@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION_O) public class AspectRatioFrameLayoutTest { private Context mContext; diff --git a/tests/robotests/src/com/android/settings/widget/DefaultIndicatorSeekBarTest.java b/tests/robotests/src/com/android/settings/widget/DefaultIndicatorSeekBarTest.java index 7fcf3289c53..47a9aa61de2 100644 --- a/tests/robotests/src/com/android/settings/widget/DefaultIndicatorSeekBarTest.java +++ b/tests/robotests/src/com/android/settings/widget/DefaultIndicatorSeekBarTest.java @@ -29,7 +29,7 @@ import org.robolectric.annotation.Config; import static junit.framework.Assert.assertEquals; @RunWith(SettingsRobolectricTestRunner.class) -@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) +@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION_O) public class DefaultIndicatorSeekBarTest { private DefaultIndicatorSeekBar mDefaultIndicatorSeekBar; diff --git a/tests/robotests/src/com/android/settings/widget/EntityHeaderControllerTest.java b/tests/robotests/src/com/android/settings/widget/EntityHeaderControllerTest.java index 9c6ee457c42..af30e4f443d 100644 --- a/tests/robotests/src/com/android/settings/widget/EntityHeaderControllerTest.java +++ b/tests/robotests/src/com/android/settings/widget/EntityHeaderControllerTest.java @@ -58,7 +58,7 @@ import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @RunWith(SettingsRobolectricTestRunner.class) -@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) +@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION_O) public class EntityHeaderControllerTest { @Mock(answer = Answers.RETURNS_DEEP_STUBS) diff --git a/tests/robotests/src/com/android/settings/widget/FixedLineSummaryPreferenceTest.java b/tests/robotests/src/com/android/settings/widget/FixedLineSummaryPreferenceTest.java index 0d9a77bef58..5022ceb87e8 100644 --- a/tests/robotests/src/com/android/settings/widget/FixedLineSummaryPreferenceTest.java +++ b/tests/robotests/src/com/android/settings/widget/FixedLineSummaryPreferenceTest.java @@ -38,7 +38,7 @@ import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @RunWith(SettingsRobolectricTestRunner.class) -@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) +@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION_O) public class FixedLineSummaryPreferenceTest { @Mock diff --git a/tests/robotests/src/com/android/settings/widget/LoadingViewControllerTest.java b/tests/robotests/src/com/android/settings/widget/LoadingViewControllerTest.java index 09b52c8b83e..6ab2b179729 100644 --- a/tests/robotests/src/com/android/settings/widget/LoadingViewControllerTest.java +++ b/tests/robotests/src/com/android/settings/widget/LoadingViewControllerTest.java @@ -37,7 +37,7 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; @RunWith(SettingsRobolectricTestRunner.class) -@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) +@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION_O) public class LoadingViewControllerTest { private Context mContext; diff --git a/tests/robotests/src/com/android/settings/widget/MasterSwitchPreferenceTest.java b/tests/robotests/src/com/android/settings/widget/MasterSwitchPreferenceTest.java index 1c449cc8183..c80c7b476d1 100644 --- a/tests/robotests/src/com/android/settings/widget/MasterSwitchPreferenceTest.java +++ b/tests/robotests/src/com/android/settings/widget/MasterSwitchPreferenceTest.java @@ -40,7 +40,7 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; @RunWith(SettingsRobolectricTestRunner.class) -@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) +@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION_O) public class MasterSwitchPreferenceTest { private Context mContext; diff --git a/tests/robotests/src/com/android/settings/widget/PreferenceCategoryControllerTest.java b/tests/robotests/src/com/android/settings/widget/PreferenceCategoryControllerTest.java index 23aa378dad1..d8661bf7ea1 100644 --- a/tests/robotests/src/com/android/settings/widget/PreferenceCategoryControllerTest.java +++ b/tests/robotests/src/com/android/settings/widget/PreferenceCategoryControllerTest.java @@ -36,7 +36,7 @@ import java.util.ArrayList; import java.util.List; @RunWith(SettingsRobolectricTestRunner.class) -@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) +@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION_O) public class PreferenceCategoryControllerTest { private Context mContext; diff --git a/tests/robotests/src/com/android/settings/widget/RadioButtonPickerFragmentTest.java b/tests/robotests/src/com/android/settings/widget/RadioButtonPickerFragmentTest.java index 8ca68ae50b7..9fc5b2e7ce5 100644 --- a/tests/robotests/src/com/android/settings/widget/RadioButtonPickerFragmentTest.java +++ b/tests/robotests/src/com/android/settings/widget/RadioButtonPickerFragmentTest.java @@ -45,7 +45,7 @@ import java.util.ArrayList; import java.util.List; @RunWith(SettingsRobolectricTestRunner.class) -@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) +@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION_O) public class RadioButtonPickerFragmentTest { diff --git a/tests/robotests/src/com/android/settings/widget/RingProgressBarTest.java b/tests/robotests/src/com/android/settings/widget/RingProgressBarTest.java index a1a2f2452c5..0226c22eee1 100644 --- a/tests/robotests/src/com/android/settings/widget/RingProgressBarTest.java +++ b/tests/robotests/src/com/android/settings/widget/RingProgressBarTest.java @@ -31,7 +31,7 @@ import org.robolectric.annotation.Config; import static junit.framework.Assert.assertEquals; @RunWith(SettingsRobolectricTestRunner.class) -@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) +@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION_O) public class RingProgressBarTest { private Context mContext = RuntimeEnvironment.application; diff --git a/tests/robotests/src/com/android/settings/widget/RtlCompatibleViewPagerTest.java b/tests/robotests/src/com/android/settings/widget/RtlCompatibleViewPagerTest.java index 78afc43026c..1072152712a 100644 --- a/tests/robotests/src/com/android/settings/widget/RtlCompatibleViewPagerTest.java +++ b/tests/robotests/src/com/android/settings/widget/RtlCompatibleViewPagerTest.java @@ -36,7 +36,7 @@ import static com.google.common.truth.Truth.assertThat; @RunWith(SettingsRobolectricTestRunner.class) @Config( manifest = TestConfig.MANIFEST_PATH, - sdk = TestConfig.SDK_VERSION, + sdk = TestConfig.SDK_VERSION_O, shadows = {ShadowTextUtils.class} ) public class RtlCompatibleViewPagerTest { diff --git a/tests/robotests/src/com/android/settings/widget/ScrollToParentEditTextTest.java b/tests/robotests/src/com/android/settings/widget/ScrollToParentEditTextTest.java index 57550156142..8e912596f22 100644 --- a/tests/robotests/src/com/android/settings/widget/ScrollToParentEditTextTest.java +++ b/tests/robotests/src/com/android/settings/widget/ScrollToParentEditTextTest.java @@ -41,7 +41,7 @@ import org.robolectric.annotation.Config; import org.robolectric.shadows.ShadowView; @RunWith(SettingsRobolectricTestRunner.class) -@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) +@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION_O) public class ScrollToParentEditTextTest { private static final int EDIT_TEXT_SIZE = 20; diff --git a/tests/robotests/src/com/android/settings/widget/SummaryUpdaterTest.java b/tests/robotests/src/com/android/settings/widget/SummaryUpdaterTest.java index 2f42550b6e4..8dbe552d61a 100644 --- a/tests/robotests/src/com/android/settings/widget/SummaryUpdaterTest.java +++ b/tests/robotests/src/com/android/settings/widget/SummaryUpdaterTest.java @@ -34,7 +34,7 @@ import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; @RunWith(SettingsRobolectricTestRunner.class) -@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) +@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION_O) public class SummaryUpdaterTest { private Context mContext; diff --git a/tests/robotests/src/com/android/settings/widget/ValidatedEditTextPreferenceTest.java b/tests/robotests/src/com/android/settings/widget/ValidatedEditTextPreferenceTest.java index 59be160dead..ff2332c07f0 100644 --- a/tests/robotests/src/com/android/settings/widget/ValidatedEditTextPreferenceTest.java +++ b/tests/robotests/src/com/android/settings/widget/ValidatedEditTextPreferenceTest.java @@ -42,7 +42,7 @@ import org.robolectric.RuntimeEnvironment; import org.robolectric.annotation.Config; @RunWith(SettingsRobolectricTestRunner.class) -@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) +@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION_O) public class ValidatedEditTextPreferenceTest { @Mock diff --git a/tests/unit/src/com/android/settings/core/UserRestrictionTest.java b/tests/unit/src/com/android/settings/core/UserRestrictionTest.java new file mode 100644 index 00000000000..ad00bd71e0d --- /dev/null +++ b/tests/unit/src/com/android/settings/core/UserRestrictionTest.java @@ -0,0 +1,151 @@ +/* + * Copyright (C) 2017 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.settings.core; + +import static junit.framework.Assert.fail; + +import android.content.Context; +import android.content.res.Resources; +import android.content.res.TypedArray; +import android.content.res.XmlResourceParser; +import android.os.UserManager; +import android.platform.test.annotations.Presubmit; +import android.provider.SearchIndexableResource; +import android.support.test.InstrumentationRegistry; +import android.support.test.filters.MediumTest; +import android.support.test.runner.AndroidJUnit4; +import android.util.AttributeSet; +import android.util.Log; +import android.util.Xml; + +import com.android.settings.search.DatabaseIndexingUtils; +import com.android.settings.search.Indexable; +import com.android.settings.search.SearchIndexableResources; + +import com.google.android.collect.Sets; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.xmlpull.v1.XmlPullParser; +import org.xmlpull.v1.XmlPullParserException; + +import java.io.IOException; +import java.util.List; +import java.util.Set; + +@RunWith(AndroidJUnit4.class) +@MediumTest +public class UserRestrictionTest { + + private static final String TAG = "UserRestrictionTest"; + + private Context mContext; + + private static final Set USER_RESTRICTIONS = Sets.newHashSet( + UserManager.DISALLOW_CONFIG_DATE_TIME, + UserManager.DISALLOW_CONFIG_CREDENTIALS, + UserManager.DISALLOW_NETWORK_RESET, + UserManager.DISALLOW_FACTORY_RESET, + UserManager.DISALLOW_CONFIG_TETHERING, + UserManager.DISALLOW_CONFIG_VPN, + UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS + ); + + @Before + public void setUp() { + mContext = InstrumentationRegistry.getTargetContext(); + } + + /** + * Verity that userRestriction attributes are entered and parsed successfully. + */ + @Test + public void userRestrictionAttributeShouldBeValid() + throws IOException, XmlPullParserException, Resources.NotFoundException { + for (Class clazz : SearchIndexableResources.providerValues()) { + verifyUserRestriction(clazz); + } + } + + private void verifyUserRestriction(Class clazz) + throws IOException, XmlPullParserException, Resources.NotFoundException { + if (clazz == null) { + return; + } + final String className = clazz.getName(); + final Indexable.SearchIndexProvider provider = + DatabaseIndexingUtils.getSearchIndexProvider(clazz); + final List resourcesToIndex = + provider.getXmlResourcesToIndex(mContext, true); + + if (resourcesToIndex == null) { + Log.d(TAG, className + "is not providing SearchIndexableResource, skipping"); + return; + } + + for (SearchIndexableResource sir : resourcesToIndex) { + if (sir.xmlResId <= 0) { + Log.d(TAG, className + " doesn't have a valid xml to index."); + continue; + } + final XmlResourceParser parser = mContext.getResources().getXml(sir.xmlResId); + + int type; + while ((type = parser.next()) != XmlPullParser.END_DOCUMENT + && type != XmlPullParser.START_TAG) { + // Parse next until start tag is found + } + final int outerDepth = parser.getDepth(); + + do { + if (type != XmlPullParser.START_TAG) { + continue; + } + final String nodeName = parser.getName(); + if (!nodeName.endsWith("Preference")) { + continue; + } + final AttributeSet attrs = Xml.asAttributeSet(parser); + final String userRestriction = getDataUserRestrictions(mContext, attrs); + if (userRestriction != null) { + if(!isValidRestriction(userRestriction)) { + fail("userRestriction in " + className + " not valid."); + } + } + } while ((type = parser.next()) != XmlPullParser.END_DOCUMENT + && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)); + } + } + + boolean isValidRestriction(String userRestriction) { + return USER_RESTRICTIONS.contains(userRestriction); + } + + private String getDataUserRestrictions(Context context, AttributeSet attrs) { + return getData(context, attrs, + com.android.settingslib.R.styleable.RestrictedPreference, + com.android.settingslib.R.styleable.RestrictedPreference_userRestriction); + } + + private String getData(Context context, AttributeSet set, int[] attrs, int resId) { + final TypedArray ta = context.obtainStyledAttributes(set, attrs); + String data = ta.getString(resId); + ta.recycle(); + return data; + } +}