透過 Macrobenchmark 控制應用程式 (Views)

概念和 Jetpack Compose 實作

與大多數 Android UI 測試不同,Macrobenchmark 測試會在應用程式本身以外的程序中執行。對於停止應用程式處理程序及從 DEX 位元碼編譯至機器碼等操作,上述作法是必要的。

您可以使用 UIAutomator 程式庫,或透過測試程序控制目標應用程式,驅動應用程式狀態。不過無法將 EspressoActivityScenario 用於 Macrobenchmark,因為這類方法需搭配應用程式在共用程序中執行。

以下示範使用資源 ID 尋找 RecyclerView,然後向下捲動多次:

Kotlin

@Test
fun scrollList() {
    benchmarkRule.measureRepeated(
        // ...
        setupBlock = {
            uiAutomator {
                // Before starting to measure, navigate to the UI to be measured
                startIntent(Intent("$packageName.RECYCLER_VIEW_ACTIVITY"))
            }
        }
    ) {
        uiAutomator {
            val recycler = onElement { className == "androidx.recyclerview.widget.RecyclerView" }
            // Scroll down several times
            repeat(3) { recycler.fling(Direction.DOWN) }
        }

    }
}

Java

@Test
public void scrollList() {
    benchmarkRule.measureRepeated(
        // ...
        /* setupBlock */ scope -> {
            // Before measuring, navigate to the UI to be measured.
            val intent = Intent("$packageName.RECYCLER_VIEW_ACTIVITY")
            scope.startActivityAndWait();
            return Unit.INSTANCE;
        },
        /* measureBlock */ scope -> {
            UiDevice device = scope.getDevice();
            UiObject2 recycler = device.findObject(By.res(scope.getPackageName(), "recycler"));

            // Set gesture margin to avoid triggering gesture navigation
            // with input events from automation.
            recycler.setGestureMargin(device.getDisplayWidth() / 5);

            // Fling the recycler several times.
            for (int i = 0; i < 3; i++) {
                recycler.fling(Direction.DOWN);
            }

            return Unit.INSTANCE;
        }
    );
}

效能評定不一定要捲動 UI。舉例來說,可以改為執行動畫。這也不需要特別使用 UI Automator,只要畫面是由系統產生,系統就會收集成效指標。

有時候,您會希望能夠針對應用程式的某些部分執行基準測試,但這些部分卻無法直接從外部存取。這種情形可能包括存取內部活動 (以 exported=false 標示)、前往 Fragment,或滑動隱藏部分 UI 等。您需要和使用者一樣,以手動方式前往應用程式的這些部分,才能執行基準測試。

這時只要變更 setupBlock{} 內的程式碼,使其包含所要的效果 (例如:輕觸按鈕、滑動等),就可以達到目的。measureBlock{} 中只會包含您實際要執行基準測試的 UI 操作:

Kotlin

@Test
fun nonExportedActivityScrollList() {
    benchmarkRule.measureRepeated(
        // ...
        setupBlock = setupBenchmark()
    ) {
        // ...
    }
}

private fun setupBenchmark(): MacrobenchmarkScope.() -> Unit = {
    uiAutomator {
        // Before starting to measure, navigate to the UI to be measured
        startApp(TARGET_PACKAGE)
        // click a button to launch the target activity.
        onElement { textAsString() == "RecyclerView" }.click()
        // wait until the activity is shown
        waitForStableInActiveWindow()
    }
}

Java

@Test
public void scrollList() {
    benchmarkRule.measureRepeated(
        // ...
        /* setupBlock */ scope -> {
            // Before measuring, navigate to the default activity.
            scope.startActivityAndWait();

            // Click a button to launch the target activity.
            // While you use resourceId here to find the button, you can also
            // use accessibility info or button text content.
            UiObject2 launchRecyclerActivity = scope.getDevice().findObject(
                By.res(packageName, "launchRecyclerActivity")
            )
            launchRecyclerActivity.click();

            // Wait until activity is shown.
            scope.getDevice().wait(
                Until.hasObject(By.clazz("$packageName.NonExportedRecyclerActivity")),
                10000L
            )

            return Unit.INSTANCE;
        },
        /* measureBlock */ scope -> {
            // ...
        }
    );
}