Write Jetpack Compose tests for foldable devices

Jetpack Compose provides a testing API that can be used to test your layouts, much like how Espresso is used with the traditional view system. Jetpack Window Manager also provides a testing library that makes it easy to simulate FoldingFeature objects. With these resources, you can write instrumented UI tests in your Compose projects for foldable and dual-screen devices.

ComposeTesting library

To reduce the amount of code needed to write foldable tests, you can also use the ComposeTesting library, which is part of our Test Kit. This library contains utility methods for simulating FoldingFeature objects and other useful functionality for Compose UI tests, including swipe gesture and string resource helper methods.

To use the ComposeTesting library in your tests, make sure to click the With ComposeTesting library tab for the code snippets in this article.

Setup

  1. 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.

  2. Make sure you have the mavenCentral() repository in your top-level build.gradle file:

    allprojects {
        repositories {
            google()
            mavenCentral()
         }
    }
    
  3. Add the following dependencies to your module-level build.gradle file (current version may be different from what's shown here):

    androidTestImplementation "androidx.compose.ui:ui-test-junit4:1.3.0"
    androidTestImplementation "androidx.window:window-testing:1.0.0"
    
    // Only necessary if Jetpack Window Manager isn't imported for implementation
    androidTestImplementation "androidx.window:window:1.0.0"
    
  4. Ensure the compileSdkVersion is set to API 33 and the targetSdkVersion is set to API 32 or newer in your module-level build.gradle file:

    android { 
        compileSdkVersion 33
    
        defaultConfig { 
            targetSdkVersion 32
        } 
        ... 
    }
    
  5. Create a TestRule that can perform Compose checks and simulate folding features. You can do this by chaining together two rules: a WindowLayoutInfo publisher rule and an Android Compose test rule.

    private val composeTestRule = createAndroidComposeRule<MainActivity>()
    private val publisherRule = WindowLayoutInfoPublisherRule()
    
    @get:Rule
    val testRule: TestRule
    
    init {
        testRule = RuleChain.outerRule(publisherRule).around(composeTestRule)
    }
    

How to write tests

To write a Compose test for foldable and dual-screen devices, follow these four steps:

  1. Set the content of the test
  2. Simulate a FoldingFeature
  3. Find the node(s) to test
  4. Perform assertions or actions on the node(s)

The code snippets below show an example test that checks to see if the "pane 1" and "pane 2" text elements are shown in the TwoPaneSample composable when a vertical FoldingFeature is present.

@Test
fun sample_verticalFoldingFeature_showsTwoPanes() {
    // 1. Optional: set the content of the test (default is MainActivity content)
    composeTestRule.activity.setContent {
        TwoPaneSample()
    }

    // 2. Simulate a vertical FoldingFeature
    composeTestRule.activityRule.scenario.onActivity { activity ->
        val verticalFoldingFeature = FoldingFeature(
            activity = activity,
            orientation = FoldingFeature.Orientation.VERTICAL
        )
        val windowLayoutInfo = TestWindowLayoutInfo(listOf(verticalFoldingFeature))
        publisherRule.overrideWindowLayoutInfo(windowLayoutInfo)
    }

    // 3. Find the nodes to test and 4. Perform assertions on the nodes
    composeTestRule.onNodeWithText("pane 1").assertIsDisplayed()
    composeTestRule.onNodeWithText("pane 2").assertIsDisplayed()
}

When writing your own tests, you can customize each step based on your needs. Any composable can go in the setContent lambda, and you can change the position, size, orientation, and state of the FoldingFeature.

Resources

To learn more about testing with Jetpack Compose and Jetpack Window Manager, refer to these resources: