MediatorLiveData extends MutableLiveData<T> { @MainThread public <S> void addSource(LiveData<S> source, Observer<S> onChanged) { Source<S> e = new Source<>(source, onChanged); Source<?> existing = mSources.putIfAbsent(source, e); if (existing != null && existing.mObserver != onChanged) { throw new IllegalArgumentException( "This source was already added with the different observer"); } if (existing != null) { return; } if (hasActiveObservers()) { e.plug(); } }  @MainThread void addSource(LiveData<S> source, Observer<S> onChanged); @MainThread void removeSource(LiveData<S> toRemote);  }
@Test public void testSingleDelivery() { Observer observer = mock(Observer.class); mMediator.addSource(mSource, observer); mSource.setValue("flatfoot"); verify(observer).onChanged("flatfoot"); mRegistry.handleLifecycleEvent(Lifecycle.Event.ON_STOP); reset(observer); verify(observer, never()).onChanged(any()); }
@Test public void testChangeWhileInactive() { Observer observer = mock(Observer.class); mMediator.addSource(mSource, observer); mMediator.observe(mOwner, mock(Observer.class)); mSource.setValue("one"); verify(observer).onChanged("one"); mRegistry.handleLifecycleEvent(Lifecycle.Event.ON_STOP); reset(observer); mSource.setValue("flatfoot"); mRegistry.handleLifecycleEvent(Lifecycle.Event.ON_START); verify(observer).onChanged("flatfoot"); }
@Test public void testAddSourceToActive() { mSource.setValue("flatfoot"); Observer observer = mock(Observer.class); mMediator.addSource(mSource, observer); verify(observer).onChanged("flatfoot"); }
@Test public void testAddSourceToInActive() { mSource.setValue("flatfoot"); mRegistry.handleLifecycleEvent(Lifecycle.Event.ON_STOP); Observer observer = mock(Observer.class); mMediator.addSource(mSource, observer); verify(observer, never()).onChanged(any()); mRegistry.handleLifecycleEvent(Lifecycle.Event.ON_START); verify(observer).onChanged("flatfoot"); }
@Test public void testSourceInactive() { Observer observer = mock(Observer.class); mMediator.addSource(mSource, observer); assertThat(mSourceActive, is(true)); mRegistry.handleLifecycleEvent(Lifecycle.Event.ON_STOP); assertThat(mSourceActive, is(false)); mRegistry.handleLifecycleEvent(Lifecycle.Event.ON_START); assertThat(mSourceActive, is(true)); }
@Test public void testNoLeakObserver() { assertThat(mSource.hasObservers(), is(false)); Observer observer = mock(Observer.class); mMediator.addSource(mSource, observer); assertThat(mSource.hasObservers(), is(true)); mRegistry.handleLifecycleEvent(Lifecycle.Event.ON_STOP); mRegistry.handleLifecycleEvent(Lifecycle.Event.ON_DESTROY); mMediator = null; assertThat(mSource.hasObservers(), is(false)); }
@Test public void testMultipleSources() { Observer observer1 = mock(Observer.class); mMediator.addSource(mSource, observer1); MutableLiveData<Integer> source2 = new MutableLiveData<>(); Observer observer2 = mock(Observer.class); mMediator.addSource(source2, observer2); mSource.setValue("flatfoot"); verify(observer1).onChanged("flatfoot"); verify(observer2, never()).onChanged(any()); reset(observer1, observer2); source2.setValue(1703); verify(observer1, never()).onChanged(any()); verify(observer2).onChanged(1703); reset(observer1, observer2); mRegistry.handleLifecycleEvent(Lifecycle.Event.ON_STOP); mSource.setValue("failure"); source2.setValue(0); verify(observer1, never()).onChanged(any()); verify(observer2, never()).onChanged(any()); }
@Test(expected = IllegalArgumentException.class) public void reAddSameSourceWithDifferentObserver() { mMediator.addSource(mSource, mock(Observer.class)); mMediator.addSource(mSource, mock(Observer.class)); }
@Test public void addSourceDuringOnActive() { mRegistry.handleLifecycleEvent(Lifecycle.Event.ON_STOP); mSource.setValue("a"); mMediator.addSource(mSource, new Observer<String>() { @Override public void onChanged(@Nullable String s) { MutableLiveData<String> source = new MutableLiveData<>(); source.setValue("b"); mMediator.addSource(source, new Observer<String>() { @Override public void onChanged(@Nullable String s) { mMediator.setValue("c"); } }); } }); mRegistry.handleLifecycleEvent(Lifecycle.Event.ON_START); assertThat(mMediator.getValue(), is("c")); }