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
DeviceConfigurationOverride.Companion.Keyboard(
    keyboardType: Int,
    isHardKeyboardHidden: Boolean,
    isHidden: Boolean
)

A DeviceConfigurationOverride that overrides the current keyboard type.

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
DeviceConfigurationOverride.Companion.Navigation(
    navigationType: Int,
    isHidden: Boolean
)

A DeviceConfigurationOverride that overrides the current navigation type and whether it is hidden.

android
DeviceConfigurationOverride

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

android
DeviceConfigurationOverride

A DeviceConfigurationOverride that overrides the current touchscreen type.

android
DeviceConfigurationOverride

A DeviceConfigurationOverride that overrides the current ui mode type.

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
}
Parameters
isDarkMode: Boolean

if true, render content under test in dark mode.

Returns
DeviceConfigurationOverride

a DeviceConfigurationOverride that specifies the dark mode for the content under test.

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
}
Parameters
fontScale: Float

the font scale to use for the content under test.

Returns
DeviceConfigurationOverride

a DeviceConfigurationOverride that specifies the font scale for the content under test.

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
}
Parameters
fontWeightAdjustment: Int

the font weight adjustment to use to render the content under test.

Returns
DeviceConfigurationOverride

a DeviceConfigurationOverride that specifies the font weight adjustment for the content under test.

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
}
Parameters
size: DpSize

the DpSize to force the contained content to render in, changing density if necessary

Returns
DeviceConfigurationOverride

a DeviceConfigurationOverride that forces the content size.

Keyboard

fun DeviceConfigurationOverride.Companion.Keyboard(
    keyboardType: Int,
    isHardKeyboardHidden: Boolean = false,
    isHidden: Boolean = false
): DeviceConfigurationOverride

A DeviceConfigurationOverride that overrides the current keyboard type.

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

DeviceConfigurationOverride(
    DeviceConfigurationOverride.Keyboard(Configuration.KEYBOARD_QWERTY)
) {
    LocalConfiguration.current.keyboard // will be Configuration.KEYBOARD_QWERTY
}
Parameters
keyboardType: Int

the keyboard type to render content under test in. This should be one of the Configuration.KEYBOARD_* types.

isHardKeyboardHidden: Boolean = false

if true, render the content under test with a hidden hard keyboard.

isHidden: Boolean = false

if true, render the content under test with a hidden keyboard.

Returns
DeviceConfigurationOverride

a DeviceConfigurationOverride that specifies the keyboard status for the content under test.

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
}
Parameters
layoutDirection: LayoutDirection

the LayoutDirection to use for the content under test.

Returns
DeviceConfigurationOverride

a DeviceConfigurationOverride that specifies the layout direction for the content under test.

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
}
Parameters
locales: LocaleList

the LocaleList to use for the content under test.

Returns
DeviceConfigurationOverride

a DeviceConfigurationOverride that specifies the locales for the content under test.

Navigation

fun DeviceConfigurationOverride.Companion.Navigation(
    navigationType: Int,
    isHidden: Boolean = false
): DeviceConfigurationOverride

A DeviceConfigurationOverride that overrides the current navigation type and whether it is hidden.

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

DeviceConfigurationOverride(
    DeviceConfigurationOverride.Navigation(
        navigationType = Configuration.NAVIGATION_DPAD,
        isHidden = false,
    )
) {
    LocalConfiguration.current.navigation // will be Configuration.NAVIGATION_DPAD
    LocalConfiguration.current.navigationHidden // will be Configuration.NAVIGATIONHIDDEN_NO
}
Parameters
navigationType: Int

the navigation type to render the content under test in. This should be one of the Configuration.NAVIGATION_* values.

isHidden: Boolean = false

if true, render the content under test with hidden navigation.

Returns
DeviceConfigurationOverride

a DeviceConfigurationOverride that specifies the navigation type for the content under test.

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
}
Parameters
isScreenRound: Boolean

if true, render content under test in a round screen.

Returns
DeviceConfigurationOverride

a DeviceConfigurationOverride that specifies whether the screen is round for the content under test.

Touchscreen

fun DeviceConfigurationOverride.Companion.Touchscreen(
    isTouchScreen: Boolean
): DeviceConfigurationOverride

A DeviceConfigurationOverride that overrides the current touchscreen type.

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

DeviceConfigurationOverride(DeviceConfigurationOverride.Touchscreen(false)) {
    LocalConfiguration.current.touchscreen // will be Configuration.TOUCHSCREEN_NOTOUCH
}

UiMode

fun DeviceConfigurationOverride.Companion.UiMode(uiModeType: Int): DeviceConfigurationOverride

A DeviceConfigurationOverride that overrides the current ui mode type.

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

DeviceConfigurationOverride(
    DeviceConfigurationOverride.UiMode(Configuration.UI_MODE_TYPE_CAR)
) {
    // will be Configuration.UI_MODE_TYPE_CAR
    LocalConfiguration.current.uiMode and Configuration.UI_MODE_TYPE_MASK
}
Parameters
uiModeType: Int

the uiMode type to render the content under test in. This should be one of the Configuration.UI_MODE_TYPE_* values.

Returns
DeviceConfigurationOverride

a DeviceConfigurationOverride that specifies the uiMode type for the content under test.

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)
    )
}
Parameters
windowInsets: WindowInsetsCompat

the WindowInsetsCompat to render the content under test in.

Returns
DeviceConfigurationOverride

a DeviceConfigurationOverride that specifies the window insets for the content under test.