Parcelize: Fix test code

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.
This commit is contained in:
Steven Schäfer
2020-05-13 16:30:04 +02:00
committed by Yan Zhulanow
parent d93e5d3dc0
commit 9b7a95b05c
40 changed files with 40 additions and 0 deletions
@@ -51,6 +51,7 @@ fun box() = parcelTest { parcel ->
val bytes = parcel.marshall() val bytes = parcel.marshall()
parcel.unmarshall(bytes, 0, bytes.size) parcel.unmarshall(bytes, 0, bytes.size)
parcel.setDataPosition(0)
val first2 = readFromParcel<PrimitiveTypes>(parcel) val first2 = readFromParcel<PrimitiveTypes>(parcel)
val second2 = readFromParcel<PrimitiveTypes>(parcel) val second2 = readFromParcel<PrimitiveTypes>(parcel)
@@ -31,6 +31,7 @@ fun box() = parcelTest { parcel ->
val bytes = parcel.marshall() val bytes = parcel.marshall()
parcel.unmarshall(bytes, 0, bytes.size) parcel.unmarshall(bytes, 0, bytes.size)
parcel.setDataPosition(0)
val first2 = readFromParcel<Test>(parcel) val first2 = readFromParcel<Test>(parcel)
@@ -69,6 +69,7 @@ fun box() = parcelTest { parcel ->
val bytes = parcel.marshall() val bytes = parcel.marshall()
parcel.unmarshall(bytes, 0, bytes.size) parcel.unmarshall(bytes, 0, bytes.size)
parcel.setDataPosition(0)
val first2 = readFromParcel<Test>(parcel) val first2 = readFromParcel<Test>(parcel)
@@ -35,6 +35,7 @@ fun box() = parcelTest { parcel ->
val bytes = parcel.marshall() val bytes = parcel.marshall()
parcel.unmarshall(bytes, 0, bytes.size) parcel.unmarshall(bytes, 0, bytes.size)
parcel.setDataPosition(0)
val test2 = readFromParcel<ServiceContainer>(parcel) val test2 = readFromParcel<ServiceContainer>(parcel)
} }
@@ -34,6 +34,7 @@ fun box() = parcelTest { parcel ->
val bytes = parcel.marshall() val bytes = parcel.marshall()
parcel.unmarshall(bytes, 0, bytes.size) parcel.unmarshall(bytes, 0, bytes.size)
parcel.setDataPosition(0)
val first2 = readFromParcel<BoxedTypes>(parcel) val first2 = readFromParcel<BoxedTypes>(parcel)
@@ -17,6 +17,7 @@ fun box() = parcelTest { parcel ->
val bytes = parcel.marshall() val bytes = parcel.marshall()
parcel.unmarshall(bytes, 0, bytes.size) parcel.unmarshall(bytes, 0, bytes.size)
parcel.setDataPosition(0)
val test2 = readFromParcel<User>(parcel) val test2 = readFromParcel<User>(parcel)
@@ -17,6 +17,7 @@ fun box() = parcelTest { parcel ->
val bytes = parcel.marshall() val bytes = parcel.marshall()
parcel.unmarshall(bytes, 0, bytes.size) parcel.unmarshall(bytes, 0, bytes.size)
parcel.setDataPosition(0)
val test2 = readFromParcel<Test>(parcel) val test2 = readFromParcel<Test>(parcel)
@@ -29,6 +29,7 @@ fun box() = parcelTest { parcel ->
val bytes = parcel.marshall() val bytes = parcel.marshall()
parcel.unmarshall(bytes, 0, bytes.size) parcel.unmarshall(bytes, 0, bytes.size)
parcel.setDataPosition(0)
val userParcelable2 = readFromParcel<UserParcelable>(parcel) val userParcelable2 = readFromParcel<UserParcelable>(parcel)
@@ -34,6 +34,7 @@ fun box() = parcelTest { parcel ->
val bytes = parcel.marshall() val bytes = parcel.marshall()
parcel.unmarshall(bytes, 0, bytes.size) parcel.unmarshall(bytes, 0, bytes.size)
parcel.setDataPosition(0)
val test2 = readFromParcel<Ints>(parcel) val test2 = readFromParcel<Ints>(parcel)
@@ -39,6 +39,7 @@ fun box() = parcelTest { parcel ->
val bytes = parcel.marshall() val bytes = parcel.marshall()
parcel.unmarshall(bytes, 0, bytes.size) parcel.unmarshall(bytes, 0, bytes.size)
parcel.setDataPosition(0)
val test2 = readFromParcel<Test>(parcel) val test2 = readFromParcel<Test>(parcel)
@@ -40,6 +40,7 @@ fun box() = parcelTest { parcel ->
val bytes = parcel.marshall() val bytes = parcel.marshall()
parcel.unmarshall(bytes, 0, bytes.size) parcel.unmarshall(bytes, 0, bytes.size)
parcel.setDataPosition(0)
val test2 = readFromParcel<Test>(parcel) val test2 = readFromParcel<Test>(parcel)
@@ -41,6 +41,7 @@ fun box() = parcelTest { parcel ->
val bytes = parcel.marshall() val bytes = parcel.marshall()
parcel.unmarshall(bytes, 0, bytes.size) parcel.unmarshall(bytes, 0, bytes.size)
parcel.setDataPosition(0)
val test2 = readFromParcel<Test>(parcel) val test2 = readFromParcel<Test>(parcel)
@@ -28,6 +28,7 @@ fun box() = parcelTest { parcel ->
val bytes = parcel.marshall() val bytes = parcel.marshall()
parcel.unmarshall(bytes, 0, bytes.size) parcel.unmarshall(bytes, 0, bytes.size)
parcel.setDataPosition(0)
val user2 = readFromParcel<User>(parcel) val user2 = readFromParcel<User>(parcel)
@@ -25,6 +25,7 @@ fun box() = parcelTest { parcel ->
val bytes = parcel.marshall() val bytes = parcel.marshall()
parcel.unmarshall(bytes, 0, bytes.size) parcel.unmarshall(bytes, 0, bytes.size)
parcel.setDataPosition(0)
val black2 = readFromParcel<Color>(parcel) val black2 = readFromParcel<Color>(parcel)
val obj2 = readFromParcel<Obj>(parcel) val obj2 = readFromParcel<Obj>(parcel)
@@ -20,6 +20,7 @@ fun box() = parcelTest { parcel ->
val bytes = parcel.marshall() val bytes = parcel.marshall()
parcel.unmarshall(bytes, 0, bytes.size) parcel.unmarshall(bytes, 0, bytes.size)
parcel.setDataPosition(0)
val test2 = readFromParcel<Test>(parcel) val test2 = readFromParcel<Test>(parcel)
assert(test == test2) assert(test == test2)
@@ -19,6 +19,7 @@ fun box() = parcelTest { parcel ->
val bytes = parcel.marshall() val bytes = parcel.marshall()
parcel.unmarshall(bytes, 0, bytes.size) parcel.unmarshall(bytes, 0, bytes.size)
parcel.setDataPosition(0)
val test2 = readFromParcel<ExceptionContainer>(parcel) val test2 = readFromParcel<ExceptionContainer>(parcel)
} }
@@ -32,6 +32,7 @@ fun box() = parcelTest { parcel ->
val bytes = parcel.marshall() val bytes = parcel.marshall()
parcel.unmarshall(bytes, 0, bytes.size) parcel.unmarshall(bytes, 0, bytes.size)
parcel.setDataPosition(0)
val film2 = readFromParcel<Film>(parcel) val film2 = readFromParcel<Film>(parcel)
assert(film == film2) assert(film == film2)
@@ -26,6 +26,7 @@ fun box() = parcelTest { parcel ->
val bytes = parcel.marshall() val bytes = parcel.marshall()
parcel.unmarshall(bytes, 0, bytes.size) parcel.unmarshall(bytes, 0, bytes.size)
parcel.setDataPosition(0)
val second = J.readParcel(parcel) val second = J.readParcel(parcel)
assert(first == second) assert(first == second)
@@ -25,6 +25,7 @@ fun box() = parcelTest { parcel ->
val bytes = parcel.marshall() val bytes = parcel.marshall()
parcel.unmarshall(bytes, 0, bytes.size) parcel.unmarshall(bytes, 0, bytes.size)
parcel.setDataPosition(0)
val test2 = readFromParcel<J>(parcel) val test2 = readFromParcel<J>(parcel)
@@ -21,6 +21,7 @@ fun box() = parcelTest { parcel ->
val bytes = parcel.marshall() val bytes = parcel.marshall()
parcel.unmarshall(bytes, 0, bytes.size) parcel.unmarshall(bytes, 0, bytes.size)
parcel.setDataPosition(0)
val test2 = readFromParcel<M>(parcel) val test2 = readFromParcel<M>(parcel)
@@ -32,6 +32,7 @@ fun box() = parcelTest { parcel ->
val bytes = parcel.marshall() val bytes = parcel.marshall()
parcel.unmarshall(bytes, 0, bytes.size) parcel.unmarshall(bytes, 0, bytes.size)
parcel.setDataPosition(0)
val first2 = readFromParcel<Test>(parcel) val first2 = readFromParcel<Test>(parcel)
@@ -39,6 +39,7 @@ fun box() = parcelTest { parcel ->
val bytes = parcel.marshall() val bytes = parcel.marshall()
parcel.unmarshall(bytes, 0, bytes.size) parcel.unmarshall(bytes, 0, bytes.size)
parcel.setDataPosition(0)
val first2 = readFromParcel<Test>(parcel) val first2 = readFromParcel<Test>(parcel)
@@ -22,6 +22,7 @@ fun box() = parcelTest { parcel ->
val bytes = parcel.marshall() val bytes = parcel.marshall()
parcel.unmarshall(bytes, 0, bytes.size) parcel.unmarshall(bytes, 0, bytes.size)
parcel.setDataPosition(0)
readFromParcel<User>(parcel) readFromParcel<User>(parcel)
readFromParcel<User2>(parcel) readFromParcel<User2>(parcel)
@@ -28,6 +28,7 @@ fun box() = parcelTest { parcel ->
val bytes = parcel.marshall() val bytes = parcel.marshall()
parcel.unmarshall(bytes, 0, bytes.size) parcel.unmarshall(bytes, 0, bytes.size)
parcel.setDataPosition(0)
val foo2 = readFromParcel<Foo>(parcel) val foo2 = readFromParcel<Foo>(parcel)
assert(foo2.values.size() == 1) assert(foo2.values.size() == 1)
@@ -17,6 +17,7 @@ fun box() = parcelTest { parcel ->
val bytes = parcel.marshall() val bytes = parcel.marshall()
parcel.unmarshall(bytes, 0, bytes.size) parcel.unmarshall(bytes, 0, bytes.size)
parcel.setDataPosition(0)
readFromParcel<MyObject>(parcel) readFromParcel<MyObject>(parcel)
} }
@@ -43,6 +43,7 @@ fun box() = parcelTest { parcel ->
val bytes = parcel.marshall() val bytes = parcel.marshall()
parcel.unmarshall(bytes, 0, bytes.size) parcel.unmarshall(bytes, 0, bytes.size)
parcel.setDataPosition(0)
val first2 = readFromParcel<Test>(parcel) val first2 = readFromParcel<Test>(parcel)
@@ -17,6 +17,7 @@ fun box() = parcelTest { parcel ->
val bytes = parcel.marshall() val bytes = parcel.marshall()
parcel.unmarshall(bytes, 0, bytes.size) parcel.unmarshall(bytes, 0, bytes.size)
parcel.setDataPosition(0)
val first2 = readFromParcel<Test>(parcel) val first2 = readFromParcel<Test>(parcel)
@@ -31,6 +31,7 @@ fun box() = parcelTest { parcel ->
val bytes = parcel.marshall() val bytes = parcel.marshall()
parcel.unmarshall(bytes, 0, bytes.size) parcel.unmarshall(bytes, 0, bytes.size)
parcel.setDataPosition(0)
val first2 = readFromParcel<Test>(parcel) val first2 = readFromParcel<Test>(parcel)
@@ -35,6 +35,7 @@ fun box() = parcelTest { parcel ->
val bytes = parcel.marshall() val bytes = parcel.marshall()
parcel.unmarshall(bytes, 0, bytes.size) parcel.unmarshall(bytes, 0, bytes.size)
parcel.setDataPosition(0)
val first2 = readFromParcel<Test>(parcel) val first2 = readFromParcel<Test>(parcel)
@@ -17,6 +17,7 @@ fun box() = parcelTest { parcel ->
val bytes = parcel.marshall() val bytes = parcel.marshall()
parcel.unmarshall(bytes, 0, bytes.size) parcel.unmarshall(bytes, 0, bytes.size)
parcel.setDataPosition(0)
val first2 = readFromParcel<Test>(parcel) val first2 = readFromParcel<Test>(parcel)
@@ -33,6 +33,7 @@ fun box() = parcelTest { parcel ->
val bytes = parcel.marshall() val bytes = parcel.marshall()
parcel.unmarshall(bytes, 0, bytes.size) parcel.unmarshall(bytes, 0, bytes.size)
parcel.setDataPosition(0)
val first2 = readFromParcel<Test>(parcel) val first2 = readFromParcel<Test>(parcel)
@@ -27,6 +27,7 @@ fun box() = parcelTest { parcel ->
val bytes = parcel.marshall() val bytes = parcel.marshall()
parcel.unmarshall(bytes, 0, bytes.size) parcel.unmarshall(bytes, 0, bytes.size)
parcel.setDataPosition(0)
val test2 = readFromParcel<Test>(parcel) val test2 = readFromParcel<Test>(parcel)
assert(test == test2) assert(test == test2)
@@ -28,6 +28,7 @@ fun box() = parcelTest { parcel ->
val bytes = parcel.marshall() val bytes = parcel.marshall()
parcel.unmarshall(bytes, 0, bytes.size) parcel.unmarshall(bytes, 0, bytes.size)
parcel.setDataPosition(0)
val first2 = readFromParcel<Test>(parcel) val first2 = readFromParcel<Test>(parcel)
val second2 = readFromParcel<Test>(parcel) val second2 = readFromParcel<Test>(parcel)
@@ -19,6 +19,7 @@ fun box() = parcelTest { parcel ->
val bytes = parcel.marshall() val bytes = parcel.marshall()
parcel.unmarshall(bytes, 0, bytes.size) parcel.unmarshall(bytes, 0, bytes.size)
parcel.setDataPosition(0)
val first2 = readFromParcel<Test>(parcel) val first2 = readFromParcel<Test>(parcel)
val second2 = readFromParcel<Test>(parcel) val second2 = readFromParcel<Test>(parcel)
@@ -24,6 +24,7 @@ fun box() = parcelTest { parcel ->
val bytes = parcel.marshall() val bytes = parcel.marshall()
parcel.unmarshall(bytes, 0, bytes.size) parcel.unmarshall(bytes, 0, bytes.size)
parcel.setDataPosition(0)
val test2 = readFromParcel<Test>(parcel) val test2 = readFromParcel<Test>(parcel)
assert(test == test2) assert(test == test2)
@@ -23,6 +23,7 @@ fun box() = parcelTest { parcel ->
val bytes = parcel.marshall() val bytes = parcel.marshall()
parcel.unmarshall(bytes, 0, bytes.size) parcel.unmarshall(bytes, 0, bytes.size)
parcel.setDataPosition(0)
val test2 = readFromParcel<User>(parcel) val test2 = readFromParcel<User>(parcel)
@@ -29,6 +29,7 @@ fun box() = parcelTest { parcel ->
val bytes = parcel.marshall() val bytes = parcel.marshall()
parcel.unmarshall(bytes, 0, bytes.size) parcel.unmarshall(bytes, 0, bytes.size)
parcel.setDataPosition(0)
val first2 = readFromParcel<PrimitiveTypes>(parcel) val first2 = readFromParcel<PrimitiveTypes>(parcel)
val second2 = readFromParcel<PrimitiveTypes>(parcel) val second2 = readFromParcel<PrimitiveTypes>(parcel)
@@ -18,6 +18,7 @@ fun box() = parcelTest { parcel ->
val bytes = parcel.marshall() val bytes = parcel.marshall()
parcel.unmarshall(bytes, 0, bytes.size) parcel.unmarshall(bytes, 0, bytes.size)
parcel.setDataPosition(0)
val user2 = readFromParcel<User>(parcel) val user2 = readFromParcel<User>(parcel)
assert(user == user2) assert(user == user2)
@@ -25,6 +25,7 @@ fun box() = parcelTest { parcel ->
val bytes = parcel.marshall() val bytes = parcel.marshall()
parcel.unmarshall(bytes, 0, bytes.size) parcel.unmarshall(bytes, 0, bytes.size)
parcel.setDataPosition(0)
val user2 = readFromParcel<User>(parcel) val user2 = readFromParcel<User>(parcel)
@@ -17,6 +17,7 @@ fun box() = parcelTest { parcel ->
val bytes = parcel.marshall() val bytes = parcel.marshall()
parcel.unmarshall(bytes, 0, bytes.size) parcel.unmarshall(bytes, 0, bytes.size)
parcel.setDataPosition(0)
val test2 = readFromParcel<User>(parcel) val test2 = readFromParcel<User>(parcel)