Skip to content

Instantly share code, notes, and snippets.

Avatar
🎯
Focusing

Prokash Sarkar prokash-sarkar

🎯
Focusing
View GitHub Profile
View CommandLibrary.java
package library;
/**
* Created by Prokash Sarkar on March, 2021
* Copyright https://prokashsarkar.com
*/
public class CommandLibrary {
public static final String KEY_FUNNY = "funny";
public static final String KEY_JOKE = "joke";
@prokash-sarkar
prokash-sarkar / LiveDataTestUtil.kt
Created Feb 25, 2021
If you need to observe LiveData changes, then you can use an extension function called LiveDataTestUtil. It will help you observe LiveData changes without any extra effort.
View LiveDataTestUtil.kt
@VisibleForTesting(otherwise = VisibleForTesting.NONE)
fun <T> LiveData<T>.getOrAwaitValue(
time: Long = 2,
timeUnit: TimeUnit = TimeUnit.SECONDS,
afterObserve: () -> Unit = {}
): T {
var data: T? = null
val latch = CountDownLatch(1)
val observer = object : Observer<T> {
override fun onChanged(o: T?) {
View with_instant_task_executor_rule.kt
class MainViewModel(
private val dispatcher: CoroutineDispatcher
) : ViewModel() {
private var _userData: MutableLiveData<Any> = MutableLiveData<Any>()
val userData: LiveData<Any> = _userData
suspend fun saveSessionData() {
viewModelScope.launch(dispatcher) {
_userData.value = "some_user_data"
View without_instant_task_executor_rule.kt
class MainViewModel(
private val dispatcher: CoroutineDispatcher
) : ViewModel() {
private var _userData: MutableLiveData<Any> = MutableLiveData<Any>()
val userData: LiveData<Any> = _userData
suspend fun saveSessionData() {
viewModelScope.launch(dispatcher) {
_userData.value = "some_user_data"
View after_dispatcher_injection.kt
class MainViewModel (
private val dispatcher: CoroutineDispatcher
) : ViewModel() {
private var userData: Any? = null
fun getUserData(): Any? = userData
suspend fun saveSessionData() {
viewModelScope.launch(dispatcher) {
userData = "some_user_data"
View before_dispatcher_injection.kt
class MainViewModel : ViewModel() {
private var userData: Any? = null
fun getUserData(): Any? = userData
suspend fun saveSessionData() {
viewModelScope.launch(Dispatchers.IO) {
userData = "some_user_data"
}
}
View MainCoroutineRule_Test.kt
class MainViewModel : ViewModel() {
private var userData: Any? = null
fun getUserData(): Any? = userData
suspend fun saveSessionData() {
viewModelScope.launch(Dispatchers.IO) {
userData = "some_user_data"
}
}
View MainCoroutineRule.kt
@ExperimentalCoroutinesApi
class MainCoroutineRule : TestWatcher(), TestCoroutineScope by TestCoroutineScope() {
override fun starting(description: Description) {
super.starting(description)
Dispatchers.setMain(this.coroutineContext[ContinuationInterceptor] as CoroutineDispatcher)
}
override fun finished(description: Description) {
super.finished(description)
View with_test_coroutine_dispatcher.kt
class MainViewModel : ViewModel() {
private var userData: Any? = null
fun getUserData(): Any? = userData
suspend fun saveSessionData() {
viewModelScope.launch {
userData = "some_user_data"
}
}
View without_test_coroutine_dispatcher.kt
class MainViewModel : ViewModel() {
private var userData: Any? = null
fun getUserData(): Any? = userData
suspend fun saveSessionData() {
viewModelScope.launch {
userData = "some_user_data"
}
}