Skip to content

Commit

Permalink
Add support for ImageLoader.newBuilder(). (#653)
Browse files Browse the repository at this point in the history
* Support ImageLoader.newBuilder and sharing memory caches between image loaders.

* Add tests.

* Fix imports.

* Docs.
  • Loading branch information
colinrtwhite authored Feb 8, 2021
1 parent c417184 commit 60adb4e
Show file tree
Hide file tree
Showing 9 changed files with 206 additions and 97 deletions.
2 changes: 2 additions & 0 deletions coil-base/api/coil-base.api
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public abstract interface class coil/ImageLoader {
public abstract fun getBitmapPool ()Lcoil/bitmap/BitmapPool;
public abstract fun getDefaults ()Lcoil/request/DefaultRequestOptions;
public abstract fun getMemoryCache ()Lcoil/memory/MemoryCache;
public abstract fun newBuilder ()Lcoil/ImageLoader$Builder;
public abstract fun shutdown ()V
}

Expand Down Expand Up @@ -105,6 +106,7 @@ public final class coil/ImageLoader$Builder {
public final fun fallback (Landroid/graphics/drawable/Drawable;)Lcoil/ImageLoader$Builder;
public final fun launchInterceptorChainOnMainThread (Z)Lcoil/ImageLoader$Builder;
public final fun logger (Lcoil/util/Logger;)Lcoil/ImageLoader$Builder;
public final fun memoryCache (Lcoil/memory/MemoryCache;)Lcoil/ImageLoader$Builder;
public final fun memoryCachePolicy (Lcoil/request/CachePolicy;)Lcoil/ImageLoader$Builder;
public final fun networkCachePolicy (Lcoil/request/CachePolicy;)Lcoil/ImageLoader$Builder;
public final fun okHttpClient (Lkotlin/jvm/functions/Function0;)Lcoil/ImageLoader$Builder;
Expand Down
43 changes: 38 additions & 5 deletions coil-base/src/androidTest/java/coil/RealImageLoaderTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import android.graphics.Color
import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.ColorDrawable
import android.widget.ImageView
import androidx.core.graphics.createBitmap
import androidx.core.net.toUri
import androidx.lifecycle.Lifecycle
import androidx.test.core.app.ApplicationProvider
Expand All @@ -24,6 +25,7 @@ import coil.decode.Decoder
import coil.decode.Options
import coil.fetch.AssetUriFetcher.Companion.ASSET_FILE_PATH_ROOT
import coil.memory.MemoryCache
import coil.memory.RealMemoryCache
import coil.memory.RealWeakMemoryCache
import coil.memory.StrongMemoryCache
import coil.request.CachePolicy
Expand All @@ -35,6 +37,7 @@ import coil.request.SuccessResult
import coil.size.PixelSize
import coil.size.Precision
import coil.size.Size
import coil.util.ImageLoaderOptions
import coil.util.TestActivity
import coil.util.Utils
import coil.util.activity
Expand Down Expand Up @@ -65,6 +68,7 @@ import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
import kotlin.test.assertNotNull
import kotlin.test.assertNull
import kotlin.test.assertSame
import kotlin.test.assertTrue

class RealImageLoaderTest {
Expand All @@ -85,18 +89,16 @@ class RealImageLoaderTest {
val weakMemoryCache = RealWeakMemoryCache(null)
val referenceCounter = RealBitmapReferenceCounter(weakMemoryCache, bitmapPool, null)
strongMemoryCache = StrongMemoryCache(weakMemoryCache, referenceCounter, Int.MAX_VALUE, null)
val memoryCache = RealMemoryCache(strongMemoryCache, weakMemoryCache, referenceCounter, bitmapPool)
imageLoader = RealImageLoader(
context = context,
defaults = DefaultRequestOptions(),
bitmapPool = bitmapPool,
referenceCounter = referenceCounter,
strongMemoryCache = strongMemoryCache,
weakMemoryCache = weakMemoryCache,
memoryCache = memoryCache,
callFactory = OkHttpClient(),
eventListenerFactory = EventListener.Factory.NONE,
componentRegistry = ComponentRegistry(),
addLastModifiedToFileCacheKey = true,
launchInterceptorChainOnMainThread = true,
options = ImageLoaderOptions(),
logger = null
)
activityRule.scenario.moveToState(Lifecycle.State.RESUMED)
Expand Down Expand Up @@ -427,6 +429,37 @@ class RealImageLoaderTest {
assertTrue(isSuccessful)
}

@Test
fun newBuilderSharesMemoryCache() {
val key = MemoryCache.Key("fake_key")
val imageLoader1 = ImageLoader(context)
val imageLoader2 = imageLoader1.newBuilder().build()

assertSame(imageLoader1.memoryCache, imageLoader2.memoryCache)
assertNull(imageLoader1.memoryCache[key])
assertNull(imageLoader2.memoryCache[key])

val bitmap = createBitmap(100, 100)
imageLoader1.memoryCache[key] = bitmap

assertSame(bitmap, imageLoader2.memoryCache[key])
}

@Test
fun newBuilderSharesBitmapPool() {
val imageLoader1 = ImageLoader.Builder(context).bitmapPoolPercentage(0.5).build()
val imageLoader2 = imageLoader1.newBuilder().build()

assertSame(imageLoader1.bitmapPool, imageLoader2.bitmapPool)
assertNull(imageLoader1.bitmapPool.getOrNull(100, 100, Bitmap.Config.ARGB_8888))
assertNull(imageLoader2.bitmapPool.getOrNull(100, 100, Bitmap.Config.ARGB_8888))

val bitmap = createBitmap(100, 100)
imageLoader1.bitmapPool.put(bitmap)

assertSame(bitmap, imageLoader2.bitmapPool.getOrNull(100, 100, Bitmap.Config.ARGB_8888))
}

private fun testEnqueue(data: Any, expectedSize: PixelSize = PixelSize(80, 100)) {
val imageView = activityRule.scenario.activity.imageView
imageView.scaleType = ImageView.ScaleType.FIT_CENTER
Expand Down
11 changes: 5 additions & 6 deletions coil-base/src/androidTest/java/coil/util/SystemCallbacksTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import coil.RealImageLoader
import coil.bitmap.BitmapPool
import coil.bitmap.RealBitmapReferenceCounter
import coil.memory.MemoryCache
import coil.memory.RealMemoryCache
import coil.memory.RealWeakMemoryCache
import coil.memory.StrongMemoryCache
import coil.request.DefaultRequestOptions
Expand Down Expand Up @@ -40,7 +41,7 @@ class SystemCallbacksTest {
Runtime.getRuntime().gc()

// Keep allocating bitmaps until either the image loader is freed or we run out of memory.
bitmaps += createBitmap(500, 500, Bitmap.Config.ARGB_8888)
bitmaps += createBitmap(500, 500)
}
bitmaps.clear()

Expand All @@ -56,18 +57,16 @@ class SystemCallbacksTest {
val weakMemoryCache = RealWeakMemoryCache(null)
val referenceCounter = RealBitmapReferenceCounter(weakMemoryCache, bitmapPool, null)
val strongMemoryCache = StrongMemoryCache(weakMemoryCache, referenceCounter, Int.MAX_VALUE, null)
val memoryCache = RealMemoryCache(strongMemoryCache, weakMemoryCache, referenceCounter, bitmapPool)
val imageLoader = RealImageLoader(
context = context,
defaults = DefaultRequestOptions(),
bitmapPool = bitmapPool,
referenceCounter = referenceCounter,
strongMemoryCache = strongMemoryCache,
weakMemoryCache = weakMemoryCache,
memoryCache = memoryCache,
callFactory = OkHttpClient(),
eventListenerFactory = EventListener.Factory.NONE,
componentRegistry = ComponentRegistry(),
addLastModifiedToFileCacheKey = true,
launchInterceptorChainOnMainThread = true,
options = ImageLoaderOptions(),
logger = null
)
val systemCallbacks = SystemCallbacks(imageLoader, context)
Expand Down
Loading

0 comments on commit 60adb4e

Please sign in to comment.