Skip to content

Commit

Permalink
improvement: Longer walk-though for baseline profile generation
Browse files Browse the repository at this point in the history
  • Loading branch information
AChep committed Dec 13, 2024
1 parent 1213e5e commit 35bcd85
Show file tree
Hide file tree
Showing 8 changed files with 163 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,21 @@ package com.artemchep.macrobenchmark.baselineprofile

import android.os.Build
import androidx.annotation.RequiresApi
import androidx.benchmark.macro.MacrobenchmarkScope
import androidx.benchmark.macro.junit4.BaselineProfileRule
import com.artemchep.macrobenchmark.PACKAGE_NAME
import com.artemchep.test.feature.coreFeature
import com.artemchep.test.feature.ensureGeneratorScreen
import com.artemchep.test.feature.ensureMainScreen
import com.artemchep.test.feature.ensureSendsScreen
import com.artemchep.test.feature.ensureSettingsScreen
import com.artemchep.test.feature.ensureWatchtowerScreen
import com.artemchep.test.feature.generatorFeature
import com.artemchep.test.feature.sendsFeature
import com.artemchep.test.feature.settingsFeature
import com.artemchep.test.feature.watchtowerFeature
import com.artemchep.test.util.ScreenRecorderTestWatcher
import com.artemchep.test.util.wait
import org.junit.Rule
import org.junit.Test

Expand All @@ -23,9 +33,8 @@ class BaselineProfileGenerator {
val baselineProfileRule = BaselineProfileRule()

@Test
fun generate() = baselineProfileRule.collect(
fun generateBaselineProfile() = baselineProfileRule.collect(
packageName = PACKAGE_NAME,
includeInStartupProfile = true,
) {
// This block defines the app's critical user journey. Here we are interested in
// optimizing for app startup. But you can also navigate and scroll
Expand All @@ -35,5 +44,43 @@ class BaselineProfileGenerator {
startActivityAndWait()

device.coreFeature.ensureMainScreen()
device.waitForIdle()

walkThroughSends()
walkThroughGenerator()
walkThroughWatchtower()
walkThroughSettings()
}

private fun MacrobenchmarkScope.walkThroughSends() {
device.sendsFeature.ensureSendsScreen()
device.wait(2_000L)
}

private fun MacrobenchmarkScope.walkThroughGenerator() {
device.generatorFeature.ensureGeneratorScreen()
device.wait(2_000L)
}

private fun MacrobenchmarkScope.walkThroughWatchtower() {
device.watchtowerFeature.ensureWatchtowerScreen()
device.wait(5_000L)
}

private fun MacrobenchmarkScope.walkThroughSettings() {
device.settingsFeature.ensureSettingsScreen()
device.wait(2_000L)
}

@Test
fun generateStartupProfile() = baselineProfileRule.collect(
packageName = PACKAGE_NAME,
includeInStartupProfile = true,
) {
pressHome()
startActivityAndWait()

device.coreFeature.ensureMainScreen()
device.waitForIdle()
}
}
15 changes: 15 additions & 0 deletions androidTest/src/main/java/com/artemchep/test/feature/generator.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.artemchep.test.feature

import androidx.test.uiautomator.UiDevice

@JvmInline
value class FeatureGenerator(
val device: UiDevice,
)

val UiDevice.generatorFeature get() = FeatureGenerator(this)

fun FeatureGenerator.ensureGeneratorScreen() =
device.coreFeature.launchScreen(
actionButtonResource = "nav_bar:generator",
)
23 changes: 22 additions & 1 deletion androidTest/src/main/java/com/artemchep/test/feature/screen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,15 @@ fun FeatureCore.ensureMainScreen(): UiObject2 = kotlin.run {
password.text = "111111"
val btn = screen
.findObject(By.res("btn:go"))
btn.wait(Until.clickable(true), 3000L)
btn.wait(Until.enabled(true), 10_000L)
btn.wait(Until.clickable(true), 10_000L)
require(
value = btn.isEnabled &&
btn.isClickable,
) {
"Unlock / Create vault button is not yet " +
"active!"
}
btn.click()

// Wait till main screen is loaded
Expand Down Expand Up @@ -102,3 +110,16 @@ fun FeatureCore.launchDefaultActivityAndWait(
10_000,
)
}

fun FeatureCore.launchScreen(
actionButtonResource: String,
) {
val actionButtonSelector = By.res(actionButtonResource)
val actionButton = device.wait(
Until.findObject(actionButtonSelector),
5_000L,
)

actionButton.click()
device.waitForIdle()
}
15 changes: 15 additions & 0 deletions androidTest/src/main/java/com/artemchep/test/feature/sends.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.artemchep.test.feature

import androidx.test.uiautomator.UiDevice

@JvmInline
value class FeatureSends(
val device: UiDevice,
)

val UiDevice.sendsFeature get() = FeatureSends(this)

fun FeatureSends.ensureSendsScreen() =
device.coreFeature.launchScreen(
actionButtonResource = "nav_bar:sends",
)
15 changes: 15 additions & 0 deletions androidTest/src/main/java/com/artemchep/test/feature/settings.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.artemchep.test.feature

import androidx.test.uiautomator.UiDevice

@JvmInline
value class FeatureSettings(
val device: UiDevice,
)

val UiDevice.settingsFeature get() = FeatureSettings(this)

fun FeatureSettings.ensureSettingsScreen() =
device.coreFeature.launchScreen(
actionButtonResource = "nav_bar:settings",
)
15 changes: 15 additions & 0 deletions androidTest/src/main/java/com/artemchep/test/feature/watchtower.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.artemchep.test.feature

import androidx.test.uiautomator.UiDevice

@JvmInline
value class FeatureWatchtower(
val device: UiDevice,
)

val UiDevice.watchtowerFeature get() = FeatureWatchtower(this)

fun FeatureWatchtower.ensureWatchtowerScreen() =
device.coreFeature.launchScreen(
actionButtonResource = "nav_bar:watchtower",
)
11 changes: 11 additions & 0 deletions androidTest/src/main/java/com/artemchep/test/util/wait.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.artemchep.test.util

import androidx.test.uiautomator.UiDevice

/** Always waits for a given time */
fun UiDevice.wait(ms: Long) {
wait(
{ null },
ms,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.layout.onSizeChanged
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.coerceAtLeast
import androidx.compose.ui.unit.dp
Expand Down Expand Up @@ -134,8 +135,15 @@ import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.flow.map
import org.kodein.di.compose.rememberInstance

const val HOME_VAULT_TEST_TAG = "nav_bar:vault"
const val HOME_SENDS_TEST_TAG = "nav_bar:sends"
const val HOME_GENERATOR_TEST_TAG = "nav_bar:generator"
const val HOME_WATCHTOWER_TEST_TAG = "nav_bar:watchtower"
const val HOME_SETTINGS_TEST_TAG = "nav_bar:settings"

data class Rail(
val key: String,
val testTag: String,
val route: Route,
val icon: ImageVector,
val iconSelected: ImageVector,
Expand Down Expand Up @@ -192,27 +200,31 @@ fun HomeScreen(
persistentListOf(
Rail(
key = "vault",
testTag = HOME_VAULT_TEST_TAG,
route = vaultRoute,
icon = Icons.Outlined.Home,
iconSelected = Icons.Filled.Home,
label = TextHolder.Res(Res.string.home_vault_label),
),
Rail(
key = "sends",
testTag = HOME_SENDS_TEST_TAG,
route = sendsRoute,
icon = Icons.AutoMirrored.Outlined.Send,
iconSelected = Icons.AutoMirrored.Filled.Send,
label = TextHolder.Res(Res.string.home_send_label),
),
Rail(
key = "generator",
testTag = HOME_GENERATOR_TEST_TAG,
route = generatorRoute,
icon = Icons.Outlined.Password,
iconSelected = Icons.Filled.Password,
label = TextHolder.Res(Res.string.home_generator_label),
),
Rail(
key = "watchtower",
testTag = HOME_WATCHTOWER_TEST_TAG,
route = watchtowerRoute,
icon = Icons.Outlined.Security,
iconSelected = Icons.Filled.Security,
Expand All @@ -224,6 +236,7 @@ fun HomeScreen(
),
Rail(
key = "settings",
testTag = HOME_SETTINGS_TEST_TAG,
route = settingsRoute,
icon = Icons.Outlined.Settings,
iconSelected = Icons.Filled.Settings,
Expand Down Expand Up @@ -292,6 +305,8 @@ fun HomeScreenContent(
key(r.key) {
val counterState = r.counterFlow.collectAsState(null)
RailNavigationControllerItem(
modifier = Modifier
.testTag(r.testTag),
backStack = backStack,
route = r.route,
icon = r.icon,
Expand Down Expand Up @@ -433,6 +448,8 @@ fun HomeScreenContent(
key(r.key) {
val counterState = r.counterFlow.collectAsState(null)
BottomNavigationControllerItem(
modifier = Modifier
.testTag(r.testTag),
backStack = backStack,
route = r.route,
icon = r.icon,
Expand Down Expand Up @@ -770,6 +787,7 @@ private fun RailStatusBadgeContent(

@Composable
private fun ColumnScope.RailNavigationControllerItem(
modifier: Modifier = Modifier,
backStack: ImmutableList<NavigationEntry>,
route: Route,
icon: ImageVector,
Expand All @@ -780,6 +798,7 @@ private fun ColumnScope.RailNavigationControllerItem(
val controller = LocalNavigationController.current
val selected = isSelected(backStack, route)
NavigationRailItem(
modifier = modifier,
icon = {
NavigationIcon(
selected = selected,
Expand All @@ -799,6 +818,7 @@ private fun ColumnScope.RailNavigationControllerItem(

@Composable
private fun RowScope.BottomNavigationControllerItem(
modifier: Modifier = Modifier,
backStack: ImmutableList<NavigationEntry>,
route: Route,
icon: ImageVector,
Expand All @@ -809,6 +829,7 @@ private fun RowScope.BottomNavigationControllerItem(
val controller = LocalNavigationController.current
val selected = isSelected(backStack, route)
NavigationBarItem(
modifier = modifier,
icon = {
NavigationIcon(
selected = selected,
Expand Down

0 comments on commit 35bcd85

Please sign in to comment.