RxPreferences { @NonNull public Editor edit() { return new Editor(preferences.edit()); } RxPreferences(@NonNull SharedPreferences preferences); @NonNull Single<Map<String, ?>> getAllOnce(); @NonNull Single<String> getStringOnce(@NonNull String key, @NonNull String defaultValue); @NonNull Single<Set<String>> getStringSetOnce(@NonNull String key, @NonNull Set<String> defaultValues); @NonNull Single<Integer> getIntOnce(@NonNull String key, int defaultValue); @NonNull Single<Long> getLongOnce(@NonNull String key, long defaultValue); @NonNull Single<Float> getFloatOnce(@NonNull String key, float defaultValue); @NonNull Single<Boolean> getBooleanOnce(@NonNull String key, boolean defaultValue); @NonNull Single<Boolean> containsOnce(@NonNull String key); @NonNull Observable<Map<String, ?>> getAllStream(); @NonNull Observable<String> getStringStream(@NonNull String key, @NonNull String defaultValue); @NonNull Observable<Set<String>> getStringSetStream(@NonNull String key, @NonNull Set<String> defaultValue); @NonNull Observable<Integer> getIntStream(@NonNull String key, int defaultValue); @NonNull Observable<Long> getLongStream(@NonNull String key, long defaultValue); @NonNull Observable<Float> getFloatStream(@NonNull String key, float defaultValue); @NonNull Observable<Boolean> getBooleanStream(@NonNull String key, boolean defaultValue); @NonNull Observable<Boolean> containsStream(@NonNull String key); @NonNull Editor edit();  }
@Test public void edit_returnsNewInstance() { assertNotEquals(rxPreferences.edit(), rxPreferences.edit()); }
@Test public void remove_removesFromInternalPreferences() { final String testKey = "Another test key"; final Disposable subscription = rxPreferences.edit() .remove(testKey) .commit() .subscribeOn(testScheduler) .subscribe(); subscriptions.add(subscription); verify(mockSharedPreferences).edit(); verifyNoMoreInteractions(mockSharedPreferences); verify(mockSharedPreferencesEditor).remove(testKey); verifyNoMoreInteractions(mockSharedPreferencesEditor); assertFalse(subscription.isDisposed()); advanceScheduler(); verifyCommitAfterSubscribe(subscription); }
@Test public void clear_clearsFromInternalPreferences() { final Disposable subscription = rxPreferences.edit() .clear() .commit() .subscribeOn(testScheduler) .subscribe(); subscriptions.add(subscription); verify(mockSharedPreferences).edit(); verifyNoMoreInteractions(mockSharedPreferences); verify(mockSharedPreferencesEditor).clear(); verifyNoMoreInteractions(mockSharedPreferencesEditor); assertFalse(subscription.isDisposed()); advanceScheduler(); verifyCommitAfterSubscribe(subscription); }
@Test public void commit_internalEditorReturnsTrue_completes() { final TestObserver<Void> testObserver = rxPreferences.edit() .commit() .test(); subscriptions.add(testObserver); testObserver.assertComplete(); }
@Test public void commit_internalEditorReturnsFalse_propagatesCommitException() { when(mockSharedPreferencesEditor.commit()).thenReturn(false); final TestObserver<Void> testObserver = rxPreferences.edit() .commit() .test(); subscriptions.add(testObserver); testObserver.assertError(RxPreferences.Editor.CommitException.class); }