|
| 1 | +package kiwi.orbit.baselineprofile |
| 2 | + |
| 3 | +import androidx.benchmark.macro.MacrobenchmarkScope |
| 4 | +import androidx.benchmark.macro.junit4.BaselineProfileRule |
| 5 | +import androidx.test.filters.LargeTest |
| 6 | +import androidx.test.uiautomator.By |
| 7 | +import androidx.test.uiautomator.UiScrollable |
| 8 | +import androidx.test.uiautomator.UiSelector |
| 9 | +import androidx.test.uiautomator.Until |
| 10 | +import kiwi.orbit.compose.catalog.semantics.AlertScreenSemantics |
| 11 | +import kiwi.orbit.compose.catalog.semantics.ButtonScreenSemantics |
| 12 | +import kiwi.orbit.compose.catalog.semantics.DialogScreenSemantics |
| 13 | +import kiwi.orbit.compose.catalog.semantics.MainScreenSemantics |
| 14 | +import kiwi.orbit.compose.catalog.semantics.PillButtonScreenSemantics |
| 15 | +import kiwi.orbit.compose.catalog.semantics.SelectFieldScreenSemantics |
| 16 | +import kiwi.orbit.compose.catalog.semantics.SubScreenSemantics |
| 17 | +import kiwi.orbit.compose.catalog.semantics.ToastScreenSemantics |
| 18 | +import kiwi.orbit.compose.catalog.semantics.TopAppBarScreenSemantics |
| 19 | +import org.junit.Rule |
| 20 | +import org.junit.Test |
| 21 | + |
| 22 | +/** |
| 23 | + * This test class generates a basic startup baseline profile for the target package. |
| 24 | + * |
| 25 | + * We recommend you start with this but add important user flows to the profile to improve their performance. |
| 26 | + * Refer to the [baseline profile documentation](https://d.android.com/topic/performance/baselineprofiles) |
| 27 | + * for more information. |
| 28 | + * |
| 29 | + * You can run the generator with the Generate Baseline Profile run configuration, |
| 30 | + * or directly with `generateBaselineProfile` Gradle task: |
| 31 | + * ``` |
| 32 | + * ./gradlew :catalog:generateReleaseBaselineProfile -Pandroid.testInstrumentationRunnerArguments.androidx.benchmark.enabledRules=BaselineProfile |
| 33 | + * ``` |
| 34 | + * The run configuration runs the Gradle task and applies filtering to run only the generators. |
| 35 | + * |
| 36 | + * Check [documentation](https://d.android.com/topic/performance/benchmarking/macrobenchmark-instrumentation-args) |
| 37 | + * for more information about available instrumentation arguments. |
| 38 | + * |
| 39 | + * After you run the generator, you can verify the improvements running the [StartupBenchmarks] benchmark. |
| 40 | + **/ |
| 41 | +@LargeTest |
| 42 | +internal class BaselineProfileGenerator { |
| 43 | + |
| 44 | + private companion object { |
| 45 | + const val TIMEOUT = 5000L |
| 46 | + } |
| 47 | + |
| 48 | + @get:Rule |
| 49 | + val rule = BaselineProfileRule() |
| 50 | + |
| 51 | + @Test |
| 52 | + fun generate() { |
| 53 | + rule.collect( |
| 54 | + packageName = "kiwi.orbit.compose.catalog", |
| 55 | + maxIterations = 5, |
| 56 | + stableIterations = 2, |
| 57 | + ) { |
| 58 | + pressHome() |
| 59 | + startActivityAndWait() |
| 60 | + |
| 61 | + profileSubScreen(MainScreenSemantics.ColorsItemTag) |
| 62 | + profileSubScreen(MainScreenSemantics.IconsItemTag) |
| 63 | + profileSubScreen(MainScreenSemantics.IllustrationsItemTag) |
| 64 | + profileSubScreen(MainScreenSemantics.TypographyItemTag) |
| 65 | + |
| 66 | + profileSubScreen(MainScreenSemantics.AlertItemTag) { |
| 67 | + device.findObject(By.res(AlertScreenSemantics.NormalTabTag)).click() |
| 68 | + device.findObject(By.res(AlertScreenSemantics.SuppressedTabTag)).click() |
| 69 | + device.findObject(By.res(AlertScreenSemantics.InlineTabTag)).click() |
| 70 | + } |
| 71 | + profileSubScreen(MainScreenSemantics.BadgeItemTag) |
| 72 | + profileSubScreen(MainScreenSemantics.BadgeListItemTag) |
| 73 | + profileSubScreen(MainScreenSemantics.ButtonItemTag) { |
| 74 | + device.findObject(By.res(ButtonScreenSemantics.ButtonTabTag)).click() |
| 75 | + device.findObject(By.res(ButtonScreenSemantics.ButtonLinkTabTag)).click() |
| 76 | + } |
| 77 | + profileSubScreen(MainScreenSemantics.CardItemTag) |
| 78 | + profileSubScreen(MainScreenSemantics.CheckboxItemTag) |
| 79 | + profileSubScreen(MainScreenSemantics.ChoiceTileItemTag) |
| 80 | + profileSubScreen(MainScreenSemantics.CollapseItemTag) |
| 81 | + profileSubScreen(MainScreenSemantics.DialogItemTag) { |
| 82 | + device.findObject(By.res(DialogScreenSemantics.OrbitDialogButtonTag)) |
| 83 | + .clickAndWait(Until.newWindow(), TIMEOUT) |
| 84 | + device.pressBack() |
| 85 | + device.findObject(By.res(DialogScreenSemantics.M3DialogButtonTag)) |
| 86 | + .clickAndWait(Until.newWindow(), TIMEOUT) |
| 87 | + device.pressBack() |
| 88 | + device.findObject(By.res(DialogScreenSemantics.M3TimePickerButtonTag)) |
| 89 | + .clickAndWait(Until.newWindow(), TIMEOUT) |
| 90 | + device.pressBack() |
| 91 | + device.findObject(By.res(DialogScreenSemantics.M3DatePickerButtonTag)) |
| 92 | + .clickAndWait(Until.newWindow(), TIMEOUT) |
| 93 | + device.pressBack() |
| 94 | + } |
| 95 | + profileSubScreen(MainScreenSemantics.EmptyStateItemTag) |
| 96 | + profileSubScreen(MainScreenSemantics.KeyValueItemTag) |
| 97 | + profileSubScreen(MainScreenSemantics.ListItemTag) |
| 98 | + profileSubScreen(MainScreenSemantics.ListChoiceItemTag) |
| 99 | + profileSubScreen(MainScreenSemantics.LoadingItemTag) |
| 100 | + profileSubScreen(MainScreenSemantics.PillButtonItemTag) { |
| 101 | + device.findObject(By.res(PillButtonScreenSemantics.ShowWithIconButtonTag)).click() |
| 102 | + } |
| 103 | + profileSubScreen(MainScreenSemantics.ProgressIndicatorItemTag) |
| 104 | + profileSubScreen(MainScreenSemantics.RadioItemTag) |
| 105 | + profileSubScreen(MainScreenSemantics.SeatItemTag) |
| 106 | + profileSubScreen(MainScreenSemantics.SegmentedSwitchItemTag) |
| 107 | + profileSubScreen(MainScreenSemantics.SelectFieldItemTag) { |
| 108 | + device.findObject(By.res(SelectFieldScreenSemantics.CountrySelectFieldTag)).click() // open |
| 109 | + device.findObject(By.res(SelectFieldScreenSemantics.CountrySelectFieldTag)).click() // close |
| 110 | + Thread.sleep(1000L) // back navigation is blocked until the popup is fully closed |
| 111 | + } |
| 112 | + profileSubScreen(MainScreenSemantics.SliderItemTag) |
| 113 | + profileSubScreen(MainScreenSemantics.StepperItemTag) |
| 114 | + profileSubScreen(MainScreenSemantics.SurfaceCardItemTag) |
| 115 | + profileSubScreen(MainScreenSemantics.SwitchItemTag) |
| 116 | + profileSubScreen(MainScreenSemantics.TabsItemTag) |
| 117 | + profileSubScreen(MainScreenSemantics.TagItemTag) |
| 118 | + profileSubScreen(MainScreenSemantics.TextFieldItemTag) |
| 119 | + profileSubScreen(MainScreenSemantics.TileItemTag) |
| 120 | + profileSubScreen(MainScreenSemantics.TileGroupItemTag) |
| 121 | + profileSubScreen(MainScreenSemantics.TimelineItemTag) |
| 122 | + profileSubScreen(MainScreenSemantics.ToastItemTag) { |
| 123 | + device.findObject(By.res(ToastScreenSemantics.ToastSignedInButtonTag)).click() |
| 124 | + } |
| 125 | + profileSubScreen(MainScreenSemantics.TopAppBarItemTag) { |
| 126 | + device.findObject(By.res(TopAppBarScreenSemantics.NormalSimpleButtonTag)).click() |
| 127 | + device.wait(Until.hasObject(By.res(TopAppBarScreenSemantics.NormalSimpleScreenTag)), TIMEOUT) |
| 128 | + device.pressBack() |
| 129 | + device.wait(Until.hasObject(By.res(SubScreenSemantics.Tag)), TIMEOUT) |
| 130 | + device.findObject(By.res(TopAppBarScreenSemantics.LargeSimpleButtonTag)).click() |
| 131 | + device.wait(Until.hasObject(By.res(TopAppBarScreenSemantics.LargeSimpleScreenTag)), TIMEOUT) |
| 132 | + device.pressBack() |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + private fun MacrobenchmarkScope.profileSubScreen( |
| 138 | + mainScreenItemTag: String, |
| 139 | + profileContent: MacrobenchmarkScope.() -> Unit = {}, |
| 140 | + ) { |
| 141 | + val mainScreenScrollable = UiScrollable(UiSelector().scrollable(true)) |
| 142 | + mainScreenScrollable.scrollIntoView(UiSelector().resourceId(mainScreenItemTag)) |
| 143 | + device.wait(Until.hasObject(By.res(mainScreenItemTag)), TIMEOUT) |
| 144 | + |
| 145 | + device.findObject(By.res(mainScreenItemTag)).click() |
| 146 | + device.wait(Until.hasObject(By.res(SubScreenSemantics.Tag)), TIMEOUT) |
| 147 | + |
| 148 | + profileContent() |
| 149 | + |
| 150 | + device.pressBack() |
| 151 | + device.wait(Until.hasObject(By.res(MainScreenSemantics.Tag)), TIMEOUT) |
| 152 | + } |
| 153 | +} |
0 commit comments