DeviceConfigurationOverride.Companion


Summary

Extension functions

DeviceConfigurationOverride

A DeviceConfigurationOverride that overrides the dark mode or light mode theme for the contained content.

android
DeviceConfigurationOverride

A DeviceConfigurationOverride that overrides the font scale for the contained content.

Cmn
android
DeviceConfigurationOverride
@RequiresApi(value = 31)
DeviceConfigurationOverride.Companion.FontWeightAdjustment(
    fontWeightAdjustment: Int
)

A DeviceConfigurationOverride that overrides the font weight adjustment for the contained content.

android
DeviceConfigurationOverride

A DeviceConfigurationOverride that overrides the available size for the contained content.

Cmn
android
DeviceConfigurationOverride

A DeviceConfigurationOverride that overrides the layout direction for the contained content.

Cmn
android
DeviceConfigurationOverride

A DeviceConfigurationOverride that overrides the locales for the contained content.

android
DeviceConfigurationOverride

A DeviceConfigurationOverride that overrides whether the screen is round for the contained content.

android
DeviceConfigurationOverride

A DeviceConfigurationOverride that overrides the window insets for the contained content.

android

Extension functions

DarkMode

fun DeviceConfigurationOverride.Companion.DarkMode(isDarkMode: Boolean): DeviceConfigurationOverride

A DeviceConfigurationOverride that overrides the dark mode or light mode theme for the contained content. Inside the content under test, isSystemInDarkTheme() will return isDarkMode.

import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.ui.test.DarkMode
import androidx.compose.ui.test.DeviceConfigurationOverride

DeviceConfigurationOverride(DeviceConfigurationOverride.DarkMode(true)) {
    isSystemInDarkTheme() // will be true
}

FontScale

fun DeviceConfigurationOverride.Companion.FontScale(fontScale: Float): DeviceConfigurationOverride

A DeviceConfigurationOverride that overrides the font scale for the contained content.

import androidx.compose.ui.test.DeviceConfigurationOverride
import androidx.compose.ui.test.FontScale

DeviceConfigurationOverride(DeviceConfigurationOverride.FontScale(1.5f)) {
    MyScreen() // will be rendered with a larger than default font scale
}

FontWeightAdjustment

@RequiresApi(value = 31)
fun DeviceConfigurationOverride.Companion.FontWeightAdjustment(
    fontWeightAdjustment: Int
): DeviceConfigurationOverride

A DeviceConfigurationOverride that overrides the font weight adjustment for the contained content.

import androidx.compose.ui.test.DeviceConfigurationOverride
import androidx.compose.ui.test.FontWeightAdjustment

DeviceConfigurationOverride(DeviceConfigurationOverride.FontWeightAdjustment(200)) {
    MyComponent() // will be rendered with adjusted font weight
}

ForcedSize

fun DeviceConfigurationOverride.Companion.ForcedSize(size: DpSize): DeviceConfigurationOverride

A DeviceConfigurationOverride that overrides the available size for the contained content.

This is only suitable for tests, since this will override LocalDensity to ensure that the size is met (as opposed to Modifier.requiredSize which will result in clipping).

import androidx.compose.ui.test.DeviceConfigurationOverride
import androidx.compose.ui.test.ForcedSize
import androidx.compose.ui.unit.DpSize
import androidx.compose.ui.unit.dp

DeviceConfigurationOverride(DeviceConfigurationOverride.ForcedSize(DpSize(1280.dp, 800.dp))) {
    MyScreen() // will be rendered in the space for 1280dp by 800dp without clipping
}

LayoutDirection

fun DeviceConfigurationOverride.Companion.LayoutDirection(
    layoutDirection: LayoutDirection
): DeviceConfigurationOverride

A DeviceConfigurationOverride that overrides the layout direction for the contained content.

import androidx.compose.ui.test.DeviceConfigurationOverride
import androidx.compose.ui.test.LayoutDirection
import androidx.compose.ui.unit.LayoutDirection

DeviceConfigurationOverride(DeviceConfigurationOverride.LayoutDirection(LayoutDirection.Rtl)) {
    MyComponent() // will be rendered with a right-to-left layout direction
}

Locales

fun DeviceConfigurationOverride.Companion.Locales(locales: LocaleList): DeviceConfigurationOverride

A DeviceConfigurationOverride that overrides the locales for the contained content.

This will change resource resolution for the content under test, and also override the layout direction as specified by the locales.

import androidx.compose.ui.test.DeviceConfigurationOverride
import androidx.compose.ui.test.Locales
import androidx.compose.ui.text.intl.LocaleList

DeviceConfigurationOverride(DeviceConfigurationOverride.Locales(LocaleList("es-ES"))) {
    MyScreen() // will be rendered with overridden locale
}

RoundScreen

@RequiresApi(value = 23)
fun DeviceConfigurationOverride.Companion.RoundScreen(
    isScreenRound: Boolean
): DeviceConfigurationOverride

A DeviceConfigurationOverride that overrides whether the screen is round for the contained content.

import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.test.DeviceConfigurationOverride
import androidx.compose.ui.test.RoundScreen

DeviceConfigurationOverride(DeviceConfigurationOverride.RoundScreen(true)) {
    LocalConfiguration.current.isScreenRound // will be true
}

WindowInsets

fun DeviceConfigurationOverride.Companion.WindowInsets(
    windowInsets: WindowInsetsCompat
): DeviceConfigurationOverride

A DeviceConfigurationOverride that overrides the window insets for the contained content.

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.safeDrawingPadding
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.test.DeviceConfigurationOverride
import androidx.compose.ui.test.WindowInsets
import androidx.compose.ui.unit.DpRect
import androidx.compose.ui.unit.IntRect
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.roundToIntRect
import androidx.core.view.WindowInsetsCompat

fun IntRect.toAndroidXInsets() = androidx.core.graphics.Insets.of(left, top, right, bottom)

DeviceConfigurationOverride(
    DeviceConfigurationOverride.WindowInsets(
        WindowInsetsCompat.Builder()
            .setInsets(
                WindowInsetsCompat.Type.captionBar(),
                with(LocalDensity.current) { DpRect(0.dp, 64.dp, 0.dp, 0.dp).toRect() }
                    .roundToIntRect()
                    .toAndroidXInsets()
            )
            .setInsets(
                WindowInsetsCompat.Type.navigationBars(),
                with(LocalDensity.current) { DpRect(24.dp, 0.dp, 48.dp, 24.dp).toRect() }
                    .roundToIntRect()
                    .toAndroidXInsets()
            )
            .build()
    )
) {
    Box(
        Modifier.background(Color.Blue)
            // Will apply 64dp padding on the top, 24dp padding on the sides, and 48dp on the
            // bottom
            .safeDrawingPadding()
            .background(Color.Red)
    )
}