Files
build/tools/aconfig/tests/AconfigHostTest.java
Zhi Dou 06a448fac7 aconfig: generate full fakefeatureflagsimpl in prod mode
This change will generate full fakefeatureflagsimpl in prod mode.
FakeFeatureFlagsImp will be the same in test mode and the prod mode.
FeatureFlagsImpl will be all unimplemented in test mode. setFlag,
resetAll are added into the interface FeatureFlags.

The reason to make this change is for project using injection pattern,
the project doesn't have to use test mode to test the flag guarded code.
The project can directly use the FakeFeatureFlagsImpl for testing.

Bug: 294838180
Test: atest AconfigJavaHostTest --host AND atest aconfig.test.java
Change-Id: Ib6d40fd3a9ef872e01594fd4f8d6c4cb10bb173a
2023-08-28 16:54:15 +00:00

89 lines
3.2 KiB
Java

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());
FakeFeatureFlagsImpl featureFlags = new FakeFeatureFlagsImpl();
assertThrows(IllegalArgumentException.class, () -> featureFlags.disabledRo());
}
@Test
public void testSetFlagInFakeFeatureFlagsImpl() {
FakeFeatureFlagsImpl 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() {
FakeFeatureFlagsImpl featureFlags = new FakeFeatureFlagsImpl();
assertThrows(IllegalArgumentException.class,
() -> featureFlags.setFlag("Randome_name", true));
}
@Test
public void testResetFlagsInFakeFeatureFlagsImpl() {
FakeFeatureFlagsImpl 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() {
FakeFeatureFlagsImpl 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() {
FakeFeatureFlagsImpl 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());
}
}