1 package com.xtremelabs.robolectric.shadows; 2 3 import android.content.Context; 4 import android.content.SharedPreferences; 5 import android.content.SharedPreferences.Editor; 6 import android.preference.PreferenceManager; 7 import com.xtremelabs.robolectric.Robolectric; 8 import com.xtremelabs.robolectric.WithTestDefaultsRunner; 9 import com.xtremelabs.robolectric.tester.android.content.TestSharedPreferences; 10 import org.junit.Test; 11 import org.junit.runner.RunWith; 12 13 import java.util.Map; 14 15 import static org.junit.Assert.assertEquals; 16 import static org.junit.Assert.assertNotNull; 17 18 @RunWith(WithTestDefaultsRunner.class) 19 public class PreferenceManagerTest { 20 @Test shouldProvideDefaultSharedPreferences()21 public void shouldProvideDefaultSharedPreferences() throws Exception { 22 Map<String, Map<String, Object>> content = Robolectric.getShadowApplication().getSharedPreferenceMap(); 23 24 TestSharedPreferences testPrefs = new TestSharedPreferences(content, "__default__", Context.MODE_PRIVATE); 25 Editor editor = testPrefs.edit(); 26 editor.putInt("foobar", 13); 27 editor.commit(); 28 29 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(Robolectric.application); 30 31 assertNotNull(prefs); 32 assertEquals(13, prefs.getInt("foobar", 0)); 33 } 34 35 } 36