Skip to content

Commit 0c542b4

Browse files
author
Murat Yener
authored
Merge pull request #827 from android/power-metric-benchmark
Adds PowerMetric benchmark to measure Light vs Dark theme
2 parents 56389cb + e18f7cb commit 0c542b4

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed

benchmarks/src/main/java/com/google/samples/apps/nowinandroid/foryou/ForYouActions.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,17 @@ fun MacrobenchmarkScope.forYouScrollFeedDownUp() {
8888
val feedList = device.findObject(By.res("forYou:feed"))
8989
device.flingElementDownUp(feedList)
9090
}
91+
92+
fun MacrobenchmarkScope.setAppTheme(isDark: Boolean) {
93+
when (isDark) {
94+
true -> device.findObject(By.text("Dark")).click()
95+
false -> device.findObject(By.text("Light")).click()
96+
}
97+
device.waitForIdle()
98+
device.findObject(By.text("OK")).click()
99+
100+
// Wait until the top app bar is visible on screen
101+
device.wait(Until.hasObject(By.res("niaTopAppBar")), 2_000)
102+
val topAppBar = device.findObject(By.res("niaTopAppBar"))
103+
topAppBar.wait(Until.hasObject(By.text("Now in Android")), 2_000)
104+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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

Comments
 (0)