Skip to content

Commit

Permalink
Add test for MemoryCache.Key parcel implementation. (#1429)
Browse files Browse the repository at this point in the history
* Add test for MemoryCache.Key parcel implementation.

* Update public API.

* Get the creator using reflection.

* Tweak.

* Formatting.

* Set explicit size.
  • Loading branch information
colinrtwhite committed Sep 7, 2022
1 parent cb0a5c1 commit 0b1718b
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 25 deletions.
8 changes: 0 additions & 8 deletions coil-base/api/coil-base.api
Original file line number Diff line number Diff line change
Expand Up @@ -426,14 +426,6 @@ public final class coil/memory/MemoryCache$Key : android/os/Parcelable {
public fun writeToParcel (Landroid/os/Parcel;I)V
}

public final class coil/memory/MemoryCache$Key$Creator : android/os/Parcelable$Creator {
public fun <init> ()V
public fun createFromParcel (Landroid/os/Parcel;)Lcoil/memory/MemoryCache$Key;
public synthetic fun createFromParcel (Landroid/os/Parcel;)Ljava/lang/Object;
public fun newArray (I)[Lcoil/memory/MemoryCache$Key;
public synthetic fun newArray (I)[Ljava/lang/Object;
}

public final class coil/memory/MemoryCache$Value {
public fun <init> (Landroid/graphics/Bitmap;Ljava/util/Map;)V
public synthetic fun <init> (Landroid/graphics/Bitmap;Ljava/util/Map;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
Expand Down
31 changes: 14 additions & 17 deletions coil-base/src/main/java/coil/memory/MemoryCache.kt
Original file line number Diff line number Diff line change
Expand Up @@ -89,25 +89,22 @@ interface MemoryCache {
}
}

class Creator : Parcelable.Creator<Key> {

override fun createFromParcel(parcel: Parcel): Key {
val key = parcel.readString()!!
val size = parcel.readInt()
val extras = mutableMapOf<String, String>()
repeat(size) {
val extraKey = parcel.readString()!!
val extraValue = parcel.readString()!!
extras[extraKey] = extraValue
private companion object {
@JvmField val CREATOR = object : Parcelable.Creator<Key> {
override fun createFromParcel(parcel: Parcel): Key {
val key = parcel.readString()!!
val size = parcel.readInt()
val extras = LinkedHashMap<String, String>(size)
repeat(size) {
val extraKey = parcel.readString()!!
val extraValue = parcel.readString()!!
extras[extraKey] = extraValue
}
return Key(key, extras)
}
return Key(key, extras)
}

override fun newArray(size: Int) = arrayOfNulls<Key>(size)
}

internal companion object {
@JvmField val CREATOR: Parcelable.Creator<*> = Creator()
override fun newArray(size: Int) = arrayOfNulls<Key>(size)
}
}
}

Expand Down
20 changes: 20 additions & 0 deletions coil-base/src/test/java/coil/memory/RealMemoryCacheTest.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package coil.memory

import android.os.Parcel
import android.os.Parcelable
import coil.memory.MemoryCache.Key
import coil.memory.MemoryCache.Value
import coil.util.DEFAULT_BITMAP_SIZE
Expand Down Expand Up @@ -107,4 +109,22 @@ class RealMemoryCacheTest {
assertTrue(weakCache.remove(key))
assertFalse(weakCache.remove(key))
}

@Test
fun `memory cache key can be written to and read from parcel`() {
val expected = Key("a", mapOf("b" to "c"))

val parcel = Parcel.obtain()
parcel.writeParcelable(expected, 0)
val expectedPosition = parcel.dataPosition()
parcel.setDataPosition(0)

// writeParcelable writes the class name automatically.
assertEquals(Key::class.java.name, parcel.readString())

val creator = Key::class.java.getField("CREATOR").get(null) as Parcelable.Creator<*>
val actual = creator.createFromParcel(parcel)
assertEquals(expected, actual)
assertEquals(expectedPosition, parcel.dataPosition())
}
}

0 comments on commit 0b1718b

Please sign in to comment.