Skip to content
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

Fix propagation of LocalLocalization #1202

Merged
merged 1 commit into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()),
igordmn marked this conversation as resolved.
Show resolved Hide resolved
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)
}
}
Loading