### Question:
SharedPreferencesUtils { public boolean getAndSetBooleanPreference(String preference, boolean defaultValue) { Application application = (Application) firebaseApp.getApplicationContext(); SharedPreferences preferences = application.getSharedPreferences(PREFERENCES_PACKAGE_NAME, Context.MODE_PRIVATE); if (preferences.contains(preference)) { boolean result = preferences.getBoolean(preference, defaultValue); return result; } setBooleanPreference(preference, defaultValue); return defaultValue; } @Inject  SharedPreferencesUtils(FirebaseApp firebaseApp); void setBooleanPreference(String preference, boolean value); void clearPreference(String preference); boolean getAndSetBooleanPreference(String preference, boolean defaultValue); boolean getBooleanPreference(String preference, boolean defaultValue); boolean isPreferenceSet(String preference); boolean isManifestSet(String preference); boolean getBooleanManifestValue(String preference, boolean defaultValue);  }### Answer:
@Test public void getAndSetBooleanPreference_correctlySetsPreference() throws Exception { sharedPreferencesUtils.getAndSetBooleanPreference(TEST_PREFERENCE, false); verify(editor).putBoolean(TEST_PREFERENCE, false); verify(editor).apply(); }
@Test public void getAndSetBooleanPreference_correctlyGetsPreference() throws Exception { when(sharedPreferences.contains(TEST_PREFERENCE)).thenReturn(true); when(sharedPreferences.getBoolean(TEST_PREFERENCE, true)).thenReturn(false); assertThat(sharedPreferencesUtils.getAndSetBooleanPreference(TEST_PREFERENCE, true)).isFalse(); verify(sharedPreferences).contains(TEST_PREFERENCE); verify(sharedPreferences).getBoolean(TEST_PREFERENCE, true); }
@Test public void getAndSetBooleanPreference_correctlyGetsPreferenceFromDefault() throws Exception { when(sharedPreferences.contains(TEST_PREFERENCE)).thenReturn(false); assertThat(sharedPreferencesUtils.getAndSetBooleanPreference(TEST_PREFERENCE, true)).isTrue(); verify(sharedPreferences).contains(TEST_PREFERENCE); verify(editor).putBoolean(TEST_PREFERENCE, true); verify(editor).apply(); }
@Test public void getAndSetBooleanPreference_correctlyUsesDefaultWhenAllElseFails() throws Exception { Bundle metaData = createNewBundle(); ApplicationInfo appInfo = new ApplicationInfo(); appInfo.metaData = metaData; when(packageManager.getApplicationInfo(packageName, PackageManager.GET_META_DATA)) .thenReturn(appInfo); when(sharedPreferences.contains(any())).thenReturn(false); assertThat(sharedPreferencesUtils.getAndSetBooleanPreference(TEST_PREFERENCE, false)).isFalse(); verify(editor).putBoolean(TEST_PREFERENCE, false); verify(editor).apply(); }
@Test public void getBooleanPreference_correctlyGetsPreference() throws Exception { when(sharedPreferences.contains(TEST_PREFERENCE)).thenReturn(true); when(sharedPreferences.getBoolean(TEST_PREFERENCE, true)).thenReturn(false); assertThat(sharedPreferencesUtils.getAndSetBooleanPreference(TEST_PREFERENCE, true)).isFalse(); verify(sharedPreferences).contains(TEST_PREFERENCE); verify(sharedPreferences).getBoolean(TEST_PREFERENCE, true); }
@Test public void getBooleanPreference_correctlyGetsPreferenceFromDefault() throws Exception { when(sharedPreferences.contains(TEST_PREFERENCE)).thenReturn(false); assertThat(sharedPreferencesUtils.getAndSetBooleanPreference(TEST_PREFERENCE, true)).isTrue(); verify(sharedPreferences).contains(TEST_PREFERENCE); }
@Test public void getBooleanPreference_correctlyUsesDefaultWhenAllElseFails() throws Exception { Bundle metaData = createNewBundle(); ApplicationInfo appInfo = new ApplicationInfo(); appInfo.metaData = metaData; when(packageManager.getApplicationInfo(packageName, PackageManager.GET_META_DATA)) .thenReturn(appInfo); when(sharedPreferences.contains(any())).thenReturn(false); assertThat(sharedPreferencesUtils.getAndSetBooleanPreference(TEST_PREFERENCE, false)).isFalse(); verify(editor).putBoolean(TEST_PREFERENCE, false); verify(editor).apply(); }