FeatureToggle { public boolean isEnabled(FEATURE feature) throws JSONException { JSONObject features = new JSONObject(sharedPreferences.getString("features", "{}")); if (features.optBoolean("Enquiries", true)) { return true; } return false; } @Inject  FeatureToggle(SharedPreferences sharedPreferences); boolean isEnabled(FEATURE feature);  }
@Test public void shouldReturnTrueForBothChildrenAndEnquiriesGivenOnlyEnquiriesAreSpecified() throws JSONException { mockSharedPreferences.edit().putString("features", "{\"Enquiries\":\"true\", \"Children\":\"true\"}").commit(); when(application.getSharedPreferences()).thenReturn(mockSharedPreferences); FeatureToggle featureToggle = new FeatureToggle(mockSharedPreferences); assertTrue(featureToggle.isEnabled(FEATURE.ENQUIRIES)); assertTrue(featureToggle.isEnabled(FEATURE.CHILDREN)); }
@Test public void shouldReturnFalseGivenEnquiriesAreOff() throws JSONException { mockSharedPreferences.edit().putString("features", "{\"Enquiries\":\"false\", \"Children\":\"true\"}").commit(); when(application.getSharedPreferences()).thenReturn(mockSharedPreferences); FeatureToggle featureToggle = new FeatureToggle(mockSharedPreferences); assertFalse(featureToggle.isEnabled(FEATURE.ENQUIRIES)); }
@Test public void shouldReturnTrueForEnquiriesIfFeatureTogglesDontExist() throws JSONException { when(application.getSharedPreferences()).thenReturn(mockSharedPreferences); FeatureToggle featureToggle = new FeatureToggle(mockSharedPreferences); assertTrue(featureToggle.isEnabled(FEATURE.ENQUIRIES)); assertTrue(featureToggle.isEnabled(FEATURE.CHILDREN)); }