Update Koin to 3.0

This commit is contained in:
Koitharu
2021-03-29 19:18:08 +03:00
parent b07fcf5842
commit b17d8efa5c
3 changed files with 58 additions and 49 deletions

View File

@@ -92,8 +92,8 @@ dependencies {
implementation 'com.hannesdorfmann:adapterdelegates4-kotlin-dsl:4.3.0' implementation 'com.hannesdorfmann:adapterdelegates4-kotlin-dsl:4.3.0'
implementation 'com.hannesdorfmann:adapterdelegates4-kotlin-dsl-viewbinding:4.3.0' implementation 'com.hannesdorfmann:adapterdelegates4-kotlin-dsl-viewbinding:4.3.0'
implementation 'org.koin:koin-android:2.2.2' implementation 'io.insert-koin:koin-android:3.0.1-beta-2'
implementation 'org.koin:koin-androidx-viewmodel:2.2.2' implementation 'io.insert-koin:koin-android-ext:3.0.1-beta-2'
implementation 'io.coil-kt:coil-base:1.1.1' implementation 'io.coil-kt:coil-base:1.1.1'
implementation 'com.davemorrissey.labs:subsampling-scale-image-view-androidx:3.10.0' implementation 'com.davemorrissey.labs:subsampling-scale-image-view-androidx:3.10.0'
implementation 'com.tomclaw.cache:cache:1.0' implementation 'com.tomclaw.cache:cache:1.0'
@@ -102,5 +102,5 @@ dependencies {
testImplementation 'junit:junit:4.13.2' testImplementation 'junit:junit:4.13.2'
testImplementation 'org.json:json:20201115' testImplementation 'org.json:json:20201115'
testImplementation 'org.koin:koin-test:2.2.2' testImplementation 'io.insert-koin:koin-test-junit4:3.0.1-beta-2'
} }

View File

@@ -1,35 +1,32 @@
package org.koitharu.kotatsu.parsers package org.koitharu.kotatsu.parsers
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
import okhttp3.CookieJar
import okhttp3.OkHttpClient
import org.junit.Assert import org.junit.Assert
import org.junit.BeforeClass import org.junit.Rule
import org.junit.Test import org.junit.Test
import org.junit.runner.RunWith import org.junit.runner.RunWith
import org.junit.runners.Parameterized import org.junit.runners.Parameterized
import org.koin.core.context.startKoin import org.koin.core.component.inject
import org.koin.dsl.module import org.koin.core.parameter.parametersOf
import org.koin.test.KoinTest import org.koin.test.KoinTest
import org.koin.test.get import org.koin.test.KoinTestRule
import org.koitharu.kotatsu.base.domain.MangaLoaderContext
import org.koitharu.kotatsu.core.model.MangaSource import org.koitharu.kotatsu.core.model.MangaSource
import org.koitharu.kotatsu.core.network.UserAgentInterceptor
import org.koitharu.kotatsu.core.parser.RemoteMangaRepository import org.koitharu.kotatsu.core.parser.RemoteMangaRepository
import org.koitharu.kotatsu.core.prefs.SourceSettings
import org.koitharu.kotatsu.utils.AssertX import org.koitharu.kotatsu.utils.AssertX
import org.koitharu.kotatsu.utils.ext.isDistinctBy import org.koitharu.kotatsu.utils.ext.isDistinctBy
import java.util.concurrent.TimeUnit
@RunWith(Parameterized::class) @RunWith(Parameterized::class)
class RemoteRepositoryTest(source: MangaSource) : KoinTest { class RemoteRepositoryTest(source: MangaSource) : KoinTest {
private val repo = try { private val repo by inject<RemoteMangaRepository> {
source.cls.getDeclaredConstructor(MangaLoaderContext::class.java) parametersOf(source)
.newInstance(get<MangaLoaderContext>()) }
} catch (e: NoSuchMethodException) {
source.cls.newInstance() @get:Rule
} as RemoteMangaRepository val koinTestRule = KoinTestRule.create {
printLogger()
modules(repositoryTestModule)
}
@Test @Test
fun list() { fun list() {
@@ -71,8 +68,8 @@ class RemoteRepositoryTest(source: MangaSource) : KoinTest {
val tags = runBlocking { repo.getTags() } val tags = runBlocking { repo.getTags() }
Assert.assertFalse("No tags found", tags.isEmpty()) Assert.assertFalse("No tags found", tags.isEmpty())
val tag = tags.random() val tag = tags.random()
Assert.assertFalse("Tag title is blank for ${tag}", tag.key.isBlank()) Assert.assertFalse("Tag title is blank for $tag", tag.key.isBlank())
Assert.assertFalse("Tag title is blank for ${tag}", tag.title.isBlank()) Assert.assertFalse("Tag title is blank for $tag", tag.title.isBlank())
val list = runBlocking { repo.getList(0, tag = tag) } val list = runBlocking { repo.getList(0, tag = tag) }
Assert.assertFalse("List is empty", list.isEmpty()) Assert.assertFalse("List is empty", list.isEmpty())
val item = list.random() val item = list.random()
@@ -128,34 +125,6 @@ class RemoteRepositoryTest(source: MangaSource) : KoinTest {
companion object { companion object {
@JvmStatic
@BeforeClass
fun initialize() {
startKoin {
modules(
module {
single<CookieJar> { TemporaryCookieJar() }
factory {
OkHttpClient.Builder()
.cookieJar(get())
.addInterceptor(UserAgentInterceptor())
.connectTimeout(20, TimeUnit.SECONDS)
.readTimeout(60, TimeUnit.SECONDS)
.writeTimeout(20, TimeUnit.SECONDS)
.build()
}
single<MangaLoaderContext> {
object : MangaLoaderContext(get(), get()) {
override fun getSettings(source: MangaSource): SourceSettings {
return SourceSettingsMock()
}
}
}
}
)
}
}
@JvmStatic @JvmStatic
@Parameterized.Parameters(name = "{0}") @Parameterized.Parameters(name = "{0}")
fun getProviders() = (MangaSource.values().toList() - MangaSource.LOCAL).toTypedArray() fun getProviders() = (MangaSource.values().toList() - MangaSource.LOCAL).toTypedArray()

View File

@@ -0,0 +1,40 @@
package org.koitharu.kotatsu.parsers
import okhttp3.CookieJar
import okhttp3.OkHttpClient
import org.koin.dsl.module
import org.koitharu.kotatsu.base.domain.MangaLoaderContext
import org.koitharu.kotatsu.core.model.MangaSource
import org.koitharu.kotatsu.core.network.UserAgentInterceptor
import org.koitharu.kotatsu.core.parser.RemoteMangaRepository
import org.koitharu.kotatsu.core.prefs.SourceSettings
import java.util.concurrent.TimeUnit
val repositoryTestModule
get() = module {
single<CookieJar> { TemporaryCookieJar() }
factory {
OkHttpClient.Builder()
.cookieJar(get())
.addInterceptor(UserAgentInterceptor())
.connectTimeout(20, TimeUnit.SECONDS)
.readTimeout(60, TimeUnit.SECONDS)
.writeTimeout(20, TimeUnit.SECONDS)
.build()
}
single<MangaLoaderContext> {
object : MangaLoaderContext(get(), get()) {
override fun getSettings(source: MangaSource): SourceSettings {
return SourceSettingsMock()
}
}
}
factory { (source: MangaSource) ->
runCatching {
source.cls.getDeclaredConstructor(MangaLoaderContext::class.java)
.newInstance(get<MangaLoaderContext>())
}.recoverCatching {
source.cls.newInstance()
}.getOrThrow() as RemoteMangaRepository
}
}