Browse Source

Override non-composable function getResourceEnvironment for tests (#4057)

pull/4061/head
Konstantin 11 months ago committed by GitHub
parent
commit
8423efc466
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      components/gradle.properties
  2. 5
      components/resources/library/src/androidInstrumentedTest/kotlin/org/jetbrains/compose/resources/ComposeResourceTest.android.kt
  3. 2
      components/resources/library/src/androidMain/kotlin/org/jetbrains/compose/resources/ResourceEnvironment.android.kt
  4. 5
      components/resources/library/src/commonMain/kotlin/org/jetbrains/compose/resources/ResourceEnvironment.kt
  5. 10
      components/resources/library/src/commonTest/kotlin/org/jetbrains/compose/resources/TestComposeEnvironment.kt
  6. 2
      components/resources/library/src/desktopMain/kotlin/org/jetbrains/compose/resources/ResourceEnvironment.desktop.kt
  7. 5
      components/resources/library/src/desktopTest/kotlin/org/jetbrains/compose/resources/ComposeResourceTest.desktop.kt
  8. 2
      components/resources/library/src/iosMain/kotlin/org/jetbrains/compose/resources/ResourceEnvironment.ios.kt
  9. 2
      components/resources/library/src/jsMain/kotlin/org/jetbrains/compose/resources/ResourceEnvironment.js.kt
  10. 2
      components/resources/library/src/macosMain/kotlin/org/jetbrains/compose/resources/ResourceEnvironment.macos.kt
  11. 2
      components/resources/library/src/wasmJsMain/kotlin/org/jetbrains/compose/resources/ResourceEnvironment.wasmJs.kt
  12. 2
      gradle-plugins/gradle.properties

2
components/gradle.properties

@ -8,7 +8,7 @@ android.useAndroidX=true
#Versions #Versions
kotlin.version=1.9.21 kotlin.version=1.9.21
compose.version=1.6.0-dev1334 compose.version=1.6.0-dev1340
agp.version=8.1.2 agp.version=8.1.2
#Compose #Compose

5
components/resources/library/src/androidInstrumentedTest/kotlin/org/jetbrains/compose/resources/ComposeResourceTest.android.kt

@ -21,6 +21,11 @@ class ComposeResourceTest {
dropImageCache() dropImageCache()
} }
@Before
fun configureTestEnvironment() {
getResourceEnvironment = ::getTestEnvironment
}
@Test @Test
fun testCountRecompositions() = runComposeUiTest { fun testCountRecompositions() = runComposeUiTest {
runBlockingTest { runBlockingTest {

2
components/resources/library/src/androidMain/kotlin/org/jetbrains/compose/resources/ResourceEnvironment.android.kt

@ -4,7 +4,7 @@ import android.content.res.Configuration
import android.content.res.Resources import android.content.res.Resources
import java.util.* import java.util.*
internal actual fun getResourceEnvironment(): ResourceEnvironment { internal actual fun getSystemEnvironment(): ResourceEnvironment {
val locale = Locale.getDefault() val locale = Locale.getDefault()
val configuration = Resources.getSystem().configuration val configuration = Resources.getSystem().configuration
val isDarkTheme = configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK == Configuration.UI_MODE_NIGHT_YES val isDarkTheme = configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK == Configuration.UI_MODE_NIGHT_YES

5
components/resources/library/src/commonMain/kotlin/org/jetbrains/compose/resources/ResourceEnvironment.kt

@ -39,11 +39,14 @@ internal val DefaultComposeEnvironment = object : ComposeEnvironment {
//ComposeEnvironment provider will be overridden for tests //ComposeEnvironment provider will be overridden for tests
internal val LocalComposeEnvironment = staticCompositionLocalOf { DefaultComposeEnvironment } internal val LocalComposeEnvironment = staticCompositionLocalOf { DefaultComposeEnvironment }
internal expect fun getSystemEnvironment(): ResourceEnvironment
//the function reference will be overridden for tests
/** /**
* Provides the resource environment for non-composable access to string resources. * Provides the resource environment for non-composable access to string resources.
* It is an expensive operation! Don't use it in composable functions with no cache! * It is an expensive operation! Don't use it in composable functions with no cache!
*/ */
internal expect fun getResourceEnvironment(): ResourceEnvironment internal var getResourceEnvironment = ::getSystemEnvironment
internal fun Resource.getPathByEnvironment(environment: ResourceEnvironment): String { internal fun Resource.getPathByEnvironment(environment: ResourceEnvironment): String {
//Priority of environments: https://developer.android.com/guide/topics/resources/providing-resources#table2 //Priority of environments: https://developer.android.com/guide/topics/resources/providing-resources#table2

10
components/resources/library/src/commonTest/kotlin/org/jetbrains/compose/resources/TestComposeEnvironment.kt

@ -2,12 +2,14 @@ package org.jetbrains.compose.resources
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
internal val TestComposeEnvironment = object : ComposeEnvironment { internal fun getTestEnvironment() = ResourceEnvironment(
@Composable
override fun rememberEnvironment() = ResourceEnvironment(
language = LanguageQualifier("en"), language = LanguageQualifier("en"),
region = RegionQualifier("US"), region = RegionQualifier("US"),
theme = ThemeQualifier.LIGHT, theme = ThemeQualifier.LIGHT,
density = DensityQualifier.XHDPI density = DensityQualifier.XHDPI
) )
internal val TestComposeEnvironment = object : ComposeEnvironment {
@Composable
override fun rememberEnvironment() = getTestEnvironment()
} }

2
components/resources/library/src/desktopMain/kotlin/org/jetbrains/compose/resources/ResourceEnvironment.desktop.kt

@ -5,7 +5,7 @@ import org.jetbrains.skiko.currentSystemTheme
import java.awt.Toolkit import java.awt.Toolkit
import java.util.* import java.util.*
internal actual fun getResourceEnvironment(): ResourceEnvironment { internal actual fun getSystemEnvironment(): ResourceEnvironment {
val locale = Locale.getDefault() val locale = Locale.getDefault()
//FIXME: don't use skiko internals //FIXME: don't use skiko internals
val isDarkTheme = currentSystemTheme == SystemTheme.DARK val isDarkTheme = currentSystemTheme == SystemTheme.DARK

5
components/resources/library/src/desktopTest/kotlin/org/jetbrains/compose/resources/ComposeResourceTest.desktop.kt

@ -21,6 +21,11 @@ class ComposeResourceTest {
dropImageCache() dropImageCache()
} }
@Before
fun configureTestEnvironment() {
getResourceEnvironment = ::getTestEnvironment
}
@Test @Test
fun testCountRecompositions() = runComposeUiTest { fun testCountRecompositions() = runComposeUiTest {
runBlockingTest { runBlockingTest {

2
components/resources/library/src/iosMain/kotlin/org/jetbrains/compose/resources/ResourceEnvironment.ios.kt

@ -4,7 +4,7 @@ import platform.Foundation.*
import platform.UIKit.UIScreen import platform.UIKit.UIScreen
import platform.UIKit.UIUserInterfaceStyle import platform.UIKit.UIUserInterfaceStyle
internal actual fun getResourceEnvironment(): ResourceEnvironment { internal actual fun getSystemEnvironment(): ResourceEnvironment {
val locale = NSLocale.currentLocale() val locale = NSLocale.currentLocale()
val mainScreen = UIScreen.mainScreen val mainScreen = UIScreen.mainScreen

2
components/resources/library/src/jsMain/kotlin/org/jetbrains/compose/resources/ResourceEnvironment.js.kt

@ -9,7 +9,7 @@ private external class Intl {
} }
} }
internal actual fun getResourceEnvironment(): ResourceEnvironment { internal actual fun getSystemEnvironment(): ResourceEnvironment {
val locale = Intl.Locale(window.navigator.language) val locale = Intl.Locale(window.navigator.language)
val isDarkTheme = window.matchMedia("(prefers-color-scheme: dark)").matches val isDarkTheme = window.matchMedia("(prefers-color-scheme: dark)").matches
//96 - standard browser DPI https://developer.mozilla.org/en-US/docs/Web/API/Window/devicePixelRatio //96 - standard browser DPI https://developer.mozilla.org/en-US/docs/Web/API/Window/devicePixelRatio

2
components/resources/library/src/macosMain/kotlin/org/jetbrains/compose/resources/ResourceEnvironment.macos.kt

@ -6,7 +6,7 @@ import platform.CoreGraphics.CGDisplayPixelsWide
import platform.CoreGraphics.CGDisplayScreenSize import platform.CoreGraphics.CGDisplayScreenSize
import platform.Foundation.* import platform.Foundation.*
internal actual fun getResourceEnvironment(): ResourceEnvironment { internal actual fun getSystemEnvironment(): ResourceEnvironment {
val locale = NSLocale.currentLocale() val locale = NSLocale.currentLocale()
val isDarkTheme = NSUserDefaults.standardUserDefaults.stringForKey("AppleInterfaceStyle") == "Dark" val isDarkTheme = NSUserDefaults.standardUserDefaults.stringForKey("AppleInterfaceStyle") == "Dark"

2
components/resources/library/src/wasmJsMain/kotlin/org/jetbrains/compose/resources/ResourceEnvironment.wasmJs.kt

@ -9,7 +9,7 @@ private external class Intl {
} }
} }
internal actual fun getResourceEnvironment(): ResourceEnvironment { internal actual fun getSystemEnvironment(): ResourceEnvironment {
val locale = Intl.Locale(window.navigator.language) val locale = Intl.Locale(window.navigator.language)
val isDarkTheme = window.matchMedia("(prefers-color-scheme: dark)").matches val isDarkTheme = window.matchMedia("(prefers-color-scheme: dark)").matches
//96 - standard browser DPI https://developer.mozilla.org/en-US/docs/Web/API/Window/devicePixelRatio //96 - standard browser DPI https://developer.mozilla.org/en-US/docs/Web/API/Window/devicePixelRatio

2
gradle-plugins/gradle.properties

@ -2,7 +2,7 @@ org.gradle.parallel=true
kotlin.code.style=official kotlin.code.style=official
# Default version of Compose Libraries used by Gradle plugin # Default version of Compose Libraries used by Gradle plugin
compose.version=1.6.0-dev1268 compose.version=1.6.0-dev1340
# The latest version of Compose Compiler used by Gradle plugin. Used only in tests/CI. # The latest version of Compose Compiler used by Gradle plugin. Used only in tests/CI.
compose.tests.compiler.version=1.5.4 compose.tests.compiler.version=1.5.4
# The latest version of Kotlin compatible with compose.tests.compiler.version. Used only in tests/CI. # The latest version of Kotlin compatible with compose.tests.compiler.version. Used only in tests/CI.

Loading…
Cancel
Save