Skip to content
This repository has been archived by the owner on Aug 4, 2019. It is now read-only.

Commit

Permalink
Review the code, Add pattern view model test closed #25
Browse files Browse the repository at this point in the history
  • Loading branch information
EhsanMashhadi committed Jul 28, 2019
1 parent e4bd3db commit 1a159c4
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 23 deletions.
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ dependencies {
implementation "org.koin:koin-android:2.0.1"
implementation "org.koin:koin-android-viewmodel:2.0.1"

testImplementation "android.arch.core:core-testing:1.1.1"
testImplementation "org.mockito:mockito-inline:3.0.0"
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.2.0'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package de.netalic.peacock.ui.base

import de.netalic.peacock.R
import de.netalic.peacock.ui.base.BaseActivity
import kotlinx.android.synthetic.main.activity_mainhost.*

class MainHostActivity : BaseActivity() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ import de.netalic.peacock.ui.base.BaseFragment
import de.netalic.peacock.ui.base.MainHostActivity
import kotlinx.android.synthetic.main.fragment_patternlogin.*
import org.koin.android.viewmodel.ext.android.viewModel
import org.koin.core.KoinComponent
import timber.log.Timber


class PatternFragment : BaseFragment(), PatternLockViewListener {
Expand Down Expand Up @@ -44,7 +42,8 @@ class PatternFragment : BaseFragment(), PatternLockViewListener {
mPatternViewModel.getResponse().observe(this, Observer {

when (it.data) {
ResponseStatus.FIRST_SUCCESS -> mTextViewMessage.text = getString(R.string.patternLogin_messageDrawAgain)
ResponseStatus.FIRST_SUCCESS -> mTextViewMessage.text =
getString(R.string.patternLogin_messageDrawAgain)
ResponseStatus.SECOND_SUCCESS -> {
Toast.makeText(requireContext(), "MATCH", Toast.LENGTH_LONG).show()
mPatternLockView.removePatternLockListener(this)
Expand Down Expand Up @@ -73,6 +72,7 @@ class PatternFragment : BaseFragment(), PatternLockViewListener {
val result = PatternLockUtils.patternToString(mPatternLockView, pattern)
mPatternViewModel.onPatternListener(result)
}

override fun onCleared() {}
override fun onStarted() {}
override fun onProgress(progressPattern: MutableList<PatternLockView.Dot>?) {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,5 @@ class PatternViewModel : BaseViewModel() {
mResponse.value = MyResponse.success(ResponseStatus.FAILED)
}
}

}

}
17 changes: 0 additions & 17 deletions app/src/test/java/de/netalic/peacock/ExampleUnitTest.kt

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package de.netalic.peacock.ui.login.pattern

import androidx.arch.core.executor.testing.InstantTaskExecutorRule
import com.ehsanmashhadi.samplestructure.util.LiveDataTestUtil
import org.junit.Assert
import org.junit.Before
import org.junit.Rule
import org.junit.Test

class PatternViewModelTest {

@get:Rule
var instantExecutorRule = InstantTaskExecutorRule()

private lateinit var mPatternViewModel: PatternViewModel

@Before
fun setUp() {
mPatternViewModel = PatternViewModel()
}

@Test
fun patternViewModel_drawPatternSuccess() {

val pattern = "123456"
mPatternViewModel.onPatternListener(pattern)
Assert.assertEquals(
LiveDataTestUtil.getValue(mPatternViewModel.getResponse()).data,
ResponseStatus.FIRST_SUCCESS
)
mPatternViewModel.onPatternListener(pattern)
Assert.assertEquals(
LiveDataTestUtil.getValue(mPatternViewModel.getResponse()).data,
ResponseStatus.SECOND_SUCCESS
)
}

@Test
fun patternViewModel_drawPatternFailed() {

val firstPattern = "123456"
val secondPattern = "654321"

mPatternViewModel.onPatternListener(firstPattern)
Assert.assertEquals(
LiveDataTestUtil.getValue(mPatternViewModel.getResponse()).data,
ResponseStatus.FIRST_SUCCESS
)
mPatternViewModel.onPatternListener(secondPattern)
Assert.assertEquals(
LiveDataTestUtil.getValue(mPatternViewModel.getResponse()).data,
ResponseStatus.FAILED
)
}
}
29 changes: 29 additions & 0 deletions app/src/test/java/de/netalic/peacock/util/LiveDataUtil.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.ehsanmashhadi.samplestructure.util

import java.util.concurrent.CountDownLatch
import java.util.concurrent.TimeUnit
import androidx.lifecycle.LiveData
import androidx.lifecycle.Observer

object LiveDataTestUtil {

/**
* Get the value from a LiveData object. We're waiting for LiveData to emit, for 2 seconds.
* Once we got a notification via onChanged, we stop observing.
*/
@Throws(InterruptedException::class)
fun <T> getValue(liveData: LiveData<T>): T {
val data = arrayOfNulls<Any>(1)
val latch = CountDownLatch(1)
val observer = object : Observer<T> {
override fun onChanged(o: T?) {
data[0] = o
latch.countDown()
liveData.removeObserver(this)
}
}
liveData.observeForever(observer)
latch.await(2, TimeUnit.SECONDS)
return data[0] as T
}
}

0 comments on commit 1a159c4

Please sign in to comment.