Add setFlag and resetAll in FeatureFlags test mode

Add methods setFlag and resetAll in FeatureFlags in test mode. For the
injection usecase, user will use the interface FeatureFlags in the code
to control the flags.

Add tests for test mode.

Bug: 280833463
Test: Atest AconfigJavaHostTest --host
Change-Id: Ib59ba35a9011a6400af42fc9c283d37193577997
This commit is contained in:
Zhi Dou
2023-08-07 18:09:28 +00:00
parent 5aaeee3749
commit a7200115c5
7 changed files with 161 additions and 5 deletions

View File

@@ -0,0 +1,88 @@
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import com.android.aconfig.test.FakeFeatureFlagsImpl;
import com.android.aconfig.test.FeatureFlags;
import com.android.aconfig.test.FeatureFlagsImpl;
import com.android.aconfig.test.Flags;
@RunWith(JUnit4.class)
public final class AconfigHostTest {
@Test
public void testThrowsExceptionIfFlagNotSet() {
assertThrows(NullPointerException.class, () -> Flags.disabledRo());
FeatureFlags featureFlags = new FakeFeatureFlagsImpl();
assertThrows(IllegalArgumentException.class, () -> featureFlags.disabledRo());
}
@Test
public void testSetFlagInFakeFeatureFlagsImpl() {
FeatureFlags featureFlags = new FakeFeatureFlagsImpl();
featureFlags.setFlag(Flags.FLAG_ENABLED_RW, true);
assertTrue(featureFlags.enabledRw());
featureFlags.setFlag(Flags.FLAG_ENABLED_RW, false);
assertFalse(featureFlags.enabledRw());
//Set Flags
assertThrows(NullPointerException.class, () -> Flags.enabledRw());
Flags.setFeatureFlags(featureFlags);
featureFlags.setFlag(Flags.FLAG_ENABLED_RW, true);
assertTrue(Flags.enabledRw());
Flags.unsetFeatureFlags();
}
@Test
public void testSetFlagWithRandomName() {
FeatureFlags featureFlags = new FakeFeatureFlagsImpl();
assertThrows(IllegalArgumentException.class,
() -> featureFlags.setFlag("Randome_name", true));
}
@Test
public void testResetFlagsInFakeFeatureFlagsImpl() {
FeatureFlags featureFlags = new FakeFeatureFlagsImpl();
featureFlags.setFlag(Flags.FLAG_ENABLED_RO, true);
assertTrue(featureFlags.enabledRo());
featureFlags.resetAll();
assertThrows(IllegalArgumentException.class, () -> featureFlags.enabledRo());
// Set value after reset
featureFlags.setFlag(Flags.FLAG_ENABLED_RO, false);
assertFalse(featureFlags.enabledRo());
}
@Test
public void testFlagsSetFeatureFlags() {
FeatureFlags featureFlags = new FakeFeatureFlagsImpl();
featureFlags.setFlag(Flags.FLAG_ENABLED_RW, true);
assertThrows(NullPointerException.class, () -> Flags.enabledRw());
Flags.setFeatureFlags(featureFlags);
assertTrue(Flags.enabledRw());
Flags.unsetFeatureFlags();
}
@Test
public void testFlagsUnsetFeatureFlags() {
FeatureFlags featureFlags = new FakeFeatureFlagsImpl();
featureFlags.setFlag(Flags.FLAG_ENABLED_RW, true);
assertThrows(NullPointerException.class, () -> Flags.enabledRw());
Flags.setFeatureFlags(featureFlags);
assertTrue(Flags.enabledRw());
Flags.unsetFeatureFlags();
assertThrows(NullPointerException.class, () -> Flags.enabledRw());
}
@Test
public void testFeatureFlagsImplNotImpl() {
FeatureFlags featureFlags = new FeatureFlagsImpl();
assertThrows(UnsupportedOperationException.class,
() -> featureFlags.enabledRw());
}
}