Skip to content

Commit 7dc0336

Browse files
author
Murat Yener
committed
Ads PowerMetric benchmark which scrolls through the topics list in Light vs Dark theme
1 parent 4d65946 commit 7dc0336

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed

benchmarks/src/main/java/com/google/samples/apps/nowinandroid/interests/InterestsActions.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,17 @@ fun MacrobenchmarkScope.interestsToggleBookmarked() {
4848
checkable.click()
4949
device.waitForIdle()
5050
}
51+
52+
fun MacrobenchmarkScope.setAppTheme(isDark: Boolean) {
53+
when (isDark){
54+
true -> device.findObject(By.text("Dark")).click()
55+
false -> device.findObject(By.text("Light")).click()
56+
}
57+
device.waitForIdle()
58+
device.findObject(By.text("OK")).click()
59+
60+
// Wait until interests are shown on screen
61+
device.wait(Until.hasObject(By.res("niaTopAppBar")), 2_000)
62+
val topAppBar = device.findObject(By.res("niaTopAppBar"))
63+
topAppBar.wait(Until.hasObject(By.text("Now in Android")), 2_000)
64+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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 org.junit.Rule
37+
import org.junit.Test
38+
import org.junit.runner.RunWith
39+
40+
@OptIn(ExperimentalMetricApi::class)
41+
@RequiresApi(VERSION_CODES.Q)
42+
@RunWith(AndroidJUnit4::class)
43+
class ScrollTopicListPowerMetricsBenchmark {
44+
@get:Rule
45+
val benchmarkRule = MacrobenchmarkRule()
46+
47+
private val categories = PowerCategory.values()
48+
.associateWith { PowerCategoryDisplayLevel.TOTAL }
49+
50+
@Test
51+
fun benchmarkStateChangeCompilationLight() =
52+
benchmarkStateChangeWithTheme(CompilationMode.Partial(), false)
53+
54+
@Test
55+
fun benchmarkStateChangeCompilationDark() =
56+
benchmarkStateChangeWithTheme(CompilationMode.Partial(), true)
57+
58+
private fun benchmarkStateChangeWithTheme(compilationMode: CompilationMode, isDark: Boolean) =
59+
benchmarkRule.measureRepeated(
60+
packageName = PACKAGE_NAME,
61+
metrics = listOf(FrameTimingMetric(), PowerMetric(PowerMetric.Energy(categories))),
62+
compilationMode = compilationMode,
63+
iterations = 2,
64+
startupMode = StartupMode.WARM,
65+
setupBlock = {
66+
// Start the app
67+
pressHome()
68+
startActivityAndWait()
69+
allowNotifications()
70+
// Navigate to interests screen
71+
device.findObject(By.desc("Settings")).click()
72+
device.waitForIdle()
73+
setAppTheme(isDark)
74+
},
75+
) {
76+
forYouWaitForContent()
77+
forYouSelectTopics()
78+
repeat(3) {
79+
forYouScrollFeedDownUp()
80+
}
81+
}
82+
}

0 commit comments

Comments
 (0)