ReflectiveGenericLifecycleObserver implements LifecycleEventObserver { @Override public void onStateChanged(@NonNull LifecycleOwner source, @NonNull Event event) { mInfo.invokeCallbacks(source, event, mWrapped); } ReflectiveGenericLifecycleObserver(Object wrapped); @Override void onStateChanged(@NonNull LifecycleOwner source, @NonNull Event event);  }
@Test public void anyState() { AnyStateListener obj = mock(AnyStateListener.class); ReflectiveGenericLifecycleObserver observer = new ReflectiveGenericLifecycleObserver(obj); when(mLifecycle.getCurrentState()).thenReturn(STARTED); observer.onStateChanged(mOwner, ON_CREATE); verify(obj).onAnyState(mOwner, ON_CREATE); reset(obj); observer.onStateChanged(mOwner, ON_START); verify(obj).onAnyState(mOwner, ON_START); reset(obj); observer.onStateChanged(mOwner, ON_RESUME); verify(obj).onAnyState(mOwner, ON_RESUME); reset(obj); observer.onStateChanged(mOwner, ON_PAUSE); verify(obj).onAnyState(mOwner, ON_PAUSE); reset(obj); observer.onStateChanged(mOwner, ON_STOP); verify(obj).onAnyState(mOwner, ON_STOP); reset(obj); observer.onStateChanged(mOwner, ON_DESTROY); verify(obj).onAnyState(mOwner, ON_DESTROY); reset(obj); }
@Test public void singleMethod() { CreatedStateListener obj = mock(CreatedStateListener.class); ReflectiveGenericLifecycleObserver observer = new ReflectiveGenericLifecycleObserver(obj); when(mLifecycle.getCurrentState()).thenReturn(CREATED); observer.onStateChanged(mOwner, ON_CREATE); verify(obj).onCreated(); verify(obj).onCreated(mOwner); }
@Test public void eachEvent() { AllMethodsListener obj = mock(AllMethodsListener.class); ReflectiveGenericLifecycleObserver observer = new ReflectiveGenericLifecycleObserver(obj); when(mLifecycle.getCurrentState()).thenReturn(CREATED); observer.onStateChanged(mOwner, ON_CREATE); verify(obj).created(); reset(obj); when(mLifecycle.getCurrentState()).thenReturn(STARTED); observer.onStateChanged(mOwner, ON_START); verify(obj).started(); reset(obj); when(mLifecycle.getCurrentState()).thenReturn(RESUMED); observer.onStateChanged(mOwner, ON_RESUME); verify(obj).resumed(); reset(obj); when(mLifecycle.getCurrentState()).thenReturn(STARTED); observer.onStateChanged(mOwner, ON_PAUSE); verify(obj).paused(); reset(obj); when(mLifecycle.getCurrentState()).thenReturn(CREATED); observer.onStateChanged(mOwner, ON_STOP); verify(obj).stopped(); reset(obj); when(mLifecycle.getCurrentState()).thenReturn(INITIALIZED); observer.onStateChanged(mOwner, ON_DESTROY); verify(obj).destroyed(); reset(obj); }
@Test public void testFailingObserver() { class UnprecedentedError extends Error { } LifecycleObserver obj = new LifecycleObserver() { @OnLifecycleEvent(ON_START) void started() { throw new UnprecedentedError(); } }; ReflectiveGenericLifecycleObserver observer = new ReflectiveGenericLifecycleObserver(obj); try { observer.onStateChanged(mOwner, ON_START); fail(); } catch (Exception e) { assertThat("exception cause is wrong", e.getCause() instanceof UnprecedentedError); } }
@Test public void testPrivateObserverMethods() { class ObserverWithPrivateMethod implements LifecycleObserver { boolean mCalled = false; @OnLifecycleEvent(ON_START) private void started() { mCalled = true; } } ObserverWithPrivateMethod obj = mock(ObserverWithPrivateMethod.class); ReflectiveGenericLifecycleObserver observer = new ReflectiveGenericLifecycleObserver(obj); observer.onStateChanged(mOwner, ON_START); assertThat(obj.mCalled, is(true)); }
@Test public void testValidSuper1() { DerivedClass2 obj = mock(DerivedClass2.class); ReflectiveGenericLifecycleObserver observer = new ReflectiveGenericLifecycleObserver(obj); observer.onStateChanged(mock(LifecycleOwner.class), ON_START); verify(obj).foo(Matchers.<LifecycleOwner>any()); verify(obj, never()).foo(); reset(obj); observer.onStateChanged(mock(LifecycleOwner.class), ON_STOP); verify(obj).foo(); verify(obj, never()).foo(Matchers.<LifecycleOwner>any()); }
@Test public void testValidSuper2() { DerivedClass4 obj = mock(DerivedClass4.class); ReflectiveGenericLifecycleObserver observer = new ReflectiveGenericLifecycleObserver(obj); observer.onStateChanged(mock(LifecycleOwner.class), ON_START); verify(obj).foo(Matchers.<LifecycleOwner>any()); verify(obj).foo(); }