@SuppressWarnings("unchecked") @Test public void testRemoveAll() { final SharedPreferences mockPreferences = mock(SharedPreferences.class); final SharedPreferences.Editor mockEditor = mock(SharedPreferences.Editor.class); when(mockPreferences.edit()).thenReturn(mockEditor); final Map<String, Object> values = new HashMap<>(5); for (int i = 0; i < 5; i++) { final String key = PREF_KEY + "." + i; values.put(key, Integer.toString(i)); when(mockEditor.remove(key)).thenReturn(mockEditor); } when(mockPreferences.getAll()).thenReturn((Map) values); final SharedPreferencesFacade facade = new SimpleSharedPreferencesFacade(mockPreferences); facade.removeAll(); verify(mockPreferences, times(values.size())).edit(); for (int i = 0; i < values.size(); i++) { final String key = PREF_KEY + "." + i; verify(mockEditor).remove(key); } verify(mockEditor, times(values.size())).commit(); }
@Override public int removeAll() { final Map<String, ?> values = preferences.getAll(); int result = 0; if (!values.isEmpty()) { for (final Map.Entry<String, ?> entry : values.entrySet()) { if (preferences.edit().remove(entry.getKey()).commit()) result++; } } return result; }
SimpleSharedPreferencesFacade implements SharedPreferencesFacade { @Override public int removeAll() { final Map<String, ?> values = preferences.getAll(); int result = 0; if (!values.isEmpty()) { for (final Map.Entry<String, ?> entry : values.entrySet()) { if (preferences.edit().remove(entry.getKey()).commit()) result++; } } return result; } }
SimpleSharedPreferencesFacade implements SharedPreferencesFacade { @Override public int removeAll() { final Map<String, ?> values = preferences.getAll(); int result = 0; if (!values.isEmpty()) { for (final Map.Entry<String, ?> entry : values.entrySet()) { if (preferences.edit().remove(entry.getKey()).commit()) result++; } } return result; } SimpleSharedPreferencesFacade(@NonNull final SharedPreferences preferences); }
SimpleSharedPreferencesFacade implements SharedPreferencesFacade { @Override public int removeAll() { final Map<String, ?> values = preferences.getAll(); int result = 0; if (!values.isEmpty()) { for (final Map.Entry<String, ?> entry : values.entrySet()) { if (preferences.edit().remove(entry.getKey()).commit()) result++; } } return result; } SimpleSharedPreferencesFacade(@NonNull final SharedPreferences preferences); @NonNull final SharedPreferences getPreferences(); @Override void registerOnSharedPreferenceChangeListener(@NonNull final SharedPreferences.OnSharedPreferenceChangeListener listener); @Override void unregisterOnSharedPreferenceChangeListener(@NonNull final SharedPreferences.OnSharedPreferenceChangeListener listener); @Override boolean contains(@NonNull final String key); @Override boolean putString(@NonNull final String key, @Nullable final String value); @Override @Nullable String getString(@NonNull final String key, @Nullable final String defValue); @Override boolean putStringSet(@NonNull final String key, @Nullable final Set<String> values); @Override @Nullable Set<String> getStringSet(@NonNull final String key, @Nullable final Set<String> defValues); @Override boolean putInt(@NonNull final String key, final int value); @Override int getInt(@NonNull final String key, final int defValue); @Override boolean putFloat(@NonNull final String key, final float value); @Override float getFloat(@NonNull final String key, final float defValue); @Override boolean putLong(@NonNull final String key, final long value); @Override long getLong(@NonNull final String key, final long defValue); @Override boolean putBoolean(@NonNull final String key, final boolean value); @Override boolean getBoolean(@NonNull final String key, final boolean defValue); @Override boolean remove(@NonNull final String key); @Override int removeAll(); }
SimpleSharedPreferencesFacade implements SharedPreferencesFacade { @Override public int removeAll() { final Map<String, ?> values = preferences.getAll(); int result = 0; if (!values.isEmpty()) { for (final Map.Entry<String, ?> entry : values.entrySet()) { if (preferences.edit().remove(entry.getKey()).commit()) result++; } } return result; } SimpleSharedPreferencesFacade(@NonNull final SharedPreferences preferences); @NonNull final SharedPreferences getPreferences(); @Override void registerOnSharedPreferenceChangeListener(@NonNull final SharedPreferences.OnSharedPreferenceChangeListener listener); @Override void unregisterOnSharedPreferenceChangeListener(@NonNull final SharedPreferences.OnSharedPreferenceChangeListener listener); @Override boolean contains(@NonNull final String key); @Override boolean putString(@NonNull final String key, @Nullable final String value); @Override @Nullable String getString(@NonNull final String key, @Nullable final String defValue); @Override boolean putStringSet(@NonNull final String key, @Nullable final Set<String> values); @Override @Nullable Set<String> getStringSet(@NonNull final String key, @Nullable final Set<String> defValues); @Override boolean putInt(@NonNull final String key, final int value); @Override int getInt(@NonNull final String key, final int defValue); @Override boolean putFloat(@NonNull final String key, final float value); @Override float getFloat(@NonNull final String key, final float defValue); @Override boolean putLong(@NonNull final String key, final long value); @Override long getLong(@NonNull final String key, final long defValue); @Override boolean putBoolean(@NonNull final String key, final boolean value); @Override boolean getBoolean(@NonNull final String key, final boolean defValue); @Override boolean remove(@NonNull final String key); @Override int removeAll();  }