This repository has been archived by the owner on Aug 4, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Review the code, Add pattern view model test closed #25
- Loading branch information
1 parent
e4bd3db
commit 1a159c4
Showing
7 changed files
with
88 additions
and
23 deletions.
There are no files selected for viewing
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
1 change: 0 additions & 1 deletion
1
app/src/main/java/de/netalic/peacock/ui/base/MainHostActivity.kt
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,7 +37,5 @@ class PatternViewModel : BaseViewModel() { | |
mResponse.value = MyResponse.success(ResponseStatus.FAILED) | ||
} | ||
} | ||
|
||
} | ||
|
||
} |
This file was deleted.
Oops, something went wrong.
55 changes: 55 additions & 0 deletions
55
app/src/test/java/de/netalic/peacock/ui/login/pattern/PatternViewModelTest.kt
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
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 | ||
) | ||
} | ||
} |
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
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 | ||
} | ||
} |