From b0a1e0884ec43b2426ae6617174254745b50c772 Mon Sep 17 00:00:00 2001 From: Michael Comella Date: Wed, 11 Nov 2020 13:42:48 -0800 Subject: [PATCH] For #16376: test view hierarchy depth in StartupExcessive*Test. --- .../ui/StartupExcessiveResourceUseTest.kt | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/app/src/androidTest/java/org/mozilla/fenix/ui/StartupExcessiveResourceUseTest.kt b/app/src/androidTest/java/org/mozilla/fenix/ui/StartupExcessiveResourceUseTest.kt index b0547e69d..82b4be6cf 100644 --- a/app/src/androidTest/java/org/mozilla/fenix/ui/StartupExcessiveResourceUseTest.kt +++ b/app/src/androidTest/java/org/mozilla/fenix/ui/StartupExcessiveResourceUseTest.kt @@ -4,8 +4,13 @@ package org.mozilla.fenix.ui +import android.util.Log +import android.view.View +import android.view.ViewGroup +import androidx.core.view.children import androidx.test.platform.app.InstrumentationRegistry import androidx.test.uiautomator.UiDevice +import kotlinx.android.synthetic.main.activity_home.* import org.junit.Assert.assertEquals import org.junit.Rule import org.junit.Test @@ -18,6 +23,7 @@ import org.mozilla.fenix.perf.ComponentInitCount private const val EXPECTED_SUPPRESSION_COUNT = 11 private const val EXPECTED_RUNBLOCKING_COUNT = 2 private const val EXPECTED_COMPONENT_INIT_COUNT = 42 +private const val EXPECTED_VIEW_HIERARCHY_DEPTH = 12 private val failureMsgStrictMode = getErrorMessage( shortName = "StrictMode suppression", @@ -34,6 +40,12 @@ private val failureMsgComponentInit = getErrorMessage( implications = "initializing new components on start up may be an indication that we're doing more work than necessary on start up?" ) +private val failureMsgViewHierarchyDepth = getErrorMessage( + shortName = "view hierarchy depth", + implications = "having a deep view hierarchy can slow down measure/layout performance?" +) + "Please note that we're not sure if this is a useful metric to assert: with your feedback, " + + "we'll find out over time if it is or is not." + /** * A performance test to limit the number of StrictMode suppressions and number of runBlocking used * on startup. @@ -67,10 +79,25 @@ class StartupExcessiveResourceUseTest { val actualSuppresionCount = activityTestRule.activity.components.strictMode.suppressionCount.get().toInt() val actualRunBlocking = RunBlockingCounter.count.get() val actualComponentInitCount = ComponentInitCount.count.get() + val actualViewHierarchyDepth = countAndLogViewHierarchyDepth(activityTestRule.activity.rootContainer, 1) assertEquals(failureMsgStrictMode, EXPECTED_SUPPRESSION_COUNT, actualSuppresionCount) assertEquals(failureMsgRunBlocking, EXPECTED_RUNBLOCKING_COUNT, actualRunBlocking) assertEquals(failureMsgComponentInit, EXPECTED_COMPONENT_INIT_COUNT, actualComponentInitCount) + assertEquals(failureMsgViewHierarchyDepth, EXPECTED_VIEW_HIERARCHY_DEPTH, actualViewHierarchyDepth) + } +} + +private fun countAndLogViewHierarchyDepth(view: View, level: Int): Int { + // Log for debugging purposes: not sure if this is actually helpful. + val indent = "| ".repeat(level - 1) + Log.d("Startup...Test", "${indent}$view") + + return if (view !is ViewGroup) { + level + } else { + val maxDepth = view.children.map { countAndLogViewHierarchyDepth(it, level + 1) }.maxOrNull() + maxDepth ?: level } }