1 /*
2  * Copyright (C) 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.permissioncontroller
18 
19 import android.view.View
20 import android.widget.TextView
21 import androidx.preference.R
22 import androidx.recyclerview.widget.RecyclerView
23 import androidx.test.espresso.Espresso.onView
24 import androidx.test.espresso.UiController
25 import androidx.test.espresso.ViewAction
26 import androidx.test.espresso.contrib.RecyclerViewActions.scrollTo
27 import androidx.test.espresso.matcher.ViewMatchers.hasDescendant
28 import androidx.test.espresso.matcher.ViewMatchers.hasSibling
29 import androidx.test.espresso.matcher.ViewMatchers.withId
30 import androidx.test.espresso.matcher.ViewMatchers.withResourceName
31 import androidx.test.espresso.matcher.ViewMatchers.withText
32 import org.hamcrest.Matchers
33 import org.hamcrest.Matchers.allOf
34 
35 /**
36  * Get a {@link ViewAction} that runs a command on a view.
37  *
38  * @param onViewAction action to run on the view
39  */
runOnViewnull40 fun <T : View> runOnView(onViewAction: (T) -> Unit): ViewAction {
41     return object : ViewAction {
42         override fun getDescription() = "run on view"
43 
44         override fun getConstraints() = Matchers.any(View::class.java)
45 
46         override fun perform(uiController: UiController, view: View) {
47             onViewAction(view as T)
48         }
49     }
50 }
51 
52 /**
53  * Scroll until a preference is visible.
54  *
55  * @param title title of the preference
56  */
scrollToPreferencenull57 fun scrollToPreference(title: CharSequence) {
58     onView(withId(R.id.recycler_view))
59             .perform(scrollTo<RecyclerView.ViewHolder>(
60                     hasDescendant(withText(title.toString()))))
61 }
62 
63 /**
64  * Make sure a preference cannot be found.
65  *
66  * @param title title of the preference
67  */
assertDoesNotHavePreferencenull68 fun assertDoesNotHavePreference(title: CharSequence) {
69     try {
70         scrollToPreference(title)
71     } catch (e: Exception) {
72         return
73     }
74 
75     throw AssertionError("View with title $title was found")
76 }
77 
78 /**
79  * Get summary of preference.
80  *
81  * @param title title of the preference
82  *
83  * @return summary of preference
84  */
getPreferenceSummarynull85 fun getPreferenceSummary(title: CharSequence): CharSequence {
86     lateinit var summary: CharSequence
87 
88     onView(allOf(hasSibling(withText(title.toString())), withResourceName("summary")))
89             .perform(runOnView<TextView> { summary = it.text })
90 
91     return summary
92 }
93