@Test
public void getAllStream_unregistersListenerOnUnsubscribe() {

    final Map<String, ?> returnedMap = Collections.singletonMap("Test dummy long key", 23453534523L);
    //noinspection unchecked
    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);
}