Libraries and tools to test different screen sizes

Android provides a variety of tools and APIs that can help you create tests for different screen and window sizes.

DeviceConfigurationOverride

The DeviceConfigurationOverride composable lets you override configuration attributes to test multiple screen and window sizes in Compose layouts. The ForcedSize override fits any layout in the available space, which lets you to run any UI test on any screen size. For example, you can use a small phone form factor to run all your UI tests, including UI tests for big phones, foldables, and tablets.

   DeviceConfigurationOverride(
        DeviceConfigurationOverride.ForcedSize(DpSize(1280.dp, 800.dp))
    ) {
        MyScreen() // Will be rendered in the space for 1280dp by 800dp without clipping.
    }
Figure 1. Using DeviceConfigurationOverride to fit a tablet layout inside a smaller form factor device, as in \*Now in Android*.

Additionally, you can use this composable to set font scale, themes, and other properties that you might want to test on different window sizes.

Robolectric

Use Robolectric to run Compose or view-based UI tests on the JVM locally—no devices or emulators required. You can configure Robolectric to use specific screen sizes, among other useful properties.

In the following example from Now in Android, Robolectric is configured to emulate a screen size of 1000x1000 dp with a resolution of 480 dpi:

@RunWith(RobolectricTestRunner::class)
// Configure Robolectric to use a very large screen size that can fit all of the test sizes.
// This allows enough room to render the content under test without clipping or scaling.
@Config(qualifiers = "w1000dp-h1000dp-480dpi")
class NiaAppScreenSizesScreenshotTests { ... }

You can also set the qualifiers from the test body as done in this snippet from the Now in Android example:

val (width, height, dpi) = ...

// Set qualifiers from specs.
RuntimeEnvironment