Merge "Remove BenchmarkRule requirement to be used each test" into androidx-main
diff --git a/benchmark/common/src/androidTest/java/androidx/benchmark/BenchmarkStateTest.kt b/benchmark/common/src/androidTest/java/androidx/benchmark/BenchmarkStateTest.kt
index 2c6518d..ca08357 100644
--- a/benchmark/common/src/androidTest/java/androidx/benchmark/BenchmarkStateTest.kt
+++ b/benchmark/common/src/androidTest/java/androidx/benchmark/BenchmarkStateTest.kt
@@ -229,7 +229,6 @@
         } catch (e: IllegalStateException) {
             assertEquals(initialPriority, ThreadPriority.get())
             assertTrue(e.message!!.contains("wasn't started"))
-            assertTrue(e.message!!.contains("benchmarkRule.measureRepeated {}"))
         }
     }
 
diff --git a/benchmark/common/src/main/java/androidx/benchmark/BenchmarkState.kt b/benchmark/common/src/main/java/androidx/benchmark/BenchmarkState.kt
index de873fb..4197561 100644
--- a/benchmark/common/src/main/java/androidx/benchmark/BenchmarkState.kt
+++ b/benchmark/common/src/main/java/androidx/benchmark/BenchmarkState.kt
@@ -124,10 +124,7 @@
 
     private fun checkState() {
         check(state != NOT_STARTED) {
-            "The benchmark wasn't started! Every test in a class " +
-                "with a BenchmarkRule must contain a benchmark. In Kotlin, call " +
-                "benchmarkRule.measureRepeated {}, or in Java, call " +
-                "benchmarkRule.getState().keepRunning() to run your benchmark."
+            "Attempting to interact with a benchmark that wasn't started!"
         }
         check(state == FINISHED) {
             "The benchmark hasn't finished! In Java, use " +
@@ -501,6 +498,10 @@
         simpleClassName: String,
         methodName: String
     ) {
+        if (state == NOT_STARTED) {
+            return; // nothing to report, BenchmarkState wasn't used
+        }
+
         checkState() // this method is triggered externally
         val fullTestName = "$PREFIX$simpleClassName.$methodName"
         val bundle = getFullStatusReport(
diff --git a/benchmark/junit4/src/androidTest/java/androidx/benchmark/junit4/BenchmarkRuleNotUsedTest.kt b/benchmark/junit4/src/androidTest/java/androidx/benchmark/junit4/BenchmarkRuleNotUsedTest.kt
new file mode 100644
index 0000000..4100a22
--- /dev/null
+++ b/benchmark/junit4/src/androidTest/java/androidx/benchmark/junit4/BenchmarkRuleNotUsedTest.kt
@@ -0,0 +1,40 @@
+/*
+ * Copyright 2019 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.junit4
+
+import androidx.test.ext.junit.runners.AndroidJUnit4
+import androidx.test.filters.SmallTest
+import org.junit.Rule
+import org.junit.Test
+import org.junit.runner.RunWith
+
+@SmallTest
+@RunWith(AndroidJUnit4::class)
+public class BenchmarkRuleNotUsedTest {
+    @get:Rule
+    public val benchmarkRule: BenchmarkRule = BenchmarkRule(enableReport = true)
+
+    /**
+     * Previously this test would fail, because BenchmarkState().report(...) used in the
+     * post-test BenchmarkRule logic would fail, due to not yet being initialized.
+     *
+     * Now, this test passes to enable other tests to be written without using the BenchmarkRule.
+     */
+    @Test
+    public fun testWithoutMeasurement() {
+    }
+}
\ No newline at end of file
diff --git a/benchmark/junit4/src/androidTest/java/androidx/benchmark/junit4/BenchmarkRuleTest.kt b/benchmark/junit4/src/androidTest/java/androidx/benchmark/junit4/BenchmarkRuleTest.kt
index c55ef2d..9b4f1f3 100644
--- a/benchmark/junit4/src/androidTest/java/androidx/benchmark/junit4/BenchmarkRuleTest.kt
+++ b/benchmark/junit4/src/androidTest/java/androidx/benchmark/junit4/BenchmarkRuleTest.kt
@@ -18,7 +18,7 @@
 
 import androidx.test.ext.junit.runners.AndroidJUnit4
 import androidx.test.filters.LargeTest
-import org.junit.Assert.assertTrue
+import org.junit.Assert
 import org.junit.Rule
 import org.junit.Test
 import org.junit.runner.RunWith
@@ -26,19 +26,19 @@
 
 @LargeTest
 @RunWith(AndroidJUnit4::class)
-class BenchmarkRuleTest {
+public class BenchmarkRuleTest {
     @get:Rule
-    val benchmarkRule = BenchmarkRule(enableReport = false)
+    public val benchmarkRule: BenchmarkRule = BenchmarkRule(enableReport = false)
 
     @Test
-    fun runWithTimingDisabled() {
+    public fun runWithTimingDisabled() {
         benchmarkRule.measureRepeated {
             runWithTimingDisabled {
                 Thread.sleep(5)
             }
         }
         val min = benchmarkRule.getState().getMinTimeNanos()
-        assertTrue(
+        Assert.assertTrue(
             "minimum $min should be less than 1ms",
             min < TimeUnit.MILLISECONDS.toNanos(1)
         )