diff --git a/tools/aconfig/src/codegen_java.rs b/tools/aconfig/src/codegen_java.rs index 8ab6ffa8f7..a11737034c 100644 --- a/tools/aconfig/src/codegen_java.rs +++ b/tools/aconfig/src/codegen_java.rs @@ -47,9 +47,13 @@ where "FeatureFlags.java", include_str!("../templates/FeatureFlags.java.template"), )?; + template.add_template( + "FakeFeatureFlagsImpl.java", + include_str!("../templates/FakeFeatureFlagsImpl.java.template"), + )?; let path: PathBuf = package.split('.').collect(); - ["Flags.java", "FeatureFlagsImpl.java", "FeatureFlags.java"] + ["Flags.java", "FeatureFlags.java", "FeatureFlagsImpl.java", "FakeFeatureFlagsImpl.java"] .iter() .map(|file| { Ok(OutputFile { @@ -143,6 +147,29 @@ mod tests { } "#; + const EXPECTED_METHOD_NOT_IMPL_COMMON_CONTENT: &str = r#" + @Override + public boolean disabledRo() { + throw new UnsupportedOperationException( + "Method is not implemented."); + } + @Override + public boolean disabledRw() { + throw new UnsupportedOperationException( + "Method is not implemented."); + } + @Override + public boolean enabledRo() { + throw new UnsupportedOperationException( + "Method is not implemented."); + } + @Override + public boolean enabledRw() { + throw new UnsupportedOperationException( + "Method is not implemented."); + } + "#; + #[test] fn test_generate_java_code_production() { let parsed_flags = crate::test::parse_test_flags(); @@ -156,7 +183,15 @@ mod tests { + r#" private static FeatureFlags FEATURE_FLAGS = new FeatureFlagsImpl(); }"#; - let expected_featureflagsimpl_content = r#" + let expect_fakefeatureflagsimpl_content = r#" + package com.android.aconfig.test; + public class FakeFeatureFlagsImpl implements FeatureFlags {"# + .to_owned() + + EXPECTED_METHOD_NOT_IMPL_COMMON_CONTENT + + r#" + } + "#; + let expect_featureflagsimpl_content = r#" package com.android.aconfig.test; import android.provider.DeviceConfig; public final class FeatureFlagsImpl implements FeatureFlags { @@ -188,8 +223,12 @@ 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", expected_featureflagsimpl_content), + ("com/android/aconfig/test/FeatureFlagsImpl.java", expect_featureflagsimpl_content), ("com/android/aconfig/test/FeatureFlags.java", EXPECTED_FEATUREFLAGS_CONTENT), + ( + "com/android/aconfig/test/FakeFeatureFlagsImpl.java", + expect_fakefeatureflagsimpl_content.as_str(), + ), ]); for file in generated_files { @@ -221,22 +260,30 @@ mod tests { .unwrap(); let expect_flags_content = EXPECTED_FLAG_COMMON_CONTENT.to_string() + r#" - public static void setFeatureFlagsImpl(FeatureFlags featureFlags) { + public static void setFeatureFlags(FeatureFlags featureFlags) { Flags.FEATURE_FLAGS = featureFlags; } - public static void unsetFeatureFlagsImpl() { + public static void unsetFeatureFlags() { Flags.FEATURE_FLAGS = null; } private static FeatureFlags FEATURE_FLAGS; } "#; - let expected_featureflagsimpl_content = r#" + let expect_featureflagsimpl_content = r#" + package com.android.aconfig.test; + public final class FeatureFlagsImpl implements FeatureFlags {"# + .to_owned() + + EXPECTED_METHOD_NOT_IMPL_COMMON_CONTENT + + r#" + } + "#; + let expect_fakefeatureflagsimpl_content = r#" package com.android.aconfig.test; import static java.util.stream.Collectors.toMap; import java.util.HashMap; import java.util.Map; import java.util.stream.Stream; - public final class FeatureFlagsImpl implements FeatureFlags { + public class FakeFeatureFlagsImpl implements FeatureFlags { @Override public boolean disabledRo() { return getFlag(Flags.FLAG_DISABLED_RO); @@ -284,10 +331,18 @@ 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", expected_featureflagsimpl_content), ("com/android/aconfig/test/FeatureFlags.java", EXPECTED_FEATUREFLAGS_CONTENT), + ( + "com/android/aconfig/test/FeatureFlagsImpl.java", + expect_featureflagsimpl_content.as_str(), + ), + ( + "com/android/aconfig/test/FakeFeatureFlagsImpl.java", + expect_fakefeatureflagsimpl_content, + ), ]); for file in generated_files { diff --git a/tools/aconfig/templates/FakeFeatureFlagsImpl.java.template b/tools/aconfig/templates/FakeFeatureFlagsImpl.java.template new file mode 100644 index 0000000000..db35d28f68 --- /dev/null +++ b/tools/aconfig/templates/FakeFeatureFlagsImpl.java.template @@ -0,0 +1,59 @@ +package {package_name}; +{{ if is_test_mode }} +import static java.util.stream.Collectors.toMap; + +import java.util.HashMap; +import java.util.Map; +import java.util.stream.Stream; + +public class FakeFeatureFlagsImpl implements FeatureFlags \{ +{{ for item in class_elements}} + @Override + public boolean {item.method_name}() \{ + return getFlag(Flags.FLAG_{item.flag_name_constant_suffix}); + } +{{ endfor}} + public void setFlag(String flagName, boolean value) \{ + if (!this.mFlagMap.containsKey(flagName)) \{ + throw new IllegalArgumentException("no such flag" + flagName); + } + this.mFlagMap.put(flagName, value); + } + + public void resetAll() \{ + for (Map.Entry entry : mFlagMap.entrySet()) \{ + entry.setValue(null); + } + } + + private boolean getFlag(String flagName) \{ + Boolean value = this.mFlagMap.get(flagName); + if (value == null) \{ + throw new IllegalArgumentException(flagName + " is not set"); + } + return value; + } + + private HashMap mFlagMap = Stream.of( + {{-for item in class_elements}} + Flags.FLAG_{item.flag_name_constant_suffix}{{ if not @last }},{{ endif }} + {{ -endfor }} + ) + .collect( + HashMap::new, + (map, elem) -> map.put(elem, null), + HashMap::putAll + ); +} +{{ else }} +{#- Generate only stub if in prod mode #} +public class FakeFeatureFlagsImpl implements FeatureFlags \{ +{{ for item in class_elements}} + @Override + public boolean {item.method_name}() \{ + throw new UnsupportedOperationException( + "Method is not implemented."); + } +{{ endfor}} +} +{{ endif }} diff --git a/tools/aconfig/templates/FeatureFlagsImpl.java.template b/tools/aconfig/templates/FeatureFlagsImpl.java.template index 082d476de5..ba86ce552c 100644 --- a/tools/aconfig/templates/FeatureFlagsImpl.java.template +++ b/tools/aconfig/templates/FeatureFlagsImpl.java.template @@ -1,65 +1,33 @@ package {package_name}; -{{ -if is_test_mode }} -import static java.util.stream.Collectors.toMap; - -import java.util.HashMap; -import java.util.Map; -import java.util.stream.Stream; -{{ else}} +{{ if not is_test_mode }} {{ if is_read_write- }} import android.provider.DeviceConfig; -{{ -endif- }} {{ endif }} public final class FeatureFlagsImpl implements FeatureFlags \{ {{ for item in class_elements}} @Override public boolean {item.method_name}() \{ - {{ -if not is_test_mode- }} - {{ if item.is_read_write }} + {{ -if item.is_read_write }} return DeviceConfig.getBoolean( "{item.device_config_namespace}", "{item.device_config_flag}", {item.default_value} ); - {{ -else }} + {{ else }} return {item.default_value}; - {{ -endif- }} - {{ else }} - return getFlag(Flags.FLAG_{item.flag_name_constant_suffix}); - {{ -endif }} + {{ endif- }} } {{ endfor- }} -{{ if is_test_mode }} - public void setFlag(String flagName, boolean value) \{ - if (!this.mFlagMap.containsKey(flagName)) \{ - throw new IllegalArgumentException("no such flag" + flagName); - } - this.mFlagMap.put(flagName, value); - } - - public void resetAll() \{ - for (Map.Entry entry : mFlagMap.entrySet()) \{ - entry.setValue(null); - } - } - - private boolean getFlag(String flagName) \{ - Boolean value = this.mFlagMap.get(flagName); - if (value == null) \{ - throw new IllegalArgumentException(flagName + " is not set"); - } - return value; - } - - private HashMap mFlagMap = Stream.of( - {{-for item in class_elements}} - Flags.FLAG_{item.flag_name_constant_suffix}{{ if not @last }},{{ endif }} - {{ -endfor }} - ) - .collect( - HashMap::new, - (map, elem) -> map.put(elem, null), - HashMap::putAll - ); -{{ -endif }} } +{{ else }} +{#- Generate only stub if in test mode #} +public final class FeatureFlagsImpl implements FeatureFlags \{ +{{ for item in class_elements}} + @Override + public boolean {item.method_name}() \{ + throw new UnsupportedOperationException( + "Method is not implemented."); + } +{{ endfor- }} +} +{{ endif }} diff --git a/tools/aconfig/templates/Flags.java.template b/tools/aconfig/templates/Flags.java.template index c244b15156..012eba6660 100644 --- a/tools/aconfig/templates/Flags.java.template +++ b/tools/aconfig/templates/Flags.java.template @@ -10,11 +10,11 @@ public final class Flags \{ } {{ endfor }} {{ -if is_test_mode }} - public static void setFeatureFlagsImpl(FeatureFlags featureFlags) \{ + public static void setFeatureFlags(FeatureFlags featureFlags) \{ Flags.FEATURE_FLAGS = featureFlags; } - public static void unsetFeatureFlagsImpl() \{ + public static void unsetFeatureFlags() \{ Flags.FEATURE_FLAGS = null; } {{ endif}}