diff --git a/app/build.gradle b/app/build.gradle index fdd873c..66468ac 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -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' diff --git a/app/src/main/java/de/netalic/peacock/ui/base/MainHostActivity.kt b/app/src/main/java/de/netalic/peacock/ui/base/MainHostActivity.kt index 7e8ebd5..c1b3c3a 100644 --- a/app/src/main/java/de/netalic/peacock/ui/base/MainHostActivity.kt +++ b/app/src/main/java/de/netalic/peacock/ui/base/MainHostActivity.kt @@ -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() { diff --git a/app/src/main/java/de/netalic/peacock/ui/login/pattern/PatternFragment.kt b/app/src/main/java/de/netalic/peacock/ui/login/pattern/PatternFragment.kt index 20fc2f0..875cd72 100644 --- a/app/src/main/java/de/netalic/peacock/ui/login/pattern/PatternFragment.kt +++ b/app/src/main/java/de/netalic/peacock/ui/login/pattern/PatternFragment.kt @@ -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 { @@ -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) @@ -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?) {} diff --git a/app/src/main/java/de/netalic/peacock/ui/login/pattern/PatternViewModel.kt b/app/src/main/java/de/netalic/peacock/ui/login/pattern/PatternViewModel.kt index b56e8a6..a5e1df5 100644 --- a/app/src/main/java/de/netalic/peacock/ui/login/pattern/PatternViewModel.kt +++ b/app/src/main/java/de/netalic/peacock/ui/login/pattern/PatternViewModel.kt @@ -37,7 +37,5 @@ class PatternViewModel : BaseViewModel() { mResponse.value = MyResponse.success(ResponseStatus.FAILED) } } - } - } \ No newline at end of file diff --git a/app/src/test/java/de/netalic/peacock/ExampleUnitTest.kt b/app/src/test/java/de/netalic/peacock/ExampleUnitTest.kt deleted file mode 100644 index 5935b93..0000000 --- a/app/src/test/java/de/netalic/peacock/ExampleUnitTest.kt +++ /dev/null @@ -1,17 +0,0 @@ -package de.netalic.peacock - -import org.junit.Test - -import org.junit.Assert.* - -/** - * Example local unit test, which will execute on the development machine (host). - * - * See [testing documentation](http://d.android.com/tools/testing). - */ -class ExampleUnitTest { - @Test - fun addition_isCorrect() { - assertEquals(4, 2 + 2) - } -} diff --git a/app/src/test/java/de/netalic/peacock/ui/login/pattern/PatternViewModelTest.kt b/app/src/test/java/de/netalic/peacock/ui/login/pattern/PatternViewModelTest.kt new file mode 100644 index 0000000..20a9a61 --- /dev/null +++ b/app/src/test/java/de/netalic/peacock/ui/login/pattern/PatternViewModelTest.kt @@ -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 + ) + } +} \ No newline at end of file diff --git a/app/src/test/java/de/netalic/peacock/util/LiveDataUtil.kt b/app/src/test/java/de/netalic/peacock/util/LiveDataUtil.kt new file mode 100644 index 0000000..d34b30b --- /dev/null +++ b/app/src/test/java/de/netalic/peacock/util/LiveDataUtil.kt @@ -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 getValue(liveData: LiveData): T { + val data = arrayOfNulls(1) + val latch = CountDownLatch(1) + val observer = object : Observer { + 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 + } +}