How can I check status bar background color in android (using espresso, uiautomator or ...) ?
For example, I want to check that the status bar color is purple or not !!
How can I check status bar background color in android (using espresso, uiautomator or ...) ?
For example, I want to check that the status bar color is purple or not !!
Option 1:
If you are using an ActivityRule, or something similar where you have access to the activity, great! You can access the statusBarColor easily, with activityRule.activity.window.statusBarColor. You can assert that the color matches your expected color just like in any unit test.
Assert.assertEquals(InstrumentationRegistry.getInstrumentation().context.getColor(R.color.purple), activityRule.activity.window.statusBarColor)
Option 2:
If you're not using an ActivityRule, or the activity there is not the one you are testing (maybe you clicked a button that navigated you to a new activity and destroyed the original), you will probably need to create your own ViewAction.
Here's the one I created for testing in my flow. It gets the base context from a view, and unwraps it until it gets to the activity. Then it does the exact steps as above, for getting the statusBarColor from the activity. I prefer throwing PerformExceptions to directly making assertions in here, since the user can handle the exception a little more gracefully. Up to you!
The resolved hex colors serve no purpose, besides making the exceptions a little more readable.
fun withStatusBarColor(@ColorRes color: Int): ViewAction {
return object : ViewAction {
private var resolvedColorHex = ""
private var resolvedColor = 0
override fun getConstraints(): Matcher<View> {
return CoreMatchers.allOf(ViewMatchers.isEnabled(), ViewMatchers.isDisplayed())
}
override fun getDescription(): String {
return "checking status bar color"
}
override fun perform(controller: UiController?, view: View) {
// get the base activity context to get the window
var context = view.context
while(context != null && context !is Activity && (context is ContextThemeWrapper || context is ContextWrapper)) {
context = when (context) {
is ContextThemeWrapper -> context.baseContext
is ContextWrapper -> context.baseContext
else -> {
val exception = IllegalArgumentException("unexpected context type: ${context::class.java.simpleName}")
throw PerformException.Builder()
.withActionDescription(description)
.withViewDescription("context: ${context::class.java.simpleName}")
.withCause(exception).build()
}
}
}
if(context is Activity) {
resolvedColor = context.getColor(color)
resolvedColorHex = String.format("#%06X", (0xFFFFFF and resolvedColor))
val statusBarColor = context.window.statusBarColor
val statusBarColorHex = String.format("#%06X", (0xFFFFFF and statusBarColor))
if(resolvedColor != context.window.statusBarColor) {
val cause = "The expected status bar color: $resolvedColor ($resolvedColorHex) did not match the actual status bar color: $statusBarColor ($statusBarColorHex)"
throw PerformException.Builder()
.withActionDescription(description)
.withCause(AssertionError(cause))
.withViewDescription("status bar color: $statusBarColorHex")
.build()
}
} else {
val contextClass = if(context != null) context::class.java.simpleName else null
throw PerformException.Builder()
.withActionDescription(description)
.withCause(IllegalArgumentException("Context: $contextClass was not an Activity"))
.withViewDescription("context: $contextClass")
.build()
}
}
}
}
And to use it, perform the action on any view in your activity: onView(withId(R.id.toolbar)).perform(withStatusBarColor(R.color.purple))