-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Add instrumented tests for LocalPlaylistManager.createPlaylist #5531
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
85 changes: 85 additions & 0 deletions
85
app/src/androidTest/java/org/schabi/newpipe/local/playlist/LocalPlaylistManagerTest.kt
This file contains hidden or 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,85 @@ | ||
| 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 | ||
| import org.junit.Test | ||
| 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.TrampolineSchedulerRule | ||
| import java.util.concurrent.TimeUnit | ||
|
|
||
| class LocalPlaylistManagerTest { | ||
|
|
||
| private lateinit var manager: LocalPlaylistManager | ||
| private lateinit var database: AppDatabase | ||
|
|
||
| @get:Rule | ||
| val trampolineScheduler = TrampolineSchedulerRule() | ||
|
|
||
| @get:Rule | ||
| val timeout = Timeout(10, TimeUnit.SECONDS) | ||
|
|
||
| @Before | ||
| fun setup() { | ||
| database = Room.inMemoryDatabaseBuilder( | ||
| ApplicationProvider.getApplicationContext(), | ||
| AppDatabase::class.java | ||
| ) | ||
| .allowMainThreadQueries() | ||
| .build() | ||
|
|
||
| manager = LocalPlaylistManager(database) | ||
| } | ||
|
|
||
| @After | ||
| fun cleanUp() { | ||
| database.close() | ||
| } | ||
|
|
||
| @Test | ||
| fun createPlaylist() { | ||
| val stream = StreamEntity( | ||
| serviceId = 1, url = "https://newpipe.net/", title = "title", | ||
| streamType = StreamType.VIDEO_STREAM, duration = 1, uploader = "uploader" | ||
| ) | ||
|
|
||
| val result = manager.createPlaylist("name", listOf(stream)) | ||
|
|
||
| // This should not behave like this. | ||
| // Currently list of all stream ids is returned instead of playlist id | ||
| result.test().await().assertValue(listOf(1L)) | ||
| } | ||
|
|
||
| @Test | ||
| fun createPlaylist_emptyPlaylistMustReturnEmpty() { | ||
| val result = manager.createPlaylist("name", emptyList()) | ||
|
|
||
| // This should not behave like this. | ||
| // It should throw an error because currently the result is null | ||
| result.test().await().assertComplete() | ||
| manager.playlists.test().awaitCount(1).assertValue(emptyList()) | ||
| } | ||
|
|
||
| @Test() | ||
| fun createPlaylist_nonExistentStreamsAreUpserted() { | ||
| val stream = StreamEntity( | ||
| serviceId = 1, url = "https://newpipe.net/", title = "title", | ||
| streamType = StreamType.VIDEO_STREAM, duration = 1, uploader = "uploader" | ||
| ) | ||
| database.streamDAO().insert(stream) | ||
| val upserted = StreamEntity( | ||
| serviceId = 1, url = "https://newpipe.net/2", title = "title2", | ||
| streamType = StreamType.VIDEO_STREAM, duration = 1, uploader = "uploader" | ||
| ) | ||
|
|
||
| val result = manager.createPlaylist("name", listOf(stream, upserted)) | ||
|
|
||
| result.test().await().assertComplete() | ||
| database.streamDAO().all.test().awaitCount(1).assertValue(listOf(stream, upserted)) | ||
| } | ||
| } | ||
37 changes: 37 additions & 0 deletions
37
app/src/androidTest/java/org/schabi/newpipe/testUtil/TrampolineSchedulerRule.kt
This file contains hidden or 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,37 @@ | ||
| package org.schabi.newpipe.testUtil | ||
|
|
||
| import io.reactivex.rxjava3.android.plugins.RxAndroidPlugins | ||
| import io.reactivex.rxjava3.plugins.RxJavaPlugins | ||
| import io.reactivex.rxjava3.schedulers.Schedulers | ||
| import org.junit.rules.TestRule | ||
| import org.junit.runner.Description | ||
| import org.junit.runners.model.Statement | ||
|
|
||
| /** | ||
| * Always run on [Schedulers.trampoline]. | ||
| * This executes the task in the current thread in FIFO manner. | ||
| * This ensures that tasks are run quickly inside the tests | ||
| * and not scheduled away to another thread for later execution | ||
| */ | ||
| class TrampolineSchedulerRule : TestRule { | ||
|
|
||
| private val scheduler = Schedulers.trampoline() | ||
XiangRongLin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| override fun apply(base: Statement, description: Description): Statement = | ||
| object : Statement() { | ||
| override fun evaluate() { | ||
| try { | ||
| RxJavaPlugins.setComputationSchedulerHandler { scheduler } | ||
| RxJavaPlugins.setIoSchedulerHandler { scheduler } | ||
| RxJavaPlugins.setNewThreadSchedulerHandler { scheduler } | ||
| RxJavaPlugins.setSingleSchedulerHandler { scheduler } | ||
| RxAndroidPlugins.setInitMainThreadSchedulerHandler { scheduler } | ||
|
|
||
| base.evaluate() | ||
| } finally { | ||
| RxJavaPlugins.reset() | ||
| RxAndroidPlugins.reset() | ||
| } | ||
| } | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.