Commit 8cf028c2 authored by Shivansh Saini's avatar Shivansh Saini

Fixed ServiceLocator to work with tests

Signed-off-by: Shivansh Saini's avatarShivansh Saini <shivanshs9@gmail.com>
parent 805ab4a2
......@@ -4,7 +4,6 @@ import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Parcel;
import org.videolan.libvlc.Media;
import org.videolan.libvlc.interfaces.IMedia;
import org.videolan.medialibrary.interfaces.AbstractMedialibrary;
import org.videolan.medialibrary.interfaces.media.AbstractAlbum;
......@@ -33,7 +32,9 @@ public class MLServiceLocator {
private static volatile AbstractMedialibrary instance;
public static void setLocatorMode(LocatorMode mode) {
if (instance != null) throw new IllegalStateException("LocatorMode must be set before AbstractMedialibrary initialization");
if (instance != null && mode != sMode) {
throw new IllegalStateException("LocatorMode must be set before AbstractMedialibrary initialization");
}
MLServiceLocator.sMode = mode;
}
......
......@@ -173,4 +173,4 @@ class MediaListActivity : BaseTvActivity(), ITVEventsHandler, CoroutineScope by
@ObsoleteCoroutinesApi
@ExperimentalCoroutinesApi
internal fun MediaListActivity.getViewModel(playlist: MediaLibraryItem) = ViewModelProviders.of(this, PlaylistViewModel.Factory(this, application, playlist)).get(PlaylistViewModel::class.java)
\ No newline at end of file
internal fun MediaListActivity.getViewModel(playlist: MediaLibraryItem) = ViewModelProviders.of(this, PlaylistViewModel.Factory(this, playlist)).get(PlaylistViewModel::class.java)
\ No newline at end of file
......@@ -31,7 +31,6 @@ import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.channels.SendChannel
import kotlinx.coroutines.channels.actor
import kotlinx.coroutines.channels.mapNotNullTo
import org.videolan.libvlc.Media
import org.videolan.libvlc.interfaces.IMedia
import org.videolan.libvlc.util.MediaBrowser
import org.videolan.libvlc.util.MediaBrowser.EventListener
......@@ -41,7 +40,6 @@ import org.videolan.medialibrary.interfaces.media.AbstractMediaWrapper
import org.videolan.medialibrary.media.MediaLibraryItem
import org.videolan.medialibrary.media.Storage
import org.videolan.vlc.R
import org.videolan.vlc.VLCApplication
import org.videolan.vlc.util.*
import java.util.*
......
......@@ -21,7 +21,7 @@
package org.videolan.vlc.providers
import android.content.Context
import org.videolan.libvlc.Media
import org.videolan.libvlc.interfaces.IMedia
import org.videolan.libvlc.util.MediaBrowser
import org.videolan.medialibrary.MLServiceLocator
import org.videolan.medialibrary.interfaces.media.AbstractMediaWrapper
......@@ -39,7 +39,7 @@ class FilePickerProvider(context: Context, dataset: LiveDataset<MediaLibraryItem
mediabrowser?.setIgnoreFileTypes("db,nfo,ini,jpg,jpeg,ljpg,gif,png,pgm,pgmyuv,pbm,pam,tga,bmp,pnm,xpm,xcf,pcx,tif,tiff,lbm,sfv")
}
override suspend fun findMedia(media: Media) = MLServiceLocator.getAbstractMediaWrapper(media)?.takeIf { mw ->
override suspend fun findMedia(media: IMedia) = MLServiceLocator.getAbstractMediaWrapper(media)?.takeIf { mw ->
mw.type == AbstractMediaWrapper.TYPE_DIR || mw.type == AbstractMediaWrapper.TYPE_SUBTITLE
}
......
......@@ -23,9 +23,7 @@ package org.videolan.vlc.providers
import android.content.Context
import android.net.Uri
import android.text.TextUtils
import org.videolan.libvlc.Media
import org.videolan.libvlc.interfaces.IMedia
import org.videolan.medialibrary.interfaces.media.AbstractMediaWrapper
import org.videolan.medialibrary.media.MediaLibraryItem
import org.videolan.medialibrary.media.Storage
import org.videolan.vlc.R
......@@ -61,7 +59,7 @@ class StorageProvider(context: Context, dataset: LiveDataset<MediaLibraryItem>,
dataset.value = storagesList
}
override suspend fun findMedia(media: Media) = media.takeIf { it.isStorage() }?.let { Storage(it.uri) }
override suspend fun findMedia(media: IMedia) = media.takeIf { it.isStorage() }?.let { Storage(it.uri) }
override fun computeHeaders(value: MutableList<MediaLibraryItem>) {}
}
......
......@@ -22,19 +22,19 @@ package org.videolan.vlc.viewmodels
import android.content.Context
import androidx.lifecycle.MutableLiveData
import kotlinx.coroutines.*
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.ObsoleteCoroutinesApi
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.channels.actor
import kotlinx.coroutines.isActive
import kotlinx.coroutines.withContext
import org.videolan.medialibrary.media.MediaLibraryItem
import org.videolan.vlc.util.FilterDelegate
import org.videolan.vlc.util.LiveDataset
import org.videolan.vlc.util.ModelsHelper
import org.videolan.vlc.util.map
import org.videolan.vlc.util.*
private const val TAG = "VLC/BaseModel"
@ExperimentalCoroutinesApi
@ObsoleteCoroutinesApi
abstract class BaseModel<T : MediaLibraryItem>(context: Context) : SortableModel(context) {
abstract class BaseModel<T : MediaLibraryItem>(context: Context, val coroutineContextProvider: CoroutineContextProvider) : SortableModel(context) {
private val filter by lazy(LazyThreadSafetyMode.NONE) { FilterDelegate(dataset) }
......@@ -91,7 +91,7 @@ abstract class BaseModel<T : MediaLibraryItem>(context: Context) : SortableModel
open suspend fun addMedia(mediaList: List<T>) = dataset.add(mediaList)
protected open suspend fun updateItems(mediaList: List<T>) {
dataset.value = withContext(Dispatchers.Default) {
dataset.value = withContext(coroutineContextProvider.Default) {
val list = dataset.value
val iterator = list.listIterator()
while (iterator.hasNext()) {
......
......@@ -23,16 +23,16 @@ package org.videolan.vlc.viewmodels
import android.content.Context
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import org.videolan.medialibrary.interfaces.media.AbstractMediaWrapper
import org.videolan.vlc.util.CoroutineContextProvider
class HistoryModel(context: Context) : MedialibraryModel<AbstractMediaWrapper>(context) {
class HistoryModel(context: Context, coroutineContextProvider: CoroutineContextProvider = CoroutineContextProvider()) : MedialibraryModel<AbstractMediaWrapper>(context, coroutineContextProvider) {
override fun canSortByName() = false
override suspend fun updateList() {
dataset.value = withContext(Dispatchers.Default) { medialibrary.lastMediaPlayed().toMutableList() }
dataset.value = withContext(coroutineContextProvider.Default) { medialibrary.lastMediaPlayed().toMutableList() }
}
fun moveUp(media: AbstractMediaWrapper) {
......
......@@ -24,10 +24,11 @@ import android.content.Context
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.ObsoleteCoroutinesApi
import org.videolan.medialibrary.media.MediaLibraryItem
import org.videolan.vlc.util.CoroutineContextProvider
@ObsoleteCoroutinesApi
@ExperimentalCoroutinesApi
abstract class MedialibraryModel<T : MediaLibraryItem>(context: Context) : BaseModel<T>(context), ICallBackHandler by CallBackDelegate() {
abstract class MedialibraryModel<T : MediaLibraryItem>(context: Context, coroutineContextProvider: CoroutineContextProvider) : BaseModel<T>(context, coroutineContextProvider), ICallBackHandler by CallBackDelegate() {
init {
@Suppress("LeakingThis")
......
......@@ -28,9 +28,10 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.videolan.medialibrary.interfaces.media.AbstractMediaWrapper
import org.videolan.vlc.util.CoroutineContextProvider
class StreamsModel(context: Context) : MedialibraryModel<AbstractMediaWrapper>(context) {
class StreamsModel(context: Context, coroutineContextProvider: CoroutineContextProvider = CoroutineContextProvider()) : MedialibraryModel<AbstractMediaWrapper>(context, coroutineContextProvider) {
val observableSearchText = ObservableField<String>()
init {
......@@ -39,7 +40,7 @@ class StreamsModel(context: Context) : MedialibraryModel<AbstractMediaWrapper>(c
override suspend fun updateList() {
dataset.value = withContext(Dispatchers.Default) { medialibrary.lastStreamsPlayed().toMutableList() }
dataset.value = withContext(coroutineContextProvider.Default) { medialibrary.lastStreamsPlayed().toMutableList() }
}
fun rename(position: Int, name: String) {
......
......@@ -6,7 +6,10 @@ import androidx.databinding.Observable
import androidx.databinding.ObservableBoolean
import androidx.databinding.ObservableField
import androidx.lifecycle.*
import kotlinx.coroutines.*
import kotlinx.coroutines.Job
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.videolan.vlc.R
import org.videolan.vlc.api.NoConnectivityException
import org.videolan.vlc.api.OpenSubtitle
......@@ -14,6 +17,7 @@ import org.videolan.vlc.gui.dialogs.State
import org.videolan.vlc.gui.dialogs.SubtitleItem
import org.videolan.vlc.repository.ExternalSubRepository
import org.videolan.vlc.repository.OpenSubtitleRepository
import org.videolan.vlc.util.CoroutineContextProvider
import org.videolan.vlc.util.FileUtils
import org.videolan.vlc.util.Settings
import java.io.File
......@@ -21,7 +25,7 @@ import java.util.*
private const val LAST_USED_LANGUAGES = "last_used_subtitles"
class SubtitlesModel(private val context: Context, private val mediaUri: Uri): ScopedModel() {
class SubtitlesModel(private val context: Context, private val mediaUri: Uri, val coroutineContextProvider: CoroutineContextProvider = CoroutineContextProvider()): ScopedModel() {
val observableSearchName = ObservableField<String>()
val observableSearchEpisode = ObservableField<String>()
val observableSearchSeason = ObservableField<String>()
......@@ -84,11 +88,11 @@ class SubtitlesModel(private val context: Context, private val mediaUri: Uri): S
}
}
private suspend fun merge(downloadedResult: List<SubtitleItem>?, downloadingResult: List<SubtitleItem>?): List<SubtitleItem> = withContext(Dispatchers.Default) {
private suspend fun merge(downloadedResult: List<SubtitleItem>?, downloadingResult: List<SubtitleItem>?): List<SubtitleItem> = withContext(coroutineContextProvider.Default) {
downloadedResult.orEmpty() + downloadingResult?.toList().orEmpty()
}
private suspend fun updateListState(apiResultLiveData: List<OpenSubtitle>?, history: List<SubtitleItem>?): MutableList<SubtitleItem> = withContext(Dispatchers.Default) {
private suspend fun updateListState(apiResultLiveData: List<OpenSubtitle>?, history: List<SubtitleItem>?): MutableList<SubtitleItem> = withContext(coroutineContextProvider.Default) {
val list = mutableListOf<SubtitleItem>()
apiResultLiveData?.forEach { openSubtitle ->
val exist = history?.find { it.idSubtitle == openSubtitle.idSubtitle }
......@@ -138,7 +142,7 @@ class SubtitlesModel(private val context: Context, private val mediaUri: Uri): S
searchJob = launch {
try {
val subs = if (byHash) {
withContext(Dispatchers.IO) {
withContext(coroutineContextProvider.IO) {
val videoFile = File(mediaUri.path)
val hash = FileUtils.computeHash(videoFile)
val fileLength = videoFile.length()
......
......@@ -31,6 +31,7 @@ import org.videolan.medialibrary.interfaces.media.AbstractMediaWrapper
import org.videolan.medialibrary.media.MediaLibraryItem
import org.videolan.vlc.providers.*
import org.videolan.vlc.repository.DirectoryRepository
import org.videolan.vlc.util.CoroutineContextProvider
import org.videolan.vlc.viewmodels.BaseModel
import org.videolan.vlc.viewmodels.tv.TvBrowserModel
......@@ -41,7 +42,7 @@ const val TYPE_STORAGE = 3
@ObsoleteCoroutinesApi
@ExperimentalCoroutinesApi
open class BrowserModel(context: Context, val url: String?, type: Int, showHiddenFiles: Boolean, private val showDummyCategory: Boolean) : BaseModel<MediaLibraryItem>(context), TvBrowserModel {
open class BrowserModel(context: Context, val url: String?, type: Int, showHiddenFiles: Boolean, private val showDummyCategory: Boolean, coroutineContextProvider: CoroutineContextProvider = CoroutineContextProvider()) : BaseModel<MediaLibraryItem>(context, coroutineContextProvider), TvBrowserModel {
override var currentItem: MediaLibraryItem? = null
override var nbColumns: Int = 0
......
......@@ -23,8 +23,9 @@ package org.videolan.vlc.viewmodels.browser
import android.content.Context
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import org.videolan.vlc.util.CoroutineContextProvider
class NetworkModel(context: Context, url: String? = null, showHiddenFiles: Boolean) : BrowserModel(context, url, TYPE_NETWORK, showHiddenFiles, true) {
class NetworkModel(context: Context, url: String? = null, showHiddenFiles: Boolean, coroutineContextProvider: CoroutineContextProvider = CoroutineContextProvider()) : BrowserModel(context, url, TYPE_NETWORK, showHiddenFiles, true, coroutineContextProvider) {
class Factory(val context: Context, val url: String?, private val showHiddenFiles: Boolean): ViewModelProvider.NewInstanceFactory() {
override fun <T : ViewModel> create(modelClass: Class<T>): T {
......
......@@ -24,7 +24,10 @@ import android.content.Context
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.ViewModelProviders
import kotlinx.coroutines.*
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.ObsoleteCoroutinesApi
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.videolan.medialibrary.interfaces.media.AbstractFolder
import org.videolan.medialibrary.media.MediaLibraryItem
import org.videolan.vlc.gui.folders.FoldersFragment
......@@ -32,22 +35,23 @@ import org.videolan.vlc.media.MediaUtils
import org.videolan.vlc.media.getAll
import org.videolan.vlc.providers.medialibrary.FoldersProvider
import org.videolan.vlc.providers.medialibrary.MedialibraryProvider
import org.videolan.vlc.util.CoroutineContextProvider
import org.videolan.vlc.viewmodels.MedialibraryViewModel
@ExperimentalCoroutinesApi
@ObsoleteCoroutinesApi
class FoldersViewModel(context: Context, val type : Int) : MedialibraryViewModel(context) {
class FoldersViewModel(context: Context, val type : Int, val coroutineContextProvider: CoroutineContextProvider = CoroutineContextProvider()) : MedialibraryViewModel(context) {
val provider = FoldersProvider(context, this, type)
override val providers: Array<MedialibraryProvider<out MediaLibraryItem>> = arrayOf(provider)
suspend fun play(position: Int) {
val list = withContext(Dispatchers.IO) { provider.pagedList.value?.get(position)?.getAll()}
val list = withContext(coroutineContextProvider.IO) { provider.pagedList.value?.get(position)?.getAll()}
list?.let { MediaUtils.openList(context, it, 0) }
}
suspend fun append(position: Int) {
val list = withContext(Dispatchers.IO) { provider.pagedList.value?.get(position)?.getAll()}
val list = withContext(coroutineContextProvider.IO) { provider.pagedList.value?.get(position)?.getAll()}
list?.let { MediaUtils.appendMedia(context, it) }
}
......
......@@ -23,7 +23,7 @@ class HistoryModelTest : BaseTest() {
override fun beforeTest() {
super.beforeTest()
mediaLibrary.clearHistory()
historyModel = HistoryModel(application, TestCoroutineContextProvider())
historyModel = HistoryModel(context, TestCoroutineContextProvider())
}
@Test
......
......@@ -12,7 +12,6 @@ import kotlinx.coroutines.ObsoleteCoroutinesApi
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TemporaryFolder
import org.robolectric.RuntimeEnvironment
import org.videolan.vlc.BaseTest
import org.videolan.vlc.R
import org.videolan.vlc.api.NoConnectivityException
......@@ -25,7 +24,6 @@ import org.videolan.vlc.util.FileUtils
import org.videolan.vlc.util.TestCoroutineContextProvider
import org.videolan.vlc.util.TestUtil
import org.videolan.vlc.util.applyMock
import java.io.File
import java.io.IOException
import java.util.concurrent.TimeUnit
......@@ -63,7 +61,7 @@ class SubtitlesModelTest : BaseTest() {
override fun beforeTest() {
super.beforeTest()
mediaPath = temporaryFolder.newFile("fake_media").path
subtitlesModel = SubtitlesModel(application, Uri.parse(mediaPath), TestCoroutineContextProvider())
subtitlesModel = SubtitlesModel(context, Uri.parse(mediaPath), coroutineContextProvider = TestCoroutineContextProvider())
}
@Test
......
......@@ -30,7 +30,7 @@ class AlbumSongsViewModelTest : BaseTest() {
parent = MLServiceLocator.getAbstractArtist(ds.uuid, name, "", "", "")
else
parent = MLServiceLocator.getAbstractGenre(ds.uuid, name)
albumSongsViewModel = AlbumSongsViewModel(context, application, parent)
albumSongsViewModel = AlbumSongsViewModel(context, parent)
}
private fun createDummyAudios(count: Int, title: String): List<Long> = (1..count).map {
......
......@@ -5,8 +5,6 @@ import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.ObsoleteCoroutinesApi
import org.junit.Assert.*
import org.junit.Test
import org.videolan.medialibrary.MLServiceLocator
import org.videolan.medialibrary.interfaces.AbstractMedialibrary
import org.videolan.medialibrary.interfaces.media.AbstractMediaWrapper
import org.videolan.medialibrary.stubs.StubDataSource
import org.videolan.vlc.BaseTest
......@@ -20,7 +18,7 @@ class AudioBrowserViewModelTest : BaseTest() {
override fun beforeTest() {
super.beforeTest()
StubDataSource.getInstance().resetData()
audioBrowserViewModel = AudioBrowserViewModel(context, application)
audioBrowserViewModel = AudioBrowserViewModel(context)
}
private fun createDummyAudios(count: Int, title: String): List<Long> = (1..count).map {
......
......@@ -8,7 +8,7 @@ import org.junit.Test
import org.videolan.medialibrary.interfaces.media.AbstractFolder
import org.videolan.medialibrary.stubs.StubDataSource
import org.videolan.vlc.BaseTest
import org.videolan.vlc.util.MEDIALIBRARY_PAGE_SIZE
import org.videolan.vlc.util.TestCoroutineContextProvider
@ObsoleteCoroutinesApi
@ExperimentalCoroutinesApi
......@@ -22,7 +22,7 @@ class FoldersViewModelTest: BaseTest() {
}
internal fun setupViewModel() {
foldersViewModel = FoldersViewModel(context, application, AbstractFolder.TYPE_FOLDER_VIDEO)
foldersViewModel = FoldersViewModel(context, AbstractFolder.TYPE_FOLDER_VIDEO, TestCoroutineContextProvider())
}
@Test
......
......@@ -5,7 +5,6 @@ import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.ObsoleteCoroutinesApi
import org.junit.Assert.*
import org.junit.Test
import org.videolan.medialibrary.MLServiceLocator
import org.videolan.medialibrary.interfaces.media.AbstractMediaWrapper
import org.videolan.medialibrary.interfaces.media.AbstractPlaylist
import org.videolan.medialibrary.stubs.StubDataSource
......@@ -33,7 +32,7 @@ class PlaylistViewModelTest : BaseTest() {
private fun setupViewModel(playlist: String, mediaIds: List<Long>? = null) {
parent = medialibrary.createPlaylist(playlist).apply { if (mediaIds != null) append(mediaIds) }
playlistViewModel = PlaylistViewModel(context, application, parent)
playlistViewModel = PlaylistViewModel(context, parent)
}
@Test
......
......@@ -3,12 +3,11 @@ package org.videolan.vlc.viewmodels.mobile
import com.jraska.livedata.test
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.ObsoleteCoroutinesApi
import org.videolan.vlc.BaseTest
import org.junit.Assert.*
import org.junit.Test
import org.videolan.medialibrary.MLServiceLocator
import org.videolan.medialibrary.stubs.StubDataSource
import org.videolan.vlc.BaseTest
import org.videolan.vlc.util.MEDIALIBRARY_PAGE_SIZE
@ObsoleteCoroutinesApi
......@@ -20,7 +19,7 @@ class PlaylistsViewModelTest : BaseTest() {
override fun beforeTest() {
super.beforeTest()
StubDataSource.getInstance().resetData()
playlistsViewModel = PlaylistsViewModel(context, application)
playlistsViewModel = PlaylistsViewModel(context)
}
private fun createDummyPlaylists(count: Int) {
......
......@@ -21,7 +21,7 @@ class VideosViewModelTest : BaseTest() {
}
private fun setupViewModel(folder: AbstractFolder?) {
videosViewModel = VideosViewModel(context, application, folder)
videosViewModel = VideosViewModel(context, folder)
}
@Test
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment