-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Dispose of the composition when we're done with it. (#2494)
* Dispose of the composition when we're done with it. This adds a test for DisposableEffect callbacks. #2248 * Fixup LeaksTest * Spotless * Track HostApi change * Track HostApi interface change --------- Co-authored-by: Jesse Wilson <[email protected]>
- Loading branch information
1 parent
660d72f
commit 989d7b4
Showing
15 changed files
with
167 additions
and
13 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
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
80 changes: 80 additions & 0 deletions
80
...od-treehouse-host/src/appsJvmTest/kotlin/app/cash/redwood/treehouse/GuestLifecycleTest.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,80 @@ | ||
/* | ||
* Copyright (C) 2024 Square, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package app.cash.redwood.treehouse | ||
|
||
import assertk.assertThat | ||
import assertk.assertions.isEmpty | ||
import assertk.assertions.isEqualTo | ||
import com.example.redwood.testapp.testing.ButtonValue | ||
import com.example.redwood.testapp.testing.TextInputValue | ||
import kotlin.test.Test | ||
import kotlinx.coroutines.test.runTest | ||
|
||
class GuestLifecycleTest { | ||
@Test | ||
fun disposableEffectDisposedWhenRemovedFromComposition() = runTest { | ||
val tester = TreehouseTester(this) | ||
val treehouseApp = tester.loadApp() | ||
val content = tester.content(treehouseApp) | ||
val view = tester.view() | ||
|
||
content.bind(view) | ||
|
||
content.awaitContent(1) | ||
val textInputValue = view.views.single() as TextInputValue | ||
assertThat(textInputValue.text).isEqualTo("what would you like to see?") | ||
textInputValue.onChange!!.invoke("GuestLifecycleTestShowDisposable") | ||
|
||
tester.sendFrame() | ||
assertThat(tester.hostApi.takeMessage()).isEqualTo("DisposableEffect.effect()") | ||
content.awaitContent(2) | ||
val nextButton = view.views.single() as ButtonValue | ||
assertThat(nextButton.text).isEqualTo("Next") | ||
nextButton.onClick!!.invoke() | ||
|
||
tester.sendFrame() | ||
assertThat(tester.hostApi.takeMessage()).isEqualTo("DisposableEffect.dispose()") | ||
content.awaitContent(3) | ||
assertThat(view.views).isEmpty() | ||
|
||
treehouseApp.stop() | ||
treehouseApp.close() | ||
} | ||
|
||
@Test | ||
fun disposableEffectDisposedWhenContentUnbound() = runTest { | ||
val tester = TreehouseTester(this) | ||
val treehouseApp = tester.loadApp() | ||
val content = tester.content(treehouseApp) | ||
val view = tester.view() | ||
|
||
content.bind(view) | ||
|
||
content.awaitContent(1) | ||
val textInputValue = view.views.single() as TextInputValue | ||
assertThat(textInputValue.text).isEqualTo("what would you like to see?") | ||
textInputValue.onChange!!.invoke("GuestLifecycleTestShowDisposable") | ||
|
||
tester.sendFrame() | ||
assertThat(tester.hostApi.takeMessage()).isEqualTo("DisposableEffect.effect()") | ||
|
||
content.unbind() | ||
assertThat(tester.hostApi.takeMessage()).isEqualTo("DisposableEffect.dispose()") | ||
|
||
treehouseApp.stop() | ||
treehouseApp.close() | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,6 +48,9 @@ class IosHostApi : HostApi { | |
} | ||
} | ||
|
||
func log(message: String) { | ||
} | ||
|
||
func close() { | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,6 +42,9 @@ class IosHostApi : HostApi { | |
} | ||
} | ||
|
||
func log(message: String) { | ||
} | ||
|
||
func close() { | ||
} | ||
} |
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