mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-27 12:53:55 +01:00
* #3468 Switch from RvRenderer to AdapterDelegates - replace SearchDepictionsRenderer * #3468 Switch from RvRenderer to AdapterDelegates - replace UploadCategoryDepictionsRenderer * #3468 Switch from RvRenderer to AdapterDelegates - update BaseAdapter to be easier to use * #3468 Switch from RvRenderer to AdapterDelegates - replace SearchImagesRenderer * #3468 Switch from RvRenderer to AdapterDelegates - replace SearchCategoriesRenderer * #3468 Switch from RvRenderer to AdapterDelegates - replace NotificationRenderer * #3468 Switch from RvRenderer to AdapterDelegates - replace UploadDepictsRenderer * #3468 Switch from RvRenderer to AdapterDelegates - replace PlaceRenderer * #3756 Convert SearchDepictionsFragment to use Pagination - convert SearchDepictionsFragment * #3756 Convert SearchDepictionsFragment to use Pagination - fix presenter unit tests now that view is not nullable - fix Category prefix imports * #3756 Convert SearchDepictionsFragment to use Pagination - test DataSource related classes * #3756 Convert SearchDepictionsFragment to use Pagination - reset rx scheduler - ignore failing test * #3760 Convert SearchCategoriesFragment to use Pagination - extract functionality of pagination to base classes - add category pagination * #3772 Convert SearchImagesFragment to use Pagination - convert SearchImagesFragment - tidy up showing the empty view - make search fragments show snackbar with appropriate text * #3772 Convert SearchImagesFragment to use Pagination - allow viewpager to load more data * #3760 remove test that got re-added by merge * #3760 remove duplicate dependency * #3772 fix compilation * #3780 Create media using a combination of Entities & MwQueryResult - construct media with an entity - move fields from media down to contribution - move dynamic fields outside of media - remove unused constructors - remove all unnecessary fetching of captions/descriptions - bump database version * #3808 Construct media objects that depict an item id correctly - use generator to construct media for DepictedImages * #3780 Create media using a combination of Entities & MwQueryResult - update wikicode to align with expected behaviour * #3780 Create media using a combination of Entities & MwQueryResult - replace old site of thumbnail title with most relevant caption
This commit is contained in:
parent
bf4b7e2efc
commit
4b22583b60
46 changed files with 803 additions and 1532 deletions
|
|
@ -1,5 +1,6 @@
|
|||
import com.nhaarman.mockitokotlin2.mock
|
||||
import com.nhaarman.mockitokotlin2.whenever
|
||||
import fr.free.nrw.commons.Media
|
||||
import fr.free.nrw.commons.category.CategoryItem
|
||||
import fr.free.nrw.commons.location.LatLng
|
||||
import fr.free.nrw.commons.nearby.Label
|
||||
|
|
@ -7,6 +8,7 @@ import fr.free.nrw.commons.nearby.Place
|
|||
import fr.free.nrw.commons.nearby.Sitelinks
|
||||
import fr.free.nrw.commons.upload.structure.depictions.DepictedItem
|
||||
import org.wikipedia.wikidata.*
|
||||
import java.util.*
|
||||
|
||||
fun depictedItem(
|
||||
name: String = "label",
|
||||
|
|
@ -29,6 +31,38 @@ fun depictedItem(
|
|||
fun categoryItem(name: String = "name", selected: Boolean = false) =
|
||||
CategoryItem(name, selected)
|
||||
|
||||
fun media(
|
||||
thumbUrl: String? = "thumbUrl",
|
||||
imageUrl: String? = "imageUrl",
|
||||
filename: String? = "filename",
|
||||
fallbackDescription: String? = "fallbackDescription",
|
||||
dateUploaded: Date? = Date(),
|
||||
license: String? = "license",
|
||||
licenseUrl: String? = "licenseUrl",
|
||||
creator: String? = "creator",
|
||||
pageId: String = "pageId",
|
||||
categories: List<String>? = listOf("categories"),
|
||||
coordinates: LatLng? = LatLng(0.0, 0.0, 0.0f),
|
||||
captions: Map<String?, String?> = mapOf("en" to "caption"),
|
||||
descriptions: Map<String?, String?> = mapOf("en" to "description"),
|
||||
depictionIds: List<String> = listOf("depictionId")
|
||||
) = Media(
|
||||
thumbUrl,
|
||||
imageUrl,
|
||||
filename,
|
||||
fallbackDescription,
|
||||
dateUploaded,
|
||||
license,
|
||||
licenseUrl,
|
||||
creator,
|
||||
pageId,
|
||||
categories,
|
||||
coordinates,
|
||||
captions,
|
||||
descriptions,
|
||||
depictionIds
|
||||
)
|
||||
|
||||
fun place(
|
||||
name: String = "name",
|
||||
label: Label? = null,
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
package fr.free.nrw.commons
|
||||
|
||||
import media
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
|
|
@ -11,13 +12,15 @@ import org.robolectric.annotation.Config
|
|||
class MediaTest {
|
||||
@Test
|
||||
fun displayTitleShouldStripExtension() {
|
||||
val m = Media("File:Example.jpg")
|
||||
val m = media(filename = "File:Example.jpg")
|
||||
assertEquals("Example", m.displayTitle)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun displayTitleShouldUseSpaceForUnderscore() {
|
||||
val m = Media("File:Example 1_2.jpg")
|
||||
val m = media(filename = "File:Example 1_2.jpg")
|
||||
assertEquals("Example 1 2", m.displayTitle)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -1,108 +0,0 @@
|
|||
package fr.free.nrw.commons.bookmarks.pictures;
|
||||
|
||||
import android.net.Uri;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import fr.free.nrw.commons.Media;
|
||||
import fr.free.nrw.commons.bookmarks.Bookmark;
|
||||
import fr.free.nrw.commons.media.MediaClient;
|
||||
import io.reactivex.Single;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* Tests for bookmark pictures controller
|
||||
*/
|
||||
public class BookmarkPicturesControllerTest {
|
||||
|
||||
@Mock
|
||||
MediaClient mediaClient;
|
||||
@Mock
|
||||
BookmarkPicturesDao bookmarkDao;
|
||||
|
||||
@InjectMocks
|
||||
BookmarkPicturesController bookmarkPicturesController;
|
||||
|
||||
|
||||
/**
|
||||
* Init mocks
|
||||
*/
|
||||
@Before
|
||||
public void setup() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
Media mockMedia = getMockMedia();
|
||||
when(bookmarkDao.getAllBookmarks())
|
||||
.thenReturn(getMockBookmarkList());
|
||||
when(mediaClient.getMedia(anyString()))
|
||||
.thenReturn(Single.just(mockMedia));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get mock bookmark list
|
||||
* @return
|
||||
*/
|
||||
private List<Bookmark> getMockBookmarkList() {
|
||||
ArrayList<Bookmark> list = new ArrayList<>();
|
||||
list.add(new Bookmark("File:Test1.jpg", "Maskaravivek", Uri.EMPTY));
|
||||
list.add(new Bookmark("File:Test2.jpg", "Maskaravivek", Uri.EMPTY));
|
||||
return list;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test case where all bookmark pictures are fetched and media is found against it
|
||||
*/
|
||||
@Test
|
||||
public void loadBookmarkedPictures() {
|
||||
List<Media> bookmarkedPictures = bookmarkPicturesController.loadBookmarkedPictures().blockingGet();
|
||||
assertEquals(2, bookmarkedPictures.size());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test case where all bookmark pictures are fetched and only one media is found
|
||||
*/
|
||||
@Test
|
||||
public void loadBookmarkedPicturesForNullMedia() {
|
||||
when(mediaClient.getMedia("File:Test1.jpg"))
|
||||
.thenReturn(Single.error(new NullPointerException("Error occurred")));
|
||||
when(mediaClient.getMedia("File:Test2.jpg"))
|
||||
.thenReturn(Single.just(getMockMedia()));
|
||||
List<Media> bookmarkedPictures = bookmarkPicturesController.loadBookmarkedPictures().blockingGet();
|
||||
assertEquals(1, bookmarkedPictures.size());
|
||||
}
|
||||
|
||||
private Media getMockMedia() {
|
||||
return new Media("File:Test.jpg");
|
||||
}
|
||||
|
||||
/**
|
||||
* Test case where current bookmarks don't match the bookmarks in DB
|
||||
*/
|
||||
@Test
|
||||
public void needRefreshBookmarkedPictures() {
|
||||
boolean needRefreshBookmarkedPictures = bookmarkPicturesController.needRefreshBookmarkedPictures();
|
||||
assertTrue(needRefreshBookmarkedPictures);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test case where the DB is up to date with the bookmarks loaded in the list
|
||||
*/
|
||||
@Test
|
||||
public void doNotNeedRefreshBookmarkedPictures() {
|
||||
List<Media> bookmarkedPictures = bookmarkPicturesController.loadBookmarkedPictures().blockingGet();
|
||||
assertEquals(2, bookmarkedPictures.size());
|
||||
boolean needRefreshBookmarkedPictures = bookmarkPicturesController.needRefreshBookmarkedPictures();
|
||||
assertFalse(needRefreshBookmarkedPictures);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,110 @@
|
|||
package fr.free.nrw.commons.bookmarks.pictures
|
||||
|
||||
import android.net.Uri
|
||||
import com.nhaarman.mockitokotlin2.whenever
|
||||
import fr.free.nrw.commons.Media
|
||||
import fr.free.nrw.commons.bookmarks.Bookmark
|
||||
import fr.free.nrw.commons.media.MediaClient
|
||||
import io.reactivex.Single
|
||||
import media
|
||||
import org.junit.Assert
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import org.mockito.ArgumentMatchers
|
||||
import org.mockito.InjectMocks
|
||||
import org.mockito.Mock
|
||||
import org.mockito.MockitoAnnotations
|
||||
import java.util.*
|
||||
|
||||
/**
|
||||
* Tests for bookmark pictures controller
|
||||
*/
|
||||
class BookmarkPicturesControllerTest {
|
||||
@Mock
|
||||
var mediaClient: MediaClient? = null
|
||||
|
||||
@Mock
|
||||
var bookmarkDao: BookmarkPicturesDao? = null
|
||||
|
||||
@InjectMocks
|
||||
var bookmarkPicturesController: BookmarkPicturesController? = null
|
||||
|
||||
/**
|
||||
* Init mocks
|
||||
*/
|
||||
@Before
|
||||
fun setup() {
|
||||
MockitoAnnotations.initMocks(this)
|
||||
val mockMedia = mockMedia
|
||||
whenever(bookmarkDao!!.allBookmarks)
|
||||
.thenReturn(mockBookmarkList)
|
||||
whenever(
|
||||
mediaClient!!.getMedia(
|
||||
ArgumentMatchers.anyString()
|
||||
)
|
||||
)
|
||||
.thenReturn(Single.just(mockMedia))
|
||||
}
|
||||
|
||||
/**
|
||||
* Get mock bookmark list
|
||||
* @return
|
||||
*/
|
||||
private val mockBookmarkList: List<Bookmark>
|
||||
private get() {
|
||||
val list = ArrayList<Bookmark>()
|
||||
list.add(Bookmark("File:Test1.jpg", "Maskaravivek", Uri.EMPTY))
|
||||
list.add(Bookmark("File:Test2.jpg", "Maskaravivek", Uri.EMPTY))
|
||||
return list
|
||||
}
|
||||
|
||||
/**
|
||||
* Test case where all bookmark pictures are fetched and media is found against it
|
||||
*/
|
||||
@Test
|
||||
fun loadBookmarkedPictures() {
|
||||
val bookmarkedPictures =
|
||||
bookmarkPicturesController!!.loadBookmarkedPictures().blockingGet()
|
||||
Assert.assertEquals(2, bookmarkedPictures.size.toLong())
|
||||
}
|
||||
|
||||
/**
|
||||
* Test case where all bookmark pictures are fetched and only one media is found
|
||||
*/
|
||||
@Test
|
||||
fun loadBookmarkedPicturesForNullMedia() {
|
||||
whenever(mediaClient!!.getMedia("File:Test1.jpg"))
|
||||
.thenReturn(Single.error(NullPointerException("Error occurred")))
|
||||
whenever(mediaClient!!.getMedia("File:Test2.jpg"))
|
||||
.thenReturn(Single.just(mockMedia))
|
||||
val bookmarkedPictures =
|
||||
bookmarkPicturesController!!.loadBookmarkedPictures().blockingGet()
|
||||
Assert.assertEquals(1, bookmarkedPictures.size.toLong())
|
||||
}
|
||||
|
||||
private val mockMedia: Media
|
||||
private get() = media(filename="File:Test.jpg")
|
||||
|
||||
/**
|
||||
* Test case where current bookmarks don't match the bookmarks in DB
|
||||
*/
|
||||
@Test
|
||||
fun needRefreshBookmarkedPictures() {
|
||||
val needRefreshBookmarkedPictures =
|
||||
bookmarkPicturesController!!.needRefreshBookmarkedPictures()
|
||||
Assert.assertTrue(needRefreshBookmarkedPictures)
|
||||
}
|
||||
|
||||
/**
|
||||
* Test case where the DB is up to date with the bookmarks loaded in the list
|
||||
*/
|
||||
@Test
|
||||
fun doNotNeedRefreshBookmarkedPictures() {
|
||||
val bookmarkedPictures =
|
||||
bookmarkPicturesController!!.loadBookmarkedPictures().blockingGet()
|
||||
Assert.assertEquals(2, bookmarkedPictures.size.toLong())
|
||||
val needRefreshBookmarkedPictures =
|
||||
bookmarkPicturesController!!.needRefreshBookmarkedPictures()
|
||||
Assert.assertFalse(needRefreshBookmarkedPictures)
|
||||
}
|
||||
}
|
||||
|
|
@ -8,12 +8,12 @@ import fr.free.nrw.commons.auth.SessionManager
|
|||
import fr.free.nrw.commons.mwapi.OkHttpJsonApiClient
|
||||
import fr.free.nrw.commons.utils.ViewUtilWrapper
|
||||
import io.reactivex.Single
|
||||
import media
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import org.mockito.ArgumentMatchers.anyInt
|
||||
import org.mockito.InjectMocks
|
||||
import org.mockito.Mock
|
||||
import org.mockito.Mockito
|
||||
import org.mockito.Mockito.*
|
||||
import org.mockito.MockitoAnnotations
|
||||
import java.util.*
|
||||
|
|
@ -55,11 +55,10 @@ class ReasonBuilderTest {
|
|||
`when`(okHttpJsonApiClient!!.getAchievements(anyString()))
|
||||
.thenReturn(Single.just(mock(FeedbackResponse::class.java)))
|
||||
|
||||
val media = Media("test_file")
|
||||
media.dateUploaded=Date()
|
||||
val media = media(filename="test_file", dateUploaded = Date())
|
||||
|
||||
reasonBuilder!!.getReason(media, "test")
|
||||
verify(sessionManager, times(0))!!.forceLogin(any(Context::class.java))
|
||||
verify(okHttpJsonApiClient, times(1))!!.getAchievements(anyString())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,10 +3,8 @@ package fr.free.nrw.commons.depictions
|
|||
import fr.free.nrw.commons.Media
|
||||
import fr.free.nrw.commons.depictions.Media.DepictedImagesFragment
|
||||
import fr.free.nrw.commons.depictions.Media.DepictedImagesPresenter
|
||||
import fr.free.nrw.commons.explore.depictions.DepictsClient
|
||||
import fr.free.nrw.commons.kvstore.JsonKvStore
|
||||
import fr.free.nrw.commons.media.MediaClient
|
||||
import io.reactivex.Observable
|
||||
import io.reactivex.Single
|
||||
import io.reactivex.schedulers.TestScheduler
|
||||
import org.junit.Before
|
||||
|
|
@ -27,9 +25,6 @@ class DepictedImagesPresenterTest {
|
|||
@Mock
|
||||
lateinit var jsonKvStore: JsonKvStore
|
||||
|
||||
@Mock
|
||||
lateinit var depictsClient: DepictsClient
|
||||
|
||||
@Mock
|
||||
lateinit var mediaClient: MediaClient
|
||||
|
||||
|
|
@ -40,7 +35,7 @@ class DepictedImagesPresenterTest {
|
|||
@Mock
|
||||
lateinit var mediaItem: Media
|
||||
|
||||
var testObservable: Observable<List<Media>>? = null
|
||||
var testSingle: Single<List<Media>>? = null
|
||||
|
||||
|
||||
@Before
|
||||
|
|
@ -49,28 +44,20 @@ class DepictedImagesPresenterTest {
|
|||
MockitoAnnotations.initMocks(this)
|
||||
testScheduler = TestScheduler()
|
||||
mediaList.add(mediaItem)
|
||||
testObservable = Observable.just(mediaList)
|
||||
depictedImagesPresenter = DepictedImagesPresenter(jsonKvStore, depictsClient, mediaClient, testScheduler, testScheduler)
|
||||
testSingle = Single.just(mediaList)
|
||||
depictedImagesPresenter = DepictedImagesPresenter(jsonKvStore,
|
||||
mediaClient, testScheduler, testScheduler)
|
||||
depictedImagesPresenter.onAttachView(view)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun initList() {
|
||||
Mockito.`when`(
|
||||
depictsClient.fetchImagesForDepictedItem(ArgumentMatchers.anyString(),
|
||||
mediaClient.fetchImagesForDepictedItem(ArgumentMatchers.anyString(),
|
||||
ArgumentMatchers.anyInt())
|
||||
).thenReturn(testObservable)
|
||||
).thenReturn(testSingle)
|
||||
depictedImagesPresenter.initList("rabbit")
|
||||
depictedImagesPresenter.handleSuccess(mediaList)
|
||||
verify(view)?.handleSuccess(mediaList)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun replaceTitlesWithCaptions() {
|
||||
var stringObservable: Single<String>? = Single.just(String())
|
||||
Mockito.`when`(mediaClient.getCaptionByWikibaseIdentifier(ArgumentMatchers.anyString()))?.thenReturn(stringObservable)
|
||||
depictedImagesPresenter.replaceTitlesWithCaptions("File:rabbit.jpg", 0)
|
||||
testScheduler.triggerActions()
|
||||
verify(view)?.handleLabelforImage("", 0)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,7 +4,9 @@ import androidx.arch.core.executor.testing.InstantTaskExecutorRule
|
|||
import androidx.lifecycle.LiveData
|
||||
import androidx.paging.PagedList
|
||||
import com.jraska.livedata.test
|
||||
import com.nhaarman.mockitokotlin2.*
|
||||
import com.nhaarman.mockitokotlin2.mock
|
||||
import com.nhaarman.mockitokotlin2.verify
|
||||
import com.nhaarman.mockitokotlin2.whenever
|
||||
import io.reactivex.processors.PublishProcessor
|
||||
import io.reactivex.schedulers.TestScheduler
|
||||
import org.junit.Before
|
||||
|
|
|
|||
|
|
@ -1,10 +1,7 @@
|
|||
package fr.free.nrw.commons.explore.media
|
||||
|
||||
import com.nhaarman.mockitokotlin2.mock
|
||||
import com.nhaarman.mockitokotlin2.verify
|
||||
import com.nhaarman.mockitokotlin2.whenever
|
||||
import fr.free.nrw.commons.Media
|
||||
import fr.free.nrw.commons.depictions.Media.DepictedImagesFragment.PAGE_ID_PREFIX
|
||||
import fr.free.nrw.commons.media.MediaClient
|
||||
import io.reactivex.Single
|
||||
import org.hamcrest.MatcherAssert.assertThat
|
||||
|
|
@ -13,10 +10,6 @@ import org.junit.Before
|
|||
import org.junit.Test
|
||||
import org.mockito.Mock
|
||||
import org.mockito.MockitoAnnotations
|
||||
import org.wikipedia.dataclient.mwapi.MwQueryPage
|
||||
import org.wikipedia.dataclient.mwapi.MwQueryResponse
|
||||
import org.wikipedia.dataclient.mwapi.MwQueryResult
|
||||
import org.wikipedia.wikidata.Entities
|
||||
|
||||
class PageableMediaDataSourceTest {
|
||||
@Mock
|
||||
|
|
@ -31,41 +24,10 @@ class PageableMediaDataSourceTest {
|
|||
|
||||
@Test
|
||||
fun `loadFunction invokes mediaClient and has Label`() {
|
||||
val (media, entity: Entities.Entity) = expectMediaAndEntity()
|
||||
val label: Entities.Label = mock()
|
||||
whenever(entity.labels()).thenReturn(mapOf(" " to label))
|
||||
whenever(label.value()).thenReturn("label")
|
||||
val pageableMediaDataSource = PageableMediaDataSource(mock(), mediaConverter, mediaClient)
|
||||
pageableMediaDataSource.onQueryUpdated("test")
|
||||
assertThat(pageableMediaDataSource.loadFunction(0,1), `is`(listOf(media)))
|
||||
verify(media).caption = "label"
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `loadFunction invokes mediaClient and does not have Label`() {
|
||||
val (media, entity: Entities.Entity) = expectMediaAndEntity()
|
||||
whenever(entity.labels()).thenReturn(mapOf())
|
||||
val pageableMediaDataSource = PageableMediaDataSource(mock(), mediaConverter, mediaClient)
|
||||
pageableMediaDataSource.onQueryUpdated("test")
|
||||
assertThat(pageableMediaDataSource.loadFunction(0,1), `is`(listOf(media)))
|
||||
verify(media).caption = MediaClient.NO_CAPTION
|
||||
}
|
||||
|
||||
private fun expectMediaAndEntity(): Pair<Media, Entities.Entity> {
|
||||
val queryResponse: MwQueryResponse = mock()
|
||||
whenever(mediaClient.getMediaListFromSearch("test", 0, 1))
|
||||
.thenReturn(Single.just(queryResponse))
|
||||
val queryResult: MwQueryResult = mock()
|
||||
whenever(queryResponse.query()).thenReturn(queryResult)
|
||||
val queryPage: MwQueryPage = mock()
|
||||
whenever(queryResult.pages()).thenReturn(listOf(queryPage))
|
||||
val media = mock<Media>()
|
||||
whenever(mediaConverter.convert(queryPage)).thenReturn(media)
|
||||
whenever(media.pageId).thenReturn("1")
|
||||
val entities: Entities = mock()
|
||||
whenever(mediaClient.getEntities("${PAGE_ID_PREFIX}1")).thenReturn(Single.just(entities))
|
||||
val entity: Entities.Entity = mock()
|
||||
whenever(entities.entities()).thenReturn(mapOf("" to entity))
|
||||
return Pair(media, entity)
|
||||
.thenReturn(Single.just(emptyList()))
|
||||
val pageableMediaDataSource = PageableMediaDataSource(mock(), mediaClient)
|
||||
pageableMediaDataSource.onQueryUpdated("test")
|
||||
assertThat(pageableMediaDataSource.loadFunction(0,1), `is`(emptyList()))
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,11 +1,12 @@
|
|||
package fr.free.nrw.commons.media
|
||||
|
||||
import com.nhaarman.mockitokotlin2.mock
|
||||
import com.nhaarman.mockitokotlin2.whenever
|
||||
import fr.free.nrw.commons.Media
|
||||
import fr.free.nrw.commons.explore.media.MediaConverter
|
||||
import fr.free.nrw.commons.media.model.PageMediaListItem
|
||||
import fr.free.nrw.commons.media.model.PageMediaListResponse
|
||||
import fr.free.nrw.commons.utils.CommonsDateUtil
|
||||
import io.reactivex.Observable
|
||||
import io.reactivex.Single
|
||||
import junit.framework.Assert.*
|
||||
import org.junit.Before
|
||||
|
|
@ -17,6 +18,7 @@ import org.wikipedia.dataclient.mwapi.MwQueryPage
|
|||
import org.wikipedia.dataclient.mwapi.MwQueryResponse
|
||||
import org.wikipedia.dataclient.mwapi.MwQueryResult
|
||||
import org.wikipedia.gallery.ImageInfo
|
||||
import org.wikipedia.wikidata.Entities
|
||||
import java.util.*
|
||||
|
||||
|
||||
|
|
@ -24,12 +26,14 @@ class MediaClientTest {
|
|||
|
||||
@Mock
|
||||
internal var mediaInterface: MediaInterface? = null
|
||||
@Mock
|
||||
internal var mediaConverter: MediaConverter? = null
|
||||
@Mock
|
||||
internal var mediaDetailInterface: MediaDetailInterface? = null
|
||||
|
||||
@Mock
|
||||
internal var pageMediaInterface: PageMediaInterface? = null
|
||||
|
||||
@Mock
|
||||
internal var mediaDetailInterface: MediaDetailInterface? = null
|
||||
|
||||
@InjectMocks
|
||||
var mediaClient: MediaClient? = null
|
||||
|
|
@ -51,7 +55,7 @@ class MediaClientTest {
|
|||
`when`(mockResponse.query()).thenReturn(mwQueryResult)
|
||||
|
||||
`when`(mediaInterface!!.checkPageExistsUsingTitle(ArgumentMatchers.anyString()))
|
||||
.thenReturn(Observable.just(mockResponse))
|
||||
.thenReturn(Single.just(mockResponse))
|
||||
|
||||
val checkPageExistsUsingTitle =
|
||||
mediaClient!!.checkPageExistsUsingTitle("File:Test.jpg").blockingGet()
|
||||
|
|
@ -69,7 +73,7 @@ class MediaClientTest {
|
|||
`when`(mockResponse.query()).thenReturn(mwQueryResult)
|
||||
|
||||
`when`(mediaInterface!!.checkPageExistsUsingTitle(ArgumentMatchers.anyString()))
|
||||
.thenReturn(Observable.just(mockResponse))
|
||||
.thenReturn(Single.just(mockResponse))
|
||||
|
||||
val checkPageExistsUsingTitle =
|
||||
mediaClient!!.checkPageExistsUsingTitle("File:Test.jpg").blockingGet()
|
||||
|
|
@ -87,7 +91,7 @@ class MediaClientTest {
|
|||
`when`(mockResponse.query()).thenReturn(mwQueryResult)
|
||||
|
||||
`when`(mediaInterface!!.checkFileExistsUsingSha(ArgumentMatchers.anyString()))
|
||||
.thenReturn(Observable.just(mockResponse))
|
||||
.thenReturn(Single.just(mockResponse))
|
||||
|
||||
val checkFileExistsUsingSha = mediaClient!!.checkFileExistsUsingSha("abcde").blockingGet()
|
||||
assertTrue(checkFileExistsUsingSha)
|
||||
|
|
@ -104,7 +108,7 @@ class MediaClientTest {
|
|||
`when`(mockResponse.query()).thenReturn(mwQueryResult)
|
||||
|
||||
`when`(mediaInterface!!.checkFileExistsUsingSha(ArgumentMatchers.anyString()))
|
||||
.thenReturn(Observable.just(mockResponse))
|
||||
.thenReturn(Single.just(mockResponse))
|
||||
|
||||
val checkFileExistsUsingSha = mediaClient!!.checkFileExistsUsingSha("abcde").blockingGet()
|
||||
assertFalse(checkFileExistsUsingSha)
|
||||
|
|
@ -112,21 +116,12 @@ class MediaClientTest {
|
|||
|
||||
@Test
|
||||
fun getMedia() {
|
||||
val imageInfo = ImageInfo()
|
||||
|
||||
val mwQueryPage = mock(MwQueryPage::class.java)
|
||||
`when`(mwQueryPage.title()).thenReturn("Test")
|
||||
`when`(mwQueryPage.imageInfo()).thenReturn(imageInfo)
|
||||
|
||||
val mwQueryResult = mock(MwQueryResult::class.java)
|
||||
`when`(mwQueryResult.firstPage()).thenReturn(mwQueryPage)
|
||||
val mockResponse = mock(MwQueryResponse::class.java)
|
||||
`when`(mockResponse.query()).thenReturn(mwQueryResult)
|
||||
val (mockResponse, media: Media) = expectGetEntitiesAndMediaConversion()
|
||||
|
||||
`when`(mediaInterface!!.getMedia(ArgumentMatchers.anyString()))
|
||||
.thenReturn(Observable.just(mockResponse))
|
||||
.thenReturn(Single.just(mockResponse))
|
||||
|
||||
assertEquals("Test", mediaClient!!.getMedia("abcde").blockingGet().filename)
|
||||
mediaClient!!.getMedia("abcde").test().assertValue(media)
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
@ -143,34 +138,34 @@ class MediaClientTest {
|
|||
`when`(mockResponse.query()).thenReturn(mwQueryResult)
|
||||
|
||||
`when`(mediaInterface!!.getMedia(ArgumentMatchers.anyString()))
|
||||
.thenReturn(Observable.just(mockResponse))
|
||||
|
||||
assertEquals(Media.EMPTY, mediaClient!!.getMedia("abcde").blockingGet())
|
||||
.thenReturn(Single.just(mockResponse))
|
||||
mediaClient!!.getMedia("abcde").test().assertErrorMessage("empty list passed for ids")
|
||||
}
|
||||
|
||||
@Captor
|
||||
private val filenameCaptor: ArgumentCaptor<String>? = null
|
||||
|
||||
@Test
|
||||
fun getPictureOfTheDay() {
|
||||
val template = "Template:Potd/" + CommonsDateUtil.getIso8601DateFormatShort().format(Date())
|
||||
|
||||
val imageInfo = ImageInfo()
|
||||
val (mockResponse, media: Media) = expectGetEntitiesAndMediaConversion()
|
||||
`when`(mediaInterface!!.getMediaWithGenerator(template))
|
||||
.thenReturn(Single.just(mockResponse))
|
||||
mediaClient!!.getPictureOfTheDay().test().assertValue(media)
|
||||
}
|
||||
|
||||
val mwQueryPage = mock(MwQueryPage::class.java)
|
||||
`when`(mwQueryPage.title()).thenReturn("Test")
|
||||
`when`(mwQueryPage.imageInfo()).thenReturn(imageInfo)
|
||||
|
||||
val mwQueryResult = mock(MwQueryResult::class.java)
|
||||
`when`(mwQueryResult.firstPage()).thenReturn(mwQueryPage)
|
||||
private fun expectGetEntitiesAndMediaConversion(): Pair<MwQueryResponse, Media> {
|
||||
val mockResponse = mock(MwQueryResponse::class.java)
|
||||
`when`(mockResponse.query()).thenReturn(mwQueryResult)
|
||||
|
||||
`when`(mediaInterface!!.getMediaWithGenerator(filenameCaptor!!.capture()))
|
||||
.thenReturn(Observable.just(mockResponse))
|
||||
|
||||
assertEquals("Test", mediaClient!!.pictureOfTheDay.blockingGet().filename)
|
||||
assertEquals(template, filenameCaptor.value);
|
||||
val queryResult: MwQueryResult = mock()
|
||||
whenever(mockResponse.query()).thenReturn(queryResult)
|
||||
val queryPage: MwQueryPage = mock()
|
||||
whenever(queryResult.pages()).thenReturn(listOf(queryPage))
|
||||
whenever(queryPage.pageId()).thenReturn(0)
|
||||
val entities: Entities = mock()
|
||||
whenever(mediaDetailInterface!!.getEntity("M0")).thenReturn(Single.just(entities))
|
||||
val entity: Entities.Entity = mock()
|
||||
whenever(entities.entities()).thenReturn(mapOf("id" to entity))
|
||||
val media: Media = mock()
|
||||
whenever(mediaConverter!!.convert(queryPage, entity)).thenReturn(media)
|
||||
return Pair(mockResponse, media)
|
||||
}
|
||||
|
||||
@Captor
|
||||
|
|
@ -179,17 +174,8 @@ class MediaClientTest {
|
|||
@Test
|
||||
fun getMediaListFromCategoryTwice() {
|
||||
val mockContinuation = mapOf(Pair("gcmcontinue", "test"))
|
||||
val imageInfo = ImageInfo()
|
||||
|
||||
val mwQueryPage = mock(MwQueryPage::class.java)
|
||||
`when`(mwQueryPage.title()).thenReturn("Test")
|
||||
`when`(mwQueryPage.imageInfo()).thenReturn(imageInfo)
|
||||
|
||||
val mwQueryResult = mock(MwQueryResult::class.java)
|
||||
`when`(mwQueryResult.pages()).thenReturn(listOf(mwQueryPage))
|
||||
|
||||
val mockResponse = mock(MwQueryResponse::class.java)
|
||||
`when`(mockResponse.query()).thenReturn(mwQueryResult)
|
||||
val (mockResponse, media: Media) = expectGetEntitiesAndMediaConversion()
|
||||
`when`(mockResponse.continuation()).thenReturn(mockContinuation)
|
||||
|
||||
`when`(
|
||||
|
|
@ -198,31 +184,23 @@ class MediaClientTest {
|
|||
continuationCaptor!!.capture()
|
||||
)
|
||||
)
|
||||
.thenReturn(Observable.just(mockResponse))
|
||||
.thenReturn(Single.just(mockResponse))
|
||||
|
||||
val media1 = mediaClient!!.getMediaListFromCategory("abcde").blockingGet().get(0)
|
||||
val media2 = mediaClient!!.getMediaListFromCategory("abcde").blockingGet().get(0)
|
||||
|
||||
assertEquals(continuationCaptor.allValues[0], emptyMap<String, String>())
|
||||
assertEquals(continuationCaptor.allValues[1], mockContinuation)
|
||||
|
||||
assertEquals(media1.filename, "Test")
|
||||
assertEquals(media2.filename, "Test")
|
||||
assertEquals(media1, media)
|
||||
assertEquals(media2, media)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun getMediaListForUser() {
|
||||
val mockContinuation = mapOf("gcmcontinue" to "test")
|
||||
val imageInfo = ImageInfo()
|
||||
|
||||
val mwQueryPage = mock(MwQueryPage::class.java)
|
||||
whenever(mwQueryPage.title()).thenReturn("Test")
|
||||
whenever(mwQueryPage.imageInfo()).thenReturn(imageInfo)
|
||||
|
||||
val mwQueryResult = mock(MwQueryResult::class.java)
|
||||
whenever(mwQueryResult.pages()).thenReturn(listOf(mwQueryPage))
|
||||
|
||||
val mockResponse = mock(MwQueryResponse::class.java)
|
||||
whenever(mockResponse.query()).thenReturn(mwQueryResult)
|
||||
val (mockResponse, media: Media) = expectGetEntitiesAndMediaConversion()
|
||||
whenever(mockResponse.continuation()).thenReturn(mockContinuation)
|
||||
|
||||
whenever(
|
||||
|
|
@ -231,7 +209,7 @@ class MediaClientTest {
|
|||
continuationCaptor!!.capture()
|
||||
)
|
||||
)
|
||||
.thenReturn(Observable.just(mockResponse))
|
||||
.thenReturn(Single.just(mockResponse))
|
||||
val media1 = mediaClient!!.getMediaListForUser("Test").blockingGet().get(0)
|
||||
val media2 = mediaClient!!.getMediaListForUser("Test").blockingGet().get(0)
|
||||
|
||||
|
|
@ -251,7 +229,7 @@ class MediaClientTest {
|
|||
mockResponse.setParse(mwParseResult)
|
||||
|
||||
`when`(mediaInterface!!.getPageHtml(ArgumentMatchers.anyString()))
|
||||
.thenReturn(Observable.just(mockResponse))
|
||||
.thenReturn(Single.just(mockResponse))
|
||||
|
||||
assertEquals("Test", mediaClient!!.getPageHtml("abcde").blockingGet())
|
||||
}
|
||||
|
|
@ -282,7 +260,7 @@ class MediaClientTest {
|
|||
mockResponse.setParse(null)
|
||||
|
||||
`when`(mediaInterface!!.getPageHtml(ArgumentMatchers.anyString()))
|
||||
.thenReturn(Observable.just(mockResponse))
|
||||
.thenReturn(Single.just(mockResponse))
|
||||
|
||||
assertEquals("", mediaClient!!.getPageHtml("abcde").blockingGet())
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue