/* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ package org.mozilla.fenix.helpers import android.graphics.Bitmap import android.view.View import android.view.ViewGroup import org.hamcrest.CoreMatchers.not import org.hamcrest.Description import org.hamcrest.Matcher import org.hamcrest.TypeSafeMatcher import org.mozilla.fenix.helpers.matchers.BitmapDrawableMatcher import androidx.test.espresso.matcher.ViewMatchers.isChecked as espressoIsChecked import androidx.test.espresso.matcher.ViewMatchers.isEnabled as espressoIsEnabled import androidx.test.espresso.matcher.ViewMatchers.isSelected as espressoIsSelected /** * The [espressoIsEnabled] function that can also handle disabled state through the boolean argument. */ fun isEnabled(isEnabled: Boolean): Matcher = maybeInvertMatcher(espressoIsEnabled(), isEnabled) /** * The [espressoIsChecked] function that can also handle unchecked state through the boolean argument. */ fun isChecked(isChecked: Boolean): Matcher = maybeInvertMatcher(espressoIsChecked(), isChecked) /** * The [espressoIsSelected] function that can also handle not selected state through the boolean argument. */ fun isSelected(isSelected: Boolean): Matcher = maybeInvertMatcher(espressoIsSelected(), isSelected) private fun maybeInvertMatcher(matcher: Matcher, useUnmodifiedMatcher: Boolean): Matcher = when { useUnmodifiedMatcher -> matcher else -> not(matcher) } fun withBitmapDrawable(bitmap: Bitmap, name: String): Matcher? = BitmapDrawableMatcher(bitmap, name) fun nthChildOf( parentMatcher: Matcher, childPosition: Int ): Matcher { return object : TypeSafeMatcher() { override fun describeTo(description: Description) { description.appendText("Position is $childPosition") } public override fun matchesSafely(view: View): Boolean { if (view.parent !is ViewGroup) { return parentMatcher.matches(view.parent) } val group = view.parent as ViewGroup return parentMatcher.matches(view.parent) && group.getChildAt(childPosition) == view } } }