Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use DI for HttpClient.Builder #1250

Merged
merged 8 commits into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 7 additions & 12 deletions app/src/androidTest/kotlin/at/bitfire/davdroid/OkhttpClientTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@

package at.bitfire.davdroid

import android.content.Context
import at.bitfire.davdroid.network.HttpClient
import at.bitfire.davdroid.settings.SettingsManager
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.android.testing.HiltAndroidRule
import dagger.hilt.android.testing.HiltAndroidTest
import okhttp3.Request
Expand All @@ -19,15 +16,11 @@ import javax.inject.Inject
@HiltAndroidTest
class OkhttpClientTest {

@get:Rule
val hiltRule = HiltAndroidRule(this)

@Inject
@ApplicationContext
lateinit var context: Context
lateinit var httpClientBuilder: HttpClient.Builder

@Inject
lateinit var settingsManager: SettingsManager
@get:Rule
val hiltRule = HiltAndroidRule(this)

@Before
fun inject() {
Expand All @@ -37,12 +30,14 @@ class OkhttpClientTest {

@Test
fun testIcloudWithSettings() {
val client = HttpClient.Builder(context).build()
client.okHttpClient.newCall(Request.Builder()
httpClientBuilder.build().use { client ->
client.okHttpClient
.newCall(Request.Builder()
.get()
.url("https://icloud.com")
.build())
.execute()
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,11 @@

package at.bitfire.davdroid.db

import android.content.Context
import android.security.NetworkSecurityPolicy
import androidx.test.filters.SmallTest
import at.bitfire.dav4jvm.DavResource
import at.bitfire.dav4jvm.property.webdav.ResourceType
import at.bitfire.davdroid.network.HttpClient
import at.bitfire.davdroid.settings.SettingsManager
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.android.testing.HiltAndroidRule
import dagger.hilt.android.testing.HiltAndroidTest
import okhttp3.HttpUrl.Companion.toHttpUrl
Expand All @@ -31,15 +28,11 @@ import javax.inject.Inject
@HiltAndroidTest
class CollectionTest {

@get:Rule
val hiltRule = HiltAndroidRule(this)

@Inject
@ApplicationContext
lateinit var context: Context
lateinit var httpClientBuilder: HttpClient.Builder

@Inject
lateinit var settingsManager: SettingsManager
@get:Rule
val hiltRule = HiltAndroidRule(this)

private lateinit var httpClient: HttpClient
private val server = MockWebServer()
Expand All @@ -48,7 +41,7 @@ class CollectionTest {
fun setup() {
hiltRule.inject()

httpClient = HttpClient.Builder(context).build()
httpClient = httpClientBuilder.build()
Assume.assumeTrue(NetworkSecurityPolicy.getInstance().isCleartextTrafficPermitted)
}

Expand Down Expand Up @@ -211,4 +204,4 @@ class CollectionTest {
assertEquals("https://example.com/1.ics".toHttpUrl(), info.source)
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@

package at.bitfire.davdroid.network

import android.content.Context
import android.security.NetworkSecurityPolicy
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.android.testing.HiltAndroidRule
import dagger.hilt.android.testing.HiltAndroidTest
import okhttp3.Request
Expand All @@ -25,21 +23,20 @@ import javax.inject.Inject
@HiltAndroidTest
class HttpClientTest {

lateinit var server: MockWebServer
lateinit var httpClient: HttpClient

@get:Rule
var hiltRule = HiltAndroidRule(this)

@Inject
@ApplicationContext
lateinit var context: Context
lateinit var httpClientBuilder: HttpClient.Builder

lateinit var httpClient: HttpClient
lateinit var server: MockWebServer

@Before
fun setUp() {
hiltRule.inject()

httpClient = HttpClient.Builder(context).build()
httpClient = httpClientBuilder.build()

server = MockWebServer()
server.start(30000)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,14 @@

package at.bitfire.davdroid.repository

import android.content.Context
import at.bitfire.davdroid.db.AppDatabase
import at.bitfire.davdroid.db.Collection
import at.bitfire.davdroid.db.Service
import at.bitfire.davdroid.settings.AccountSettings
import dagger.Lazy
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.android.testing.BindValueIntoSet
import dagger.hilt.android.testing.HiltAndroidRule
import dagger.hilt.android.testing.HiltAndroidTest
import io.mockk.mockk
import io.mockk.impl.annotations.MockK
import io.mockk.junit4.MockKRule
import io.mockk.verify
import kotlinx.coroutines.runBlocking
import okhttp3.HttpUrl.Companion.toHttpUrl
Expand All @@ -26,28 +24,36 @@ import javax.inject.Inject
@HiltAndroidTest
class DavCollectionRepositoryTest {

@get:Rule
var hiltRule = HiltAndroidRule(this)

@Inject
lateinit var accountSettingsFactory: AccountSettings.Factory

@Inject
@ApplicationContext
lateinit var context: Context
lateinit var collectionRepository: DavCollectionRepository

@Inject
lateinit var db: AppDatabase

@BindValueIntoSet
@MockK(relaxed = true)
lateinit var testObserver: DavCollectionRepository.OnChangeListener

@Inject
lateinit var serviceRepository: DavServiceRepository

@get:Rule
val hiltRule = HiltAndroidRule(this)

@get:Rule
val mockkKRule = MockKRule(this)

var service: Service? = null

@Before
fun setUp() {
hiltRule.inject()
service = createTestService(Service.TYPE_CARDDAV)!!

// insert test service
val serviceId = serviceRepository.insertOrReplace(
Service(id=0, accountName="test", type=Service.TYPE_CARDDAV, principal = null)
)
service = serviceRepository.get(serviceId)!!
}

@After
Expand All @@ -67,18 +73,6 @@ class DavCollectionRepositoryTest {
forceReadOnly = false,
)
)
val testObserver = mockk<DavCollectionRepository.OnChangeListener>(relaxed = true)
val collectionRepository = DavCollectionRepository(
accountSettingsFactory,
context,
db,
object : Lazy<Set<DavCollectionRepository.OnChangeListener>> {
override fun get(): Set<DavCollectionRepository.OnChangeListener> {
return mutableSetOf(testObserver)
}
},
serviceRepository
)

assert(db.collectionDao().get(collectionId)?.forceReadOnly == false)
verify(exactly = 0) {
Expand All @@ -91,13 +85,4 @@ class DavCollectionRepositoryTest {
}
}


// Test helpers and dependencies

private fun createTestService(serviceType: String) : Service? {
val service = Service(id=0, accountName="test", type=serviceType, principal = null)
val serviceId = serviceRepository.insertOrReplace(service)
return serviceRepository.get(serviceId)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,30 @@ import javax.inject.Inject
@HiltAndroidTest
class DavHomeSetRepositoryTest {

@get:Rule
var hiltRule = HiltAndroidRule(this)

@Inject
lateinit var repository: DavHomeSetRepository

@Inject
lateinit var serviceRepository: DavServiceRepository

@get:Rule
var hiltRule = HiltAndroidRule(this)

var serviceId: Long = 0

@Before
fun setUp() {
hiltRule.inject()

serviceId = serviceRepository.insertOrReplace(
Service(id=0, accountName="test", type= Service.TYPE_CALDAV, principal = null)
)
}


@Test
fun testInsertOrUpdate() {
// should insert new row or update (upsert) existing row - without changing its key!
val serviceId = createTestService()

val entry1 = HomeSet(id=0, serviceId=serviceId, personal=true, url="https://example.com/1".toHttpUrl())
val insertId1 = repository.insertOrUpdateByUrl(entry1)
assertEquals(1L, insertId1)
Expand All @@ -57,8 +61,6 @@ class DavHomeSetRepositoryTest {
@Test
fun testDelete() {
// should delete row with given primary key (id)
val serviceId = createTestService()

val entry1 = HomeSet(id=1, serviceId=serviceId, personal=true, url= "https://example.com/1".toHttpUrl())

val insertId1 = repository.insertOrUpdateByUrl(entry1)
Expand All @@ -69,10 +71,4 @@ class DavHomeSetRepositoryTest {
assertEquals(null, repository.getById(1L))
}


private fun createTestService() : Long {
val service = Service(id=0, accountName="test", type= Service.TYPE_CALDAV, principal = null)
return serviceRepository.insertOrReplace(service)
}

}
Loading
Loading