1 package org.robolectric.shadows; 2 3 import static org.assertj.core.api.Assertions.assertThat; 4 import static org.junit.Assert.assertFalse; 5 import static org.junit.Assert.assertTrue; 6 import static org.mockito.Mockito.mock; 7 import static org.mockito.Mockito.verifyZeroInteractions; 8 9 import android.content.Context; 10 import android.content.SharedPreferences; 11 import java.util.ArrayList; 12 import java.util.HashSet; 13 import java.util.List; 14 import java.util.Set; 15 import org.junit.Before; 16 import org.junit.Test; 17 import org.junit.runner.RunWith; 18 import org.robolectric.RobolectricTestRunner; 19 import org.robolectric.RuntimeEnvironment; 20 21 @RunWith(RobolectricTestRunner.class) 22 public class ShadowSharedPreferencesTest { 23 private final static String FILENAME = "filename"; 24 private SharedPreferences.Editor editor; 25 private SharedPreferences sharedPreferences; 26 27 private final Set<String> stringSet = new HashSet<>(); 28 29 private Context context; 30 31 @Before 32 public void setUp() { 33 context = RuntimeEnvironment.application; 34 35 sharedPreferences = context.getSharedPreferences(FILENAME, Context.MODE_PRIVATE); 36 // Ensure no shared preferences have leaked from previous tests. 37 assertThat(sharedPreferences.getAll()).hasSize(0); 38 39 editor = sharedPreferences.edit(); 40 editor.putBoolean("boolean", true); 41 editor.putFloat("float", 1.1f); 42 editor.putInt("int", 2); 43 editor.putLong("long", 3L); 44 editor.putString("string", "foobar"); 45 46 stringSet.add( "string1" ); 47 stringSet.add( "string2" ); 48 stringSet.add( "string3" ); 49 editor.putStringSet("stringSet", stringSet); 50 } 51 52 @Test 53 public void commit_shouldStoreValues() throws Exception { 54 editor.commit(); 55 56 SharedPreferences anotherSharedPreferences = context.getSharedPreferences(FILENAME, Context.MODE_PRIVATE); 57 assertTrue(anotherSharedPreferences.getBoolean("boolean", false)); 58 assertThat(anotherSharedPreferences.getFloat("float", 666f)).isEqualTo(1.1f); 59 assertThat(anotherSharedPreferences.getInt("int", 666)).isEqualTo(2); 60 assertThat(anotherSharedPreferences.getLong("long", 666L)).isEqualTo(3L); 61 assertThat(anotherSharedPreferences.getString("string", "wacka wa")).isEqualTo("foobar"); 62 assertThat(anotherSharedPreferences.getStringSet("stringSet", null)).isEqualTo(stringSet); 63 } 64 65 @Test 66 public void commit_shouldClearEditsThatNeedRemoveAndEditsThatNeedCommit() throws Exception { 67 editor.commit(); 68 editor.remove("string").commit(); 69 70 assertThat(sharedPreferences.getString("string", "no value for key")).isEqualTo("no value for key"); 71 72 SharedPreferences anotherSharedPreferences = context.getSharedPreferences(FILENAME, Context.MODE_PRIVATE); 73 anotherSharedPreferences.edit().putString("string", "value for key").commit(); 74 75 editor.commit(); 76 assertThat(sharedPreferences.getString("string", "no value for key")).isEqualTo("value for key"); 77 } 78 79 @Test 80 public void getAll_shouldReturnAllValues() throws Exception { 81 editor.commit(); 82 assertThat(sharedPreferences.getAll()).hasSize(6); 83 assertThat(sharedPreferences.getAll().get("int")).isEqualTo(2); 84 } 85 86 @Test 87 public void commit_shouldRemoveValuesThenSetValues() throws Exception { 88 editor.putString("deleteMe", "foo").commit(); 89 90 editor.remove("deleteMe"); 91 92 editor.putString("dontDeleteMe", "baz"); 93 editor.remove("dontDeleteMe"); 94 95 editor.commit(); 96 97 SharedPreferences anotherSharedPreferences = context.getSharedPreferences(FILENAME, Context.MODE_PRIVATE); 98 assertThat(anotherSharedPreferences.getBoolean("boolean", false)).isTrue(); 99 assertThat(anotherSharedPreferences.getFloat("float", 666f)).isEqualTo(1.1f); 100 assertThat(anotherSharedPreferences.getInt("int", 666)).isEqualTo(2); 101 assertThat(anotherSharedPreferences.getLong("long", 666L)).isEqualTo(3L); 102 assertThat(anotherSharedPreferences.getString("string", "wacka wa")).isEqualTo("foobar"); 103 104 assertThat(anotherSharedPreferences.getString("deleteMe", "awol")).isEqualTo("awol"); 105 assertThat(anotherSharedPreferences.getString("dontDeleteMe", "oops")).isEqualTo("oops"); 106 } 107 108 @Test 109 public void commit_shouldClearThenSetValues() throws Exception { 110 editor.putString("deleteMe", "foo"); 111 112 editor.clear(); 113 editor.putString("dontDeleteMe", "baz"); 114 115 editor.commit(); 116 117 SharedPreferences anotherSharedPreferences = context.getSharedPreferences(FILENAME, Context.MODE_PRIVATE); 118 assertTrue(anotherSharedPreferences.getBoolean("boolean", false)); 119 assertThat(anotherSharedPreferences.getFloat("float", 666f)).isEqualTo(1.1f); 120 assertThat(anotherSharedPreferences.getInt("int", 666)).isEqualTo(2); 121 assertThat(anotherSharedPreferences.getLong("long", 666L)).isEqualTo(3L); 122 assertThat(anotherSharedPreferences.getString("string", "wacka wa")).isEqualTo("foobar"); 123 124 // Android always calls clear before put on any open editor, so here "foo" is preserved rather than cleared. 125 assertThat(anotherSharedPreferences.getString("deleteMe", "awol")).isEqualTo("foo"); 126 assertThat(anotherSharedPreferences.getString("dontDeleteMe", "oops")).isEqualTo("baz"); 127 } 128 129 @Test 130 public void putString_shouldRemovePairIfValueIsNull() throws Exception { 131 editor.putString("deleteMe", "foo"); 132 133 editor.putString("deleteMe", null); 134 editor.commit(); 135 136 assertThat(sharedPreferences.getString("deleteMe", null)).isNull(); 137 } 138 139 @Test 140 public void putStringSet_shouldRemovePairIfValueIsNull() throws Exception { 141 editor.putStringSet("deleteMe", new HashSet<String>()); 142 143 editor.putStringSet("deleteMe", null); 144 editor.commit(); 145 146 assertThat(sharedPreferences.getStringSet("deleteMe", null)).isNull(); 147 } 148 149 @Test 150 public void apply_shouldStoreValues() throws Exception { 151 editor.apply(); 152 153 SharedPreferences anotherSharedPreferences = context.getSharedPreferences(FILENAME, Context.MODE_PRIVATE); 154 assertThat(anotherSharedPreferences.getString("string", "wacka wa")).isEqualTo("foobar"); 155 } 156 157 @Test 158 public void shouldReturnDefaultValues() throws Exception { 159 SharedPreferences anotherSharedPreferences = context.getSharedPreferences("bazBang", Context.MODE_PRIVATE); 160 161 assertFalse(anotherSharedPreferences.getBoolean("boolean", false)); 162 assertThat(anotherSharedPreferences.getFloat("float", 666f)).isEqualTo(666f); 163 assertThat(anotherSharedPreferences.getInt("int", 666)).isEqualTo(666); 164 assertThat(anotherSharedPreferences.getLong("long", 666L)).isEqualTo(666L); 165 assertThat(anotherSharedPreferences.getString("string", "wacka wa")).isEqualTo("wacka wa"); 166 } 167 168 @Test 169 public void shouldRemoveRegisteredListenersOnUnresgister() { 170 SharedPreferences anotherSharedPreferences = context.getSharedPreferences("bazBang", Context.MODE_PRIVATE); 171 172 SharedPreferences.OnSharedPreferenceChangeListener mockListener = mock(SharedPreferences.OnSharedPreferenceChangeListener.class); 173 anotherSharedPreferences.registerOnSharedPreferenceChangeListener(mockListener); 174 175 anotherSharedPreferences.unregisterOnSharedPreferenceChangeListener(mockListener); 176 177 anotherSharedPreferences.edit().putString("key", "value"); 178 verifyZeroInteractions(mockListener); 179 } 180 181 @Test 182 public void shouldTriggerRegisteredListeners() { 183 SharedPreferences anotherSharedPreferences = context.getSharedPreferences("bazBang", Context.MODE_PRIVATE); 184 185 final String testKey = "foo"; 186 187 final List<String> transcript = new ArrayList<>(); 188 189 SharedPreferences.OnSharedPreferenceChangeListener listener = new SharedPreferences.OnSharedPreferenceChangeListener() { 190 @Override 191 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { 192 transcript.add(key + " called"); 193 } 194 }; 195 anotherSharedPreferences.registerOnSharedPreferenceChangeListener(listener); 196 anotherSharedPreferences.edit().putString(testKey, "bar").commit(); 197 198 assertThat(transcript).containsExactly(testKey+ " called"); 199 } 200 } 201