Initial commit
This initial commit includes HUSH specific changes starting at this commit:
d14637012c
This commit is contained in:
54
app/src/test/java/cash/z/ecc/android/ScratchPad.kt
Normal file
54
app/src/test/java/cash/z/ecc/android/ScratchPad.kt
Normal file
@@ -0,0 +1,54 @@
|
||||
package cash.z.ecc.android
|
||||
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.flow.combine
|
||||
import kotlinx.coroutines.flow.flowOf
|
||||
import kotlinx.coroutines.flow.onEach
|
||||
import kotlinx.coroutines.flow.onStart
|
||||
import kotlinx.coroutines.flow.scanReduce
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import org.junit.Test
|
||||
|
||||
class ScratchPad {
|
||||
|
||||
val t get() = System.currentTimeMillis()
|
||||
var t0 = 0L
|
||||
val Δt get() = t - t0
|
||||
|
||||
@Test
|
||||
fun testMarblesCombine() = runBlocking {
|
||||
var started = false
|
||||
val flow = flowOf(1, 2, 3, 4, 5, 6, 7, 8, 9).onEach {
|
||||
delay(100)
|
||||
if (!started) {
|
||||
t0 = t
|
||||
started = true
|
||||
}
|
||||
println("$Δt\temitting $it")
|
||||
}
|
||||
val flow2 = flowOf("a", "b", "c", "d", "e", "f").onEach { delay(150); println("$Δt\temitting $it") }
|
||||
val flow3 = flowOf("A", "B").onEach { delay(450); println("$Δt\temitting $it") }
|
||||
combine(flow, flow2, flow3) { i, s, t -> "$i$s$t" }.onStart {
|
||||
t0 = t
|
||||
}.collect {
|
||||
// if (!started) {
|
||||
// println("$Δt until first emission")
|
||||
// t0 = t
|
||||
// started = true
|
||||
// }
|
||||
println("$Δt\t$it") // Will print "1a 2a 2b 2c"
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testMarblesScan() = runBlocking {
|
||||
val flow = flowOf(1, 2, 3, 4, 5)
|
||||
|
||||
flow.scanReduce { accumulator, value ->
|
||||
println("was: $accumulator now: $value")
|
||||
value
|
||||
}.collect {
|
||||
println("got $it")
|
||||
}
|
||||
}
|
||||
}
|
||||
112
app/src/test/java/cash/z/ecc/android/SendViewModelTest.kt
Normal file
112
app/src/test/java/cash/z/ecc/android/SendViewModelTest.kt
Normal file
@@ -0,0 +1,112 @@
|
||||
package cash.z.ecc.android
|
||||
|
||||
import cash.z.ecc.android.feedback.Feedback
|
||||
import cash.z.ecc.android.sdk.db.entity.PendingTransaction
|
||||
import cash.z.ecc.android.sdk.db.entity.isCreated
|
||||
import cash.z.ecc.android.sdk.db.entity.isCreating
|
||||
import cash.z.ecc.android.sdk.db.entity.isMined
|
||||
import cash.z.ecc.android.sdk.db.entity.isSubmitSuccess
|
||||
import cash.z.ecc.android.ui.send.SendViewModel
|
||||
import com.nhaarman.mockitokotlin2.verify
|
||||
import com.nhaarman.mockitokotlin2.whenever
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.newSingleThreadContext
|
||||
import kotlinx.coroutines.test.setMain
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import org.mockito.Mock
|
||||
import org.mockito.MockitoAnnotations
|
||||
import org.mockito.Spy
|
||||
|
||||
class SendViewModelTest {
|
||||
|
||||
@Mock lateinit var creatingTx: PendingTransaction
|
||||
@Mock lateinit var createdTx: PendingTransaction
|
||||
@Mock lateinit var submittedTx: PendingTransaction
|
||||
@Mock lateinit var minedTx: PendingTransaction
|
||||
|
||||
@Mock
|
||||
lateinit var feedback: Feedback
|
||||
|
||||
@Spy
|
||||
lateinit var sendViewModel: SendViewModel
|
||||
|
||||
@Before
|
||||
fun setUp() {
|
||||
MockitoAnnotations.initMocks(this)
|
||||
Dispatchers.setMain(newSingleThreadContext("Main thread"))
|
||||
|
||||
whenever(creatingTx.id).thenReturn(7)
|
||||
whenever(creatingTx.submitAttempts).thenReturn(0)
|
||||
|
||||
whenever(createdTx.id).thenReturn(7)
|
||||
whenever(createdTx.raw).thenReturn(byteArrayOf(0x1))
|
||||
|
||||
whenever(submittedTx.id).thenReturn(7)
|
||||
whenever(submittedTx.raw).thenReturn(byteArrayOf(0x1))
|
||||
whenever(submittedTx.submitAttempts).thenReturn(1)
|
||||
|
||||
whenever(minedTx.id).thenReturn(7)
|
||||
whenever(minedTx.raw).thenReturn(byteArrayOf(0x1))
|
||||
whenever(minedTx.submitAttempts).thenReturn(1)
|
||||
whenever(minedTx.minedHeight).thenReturn(500_001)
|
||||
|
||||
sendViewModel.feedback = feedback
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testUpdateMetrics_creating() {
|
||||
// doNothing().whenever(sendViewModel).report(any())
|
||||
|
||||
// assertEquals(true, creatingTx.isCreating())
|
||||
// sendViewModel.updateMetrics(creatingTx)
|
||||
//
|
||||
// verify(sendViewModel).report("7.metric.tx.initialized")
|
||||
// assertEquals(1, sendViewModel.metrics.size)
|
||||
// verifyZeroInteractions(feedback)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testUpdateMetrics_created() {
|
||||
assertEquals(false, createdTx.isCreating())
|
||||
assertEquals(true, createdTx.isCreated())
|
||||
// sendViewModel.updateMetrics(creatingTx)
|
||||
// sendViewModel.updateMetrics(createdTx)
|
||||
// Thread.sleep(100)
|
||||
// println(sendViewModel.metrics)
|
||||
//
|
||||
// verify(sendViewModel).report("7.metric.tx.created")
|
||||
// assertEquals(1, sendViewModel.metrics.size)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testUpdateMetrics_submitted() {
|
||||
assertEquals(false, submittedTx.isCreating())
|
||||
assertEquals(false, submittedTx.isCreated())
|
||||
assertEquals(true, submittedTx.isSubmitSuccess())
|
||||
// sendViewModel.updateMetrics(creatingTx)
|
||||
// sendViewModel.updateMetrics(createdTx)
|
||||
// sendViewModel.updateMetrics(submittedTx)
|
||||
assertEquals(5, sendViewModel.metrics.size)
|
||||
|
||||
Thread.sleep(100)
|
||||
assertEquals(1, sendViewModel.metrics.size)
|
||||
|
||||
verify(feedback).report(sendViewModel.metrics.values.first())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testUpdateMetrics_mined() {
|
||||
assertEquals(true, minedTx.isMined())
|
||||
assertEquals(true, minedTx.isSubmitSuccess())
|
||||
// sendViewModel.updateMetrics(creatingTx)
|
||||
// sendViewModel.updateMetrics(createdTx)
|
||||
// sendViewModel.updateMetrics(submittedTx)
|
||||
// sendViewModel.updateMetrics(minedTx)
|
||||
// assertEquals(7, sendViewModel.metrics.size)
|
||||
//
|
||||
// Thread.sleep(100)
|
||||
// assertEquals(0, sendViewModel.metrics.size)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
mock-maker-inline
|
||||
Reference in New Issue
Block a user