blob: d13868bd78c28b5c819ef3d73f4753448454d0bd [file] [log] [blame]
Rahul Ravikumard651ee22021-09-21 17:22:48 -07001/*
2 * Copyright 2021 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 * http://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
17package androidx.benchmark.macro.junit4
18
19import android.Manifest
20import androidx.annotation.RequiresApi
Rahul Ravikumar6da9b7f272021-10-20 13:52:42 -070021import androidx.benchmark.macro.ExperimentalBaselineProfilesApi
Rahul Ravikumard651ee22021-09-21 17:22:48 -070022import androidx.benchmark.macro.MacrobenchmarkScope
23import androidx.benchmark.macro.collectBaselineProfile
24import androidx.test.rule.GrantPermissionRule
25import org.junit.rules.RuleChain
26import org.junit.rules.TestRule
27import org.junit.runner.Description
28import org.junit.runners.model.Statement
29
30/**
Rahul Ravikumar6da9b7f272021-10-20 13:52:42 -070031 * A [TestRule] that collects Baseline Profiles to be embedded in your APK.
Rahul Ravikumard651ee22021-09-21 17:22:48 -070032 *
Rahul Ravikumar6da9b7f272021-10-20 13:52:42 -070033 * These rules are used at install time to partially pre-compile your application code.
Rahul Ravikumard651ee22021-09-21 17:22:48 -070034 */
35@RequiresApi(28)
Rahul Ravikumar6da9b7f272021-10-20 13:52:42 -070036@ExperimentalBaselineProfilesApi
Rahul Ravikumard651ee22021-09-21 17:22:48 -070037class BaselineProfileRule : TestRule {
38 private lateinit var currentDescription: Description
39
40 override fun apply(base: Statement, description: Description): Statement {
41 return RuleChain
42 .outerRule(GrantPermissionRule.grant(Manifest.permission.WRITE_EXTERNAL_STORAGE))
43 .around(::applyInternal)
44 .apply(base, description)
45 }
46
47 private fun applyInternal(base: Statement, description: Description) = object : Statement() {
48 override fun evaluate() {
49 currentDescription = description
50 base.evaluate()
51 }
52 }
53
Rahul Ravikumar6da9b7f272021-10-20 13:52:42 -070054 /**
55 * Collects baseline profiles for a critical user journey.
56 * @param packageName Package name of the app for which profiles are to be generated.
Rahul Ravikumar6da9b7f272021-10-20 13:52:42 -070057 * @param [profileBlock] defines the critical user journey.
58 */
Rahul Ravikumard651ee22021-09-21 17:22:48 -070059 public fun collectBaselineProfile(
60 packageName: String,
Rahul Ravikumard651ee22021-09-21 17:22:48 -070061 profileBlock: MacrobenchmarkScope.() -> Unit
62 ) {
63 collectBaselineProfile(
64 currentDescription.toUniqueName(),
65 packageName = packageName,
Rahul Ravikumard651ee22021-09-21 17:22:48 -070066 profileBlock = profileBlock
67 )
68 }
69
70 private fun Description.toUniqueName() = testClass.simpleName + "_" + methodName
71}