Commit cd274023 authored by Shivansh Saini's avatar Shivansh Saini

Fixed all Unit tests

Signed-off-by: Shivansh Saini's avatarShivansh Saini <shivanshs9@gmail.com>
parent cea836bd
Pipeline #9022 passed with stage
in 50 minutes and 29 seconds
......@@ -46,7 +46,7 @@ public class StubArtist extends AbstractArtist {
ArrayList<AbstractAlbum> results = new ArrayList<>();
for (AbstractAlbum album : dt.mAlbums) {
if (album.getDescription().equals(this.getTitle()) &&
album.getTitle().equals(query)) {
album.getTitle().contains(query)) {
results.add(album);
}
}
......
......@@ -119,7 +119,7 @@ public class StubDataSource {
public AbstractMediaWrapper addMediaWrapper(String mrl, String title, int type) {
AbstractMediaWrapper media = MLServiceLocator.getAbstractMediaWrapper(getUUID(), mrl, 0L, 280224L, type,
title, title, "Artisto", "Jazz", "XYZ CD1", "", 0, 0, baseMrl + title, -2,
title, title, "Artisto", "Jazz", "XYZ CD1", "CrazyArtists", 0, 0, baseMrl + title, -2,
1, 1, 0, 1547452796L, 0L, true);
if (type == AbstractMediaWrapper.TYPE_ALL) type = media.getType();
if (type == AbstractMediaWrapper.TYPE_VIDEO) addVideo(media);
......
......@@ -287,11 +287,9 @@ public class StubMedialibrary extends AbstractMedialibrary {
return playlist;
}
public void pauseBackgroundOperations() {
}
public void pauseBackgroundOperations() {}
public void resumeBackgroundOperations() {
}
public void resumeBackgroundOperations() {}
public void reload() {
Log.e(TAG, "reload: no entrypoint");
......@@ -305,11 +303,9 @@ public class StubMedialibrary extends AbstractMedialibrary {
onBackgroundTasksIdleChanged(true);
}
public void forceParserRetry() {
}
public void forceParserRetry() {}
public void forceRescan() {
}
public void forceRescan() {}
public AbstractMediaWrapper[] lastMediaPlayed() {
ArrayList<AbstractMediaWrapper> results = new ArrayList<>();
......@@ -437,8 +433,7 @@ public class StubMedialibrary extends AbstractMedialibrary {
return getFolders(type, 0, false, 0, 0).length;
}
public void requestThumbnail(long id) {
}
public void requestThumbnail(long id) {}
public boolean increasePlayCount(long mediaId) {
for (int i = 0; i < dt.mVideoMediaWrappers.size(); i++) {
......
......@@ -7,7 +7,7 @@ import androidx.test.runner.AndroidJUnit4
import org.junit.Before
import org.junit.Rule
import org.junit.runner.RunWith
import org.videolan.medialibrary.MLServiceLocator
import org.videolan.vlc.util.TestCoroutineContextProvider
@RunWith(AndroidJUnit4::class)
......@@ -22,7 +22,7 @@ abstract class BaseUITest {
@Before
fun init() {
context.startMedialibrary()
context.startMedialibrary(coroutineContextProvider = TestCoroutineContextProvider())
beforeTest()
}
......
......@@ -14,6 +14,7 @@ import androidx.annotation.ColorInt
import androidx.annotation.DrawableRes
import androidx.annotation.IdRes
import androidx.appcompat.view.menu.ActionMenuItemView
import androidx.core.content.ContextCompat
import androidx.databinding.ViewDataBinding
import androidx.recyclerview.widget.RecyclerView
import androidx.test.espresso.matcher.BoundedMatcher
......@@ -237,9 +238,9 @@ fun withImageDrawable(resourceId: Int): Matcher<View> {
}
}
private fun sameBitmap(context: Context, drawable: Drawable?, resourceId: Int): Boolean {
private fun sameBitmap(context: Context, drawable: Drawable?, @DrawableRes resourceId: Int): Boolean {
var drawable = drawable
var otherDrawable = context.resources.getDrawable(resourceId)
var otherDrawable = ContextCompat.getDrawable(context, resourceId)
if (drawable == null || otherDrawable == null) {
return false
}
......
......@@ -3,11 +3,12 @@ package org.videolan.vlc.gui
import android.content.Intent
import android.widget.EditText
import androidx.test.espresso.Espresso
import androidx.test.espresso.Espresso.*
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.Espresso.openActionBarOverflowOrOptionsMenu
import androidx.test.espresso.action.ViewActions.*
import androidx.test.espresso.assertion.ViewAssertions.*
import androidx.test.espresso.contrib.DrawerActions.*
import androidx.test.espresso.matcher.RootMatchers.*
import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.contrib.DrawerActions.open
import androidx.test.espresso.matcher.RootMatchers.isPlatformPopup
import androidx.test.espresso.matcher.ViewMatchers.*
import androidx.test.rule.ActivityTestRule
import com.google.android.material.internal.NavigationMenuItemView
......@@ -15,14 +16,14 @@ import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.ObsoleteCoroutinesApi
import org.hamcrest.Matchers.*
import org.junit.After
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.videolan.medialibrary.interfaces.AbstractMedialibrary
import org.videolan.vlc.BaseUITest
import org.videolan.vlc.R
import org.videolan.vlc.*
import org.videolan.vlc.gui.dialogs.SavePlaylistDialog
import org.videolan.vlc.util.CoroutineContextProvider
import org.videolan.vlc.util.EXTRA_TARGET
import org.videolan.vlc.util.TestCoroutineContextProvider
@ObsoleteCoroutinesApi
@ExperimentalCoroutinesApi
......@@ -34,6 +35,9 @@ class PlaylistFragmentUITest: BaseUITest() {
lateinit var activity: MainActivity
override fun beforeTest() {
SavePlaylistDialog.overrideCreator = false
SavePlaylistDialog.registerCreator(clazz = CoroutineContextProvider::class.java) { TestCoroutineContextProvider() }
val intent = Intent().apply {
putExtra(EXTRA_TARGET, R.id.nav_playlists)
}
......@@ -80,10 +84,6 @@ class PlaylistFragmentUITest: BaseUITest() {
onView(withId(R.id.dialog_playlist_save))
.perform(click())
// Because playlist is saved with IO dispatcher.
// TODO: Once tests_vm is merged, I'll update the WorkersKt to use CoroutineContextProvider and remove this hack.
Thread.sleep(2000)
// Navigate back to playlists view
onView(withId(R.id.root_container))
.perform(open())
......
......@@ -455,12 +455,12 @@ fun Context.rescan() {
ContextCompat.startForegroundService(this, Intent(ACTION_FORCE_RELOAD, null, this, MediaParsingService::class.java))
}
fun Context.startMedialibrary(firstRun: Boolean = false, upgrade: Boolean = false, parse: Boolean = true) = AppScope.launch {
fun Context.startMedialibrary(firstRun: Boolean = false, upgrade: Boolean = false, parse: Boolean = true, coroutineContextProvider: CoroutineContextProvider = CoroutineContextProvider()) = AppScope.launch {
if (AbstractMedialibrary.getInstance().isStarted || !Permissions.canReadStorage(this@startMedialibrary)) return@launch
val prefs = withContext(Dispatchers.IO) { Settings.getInstance(this@startMedialibrary) }
val prefs = withContext(coroutineContextProvider.IO) { Settings.getInstance(this@startMedialibrary) }
val scanOpt = if (Settings.showTvUi) ML_SCAN_ON else prefs.getInt(KEY_MEDIALIBRARY_SCAN, -1)
if (parse && scanOpt == -1) {
if (dbExists()) prefs.edit().putInt(KEY_MEDIALIBRARY_SCAN, ML_SCAN_ON).apply()
if (dbExists(coroutineContextProvider)) prefs.edit().putInt(KEY_MEDIALIBRARY_SCAN, ML_SCAN_ON).apply()
}
val intent = Intent(ACTION_INIT, null, this@startMedialibrary, MediaParsingService::class.java)
ContextCompat.startForegroundService(this@startMedialibrary, intent
......@@ -469,7 +469,7 @@ fun Context.startMedialibrary(firstRun: Boolean = false, upgrade: Boolean = fals
.putExtra(EXTRA_PARSE, parse && scanOpt != ML_SCAN_OFF))
}
private suspend fun Context.dbExists() = withContext(Dispatchers.IO) {
private suspend fun Context.dbExists(coroutineContextProvider: CoroutineContextProvider = CoroutineContextProvider()) = withContext(coroutineContextProvider.IO) {
File(getDir("db", Context.MODE_PRIVATE).toString() + AbstractMedialibrary.VLC_MEDIA_DB_NAME).exists()
}
......
......@@ -36,7 +36,10 @@ import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.bottomsheet.BottomSheetBehavior.STATE_EXPANDED
import com.google.android.material.textfield.TextInputLayout
import kotlinx.coroutines.*
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.videolan.medialibrary.Tools
import org.videolan.medialibrary.interfaces.AbstractMedialibrary
import org.videolan.medialibrary.interfaces.media.AbstractMediaWrapper
......@@ -45,6 +48,8 @@ import org.videolan.medialibrary.media.MediaLibraryItem
import org.videolan.tools.isStarted
import org.videolan.vlc.R
import org.videolan.vlc.gui.SimpleAdapter
import org.videolan.vlc.util.CoroutineContextProvider
import org.videolan.vlc.util.DependencyProvider
import java.util.*
class SavePlaylistDialog : VLCBottomSheetDialogFragment(), View.OnClickListener,
......@@ -64,8 +69,15 @@ class SavePlaylistDialog : VLCBottomSheetDialogFragment(), View.OnClickListener,
private lateinit var medialibrary: AbstractMedialibrary
private var currentPLaylist: AbstractPlaylist? = null
private val coroutineContextProvider: CoroutineContextProvider
override fun initialFocusedView(): View = listView
init {
SavePlaylistDialog.registerCreator { CoroutineContextProvider() }
coroutineContextProvider = SavePlaylistDialog.get(0)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
medialibrary = AbstractMedialibrary.getInstance()
......@@ -122,7 +134,7 @@ class SavePlaylistDialog : VLCBottomSheetDialogFragment(), View.OnClickListener,
}
private fun savePlaylist() = launch{
withContext(Dispatchers.IO) {
withContext(coroutineContextProvider.IO) {
val name = editText?.text?.toString()?.trim { it <= ' ' } ?: return@withContext
val addTracks = !Tools.isArrayEmpty(newTrack)
var playlist = if (currentPLaylist?.title == name) {
......@@ -172,7 +184,7 @@ class SavePlaylistDialog : VLCBottomSheetDialogFragment(), View.OnClickListener,
editText?.setText(item.title)
}
companion object {
companion object: DependencyProvider<Any>() {
val TAG = "VLC/SavePlaylistDialog"
......
......@@ -12,9 +12,8 @@ import androidx.localbroadcastmanager.content.LocalBroadcastManager
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.channels.actor
import org.videolan.libvlc.*
import org.videolan.libvlc.interfaces.IMedia
import org.videolan.libvlc.interfaces.IMediaFactory
import org.videolan.libvlc.MediaPlayer
import org.videolan.libvlc.RendererItem
import org.videolan.libvlc.util.AndroidUtil
import org.videolan.medialibrary.MLServiceLocator
import org.videolan.medialibrary.interfaces.AbstractMedialibrary
......@@ -67,7 +66,7 @@ class PlaylistManager(val service: PlaybackService) : MediaWrapperList.EventList
private var entryUrl : String? = null
val abRepeat by lazy(LazyThreadSafetyMode.NONE) { MutableLiveData<ABRepeat>().apply { value = ABRepeat() } }
internal val mMediaFactory = FactoryManager.getFactory(IMediaFactory.factoryId) as IMediaFactory
private val mediaFactory = FactoryManager.getFactory(IMediaFactory.factoryId) as IMediaFactory
fun hasCurrentMedia() = isValidPosition(currentIndex)
......@@ -298,7 +297,7 @@ class PlaylistManager(val service: PlaybackService) : MediaWrapperList.EventList
return
}
val start = getStartTime(mw)
val media = mMediaFactory.getFromUri(VLCInstance.get(service), uri)
val media = mediaFactory.getFromUri(VLCInstance.get(service), uri)
media.addOption(":start-time=${start/1000L}")
VLCOptions.setMediaOptions(media, ctx, flags or mw.flags)
/* keeping only video during benchmark */
......
......@@ -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.interfaces.IMedia
import org.videolan.libvlc.util.MediaBrowser
import org.videolan.libvlc.util.MediaBrowser.EventListener
import org.videolan.medialibrary.MLServiceLocator
......@@ -73,7 +72,7 @@ abstract class BrowserProvider(
private val completionHandler : CompletionHandler = object : CompletionHandler {
override fun invoke(cause: Throwable?) {
if (mediabrowser != null) AppScope.launch(Dispatchers.IO) { // use global scope because current is cancelled
if (mediabrowser != null) AppScope.launch(coroutineContextProvider.IO) { // use global scope because current is cancelled
mediabrowser?.release()
mediabrowser = null
}
......@@ -89,8 +88,8 @@ abstract class BrowserProvider(
BrowseRoot -> browseRootImpl()
Refresh -> browseImpl(url)
is ParseSubDirectories -> parseSubDirectoriesImpl(action.list)
ClearListener -> withContext(Dispatchers.IO) { mediabrowser?.changeEventListener(null) }
Release -> withContext(Dispatchers.IO) {
ClearListener -> withContext(coroutineContextProvider.IO) { mediabrowser?.changeEventListener(null) }
Release -> withContext(coroutineContextProvider.IO) {
mediabrowser?.release()
mediabrowser = null
}
......
......@@ -25,7 +25,10 @@ import android.hardware.usb.UsbDevice
import android.net.Uri
import android.text.TextUtils
import androidx.lifecycle.Observer
import kotlinx.coroutines.*
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.ObsoleteCoroutinesApi
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.videolan.libvlc.util.AndroidUtil
import org.videolan.medialibrary.MLServiceLocator
import org.videolan.medialibrary.interfaces.media.AbstractMediaWrapper
......@@ -141,7 +144,7 @@ open class FileBrowserProvider(
headers.clear()
}
override suspend fun requestBrowsing(url: String?) = withContext(Dispatchers.IO) {
override suspend fun requestBrowsing(url: String?) = withContext(coroutineContextProvider.IO) {
initBrowser()
mediabrowser?.let { if (url != null) it.browse(Uri.parse(url), getFlags()) }
}
......@@ -150,7 +153,7 @@ open class FileBrowserProvider(
when {
url == "otg://" || url?.startsWith("content:") == true -> launch {
loading.postValue(true)
dataset.value = withContext(Dispatchers.IO) { getDocumentFiles(context, Uri.parse(url).path?.substringAfterLast(':') ?: "") as? MutableList<MediaLibraryItem> ?: mutableListOf() }
dataset.value = withContext(coroutineContextProvider.IO) { getDocumentFiles(context, Uri.parse(url).path?.substringAfterLast(':') ?: "") as? MutableList<MediaLibraryItem> ?: mutableListOf() }
loading.postValue(false)
}
else -> super.browse(url)
......
......@@ -26,9 +26,6 @@ import android.content.Context
import android.content.Intent
import android.util.Log
import kotlinx.coroutines.ObsoleteCoroutinesApi
import org.videolan.libvlc.FactoryManager
import org.videolan.libvlc.interfaces.ILibVLC
import org.videolan.libvlc.interfaces.ILibVLCFactory
import org.videolan.libvlc.util.VLCUtil
import org.videolan.vlc.VLCApplication
import org.videolan.vlc.VLCCrashHandler
......@@ -41,7 +38,7 @@ object VLCInstance {
@SuppressLint("StaticFieldLeak")
private var sLibVLC: ILibVLC? = null
internal val mLibVLCFactory = FactoryManager.getFactory(ILibVLCFactory.factoryId) as ILibVLCFactory
private val libVLCFactory = FactoryManager.getFactory(ILibVLCFactory.factoryId) as ILibVLCFactory
/** A set of utility functions for the VLC application */
@Synchronized
......@@ -57,7 +54,7 @@ object VLCInstance {
}
// TODO change LibVLC signature to accept a List instead of an ArrayList
sLibVLC = mLibVLCFactory.getFromOptions(context, VLCOptions.libOptions)
sLibVLC = libVLCFactory.getFromOptions(context, VLCOptions.libOptions)
}
return sLibVLC!!
}
......@@ -67,7 +64,7 @@ object VLCInstance {
fun restart() {
if (sLibVLC != null) {
sLibVLC!!.release()
sLibVLC = mLibVLCFactory.getFromOptions(VLCApplication.appContext, VLCOptions.libOptions)
sLibVLC = libVLCFactory.getFromOptions(VLCApplication.appContext, VLCOptions.libOptions)
}
}
......
......@@ -26,7 +26,10 @@ import androidx.fragment.app.Fragment
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.AbstractMediaWrapper
import org.videolan.medialibrary.media.MediaLibraryItem
import org.videolan.vlc.providers.*
......@@ -64,7 +67,7 @@ open class BrowserModel(context: Context, val url: String?, type: Int, showHidde
launch {
this@BrowserModel.sort = sort
desc = !desc
dataset.value = withContext(Dispatchers.Default) { dataset.value.apply { sortWith(if (desc) descComp else ascComp) }.also { provider.computeHeaders(dataset.value) } }
dataset.value = withContext(coroutineContextProvider.Default) { dataset.value.apply { sortWith(if (desc) descComp else ascComp) }.also { provider.computeHeaders(dataset.value) } }
}
}
......
......@@ -14,8 +14,6 @@ import org.junit.Rule
import org.junit.Test
import org.junit.rules.TemporaryFolder
import org.videolan.libvlc.LibVLC
import org.videolan.libvlc.interfaces.IMedia
import org.videolan.libvlc.stubs.StubMedia
import org.videolan.libvlc.util.MediaBrowser
import org.videolan.medialibrary.interfaces.media.AbstractMediaWrapper
import org.videolan.vlc.BaseTest
......@@ -74,8 +72,8 @@ class FileBrowserModelTest : BaseTest() {
}
private fun setupTestFiles() {
(1..countDirs).map { temporaryFolder.newFile("dir$it") }
(1..countVideos).map { temporaryFolder.newFile("video$it.mp4") }
(0 until countDirs).map { temporaryFolder.newFile("dir$it") }
(0 until countVideos).map { temporaryFolder.newFile("video$it.mp4") }
}
private fun addFileToProvider(i: Int, file: File) {
......@@ -119,7 +117,6 @@ class FileBrowserModelTest : BaseTest() {
Thread.sleep(1000)
// TODO Hack because parseSubDirectories is called twice for some reason.
// browserProvider.onBrowseEnd()
fillFilesInDataset(temporaryFolder.root)
Thread.sleep(1000)
......@@ -135,7 +132,6 @@ class FileBrowserModelTest : BaseTest() {
.value()[0]
Thread.sleep(1000)
// browserProvider.onBrowseEnd()
fillFilesInDataset(temporaryFolder.root)
Thread.sleep(1000)
......
......@@ -10,11 +10,6 @@ import kotlinx.coroutines.ObsoleteCoroutinesApi
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TemporaryFolder
import org.videolan.libvlc.LibVLC
import org.videolan.libvlc.Media
import org.videolan.libvlc.interfaces.ILibVLC
import org.videolan.libvlc.interfaces.IMedia
import org.videolan.libvlc.stubs.StubMedia
import org.videolan.libvlc.util.MediaBrowser
import org.videolan.vlc.BaseTest
import org.videolan.vlc.providers.BrowserProvider
......@@ -61,8 +56,8 @@ class FilePickerModelTest : BaseTest() {
}
private fun setupTestFiles() {
(1..countDirs).map { temporaryFolder.newFile("dir$it") }
(1..countVideos).map { temporaryFolder.newFile("video$it.mp4") }
(0 until countDirs).map { temporaryFolder.newFile("dir$it") }
(0 until countVideos).map { temporaryFolder.newFile("video$it.mp4") }
}
private fun addFileToProvider(i: Int, file: File) {
......@@ -121,7 +116,6 @@ class FilePickerModelTest : BaseTest() {
result = browserModel.dataset.test()
.value()
// TODO: This will fail because the refresh behavior is buggy of BrowserProvider subclasses.
assertEquals(countDirs + 1, result.size)
}
}
\ No newline at end of file
......@@ -41,9 +41,6 @@ class NetworkModelTest : BaseTest() {
private lateinit var browserModel: BrowserModel
private lateinit var browserProvider: BrowserProvider
private val countVideos = 2
private val countDirs = 4
init {
BrowserFavRepository.applyMock(mockedFavoritesRepo)
......
......@@ -14,8 +14,6 @@ import org.junit.Rule
import org.junit.Test
import org.junit.rules.TemporaryFolder
import org.videolan.libvlc.LibVLC
import org.videolan.libvlc.interfaces.IMedia
import org.videolan.libvlc.stubs.StubMedia
import org.videolan.libvlc.util.MediaBrowser
import org.videolan.medialibrary.Medialibrary
import org.videolan.vlc.BaseTest
......@@ -84,9 +82,9 @@ class StorageModelTest : BaseTest() {
}
private fun setupTestFiles() {
(1..countDirs).map { temporaryFolder.newFile("dir$it") }
(1..countHiddenDirs).map { temporaryFolder.newFile(".hiddenDir$it") }
(1..countVideos).map { temporaryFolder.newFile("video$it.mp4") }
(0 until countDirs).map { temporaryFolder.newFile("dir$it") }
(0 until countHiddenDirs).map { temporaryFolder.newFile(".hiddenDir$it") }
(0 until countVideos).map { temporaryFolder.newFile("video$it.mp4") }
}
private fun addFileToProvider(i: Int, file: File) {
......
......@@ -18,7 +18,7 @@ class AlbumSongsViewModelTest : BaseTest() {
private lateinit var albumSongsViewModel: AlbumSongsViewModel
private lateinit var parent: MediaLibraryItem
private val ds = StubDataSource.getInstance()
private val dataSource = StubDataSource.getInstance()
override fun beforeTest() {
super.beforeTest()
......@@ -27,14 +27,14 @@ class AlbumSongsViewModelTest : BaseTest() {
private fun setupViewModel(name: String, isArtist: Boolean) {
if (isArtist)
parent = MLServiceLocator.getAbstractArtist(ds.uuid, name, "", "", "")
parent = MLServiceLocator.getAbstractArtist(dataSource.uuid, name, "", "", "")
else
parent = MLServiceLocator.getAbstractGenre(ds.uuid, name)
parent = MLServiceLocator.getAbstractGenre(dataSource.uuid, name)
albumSongsViewModel = AlbumSongsViewModel(context, parent)
}
private fun createDummyAudios(count: Int, title: String): List<Long> = (1..count).map {
ds.addMediaWrapper("$title $it", AbstractMediaWrapper.TYPE_AUDIO).id
dataSource.addMediaWrapper("$title $it", AbstractMediaWrapper.TYPE_AUDIO).id
}
private fun waitForProvidersData() = albumSongsViewModel.providers.map {
......@@ -51,7 +51,7 @@ class AlbumSongsViewModelTest : BaseTest() {
@Test
fun whenNoTrackExistForGivenGenre_checkResultIsEmpty() {
ds.setAudioByCount(2, null)
dataSource.setAudioByCount(2, null)
setupViewModel("xyz", false)
waitForProvidersData()
assertTrue(albumSongsViewModel.isEmpty())
......@@ -59,7 +59,7 @@ class AlbumSongsViewModelTest : BaseTest() {
@Test
fun whenSomeTrackExistForGivenGenre_checkResultIsNotEmpty() {
ds.setAudioByCount(2, null)
dataSource.setAudioByCount(2, null)
setupViewModel("Rock", false)
waitForProvidersData()
assertFalse(albumSongsViewModel.isEmpty())
......@@ -71,7 +71,7 @@ class AlbumSongsViewModelTest : BaseTest() {
@Test
fun whenNoTrackExistForGivenArtist_checkResultIsEmpty() {
ds.setAudioByCount(2, null)
dataSource.setAudioByCount(2, null)
setupViewModel("xyz", true)
waitForProvidersData()
assertTrue(albumSongsViewModel.isEmpty())
......@@ -79,7 +79,7 @@ class AlbumSongsViewModelTest : BaseTest() {
@Test
fun whenSomeTrackExistForGivenArtist_checkResultIsNotEmpty() {
ds.setAudioByCount(2, null)
dataSource.setAudioByCount(2, null)
setupViewModel("Peter Frampton", true)
waitForProvidersData()
assertFalse(albumSongsViewModel.isEmpty())
......@@ -92,7 +92,7 @@ class AlbumSongsViewModelTest : BaseTest() {
@Test
fun whenMoreThanMaxSizeTracks_checkTotalCountIsTotal() {
val count = MEDIALIBRARY_PAGE_SIZE * 3 + 1
ds.setAudioByCount(count, null)
dataSource.setAudioByCount(count, null)
setupViewModel("Rock", false)
assertEquals(count, albumSongsViewModel.tracksProvider.getTotalCount())
......@@ -101,7 +101,7 @@ class AlbumSongsViewModelTest : BaseTest() {
@Test
fun whenMoreThanMaxSizeTracks_checkLastResultIsNotLoadedYet() {
val count = MEDIALIBRARY_PAGE_SIZE * 3 + 1
ds.setAudioByCount(count, null)
dataSource.setAudioByCount(count, null)
setupViewModel("Rock", false)
waitForProvidersData()
......@@ -112,7 +112,7 @@ class AlbumSongsViewModelTest : BaseTest() {
@Test
fun whenMoreThanMaxSizeTracks_checkGetAllReturnsAll() {
val count = MEDIALIBRARY_PAGE_SIZE * 3 + 1
ds.setAudioByCount(count, null)
dataSource.setAudioByCount(count, null)
setupViewModel("Rock", false)
waitForProvidersData()
......@@ -145,7 +145,7 @@ class AlbumSongsViewModelTest : BaseTest() {
@Test
fun whenThereAreAlbumsWithArtistButFilteredWithNonExistingAlbum_checkAlbumResultIsEmpty() {
createDummyAudios(3, "XYZ")
setupViewModel("Artisto", true)
setupViewModel("CrazyArtists", true)
albumSongsViewModel.filter("unknown")
......@@ -169,7 +169,7 @@ class AlbumSongsViewModelTest : BaseTest() {
@Test
fun whenThereAreAlbumsWithArtistAndFilteredWithExistingAlbum_checkAlbumResultIsNotEmpty() {
createDummyAudios(3, "XYZ")
setupViewModel("Artisto", true)
setupViewModel("CrazyArtists", true) // That's the name of Album Artist
albumSongsViewModel.filter("CD1")
......@@ -262,7 +262,7 @@ class AlbumSongsViewModelTest : BaseTest() {
fun whenThereAreFourArtistTracksWithAlternatelyDifferentTitles_checkAlbumHeadersIsSortedByDate() {
createDummyAudios(2, "test")
createDummyAudios(2, "fake")
setupViewModel("", true)
setupViewModel("CrazyArtists", true)
waitForProvidersData()
......
<
......@@ -8,7 +8,9 @@ import org.junit.Test
import org.videolan.medialibrary.interfaces.media.AbstractMediaWrapper
import org.videolan.medialibrary.stubs.StubDataSource
import org.videolan.vlc.BaseTest
import org.videolan.vlc.util.KEY_ARTISTS_SHOW_ALL
import org.videolan.vlc.util.MEDIALIBRARY_PAGE_SIZE
import org.videolan.vlc.util.Settings
@ExperimentalCoroutinesApi
@ObsoleteCoroutinesApi
......@@ -18,10 +20,9 @@ class AudioBrowserViewModelTest : BaseTest() {
override fun beforeTest() {
super.beforeTest()
StubDataSource.getInstance().resetData()
audioBrowserViewModel = AudioBrowserViewModel(context)
}
private fun createDummyAudios(count: Int, title: String): List<Long> = (1..count).map {
private fun createDummyAudios(count: Int, title: String): List<Long> = (0 until count).map {
StubDataSource.getInstance().addMediaWrapper("$title $it", AbstractMediaWrapper.TYPE_AUDIO).id
}
......@@ -29,8 +30,17 @@ class AudioBrowserViewModelTest : BaseTest() {
it.pagedList.test().awaitValue()
}
private fun setupViewModel(showAll: Boolean = false) {
Settings.getInstance(context).edit().run {
putBoolean(KEY_ARTISTS_SHOW_ALL, showAll)
commit()
}
audioBrowserViewModel = AudioBrowserViewModel(context)
}
@Test
fun whenNoArtistAlbumTrackGenre_checkResultIsEmpty() {
setupViewModel()
waitForProvidersData()
assertTrue(audioBrowserViewModel.isEmpty())
......@@ -38,6 +48,7 @@ class AudioBrowserViewModelTest : BaseTest() {
@Test
fun whenNoArtistAlbumTrackGenre_checkTotalCountIsZero() {
setupViewModel()
waitForProvidersData()
assertEquals(0, audioBrowserViewModel.tracksProvider.getTotalCount())
......@@ -48,6 +59,7 @@ class AudioBrowserViewModelTest : BaseTest() {
@Test
fun whenThereAre5Tracks_checkResultIsNotEmpty() {
setupViewModel()
StubDataSource.getInstance().setAudioByCount(2, null)
createDummyAudios(3, "XYZ")
......@@ -55,17 +67,31 @@ class AudioBrowserViewModelTest : BaseTest() {
}
@Test
fun whenThereAre5Tracks_checkTracksAre5GenresAre2AlbumsAre2ArtistsAre2ForTotalCount() {
fun whenThereAre5Tracks_checkTracksAre5GenresAre5AlbumsAre5ArtistsAre5ForTotalCount() {
setupViewModel()
StubDataSource.getInstance().setAudioByCount(2, null) // AlbumArtist & Artist are same, so only one is added.
createDummyAudios(3, "XYZ") // AlbumArtist & Artist are different, so both are added.
waitForProvidersData()
assertEquals(5, audioBrowserViewModel.tracksProvider.getTotalCount())
assertEquals(5, audioBrowserViewModel.genresProvider.getTotalCount())
/* TODO: I haven't yet checked the Medialibrary source code, but I doubt it would add duplicate album for each audio file
* So gotta fix the logic in stubs to simulate that behaviour. Once that's fixed, I'll update my tests with proper logic.
*/
assertEquals(5, audioBrowserViewModel.albumsProvider.getTotalCount())
assertEquals(5, audioBrowserViewModel.artistsProvider.getTotalCount()) // Be default, showAll is false
}
@Test
fun whenThereAre5TracksWithShowAllTrue_checkTracksAre5GenresAre5AlbumsAre5ArtistsAre5ForTotalCount() {
setupViewModel(showAll = true)
StubDataSource.getInstance().setAudioByCount(2, null) // AlbumArtist & Artist are same, so only one is added.
createDummyAudios(3, "XYZ") // AlbumArtist & Artist are different, so both are added.
waitForProvidersData()
assertEquals(5, audioBrowserViewModel.tracksProvider.getTotalCount())
assertEquals(5, audioBrowserViewModel.genresProvider.getTotalCount())
assertEquals(5, audioBrowserViewModel.albumsProvider.getTotalCount())
assertEquals(8, audioBrowserViewModel.artistsProvider.getTotalCount())
......@@ -73,6 +99,7 @@ class AudioBrowserViewModelTest : BaseTest() {
@Test
fun whenThereAre5TracksWithShowAllTrue_checkTracksAre5GenresAre5AlbumsAre5ArtistsAre5ForPagedData() {
setupViewModel(showAll = true)
StubDataSource.getInstance().setAudioByCount(2, null) // AlbumArtist & Artist are same, so only one is added.
createDummyAudios(3, "XYZ") // AlbumArtist & Artist are different, so both are added.
......@@ -81,11 +108,12 @@ class AudioBrowserViewModelTest : BaseTest() {
assertEquals(5, audioBrowserViewModel.tracksProvider.pagedList.test().value().size)