Lifecycling { @NonNull static LifecycleEventObserver lifecycleEventObserver(Object object) { boolean isLifecycleEventObserver = object instanceof LifecycleEventObserver; boolean isFullLifecycleObserver = object instanceof FullLifecycleObserver; if (isLifecycleEventObserver && isFullLifecycleObserver) { return new FullLifecycleObserverAdapter((FullLifecycleObserver) object, (LifecycleEventObserver) object); } if (isFullLifecycleObserver) { return new FullLifecycleObserverAdapter((FullLifecycleObserver) object, null); } if (isLifecycleEventObserver) { return (LifecycleEventObserver) object; } final Class<?> klass = object.getClass(); int type = getObserverConstructorType(klass); if (type == GENERATED_CALLBACK) { List<Constructor<? extends GeneratedAdapter>> constructors = sClassToAdapters.get(klass); if (constructors.size() == 1) { GeneratedAdapter generatedAdapter = createGeneratedAdapter( constructors.get(0), object); return new SingleGeneratedAdapterObserver(generatedAdapter); } GeneratedAdapter[] adapters = new GeneratedAdapter[constructors.size()]; for (int i = 0; i < constructors.size(); i++) { adapters[i] = createGeneratedAdapter(constructors.get(i), object); } return new CompositeGeneratedAdaptersObserver(adapters); } return new ReflectiveGenericLifecycleObserver(object); } private  Lifecycling(); static String getAdapterName(String className);  }
@Test public void testDerivedWithNewLfMethodsNoGeneratedAdapter() { LifecycleEventObserver callback = lifecycleEventObserver(new DerivedWithNewMethods()); assertThat(callback, instanceOf(ReflectiveGenericLifecycleObserver.class)); }
@Test public void testDerivedWithNoNewLfMethodsNoGeneratedAdapter() { LifecycleEventObserver callback = lifecycleEventObserver(new DerivedWithNoNewMethods()); assertThat(callback, instanceOf(SingleGeneratedAdapterObserver.class)); }
@Test public void testDerivedWithOverridenMethodsNoGeneratedAdapter() { LifecycleEventObserver callback = lifecycleEventObserver( new DerivedWithOverridenMethodsWithLfAnnotation()); assertThat(callback, instanceOf(ReflectiveGenericLifecycleObserver.class)); }
@Test public void testInterfaceImpl1NoGeneratedAdapter() { LifecycleEventObserver callback = lifecycleEventObserver(new InterfaceImpl1()); assertThat(callback, instanceOf(SingleGeneratedAdapterObserver.class)); }
@Test public void testInterfaceImpl2NoGeneratedAdapter() { LifecycleEventObserver callback = lifecycleEventObserver(new InterfaceImpl2()); assertThat(callback, instanceOf(CompositeGeneratedAdaptersObserver.class)); }
@Test public void testInterfaceImpl3NoGeneratedAdapter() { LifecycleEventObserver callback = lifecycleEventObserver(new InterfaceImpl3()); assertThat(callback, instanceOf(CompositeGeneratedAdaptersObserver.class)); }
@Test public void testDerivedSequence() { LifecycleEventObserver callback2 = lifecycleEventObserver(new DerivedSequence2()); assertThat(callback2, instanceOf(ReflectiveGenericLifecycleObserver.class)); LifecycleEventObserver callback1 = lifecycleEventObserver(new DerivedSequence1()); assertThat(callback1, instanceOf(SingleGeneratedAdapterObserver.class)); }
@SuppressWarnings("deprecation") @Test public void testDeprecatedGenericLifecycleObserver() { GenericLifecycleObserver genericLifecycleObserver = new GenericLifecycleObserver() { @Override public void onStateChanged(@NonNull LifecycleOwner source, @NonNull Lifecycle.Event event) { } }; LifecycleEventObserver observer = lifecycleEventObserver(genericLifecycleObserver); assertThat(observer, is(observer)); }
@Test public void fullLifecycleObserverAndAnnotations() { class AnnotatedFullLifecycleObserver implements FullLifecycleObserver { @OnLifecycleEvent(ON_ANY) public void onAny() { throw new IllegalStateException("Annotations in FullLifecycleObserver " + "must not be called"); } @Override public void onCreate(LifecycleOwner owner) { } @Override public void onStart(LifecycleOwner owner) { } @Override public void onResume(LifecycleOwner owner) { } @Override public void onPause(LifecycleOwner owner) { } @Override public void onStop(LifecycleOwner owner) { } @Override public void onDestroy(LifecycleOwner owner) { } } LifecycleEventObserver callback = lifecycleEventObserver( new AnnotatedFullLifecycleObserver()); callback.onStateChanged(new DefaultLifecycleOwner(), Lifecycle.Event.ON_CREATE); callback.onStateChanged(new DefaultLifecycleOwner(), Lifecycle.Event.ON_START); callback.onStateChanged(new DefaultLifecycleOwner(), Lifecycle.Event.ON_RESUME); callback.onStateChanged(new DefaultLifecycleOwner(), Lifecycle.Event.ON_PAUSE); callback.onStateChanged(new DefaultLifecycleOwner(), Lifecycle.Event.ON_STOP); callback.onStateChanged(new DefaultLifecycleOwner(), Lifecycle.Event.ON_DESTROY); }
@Test public void lifecycleEventObserverAndAnnotations() { class AnnotatedLifecycleEventObserver implements LifecycleEventObserver { @OnLifecycleEvent(ON_ANY) public void onAny() { throw new IllegalStateException("Annotations in FullLifecycleObserver " + "must not be called"); } @Override public void onStateChanged(@NonNull LifecycleOwner source, @NonNull Lifecycle.Event event) { } } LifecycleEventObserver callback = lifecycleEventObserver( new AnnotatedLifecycleEventObserver()); callback.onStateChanged(new DefaultLifecycleOwner(), Lifecycle.Event.ON_CREATE); callback.onStateChanged(new DefaultLifecycleOwner(), Lifecycle.Event.ON_START); callback.onStateChanged(new DefaultLifecycleOwner(), Lifecycle.Event.ON_RESUME); callback.onStateChanged(new DefaultLifecycleOwner(), Lifecycle.Event.ON_PAUSE); callback.onStateChanged(new DefaultLifecycleOwner(), Lifecycle.Event.ON_STOP); callback.onStateChanged(new DefaultLifecycleOwner(), Lifecycle.Event.ON_DESTROY); }