mirror of
https://github.com/jlengrand/kotlin.git
synced 2026-05-11 15:53:46 +00:00
On Android, we would need to call `setDataPosition(0)` after unmarshalling a parcel. The reason why the test code is currently working is purely because the Robolectric test framework is more permissive.
45 lines
1.1 KiB
Kotlin
Vendored
45 lines
1.1 KiB
Kotlin
Vendored
// WITH_RUNTIME
|
|
|
|
@file:JvmName("TestKt")
|
|
package test
|
|
|
|
import kotlinx.android.parcel.*
|
|
import android.os.Parcel
|
|
import android.os.Parcelable
|
|
import android.util.SparseBooleanArray
|
|
|
|
object Parceler1 : Parceler<Int> {
|
|
override fun create(parcel: Parcel) = -parcel.readInt()
|
|
|
|
override fun Int.write(parcel: Parcel, flags: Int) {
|
|
parcel.writeInt(this)
|
|
}
|
|
}
|
|
|
|
object Parceler2 : Parceler<Int> {
|
|
override fun create(parcel: Parcel) = parcel.readString().length
|
|
|
|
override fun Int.write(parcel: Parcel, flags: Int) {
|
|
parcel.writeString("Abc")
|
|
}
|
|
}
|
|
|
|
@Parcelize
|
|
@TypeParceler<Int, Parceler1>
|
|
data class Ints(val a: Int, @TypeParceler<Int, Parceler2> val b: Int, val c: @WriteWith<Parceler2> Int) : Parcelable
|
|
|
|
fun box() = parcelTest { parcel ->
|
|
val test = Ints(1, 1, 1)
|
|
test.writeToParcel(parcel, 0)
|
|
|
|
val bytes = parcel.marshall()
|
|
parcel.unmarshall(bytes, 0, bytes.size)
|
|
parcel.setDataPosition(0)
|
|
|
|
val test2 = readFromParcel<Ints>(parcel)
|
|
|
|
assert(test2.a == -test.a)
|
|
assert(test2.b == -test.b)
|
|
assert(test2.c == 3)
|
|
}
|