This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
abstract class Bird { | |
... | |
abstract fun fly() | |
} | |
class Penguin: Bird() { | |
... | |
override fun fly() { | |
throw Exception("Can't fly!") | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// domain | |
sealed class ErrorEntity { | |
sealed class ApiError: ErrorEntity() { | |
// ..... | |
} | |
sealed class FileError: ErrorEntity() { | |
object NotFound: FileError() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
abstract class OnOneClickListener(private val minClickInterval: Long = 500) : View.OnClickListener { | |
private var lastClickedTime: Long? = null | |
override fun onClick(v: View) { | |
val currentTime = SystemClock.elapsedRealtime() | |
if(lastClickedTime == null || currentTime - lastClickedTime!! > minClickInterval) { | |
onOneClick(v) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ch.immoscout24.ImmoScout24 | |
import androidx.lifecycle.Observer | |
/** | |
* Used for testing [androidx.lifecycle.LiveData] | |
* | |
* @param <T> type of data | |
*/ | |
@Suppress("unused") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private val usecase: TestUseCase = Mockito.mock(TestUseCase::class.java) | |
private val viewModel = ScreenViewModel(usecase) | |
private lateinit var stateObserver: LiveDataTestObserver<ScreenState> | |
@Before | |
fun setup() { | |
stateObserver = LiveDataTestObserver() | |
viewModel.liveData.observeForever(stateObserver) // start observing live data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class LiveDataTestObserver<T> : Observer<T> { | |
private val values: MutableList<T> = ArrayList() | |
override fun onChanged(value: T?) { | |
// whenever new value is set, add it into value list | |
value?.let { values.add(value) } | |
} | |
/** |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Test | |
fun `click button - success - should show loading then success message`() { | |
Mockito.`when`(usecase.execute()).thenReturn(Completable.complete()) | |
viewModel.clickButton() | |
// todo how to verify the state was turned into loading before showing the success message? | |
// verify last state | |
viewModel.liveData.value!!.apply { | |
assertFalse(isLoading) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ScreenViewModel: ViewModel() { | |
private val mutableLiveData = MutableLiveData<Int>() | |
val liveData: LiveData<Int> = mutableLiveData | |
fun clickButton() { | |
mutableLiveData.value = (mutableLiveData.value ?: 0) + 1 | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data class ScreenState( | |
val isLoading: Boolean, | |
val isButtonEnabled: Boolean, | |
val message: String? | |
) | |
class ScreenViewModel(private val usecase: TestUseCase): ViewModel() { | |
private val disposable = CompositeDisposable() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ComponentA | |
class ComponentB(val a: ComponentA) | |
class MyTest : KoinTest { | |
// Lazy inject property | |
val componentB : ComponentB by inject() | |
@Test | |
fun `should inject my components`() { |
NewerOlder