SharedPreferencesProvider implements Provider<SharedPreferences> { public SharedPreferences get() { if( preferencesName!=null ) return application.getSharedPreferences(preferencesName, Context.MODE_PRIVATE); if( new File("shared_prefs/"+ROBOGUICE_1_DEFAULT_FILENAME).canRead() ) return application.getSharedPreferences(ROBOGUICE_1_DEFAULT_FILENAME, Context.MODE_PRIVATE); return PreferenceManager.getDefaultSharedPreferences(application) ; } SharedPreferencesProvider(); @Inject  SharedPreferencesProvider(PreferencesNameHolder preferencesNameHolder);  SharedPreferencesProvider(String preferencesName); SharedPreferences get();  }
@Test public void shouldInjectDefaultSharedPrefs() throws Exception { final A a = Robolectric.buildActivity(A.class).create().get(); final Field f = TestSharedPreferences.class.getDeclaredField("filename"); f.setAccessible(true); assertTrue(Strings.notEmpty(f.get(a.prefs))); assertThat(f.get(a.prefs), equalTo(f.get(PreferenceManager.getDefaultSharedPreferences(a)))); }
@Test public void shouldInjectNamedSharedPrefs() throws Exception { RoboGuice.getOrCreateBaseApplicationInjector(Robolectric.application,RoboGuice.DEFAULT_STAGE, RoboGuice.newDefaultRoboModule(Robolectric.application), new ModuleA() ); try { final A a = Robolectric.buildActivity(A.class).create().get(); final Field f = TestSharedPreferences.class.getDeclaredField("filename"); f.setAccessible(true); assertEquals("FOOBAR", f.get(a.prefs)); } finally { RoboGuice.Util.reset(); } }
@Test public void shouldFallbackOnOldDefaultIfPresent() throws Exception { final File oldDefault = new File("shared_prefs/default.xml"); final File oldDir = new File("shared_prefs"); oldDir.mkdirs(); oldDefault.createNewFile(); try { final A a = Robolectric.buildActivity(A.class).create().get(); final Field f = TestSharedPreferences.class.getDeclaredField("filename"); f.setAccessible(true); assertTrue(Strings.notEmpty(f.get(a.prefs))); assertEquals("default.xml", f.get(a.prefs)); } finally { oldDefault.delete(); oldDir.delete(); } }
@Test public void shouldNotFallbackOnOldDefaultIfNamedFileSpecified() throws Exception { RoboGuice.getOrCreateBaseApplicationInjector(Robolectric.application,RoboGuice.DEFAULT_STAGE, RoboGuice.newDefaultRoboModule(Robolectric.application), new ModuleA() ); final File oldDefault = new File("shared_prefs/default.xml"); final File oldDir = new File("shared_prefs"); oldDir.mkdirs(); oldDefault.createNewFile(); try { final A a = Robolectric.buildActivity(A.class).create().get(); final Field f = TestSharedPreferences.class.getDeclaredField("filename"); f.setAccessible(true); assertTrue(Strings.notEmpty(f.get(a.prefs))); assertEquals("FOOBAR", f.get(a.prefs)); } finally { RoboGuice.Util.reset(); oldDefault.delete(); oldDir.delete(); } }