Skip to content

Instantly share code, notes, and snippets.

@lvsecoto
Created February 12, 2019 02:11
Show Gist options
  • Save lvsecoto/88bba417b3800b7b944b1883205854ec to your computer and use it in GitHub Desktop.
Save lvsecoto/88bba417b3800b7b944b1883205854ec to your computer and use it in GitHub Desktop.
用于接收View的一个参数,就更新ViewModel的LiveData的情况
import android.arch.lifecycle.LiveData;
import android.arch.lifecycle.MutableLiveData;
import android.arch.lifecycle.Transformations;
/**
* 每当输入一个参数{@link #input(Object)},就回调{@link OnGetLiveData},生成一个LiveData
*/
public class ActionLiveData<InputType, ResultType> {
private MutableLiveData<InputType> mInput = new EventMutableLiveData<>();
private final LiveData<ResultType> mResult;
/**
* 使用此静态方法方便创建{@link ActionLiveData}, 也可以使用{@link #ActionLiveData(OnGetLiveData)}
* @param onGetLiveData 回调函数,用于创建新的LiveData
*/
public static <I, O> ActionLiveData<I, O> create(OnGetLiveData<I, O> onGetLiveData) {
return new ActionLiveData<>(onGetLiveData);
}
/**
* @param onGetLiveData 回调函数,用于创建新的LiveData
*/
public ActionLiveData(OnGetLiveData<InputType, ResultType> onGetLiveData) {
mResult = Transformations.switchMap(mInput, onGetLiveData::getLiveData);
}
/**
* 调用此函数触发{@link OnGetLiveData}创建新的LiveData
* @param input
*/
public void input(InputType input) {
mInput.setValue(input);
}
public LiveData<ResultType> asLiveData() {
return mResult;
}
public interface OnGetLiveData<I, O> {
LiveData<O> getLiveData(I I);
}
}
import android.arch.core.executor.testing.InstantTaskExecutorRule;
import android.arch.lifecycle.Lifecycle;
import android.arch.lifecycle.LifecycleOwner;
import android.arch.lifecycle.LifecycleRegistry;
import android.arch.lifecycle.LiveData;
import android.arch.lifecycle.Observer;
import android.support.test.runner.AndroidJUnit4;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestRule;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@RunWith(AndroidJUnit4.class)
public class ActionLiveDataTest {
@Rule
public TestRule rule = new InstantTaskExecutorRule();
private static LifecycleOwner mockLifecycleOwner() {
LifecycleOwner owner = Mockito.mock(LifecycleOwner.class);
LifecycleRegistry lifecycle = new LifecycleRegistry(owner);
lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_RESUME);
when(owner.getLifecycle()).thenReturn(lifecycle);
return owner;
}
@SuppressWarnings("unchecked")
private static <T> Observer<T> mockObserver() {
return (Observer<T>) Mockito.mock(Observer.class);
}
@Test
public void input() {
LifecycleOwner owner = mockLifecycleOwner();
Observer<String> observer = mockObserver();
final Integer[] liveDataCreateCount = {0};
ActionLiveData<Integer, String> action =
ActionLiveData.create((Integer input) -> {
liveDataCreateCount[0]++;
return new LiveData<String>() {
@Override
protected void onActive() {
super.onActive();
setValue(input + "");
}
};
});
LiveData<String> result = action.asLiveData();
result.observe(owner, observer);
assertThat(liveDataCreateCount[0]).isEqualTo(0);
action.input(1);
assertThat(liveDataCreateCount[0]).isEqualTo(1);
verify(observer).onChanged("1");
result.removeObserver(observer);
result.observe(owner, observer);
assertThat(liveDataCreateCount[0]).isEqualTo(1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment