1 /* 2 * Copyright (C) 2022 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 platform.test.screenshot 18 19 import android.view.View 20 import android.view.ViewGroup 21 import android.view.WindowInsets 22 23 /** [Sequence] that yields all of the direct children of this [ViewGroup] */ 24 val ViewGroup.children <lambda>null25 get() = sequence { for (i in 0 until childCount) yield(getChildAt(i)) } 26 27 /** Returns the total number of child views below this view */ getChildCountRecursivelynull28fun View.getChildCountRecursively(): Int { 29 return if (this is ViewGroup) { 30 var count = childCount 31 for (i in 0 until childCount) { 32 getChildAt(i)?.let { count += it.getChildCountRecursively() } 33 } 34 count 35 } else { 36 0 37 } 38 } 39 40 /** 41 * Elevation/shadows is not deterministic when doing hardware rendering, this exentsion allows to 42 * disable it for any view in the hierarchy. 43 */ removeElevationRecursivelynull44fun View.removeElevationRecursively() { 45 this.elevation = 0f 46 (this as? ViewGroup)?.children?.forEach(View::removeElevationRecursively) 47 } 48 49 /** 50 * Different devices could have different insets (e.g. different height of the navigation bar or 51 * taskbar). This method dispatches empty insets to the whole view hierarchy and removes the 52 * original listener, so the views won't receive real insets. 53 */ removeInsetsRecursivelynull54fun View.removeInsetsRecursively() { 55 this.dispatchApplyWindowInsets(WindowInsets.CONSUMED) 56 this.setOnApplyWindowInsetsListener(null) 57 (this as? ViewGroup)?.children?.forEach(View::removeInsetsRecursively) 58 } 59