Add DatabaseMocker to mock NewPipeDatabase

This commit is contained in:
Stypox 2021-12-08 14:02:16 +01:00
parent 8a5e2ffa57
commit 7d6688f497
No known key found for this signature in database
GPG Key ID: 4BDF1B40A49FDD23
2 changed files with 35 additions and 10 deletions

View File

@ -1,7 +1,5 @@
package org.schabi.newpipe.local.playlist
import androidx.room.Room
import androidx.test.core.app.ApplicationProvider
import org.junit.After
import org.junit.Before
import org.junit.Rule
@ -10,6 +8,7 @@ import org.junit.rules.Timeout
import org.schabi.newpipe.database.AppDatabase
import org.schabi.newpipe.database.stream.model.StreamEntity
import org.schabi.newpipe.extractor.stream.StreamType
import org.schabi.newpipe.testUtil.TestDatabase
import org.schabi.newpipe.testUtil.TrampolineSchedulerRule
import java.util.concurrent.TimeUnit
@ -22,17 +21,11 @@ class LocalPlaylistManagerTest {
val trampolineScheduler = TrampolineSchedulerRule()
@get:Rule
val timeout = Timeout(10, TimeUnit.SECONDS)
val timeout = Timeout(1, TimeUnit.SECONDS)
@Before
fun setup() {
database = Room.inMemoryDatabaseBuilder(
ApplicationProvider.getApplicationContext(),
AppDatabase::class.java
)
.allowMainThreadQueries()
.build()
database = TestDatabase.createReplacingNewPipeDatabase()
manager = LocalPlaylistManager(database)
}

View File

@ -0,0 +1,32 @@
package org.schabi.newpipe.testUtil
import androidx.room.Room
import androidx.test.core.app.ApplicationProvider
import org.junit.Assert.assertSame
import org.schabi.newpipe.NewPipeDatabase
import org.schabi.newpipe.database.AppDatabase
class TestDatabase {
companion object {
fun createReplacingNewPipeDatabase(): AppDatabase {
val database = Room.inMemoryDatabaseBuilder(
ApplicationProvider.getApplicationContext(),
AppDatabase::class.java
)
.allowMainThreadQueries()
.build()
val databaseField = NewPipeDatabase::class.java.getDeclaredField("databaseInstance")
databaseField.isAccessible = true
databaseField.set(NewPipeDatabase::class, database)
assertSame(
"Mocking database failed!",
database,
NewPipeDatabase.getInstance(ApplicationProvider.getApplicationContext())
)
return database
}
}
}