Skip to content

Commit

Permalink
Fix propagation of LocalLocalization (#1202)
Browse files Browse the repository at this point in the history
  • Loading branch information
m-sasha committed Mar 19, 2024
1 parent 725d656 commit cb2c860
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 6 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 @@ -228,7 +228,7 @@ suspend fun awaitApplication(
// This is also provided in `ProvidePlatformCompositionLocals`, but
// for backwards compatibility we need to provide it in the
// application scope too.
LocalLocalization provides defaultPlatformLocalization()
LocalLocalization providesDefault defaultPlatformLocalization()
) {
applicationScope.content()
}
Expand Down
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 cb2c860

Please sign in to comment.