forked from TeamNewPipe/NewPipe
-
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.
Add DatabaseMocker to mock NewPipeDatabase
- Loading branch information
Showing
2 changed files
with
35 additions
and
10 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
32 changes: 32 additions & 0 deletions
32
app/src/androidTest/java/org/schabi/newpipe/testUtil/TestDatabase.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,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 | ||
} | ||
} | ||
} |