#1386 get tests compiling

This commit is contained in:
Sean Mac Gillicuddy 2019-09-03 17:10:11 +01:00
parent 8458d721d5
commit f7f4d6e1f1
7 changed files with 1621 additions and 1029 deletions

File diff suppressed because it is too large Load Diff

View File

@ -3,74 +3,6 @@
"_note2": "ObjectBox manages crucial IDs for your object model. See docs for details.", "_note2": "ObjectBox manages crucial IDs for your object model. See docs for details.",
"_note3": "If you have VCS merge conflicts, you must resolve them according to ObjectBox docs.", "_note3": "If you have VCS merge conflicts, you must resolve them according to ObjectBox docs.",
"entities": [ "entities": [
{
"id": "1:7257718270326155947",
"lastPropertyId": "17:8085320504542486236",
"name": "DownloadEntity",
"properties": [
{
"id": "1:2266566996008201697",
"name": "id"
},
{
"id": "2:1953917250527765737",
"name": "downloadId"
},
{
"id": "5:6575412958851693470",
"name": "bookId"
},
{
"id": "6:1075612111256674117",
"name": "title"
},
{
"id": "7:2831524841121029990",
"name": "description"
},
{
"id": "8:2334902404590133038",
"name": "language"
},
{
"id": "9:5087250349738158996",
"name": "creator"
},
{
"id": "10:6128960350043895299",
"name": "publisher"
},
{
"id": "11:3850323036475883785",
"name": "date"
},
{
"id": "12:5288623325038033644",
"name": "url"
},
{
"id": "13:2501711400901908648",
"name": "articleCount"
},
{
"id": "14:3550975911715416030",
"name": "mediaCount"
},
{
"id": "15:8949996430663588693",
"name": "size"
},
{
"id": "16:7554483297276446029",
"name": "name"
},
{
"id": "17:8085320504542486236",
"name": "favIcon"
}
],
"relations": []
},
{ {
"id": "3:5536749840871435068", "id": "3:5536749840871435068",
"lastPropertyId": "16:6142333908132117423", "lastPropertyId": "16:6142333908132117423",
@ -367,7 +299,8 @@
"modelVersion": 4, "modelVersion": 4,
"modelVersionParserMinimum": 4, "modelVersionParserMinimum": 4,
"retiredEntityUids": [ "retiredEntityUids": [
349148274283701276 349148274283701276,
7257718270326155947
], ],
"retiredIndexUids": [ "retiredIndexUids": [
1293695782925933448, 1293695782925933448,
@ -394,7 +327,22 @@
7273406943564025911, 7273406943564025911,
428251106490095982, 428251106490095982,
5162677841083528491, 5162677841083528491,
7886541039889727771 7886541039889727771,
2266566996008201697,
1953917250527765737,
6575412958851693470,
1075612111256674117,
2831524841121029990,
2334902404590133038,
5087250349738158996,
6128960350043895299,
3850323036475883785,
5288623325038033644,
2501711400901908648,
3550975911715416030,
8949996430663588693,
7554483297276446029,
8085320504542486236
], ],
"retiredRelationUids": [], "retiredRelationUids": [],
"version": 1 "version": 1

View File

@ -51,9 +51,7 @@ class DownloadViewHolder(override val containerView: View) : RecyclerView.ViewHo
stop.setOnClickListener { stop.setOnClickListener {
itemClickListener.invoke(downloadItem) itemClickListener.invoke(downloadItem)
} }
downloadState.text = toReadableState( downloadState.text = toReadableState(downloadItem.downloadState, containerView.context)
downloadItem.downloadState, containerView.context
)
eta.text = downloadItem.eta.takeIf { it.seconds > 0L }?.toHumanReadableTime() ?: "" eta.text = downloadItem.eta.takeIf { it.seconds > 0L }?.toHumanReadableTime() ?: ""
} }

View File

@ -61,7 +61,7 @@ class FileSearchTest {
every { Environment.getExternalStorageDirectory() } returns externalStorageDirectory every { Environment.getExternalStorageDirectory() } returns externalStorageDirectory
every { externalStorageDirectory.absolutePath } returns "/externalStorageDirectory" every { externalStorageDirectory.absolutePath } returns "/externalStorageDirectory"
every { context.contentResolver } returns contentResolver every { context.contentResolver } returns contentResolver
every { StorageDeviceUtils.getReadableStorage(context, false) } returns arrayListOf( every { StorageDeviceUtils.getReadableStorage(context) } returns arrayListOf(
storageDevice storageDevice
) )
every { storageDevice.name } returns "/deviceDir" every { storageDevice.name } returns "/deviceDir"
@ -80,7 +80,7 @@ class FileSearchTest {
@Test @Test
fun `scan of directory that doesn't exist returns nothing`() { fun `scan of directory that doesn't exist returns nothing`() {
every { contentResolver.query(any(), any(), any(), any(), any()) } returns null every { contentResolver.query(any(), any(), any(), any(), any()) } returns null
fileSearch.scan("doesNotExist") fileSearch.scan()
.test() .test()
.assertValue(listOf()) .assertValue(listOf())
} }
@ -91,7 +91,8 @@ class FileSearchTest {
val zimaaFile = File.createTempFile("fileToFind2", ".zimaa") val zimaaFile = File.createTempFile("fileToFind2", ".zimaa")
File.createTempFile("willNotFind", ".txt") File.createTempFile("willNotFind", ".txt")
every { contentResolver.query(any(), any(), any(), any(), any()) } returns null every { contentResolver.query(any(), any(), any(), any(), any()) } returns null
val fileList = fileSearch.scan(zimFile.parent) every { storageDevice.name } returns zimFile.parent
val fileList = fileSearch.scan()
.test() .test()
.values()[0] .values()[0]
assertThat(fileList).containsExactlyInAnyOrder(zimFile, zimaaFile) assertThat(fileList).containsExactlyInAnyOrder(zimFile, zimaaFile)
@ -106,7 +107,8 @@ class FileSearchTest {
".zim", ".zim",
File("$tempRoot${File.separator}dir").apply { mkdirs() }) File("$tempRoot${File.separator}dir").apply { mkdirs() })
every { contentResolver.query(any(), any(), any(), any(), any()) } returns null every { contentResolver.query(any(), any(), any(), any(), any()) } returns null
val fileList = fileSearch.scan(zimFile.parentFile.parent) every { storageDevice.name } returns tempRoot
val fileList = fileSearch.scan()
.test() .test()
.values()[0] .values()[0]
assertThat(fileList).containsExactlyInAnyOrder(zimFile) assertThat(fileList).containsExactlyInAnyOrder(zimFile)
@ -120,7 +122,7 @@ class FileSearchTest {
fun `scan media store, if files are readable they are returned`() { fun `scan media store, if files are readable they are returned`() {
val fileToFind = File.createTempFile("fileToFind", ".zim") val fileToFind = File.createTempFile("fileToFind", ".zim")
expectFromMediaStore(fileToFind) expectFromMediaStore(fileToFind)
fileSearch.scan("") fileSearch.scan()
.test() .test()
.assertValue(listOf(fileToFind)) .assertValue(listOf(fileToFind))
} }
@ -130,7 +132,7 @@ class FileSearchTest {
val unreadableFile = File.createTempFile("fileToFind", ".zim") val unreadableFile = File.createTempFile("fileToFind", ".zim")
expectFromMediaStore(unreadableFile) expectFromMediaStore(unreadableFile)
unreadableFile.delete() unreadableFile.delete()
fileSearch.scan("") fileSearch.scan()
.test() .test()
.assertValue(listOf()) .assertValue(listOf())
} }

View File

@ -25,6 +25,7 @@ import io.mockk.every
import io.mockk.mockk import io.mockk.mockk
import io.mockk.verify import io.mockk.verify
import io.reactivex.Single import io.reactivex.Single
import io.reactivex.processors.BehaviorProcessor
import io.reactivex.processors.PublishProcessor import io.reactivex.processors.PublishProcessor
import io.reactivex.schedulers.TestScheduler import io.reactivex.schedulers.TestScheduler
import org.junit.jupiter.api.AfterAll import org.junit.jupiter.api.AfterAll
@ -38,17 +39,12 @@ import org.kiwix.kiwixmobile.book
import org.kiwix.kiwixmobile.bookOnDisk import org.kiwix.kiwixmobile.bookOnDisk
import org.kiwix.kiwixmobile.data.DataSource import org.kiwix.kiwixmobile.data.DataSource
import org.kiwix.kiwixmobile.data.remote.KiwixService import org.kiwix.kiwixmobile.data.remote.KiwixService
import org.kiwix.kiwixmobile.database.newdb.dao.FetchDownloadDao
import org.kiwix.kiwixmobile.database.newdb.dao.NewBookDao import org.kiwix.kiwixmobile.database.newdb.dao.NewBookDao
import org.kiwix.kiwixmobile.database.newdb.dao.NewDownloadDao
import org.kiwix.kiwixmobile.database.newdb.dao.NewLanguagesDao import org.kiwix.kiwixmobile.database.newdb.dao.NewLanguagesDao
import org.kiwix.kiwixmobile.downloadItem
import org.kiwix.kiwixmobile.downloadModel import org.kiwix.kiwixmobile.downloadModel
import org.kiwix.kiwixmobile.downloadStatus
import org.kiwix.kiwixmobile.downloader.Downloader
import org.kiwix.kiwixmobile.downloader.model.DownloadItem
import org.kiwix.kiwixmobile.downloader.model.DownloadModel import org.kiwix.kiwixmobile.downloader.model.DownloadModel
import org.kiwix.kiwixmobile.downloader.model.DownloadState
import org.kiwix.kiwixmobile.downloader.model.DownloadStatus
import org.kiwix.kiwixmobile.downloader.model.UriToFileConverter
import org.kiwix.kiwixmobile.language import org.kiwix.kiwixmobile.language
import org.kiwix.kiwixmobile.library.entity.LibraryNetworkEntity.Book import org.kiwix.kiwixmobile.library.entity.LibraryNetworkEntity.Book
import org.kiwix.kiwixmobile.libraryNetworkEntity import org.kiwix.kiwixmobile.libraryNetworkEntity
@ -65,7 +61,6 @@ import org.kiwix.kiwixmobile.zim_manager.fileselect_view.StorageObserver
import org.kiwix.kiwixmobile.zim_manager.fileselect_view.adapter.BooksOnDiskListItem import org.kiwix.kiwixmobile.zim_manager.fileselect_view.adapter.BooksOnDiskListItem
import org.kiwix.kiwixmobile.zim_manager.fileselect_view.adapter.BooksOnDiskListItem.BookOnDisk import org.kiwix.kiwixmobile.zim_manager.fileselect_view.adapter.BooksOnDiskListItem.BookOnDisk
import org.kiwix.kiwixmobile.zim_manager.library_view.adapter.LibraryListItem import org.kiwix.kiwixmobile.zim_manager.library_view.adapter.LibraryListItem
import java.io.File
import java.util.Locale import java.util.Locale
import java.util.concurrent.TimeUnit.MILLISECONDS import java.util.concurrent.TimeUnit.MILLISECONDS
import java.util.concurrent.TimeUnit.SECONDS import java.util.concurrent.TimeUnit.SECONDS
@ -73,17 +68,15 @@ import java.util.concurrent.TimeUnit.SECONDS
@ExtendWith(InstantExecutorExtension::class) @ExtendWith(InstantExecutorExtension::class)
class ZimManageViewModelTest { class ZimManageViewModelTest {
private val newDownloadDao: NewDownloadDao = mockk() private val downloadDao: FetchDownloadDao = mockk()
private val newBookDao: NewBookDao = mockk() private val newBookDao: NewBookDao = mockk()
private val newLanguagesDao: NewLanguagesDao = mockk() private val newLanguagesDao: NewLanguagesDao = mockk()
private val downloader: Downloader = mockk()
private val storageObserver: StorageObserver = mockk() private val storageObserver: StorageObserver = mockk()
private val kiwixService: KiwixService = mockk() private val kiwixService: KiwixService = mockk()
private val application: Application = mockk() private val application: Application = mockk()
private val connectivityBroadcastReceiver: ConnectivityBroadcastReceiver = mockk() private val connectivityBroadcastReceiver: ConnectivityBroadcastReceiver = mockk()
private val bookUtils: BookUtils = mockk() private val bookUtils: BookUtils = mockk()
private val fat32Checker: Fat32Checker = mockk() private val fat32Checker: Fat32Checker = mockk()
private val uriToFileConverter: UriToFileConverter = mockk()
private val defaultLanguageProvider: DefaultLanguageProvider = mockk() private val defaultLanguageProvider: DefaultLanguageProvider = mockk()
private val dataSource: DataSource = mockk() private val dataSource: DataSource = mockk()
lateinit var viewModel: ZimManageViewModel lateinit var viewModel: ZimManageViewModel
@ -92,7 +85,7 @@ class ZimManageViewModelTest {
private val booksOnFileSystem: PublishProcessor<List<BookOnDisk>> = PublishProcessor.create() private val booksOnFileSystem: PublishProcessor<List<BookOnDisk>> = PublishProcessor.create()
private val books: PublishProcessor<List<BookOnDisk>> = PublishProcessor.create() private val books: PublishProcessor<List<BookOnDisk>> = PublishProcessor.create()
private val languages: PublishProcessor<List<Language>> = PublishProcessor.create() private val languages: PublishProcessor<List<Language>> = PublishProcessor.create()
private val fileSystemStates: PublishProcessor<FileSystemState> = PublishProcessor.create() private val fileSystemStates: BehaviorProcessor<FileSystemState> = BehaviorProcessor.create()
private val networkStates: PublishProcessor<NetworkState> = PublishProcessor.create() private val networkStates: PublishProcessor<NetworkState> = PublishProcessor.create()
private val booksOnDiskListItems: PublishProcessor<List<BooksOnDiskListItem>> = private val booksOnDiskListItems: PublishProcessor<List<BooksOnDiskListItem>> =
PublishProcessor.create() PublishProcessor.create()
@ -112,7 +105,7 @@ class ZimManageViewModelTest {
fun init() { fun init() {
clearAllMocks() clearAllMocks()
every { connectivityBroadcastReceiver.action } returns "test" every { connectivityBroadcastReceiver.action } returns "test"
every { newDownloadDao.downloads() } returns downloads every { downloadDao.downloads() } returns downloads
every { newBookDao.books() } returns books every { newBookDao.books() } returns books
every { storageObserver.booksOnFileSystem } returns booksOnFileSystem every { storageObserver.booksOnFileSystem } returns booksOnFileSystem
every { newLanguagesDao.languages() } returns languages every { newLanguagesDao.languages() } returns languages
@ -121,9 +114,17 @@ class ZimManageViewModelTest {
every { application.registerReceiver(any(), any()) } returns mockk() every { application.registerReceiver(any(), any()) } returns mockk()
every { dataSource.booksOnDiskAsListItems() } returns booksOnDiskListItems every { dataSource.booksOnDiskAsListItems() } returns booksOnDiskListItems
viewModel = ZimManageViewModel( viewModel = ZimManageViewModel(
newDownloadDao, newBookDao, newLanguagesDao, downloader, downloadDao,
storageObserver, kiwixService, application, connectivityBroadcastReceiver, bookUtils, newBookDao,
fat32Checker, uriToFileConverter, defaultLanguageProvider, dataSource newLanguagesDao,
storageObserver,
kiwixService,
application,
connectivityBroadcastReceiver,
bookUtils,
fat32Checker,
defaultLanguageProvider,
dataSource
) )
testScheduler.triggerActions() testScheduler.triggerActions()
} }
@ -150,63 +151,19 @@ class ZimManageViewModelTest {
@Nested @Nested
inner class Downloads { inner class Downloads {
@Test @Test
fun `on emission from database query and render downloads`() { fun `on emission from database render downloads`() {
val expectedStatus = downloadStatus() expectDownloads()
expectStatusWith(listOf(expectedStatus))
viewModel.downloadItems viewModel.downloadItems
.test() .test()
.assertValue(listOf(DownloadItem(expectedStatus))) .assertValue(listOf(downloadItem()))
} }
@Test private fun expectDownloads(
fun `on emission of successful status create a book and delete the download`() {
every { uriToFileConverter.convert(any()) } returns File("test")
val expectedStatus = downloadStatus(
downloadId = 10L,
downloadState = DownloadState.Successful
)
expectStatusWith(listOf(expectedStatus))
val element = expectedStatus.toBookOnDisk(uriToFileConverter)
verify {
newBookDao.insert(listOf(element))
newDownloadDao.delete(10L)
}
}
@Test
fun `if statuses don't have a matching Id for download in db over 3 secs then delete`() {
expectStatusWith(
listOf(downloadStatus(downloadId = 1)),
listOf(downloadModel(downloadId = 1), downloadModel(downloadId = 3))
)
testScheduler.advanceTimeBy(3, SECONDS)
testScheduler.triggerActions()
verify {
newDownloadDao.delete(3)
}
}
@Test
fun `if statuses do have a matching Id for download in db over 3 secs then don't delete`() {
expectStatusWith(
listOf(downloadStatus(downloadId = 1)),
listOf(downloadModel(downloadId = 1))
)
testScheduler.advanceTimeBy(3, SECONDS)
testScheduler.triggerActions()
verify(exactly = 0) {
newDownloadDao.delete(any())
}
}
private fun expectStatusWith(
expectedStatuses: List<DownloadStatus>,
expectedDownloads: List<DownloadModel> = listOf( expectedDownloads: List<DownloadModel> = listOf(
downloadModel() downloadModel()
) )
) { ) {
every { application.getString(any()) } returns "" every { application.getString(any()) } returns ""
every { downloader.queryStatus(expectedDownloads) } returns expectedStatuses
downloads.offer(expectedDownloads) downloads.offer(expectedDownloads)
testScheduler.triggerActions() testScheduler.triggerActions()
testScheduler.advanceTimeBy(1, SECONDS) testScheduler.advanceTimeBy(1, SECONDS)
@ -387,7 +344,6 @@ class ZimManageViewModelTest {
@Test @Test
fun `library update removes from sources`() { fun `library update removes from sources`() {
every { downloader.queryStatus(any()) } returns emptyList()
every { application.getString(R.string.your_languages) } returns "1" every { application.getString(R.string.your_languages) } returns "1"
every { application.getString(R.string.other_languages) } returns "2" every { application.getString(R.string.other_languages) } returns "2"
val bookAlreadyOnDisk = book( val bookAlreadyOnDisk = book(

View File

@ -32,7 +32,7 @@ import org.junit.jupiter.api.Test
import org.kiwix.kiwixmobile.book import org.kiwix.kiwixmobile.book
import org.kiwix.kiwixmobile.bookOnDisk import org.kiwix.kiwixmobile.bookOnDisk
import org.kiwix.kiwixmobile.data.ZimContentProvider import org.kiwix.kiwixmobile.data.ZimContentProvider
import org.kiwix.kiwixmobile.database.newdb.dao.NewDownloadDao import org.kiwix.kiwixmobile.database.newdb.dao.FetchDownloadDao
import org.kiwix.kiwixmobile.downloader.model.DownloadModel import org.kiwix.kiwixmobile.downloader.model.DownloadModel
import org.kiwix.kiwixmobile.library.entity.LibraryNetworkEntity.Book import org.kiwix.kiwixmobile.library.entity.LibraryNetworkEntity.Book
import org.kiwix.kiwixmobile.resetSchedulers import org.kiwix.kiwixmobile.resetSchedulers
@ -44,7 +44,7 @@ import java.io.File
class StorageObserverTest { class StorageObserverTest {
private val sharedPreferenceUtil: SharedPreferenceUtil = mockk() private val sharedPreferenceUtil: SharedPreferenceUtil = mockk()
private val newDownloadDao: NewDownloadDao = mockk() private val downloadDao: FetchDownloadDao = mockk()
private val fileSearch: FileSearch = mockk() private val fileSearch: FileSearch = mockk()
private val downloadModel = mockk<DownloadModel>() private val downloadModel = mockk<DownloadModel>()
private val file = mockk<File>() private val file = mockk<File>()
@ -66,9 +66,9 @@ class StorageObserverTest {
@BeforeEach fun init() { @BeforeEach fun init() {
clearAllMocks() clearAllMocks()
every { sharedPreferenceUtil.prefStorage } returns "a" every { sharedPreferenceUtil.prefStorage } returns "a"
every { fileSearch.scan("a") } returns files every { fileSearch.scan() } returns files
every { newDownloadDao.downloads() } returns downloads every { downloadDao.downloads() } returns downloads
storageObserver = StorageObserver(newDownloadDao, fileSearch) storageObserver = StorageObserver(downloadDao, fileSearch)
} }
@Test @Test

View File

@ -17,10 +17,15 @@
*/ */
package org.kiwix.kiwixmobile package org.kiwix.kiwixmobile
import com.tonyodev.fetch2.Error
import com.tonyodev.fetch2.Status
import com.tonyodev.fetch2.Status.NONE
import org.kiwix.kiwixmobile.downloader.model.Base64String
import org.kiwix.kiwixmobile.downloader.model.DownloadItem
import org.kiwix.kiwixmobile.downloader.model.DownloadModel import org.kiwix.kiwixmobile.downloader.model.DownloadModel
import org.kiwix.kiwixmobile.downloader.model.DownloadState import org.kiwix.kiwixmobile.downloader.model.DownloadState
import org.kiwix.kiwixmobile.downloader.model.DownloadState.Pending import org.kiwix.kiwixmobile.downloader.model.DownloadState.Pending
import org.kiwix.kiwixmobile.downloader.model.DownloadStatus import org.kiwix.kiwixmobile.downloader.model.Seconds
import org.kiwix.kiwixmobile.language.adapter.LanguageListItem.LanguageItem import org.kiwix.kiwixmobile.language.adapter.LanguageListItem.LanguageItem
import org.kiwix.kiwixmobile.library.entity.LibraryNetworkEntity import org.kiwix.kiwixmobile.library.entity.LibraryNetworkEntity
import org.kiwix.kiwixmobile.library.entity.LibraryNetworkEntity.Book import org.kiwix.kiwixmobile.library.entity.LibraryNetworkEntity.Book
@ -39,29 +44,37 @@ fun bookOnDisk(
file: File = File("") file: File = File("")
) = BookOnDisk(databaseId, book, file) ) = BookOnDisk(databaseId, book, file)
fun downloadStatus( fun downloadModel(
downloadId: Long = 0L, databaseId: Long = 1L,
title: String = "", downloadId: Long = 1L,
description: String = "", file: String = "",
downloadState: DownloadState = Pending, etaInMilliSeconds: Long = 0L,
bytesDownloadedSoFar: Long = 0L, bytesDownloaded: Long = 1L,
totalSizeBytes: Long = 0L, totalSizeOfDownload: Long = 1L,
lastModified: String = "", status: Status = NONE,
localUri: String? = null, error: Error = Error.NONE,
mediaProviderUri: String? = null, progress: Int = 1,
mediaType: String? = null,
uri: String? = null,
book: Book = book() book: Book = book()
) = DownloadStatus( ) = DownloadModel(
downloadId, title, description, downloadState, bytesDownloadedSoFar, databaseId, downloadId, file, etaInMilliSeconds, bytesDownloaded, totalSizeOfDownload,
totalSizeBytes, lastModified, localUri, mediaProviderUri, mediaType, uri, book status, error, progress, book
) )
fun downloadModel( fun downloadItem(
databaseId: Long? = 1L,
downloadId: Long = 1L, downloadId: Long = 1L,
book: Book = book() favIcon: Base64String = Base64String("favIcon"),
) = DownloadModel(databaseId, downloadId, book) title: String = "title",
description: String = "description",
bytesDownloaded: Long = 1L,
totalSizeBytes: Long = 1L,
progress: Int = 1,
eta: Seconds = Seconds(0),
state: DownloadState = Pending
) =
DownloadItem(
downloadId, favIcon, title, description, bytesDownloaded,
totalSizeBytes, progress, eta, state
)
fun language( fun language(
id: Long = 0, id: Long = 0,