Commit b0a1e088 authored by Michael Comella's avatar Michael Comella Committed by Michael Comella
Browse files

For #16376: test view hierarchy depth in StartupExcessive*Test.

parent 88233ef4
Loading
Loading
Loading
Loading
+27 −0
Original line number Original line Diff line number Diff line
@@ -4,8 +4,13 @@


package org.mozilla.fenix.ui
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.platform.app.InstrumentationRegistry
import androidx.test.uiautomator.UiDevice
import androidx.test.uiautomator.UiDevice
import kotlinx.android.synthetic.main.activity_home.*
import org.junit.Assert.assertEquals
import org.junit.Assert.assertEquals
import org.junit.Rule
import org.junit.Rule
import org.junit.Test
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_SUPPRESSION_COUNT = 11
private const val EXPECTED_RUNBLOCKING_COUNT = 2
private const val EXPECTED_RUNBLOCKING_COUNT = 2
private const val EXPECTED_COMPONENT_INIT_COUNT = 42
private const val EXPECTED_COMPONENT_INIT_COUNT = 42
private const val EXPECTED_VIEW_HIERARCHY_DEPTH = 12


private val failureMsgStrictMode = getErrorMessage(
private val failureMsgStrictMode = getErrorMessage(
    shortName = "StrictMode suppression",
    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?"
    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
 * A performance test to limit the number of StrictMode suppressions and number of runBlocking used
 * on startup.
 * on startup.
@@ -67,10 +79,25 @@ class StartupExcessiveResourceUseTest {
        val actualSuppresionCount = activityTestRule.activity.components.strictMode.suppressionCount.get().toInt()
        val actualSuppresionCount = activityTestRule.activity.components.strictMode.suppressionCount.get().toInt()
        val actualRunBlocking = RunBlockingCounter.count.get()
        val actualRunBlocking = RunBlockingCounter.count.get()
        val actualComponentInitCount = ComponentInitCount.count.get()
        val actualComponentInitCount = ComponentInitCount.count.get()
        val actualViewHierarchyDepth = countAndLogViewHierarchyDepth(activityTestRule.activity.rootContainer, 1)


        assertEquals(failureMsgStrictMode, EXPECTED_SUPPRESSION_COUNT, actualSuppresionCount)
        assertEquals(failureMsgStrictMode, EXPECTED_SUPPRESSION_COUNT, actualSuppresionCount)
        assertEquals(failureMsgRunBlocking, EXPECTED_RUNBLOCKING_COUNT, actualRunBlocking)
        assertEquals(failureMsgRunBlocking, EXPECTED_RUNBLOCKING_COUNT, actualRunBlocking)
        assertEquals(failureMsgComponentInit, EXPECTED_COMPONENT_INIT_COUNT, actualComponentInitCount)
        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
    }
    }
}
}