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

@@ -101,7 +101,7 @@ java_aconfig_library {
android_test {
name: "aconfig.test.java",
srcs: [
"tests/**/*.java",
"tests/AconfigTest.java",
],
manifest: "tests/AndroidManifest.xml",
certificate: "platform",
@@ -113,6 +113,25 @@ android_test {
test_suites: ["device-tests"],
}
java_aconfig_library {
name: "aconfig_host_test_java_library",
aconfig_declarations: "aconfig.test.flags",
host_supported: true,
test: true,
}
java_test_host {
name: "AconfigJavaHostTest",
srcs: [
"tests/AconfigHostTest.java",
],
static_libs: [
"aconfig_host_test_java_library",
"junit",
],
test_suites: ["general-tests"],
}
// integration tests: C++
cc_aconfig_library {

View File

@@ -116,14 +116,14 @@ mod tests {
use super::*;
use std::collections::HashMap;
const EXPECTED_FEATUREFLAGS_CONTENT: &str = r#"
const EXPECTED_FEATUREFLAGS_COMMON_CONTENT: &str = r#"
package com.android.aconfig.test;
public interface FeatureFlags {
boolean disabledRo();
boolean disabledRw();
boolean enabledRo();
boolean enabledRw();
}"#;
"#;
const EXPECTED_FLAG_COMMON_CONTENT: &str = r#"
package com.android.aconfig.test;
@@ -179,6 +179,9 @@ mod tests {
CodegenMode::Production,
)
.unwrap();
let expect_featureflags_content = EXPECTED_FEATUREFLAGS_COMMON_CONTENT.to_string()
+ r#"
}"#;
let expect_flags_content = EXPECTED_FLAG_COMMON_CONTENT.to_string()
+ r#"
private static FeatureFlags FEATURE_FLAGS = new FeatureFlagsImpl();
@@ -224,7 +227,7 @@ mod tests {
let mut file_set = HashMap::from([
("com/android/aconfig/test/Flags.java", expect_flags_content.as_str()),
("com/android/aconfig/test/FeatureFlagsImpl.java", expect_featureflagsimpl_content),
("com/android/aconfig/test/FeatureFlags.java", EXPECTED_FEATUREFLAGS_CONTENT),
("com/android/aconfig/test/FeatureFlags.java", expect_featureflags_content.as_str()),
(
"com/android/aconfig/test/FakeFeatureFlagsImpl.java",
expect_fakefeatureflagsimpl_content.as_str(),
@@ -258,6 +261,11 @@ mod tests {
CodegenMode::Test,
)
.unwrap();
let expect_featureflags_content = EXPECTED_FEATUREFLAGS_COMMON_CONTENT.to_string()
+ r#"
public void setFlag(String flagName, boolean value);
public void resetAll();
}"#;
let expect_flags_content = EXPECTED_FLAG_COMMON_CONTENT.to_string()
+ r#"
public static void setFeatureFlags(FeatureFlags featureFlags) {
@@ -275,6 +283,16 @@ mod tests {
.to_owned()
+ EXPECTED_METHOD_NOT_IMPL_COMMON_CONTENT
+ r#"
@Override
public void setFlag(String flagName, boolean value) {
throw new UnsupportedOperationException(
"Method is not implemented.");
}
@Override
public void resetAll() {
throw new UnsupportedOperationException(
"Method is not implemented.");
}
}
"#;
let expect_fakefeatureflagsimpl_content = r#"
@@ -300,12 +318,14 @@ mod tests {
public boolean enabledRw() {
return getFlag(Flags.FLAG_ENABLED_RW);
}
@Override
public void setFlag(String flagName, boolean value) {
if (!this.mFlagMap.containsKey(flagName)) {
throw new IllegalArgumentException("no such flag" + flagName);
}
this.mFlagMap.put(flagName, value);
}
@Override
public void resetAll() {
for (Map.Entry entry : mFlagMap.entrySet()) {
entry.setValue(null);
@@ -334,7 +354,7 @@ mod tests {
let mut file_set = HashMap::from([
("com/android/aconfig/test/Flags.java", expect_flags_content.as_str()),
("com/android/aconfig/test/FeatureFlags.java", EXPECTED_FEATUREFLAGS_CONTENT),
("com/android/aconfig/test/FeatureFlags.java", expect_featureflags_content.as_str()),
(
"com/android/aconfig/test/FeatureFlagsImpl.java",
expect_featureflagsimpl_content.as_str(),

View File

@@ -13,6 +13,7 @@ public class FakeFeatureFlagsImpl implements FeatureFlags \{
return getFlag(Flags.FLAG_{item.flag_name_constant_suffix});
}
{{ endfor}}
@Override
public void setFlag(String flagName, boolean value) \{
if (!this.mFlagMap.containsKey(flagName)) \{
throw new IllegalArgumentException("no such flag" + flagName);
@@ -20,6 +21,7 @@ public class FakeFeatureFlagsImpl implements FeatureFlags \{
this.mFlagMap.put(flagName, value);
}
@Override
public void resetAll() \{
for (Map.Entry entry : mFlagMap.entrySet()) \{
entry.setValue(null);

View File

@@ -4,4 +4,10 @@ public interface FeatureFlags \{
{{ for item in class_elements}}
boolean {item.method_name}();
{{ endfor }}
{{ -if is_test_mode }}
public void setFlag(String flagName, boolean value);
public void resetAll();
{{ -endif }}
}

View File

@@ -29,5 +29,16 @@ public final class FeatureFlagsImpl implements FeatureFlags \{
"Method is not implemented.");
}
{{ endfor- }}
@Override
public void setFlag(String flagName, boolean value) \{
throw new UnsupportedOperationException(
"Method is not implemented.");
}
@Override
public void resetAll() \{
throw new UnsupportedOperationException(
"Method is not implemented.");
}
}
{{ endif }}

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());
}
}

View File

@@ -8,12 +8,16 @@ import static com.android.aconfig.test.Flags.enabledRo;
import static com.android.aconfig.test.Flags.enabledRw;
import static org.junit.Assert.assertEquals;
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;
@RunWith(JUnit4.class)
public final class AconfigTest {
@Test
@@ -43,4 +47,10 @@ public final class AconfigTest {
// (currently all flags are assigned the default READ_ONLY + DISABLED)
assertFalse(enabledRw());
}
@Test
public void testFakeFeatureFlagsImplNotImpl() {
FeatureFlags featureFlags = new FakeFeatureFlagsImpl();
assertThrows(UnsupportedOperationException.class, () -> featureFlags.enabledRw());
}
}