-
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Bundle#putSerializable and Bundle#getSerializable extensions
- Loading branch information
Showing
5 changed files
with
113 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
state-keeper/src/androidUnitTest/kotlin/com/arkivanov/essenty/statekeeper/BundleExtTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.arkivanov.essenty.statekeeper | ||
|
||
import android.os.Bundle | ||
import kotlinx.serialization.Serializable | ||
import org.junit.runner.RunWith | ||
import org.robolectric.RobolectricTestRunner | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
|
||
@RunWith(RobolectricTestRunner::class) | ||
class BundleExtTest { | ||
|
||
@Test | ||
fun getSerializable_returns_same_value_after_putSerializable_without_serialization() { | ||
val value = Value(value = "123") | ||
val bundle = Bundle() | ||
bundle.putSerializable(key = "key", value = value, strategy = Value.serializer()) | ||
val newValue = bundle.getSerializable(key = "key", strategy = Value.serializer()) | ||
|
||
assertEquals(value, newValue) | ||
} | ||
|
||
@Test | ||
fun getSerializable_returns_same_value_after_putSerializable_with_serialization() { | ||
val value = Value(value = "123") | ||
val bundle = Bundle() | ||
bundle.putSerializable(key = "key", value = value, strategy = Value.serializer()) | ||
val newValue = bundle.parcelize().deparcelize().getSerializable(key = "key", strategy = Value.serializer()) | ||
|
||
assertEquals(value, newValue) | ||
} | ||
|
||
@Test | ||
fun getSerializable_returns_same_value_after_putSerializable_with_double_serialization() { | ||
val value = Value(value = "123") | ||
val bundle = Bundle() | ||
bundle.putSerializable(key = "key", value = value, strategy = Value.serializer()) | ||
bundle.putInt("int", 123) | ||
val newBundle = bundle.parcelize().deparcelize() | ||
newBundle.getInt("int") // Force partial deserialization of the Bundle | ||
val newValue = newBundle.parcelize().deparcelize().getSerializable(key = "key", strategy = Value.serializer()) | ||
|
||
assertEquals(value, newValue) | ||
} | ||
|
||
@Serializable | ||
data class Value(val value: String) | ||
} |
18 changes: 18 additions & 0 deletions
18
...-keeper/src/androidUnitTest/kotlin/com/arkivanov/essenty/statekeeper/TestUtils.android.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.arkivanov.essenty.statekeeper | ||
|
||
import android.os.Bundle | ||
import android.os.Parcel | ||
|
||
internal fun Bundle.parcelize(): ByteArray { | ||
val parcel = Parcel.obtain() | ||
parcel.writeBundle(this) | ||
return parcel.marshall() | ||
} | ||
|
||
internal fun ByteArray.deparcelize(): Bundle { | ||
val parcel = Parcel.obtain() | ||
parcel.unmarshall(this, 0, size) | ||
parcel.setDataPosition(0) | ||
|
||
return requireNotNull(parcel.readBundle()) | ||
} |