Detect *flicker*— any discontinuous, or unpredictable behavior seen during UI transitions that is not due to performance. This is often the result of a logicerror in the code and difficult to identify because the issue is transient and at times difficult to reproduce. This library helps create integration tests between `SurfaceFlinger`, `WindowManager` and `SystemUI` to identify flicker.
This set of tests use the flickerlib from `platform_testing/libraries/flicker` to execute a set of common UI transitions to detect discontinuous or unpredictable behavior.
## Adding a Test
The library builds and runs UI transitions, captures Winscope traces and exposes common assertions that can be tested against each trace.
### Building Transitions
Start by defining common or error prone transitions using `TransitionRunner`.
```java
// Example: Build a transition that cold launches an app from launcher
// Press the home button and close the app to test a cold start
.runBefore(device::pressHome)
.runBefore(testApp::exit)
// Run the transition under test
// Open the app and wait for UI to be idle
// This is the part of the transition that will be tested.
.run(testApp::open)
.run(device::waitForIdle)
// Perform any tear downs
// Close the app
.runAfterAll(testApp::exit)
// Number of times to repeat the transition to catch any flaky issues
.repeat(5);
```
Run the transition to get a list of `TransitionResult` for each time the transition is repeated.
```java
List<TransitionResult>results=transition.run();
```
`TransitionResult` contains paths to test artifacts such as Winscope traces and screen recordings.
### Checking Assertions
Each `TransitionResult` can be tested using an extension of the Google Truth library, `LayersTraceSubject` and `WmTraceSubject`. They try to balance test principles set out by Google Truth (not supporting nested assertions, keeping assertions simple) with providing support for common assertion use cases.
Each trace can be represented as a ordered collection of trace entries, with an associated timestamp. Each trace entry has common assertion checks. The trace subjects expose methods to filter the range of entries and test for changing assertions.
By default tests should inherit from `RotationTestBase` or `NonRotationTestBase` and must override the variable `transitionToRun` (Kotlin) or the function `getTransitionToRun()` (Java).
Only tests that are not supported by these classes should inherit directly from the `FlickerTestBase` class.
// check a change in assertions, e.g. wallpaper window is visible,
// then wallpaper window becomes and stays invisible
assertThat(result)
.showsBelowAppWindow("wallpaper")
.then()
.hidesBelowAppWindow("wallpaper")
.forAllEntries();
```
### Rotation animations and transitions
All assertions return `Result` which contains a `success` flag, `assertionName` string identifier, and `reason` string to provide actionable details to the user. The `reason` string is build along the way with all the details as to why the assertions failed and any hints which might help the user determine the root cause. Failed assertion message will also contain a path to the trace that was tested. Example of a failed test:
Tests that rotate the device should inherit from `RotationTestBase`.
Tests that inherit from the class automatically receive start and end rotation values.
Moreover, these tests inherit the following checks:
* all regions on the screen are covered
* status bar is always visible
* status bar rotates
* nav bar is always visible
* nav bar is rotates
```
java.lang.AssertionError: Not true that <com.android.server.wm.flicker.LayersTrace@65da4cc>
Layers Trace can be found in: /layers_trace_emptyregion.pb
Timestamp: 2308008331271
Assertion: coversRegion
Reason: Region to test: Rect(0, 0 - 1440, 2880)
first empty point: 0, 99
visible regions:
StatusBar#0Rect(0, 0 - 1440, 98)
NavigationBar#0Rect(0, 2712 - 1440, 2880)
ScreenDecorOverlay#0Rect(0, 0 - 1440, 91)
...
at com.google.common.truth.FailureStrategy.fail(FailureStrategy.java:24)
...
```
The default tests can be disabled by overriding the respective methods and including an `@Ignore` annotation.
---
### Non-Rotation animations and transitions
## Running Tests
`NonRotationTestBase` was created to make it easier to write tests that do not involve rotation (e.g., `Pip`, `split screen` or `IME`).
Tests that inherit from the class are automatically executed twice: once in portrait and once in landscape mode and the assertions are checked independently.
Moreover, these tests inherit the following checks:
* all regions on the screen are covered
* status bar is always visible
* nav bar is always visible
The tests can be run as any other Android JUnit tests. `platform_testing/tests/flicker` uses the library to test common UI transitions. Run `atest FlickerTest` to execute these tests.
The default tests can be disabled by overriding the respective methods and including an `@Ignore` annotation.
---
### Exceptional cases
## Other Topics
### Monitors
Monitors capture test artifacts for each transition run. They are started before each iteration of the test transition (after the `runBefore` calls) and stopped after the transition is completed. Each iteration will produce a new test artifact. The following monitors are available:
Tests that rotate the device should inherit from `RotationTestBase`.
This class allows the test to be freely configured and does not provide any assertions.
#### LayersTraceMonitor
Captures Layers trace. This monitor is started by default. Build a transition with `skipLayersTrace()` to disable this monitor.
#### WindowManagerTraceMonitor
Captures Window Manager trace. This monitor is started by default. Build a transition with `skipWindowManagerTrace()` to disable this monitor.
#### WindowAnimationFrameStatsMonitor
Captures WindowAnimationFrameStats for the transition. This monitor is started by default and is used to eliminate *janky* runs. If an iteration has skipped frames, as determined by WindowAnimationFrameStats, the results for the iteration is skipped. If the list of results is empty after all iterations are completed, then the test should fail. Build a transition with `includeJankyRuns()` to disable this monitor.
#### ScreenRecorder
Captures screen to a video file. This monitor is disabled by default. Build a transition with `recordEachRun()` to capture each transition or build with `recordAllRuns()` to capture every transition including setup and teardown.
---
### Example
### Extending Assertions
To add a new assertion, add a function to one of the trace entry classes, `LayersTrace.Entry` or `WindowManagerTrace.Entry`.
```java
// Example adds an assertion to the check if layer is hidden by parent.
ResultisHiddenByParent(StringlayerName){
// Result should contain a details if assertion fails for any reason
// such as if layer is not found or layer is not hidden by parent
// or layer has no parent.
// ...
Start by defining common or error prone transitions using `TransitionRunner`.
Then add a function to the trace subject `LayersTraceSubject` or `WmTraceSubject` which will add the assertion for testing. When the assertion is evaluated, the trace will first be filtered then the assertion will be applied to the remaining entries.