1# SDK Sandbox UI Tests 2 3## Overview 4 5The SDK Sandbox UI test suite facilitates the testing of the UI components of the SDK sandbox, such 6as the rendering of remote views into a `SurfaceView`. This test suite enables the testing of UI 7functionality on multiple device specifications, which may be configured on a per-class basis. 8 9These tests make use of the `SdkSandboxUiTestRule` rule, which includes a screenshot 10asserter. This asserter may be used to compare portions of the emulated display with "golden" 11images from the `assets` directory. The screenshot asserter uses an `AlmostPerfectMatcher` to 12ensure that the rendered views are indistinguishable from the expected golden images (allowing a 13very small tolerance to allow for rendering differences on different devices). 14 15As part of the `SdkSandboxUiTestRule`, an SDK is loaded in the test app. The loaded SDK 16may be configured on a per-class basis, by passing the name of the `SandboxedSdkProvider` to the 17`SdkSandboxUiTestRule`. If the test needs to interact with the SDK's `SandboxedSdk` object, this 18can be done by calling the `getSandboxedSdk()` method of the test rule. 19 20## Rendering Remote Views 21Remote views may be loaded by using `SdkSandboxUiTestRule.renderInView()`. This will render a view 22of size `width` and `height` in pixels. The view will contain the drawable defined by the passed 23resource ID. 24 25## Interacting with Activities 26 27To test UI interactions such as clicking and scrolling, or to locate a view on screen, it will 28sometimes be necessary for the test to interact with the test activity. This can be done 29by calling `getActivityScenario()` on the test rule. The test app will be launched with the default 30activity class, which may be specified in the constructor of the test rule. 31The activity may be switched by calling `switchActivity(Activity activity)`. 32 33 34```java 35public class ExampleTest { 36 SdkSandboxUiRule mRule; 37 38 @Test 39 public void testActivities() { 40 // Default activity is current running 41 mRule.getActivityScenario().onActivity(activity -> { 42 // interact with activity 43 }); 44 mRule.switchActivity(NewActivity.class); 45 // NewActivity is now running 46 mRule.getActivityScenario().onActivity(activity -> { 47 // interact with new activity 48 }); 49 } 50} 51``` 52 53## Input Injection 54Due to `SurfaceView` restrictions, `MotionEvents` cannot be programmatically injected into remotely 55rendered views. Instead, the input event can be simulated using the test instrumentation. 56For example, to simulate a click we can find the x and y co-ordinates of the view on screen, and 57send `MOTION_DOWN` and `MOTION_UP` events to the test instrumentation at these co-ordinates. 58