diff options
author | Jonathan Scott <scottjonathan@google.com> | 2022-06-20 09:35:08 +0000 |
---|---|---|
committer | Android (Google) Code Review <android-gerrit@google.com> | 2022-06-20 09:35:08 +0000 |
commit | 05400540d670d63b52660348dc17e2226f10195c (patch) | |
tree | df9d13dfd77ee7cdf99a16fb4f0bb7630ac9a1a2 | |
parent | 381927b876e8c03d0e6695f99e9b1e4a4e352dc1 (diff) | |
parent | 2733f15e1e0625e306604361a78039a094eda1f2 (diff) | |
download | cts-05400540d670d63b52660348dc17e2226f10195c.tar.gz |
Merge "Ensure not in demo mode when setting global settings." into tm-dev
7 files changed, 229 insertions, 1 deletions
diff --git a/common/device-side/bedstead/harrier/common/src/main/java/com/android/bedstead/harrier/annotations/EnsureDemoMode.java b/common/device-side/bedstead/harrier/common/src/main/java/com/android/bedstead/harrier/annotations/EnsureDemoMode.java new file mode 100644 index 00000000000..22928dc4d60 --- /dev/null +++ b/common/device-side/bedstead/harrier/common/src/main/java/com/android/bedstead/harrier/annotations/EnsureDemoMode.java @@ -0,0 +1,45 @@ +/* + * Copyright (C) 2022 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.bedstead.harrier.annotations; + +import static com.android.bedstead.harrier.annotations.AnnotationRunPrecedence.MIDDLE; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + + +/** + * Ensure that the device is in retail demo mode before running the test. + */ +@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE, ElementType.TYPE}) +@Retention(RetentionPolicy.RUNTIME) +@EnsureGlobalSettingSet(key = "device_demo_mode", value = "1") +public @interface EnsureDemoMode { + /** + * Weight sets the order that annotations will be resolved. + * + * <p>Annotations with a lower weight will be resolved before annotations with a higher weight. + * + * <p>If there is an order requirement between annotations, ensure that the weight of the + * annotation which must be resolved first is lower than the one which must be resolved later. + * + * <p>Weight can be set to a {@link AnnotationRunPrecedence} constant, or to any {@link int}. + */ + int weight() default MIDDLE; +} diff --git a/common/device-side/bedstead/harrier/common/src/main/java/com/android/bedstead/harrier/annotations/EnsureGlobalSettingSet.java b/common/device-side/bedstead/harrier/common/src/main/java/com/android/bedstead/harrier/annotations/EnsureGlobalSettingSet.java new file mode 100644 index 00000000000..6bed7c43a02 --- /dev/null +++ b/common/device-side/bedstead/harrier/common/src/main/java/com/android/bedstead/harrier/annotations/EnsureGlobalSettingSet.java @@ -0,0 +1,46 @@ +/* + * Copyright (C) 2022 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.bedstead.harrier.annotations; + +import static com.android.bedstead.harrier.annotations.AnnotationRunPrecedence.MIDDLE; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Repeatable; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE, ElementType.TYPE}) +@Retention(RetentionPolicy.RUNTIME) +@Repeatable(EnsureGlobalSettingSetGroup.class) +public @interface EnsureGlobalSettingSet { + String key(); + + String value(); + + /** + * Weight sets the order that annotations will be resolved. + * + * <p>Annotations with a lower weight will be resolved before annotations with a higher weight. + * + * <p>If there is an order requirement between annotations, ensure that the weight of the + * annotation which must be resolved first is lower than the one which must be resolved later. + * + * <p>Weight can be set to a {@link AnnotationRunPrecedence} constant, or to any {@link int}. + */ + int weight() default MIDDLE; +} diff --git a/common/device-side/bedstead/harrier/common/src/main/java/com/android/bedstead/harrier/annotations/EnsureGlobalSettingSetGroup.java b/common/device-side/bedstead/harrier/common/src/main/java/com/android/bedstead/harrier/annotations/EnsureGlobalSettingSetGroup.java new file mode 100644 index 00000000000..08f90e9bd7f --- /dev/null +++ b/common/device-side/bedstead/harrier/common/src/main/java/com/android/bedstead/harrier/annotations/EnsureGlobalSettingSetGroup.java @@ -0,0 +1,45 @@ +/* + * Copyright (C) 2021 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.bedstead.harrier.annotations; + +import static com.android.bedstead.harrier.annotations.AnnotationRunPrecedence.MIDDLE; + +import com.android.bedstead.harrier.annotations.meta.RepeatingAnnotation; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE, ElementType.TYPE}) +@Retention(RetentionPolicy.RUNTIME) +@RepeatingAnnotation +public @interface EnsureGlobalSettingSetGroup { + EnsureGlobalSettingSet[] value(); + + /** + * Weight sets the order that annotations will be resolved. + * + * <p>Annotations with a lower weight will be resolved before annotations with a higher weight. + * + * <p>If there is an order requirement between annotations, ensure that the weight of the + * annotation which must be resolved first is lower than the one which must be resolved later. + * + * <p>Weight can be set to a {@link AnnotationRunPrecedence} constant, or to any {@link int}. + */ + int weight() default MIDDLE; +} diff --git a/common/device-side/bedstead/harrier/common/src/main/java/com/android/bedstead/harrier/annotations/EnsureNotDemoMode.java b/common/device-side/bedstead/harrier/common/src/main/java/com/android/bedstead/harrier/annotations/EnsureNotDemoMode.java new file mode 100644 index 00000000000..5010873e903 --- /dev/null +++ b/common/device-side/bedstead/harrier/common/src/main/java/com/android/bedstead/harrier/annotations/EnsureNotDemoMode.java @@ -0,0 +1,45 @@ +/* + * Copyright (C) 2022 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.bedstead.harrier.annotations; + +import static com.android.bedstead.harrier.annotations.AnnotationRunPrecedence.MIDDLE; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + + +/** + * Ensure that the device is not in retail demo mode before running the test. + */ +@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE, ElementType.TYPE}) +@Retention(RetentionPolicy.RUNTIME) +@EnsureGlobalSettingSet(key = "device_demo_mode", value = "0") +public @interface EnsureNotDemoMode { + /** + * Weight sets the order that annotations will be resolved. + * + * <p>Annotations with a lower weight will be resolved before annotations with a higher weight. + * + * <p>If there is an order requirement between annotations, ensure that the weight of the + * annotation which must be resolved first is lower than the one which must be resolved later. + * + * <p>Weight can be set to a {@link AnnotationRunPrecedence} constant, or to any {@link int}. + */ + int weight() default MIDDLE; +} diff --git a/common/device-side/bedstead/harrier/src/main/java/com/android/bedstead/harrier/DeviceState.java b/common/device-side/bedstead/harrier/src/main/java/com/android/bedstead/harrier/DeviceState.java index bf67e587bce..b28b815a9de 100644 --- a/common/device-side/bedstead/harrier/src/main/java/com/android/bedstead/harrier/DeviceState.java +++ b/common/device-side/bedstead/harrier/src/main/java/com/android/bedstead/harrier/DeviceState.java @@ -50,6 +50,7 @@ import com.android.bedstead.harrier.annotations.EnsureBluetoothEnabled; import com.android.bedstead.harrier.annotations.EnsureCanGetPermission; import com.android.bedstead.harrier.annotations.EnsureDoesNotHaveAppOp; import com.android.bedstead.harrier.annotations.EnsureDoesNotHavePermission; +import com.android.bedstead.harrier.annotations.EnsureGlobalSettingSet; import com.android.bedstead.harrier.annotations.EnsureHasAppOp; import com.android.bedstead.harrier.annotations.EnsureHasPermission; import com.android.bedstead.harrier.annotations.EnsurePackageNotInstalled; @@ -778,6 +779,14 @@ public final class DeviceState extends HarrierRule { ensureBluetoothDisabled(); continue; } + + if (annotation instanceof EnsureGlobalSettingSet) { + EnsureGlobalSettingSet ensureGlobalSettingSetAnnotation = + (EnsureGlobalSettingSet) annotation; + ensureGlobalSettingSet( + ensureGlobalSettingSetAnnotation.key(), + ensureGlobalSettingSetAnnotation.value()); + } } requireSdkVersion(/* min= */ mMinSdkVersionCurrentTest, @@ -1152,7 +1161,7 @@ public final class DeviceState extends HarrierRule { private Boolean mOriginalBluetoothEnabled; private TestAppProvider mTestAppProvider = new TestAppProvider(); private Map<String, TestAppInstance> mTestApps = new HashMap<>(); - + private final Map<String, String> mOriginalGlobalSettings = new HashMap<>(); private static final class RemovedUser { // Store the user builder so we can recreate the user later @@ -1759,6 +1768,11 @@ public final class DeviceState extends HarrierRule { TestApis.bluetooth().setEnabled(mOriginalBluetoothEnabled); mOriginalBluetoothEnabled = null; } + + for (Map.Entry<String, String> s : mOriginalGlobalSettings.entrySet()) { + TestApis.settings().global().putString(s.getKey(), s.getValue()); + } + mOriginalGlobalSettings.clear(); } private UserReference createProfile( @@ -2507,4 +2521,11 @@ public final class DeviceState extends HarrierRule { mPermissionContext = mPermissionContext.withoutPermission(permission); } } + + private void ensureGlobalSettingSet(String key, String value) { + if (!mOriginalGlobalSettings.containsKey(key)) { + mOriginalGlobalSettings.put(key, TestApis.settings().global().getString(value)); + } + TestApis.settings().global().putString(key, value); + } } diff --git a/common/device-side/bedstead/harrier/src/test/java/com/android/bedstead/harrier/DeviceStateTest.java b/common/device-side/bedstead/harrier/src/test/java/com/android/bedstead/harrier/DeviceStateTest.java index a5ec1092c81..8234786e09c 100644 --- a/common/device-side/bedstead/harrier/src/test/java/com/android/bedstead/harrier/DeviceStateTest.java +++ b/common/device-side/bedstead/harrier/src/test/java/com/android/bedstead/harrier/DeviceStateTest.java @@ -53,8 +53,10 @@ import android.platform.test.annotations.AppModeFull; import com.android.bedstead.harrier.annotations.EnsureBluetoothDisabled; import com.android.bedstead.harrier.annotations.EnsureBluetoothEnabled; +import com.android.bedstead.harrier.annotations.EnsureDemoMode; import com.android.bedstead.harrier.annotations.EnsureDoesNotHaveAppOp; import com.android.bedstead.harrier.annotations.EnsureDoesNotHavePermission; +import com.android.bedstead.harrier.annotations.EnsureGlobalSettingSet; import com.android.bedstead.harrier.annotations.EnsureHasAppOp; import com.android.bedstead.harrier.annotations.EnsureHasNoSecondaryUser; import com.android.bedstead.harrier.annotations.EnsureHasNoTvProfile; @@ -63,6 +65,7 @@ import com.android.bedstead.harrier.annotations.EnsureHasPermission; import com.android.bedstead.harrier.annotations.EnsureHasSecondaryUser; import com.android.bedstead.harrier.annotations.EnsureHasTvProfile; import com.android.bedstead.harrier.annotations.EnsureHasWorkProfile; +import com.android.bedstead.harrier.annotations.EnsureNotDemoMode; import com.android.bedstead.harrier.annotations.EnsurePackageNotInstalled; import com.android.bedstead.harrier.annotations.EnsurePasswordNotSet; import com.android.bedstead.harrier.annotations.EnsureScreenIsOn; @@ -974,4 +977,25 @@ public class DeviceStateTest { assertThat(sDeviceState.testApp() .testApp().pkg().appOps().get(OPSTR_START_FOREGROUND)).isEqualTo(ALLOWED); } + + @EnsureGlobalSettingSet(key = "testGlobalSetting", value = "testValue") + @Test + public void ensureGlobalSettingSetAnnotation_globalSettingIsSet() { + assertThat(TestApis.settings().global().getString("testGlobalSetting")) + .isEqualTo("testValue"); + } + + @EnsureDemoMode + @Test + public void ensureDemoModeAnnotation_deviceIsInDemoMode() { + assertThat(TestApis.settings().global().getInt("device_demo_mode")) + .isEqualTo(1); + } + + @EnsureNotDemoMode + @Test + public void ensureNotDemoModeAnnotation_deviceIsNotInDemoMode() { + assertThat(TestApis.settings().global().getInt("device_demo_mode")) + .isEqualTo(0); + } } diff --git a/tests/devicepolicy/src/android/devicepolicy/cts/SettingsTest.java b/tests/devicepolicy/src/android/devicepolicy/cts/SettingsTest.java index a0d37e81c53..fa1a68f3d43 100644 --- a/tests/devicepolicy/src/android/devicepolicy/cts/SettingsTest.java +++ b/tests/devicepolicy/src/android/devicepolicy/cts/SettingsTest.java @@ -28,6 +28,7 @@ import static org.testng.Assert.assertThrows; import com.android.bedstead.harrier.BedsteadJUnit4; import com.android.bedstead.harrier.DeviceState; +import com.android.bedstead.harrier.annotations.EnsureNotDemoMode; import com.android.bedstead.harrier.annotations.Postsubmit; import com.android.bedstead.harrier.annotations.enterprise.CanSetPolicyTest; import com.android.bedstead.harrier.annotations.enterprise.CannotSetPolicyTest; @@ -85,6 +86,7 @@ public final class SettingsTest { @CanSetPolicyTest(policy = SetGlobalSetting.class) @Postsubmit(reason = "new test") + @EnsureNotDemoMode // retail demo mode bypasses global setting allowlist public void setGlobalSetting_unsupported_throwsSecurityException() { assertThrows(SecurityException.class, () -> sDeviceState.dpc().devicePolicyManager().setGlobalSetting( |