Zhi Dou | a720011 | 2023-08-07 18:09:28 +0000 | [diff] [blame] | 1 | import static org.junit.Assert.assertFalse; |
| 2 | import static org.junit.Assert.assertThrows; |
| 3 | import static org.junit.Assert.assertTrue; |
| 4 | |
| 5 | import org.junit.Test; |
| 6 | import org.junit.runner.RunWith; |
| 7 | import org.junit.runners.JUnit4; |
| 8 | |
| 9 | |
| 10 | import com.android.aconfig.test.FakeFeatureFlagsImpl; |
| 11 | import com.android.aconfig.test.FeatureFlags; |
| 12 | import com.android.aconfig.test.FeatureFlagsImpl; |
| 13 | import com.android.aconfig.test.Flags; |
| 14 | |
| 15 | @RunWith(JUnit4.class) |
| 16 | public final class AconfigHostTest { |
| 17 | @Test |
| 18 | public void testThrowsExceptionIfFlagNotSet() { |
| 19 | assertThrows(NullPointerException.class, () -> Flags.disabledRo()); |
Zhi Dou | 06a448f | 2023-08-15 19:33:27 +0000 | [diff] [blame^] | 20 | FakeFeatureFlagsImpl featureFlags = new FakeFeatureFlagsImpl(); |
Zhi Dou | a720011 | 2023-08-07 18:09:28 +0000 | [diff] [blame] | 21 | assertThrows(IllegalArgumentException.class, () -> featureFlags.disabledRo()); |
| 22 | } |
| 23 | |
| 24 | @Test |
| 25 | public void testSetFlagInFakeFeatureFlagsImpl() { |
Zhi Dou | 06a448f | 2023-08-15 19:33:27 +0000 | [diff] [blame^] | 26 | FakeFeatureFlagsImpl featureFlags = new FakeFeatureFlagsImpl(); |
Zhi Dou | a720011 | 2023-08-07 18:09:28 +0000 | [diff] [blame] | 27 | featureFlags.setFlag(Flags.FLAG_ENABLED_RW, true); |
| 28 | assertTrue(featureFlags.enabledRw()); |
| 29 | featureFlags.setFlag(Flags.FLAG_ENABLED_RW, false); |
| 30 | assertFalse(featureFlags.enabledRw()); |
| 31 | |
| 32 | //Set Flags |
| 33 | assertThrows(NullPointerException.class, () -> Flags.enabledRw()); |
| 34 | Flags.setFeatureFlags(featureFlags); |
| 35 | featureFlags.setFlag(Flags.FLAG_ENABLED_RW, true); |
| 36 | assertTrue(Flags.enabledRw()); |
| 37 | Flags.unsetFeatureFlags(); |
| 38 | } |
| 39 | |
| 40 | @Test |
| 41 | public void testSetFlagWithRandomName() { |
Zhi Dou | 06a448f | 2023-08-15 19:33:27 +0000 | [diff] [blame^] | 42 | FakeFeatureFlagsImpl featureFlags = new FakeFeatureFlagsImpl(); |
Zhi Dou | a720011 | 2023-08-07 18:09:28 +0000 | [diff] [blame] | 43 | assertThrows(IllegalArgumentException.class, |
| 44 | () -> featureFlags.setFlag("Randome_name", true)); |
| 45 | } |
| 46 | |
| 47 | @Test |
| 48 | public void testResetFlagsInFakeFeatureFlagsImpl() { |
Zhi Dou | 06a448f | 2023-08-15 19:33:27 +0000 | [diff] [blame^] | 49 | FakeFeatureFlagsImpl featureFlags = new FakeFeatureFlagsImpl(); |
Zhi Dou | a720011 | 2023-08-07 18:09:28 +0000 | [diff] [blame] | 50 | featureFlags.setFlag(Flags.FLAG_ENABLED_RO, true); |
| 51 | assertTrue(featureFlags.enabledRo()); |
| 52 | featureFlags.resetAll(); |
| 53 | assertThrows(IllegalArgumentException.class, () -> featureFlags.enabledRo()); |
| 54 | |
| 55 | // Set value after reset |
| 56 | featureFlags.setFlag(Flags.FLAG_ENABLED_RO, false); |
| 57 | assertFalse(featureFlags.enabledRo()); |
| 58 | } |
| 59 | |
| 60 | @Test |
| 61 | public void testFlagsSetFeatureFlags() { |
Zhi Dou | 06a448f | 2023-08-15 19:33:27 +0000 | [diff] [blame^] | 62 | FakeFeatureFlagsImpl featureFlags = new FakeFeatureFlagsImpl(); |
Zhi Dou | a720011 | 2023-08-07 18:09:28 +0000 | [diff] [blame] | 63 | featureFlags.setFlag(Flags.FLAG_ENABLED_RW, true); |
| 64 | assertThrows(NullPointerException.class, () -> Flags.enabledRw()); |
| 65 | Flags.setFeatureFlags(featureFlags); |
| 66 | assertTrue(Flags.enabledRw()); |
| 67 | Flags.unsetFeatureFlags(); |
| 68 | } |
| 69 | |
| 70 | @Test |
| 71 | public void testFlagsUnsetFeatureFlags() { |
Zhi Dou | 06a448f | 2023-08-15 19:33:27 +0000 | [diff] [blame^] | 72 | FakeFeatureFlagsImpl featureFlags = new FakeFeatureFlagsImpl(); |
Zhi Dou | a720011 | 2023-08-07 18:09:28 +0000 | [diff] [blame] | 73 | featureFlags.setFlag(Flags.FLAG_ENABLED_RW, true); |
| 74 | assertThrows(NullPointerException.class, () -> Flags.enabledRw()); |
| 75 | Flags.setFeatureFlags(featureFlags); |
| 76 | assertTrue(Flags.enabledRw()); |
| 77 | |
| 78 | Flags.unsetFeatureFlags(); |
| 79 | assertThrows(NullPointerException.class, () -> Flags.enabledRw()); |
| 80 | } |
| 81 | |
| 82 | @Test |
| 83 | public void testFeatureFlagsImplNotImpl() { |
| 84 | FeatureFlags featureFlags = new FeatureFlagsImpl(); |
| 85 | assertThrows(UnsupportedOperationException.class, |
| 86 | () -> featureFlags.enabledRw()); |
| 87 | } |
| 88 | } |