Skip to content

Commit

Permalink
Fix propagation of LocalLocalization from application scope into wind…
Browse files Browse the repository at this point in the history
…ow and from window into child scenes (e.g. popup).
  • Loading branch information
m-sasha committed Mar 19, 2024
1 parent 0e58ebf commit 81acb71
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ internal actual fun ProvidePlatformCompositionLocals(
content: @Composable () -> Unit
) {
CompositionLocalProvider(
LocalLocalization provides defaultPlatformLocalization(),
// See https://issuetracker.google.com/issues/330036209 for why `arrayOf` is used.
values = arrayOf(LocalLocalization providesDefault defaultPlatformLocalization()),
content = content
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,11 @@ import androidx.compose.ui.test.ComposeUiTest
import androidx.compose.ui.text.AnnotatedString
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.ApplicationScope
import androidx.compose.ui.window.Popup
import androidx.compose.ui.window.Window
import androidx.compose.ui.window.runApplicationTest
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNotNull

@OptIn(ExperimentalTestApi::class)
Expand Down Expand Up @@ -86,10 +89,10 @@ class PlatformLocalizationTest {
val textManager = object: TextContextMenu.TextManager {
override val selectedText: AnnotatedString
get() = AnnotatedString("")
override val cut = { println("Cutting") }
override val copy = { println("Copying") }
override val paste = { println("Pasting ")}
override val selectAll = { println("Selecting all") }
override val cut = { }
override val copy = { }
override val paste = { }
override val selectAll = { }
}

setContent {
Expand All @@ -113,4 +116,63 @@ class PlatformLocalizationTest {
onNodeWithText("Paste").assertDoesNotExist()
onNodeWithText("Select All").assertDoesNotExist()
}

/**
* Verify that providing a [PlatformLocalization] in [LocalLocalization] in the application
* scope correctly overrides it in the window scope.
*/
@Test
fun canOverridePlatformLocalizationFromAppToWindowScope() = runApplicationTest {
val customLocalization = object : PlatformLocalization {
override val copy: String
get() = "My Copy"
override val cut: String
get() = "My Cut"
override val paste: String
get() = "My Paste"
override val selectAll: String
get() = "My Select All"
}

lateinit var actualLocalization: PlatformLocalization
launchTestApplication {
CompositionLocalProvider(LocalLocalization provides customLocalization) {
Window(onCloseRequest = { }) {
actualLocalization = LocalLocalization.current
}
}
}
awaitIdle()

assertEquals(customLocalization, actualLocalization)
}

/**
* Verify that providing a [PlatformLocalization] in [LocalLocalization] in the window scope
* correctly overrides it in a [Popup].
*/
@Test
fun canOverridePlatformLocalizationFromWindowToPopupScope() = runComposeUiTest {
val customLocalization = object : PlatformLocalization {
override val copy: String
get() = "My Copy"
override val cut: String
get() = "My Cut"
override val paste: String
get() = "My Paste"
override val selectAll: String
get() = "My Select All"
}

lateinit var actualLocalization: PlatformLocalization
setContent {
CompositionLocalProvider(LocalLocalization provides customLocalization) {
Popup {
actualLocalization = LocalLocalization.current
}
}
}

assertEquals(customLocalization, actualLocalization)
}
}

0 comments on commit 81acb71

Please sign in to comment.