1 /* 2 * Copyright (C) 2023 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 17 package android.tools.monitors 18 19 import android.app.Instrumentation 20 import android.tools.Tag 21 import android.tools.io.RunStatus 22 import android.tools.io.TraceType 23 import android.tools.testutils.CleanFlickerEnvironmentRule 24 import android.tools.testutils.newTestResultWriter 25 import android.tools.testutils.outputFileName 26 import android.tools.traces.SERVICE_TRACE_CONFIG 27 import android.tools.traces.TRACE_CONFIG_REQUIRE_CHANGES 28 import android.tools.traces.deleteIfExists 29 import android.tools.traces.io.ResultReader 30 import android.tools.traces.monitors.TraceMonitor 31 import androidx.test.platform.app.InstrumentationRegistry 32 import androidx.test.uiautomator.UiDevice 33 import com.google.common.truth.Truth 34 import org.junit.After 35 import org.junit.Before 36 import org.junit.ClassRule 37 import org.junit.Test 38 39 abstract class TraceMonitorTest<T : TraceMonitor> { getMonitornull40 abstract fun getMonitor(): T 41 42 abstract fun assertTrace(traceData: ByteArray) 43 44 abstract val traceType: TraceType 45 46 protected open val tag = Tag.ALL 47 protected val instrumentation: Instrumentation = InstrumentationRegistry.getInstrumentation() 48 protected val device: UiDevice = UiDevice.getInstance(instrumentation) 49 protected val traceMonitor by lazy { getMonitor() } 50 51 @Before beforenull52 open fun before() { 53 Truth.assertWithMessage("Trace already enabled before starting test") 54 .that(traceMonitor.isEnabled) 55 .isFalse() 56 } 57 58 @After teardownnull59 fun teardown() { 60 device.pressHome() 61 if (traceMonitor.isEnabled) { 62 traceMonitor.stop(newTestResultWriter()) 63 } 64 outputFileName(RunStatus.RUN_EXECUTED).deleteIfExists() 65 Truth.assertWithMessage("Failed to disable trace at end of test") 66 .that(traceMonitor.isEnabled) 67 .isFalse() 68 } 69 70 @Test 71 @Throws(Exception::class) canStartTracenull72 fun canStartTrace() { 73 traceMonitor.start() 74 Truth.assertThat(traceMonitor.isEnabled).isTrue() 75 } 76 77 @Test 78 @Throws(Exception::class) canStopTracenull79 fun canStopTrace() { 80 traceMonitor.start() 81 Truth.assertThat(traceMonitor.isEnabled).isTrue() 82 traceMonitor.stop(newTestResultWriter()) 83 Truth.assertThat(traceMonitor.isEnabled).isFalse() 84 } 85 86 @Test 87 @Throws(Exception::class) captureTracenull88 open fun captureTrace() { 89 traceMonitor.start() 90 device.pressHome() 91 device.pressRecentApps() 92 val writer = newTestResultWriter() 93 traceMonitor.stop(writer) 94 val result = writer.write() 95 val reader = ResultReader(result, TRACE_CONFIG_REQUIRE_CHANGES) 96 Truth.assertWithMessage("Trace file exists ${traceMonitor.traceType}") 97 .that(reader.hasTraceFile(traceMonitor.traceType, tag)) 98 .isTrue() 99 100 val trace = 101 reader.readBytes(traceMonitor.traceType, tag) 102 ?: error("Missing trace file ${traceMonitor.traceType}") 103 Truth.assertWithMessage("Trace file size").that(trace.size).isGreaterThan(0) 104 assertTrace(trace) 105 } 106 107 @Test withTracingnull108 open fun withTracing() { 109 val reader = 110 traceMonitor.withTracing( 111 resultReaderProvider = { ResultReader(it, SERVICE_TRACE_CONFIG) } 112 ) { 113 device.pressHome() 114 device.pressRecentApps() 115 } 116 val trace = reader.readBytes(traceType, tag) ?: ByteArray(0) 117 assertTrace(trace) 118 } 119 120 companion object { 121 @ClassRule @JvmField val ENV_CLEANUP = CleanFlickerEnvironmentRule() 122 } 123 } 124