Commit 8101fdd4 authored by Shivansh Saini's avatar Shivansh Saini

FoldersViewModelTest added

- Added getFolders logic in StubMedialibrary
Signed-off-by: Shivansh Saini's avatarShivansh Saini <shivanshs9@gmail.com>
parent 1736dcfc
......@@ -77,7 +77,6 @@ public class StubDataSource {
}
public void setVideoByCount(int count, @Nullable String folder) {
mVideoMediaWrappers.clear();
AbstractMediaWrapper media;
String fileName;
......
......@@ -99,4 +99,14 @@ public class StubFolder extends AbstractFolder {
}
return count;
}
@Override
public boolean equals(Object obj) {
boolean result = super.equals(obj);
if (!result && obj instanceof AbstractFolder) {
AbstractFolder other = ((AbstractFolder) obj);
return other.mMrl.equals(this.mMrl);
}
return result;
}
}
......@@ -7,7 +7,6 @@ import android.webkit.URLUtil;
import androidx.annotation.NonNull;
import org.videolan.libvlc.util.Extensions;
import org.videolan.medialibrary.MLServiceLocator;
import org.videolan.medialibrary.Tools;
import org.videolan.medialibrary.interfaces.AbstractMedialibrary;
......@@ -19,9 +18,10 @@ import org.videolan.medialibrary.interfaces.media.AbstractMediaWrapper;
import org.videolan.medialibrary.interfaces.media.AbstractPlaylist;
import org.videolan.medialibrary.media.SearchAggregate;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Locale;
import java.util.List;
public class StubMedialibrary extends AbstractMedialibrary {
......@@ -43,7 +43,8 @@ public class StubMedialibrary extends AbstractMedialibrary {
public void start() {
isMedialibraryStarted = true;
synchronized (onMedialibraryReadyListeners) {
for (OnMedialibraryReadyListener listener : onMedialibraryReadyListeners) listener.onMedialibraryReady();
for (OnMedialibraryReadyListener listener : onMedialibraryReadyListeners)
listener.onMedialibraryReady();
}
}
......@@ -72,7 +73,9 @@ public class StubMedialibrary extends AbstractMedialibrary {
onDiscoveryStarted(path);
onDiscoveryCompleted(path);
}
public void removeFolder(@NonNull String mrl) {}
public void removeFolder(@NonNull String mrl) {
}
public boolean removeDevice(String uuid, String path) {
return true;
......@@ -86,6 +89,7 @@ public class StubMedialibrary extends AbstractMedialibrary {
}
return results.toArray(new String[0]);
}
public AbstractMediaWrapper[] getVideos() {
return getVideos(SORT_DEFAULT, false);
}
......@@ -264,7 +268,7 @@ public class StubMedialibrary extends AbstractMedialibrary {
public int getPlaylistsCount(String query) {
int count = 0;
for (AbstractPlaylist playlist : dt.mPlaylists) {
if (playlist.getTitle().contains(query)) count ++;
if (playlist.getTitle().contains(query)) count++;
}
return count;
}
......@@ -283,26 +287,35 @@ public class StubMedialibrary extends AbstractMedialibrary {
return playlist;
}
public void pauseBackgroundOperations() {}
public void resumeBackgroundOperations() {}
public void pauseBackgroundOperations() {
}
public void resumeBackgroundOperations() {
}
public void reload() {
Log.e(TAG, "reload: no entrypoint");
reload("");
}
public void reload(String entrypoint) {
Log.e(TAG, "reload(string entrypoint): ");
onReloadStarted(entrypoint);
onReloadCompleted(entrypoint);
onBackgroundTasksIdleChanged(true);
}
public void forceParserRetry() {}
public void forceRescan() {}
public void forceParserRetry() {
}
public void forceRescan() {
}
public AbstractMediaWrapper[] lastMediaPlayed() {
ArrayList<AbstractMediaWrapper> results = new ArrayList<>();
for (AbstractMediaWrapper media : dt.mHistory) {
if (media.getType() == AbstractMediaWrapper.TYPE_VIDEO ||
media.getType() == AbstractMediaWrapper.TYPE_AUDIO) results.add(media);
media.getType() == AbstractMediaWrapper.TYPE_AUDIO) results.add(media);
// the native method specifies an nbItems of 100, offset 0
if (results.size() >= 100) break;
}
......@@ -399,18 +412,36 @@ public class StubMedialibrary extends AbstractMedialibrary {
return dt.addMediaWrapper(mrl, title, AbstractMediaWrapper.TYPE_STREAM);
}
// TODO: Fix sorting, offset etc
public AbstractFolder[] getFolders(int type, int sort, boolean desc, int nbItems, int offset) {
return null;
List<AbstractFolder> folders = new ArrayList<>();
if (type == AbstractFolder.TYPE_FOLDER_VIDEO) {
for (AbstractFolder folder : dt.mFolders) {
if (folders.contains(folder)) continue;
String path = folder.mMrl;
if (path.isEmpty()) continue;
for (AbstractMediaWrapper mediaWrapper : dt.mVideoMediaWrappers) {
String childPath = mediaWrapper.getUri().getPath();
if (childPath == null) continue;
if (isParentFolder(path, childPath)) {
folders.add(folder);
break;
}
}
}
}
return folders.toArray(new AbstractFolder[0]);
}
public int getFoldersCount(int type) {
return 0;
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++) {
for (int i = 0; i < dt.mVideoMediaWrappers.size(); i++) {
AbstractMediaWrapper media = dt.mVideoMediaWrappers.get(i);
if (media.getId() == mediaId) {
media.setSeen(media.getSeen() + 1);
......@@ -513,7 +544,7 @@ public class StubMedialibrary extends AbstractMedialibrary {
public AbstractArtist[] searchArtist(String query) {
ArrayList<AbstractArtist> results = new ArrayList<>();
for (AbstractArtist artist: dt.mArtists) {
for (AbstractArtist artist : dt.mArtists) {
if (artist.getTitle().contains(query)) results.add(artist);
}
return results.toArray(new AbstractArtist[0]);
......@@ -526,11 +557,12 @@ public class StubMedialibrary extends AbstractMedialibrary {
public AbstractAlbum[] searchAlbum(String query) {
ArrayList<AbstractAlbum> results = new ArrayList<>();
for (AbstractAlbum album: dt.mAlbums) {
for (AbstractAlbum album : dt.mAlbums) {
if (album.getTitle().contains(query)) results.add(album);
}
return results.toArray(new AbstractAlbum[0]);
}
public AbstractAlbum[] searchAlbum(String query, int sort, boolean desc, int nbItems, int offset) {
ArrayList<AbstractAlbum> results = new ArrayList<>(Arrays.asList(searchAlbum(query)));
return dt.sortAlbum(dt.secureSublist(results, offset, offset + nbItems), sort, desc);
......@@ -538,7 +570,7 @@ public class StubMedialibrary extends AbstractMedialibrary {
public AbstractGenre[] searchGenre(String query) {
ArrayList<AbstractGenre> results = new ArrayList<>();
for (AbstractGenre genre: dt.mGenres) {
for (AbstractGenre genre : dt.mGenres) {
if (genre.getTitle().contains(query)) results.add(genre);
}
return results.toArray(new AbstractGenre[0]);
......@@ -551,7 +583,7 @@ public class StubMedialibrary extends AbstractMedialibrary {
public AbstractPlaylist[] searchPlaylist(String query) {
ArrayList<AbstractPlaylist> results = new ArrayList<>();
for (AbstractPlaylist playlist: dt.mPlaylists) {
for (AbstractPlaylist playlist : dt.mPlaylists) {
if (playlist.getTitle().contains(query)) results.add(playlist);
}
return results.toArray(new AbstractPlaylist[0]);
......@@ -561,4 +593,11 @@ public class StubMedialibrary extends AbstractMedialibrary {
ArrayList<AbstractPlaylist> results = new ArrayList<>(Arrays.asList(searchPlaylist(query)));
return dt.sortPlaylist(dt.secureSublist(results, offset, offset + nbItems), sort, desc);
}
private boolean isParentFolder(String parentMrl, String childMrl) {
if (!childMrl.contains(parentMrl)) return false;
File mediaFile = new File(childMrl);
String parentPath = mediaFile.getParent();
return parentPath.equals(parentMrl);
}
}
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.interfaces.media.AbstractFolder
import org.videolan.medialibrary.stubs.StubDataSource
import org.videolan.vlc.BaseTest
import org.videolan.vlc.util.MEDIALIBRARY_PAGE_SIZE
@ObsoleteCoroutinesApi
@ExperimentalCoroutinesApi
class FoldersViewModelTest: BaseTest() {
private lateinit var foldersViewModel: FoldersViewModel
override fun beforeTest() {
super.beforeTest()
StubDataSource.getInstance().resetData()
setupViewModel()
}
internal fun setupViewModel() {
foldersViewModel = FoldersViewModel(context, application, AbstractFolder.TYPE_FOLDER_VIDEO)
}
@Test
fun whenNoVideoFolder_checkCountIsZero() {
assertEquals(0, foldersViewModel.provider.getTotalCount())
}
@Test
fun whenNoVideoFolder_checkGetAllReturnsEmpty() {
assertEquals(emptyArray(), foldersViewModel.provider.getAll())
}
@Test
fun whenThereAre2FoldersWithVideos_checkCountIs2() {
StubDataSource.getInstance().run {
createFolder("test1")
setVideoByCount(2, "test1")
createFolder("test2")
setVideoByCount(3, "test2")
}
assertEquals(2, foldersViewModel.provider.getTotalCount())
}
@Test
fun whenThereAre2FoldersWithVideos_checkGetAllReturnsThem() {
StubDataSource.getInstance().run {
createFolder("test1")
setVideoByCount(2, "test1")
createFolder("test2")
setVideoByCount(3, "test2")
}
val testResult = foldersViewModel.provider.getAll()
assertEquals(2, testResult.size)
assertEquals("test1", testResult[0].title)
assertEquals("test2", testResult[1].title)
}
@Test
fun whenThereAre2FoldersWithVideos_checkGetPageReturnsThem() {
StubDataSource.getInstance().run {
createFolder("test1")
setVideoByCount(2, "test1")
createFolder("test2")
setVideoByCount(3, "test2")
}
val testResult = foldersViewModel.provider.pagedList.test()
.awaitValue().value()
assertEquals(2, testResult.size)
}
@Test
fun whenNoVideoFolder_checkIsEmptyReturnsTrue() {
foldersViewModel.provider.pagedList.test()
.awaitValue()
assertTrue(foldersViewModel.isEmpty())
}
@Test
fun whenThereAre2FoldersWithVideos_checkIsEmptyReturnsFalse() {
// FIXME: java.lang.NoClassDefFoundError: org/videolan/vlc/util/KextensionsKt (wrong name: org/videolan/vlc/util/KExtensionsKt)
StubDataSource.getInstance().run {
createFolder("test1")
setVideoByCount(2, "test1")
createFolder("test2")
setVideoByCount(3, "test2")
}
foldersViewModel.provider.pagedList.test()
.awaitValue()
assertFalse(foldersViewModel.isEmpty())
}
@Test
fun whenNoVideoFolderAndLaterAddedNewVideo_checkRefreshUpdatesTheList() {
foldersViewModel.provider.pagedList.test()
.awaitValue()
assertTrue(foldersViewModel.isEmpty())
StubDataSource.getInstance().setVideoByCount(1, "test1")
foldersViewModel.refresh()
val testResult = foldersViewModel.provider.pagedList.test()
.awaitValue().value()
assertFalse(foldersViewModel.isEmpty())
assertEquals(1, testResult.size)
}
@Test
fun whenFilteredAndLaterRestored_isFilteringIsTrueLaterFalse() {
StubDataSource.getInstance().setVideoByCount(3, "test1")
assertFalse(foldersViewModel.isFiltering())
foldersViewModel.filter("test")
assertTrue(foldersViewModel.isFiltering())
foldersViewModel.restore()
assertFalse(foldersViewModel.isFiltering())
}
@Test
fun whenNoVideoFolderAndFiltered_checkResultIsEmpty() {
foldersViewModel.filter("xyz")
foldersViewModel.provider.pagedList.test()
.awaitValue()
assertTrue(foldersViewModel.isEmpty())
}
@Test
fun whenNoVideoFolderAndFilteredWithNonExistingFolder_checkResultIsEmpty() {
StubDataSource.getInstance().setVideoByCount(3, "test1")
foldersViewModel.provider.pagedList.test()
.awaitValue()
assertFalse(foldersViewModel.isEmpty())
foldersViewModel.filter("unknown")
foldersViewModel.refresh()
foldersViewModel.provider.pagedList.test()
.awaitValue()
assertTrue(foldersViewModel.isEmpty())
}
@Test
fun whenNoVideoFolderAndFilteredWithExistingFolder_checkResultIsNotEmpty() {
StubDataSource.getInstance().setVideoByCount(3, "test1")
foldersViewModel.provider.pagedList.test()
.awaitValue()
assertFalse(foldersViewModel.isEmpty())
foldersViewModel.filter("test")
foldersViewModel.refresh()
foldersViewModel.provider.pagedList.test()
.awaitValue()
assertFalse(foldersViewModel.isEmpty())
}
@Test
fun whenThereAreVideoFoldersButFilteredResultContainsNone_restoringViewModelResetsFilterAndShowsItemAgain() {
StubDataSource.getInstance().setVideoByCount(3, "test")
foldersViewModel.filter("unknown")
foldersViewModel.provider.pagedList.test()
.awaitValue()
assertTrue(foldersViewModel.isEmpty())
foldersViewModel.restore()
foldersViewModel.provider.pagedList.test()
.awaitValue()
assertFalse(foldersViewModel.isEmpty())
}
}
\ No newline at end of file
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