source: class RxPreferencesTest {
  
      @Mock private SharedPreferences mockSharedPreferences;
      @Mock private SharedPreferences.Editor mockSharedPreferencesEditor;
      @InjectMocks private RxPreferences rxPreferences;
      private TestScheduler testScheduler;
      private final CompositeDisposable subscriptions;
  
      @Test
      public void getAllStream_unregistersListenerOnUnsubscribe() {
  
target:         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);
      }
  
  }