1
- package com.squareup.sample.container.overviewdetail
1
+ package com.squareup.workflow1.ui.backstack.test
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,59 @@ 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 */ }
40
+ }
32
41
}
33
42
}
34
43
44
+ @Test fun savedStateParcelingWorks () {
45
+ scenario.onActivity { activity ->
46
+ val originalView = VisibleBackStackContainer (activity).apply {
47
+ // Must have an id to participate in view persistence.
48
+ id = 42
49
+ }
50
+
51
+ // Show "able".
52
+ originalView.show(BackStackScreen (Rendering (" able" )))
53
+ // Type "first" into the rendered EditText.
54
+ (originalView.getChildAt(0 ) as EditText ).text = " first" .toEditable()
55
+ // Push "baker" on top of "able".
56
+ originalView.show(BackStackScreen (Rendering (" able" ), Rendering (" baker" )))
57
+ // Type "second" into the replacement rendered EditText.
58
+ (originalView.getChildAt(0 ) as EditText ).text = " second" .toEditable()
59
+
60
+ // Save the view state to a ByteArray and read it out again, exercising all of
61
+ // the Parcel machinery.
62
+ val savedArray = SparseArray <Parcelable >()
63
+ originalView.saveHierarchyState(savedArray)
64
+ val bytes = Parcel .obtain().let { parcel ->
65
+ parcel.writeSparseArray(savedArray)
66
+ parcel.marshall().also { parcel.recycle() }
67
+ }
68
+ val restoredArray = Parcel .obtain().let { parcel ->
69
+ parcel.unmarshall(bytes, 0 , bytes.size)
70
+ parcel.setDataPosition(0 )
71
+ parcel.readSparseArray<Parcelable >(this ::class .java.classLoader)!! .also { parcel.recycle() }
72
+ }
73
+
74
+ // Create a new BackStackContainer with the same id as the original
75
+ val restoredView = VisibleBackStackContainer (activity).apply { id = 42 }
76
+ // Have it render the same able > baker back stack that we last showed in the original.
77
+ restoredView.show(BackStackScreen (Rendering (" able" ), Rendering (" baker" )))
78
+ // Restore the view hierarchy.
79
+ restoredView.restoreHierarchyState(restoredArray)
80
+ // Android took care of restoring the text that was last shown.
81
+ assertThat((restoredView.getChildAt(0 ) as EditText ).text.toString()).isEqualTo(" second" )
82
+ // Pop back to able.
83
+ restoredView.show(BackStackScreen (Rendering (" able" )))
84
+ // BackStackContainer restored the text we had typed on that.
85
+ assertThat((restoredView.getChildAt(0 ) as EditText ).text.toString()).isEqualTo(" first" )
86
+ }
87
+ }
88
+
35
89
@Test fun firstScreenIsRendered () {
36
90
scenario.onActivity { activity ->
37
91
val c = VisibleBackStackContainer (activity)
@@ -100,3 +154,7 @@ internal class BackStackContainerTest {
100
154
}
101
155
}
102
156
}
157
+
158
+ private fun String.toEditable (): Editable {
159
+ return Editable .Factory .getInstance().newEditable(this )
160
+ }
0 commit comments