FakeFeatureFlagsImpl optionally takes defaults via a FeatureFlags.

Bug: 337449122
Flag: test_only
Test: presubmit
Change-Id: I45e2a523b36a6b14627c89ee7deffda711a32f5e
This commit is contained in:
Jeff DeCew 2024-04-27 18:06:24 +00:00
parent 836b9e1ba9
commit 23fbd1e2f6
2 changed files with 26 additions and 8 deletions

View file

@ -428,10 +428,16 @@ mod tests {
/** @hide */ /** @hide */
public class FakeFeatureFlagsImpl extends CustomFeatureFlags { public class FakeFeatureFlagsImpl extends CustomFeatureFlags {
private Map<String, Boolean> mFlagMap = new HashMap<>(); private final Map<String, Boolean> mFlagMap = new HashMap<>();
private final FeatureFlags mDefaults;
public FakeFeatureFlagsImpl() { public FakeFeatureFlagsImpl() {
this(null);
}
public FakeFeatureFlagsImpl(FeatureFlags defaults) {
super(null); super(null);
mDefaults = defaults;
// Initialize the map with null values // Initialize the map with null values
for (String flagName : getFlagNames()) { for (String flagName : getFlagNames()) {
mFlagMap.put(flagName, null); mFlagMap.put(flagName, null);
@ -441,10 +447,13 @@ mod tests {
@Override @Override
protected boolean getValue(String flagName, Predicate<FeatureFlags> getter) { protected boolean getValue(String flagName, Predicate<FeatureFlags> getter) {
Boolean value = this.mFlagMap.get(flagName); Boolean value = this.mFlagMap.get(flagName);
if (value == null) { if (value != null) {
throw new IllegalArgumentException(flagName + " is not set"); return value;
} }
return value; if (mDefaults != null) {
return getter.test(mDefaults);
}
throw new IllegalArgumentException(flagName + " is not set");
} }
public void setFlag(String flagName, boolean value) { public void setFlag(String flagName, boolean value) {

View file

@ -6,10 +6,16 @@ import java.util.function.Predicate;
/** @hide */ /** @hide */
public class FakeFeatureFlagsImpl extends CustomFeatureFlags \{ public class FakeFeatureFlagsImpl extends CustomFeatureFlags \{
private Map<String, Boolean> mFlagMap = new HashMap<>(); private final Map<String, Boolean> mFlagMap = new HashMap<>();
private final FeatureFlags mDefaults;
public FakeFeatureFlagsImpl() \{ public FakeFeatureFlagsImpl() \{
this(null);
}
public FakeFeatureFlagsImpl(FeatureFlags defaults) \{
super(null); super(null);
mDefaults = defaults;
// Initialize the map with null values // Initialize the map with null values
for (String flagName : getFlagNames()) \{ for (String flagName : getFlagNames()) \{
mFlagMap.put(flagName, null); mFlagMap.put(flagName, null);
@ -19,10 +25,13 @@ public class FakeFeatureFlagsImpl extends CustomFeatureFlags \{
@Override @Override
protected boolean getValue(String flagName, Predicate<FeatureFlags> getter) \{ protected boolean getValue(String flagName, Predicate<FeatureFlags> getter) \{
Boolean value = this.mFlagMap.get(flagName); Boolean value = this.mFlagMap.get(flagName);
if (value == null) \{ if (value != null) \{
throw new IllegalArgumentException(flagName + " is not set"); return value;
} }
return value; if (mDefaults != null) \{
return getter.test(mDefaults);
}
throw new IllegalArgumentException(flagName + " is not set");
} }
public void setFlag(String flagName, boolean value) \{ public void setFlag(String flagName, boolean value) \{