• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (C) 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5  * in compliance with the License. You may obtain a copy of the License at
6  * ```
7  *      http://www.apache.org/licenses/LICENSE-2.0
8  * ```
9  * Unless required by applicable law or agreed to in writing, software distributed under the License
10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11  * or implied. See the License for the specific language governing permissions and limitations under
12  * the License.
13  */
14 package com.android.healthconnect.controller.tests.utils
15 
16 import android.view.View
17 import org.hamcrest.Description
18 import org.hamcrest.Matcher
19 import org.hamcrest.TypeSafeMatcher
20 
21 /**
22  * A custom matcher used when there are more than one view with the same resourceId/text/contentDesc
23  * etc.
24  *
25  * @param matcher a view matcher for UI element which may have potentially more than one matched.
26  * Typical example is the element that is repeated in ListView or RecyclerView
27  * @param index the index to select matcher if there are more than one matcher. it's started at
28  * zero.
29  * @return the view matcher selected from given matcher and index.
30  */
withIndexnull31 fun withIndex(matcher: Matcher<View?>, index: Int): Matcher<View?> {
32     return object : TypeSafeMatcher<View?>() {
33         var currentIndex = 0
34         override fun describeTo(description: Description) {
35             description.appendText("with index: ")
36             description.appendValue(index)
37             matcher.describeTo(description)
38         }
39 
40         override fun matchesSafely(view: View?): Boolean {
41             return matcher.matches(view) && currentIndex++ == index
42         }
43     }
44 }
45