RxPreferences { @NonNull public Observable<Map<String, ?>> getAllStream() { return getAllOnce() .toObservable() .mergeWith(Observable.create(emitter -> { RxAllPreferencesListener listener = new RxAllPreferencesListener(emitter); registerRxPreferenceListener(listener, emitter); })); } 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 getAllStream_emitsOnListenerUpdate() { final Map<String, ?> returnedMap = Collections.singletonMap("Test dummy key", 234); when(mockSharedPreferences.getAll()).thenReturn((Map) returnedMap); final TestObserver<Map<String, ?>> subscription = rxPreferences.getAllStream().test(); subscriptions.add(subscription); subscription .assertNotComplete() .assertValueCount(1) .assertValue(returnedMap); final ArgumentCaptor<SharedPreferences.OnSharedPreferenceChangeListener> listenerCaptor = ArgumentCaptor.forClass(SharedPreferences.OnSharedPreferenceChangeListener.class); verify(mockSharedPreferences).registerOnSharedPreferenceChangeListener(listenerCaptor.capture()); final SharedPreferences.OnSharedPreferenceChangeListener listener = listenerCaptor.getValue(); listener.onSharedPreferenceChanged(mockSharedPreferences, "Another key"); subscription .assertNotComplete() .assertValueCount(2) .assertValues(returnedMap, returnedMap); }
@Test public void getAllStream_emitsCurrentValueOnSubscribe() { final Map<String, ?> returnedMap = Collections.singletonMap("Test dummy long key", 23453534523L); when(mockSharedPreferences.getAll()).thenReturn((Map) returnedMap); final Disposable subscription = rxPreferences.getAllStream() .subscribeOn(testScheduler) .subscribe(); subscriptions.add(subscription); verifyGetStreamBeforeSubscribe(subscription); advanceScheduler(); verifyNoMoreInteractions(mockSharedPreferencesEditor); verify(mockSharedPreferences).getAll(); verify(mockSharedPreferences).registerOnSharedPreferenceChangeListener(isA(RxAllPreferencesListener.class)); verifyNoMoreInteractions(mockSharedPreferences); assertFalse(subscription.isDisposed()); }
@Test public void getAllStream_unregistersListenerOnUnsubscribe() { final Map<String, ?> returnedMap = Collections.singletonMap("Test dummy long key", 23453534523L); when(mockSharedPreferences.getAll()).thenReturn((Map) returnedMap); final TestObserver<Map<String, ?>> subscription = rxPreferences.getAllStream().test(); subscriptions.add(subscription); final ArgumentCaptor<SharedPreferences.OnSharedPreferenceChangeListener> listenerCaptor = ArgumentCaptor.forClass(SharedPreferences.OnSharedPreferenceChangeListener.class); verify(mockSharedPreferences).registerOnSharedPreferenceChangeListener(listenerCaptor.capture()); final SharedPreferences.OnSharedPreferenceChangeListener listener = listenerCaptor.getValue(); verify(mockSharedPreferences, never()).unregisterOnSharedPreferenceChangeListener(any()); subscription.dispose(); verify(mockSharedPreferences).unregisterOnSharedPreferenceChangeListener(listener); }