Skip to content

Commit

Permalink
Add DatabaseMocker to mock NewPipeDatabase
Browse files Browse the repository at this point in the history
  • Loading branch information
Stypox committed Dec 12, 2021
1 parent 8a5e2ff commit 7d6688f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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

Expand All @@ -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)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
}
}

0 comments on commit 7d6688f

Please sign in to comment.