Repository navigation
fix: improve TimeCapsule timeline controls and restoration #164
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
MaxMichel2
wants to merge
1
commit into
refactor/console-log-rows
from
fix/timecapsule-restore-target
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
209 changes: 209 additions & 0 deletions
209
...e/src/androidDeviceTest/kotlin/com/worldline/devview/timecapsule/TimeCapsuleScreenTest.kt
This file contains hidden or 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,209 @@ | ||
| package com.worldline.devview.timecapsule | ||
|
|
||
| import androidx.compose.material3.MaterialTheme | ||
| import androidx.compose.runtime.mutableStateOf | ||
| import androidx.compose.runtime.saveable.rememberSaveableStateHolder | ||
| import androidx.compose.ui.test.StateRestorationTester | ||
| import androidx.compose.ui.test.assertHeightIsAtLeast | ||
| import androidx.compose.ui.test.assertIsDisplayed | ||
| import androidx.compose.ui.test.assertWidthIsAtLeast | ||
| import androidx.compose.ui.test.hasAnyAncestor | ||
| import androidx.compose.ui.test.hasTestTag | ||
| import androidx.compose.ui.test.hasText | ||
| import androidx.compose.ui.test.onNodeWithContentDescription | ||
| import androidx.compose.ui.test.onNodeWithTag | ||
| import androidx.compose.ui.test.onNodeWithText | ||
| import androidx.compose.ui.test.performClick | ||
| import androidx.compose.ui.test.performScrollTo | ||
| import androidx.compose.ui.test.v2.runComposeUiTest | ||
| import androidx.compose.ui.unit.dp | ||
| import io.kotest.matchers.shouldBe | ||
| import kotlinx.coroutines.flow.MutableStateFlow | ||
| import org.junit.Test | ||
|
|
||
| class TimeCapsuleScreenTest { | ||
| @Test | ||
| fun current_entry_has_no_restore_but_earlier_entries_restore_the_owner() = runComposeUiTest { | ||
| val owner = FakeOwner() | ||
| val capsule = capsule(owner = owner) | ||
| TimeCapsule.register(capsule = capsule) | ||
| try { | ||
| setContent { MaterialTheme { TimeCapsuleScreen() } } | ||
| onNode( | ||
| matcher = hasText(text = "Restore") and hasAnyAncestor( | ||
| matcher = hasTestTag(testTag = "time_capsule_row_1") | ||
| ) | ||
| ).assertDoesNotExist() | ||
| onNode( | ||
| matcher = hasText(text = "Restore") and hasAnyAncestor( | ||
| matcher = hasTestTag(testTag = "time_capsule_row_0") | ||
| ) | ||
| ).assertIsDisplayed().performClick() | ||
| runOnIdle { owner.state.value shouldBe 1 } | ||
| } finally { | ||
| TimeCapsule.unregister(capsule = capsule) | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| fun expand_control_has_a_48dp_target_and_toggles_details() = runComposeUiTest { | ||
| val capsule = capsule() | ||
| TimeCapsule.register(capsule = capsule) | ||
| try { | ||
| setContent { MaterialTheme { TimeCapsuleScreen() } } | ||
| onNodeWithTag(testTag = "time_capsule_expand_1") | ||
| .assertHeightIsAtLeast(expectedMinHeight = 48.dp) | ||
| .assertWidthIsAtLeast(expectedMinWidth = 48.dp) | ||
| .performClick() | ||
| onNodeWithContentDescription(label = "Collapse").assertIsDisplayed().performClick() | ||
| onNodeWithContentDescription(label = "Expand").assertIsDisplayed() | ||
| } finally { | ||
| TimeCapsule.unregister(capsule = capsule) | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| fun expanded_entry_survives_saved_state_restoration() = runComposeUiTest { | ||
| val capsule = capsule() | ||
| TimeCapsule.register(capsule = capsule) | ||
| try { | ||
| val restoration = StateRestorationTester(this) | ||
| restoration.setContent { MaterialTheme { TimeCapsuleScreen() } } | ||
| onNodeWithTag(testTag = "time_capsule_expand_1").performClick() | ||
| onNodeWithContentDescription(label = "Collapse").assertIsDisplayed() | ||
| restoration.emulateSaveAndRestore() | ||
| onNodeWithContentDescription(label = "Collapse").assertIsDisplayed() | ||
| onNodeWithText(text = "Counter(count=2)").assertIsDisplayed() | ||
| } finally { | ||
| TimeCapsule.unregister(capsule = capsule) | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| fun a_new_recording_does_not_inherit_expansion_for_reused_entry_ids() = runComposeUiTest { | ||
| val first = capsule() | ||
| val second = capsule() | ||
| TimeCapsule.register(capsule = first) | ||
| try { | ||
| setContent { MaterialTheme { TimeCapsuleScreen() } } | ||
| onNodeWithTag(testTag = "time_capsule_expand_1").performClick() | ||
| onNodeWithContentDescription(label = "Collapse").assertIsDisplayed() | ||
| runOnIdle { | ||
| TimeCapsule.register(capsule = second) | ||
| TimeCapsule.unregister(capsule = first) | ||
| } | ||
| onNodeWithContentDescription(label = "Expand").assertIsDisplayed() | ||
| onNodeWithText(text = "Counter(count=2)").assertDoesNotExist() | ||
| } finally { | ||
| TimeCapsule.unregister(capsule = first) | ||
| TimeCapsule.unregister(capsule = second) | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| fun returning_after_a_recording_changed_does_not_restore_unrelated_expansion() = runComposeUiTest { | ||
| val first = capsule() | ||
| val second = capsule() | ||
| val showScreen = mutableStateOf(value = true) | ||
| TimeCapsule.register(capsule = first) | ||
| try { | ||
| setContent { | ||
| val holder = rememberSaveableStateHolder() | ||
| if (showScreen.value) { | ||
| holder.SaveableStateProvider(key = "timeline") { | ||
| MaterialTheme { TimeCapsuleScreen() } | ||
| } | ||
| } | ||
| } | ||
| onNodeWithTag(testTag = "time_capsule_expand_1").performClick() | ||
| onNodeWithContentDescription(label = "Collapse").assertIsDisplayed() | ||
| runOnIdle { showScreen.value = false } | ||
| runOnIdle { | ||
| TimeCapsule.unregister(capsule = first) | ||
| TimeCapsule.register(capsule = second) | ||
| showScreen.value = true | ||
| } | ||
| onNodeWithContentDescription(label = "Expand").assertIsDisplayed() | ||
| onNodeWithText(text = "Counter(count=2)").assertDoesNotExist() | ||
| } finally { | ||
| TimeCapsule.unregister(capsule = first) | ||
| TimeCapsule.unregister(capsule = second) | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| fun restored_long_free_form_label_can_still_be_collapsed() = runComposeUiTest { | ||
| val capsule = ScreenCapsule( | ||
| owner = FakeOwner(), | ||
| label = { "A long free-form state description ".repeat(n = 30) }, | ||
| maxEntries = 10 | ||
| ).apply { record(state = 1) } | ||
| TimeCapsule.register(capsule = capsule) | ||
| try { | ||
| val restoration = StateRestorationTester(this) | ||
| restoration.setContent { MaterialTheme { TimeCapsuleScreen() } } | ||
| onNodeWithTag(testTag = "time_capsule_expand_0").performClick() | ||
| restoration.emulateSaveAndRestore() | ||
| onNodeWithContentDescription(label = "Collapse").performScrollTo().performClick() | ||
| onNodeWithContentDescription(label = "Expand").assertIsDisplayed() | ||
| } finally { | ||
| TimeCapsule.unregister(capsule = capsule) | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| fun ordinary_recomposition_keeps_expansion() = runComposeUiTest { | ||
| val capsule = capsule() | ||
| val bottomPadding = mutableStateOf(value = 0.dp) | ||
| TimeCapsule.register(capsule = capsule) | ||
| try { | ||
| setContent { MaterialTheme { TimeCapsuleScreen(bottomPadding = bottomPadding.value) } } | ||
| onNodeWithTag(testTag = "time_capsule_expand_1").performClick() | ||
| runOnIdle { bottomPadding.value = 16.dp } | ||
| onNodeWithContentDescription(label = "Collapse").assertIsDisplayed() | ||
| } finally { | ||
| TimeCapsule.unregister(capsule = capsule) | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| fun empty_recording_explains_when_states_will_appear() = runComposeUiTest { | ||
| val capsule = ScreenCapsule(owner = FakeOwner(), label = { it.toString() }, maxEntries = 10) | ||
| TimeCapsule.register(capsule = capsule) | ||
| try { | ||
| setContent { MaterialTheme { TimeCapsuleScreen() } } | ||
| onNodeWithText(text = "No states captured yet").assertIsDisplayed() | ||
| onNodeWithText( | ||
| text = "State changes will appear here while this screen is recording." | ||
| ).assertIsDisplayed() | ||
| } finally { | ||
| TimeCapsule.unregister(capsule = capsule) | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| fun no_recording_explains_how_to_start() = runComposeUiTest { | ||
| setContent { MaterialTheme { TimeCapsuleScreen() } } | ||
| onNodeWithText(text = "No screen is recording").assertIsDisplayed() | ||
| onNodeWithText( | ||
| text = "Add TimeCapsuleEffect(owner) to a screen to record its state history." | ||
| ).assertIsDisplayed() | ||
| } | ||
|
|
||
| private fun capsule(owner: FakeOwner = FakeOwner()): ScreenCapsule<Int> = ScreenCapsule( | ||
| owner = owner, | ||
| label = { "Counter(count=$it)" }, | ||
| maxEntries = 10 | ||
| ).apply { | ||
| record(state = 1) | ||
| record(state = 2) | ||
| } | ||
|
|
||
| private class FakeOwner : TimeCapsuleOwner<Int> { | ||
| override val state = MutableStateFlow(value = 2) | ||
|
|
||
| override fun restoreState(state: Int) { | ||
| this.state.value = state | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
expandedIdsnow goes into saved state, but it keeps IDs after their entries leave the timeline. A long-running recording can therefore save an ever-growing list even thoughmaxEntriesbounds the history. This wastes memory and increases the work needed to save the screen. Filter expansion against retained entry IDs when history changes, including after Clear.