1
1
package com.squareup.sample.container.overviewdetail
2
2
3
3
import android.content.Context
4
+ import android.os.Parcel
5
+ import android.os.Parcelable
6
+ import android.text.Editable
7
+ import android.util.SparseArray
4
8
import android.view.View
9
+ import android.widget.EditText
5
10
import androidx.activity.ComponentActivity
6
11
import androidx.test.ext.junit.rules.ActivityScenarioRule
7
12
import com.google.common.truth.Truth.assertThat
@@ -28,10 +33,58 @@ internal class BackStackContainerTest {
28
33
override val compatibilityKey = name
29
34
override val viewFactory: ViewFactory <Rendering >
30
35
get() = BuilderViewFactory (Rendering ::class ) { r, e, ctx, _ ->
31
- View (ctx).also { it.bindShowRendering(r, e) { _, _ -> /* Noop */ } }
36
+ EditText (ctx).apply {
37
+ // Must have an id to participate in view persistence.
38
+ id = 65
39
+ bindShowRendering(r, e) { _, _ -> /* Noop */ } }
32
40
}
33
41
}
34
42
43
+ @Test fun savedStateParcelingWorks () {
44
+ scenario.onActivity { activity ->
45
+ val originalView = VisibleBackStackContainer (activity).apply {
46
+ // Must have an id to participate in view persistence.
47
+ id = 42
48
+ }
49
+
50
+ // Show "able".
51
+ originalView.show(BackStackScreen (Rendering (" able" )))
52
+ // Type "first" into the rendered EditText.
53
+ (originalView.getChildAt(0 ) as EditText ).text = " first" .toEditable()
54
+ // Push "baker" on top of "able".
55
+ originalView.show(BackStackScreen (Rendering (" able" ), Rendering (" baker" )))
56
+ // Type "second" into the replacement rendered EditText.
57
+ (originalView.getChildAt(0 ) as EditText ).text = " second" .toEditable()
58
+
59
+ // Save the view state to a ByteArray and read it out again, exercising all of
60
+ // the Parcel machinery.
61
+ val savedArray = SparseArray <Parcelable >()
62
+ originalView.saveHierarchyState(savedArray)
63
+ val bytes = Parcel .obtain().let { parcel ->
64
+ parcel.writeSparseArray(savedArray)
65
+ parcel.marshall().also { parcel.recycle() }
66
+ }
67
+ val restoredArray = Parcel .obtain().let { parcel ->
68
+ parcel.unmarshall(bytes, 0 , bytes.size)
69
+ parcel.setDataPosition(0 )
70
+ parcel.readSparseArray<Parcelable >(this ::class .java.classLoader)!! .also { parcel.recycle() }
71
+ }
72
+
73
+ // Create a new BackStackContainer with the same id as the original
74
+ val restoredView = VisibleBackStackContainer (activity).apply { id = 42 }
75
+ // Have it render the same able > baker back stack that we last showed in the original.
76
+ restoredView.show(BackStackScreen (Rendering (" able" ), Rendering (" baker" )))
77
+ // Restore the view hierarchy.
78
+ restoredView.restoreHierarchyState(restoredArray)
79
+ // Android took care of restoring the text that was last shown.
80
+ assertThat((restoredView.getChildAt(0 ) as EditText ).text.toString()).isEqualTo(" second" )
81
+ // Pop back to able.
82
+ restoredView.show(BackStackScreen (Rendering (" able" )))
83
+ // BackStackContainer restored the text we had typed on that.
84
+ assertThat((restoredView.getChildAt(0 ) as EditText ).text.toString()).isEqualTo(" first" )
85
+ }
86
+ }
87
+
35
88
@Test fun firstScreenIsRendered () {
36
89
scenario.onActivity { activity ->
37
90
val c = VisibleBackStackContainer (activity)
@@ -100,3 +153,7 @@ internal class BackStackContainerTest {
100
153
}
101
154
}
102
155
}
156
+
157
+ private fun String.toEditable (): Editable {
158
+ return Editable .Factory .getInstance().newEditable(this )
159
+ }
0 commit comments