From b6434aae53155a3ffec6510da5f97f8d1f028713 Mon Sep 17 00:00:00 2001 From: Guang Zhu Date: Thu, 29 Jun 2023 15:25:08 -0700 Subject: [PATCH] add more variantions for flag on/off filtering demo ... also fixed missing copyright headers in 2 existing test files ... also made the instrumentation against the demo activity for real Bug: 277819423 Test: atest FlagStateComboTests adb shell device_config put test \ com.example.android.aconfig.demo.flags.awesome_flag_1 adb shell device_config put test \ com.example.android.aconfig.demo.flags.awesome_flag_2 Change-Id: I6083f11ee743a8b4f0f9bc23ac6945b5d072ab5e --- .../AconfigDemo/aconfig_demo_flags.aconfig | 12 ++++ samples/AconfigDemo/tests/Android.bp | 5 +- samples/AconfigDemo/tests/AndroidManifest.xml | 2 +- .../aconfig/demo/FlagStateComboTests.java | 71 +++++++++++++++++++ .../aconfig/demo/InjectedContentTests.java | 19 ++++- .../aconfig/demo/StaticContentTests.java | 19 ++++- 6 files changed, 124 insertions(+), 4 deletions(-) create mode 100644 samples/AconfigDemo/tests/src/com/example/android/aconfig/demo/FlagStateComboTests.java diff --git a/samples/AconfigDemo/aconfig_demo_flags.aconfig b/samples/AconfigDemo/aconfig_demo_flags.aconfig index 6fa2f9c7e..26e726b0b 100644 --- a/samples/AconfigDemo/aconfig_demo_flags.aconfig +++ b/samples/AconfigDemo/aconfig_demo_flags.aconfig @@ -13,6 +13,18 @@ flag { description: "This flag controls static content" } +flag { + name: "awesome_flag_1" + namespace: "test" + description: "A very awesome flag for testing purposes." +} + +flag { + name: "awesome_flag_2" + namespace: "test" + description: "Another awesome flag for testing purposes." +} + flag { name: "third_flag" namespace: "configuration" diff --git a/samples/AconfigDemo/tests/Android.bp b/samples/AconfigDemo/tests/Android.bp index e4bb718f5..9aea52608 100644 --- a/samples/AconfigDemo/tests/Android.bp +++ b/samples/AconfigDemo/tests/Android.bp @@ -11,6 +11,9 @@ android_test { "junit", "mockito-target-minus-junit4", "androidx.test.runner", + "flag-junit", + "platform-test-annotations", ], manifest: "AndroidManifest.xml", -} \ No newline at end of file + instrumentation_for: "AconfigDemoActivity", +} diff --git a/samples/AconfigDemo/tests/AndroidManifest.xml b/samples/AconfigDemo/tests/AndroidManifest.xml index e9168e7d9..6a59f639e 100644 --- a/samples/AconfigDemo/tests/AndroidManifest.xml +++ b/samples/AconfigDemo/tests/AndroidManifest.xml @@ -26,7 +26,7 @@ diff --git a/samples/AconfigDemo/tests/src/com/example/android/aconfig/demo/FlagStateComboTests.java b/samples/AconfigDemo/tests/src/com/example/android/aconfig/demo/FlagStateComboTests.java new file mode 100644 index 000000000..7dfaf26ae --- /dev/null +++ b/samples/AconfigDemo/tests/src/com/example/android/aconfig/demo/FlagStateComboTests.java @@ -0,0 +1,71 @@ +/* + * Copyright (C) 2023 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.example.android.aconfig.demo; + +import android.platform.test.annotations.RequiresFlagsDisabled; +import android.platform.test.annotations.RequiresFlagsEnabled; +import android.platform.test.flag.junit.CheckFlagsRule; +import android.platform.test.flag.junit.DeviceFlagsValueProvider; + +import com.example.android.aconfig.demo.flags.Flags; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** Tests for how combination of flags states effects execution */ +@RunWith(JUnit4.class) +public final class FlagStateComboTests { + @Rule + public final CheckFlagsRule mCheckFlagsRule = DeviceFlagsValueProvider.createCheckFlagsRule(); + + @Test + @RequiresFlagsEnabled(Flags.FLAG_AWESOME_FLAG_1) + public void requires_f1on_pass_or_skip() {} + + @Test + @RequiresFlagsDisabled(Flags.FLAG_AWESOME_FLAG_2) + public void requires_f2off_pass_or_skip() {} + + @Test + @RequiresFlagsEnabled(Flags.FLAG_AWESOME_FLAG_1) + public void requires_f1on_throw_or_skip() { + throw new RuntimeException("This exception is expected if " + + Flags.FLAG_AWESOME_FLAG_1 + " is enabled"); + } + + @Test + @RequiresFlagsDisabled(Flags.FLAG_AWESOME_FLAG_2) + public void requires_f2off_throw_or_skip() { + throw new RuntimeException("This exception is expected if " + + Flags.FLAG_AWESOME_FLAG_2 + " is disabled"); + } + + @Test + @RequiresFlagsEnabled({Flags.FLAG_AWESOME_FLAG_1, Flags.FLAG_AWESOME_FLAG_2}) + public void requires_f1on_f2on_pass_or_skip() {} + + @Test + @RequiresFlagsEnabled(Flags.FLAG_AWESOME_FLAG_1) + @RequiresFlagsDisabled(Flags.FLAG_AWESOME_FLAG_2) + public void requires_f1on_f2off_throw_or_skip() { + throw new RuntimeException("This exception is expected if " + + Flags.FLAG_AWESOME_FLAG_1 + " is enabled and " + + Flags.FLAG_AWESOME_FLAG_2 + " is disabled"); + } +} diff --git a/samples/AconfigDemo/tests/src/com/example/android/aconfig/demo/InjectedContentTests.java b/samples/AconfigDemo/tests/src/com/example/android/aconfig/demo/InjectedContentTests.java index 82351f248..465601398 100644 --- a/samples/AconfigDemo/tests/src/com/example/android/aconfig/demo/InjectedContentTests.java +++ b/samples/AconfigDemo/tests/src/com/example/android/aconfig/demo/InjectedContentTests.java @@ -1,3 +1,19 @@ +/* + * Copyright (C) 2023 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.example.android.aconfig.demo; import static org.junit.Assert.assertEquals; @@ -23,4 +39,5 @@ public final class InjectedContentTests { expected.append("The flag: appendInjectedContent is ON!!\n\n"); assertEquals("Get appendInjectedContent", expected.toString(), injectedContent.getContent()); } -} \ No newline at end of file +} + diff --git a/samples/AconfigDemo/tests/src/com/example/android/aconfig/demo/StaticContentTests.java b/samples/AconfigDemo/tests/src/com/example/android/aconfig/demo/StaticContentTests.java index d8f6765d2..bed62ffc1 100644 --- a/samples/AconfigDemo/tests/src/com/example/android/aconfig/demo/StaticContentTests.java +++ b/samples/AconfigDemo/tests/src/com/example/android/aconfig/demo/StaticContentTests.java @@ -1,3 +1,19 @@ +/* + * Copyright (C) 2023 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.example.android.aconfig.demo; import static org.junit.Assert.assertEquals; @@ -16,4 +32,5 @@ public final class StaticContentTests { public void testFlag() { assertFalse(Flags.appendStaticContent()); } -} \ No newline at end of file +} +