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

  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 "com.microsoft.device.dualscreen.testing:testing-kotlin:1.0.0-alpha4"
    androidTestImplementation "androidx.test.espresso:espresso-core:3.4.0"
    androidTestImplementation "androidx.test:runner:1.4.0"
    androidTestImplementation "androidx.test:rules:1.4.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 UI checks and simulate folding features. You can do this by chaining together two rules: a WindowLayoutInfo publisher rule and an activity scenario or Compose test rule.

    private val activityRule = ActivityScenarioRule(MainActivity::class.java)
    private val publisherRule = createWindowLayoutInfoPublisherRule()
    
    @get:Rule
    val testRule: TestRule
    
    init {
        testRule = RuleChain.outerRule(publisherRule).around(activityRule)
    }
    
  6. 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:

  1. Simulate a folding feature
  2. 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:

testVerticalFoldingFeature test 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: