Skip to content

Commit

Permalink
add a test
Browse files Browse the repository at this point in the history
  • Loading branch information
yschimke committed Jan 4, 2025
1 parent 301e212 commit e6f2a7d
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 9 deletions.
12 changes: 6 additions & 6 deletions android-test-app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,25 @@ plugins {
}

android {
compileSdk = 34
compileSdk = 35

namespace = "okhttp.android.testapp"

testBuildType = "release"
// testBuildType = "release"

defaultConfig {
minSdk = 21
targetSdk = 34
targetSdk = 35
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}

compileOptions {
targetCompatibility(JavaVersion.VERSION_11)
sourceCompatibility(JavaVersion.VERSION_11)
targetCompatibility(JavaVersion.VERSION_17)
sourceCompatibility(JavaVersion.VERSION_17)
}

kotlinOptions {
jvmTarget = JavaVersion.VERSION_11.toString()
jvmTarget = JavaVersion.VERSION_17.toString()
}

buildTypes {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright (C) 2023 Block, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package okhttp.android.testapp

import androidx.test.core.app.ApplicationProvider
import assertk.assertThat
import assertk.assertions.isEqualTo
import okhttp3.android.OkHttpClientContext.okHttpClient
import org.junit.Test

/**
* Run with "./gradlew :android-test-app:connectedCheck -PandroidBuild=true" and make sure ANDROID_SDK_ROOT is set.
*/
class OkHttpClientFactoryTest {
@Test
fun testUsesCorrectFactory() {
val application = ApplicationProvider.getApplicationContext<TestApplication>()

val client = application.okHttpClient
assertThat(client.cache?.maxSize()).isEqualTo(5_000_000)
assertThat(client.cache?.directory?.name).isEqualTo("test-app-cache")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package okhttp3.android
package okhttp.android.testapp

import assertk.assertThat
import assertk.assertions.isEqualTo
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,17 @@
package okhttp.android.testapp

import android.app.Application
import okhttp3.OkHttpClient
import okhttp3.android.AndroidOkHttpClientBuilder.androidBuilder
import okhttp3.android.OkHttpClientFactory

class TestApplication : Application()
class TestApplication : Application(), OkHttpClientFactory {
override fun newOkHttpClient(): OkHttpClient {
return OkHttpClient.androidBuilder(
context = this,
cacheDir = { cacheDir.resolve("test-app-cache") },
cacheSize = 5_000_000,
)
.build()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright (c) 2024 Block, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package okhttp3.android

import android.content.Context
import android.os.StrictMode
import java.io.File
import okhttp3.Cache
import okhttp3.OkHttpClient

/**
* [OkHttpClient.Builder] with opinionated defaults on Android.
*/
object AndroidOkHttpClientBuilder {
/**
* Create a [OkHttpClient.Builder] with opinionated defaults on Android.
*
* @param context The context for accessing resources such as file locations or android services.
* @param cacheDir lambda for providing a cache dir. Defaults to "okhttp" in [Context.getCacheDir].
* @param cacheSize the cache size. Defaults to 10MB.
*/
fun OkHttpClient.Companion.androidBuilder(
context: Context,
cacheDir: (() -> File)? = { context.cacheDir.resolve("okhttp") },
cacheSize: Long = 10_000_000L,
): OkHttpClient.Builder {
return OkHttpClient.Builder().apply {
if (cacheDir != null) {
StrictMode.allowThreadDiskWrites().resetAfter {
cache(Cache(cacheDir(), cacheSize))
}
}
}
}

private fun <R> StrictMode.ThreadPolicy.resetAfter(block: () -> R) =
try {
block()
} finally {
StrictMode.setThreadPolicy(this)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package okhttp3.android

import android.content.Context
import okhttp3.OkHttpClient
import okhttp3.android.AndroidOkHttpClientBuilder.androidBuilder
import okhttp3.internal.platform.ContextAwarePlatform
import okhttp3.internal.platform.Platform

Expand All @@ -43,6 +44,10 @@ object OkHttpClientContext {
get() = _okHttpClient

private fun newOkHttpClient(context: Context): OkHttpClient {
return (context.applicationContext as? OkHttpClientFactory)?.newOkHttpClient() ?: OkHttpClient()
val okHttpClientFactory = context.applicationContext as? OkHttpClientFactory
return okHttpClientFactory?.newOkHttpClient() ?: OkHttpClient.androidBuilder(
context = context,
cacheDir = null,
).build()
}
}

0 comments on commit e6f2a7d

Please sign in to comment.