Commit 805ab4a2 authored by Shivansh Saini's avatar Shivansh Saini

Test for AlbumSongsViewModel done

- Fixed searchTracks & searchAlbum logic in StubArtist
- Improved AudioBrowserViewModelTest
Signed-off-by: Shivansh Saini's avatarShivansh Saini <shivanshs9@gmail.com>
parent ad16d143
......@@ -45,7 +45,7 @@ public class StubArtist extends AbstractArtist {
public AbstractAlbum[] searchAlbums(String query, int sort, boolean desc, int nbItems, int offset) {
ArrayList<AbstractAlbum> results = new ArrayList<>();
for (AbstractAlbum album : dt.mAlbums) {
if (album.getDescription().equals(this.getTitle()) ||
if (album.getDescription().equals(this.getTitle()) &&
album.getTitle().equals(query)) {
results.add(album);
}
......@@ -81,8 +81,8 @@ public class StubArtist extends AbstractArtist {
public AbstractMediaWrapper[] searchTracks(String query, int sort, boolean desc, int nbItems, int offset) {
ArrayList<AbstractMediaWrapper> results = new ArrayList<>();
for (AbstractMediaWrapper media : dt.mAudioMediaWrappers) {
if (media.getArtist().equals(this.getTitle()) ||
media.getAlbumArtist().equals(this.getTitle()) ||
if ((media.getArtist().equals(this.getTitle()) ||
media.getAlbumArtist().equals(this.getTitle())) &&
media.getTitle().contains(query)) {
results.add(media);
}
......
......@@ -16,6 +16,7 @@ import org.robolectric.RobolectricTestRunner
import org.robolectric.RuntimeEnvironment
import org.robolectric.annotation.Config
import org.videolan.medialibrary.MLServiceLocator
import org.videolan.medialibrary.interfaces.AbstractMedialibrary
@ObsoleteCoroutinesApi
@ExperimentalCoroutinesApi
......@@ -24,6 +25,7 @@ import org.videolan.medialibrary.MLServiceLocator
open class BaseTest {
val context: Context = ApplicationProvider.getApplicationContext()
val application = (RuntimeEnvironment.application as VLCTestApplication)
val medialibrary: AbstractMedialibrary
//To prevent Method getMainLooper in android.os.Looper not mocked error when setting value for MutableLiveData
@get:Rule
......@@ -31,6 +33,7 @@ open class BaseTest {
init {
MockKAnnotations.init(this)
medialibrary = MLServiceLocator.getAbstractMedialibrary().apply { init(context) }
}
@Before
......
package org.videolan.vlc.viewmodels.mobile
import com.jraska.livedata.test
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.media.MediaLibraryItem
import org.videolan.medialibrary.stubs.StubDataSource
import org.videolan.vlc.BaseTest
import org.videolan.vlc.util.MEDIALIBRARY_PAGE_SIZE
@ObsoleteCoroutinesApi
@ExperimentalCoroutinesApi
class AlbumSongsViewModelTest : BaseTest() {
private lateinit var albumSongsViewModel: AlbumSongsViewModel
private lateinit var parent: MediaLibraryItem
private val ds = StubDataSource.getInstance()
override fun beforeTest() {
super.beforeTest()
StubDataSource.getInstance().resetData()
}
private fun setupViewModel(name: String, isArtist: Boolean) {
if (isArtist)
parent = MLServiceLocator.getAbstractArtist(ds.uuid, name, "", "", "")
else
parent = MLServiceLocator.getAbstractGenre(ds.uuid, name)
albumSongsViewModel = AlbumSongsViewModel(context, application, parent)
}
private fun createDummyAudios(count: Int, title: String): List<Long> = (1..count).map {
ds.addMediaWrapper("$title $it", AbstractMediaWrapper.TYPE_AUDIO).id
}
private fun waitForProvidersData() = albumSongsViewModel.providers.map {
it.pagedList.test().awaitValue()
}
@Test
fun whenNoTrackExist_checkResultIsEmpty() {
setupViewModel("test", false)
waitForProvidersData()
assertTrue(albumSongsViewModel.isEmpty())
}
@Test
fun whenNoTrackExistForGivenGenre_checkResultIsEmpty() {
ds.setAudioByCount(2, null)
setupViewModel("xyz", false)
waitForProvidersData()
assertTrue(albumSongsViewModel.isEmpty())
}
@Test
fun whenSomeTrackExistForGivenGenre_checkResultIsNotEmpty() {
ds.setAudioByCount(2, null)
setupViewModel("Rock", false)
waitForProvidersData()
assertFalse(albumSongsViewModel.isEmpty())
// Both tracks and albums are associated with this genre.
assertFalse(albumSongsViewModel.tracksProvider.isEmpty())
assertFalse(albumSongsViewModel.albumsProvider.isEmpty())
}
@Test
fun whenNoTrackExistForGivenArtist_checkResultIsEmpty() {
ds.setAudioByCount(2, null)
setupViewModel("xyz", true)
waitForProvidersData()
assertTrue(albumSongsViewModel.isEmpty())
}
@Test
fun whenSomeTrackExistForGivenArtist_checkResultIsNotEmpty() {
ds.setAudioByCount(2, null)
setupViewModel("Peter Frampton", true)
waitForProvidersData()
assertFalse(albumSongsViewModel.isEmpty())
// Both tracks and albums are associated with this genre.
assertFalse(albumSongsViewModel.tracksProvider.isEmpty())
assertFalse(albumSongsViewModel.albumsProvider.isEmpty())
}
@Test
fun whenMoreThanMaxSizeTracks_checkTotalCountIsTotal() {
val count = MEDIALIBRARY_PAGE_SIZE * 3 + 1
ds.setAudioByCount(count, null)
setupViewModel("Rock", false)
assertEquals(count, albumSongsViewModel.tracksProvider.getTotalCount())
}
@Test
fun whenMoreThanMaxSizeTracks_checkLastResultIsNotLoadedYet() {
val count = MEDIALIBRARY_PAGE_SIZE * 3 + 1
ds.setAudioByCount(count, null)
setupViewModel("Rock", false)
waitForProvidersData()
assertNull(albumSongsViewModel.tracksProvider.pagedList.test().value()[count - 1])
}
@Test
fun whenMoreThanMaxSizeTracks_checkGetAllReturnsAll() {
val count = MEDIALIBRARY_PAGE_SIZE * 3 + 1
ds.setAudioByCount(count, null)
setupViewModel("Rock", false)
waitForProvidersData()
assertNotNull(albumSongsViewModel.tracksProvider.getAll()[count - 1])
}
@Test
fun whenNoTrackAndFiltered_checkResultIsEmpty() {
setupViewModel("Artisto", true)
albumSongsViewModel.filter("xyz")
waitForProvidersData()
assertTrue(albumSongsViewModel.isEmpty())
}
@Test
fun whenThereAreTracksWithArtistButFilteredWithNonExistingTrack_checkTrackResultIsEmpty() {
createDummyAudios(3, "XYZ")
setupViewModel("Artisto", true)
albumSongsViewModel.filter("unknown")
waitForProvidersData()
assertTrue(albumSongsViewModel.tracksProvider.isEmpty())
}
@Test
fun whenThereAreAlbumsWithArtistButFilteredWithNonExistingAlbum_checkAlbumResultIsEmpty() {
createDummyAudios(3, "XYZ")
setupViewModel("Artisto", true)
albumSongsViewModel.filter("unknown")
waitForProvidersData()
assertTrue(albumSongsViewModel.albumsProvider.isEmpty())
}
@Test
fun whenThereAreTracksWithArtistAndFilteredWithExistingTrack_checkTrackResultIsNotEmpty() {
createDummyAudios(3, "XYZ")
setupViewModel("Artisto", true)
albumSongsViewModel.filter("XYZ")
waitForProvidersData()
assertFalse(albumSongsViewModel.tracksProvider.isEmpty())
}
@Test
fun whenThereAreAlbumsWithArtistAndFilteredWithExistingAlbum_checkAlbumResultIsNotEmpty() {
createDummyAudios(3, "XYZ")
setupViewModel("Artisto", true)
albumSongsViewModel.filter("CD1")
waitForProvidersData()
assertFalse(albumSongsViewModel.albumsProvider.isEmpty())
}
@Test
fun whenArtistHasSomeTracksButFilteredResultContainsNone_restoringViewModelResetsFilterAndShowsItemAgain() {
createDummyAudios(2, "test")
setupViewModel("Artisto", true)
albumSongsViewModel.filter("unknown")
waitForProvidersData()
assertTrue(albumSongsViewModel.isEmpty())
albumSongsViewModel.restore()
waitForProvidersData()
assertFalse(albumSongsViewModel.isEmpty())
}
@Test
fun whenFilteredAndLaterRestored_isFilteringIsTrueLaterFalse() {
setupViewModel("Artisto", true)
assertFalse(albumSongsViewModel.isFiltering())
albumSongsViewModel.filter("def")
assertTrue(albumSongsViewModel.isFiltering())
albumSongsViewModel.restore()
assertFalse(albumSongsViewModel.isFiltering())
}
@Test
fun whenArtistHas2TracksAndLaterAdded3Tracks_checkResultIsUpdatedWithThemOnRefresh() {
createDummyAudios(2, "test")
setupViewModel("Artisto", true)
waitForProvidersData()
assertFalse(albumSongsViewModel.isEmpty())
assertEquals(2, albumSongsViewModel.tracksProvider.pagedList.test().value().size)
createDummyAudios(3, "test")
albumSongsViewModel.refresh()
waitForProvidersData()
assertFalse(albumSongsViewModel.isEmpty())
assertEquals(5, albumSongsViewModel.tracksProvider.pagedList.test().value().size)
}
@Test
fun whenThereAreFourArtistTracksWithAlternatelyDifferentTitles_checkTrackHeadersIsSortedByAlbum() {
createDummyAudios(2, "test")
createDummyAudios(2, "fake")
setupViewModel("Artisto", true)
waitForProvidersData()
val trackHeaders = albumSongsViewModel.tracksProvider.liveHeaders.test().value()
// Assertion for track headers
assertEquals(1, trackHeaders.size())
assertEquals("XYZ CD1", trackHeaders[0])
}
@Test
fun whenThereAreFourGenreTracksWithAlternatelyDifferentTitles_checkTrackHeadersIsSortedByName() {
createDummyAudios(2, "test")
createDummyAudios(2, "fake")
setupViewModel("Jazz", false)
waitForProvidersData()
val trackHeaders = albumSongsViewModel.tracksProvider.liveHeaders.test().value()
// Assertion for track headers
assertEquals(2, trackHeaders.size())
assertEquals("F", trackHeaders[0])
assertEquals("T", trackHeaders[2])
}
@Test
fun whenThereAreFourArtistTracksWithAlternatelyDifferentTitles_checkAlbumHeadersIsSortedByDate() {
createDummyAudios(2, "test")
createDummyAudios(2, "fake")
setupViewModel("", true)
waitForProvidersData()
val albumHeaders = albumSongsViewModel.albumsProvider.liveHeaders.test().value()
// Assertion for album headers
assertEquals(1, albumHeaders.size())
assertEquals("2018", albumHeaders[0])
}
@Test
fun whenThereAreFourGenreTracksWithAlternatelyDifferentTitles_checkAlbumHeadersIsSortedByName() {
createDummyAudios(2, "test")
createDummyAudios(2, "fake")
setupViewModel("Jazz", false)
waitForProvidersData()
val albumHeaders = albumSongsViewModel.albumsProvider.liveHeaders.test().value()
// Assertion for album headers
assertEquals(1, albumHeaders.size())
assertEquals("X", albumHeaders[0])
}
}
\ No newline at end of file
......@@ -16,7 +16,6 @@ import org.videolan.vlc.util.MEDIALIBRARY_PAGE_SIZE
@ObsoleteCoroutinesApi
class AudioBrowserViewModelTest : BaseTest() {
private lateinit var audioBrowserViewModel: AudioBrowserViewModel
private val medialibrary: AbstractMedialibrary = MLServiceLocator.getAbstractMedialibrary().apply { init(context) }
override fun beforeTest() {
super.beforeTest()
......@@ -65,6 +64,10 @@ class AudioBrowserViewModelTest : BaseTest() {
waitForProvidersData()
assertEquals(5, audioBrowserViewModel.tracksProvider.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.genresProvider.getTotalCount())
assertEquals(5, audioBrowserViewModel.albumsProvider.getTotalCount())
assertEquals(8, audioBrowserViewModel.artistsProvider.getTotalCount())
......@@ -237,7 +240,7 @@ class AudioBrowserViewModelTest : BaseTest() {
}
@Test
fun whenPlaylistHasSomeTracksButFilteredResultContainsNone_restoringViewModelResetsFilterAndShowsItemAgain() {
fun whenThereAreSomeTracksButFilteredResultContainsNone_restoringViewModelResetsFilterAndShowsItemAgain() {
createDummyAudios(2, "test")
audioBrowserViewModel.filter("unknown")
......@@ -282,33 +285,60 @@ class AudioBrowserViewModelTest : BaseTest() {
}
@Test
fun whenPlaylistHasFourTracksWithAlternatelyDifferentTitles_checkHeadersContains2Letters() {
fun whenThereAreFourTracksWithAlternatelyDifferentTitles_checkTrackHeadersContainsTwoLetters() {
createDummyAudios(2, "test")
createDummyAudios(2, "fake")
audioBrowserViewModel.refresh()
waitForProvidersData()
val trackHeaders = audioBrowserViewModel.tracksProvider.liveHeaders.test().value()
val genreHeaders = audioBrowserViewModel.genresProvider.liveHeaders.test().value()
val albumHeaders = audioBrowserViewModel.albumsProvider.liveHeaders.test().value()
val artistHeaders = audioBrowserViewModel.artistsProvider.liveHeaders.test().value()
// Assertion for track headers
assertEquals(2, trackHeaders.size())
assertEquals("F", trackHeaders[0])
assertEquals("T", trackHeaders[2])
}
@Test
fun whenThereAreFourTracksWithAlternatelyDifferentTitles_checkGenreHeadersContainsOneLetter() {
createDummyAudios(2, "test")
createDummyAudios(2, "fake")
waitForProvidersData()
val genreHeaders = audioBrowserViewModel.genresProvider.liveHeaders.test().value()
// Assertion for genre headers
assertEquals(1, genreHeaders.size())
assertEquals("J", genreHeaders[0])
}
@Test
fun whenThereAreFourTracksWithAlternatelyDifferentTitles_checkAlbumHeadersContainsOneLetter() {
createDummyAudios(2, "test")
createDummyAudios(2, "fake")
waitForProvidersData()
val albumHeaders = audioBrowserViewModel.albumsProvider.liveHeaders.test().value()
// Assertion for album headers
assertEquals(1, albumHeaders.size())
assertEquals("X", albumHeaders[0])
}
@Test
fun whenThereAreFourTracksWithAlternatelyDifferentTitles_checkArtistHeadersContainsOneLetter() {
createDummyAudios(2, "test")
createDummyAudios(2, "fake")
waitForProvidersData()
val artistHeaders = audioBrowserViewModel.artistsProvider.liveHeaders.test().value()
// Assertion for artist headers
assertEquals(1, artistHeaders.size())
assertEquals(4, artistHeaders.size())
assertEquals("#", artistHeaders[0])
assertEquals("A", artistHeaders[1])
}
}
\ No newline at end of file
......@@ -16,7 +16,6 @@ import org.videolan.vlc.util.MEDIALIBRARY_PAGE_SIZE
@ExperimentalCoroutinesApi
class PlaylistViewModelTest : BaseTest() {
private lateinit var playlistViewModel: PlaylistViewModel
private val medialibrary = MLServiceLocator.getAbstractMedialibrary()
private lateinit var parent: AbstractPlaylist
override fun beforeTest() {
......
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