Simulate swipe gestures on the Surface Duo
UiAutomator is a testing framework that provides cross-app testing functionality and access to device sensors. Using the swipe
method from the UiDevice class and device dimensions, you can simulate different gestures on the Surface Duo during tests.
The Test Kit provides utility functions that perform swipes between different coordinates to simulate spanning an app, unspanning an app, switching an app between screens, and closing an app.
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):
androidTestImplementation "com.microsoft.device.dualscreen.testing:testing-kotlin:1.0.0-alpha4" androidTestImplementation "androidx.test.uiautomator:uiautomator:2.2.0" androidTestImplementation "androidx.test.espresso:espresso-core:3.4.0" androidTestImplementation "androidx.test:runner:1.4.0" androidTestImplementation "androidx.test:rules:1.4.0"
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
and aUiDevice
instance inside your test class.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 options for performing display operations on the Surface Duo. All of these methods use the swipe method from UiDevice
to simulate swipes between different coordinates. To make your tests as reliable as possible, we recommend performing assertions and checks in between swipes and minimizing the number of swipes performed in each test.
To write a test that uses swipe gestures, follow these steps:
- Perform swipe gesture
- Assert that UI has changed as expected
The example test below demonstrates a simple UI test for an app that shows two panes when spanned.
@Test
fun testSpan() {
onView(withText("pane 1")).check(matches(isDisplayed()))
// 1. Perform swipe gesture
device.spanFromStart()
// 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 testSpan
looks while running on the Surface Duo emulator:
Samples
To see more examples of how to use simulated swipe gestures in Surface Duo tests, check out these resources:
- Surface Duo SDK
- ComposeTesting library sample
- FoldingVideo
- PhotoEditor
- SourceEditor
- TwoNote
- Dual Screen Experience Example
Resources
To read more about instrumented tests and UiAutomator, check out these resources: