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.view 18 19 import android.tools.io.TraceType 20 import android.tools.monitors.TraceMonitorTest 21 import android.tools.testutils.assertArchiveContainsFiles 22 import android.tools.testutils.getActualTraceFilesFromArchive 23 import android.tools.testutils.getLauncherPackageName 24 import android.tools.testutils.getSystemUiUidName 25 import android.tools.testutils.newTestResultWriter 26 import android.tools.traces.TRACE_CONFIG_REQUIRE_CHANGES 27 import android.tools.traces.io.ResultReader 28 import android.tools.traces.monitors.view.ViewTraceMonitor 29 import android.tracing.Flags 30 import com.android.systemui.Flags.enableViewCaptureTracing 31 import com.google.common.truth.Truth 32 import java.io.File 33 import org.junit.Assume 34 import org.junit.Before 35 import org.junit.FixMethodOrder 36 import org.junit.Test 37 import org.junit.runners.MethodSorters 38 39 /** Tests for [ViewTraceMonitor] */ 40 @FixMethodOrder(MethodSorters.NAME_ASCENDING) 41 class ViewTraceMonitorTest : TraceMonitorTest<ViewTraceMonitor>() { 42 override val traceType = TraceType.VIEW 43 getMonitornull44 override fun getMonitor() = ViewTraceMonitor() 45 46 override fun assertTrace(traceData: ByteArray) { 47 Truth.assertThat(traceData.size).isGreaterThan(0) 48 } 49 50 @Before beforenull51 override fun before() { 52 Assume.assumeFalse(Flags.perfettoViewCaptureTracing()) 53 super.before() 54 } 55 56 @Test 57 @Throws(Exception::class) captureTracenull58 override fun captureTrace() { 59 var possibleExpectedTraces = listOf(EXPECTED_TRACES_LAUNCHER_ONLY) 60 if (enableViewCaptureTracing()) { 61 possibleExpectedTraces = 62 listOf(EXPECTED_TRACES_LAUNCHER_FIRST, EXPECTED_TRACES_SYSUI_FIRST) 63 } 64 65 traceMonitor.start() 66 device.pressHome() 67 device.pressRecentApps() 68 val writer = newTestResultWriter() 69 traceMonitor.stop(writer) 70 val result = writer.write() 71 val reader = ResultReader(result, TRACE_CONFIG_REQUIRE_CHANGES) 72 73 val traceArtifactPath = reader.artifactPath 74 require(traceArtifactPath.isNotEmpty()) { "Artifact path missing in result" } 75 val traceArchive = File(traceArtifactPath) 76 77 assertArchiveContainsFiles(traceArchive, possibleExpectedTraces) 78 val actualTraceFiles = getActualTraceFilesFromArchive(traceArchive) 79 val tagList = getTagListFromTraces(actualTraceFiles) 80 for (tag: String in tagList) { 81 val trace = 82 reader.readBytes(traceMonitor.traceType, tag) 83 ?: error("Missing trace file ${traceMonitor.traceType}") 84 Truth.assertWithMessage("Trace file size").that(trace.size).isGreaterThan(0) 85 assertTrace(trace) 86 } 87 } 88 89 // We override this test to create a placeholder since the [withTracing] method does not align 90 // with the implementation of multiple instances of ViewCapture. 91 @Test 92 @Throws(Exception::class) withTracingnull93 override fun withTracing() { 94 Truth.assertThat(true).isTrue() 95 } 96 getTagListFromTracesnull97 private fun getTagListFromTraces(actualTraceFiles: List<String>): List<String> { 98 var tagList: List<String> = emptyList() 99 for (traceFile: String in actualTraceFiles) { 100 tagList += traceFile.removeSuffix("__view_capture_trace.winscope") 101 } 102 return tagList 103 } 104 105 companion object { 106 val EXPECTED_TRACES_LAUNCHER_ONLY = 107 listOf("${getLauncherPackageName()}_0.vc__view_capture_trace.winscope") 108 val EXPECTED_TRACES_LAUNCHER_FIRST = 109 listOf( 110 "${getLauncherPackageName()}_0.vc__view_capture_trace.winscope", 111 "${getSystemUiUidName()}_1.vc__view_capture_trace.winscope", 112 ) 113 114 val EXPECTED_TRACES_SYSUI_FIRST = 115 listOf( 116 "${getSystemUiUidName()}_0.vc__view_capture_trace.winscope", 117 "${getLauncherPackageName()}_1.vc__view_capture_trace.winscope", 118 ) 119 } 120 } 121