Get rid of the livedata.testing library.

- The com.jraska.livedata:testing-ktx dependency was introduced to be able to test LiveData, however that dependency has not been migrated away from JCenter (to mavenCentral). Also, we are able to provide a similar functionality with an extension function, and that is what I did.
This commit is contained in:
Severiano Jaramillo 2021-03-27 17:52:10 -07:00
parent 7492bc9774
commit c8a2929c54
6 changed files with 28 additions and 35 deletions

View file

@ -131,7 +131,6 @@ dependencies {
androidTestImplementation "androidx.room:room-testing:$room_version"
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
androidTestImplementation 'com.jraska.livedata:testing-ktx:1.0.0'
}
// Added to avoid the compilation problem due to a duplicate ListenableFuture library

View file

@ -23,20 +23,18 @@ import java.util.concurrent.CountDownLatch
import java.util.concurrent.TimeUnit
object LiveDataTestUtil {
fun <T> getValue(liveData: LiveData<T>): T {
val data = arrayOfNulls<Any>(1)
fun <T> LiveData<T>.blockingObserve(): T? {
var value: T? = null
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)
@Suppress("UNCHECKED_CAST")
return data[0] as T
val observer = Observer<T> { t ->
value = t
latch.countDown()
}
observeForever(observer)
latch.await(2, TimeUnit.SECONDS)
return value
}
}

View file

@ -6,14 +6,11 @@ import androidx.arch.core.executor.testing.InstantTaskExecutorRule
import androidx.room.Room
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.jraska.livedata.test
import cy.agorise.bitsybitshareswallet.LiveDataTestUtil.blockingObserve
import cy.agorise.bitsybitshareswallet.database.BitsyDatabase
import cy.agorise.bitsybitshareswallet.database.entities.EquivalentValue
import cy.agorise.bitsybitshareswallet.database.entities.Transfer
import org.junit.After
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.*
import org.junit.runner.RunWith
import java.io.IOException
@ -111,13 +108,12 @@ class TransfersTests {
"1.2.1029856",
98,
"1.3.120",
"",
1000)
"")
db.transferDao().insert(t1)
db.transferDao().insert(t2)
db.transferDao().getTransfersWithMissingBtsValue()
.test()
.assertHasValue()
.assertValue { transfer -> transfer.id == "1.11.702181910" }
// transfer should be t2
val transfer = db.transferDao().getTransfersWithMissingBtsValue().blockingObserve()
Assert.assertEquals("1.11.684483739", transfer?.id)
}
}
}

View file

@ -19,16 +19,18 @@ data class Transfer (
@ColumnInfo(name = "memo") val memo: String,
@ColumnInfo(name = "bts_value") var btsValue: Long? = NOT_CALCULATED
){
companion object {
// Constant used to specify an uninitialized BTS equivalent value
val NOT_CALCULATED: Long? = -1L
// Constant used to specify a BTS equivalent value whose calculation returned an error
val ERROR: Long? = -2L
}
init {
if(transferAssetId == "1.3.0"){
// If the transferred asset is BTS, we can fill the btsValue field immediately
btsValue = transferAmount
}
}
companion object {
// Constant used to specify an uninitialized BTS equivalent value
const val NOT_CALCULATED: Long = -1L
// Constant used to specify a BTS equivalent value whose calculation returned an error
const val ERROR: Long = -2L
}
}

View file

@ -43,7 +43,7 @@ class TransferViewModel(application: Application) : AndroidViewModel(application
mTransferRepository.update(transfer)
}
fun updateBtsValue(transfer: Transfer, value: Long?) {
fun updateBtsValue(transfer: Transfer, value: Long) {
transfer.btsValue = value
mTransferRepository.update(transfer)
}

View file

@ -38,8 +38,6 @@ allprojects {
includeModule("me.dm7.barcodescanner", "zxing")
includeModule("me.dm7.barcodescanner", "core")
includeModule("com.andrognito.patternlockview", "patternlockview")
includeModule("com.jraska.livedata", "testing-ktx")
includeModule("com.jraska.livedata", "testing")
}
}
}