From 7d6688f4976d1c998938843f5c29d5db8c0ea7fc Mon Sep 17 00:00:00 2001 From: Stypox Date: Wed, 8 Dec 2021 14:02:16 +0100 Subject: [PATCH] Add DatabaseMocker to mock NewPipeDatabase --- .../playlist/LocalPlaylistManagerTest.kt | 13 ++------ .../schabi/newpipe/testUtil/TestDatabase.kt | 32 +++++++++++++++++++ 2 files changed, 35 insertions(+), 10 deletions(-) create mode 100644 app/src/androidTest/java/org/schabi/newpipe/testUtil/TestDatabase.kt diff --git a/app/src/androidTest/java/org/schabi/newpipe/local/playlist/LocalPlaylistManagerTest.kt b/app/src/androidTest/java/org/schabi/newpipe/local/playlist/LocalPlaylistManagerTest.kt index 0a00b0443..249492d8f 100644 --- a/app/src/androidTest/java/org/schabi/newpipe/local/playlist/LocalPlaylistManagerTest.kt +++ b/app/src/androidTest/java/org/schabi/newpipe/local/playlist/LocalPlaylistManagerTest.kt @@ -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) } diff --git a/app/src/androidTest/java/org/schabi/newpipe/testUtil/TestDatabase.kt b/app/src/androidTest/java/org/schabi/newpipe/testUtil/TestDatabase.kt new file mode 100644 index 000000000..8b1a261be --- /dev/null +++ b/app/src/androidTest/java/org/schabi/newpipe/testUtil/TestDatabase.kt @@ -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 + } + } +}