Extract MacrobenchUtils.kt to a new testutils

Modified:
- compose/integration-tests
- benchmark/integration-tests

Test: Ran macrobench
Bug: b/187328685

Change-Id: Icd1db10f4e94850233e9452636746c4392871ee2
diff --git a/benchmark/integration-tests/macrobenchmark/build.gradle b/benchmark/integration-tests/macrobenchmark/build.gradle
index 7ce9cba..3067b8a 100644
--- a/benchmark/integration-tests/macrobenchmark/build.gradle
+++ b/benchmark/integration-tests/macrobenchmark/build.gradle
@@ -37,6 +37,7 @@
 dependencies {
     androidTestImplementation(project(":benchmark:benchmark-junit4"))
     androidTestImplementation(project(":benchmark:benchmark-macro-junit4"))
+    androidTestImplementation(project(":internal-testutils-macrobenchmark"))
     androidTestImplementation(ANDROIDX_TEST_RULES)
     androidTestImplementation(ANDROIDX_TEST_EXT_JUNIT)
     androidTestImplementation(ANDROIDX_TEST_CORE)
diff --git a/benchmark/integration-tests/macrobenchmark/src/androidTest/java/androidx/benchmark/integration/macrobenchmark/MacrobenchUtils.kt b/benchmark/integration-tests/macrobenchmark/src/androidTest/java/androidx/benchmark/integration/macrobenchmark/MacrobenchUtils.kt
deleted file mode 100644
index 8a3b2aa..0000000
--- a/benchmark/integration-tests/macrobenchmark/src/androidTest/java/androidx/benchmark/integration/macrobenchmark/MacrobenchUtils.kt
+++ /dev/null
@@ -1,80 +0,0 @@
-/*
- * Copyright 2020 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package androidx.benchmark.integration.macrobenchmark
-
-import android.content.Intent
-import androidx.benchmark.macro.CompilationMode
-import androidx.benchmark.macro.StartupMode
-import androidx.benchmark.macro.StartupTimingMetric
-import androidx.benchmark.macro.isSupportedWithVmSettings
-import androidx.benchmark.macro.junit4.MacrobenchmarkRule
-
-const val TARGET_PACKAGE = "androidx.benchmark.integration.macrobenchmark.target"
-
-fun MacrobenchmarkRule.measureStartup(
-    compilationMode: CompilationMode,
-    startupMode: StartupMode,
-    iterations: Int = 3,
-    setupIntent: Intent.() -> Unit = {}
-) = measureRepeated(
-    packageName = TARGET_PACKAGE,
-    metrics = listOf(StartupTimingMetric()),
-    compilationMode = compilationMode,
-    iterations = iterations,
-    startupMode = startupMode
-) {
-    pressHome()
-    val intent = Intent()
-    intent.setPackage(TARGET_PACKAGE)
-    setupIntent(intent)
-    startActivityAndWait(intent)
-}
-
-fun createStartupCompilationParams(
-    startupModes: List<StartupMode> = listOf(StartupMode.HOT, StartupMode.WARM, StartupMode.COLD),
-    compilationModes: List<CompilationMode> = listOf(
-        CompilationMode.None,
-        CompilationMode.Interpreted,
-        CompilationMode.SpeedProfile()
-    )
-): List<Array<Any>> = mutableListOf<Array<Any>>().apply {
-    for (startupMode in startupModes) {
-        for (compilationMode in compilationModes) {
-            // Skip configs that can't run, so they don't clutter Studio benchmark
-            // output with AssumptionViolatedException dumps
-            if (compilationMode.isSupportedWithVmSettings()) {
-                add(arrayOf(startupMode, compilationMode))
-            }
-        }
-    }
-}
-
-fun createCompilationParams(
-    compilationModes: List<CompilationMode> = listOf(
-        CompilationMode.None,
-        CompilationMode.Interpreted,
-        CompilationMode.SpeedProfile()
-    )
-): List<Array<Any>> = mutableListOf<Array<Any>>().apply {
-    for (compilationMode in compilationModes) {
-        // Skip configs that can't run, so they don't clutter Studio benchmark
-        // output with AssumptionViolatedException dumps
-        if (compilationMode.isSupportedWithVmSettings()) {
-            add(arrayOf(compilationMode))
-        }
-    }
-}
\ No newline at end of file
diff --git a/benchmark/integration-tests/macrobenchmark/src/androidTest/java/androidx/benchmark/integration/macrobenchmark/SmallListStartupBenchmark.kt b/benchmark/integration-tests/macrobenchmark/src/androidTest/java/androidx/benchmark/integration/macrobenchmark/SmallListStartupBenchmark.kt
index 553c577..98b8cfb20 100644
--- a/benchmark/integration-tests/macrobenchmark/src/androidTest/java/androidx/benchmark/integration/macrobenchmark/SmallListStartupBenchmark.kt
+++ b/benchmark/integration-tests/macrobenchmark/src/androidTest/java/androidx/benchmark/integration/macrobenchmark/SmallListStartupBenchmark.kt
@@ -20,6 +20,8 @@
 import androidx.benchmark.macro.StartupMode
 import androidx.benchmark.macro.junit4.MacrobenchmarkRule
 import androidx.test.filters.LargeTest
+import androidx.testutils.createStartupCompilationParams
+import androidx.testutils.measureStartup
 import org.junit.Rule
 import org.junit.Test
 import org.junit.runner.RunWith
@@ -37,7 +39,8 @@
     @Test
     fun startup() = benchmarkRule.measureStartup(
         compilationMode = compilationMode,
-        startupMode = startupMode
+        startupMode = startupMode,
+        packageName = "androidx.benchmark.integration.macrobenchmark.target"
     ) {
         action = "androidx.benchmark.integration.macrobenchmark.target.RECYCLER_VIEW"
         putExtra("ITEM_COUNT", 5)
diff --git a/benchmark/integration-tests/macrobenchmark/src/androidTest/java/androidx/benchmark/integration/macrobenchmark/TrivialListScrollBenchmark.kt b/benchmark/integration-tests/macrobenchmark/src/androidTest/java/androidx/benchmark/integration/macrobenchmark/TrivialListScrollBenchmark.kt
index 62ede92..39efe29 100644
--- a/benchmark/integration-tests/macrobenchmark/src/androidTest/java/androidx/benchmark/integration/macrobenchmark/TrivialListScrollBenchmark.kt
+++ b/benchmark/integration-tests/macrobenchmark/src/androidTest/java/androidx/benchmark/integration/macrobenchmark/TrivialListScrollBenchmark.kt
@@ -26,6 +26,7 @@
 import androidx.test.platform.app.InstrumentationRegistry
 import androidx.test.uiautomator.By
 import androidx.test.uiautomator.UiDevice
+import androidx.testutils.createCompilationParams
 import org.junit.Before
 import org.junit.Rule
 import org.junit.Test
diff --git a/benchmark/integration-tests/macrobenchmark/src/androidTest/java/androidx/benchmark/integration/macrobenchmark/TrivialStartupBenchmark.kt b/benchmark/integration-tests/macrobenchmark/src/androidTest/java/androidx/benchmark/integration/macrobenchmark/TrivialStartupBenchmark.kt
index 29d9be3..856badf 100644
--- a/benchmark/integration-tests/macrobenchmark/src/androidTest/java/androidx/benchmark/integration/macrobenchmark/TrivialStartupBenchmark.kt
+++ b/benchmark/integration-tests/macrobenchmark/src/androidTest/java/androidx/benchmark/integration/macrobenchmark/TrivialStartupBenchmark.kt
@@ -20,6 +20,8 @@
 import androidx.benchmark.macro.StartupMode
 import androidx.benchmark.macro.junit4.MacrobenchmarkRule
 import androidx.test.filters.LargeTest
+import androidx.testutils.createStartupCompilationParams
+import androidx.testutils.measureStartup
 import org.junit.Rule
 import org.junit.Test
 import org.junit.runner.RunWith
@@ -37,7 +39,8 @@
     @Test
     fun startup() = benchmarkRule.measureStartup(
         compilationMode = compilationMode,
-        startupMode = startupMode
+        startupMode = startupMode,
+        packageName = "androidx.benchmark.integration.macrobenchmark.target"
     ) {
         action = "androidx.benchmark.integration.macrobenchmark.target.TRIVIAL_STARTUP_ACTIVITY"
     }
diff --git a/compose/integration-tests/macrobenchmark/build.gradle b/compose/integration-tests/macrobenchmark/build.gradle
index 6568c2a..f400f4d 100644
--- a/compose/integration-tests/macrobenchmark/build.gradle
+++ b/compose/integration-tests/macrobenchmark/build.gradle
@@ -35,6 +35,7 @@
 dependencies {
     androidTestImplementation(project(":benchmark:benchmark-junit4"))
     androidTestImplementation(project(":benchmark:benchmark-macro-junit4"))
+    androidTestImplementation(project(":internal-testutils-macrobenchmark"))
     androidTestImplementation(ANDROIDX_TEST_RULES)
     androidTestImplementation(ANDROIDX_TEST_EXT_JUNIT)
     androidTestImplementation(ANDROIDX_TEST_CORE)
diff --git a/compose/integration-tests/macrobenchmark/src/androidTest/java/androidx/compose/integration/macrobenchmark/SmallListStartupBenchmark.kt b/compose/integration-tests/macrobenchmark/src/androidTest/java/androidx/compose/integration/macrobenchmark/SmallListStartupBenchmark.kt
index 0fe6f16..cb9e113 100644
--- a/compose/integration-tests/macrobenchmark/src/androidTest/java/androidx/compose/integration/macrobenchmark/SmallListStartupBenchmark.kt
+++ b/compose/integration-tests/macrobenchmark/src/androidTest/java/androidx/compose/integration/macrobenchmark/SmallListStartupBenchmark.kt
@@ -20,6 +20,8 @@
 import androidx.benchmark.macro.StartupMode
 import androidx.benchmark.macro.junit4.MacrobenchmarkRule
 import androidx.test.filters.LargeTest
+import androidx.testutils.createStartupCompilationParams
+import androidx.testutils.measureStartup
 import org.junit.Rule
 import org.junit.Test
 import org.junit.runner.RunWith
@@ -37,7 +39,8 @@
     @Test
     fun startup() = benchmarkRule.measureStartup(
         compilationMode = compilationMode,
-        startupMode = startupMode
+        startupMode = startupMode,
+        packageName = "androidx.compose.integration.macrobenchmark.target"
     ) {
         action = "androidx.compose.integration.macrobenchmark.target.LAZY_COLUMN_ACTIVITY"
         putExtra("ITEM_COUNT", 5)
diff --git a/compose/integration-tests/macrobenchmark/src/androidTest/java/androidx/compose/integration/macrobenchmark/TrivialListScrollBenchmark.kt b/compose/integration-tests/macrobenchmark/src/androidTest/java/androidx/compose/integration/macrobenchmark/TrivialListScrollBenchmark.kt
index 1be3b5c..6d0939c 100644
--- a/compose/integration-tests/macrobenchmark/src/androidTest/java/androidx/compose/integration/macrobenchmark/TrivialListScrollBenchmark.kt
+++ b/compose/integration-tests/macrobenchmark/src/androidTest/java/androidx/compose/integration/macrobenchmark/TrivialListScrollBenchmark.kt
@@ -26,6 +26,7 @@
 import androidx.test.uiautomator.By
 import androidx.test.uiautomator.UiDevice
 import androidx.test.uiautomator.Until
+import androidx.testutils.createCompilationParams
 import org.junit.Before
 import org.junit.Rule
 import org.junit.Test
diff --git a/compose/integration-tests/macrobenchmark/src/androidTest/java/androidx/compose/integration/macrobenchmark/TrivialStartupBenchmark.kt b/compose/integration-tests/macrobenchmark/src/androidTest/java/androidx/compose/integration/macrobenchmark/TrivialStartupBenchmark.kt
index 45167ff..1a6254b 100644
--- a/compose/integration-tests/macrobenchmark/src/androidTest/java/androidx/compose/integration/macrobenchmark/TrivialStartupBenchmark.kt
+++ b/compose/integration-tests/macrobenchmark/src/androidTest/java/androidx/compose/integration/macrobenchmark/TrivialStartupBenchmark.kt
@@ -20,6 +20,8 @@
 import androidx.benchmark.macro.StartupMode
 import androidx.benchmark.macro.junit4.MacrobenchmarkRule
 import androidx.test.filters.LargeTest
+import androidx.testutils.createStartupCompilationParams
+import androidx.testutils.measureStartup
 import org.junit.Rule
 import org.junit.Test
 import org.junit.runner.RunWith
@@ -37,7 +39,8 @@
     @Test
     fun startup() = benchmarkRule.measureStartup(
         compilationMode = compilationMode,
-        startupMode = startupMode
+        startupMode = startupMode,
+        packageName = "androidx.compose.integration.macrobenchmark.target"
     ) {
         action = "androidx.compose.integration.macrobenchmark.target.TRIVIAL_STARTUP_ACTIVITY"
     }
diff --git a/settings.gradle b/settings.gradle
index b657ebe..fab68c1 100644
--- a/settings.gradle
+++ b/settings.gradle
@@ -665,6 +665,7 @@
 includeProject(":internal-testutils-espresso", "testutils/testutils-espresso", [BuildType.MAIN])
 includeProject(":internal-testutils-truth", "testutils/testutils-truth", [BuildType.MAIN, BuildType.FLAN])
 includeProject(":internal-testutils-ktx", "testutils/testutils-ktx", [BuildType.MAIN, BuildType.COMPOSE])
+includeProject(":internal-testutils-macrobenchmark", "testutils/testutils-macrobenchmark", [BuildType.MAIN, BuildType.COMPOSE])
 includeProject(":internal-testutils-navigation", "testutils/testutils-navigation", [BuildType.MAIN, BuildType.COMPOSE, BuildType.FLAN])
 includeProject(":internal-testutils-paging", "testutils/testutils-paging", [BuildType.MAIN, BuildType.COMPOSE])
 includeProject(":internal-testutils-gradle-plugin", "testutils/testutils-gradle-plugin", [BuildType.MAIN, BuildType.FLAN])
diff --git a/testutils/testutils-macrobenchmark/OWNERS b/testutils/testutils-macrobenchmark/OWNERS
new file mode 100644
index 0000000..bd5d7e4
--- /dev/null
+++ b/testutils/testutils-macrobenchmark/OWNERS
@@ -0,0 +1 @@
[email protected]
\ No newline at end of file
diff --git a/testutils/testutils-macrobenchmark/build.gradle b/testutils/testutils-macrobenchmark/build.gradle
new file mode 100644
index 0000000..d331ce5
--- /dev/null
+++ b/testutils/testutils-macrobenchmark/build.gradle
@@ -0,0 +1,39 @@
+/*
+ * Copyright 2018 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+
+import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
+
+import static androidx.build.dependencies.DependenciesKt.*
+
+plugins {
+    id("AndroidXPlugin")
+    id("com.android.library")
+    id("kotlin-android")
+}
+
+dependencies {
+    implementation(project(":benchmark:benchmark-macro"))
+    implementation(project(":benchmark:benchmark-macro-junit4"))
+
+    implementation(KOTLIN_STDLIB)
+}
+
+android {
+    defaultConfig {
+        minSdkVersion 18
+    }
+}
diff --git a/testutils/testutils-macrobenchmark/src/main/AndroidManifest.xml b/testutils/testutils-macrobenchmark/src/main/AndroidManifest.xml
new file mode 100644
index 0000000..e78b95a
--- /dev/null
+++ b/testutils/testutils-macrobenchmark/src/main/AndroidManifest.xml
@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+  Copyright 2021 The Android Open Source Project
+
+  Licensed under the Apache License, Version 2.0 (the "License");
+  you may not use this file except in compliance with the License.
+  You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+  -->
+<manifest package="androidx.testutils"/>
diff --git a/compose/integration-tests/macrobenchmark/src/androidTest/java/androidx/compose/integration/macrobenchmark/MacrobenchUtils.kt b/testutils/testutils-macrobenchmark/src/main/java/androidx/testutils/MacrobenchUtils.kt
similarity index 92%
rename from compose/integration-tests/macrobenchmark/src/androidTest/java/androidx/compose/integration/macrobenchmark/MacrobenchUtils.kt
rename to testutils/testutils-macrobenchmark/src/main/java/androidx/testutils/MacrobenchUtils.kt
index f893da1..2ec05dd 100644
--- a/compose/integration-tests/macrobenchmark/src/androidTest/java/androidx/compose/integration/macrobenchmark/MacrobenchUtils.kt
+++ b/testutils/testutils-macrobenchmark/src/main/java/androidx/testutils/MacrobenchUtils.kt
@@ -14,24 +14,25 @@
  * limitations under the License.
  */
 
-package androidx.compose.integration.macrobenchmark
+package androidx.testutils
 
 import android.content.Intent
+import androidx.annotation.RequiresApi
 import androidx.benchmark.macro.CompilationMode
 import androidx.benchmark.macro.StartupMode
 import androidx.benchmark.macro.StartupTimingMetric
 import androidx.benchmark.macro.isSupportedWithVmSettings
 import androidx.benchmark.macro.junit4.MacrobenchmarkRule
 
-const val TARGET_PACKAGE = "androidx.compose.integration.macrobenchmark.target"
-
+@RequiresApi(29)
 fun MacrobenchmarkRule.measureStartup(
     compilationMode: CompilationMode,
     startupMode: StartupMode,
+    packageName: String,
     iterations: Int = 3,
     setupIntent: Intent.() -> Unit = {}
 ) = measureRepeated(
-    packageName = TARGET_PACKAGE,
+    packageName = packageName,
     metrics = listOf(StartupTimingMetric()),
     compilationMode = compilationMode,
     iterations = iterations,
@@ -39,7 +40,7 @@
 ) {
     pressHome()
     val intent = Intent()
-    intent.setPackage(TARGET_PACKAGE)
+    intent.setPackage(packageName)
     setupIntent(intent)
     startActivityAndWait(intent)
 }