Simulate FoldingFeatures in UI tests
Jetpack Window Manager provides a testing library that makes it possible to create mock FoldingFeature
objects. By creating mock folding features, you can test app behavior on foldable devices during UI tests.
The Test Kit provides utility functions that can simulate folding features with different properties, including folding features that match the dimensions of several market devices.
Setup
Create a new test class file in the androidTest directory. This is where you will later add in the code snippets for test rules and tests.
Make sure you have the
mavenCentral()
repository in your top-level build.gradle file:allprojects { repositories { google() mavenCentral() } }
Add the following dependencies to your module-level build.gradle file (current version may be different from what's shown here):
Ensure the
compileSdkVersion
is set to API 33 and thetargetSdkVersion
is set to API 32 or newer in your module-level build.gradle file:android { compileSdkVersion 33 defaultConfig { targetSdkVersion 32 } ... }
Create a
TestRule
that can perform UI checks and simulate folding features. You can do this by chaining together two rules: aWindowLayoutInfo
publisher rule and an activity scenario or Compose test rule.If you're using Espresso to test views, make sure to disable animations on your device.
How to write tests
In the Test Kit libraries, we provide multiple options for simulating folding features with different properties. Due to guidance from the Jetpack Window Manager testing documentation, we recommend only simulating one folding feature per test.
To write a test that simulates a folding feature, follow these steps:
- Simulate a folding feature
- Assert that UI has changed as expected
The example test below demonstrates a simple UI test for an app that shows two panes when a vertical folding feature is present.
@Test
fun testVerticalFoldingFeature() {
onView(withText("pane 1")).check(matches(isDisplayed()))
// 1. Simulate a folding feature
publisherRule.simulateVerticalFoldingFeature(activityRule)
// 2. Assert that UI has changed as expected
onView(withText("pane 1")).check(matches(isDisplayed()))
onView(withText("pane 2")).check(matches(isDisplayed()))
}
The animation below shows how testVerticalFoldingFeature
looks while running on the Surface Duo emulator:
Samples
To see more examples of how to use simulated folding features in foldable tests, check out these resources:
Resources
To read more about instrumented tests and Jetpack Window Manager testing, check out these resources: