#1212 share test model functions between unit/instrumentation tests

This commit is contained in:
Sean Mac Gillicuddy 2019-08-13 13:13:50 +01:00
parent cec8e7e7a0
commit 4fee17d5ea
5 changed files with 115 additions and 113 deletions

View File

@ -258,6 +258,16 @@ android {
}
}
}
sourceSets {
test {
java.srcDirs += "$projectDir/src/testShared"
}
androidTest {
java.srcDirs += "$projectDir/src/testShared"
}
}
flavorDimensions "default"

View File

@ -4,16 +4,8 @@ import okhttp3.mockwebserver.Dispatcher
import okhttp3.mockwebserver.MockResponse
import okhttp3.mockwebserver.MockWebServer
import okhttp3.mockwebserver.RecordedRequest
import org.kiwix.kiwixmobile.di.modules.TestNetworkModule
import org.kiwix.kiwixmobile.library.entity.LibraryNetworkEntity
import org.kiwix.kiwixmobile.library.entity.LibraryNetworkEntity.Book
import org.kiwix.kiwixmobile.library.entity.MetaLinkNetworkEntity
import org.kiwix.kiwixmobile.library.entity.MetaLinkNetworkEntity.FileElement
import org.kiwix.kiwixmobile.library.entity.MetaLinkNetworkEntity.Pieces
import org.kiwix.kiwixmobile.library.entity.MetaLinkNetworkEntity.Url
import org.simpleframework.xml.core.Persister
import java.io.StringWriter
import java.util.LinkedList
import java.util.Stack
/*
@ -38,7 +30,7 @@ class KiwixMockServer {
val queuedResponses: Stack<MockResponse> = Stack()
private val mockWebServer = MockWebServer().apply {
start(PORT)
start(TEST_PORT)
}
fun stop() {
@ -66,85 +58,14 @@ class KiwixMockServer {
queuedResponses.push(mockResponse)
}
companion object {
const val PORT = 8080
private fun <E> Stack<E>.popOrNull() =
if (empty()) null
else pop()
private fun Any.asXmlString() = StringWriter().let {
Persister().write(this, it)
"$it"
}
}
private fun <E> Stack<E>.popOrNull() =
if (empty()) null
else pop()
fun Any.asXmlString() = StringWriter().let {
Persister().write(this, it)
"$it"
}
fun metaLinkNetworkEntity() = MetaLinkNetworkEntity().apply {
file = fileElement()
}
fun fileElement(
urls: List<Url> = listOf(
url()
),
name: String = "name",
hashes: Map<String, String> = mapOf("hash" to "hash"),
pieces: Pieces = pieces()
) = FileElement().apply {
this.name = name
this.urls = urls
this.hashes = hashes
this.pieces = pieces
}
fun pieces(
hashType: String = "hashType",
pieceHashes: List<String> = listOf("hash")
) = Pieces().apply {
this.hashType = hashType
this.pieceHashes = pieceHashes
}
fun url(
value: String = "${TestNetworkModule.MOCK_BASE_URL}relevantUrl.zim.meta4",
location: String = "location"
) = Url().apply {
this.location = location
this.value = value
}
fun book(
id: String = "id",
title: String = "title",
description: String = "description",
language: String = "eng",
creator: String = "creator",
publisher: String = "publisher",
date: String = "date",
url: String = "${TestNetworkModule.MOCK_BASE_URL}url",
articleCount: String = "mediaCount",
mediaCount: String = "mediaCount",
size: String = "1024",
name: String = "name",
favIcon: String = "favIcon"
) =
Book().apply {
this.id = id
this.title = title
this.description = description
this.language = language
this.creator = creator
this.publisher = publisher
this.date = date
this.url = url
this.articleCount = articleCount
this.mediaCount = mediaCount
this.size = size
bookName = name
favicon = favIcon
}
fun libraryNetworkEntity(books: List<Book> = emptyList()) = LibraryNetworkEntity().apply {
book = LinkedList(books)
}

View File

@ -19,7 +19,7 @@ package org.kiwix.kiwixmobile.di.modules
import dagger.Module
import okhttp3.OkHttpClient
import org.kiwix.kiwixmobile.KiwixMockServer
import org.kiwix.kiwixmobile.MOCK_BASE_URL
import org.kiwix.kiwixmobile.data.remote.KiwixService
/**
@ -35,7 +35,4 @@ class TestNetworkModule : NetworkModule() {
MOCK_BASE_URL
)
companion object {
const val MOCK_BASE_URL = "http://localhost:${KiwixMockServer.PORT}/"
}
}

View File

@ -0,0 +1,21 @@
/*
* Kiwix Android
* Copyright (C) 2018 Kiwix <android.kiwix.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.kiwix.kiwixmobile
const val TEST_PORT = 8080
const val MOCK_BASE_URL = "http://localhost:$TEST_PORT/"

View File

@ -22,10 +22,16 @@ import org.kiwix.kiwixmobile.downloader.model.DownloadState
import org.kiwix.kiwixmobile.downloader.model.DownloadState.Pending
import org.kiwix.kiwixmobile.downloader.model.DownloadStatus
import org.kiwix.kiwixmobile.language.adapter.LanguageListItem.LanguageItem
import org.kiwix.kiwixmobile.library.entity.LibraryNetworkEntity
import org.kiwix.kiwixmobile.library.entity.LibraryNetworkEntity.Book
import org.kiwix.kiwixmobile.library.entity.MetaLinkNetworkEntity
import org.kiwix.kiwixmobile.library.entity.MetaLinkNetworkEntity.FileElement
import org.kiwix.kiwixmobile.library.entity.MetaLinkNetworkEntity.Pieces
import org.kiwix.kiwixmobile.library.entity.MetaLinkNetworkEntity.Url
import org.kiwix.kiwixmobile.zim_manager.Language
import org.kiwix.kiwixmobile.zim_manager.fileselect_view.adapter.BooksOnDiskListItem.BookOnDisk
import java.io.File
import java.util.LinkedList
fun bookOnDisk(
book: Book = book(),
@ -33,28 +39,6 @@ fun bookOnDisk(
file: File = File("")
) = BookOnDisk(databaseId, book, file)
fun book(
id: String = "0",
title: String = "",
size: String = "",
favicon: String = "",
creator: String = "",
publisher: String = "",
date: String = "",
description: String = "",
language: String = ""
) = Book().apply {
this.id = id
this.title = title
this.size = size
this.favicon = favicon
this.creator = creator
this.publisher = publisher
this.date = date
this.description = description
this.language = language
}
fun downloadStatus(
downloadId: Long = 0L,
title: String = "",
@ -94,3 +78,72 @@ fun language(
fun languageItem(language: Language = language()) =
LanguageItem(language)
fun metaLinkNetworkEntity() = MetaLinkNetworkEntity().apply {
file = fileElement()
}
fun fileElement(
urls: List<Url> = listOf(
url()
),
name: String = "name",
hashes: Map<String, String> = mapOf("hash" to "hash"),
pieces: Pieces = pieces()
) = FileElement().apply {
this.name = name
this.urls = urls
this.hashes = hashes
this.pieces = pieces
}
fun pieces(
hashType: String = "hashType",
pieceHashes: List<String> = listOf("hash")
) = Pieces().apply {
this.hashType = hashType
this.pieceHashes = pieceHashes
}
fun url(
value: String = "${MOCK_BASE_URL}relevantUrl.zim.meta4",
location: String = "location"
) = Url().apply {
this.location = location
this.value = value
}
fun book(
id: String = "id",
title: String = "title",
description: String = "description",
language: String = "eng",
creator: String = "creator",
publisher: String = "publisher",
date: String = "date",
url: String = "${MOCK_BASE_URL}url",
articleCount: String = "mediaCount",
mediaCount: String = "mediaCount",
size: String = "1024",
name: String = "name",
favIcon: String = "favIcon"
) =
Book().apply {
this.id = id
this.title = title
this.description = description
this.language = language
this.creator = creator
this.publisher = publisher
this.date = date
this.url = url
this.articleCount = articleCount
this.mediaCount = mediaCount
this.size = size
bookName = name
favicon = favIcon
}
fun libraryNetworkEntity(books: List<Book> = emptyList()) = LibraryNetworkEntity().apply {
book = LinkedList(books)
}