> For the complete documentation index, see [llms.txt](https://sergio-sastre.gitbook.io/androiduitestingutils/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://sergio-sastre.gitbook.io/androiduitestingutils/usage/testrules-and-other-utils.md).

# TestRules & other utils

Some utilities and TestRules to facilitate UI and Screenshot Testing in instrumented tests.

## TestRules

```kotlin
@get:Rule
val disableAnimations = DisableAnimationsTestRule()

// Sets the Locale of the app under test only, 
// i.e. the per-app language preference feature
@get:Rule
val inAppLocale = InAppLocaleTestRule(
    locale = "en",
    activityScenarioRule = activityScenarioRule
)

// System UI
@get:Rule
val navigation = NavigationModeTestRule(Navigation.THREE_BUTTON)

@get:Rule
val statusBar = StatusBarTestRule(clockTime = ClockTime.from("10:00"))

@get:Rule
val systemUi = SystemUiTestRule(
    statusBarConfig = StatusBarConfig(clockTime = ClockTime.from("10:00")),
    navigationConfig = NavigationConfig(Navigation.THREE_BUTTON)
)

// Sets the Locale of the Android system
@get:Rule
val systemLocale = SystemLocaleTestRule("en")

@get:Rule
val uiMode = UiModeTestRule(UiMode.NIGHT)

// Accessibility Test Rules
@get:Rule
val fontSize = FontSizeTestRule(FontSize.LARGEST)

@get:Rule
val displaySize = DisplaySizeTestRule(DisplaySize.LARGEST)

@get:Rule
val boldText = FontWeightTestRule(FontWeight.BOLD)

@get:Rule
val outlineText = OutlineTextTestRule(OutlineText.ENABLED)

@get:Rule
val colorContrast = ColorContrastTestRule(ColorContrast.HIGH)

```

### **System UI**

<figure><img src="https://214357818-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FdUY7hG3p9hfCVw56vkGB%2Fuploads%2F7clQhvmrY2SwgSY2Mv6c%2FSystemUi.png?alt=media&amp;token=075f6efe-c757-49ec-8073-f0e1f556750a" alt=""><figcaption></figcaption></figure>

{% hint style="info" %}
This TestRule comes with a `drawToFullScreenBitmap()` method for screenshot testing
{% endhint %}

### **Accessibility**

From all those Test Rules, it is important to highlight those that can be used in instrumentation tests for Accessibility:

<figure><img src="https://214357818-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FdUY7hG3p9hfCVw56vkGB%2Fuploads%2FZGzv6ivHMNxshEisb55B%2F6.png?alt=media&amp;token=f86bbb64-e27c-4887-a7e8-ac01d2a4f127" alt=""><figcaption><p><br></p></figcaption></figure>

<figure><img src="https://214357818-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FdUY7hG3p9hfCVw56vkGB%2Fuploads%2FmqgVqtUuQTbzt5LmVxoW%2F7.png?alt=media&amp;token=6da28f36-00eb-43fd-9b6d-3cc1465153a7" alt=""><figcaption></figcaption></figure>

<figure><img src="https://214357818-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FdUY7hG3p9hfCVw56vkGB%2Fuploads%2FQgx8hgg1ji9kBmWjbKPX%2F9.png?alt=media&amp;token=4addcf1c-9f0f-4c19-8910-df2bda98ec2e" alt=""><figcaption></figcaption></figure>

<figure><img src="https://214357818-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FdUY7hG3p9hfCVw56vkGB%2Fuploads%2Fj0l6KSPO0oSOh1dqCj6Y%2FOutlineText.png?alt=media&amp;token=507a29be-bf01-4ff3-84a0-8c2ad586ec98" alt=""><figcaption></figcaption></figure>

<figure><img src="https://214357818-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FdUY7hG3p9hfCVw56vkGB%2Fuploads%2FYbE0MOjH9fpTeJIt7MKL%2FColorContrast.png?alt=media&amp;token=20b0ff73-6d26-4a4c-a64a-1e7d5791f67a" alt=""><figcaption></figcaption></figure>

{% hint style="info" %}
All of them, except the `OutlineTextTestRule` and the `ColorContrastTestRule` , are also supported in

* `ActivityConfigItem`
* `FragmentConfigItem`
* `ComposableConfigItem`
* `ViewConfigItem`
* `ScreenshotConfigForView` (CrossLibrary)
* `ScreenshotConfigForComposable` (CrossLibrary)
  {% endhint %}

## **WaitFor...**

1. `waitForActivity`\
   Analogue to the one defined in [pedrovgs/Shot](https://github.com/pedrovgs/Shot). It's also available in this library for compatibility with other screenshot testing frameworks, like Facebook [screenshot-tests-for-android](https://github.com/facebook/screenshot-tests-for-android).
2. `waitForFragment`\
   Analogue to `waitForActivity` but for Fragment.
3. `activity.waitForComposeView`\
   Returns the root Composable in the activity as a ComposeView. You can call later `drawToBitmap` or `drawToBitmapWithElevation` on it to screenshot test its corresponding bitmap.
4. `waitForMeasuredView/Dialog/ViewHolder(exactWidth, exactHeight)`\
   Inflates the layout in the main thread, sets its width and height to those given, and waits till the thread is idle, returning the inflated view. \
   Comes in handy with libraries that do not support to take a screenshot with a given width/height, like Dropshots.

<figure><img src="https://user-images.githubusercontent.com/6097181/211920753-35ee8f0b-d661-4623-8619-418c3972f1c2.png" alt="" width="563"><figcaption><p>Difference between waitForView() and waitForMeasuredView()</p></figcaption></figure>

{% hint style="warning" %}
Prefer `waitForMeasuredView` over `waitForView` (which is discouraged), specially if using Dropshots
{% endhint %}

## **Inflate or measure**

1. `activity.inflate(R.layout_of_your_view)`\
   Use it to inflate android Views with the activity's context configuration. In doing so, the configuration becomes effective in the view. It also adds the view to the Activity's root.<br>
2. `activity.inflateAndWaitForIdle(R.layout_of_your_view)`\
   Like `activity.inflate(...)`, but waits till the view is Idle to return it

{% hint style="warning" %}
Do not wrap it with `waitForMeasuredView{...}` or it will throw an exception.
{% endhint %}

3. `MeasureViewHelpers`\
   Analogue to the `ViewHelpers` defined in Facebook [screenshot-tests-for-android](https://github.com/facebook/screenshot-tests-for-android). In most cases, you don't need to use it directly but via `waitForMeasuredView(exactWidth, exactHeight)`, which calls `MeasuredViewHelpers` under the hood.
