|
| 1 | +/* |
| 2 | + * Copyright 2023 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 | + * https://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.google.samples.apps.nowinandroid.interests |
| 18 | + |
| 19 | +import android.os.Build.VERSION_CODES |
| 20 | +import androidx.annotation.RequiresApi |
| 21 | +import androidx.benchmark.macro.CompilationMode |
| 22 | +import androidx.benchmark.macro.ExperimentalMetricApi |
| 23 | +import androidx.benchmark.macro.FrameTimingMetric |
| 24 | +import androidx.benchmark.macro.PowerCategory |
| 25 | +import androidx.benchmark.macro.PowerCategoryDisplayLevel |
| 26 | +import androidx.benchmark.macro.PowerMetric |
| 27 | +import androidx.benchmark.macro.StartupMode |
| 28 | +import androidx.benchmark.macro.junit4.MacrobenchmarkRule |
| 29 | +import androidx.test.ext.junit.runners.AndroidJUnit4 |
| 30 | +import androidx.test.uiautomator.By |
| 31 | +import com.google.samples.apps.nowinandroid.PACKAGE_NAME |
| 32 | +import com.google.samples.apps.nowinandroid.allowNotifications |
| 33 | +import com.google.samples.apps.nowinandroid.foryou.forYouScrollFeedDownUp |
| 34 | +import com.google.samples.apps.nowinandroid.foryou.forYouSelectTopics |
| 35 | +import com.google.samples.apps.nowinandroid.foryou.forYouWaitForContent |
| 36 | +import com.google.samples.apps.nowinandroid.foryou.setAppTheme |
| 37 | +import org.junit.Rule |
| 38 | +import org.junit.Test |
| 39 | +import org.junit.runner.RunWith |
| 40 | + |
| 41 | +@OptIn(ExperimentalMetricApi::class) |
| 42 | +@RequiresApi(VERSION_CODES.Q) |
| 43 | +@RunWith(AndroidJUnit4::class) |
| 44 | +class ScrollTopicListPowerMetricsBenchmark { |
| 45 | + @get:Rule |
| 46 | + val benchmarkRule = MacrobenchmarkRule() |
| 47 | + |
| 48 | + private val categories = PowerCategory.values() |
| 49 | + .associateWith { PowerCategoryDisplayLevel.TOTAL } |
| 50 | + |
| 51 | + @Test |
| 52 | + fun benchmarkStateChangeCompilationLight() = |
| 53 | + benchmarkStateChangeWithTheme(CompilationMode.Partial(), false) |
| 54 | + |
| 55 | + @Test |
| 56 | + fun benchmarkStateChangeCompilationDark() = |
| 57 | + benchmarkStateChangeWithTheme(CompilationMode.Partial(), true) |
| 58 | + |
| 59 | + private fun benchmarkStateChangeWithTheme(compilationMode: CompilationMode, isDark: Boolean) = |
| 60 | + benchmarkRule.measureRepeated( |
| 61 | + packageName = PACKAGE_NAME, |
| 62 | + metrics = listOf(FrameTimingMetric(), PowerMetric(PowerMetric.Energy(categories))), |
| 63 | + compilationMode = compilationMode, |
| 64 | + iterations = 2, |
| 65 | + startupMode = StartupMode.WARM, |
| 66 | + setupBlock = { |
| 67 | + // Start the app |
| 68 | + pressHome() |
| 69 | + startActivityAndWait() |
| 70 | + allowNotifications() |
| 71 | + // Navigate to Settings |
| 72 | + device.findObject(By.desc("Settings")).click() |
| 73 | + device.waitForIdle() |
| 74 | + setAppTheme(isDark) |
| 75 | + }, |
| 76 | + ) { |
| 77 | + forYouWaitForContent() |
| 78 | + forYouSelectTopics() |
| 79 | + repeat(3) { |
| 80 | + forYouScrollFeedDownUp() |
| 81 | + } |
| 82 | + } |
| 83 | +} |
0 commit comments