mirror of
https://github.com/alibaba/Tangram-Android.git
synced 2026-10-17 05:07:05 +08:00
setup unit test for lifeCycleHelper
This commit is contained in:
@@ -0,0 +1,81 @@
|
||||
/*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2018 Alibaba Group
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.tmall.wireless.tangram;
|
||||
|
||||
import com.alibaba.android.rx.lifecycle.LFEvent;
|
||||
|
||||
import io.reactivex.functions.Function;
|
||||
|
||||
/**
|
||||
* Created by longerian on 2018/4/8.
|
||||
*
|
||||
* @author longerian
|
||||
* @date 2018/04/08
|
||||
*/
|
||||
|
||||
public class CellBindLFEvent extends LFEvent {
|
||||
|
||||
public static final CellBindLFEvent PRE_BIND = new CellBindLFEvent("onPreBind");
|
||||
public static final CellBindLFEvent BIND = new CellBindLFEvent("onBind");
|
||||
public static final CellBindLFEvent POST_BIND = new CellBindLFEvent("onPostBind");
|
||||
|
||||
public static final CellBindLFEvent PRE_UNBIND = new CellBindLFEvent("onPreUnbind");
|
||||
public static final CellBindLFEvent UNBIND = new CellBindLFEvent("onUnbind");
|
||||
public static final CellBindLFEvent POST_UNBIND = new CellBindLFEvent("onPostUnbind");
|
||||
|
||||
private CellBindLFEvent(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "CellBindLFEvent{" +
|
||||
"name='" + name + '\'' +
|
||||
'}';
|
||||
}
|
||||
|
||||
public static final Function<CellBindLFEvent, CellBindLFEvent> CELLBIND_LIFECYCLE =
|
||||
new Function<CellBindLFEvent, CellBindLFEvent>() {
|
||||
@Override
|
||||
public CellBindLFEvent apply(CellBindLFEvent lastEvent) throws Exception {
|
||||
if (lastEvent.equals(PRE_BIND)) {
|
||||
return PRE_UNBIND;
|
||||
}
|
||||
if (lastEvent.equals(BIND)) {
|
||||
return UNBIND;
|
||||
}
|
||||
if (lastEvent.equals(POST_BIND)) {
|
||||
return POST_UNBIND;
|
||||
}
|
||||
if (lastEvent.equals(PRE_UNBIND)) {
|
||||
return UNBIND;
|
||||
}
|
||||
if (lastEvent.equals(UNBIND)) {
|
||||
return POST_UNBIND;
|
||||
}
|
||||
throw new IllegalAccessException("Cannot bind to Activity lifecycle when outside of it.");
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -24,12 +24,20 @@
|
||||
|
||||
package com.tmall.wireless.tangram;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import com.alibaba.android.rx.lifecycle.ActivityLFEvent;
|
||||
import com.alibaba.android.rx.lifecycle.LifeCycleProviderImpl;
|
||||
|
||||
import android.content.Context;
|
||||
import android.support.test.InstrumentationRegistry;
|
||||
import android.support.test.annotation.UiThreadTest;
|
||||
import android.support.test.filters.SmallTest;
|
||||
import android.support.test.rule.UiThreadTestRule;
|
||||
import android.support.test.runner.AndroidJUnit4;
|
||||
import io.reactivex.Observable;
|
||||
import io.reactivex.observers.TestObserver;
|
||||
import io.reactivex.schedulers.TestScheduler;
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
@@ -49,16 +57,131 @@ public class LifeCycleHelperTest {
|
||||
|
||||
private final Context context = InstrumentationRegistry.getContext();
|
||||
|
||||
private Observable<Long> mObservable;
|
||||
|
||||
private TestObserver<Long> mObserver;
|
||||
|
||||
private TestScheduler mTestScheduler;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
|
||||
mTestScheduler = new TestScheduler();
|
||||
mObservable = Observable.interval(1, TimeUnit.MINUTES, mTestScheduler);
|
||||
mObserver = new TestObserver<Long>();
|
||||
}
|
||||
|
||||
@Test
|
||||
@SmallTest
|
||||
@UiThreadTest
|
||||
public void testLifeCycleProviderImpl() {
|
||||
public void testBindUntil() {
|
||||
LifeCycleProviderImpl<ActivityLFEvent> activityEventLifeCycleProvider = new LifeCycleProviderImpl<>(
|
||||
ActivityLFEvent.ACTIVITY_LIFECYCLE);
|
||||
|
||||
activityEventLifeCycleProvider.emitNext(ActivityLFEvent.CREATE);
|
||||
|
||||
mObservable.compose(activityEventLifeCycleProvider.<Long>bindUntil(ActivityLFEvent.STOP)).subscribe(mObserver);
|
||||
|
||||
mObserver.assertSubscribed();
|
||||
mObserver.assertEmpty();
|
||||
|
||||
mTestScheduler.advanceTimeTo(70, TimeUnit.SECONDS);
|
||||
mObserver.assertValues(0L);
|
||||
|
||||
activityEventLifeCycleProvider.emitNext(ActivityLFEvent.START);
|
||||
|
||||
mTestScheduler.advanceTimeTo(6, TimeUnit.MINUTES);
|
||||
mObserver.assertValues(0L, 1L, 2L, 3L, 4L, 5L);
|
||||
|
||||
activityEventLifeCycleProvider.emitNext(ActivityLFEvent.RESUME);
|
||||
|
||||
activityEventLifeCycleProvider.emitNext(ActivityLFEvent.PAUSE);
|
||||
|
||||
activityEventLifeCycleProvider.emitNext(ActivityLFEvent.STOP);
|
||||
|
||||
mObserver.assertComplete();
|
||||
|
||||
mTestScheduler.advanceTimeTo(10, TimeUnit.MINUTES);
|
||||
|
||||
mObserver.assertValueCount(6);
|
||||
|
||||
activityEventLifeCycleProvider.emitNext(ActivityLFEvent.DESTROY);
|
||||
}
|
||||
|
||||
@Test
|
||||
@UiThreadTest
|
||||
@SmallTest
|
||||
public void testBindToLifecycle() {
|
||||
LifeCycleProviderImpl<ActivityLFEvent> activityEventLifeCycleProvider = new LifeCycleProviderImpl<>(
|
||||
ActivityLFEvent.ACTIVITY_LIFECYCLE);
|
||||
|
||||
activityEventLifeCycleProvider.emitNext(ActivityLFEvent.CREATE);
|
||||
|
||||
mObservable.compose(activityEventLifeCycleProvider.<Long>bindToLifecycle()).subscribe(mObserver);
|
||||
|
||||
mTestScheduler.advanceTimeTo(70, TimeUnit.SECONDS);
|
||||
|
||||
mObserver.assertValues(0L);
|
||||
|
||||
activityEventLifeCycleProvider.emitNext(ActivityLFEvent.START);
|
||||
|
||||
mTestScheduler.advanceTimeTo(6, TimeUnit.MINUTES);
|
||||
|
||||
mObserver.assertValues(0L, 1L, 2L, 3L, 4L, 5L);
|
||||
|
||||
activityEventLifeCycleProvider.emitNext(ActivityLFEvent.RESUME);
|
||||
|
||||
activityEventLifeCycleProvider.emitNext(ActivityLFEvent.PAUSE);
|
||||
|
||||
activityEventLifeCycleProvider.emitNext(ActivityLFEvent.STOP);
|
||||
|
||||
mObserver.assertNotComplete();
|
||||
|
||||
activityEventLifeCycleProvider.emitNext(ActivityLFEvent.DESTROY);
|
||||
|
||||
mObserver.assertComplete();
|
||||
}
|
||||
|
||||
@Test
|
||||
@UiThreadTest
|
||||
@SmallTest
|
||||
public void testCustomLifecycleEvent() {
|
||||
LifeCycleProviderImpl<CellBindLFEvent> customLifecycleProvider = new LifeCycleProviderImpl<>(
|
||||
CellBindLFEvent.CELLBIND_LIFECYCLE);
|
||||
|
||||
mObservable.compose(customLifecycleProvider.<Long>bindUntil(CellBindLFEvent.UNBIND)).subscribe(mObserver);
|
||||
|
||||
mObserver.assertEmpty();
|
||||
|
||||
mTestScheduler.advanceTimeTo(70, TimeUnit.SECONDS);
|
||||
|
||||
mObserver.assertValue(0L);
|
||||
|
||||
customLifecycleProvider.emitNext(CellBindLFEvent.PRE_BIND);
|
||||
|
||||
mTestScheduler.advanceTimeTo(2, TimeUnit.MINUTES);
|
||||
|
||||
mObserver.assertValues(0L, 1L);
|
||||
|
||||
customLifecycleProvider.emitNext(CellBindLFEvent.BIND);
|
||||
|
||||
customLifecycleProvider.emitNext(CellBindLFEvent.POST_BIND);
|
||||
|
||||
mTestScheduler.advanceTimeTo(3, TimeUnit.MINUTES);
|
||||
|
||||
mObserver.assertValues(0L, 1L, 2L);
|
||||
|
||||
customLifecycleProvider.emitNext(CellBindLFEvent.PRE_UNBIND);
|
||||
|
||||
customLifecycleProvider.emitNext(CellBindLFEvent.UNBIND);
|
||||
|
||||
mTestScheduler.advanceTimeTo(4, TimeUnit.MINUTES);
|
||||
|
||||
mObserver.assertValues(0L, 1L, 2L);
|
||||
|
||||
mObserver.assertComplete();
|
||||
|
||||
customLifecycleProvider.emitNext(CellBindLFEvent.POST_UNBIND);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -39,7 +39,7 @@ import android.util.Log;
|
||||
import android.view.View;
|
||||
import com.tmall.wireless.tangram.core.service.ServiceManager;
|
||||
import com.tmall.wireless.tangram.structure.BaseCell;
|
||||
import com.tmall.wireless.tangram.support.ITimer.TimerStatus;
|
||||
import com.tmall.wireless.tangram.support.HandlerTimer.TimerStatus;
|
||||
import com.tmall.wireless.tangram.support.TimerSupport;
|
||||
import com.tmall.wireless.tangram.support.TimerSupport.OnTickListener;
|
||||
import io.reactivex.functions.Action;
|
||||
|
||||
+19
-10
@@ -33,19 +33,19 @@ import io.reactivex.functions.Function;
|
||||
* @date 2018/04/08
|
||||
*/
|
||||
|
||||
public class ActivityEvent extends Event {
|
||||
public class ActivityLFEvent extends LFEvent {
|
||||
|
||||
public static final ActivityEvent CREATE = new ActivityEvent();
|
||||
public static final ActivityEvent START = new ActivityEvent();
|
||||
public static final ActivityEvent RESUME = new ActivityEvent();
|
||||
public static final ActivityEvent PAUSE = new ActivityEvent();
|
||||
public static final ActivityEvent STOP = new ActivityEvent();
|
||||
public static final ActivityEvent DESTROY = new ActivityEvent();
|
||||
public static final ActivityLFEvent CREATE = new ActivityLFEvent("onCreate");
|
||||
public static final ActivityLFEvent START = new ActivityLFEvent("onStart");
|
||||
public static final ActivityLFEvent RESUME = new ActivityLFEvent("onResume");
|
||||
public static final ActivityLFEvent PAUSE = new ActivityLFEvent("onPause");
|
||||
public static final ActivityLFEvent STOP = new ActivityLFEvent("onStop");
|
||||
public static final ActivityLFEvent DESTROY = new ActivityLFEvent("onDestroy");
|
||||
|
||||
public static final Function<ActivityEvent, ActivityEvent> ACTIVITY_LIFECYCLE =
|
||||
new Function<ActivityEvent, ActivityEvent>() {
|
||||
public static final Function<ActivityLFEvent, ActivityLFEvent> ACTIVITY_LIFECYCLE =
|
||||
new Function<ActivityLFEvent, ActivityLFEvent>() {
|
||||
@Override
|
||||
public ActivityEvent apply(ActivityEvent lastEvent) throws Exception {
|
||||
public ActivityLFEvent apply(ActivityLFEvent lastEvent) throws Exception {
|
||||
if (lastEvent.equals(CREATE)) {
|
||||
return DESTROY;
|
||||
}
|
||||
@@ -65,5 +65,14 @@ public class ActivityEvent extends Event {
|
||||
}
|
||||
};
|
||||
|
||||
private ActivityLFEvent(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "ActivityLFEvent{" +
|
||||
"name='" + name + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
+14
-1
@@ -31,5 +31,18 @@ package com.alibaba.android.rx.lifecycle;
|
||||
* @date 2018/04/04
|
||||
*/
|
||||
|
||||
abstract public class Event {
|
||||
abstract public class LFEvent {
|
||||
|
||||
final protected String name;
|
||||
|
||||
protected LFEvent(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "LFEvent{" +
|
||||
"name='" + name + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
@@ -47,17 +47,23 @@ public class LifeCycleHelper {
|
||||
}));
|
||||
}
|
||||
|
||||
public static <T, E> LifecycleTransformer<T> bindToLifeCycle(Observable<E> lifecycle, final Function<E, E> correspondingEvents) {
|
||||
public static <T, E> LifecycleTransformer<T> bindToLifeCycle(Observable<E> lifecycle,
|
||||
final Function<E, E> correspondingEvents) {
|
||||
Observable<E> lifecycleCopy = lifecycle.share();
|
||||
return new LifecycleTransformer<>(Observable.combineLatest(lifecycle.take(1).map(correspondingEvents),
|
||||
lifecycleCopy.skip(1),
|
||||
new BiFunction<E,E, Boolean>() {
|
||||
new BiFunction<E, E, Boolean>() {
|
||||
|
||||
@Override
|
||||
public Boolean apply(E e, E e2) throws Exception {
|
||||
return e.equals(e2);
|
||||
}
|
||||
}));
|
||||
}).filter(new Predicate<Boolean>() {
|
||||
@Override
|
||||
public boolean test(Boolean cmpResult) throws Exception {
|
||||
return cmpResult;
|
||||
}
|
||||
}));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -31,7 +31,7 @@ package com.alibaba.android.rx.lifecycle;
|
||||
* @date 2018/04/04
|
||||
*/
|
||||
|
||||
public interface LifeCycleProvider<E extends Event> {
|
||||
public interface LifeCycleProvider<E extends LFEvent> {
|
||||
|
||||
void emitNext(E event);
|
||||
|
||||
|
||||
@@ -34,7 +34,7 @@ import io.reactivex.subjects.BehaviorSubject;
|
||||
* @date 2018/04/08
|
||||
*/
|
||||
|
||||
public class LifeCycleProviderImpl<E extends Event> implements LifeCycleProvider<E> {
|
||||
public class LifeCycleProviderImpl<E extends LFEvent> implements LifeCycleProvider<E> {
|
||||
|
||||
private final BehaviorSubject<E> lifecycleSubject = BehaviorSubject.create();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user