Skip to content

Commit 8213fff

Browse files
authored
Merge pull request #380 from android/ben/topics_recomposition
Allow InterestsItem to be skipped on recomposition
2 parents 0faa7b2 + 3a501f4 commit 8213fff

File tree

3 files changed

+77
-3
lines changed

3 files changed

+77
-3
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package com.google.samples.apps.nowinandroid.interests
1919
import androidx.benchmark.macro.MacrobenchmarkScope
2020
import androidx.test.uiautomator.By
2121
import androidx.test.uiautomator.Direction
22+
import androidx.test.uiautomator.Until
2223

2324
fun MacrobenchmarkScope.interestsScrollTopicsDownUp() {
2425
val topicsList = device.findObject(By.res("interests:topics"))
@@ -33,3 +34,14 @@ fun MacrobenchmarkScope.interestsScrollPeopleDownUp() {
3334
device.waitForIdle()
3435
peopleList.fling(Direction.UP)
3536
}
37+
38+
fun MacrobenchmarkScope.interestsWaitForTopics() {
39+
device.wait(Until.hasObject(By.text("Accessibility")), 30_000)
40+
}
41+
42+
fun MacrobenchmarkScope.interestsToggleBookmarked() {
43+
val topicsList = device.findObject(By.res("interests:topics"))
44+
val checkable = topicsList.findObject(By.checkable(true))
45+
checkable.click()
46+
device.waitForIdle()
47+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright 2022 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 androidx.benchmark.macro.CompilationMode
20+
import androidx.benchmark.macro.FrameTimingMetric
21+
import androidx.benchmark.macro.StartupMode
22+
import androidx.benchmark.macro.junit4.MacrobenchmarkRule
23+
import androidx.test.ext.junit.runners.AndroidJUnit4
24+
import androidx.test.uiautomator.By
25+
import com.google.samples.apps.nowinandroid.PACKAGE_NAME
26+
import org.junit.Rule
27+
import org.junit.Test
28+
import org.junit.runner.RunWith
29+
30+
@RunWith(AndroidJUnit4::class)
31+
class TopicsScreenRecompositionBenchmark {
32+
@get:Rule
33+
val benchmarkRule = MacrobenchmarkRule()
34+
35+
@Test
36+
fun benchmarkStateChangeCompilationBaselineProfile() =
37+
benchmarkStateChange(CompilationMode.Partial())
38+
39+
private fun benchmarkStateChange(compilationMode: CompilationMode) =
40+
benchmarkRule.measureRepeated(
41+
packageName = PACKAGE_NAME,
42+
metrics = listOf(FrameTimingMetric()),
43+
compilationMode = compilationMode,
44+
iterations = 10,
45+
startupMode = StartupMode.WARM,
46+
setupBlock = {
47+
// Start the app
48+
pressHome()
49+
startActivityAndWait()
50+
51+
// Navigate to interests screen
52+
device.findObject(By.text("Interests")).click()
53+
device.waitForIdle()
54+
}
55+
) {
56+
interestsWaitForTopics()
57+
repeat(3) {
58+
interestsToggleBookmarked()
59+
}
60+
}
61+
}

feature/interests/src/main/java/com/google/samples/apps/nowinandroid/feature/interests/TabContent.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,15 @@ fun TopicsTabContent(
4646
contentPadding = PaddingValues(top = 8.dp)
4747
) {
4848
topics.forEach { followableTopic ->
49-
item {
49+
val topicId = followableTopic.topic.id
50+
item(key = topicId) {
5051
InterestsItem(
5152
name = followableTopic.topic.name,
5253
following = followableTopic.isFollowed,
5354
description = followableTopic.topic.shortDescription,
5455
topicImageUrl = followableTopic.topic.imageUrl,
55-
onClick = { onTopicClick(followableTopic.topic.id) },
56-
onFollowButtonClick = { onFollowButtonClick(followableTopic.topic.id, it) }
56+
onClick = { onTopicClick(topicId) },
57+
onFollowButtonClick = { onFollowButtonClick(topicId, it) }
5758
)
5859
}
5960
}

0 commit comments

Comments
 (0)