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.device.traces.io 18 19 import android.tools.TEST_SCENARIO 20 import android.tools.common.io.ResultArtifactDescriptor 21 import android.tools.common.io.RunStatus 22 import android.tools.common.io.TraceType 23 import android.tools.createDefaultArtifactBuilder 24 import android.tools.rules.CleanFlickerEnvironmentRule 25 import com.google.common.truth.Truth 26 import java.io.File 27 import kotlin.io.path.createTempDirectory 28 import org.junit.ClassRule 29 import org.junit.Test 30 31 /** Tests for [ArtifactBuilder] */ 32 class ArtifactBuilderTest { 33 @Test buildArtifactAndFileExistsnull34 fun buildArtifactAndFileExists() { 35 for (status in RunStatus.values()) { 36 val artifact = createDefaultArtifactBuilder(status).build() 37 Truth.assertWithMessage("Artifact exists").that(artifact.file.exists()).isTrue() 38 artifact.deleteIfExists() 39 } 40 } 41 42 @Test buildArtifactWithStatusnull43 fun buildArtifactWithStatus() { 44 for (status in RunStatus.values()) { 45 val artifact = createDefaultArtifactBuilder(status).build() 46 val statusFromFile = RunStatus.fromFileName(artifact.file.name) 47 Truth.assertWithMessage("Run status").that(statusFromFile).isEqualTo(status) 48 artifact.deleteIfExists() 49 } 50 } 51 52 @Test buildArtifactWithScenarionull53 fun buildArtifactWithScenario() { 54 val artifact = createDefaultArtifactBuilder(RunStatus.RUN_FAILED).build() 55 Truth.assertWithMessage("Scenario") 56 .that(artifact.file.name) 57 .contains(TEST_SCENARIO.toString()) 58 artifact.deleteIfExists() 59 } 60 61 @Test buildArtifactWithOutputDirnull62 fun buildArtifactWithOutputDir() { 63 val expectedDir = createTempDirectory().toFile() 64 val artifact = 65 createDefaultArtifactBuilder(RunStatus.RUN_FAILED, outputDir = expectedDir).build() 66 Truth.assertWithMessage("Output dir").that(artifact.file.parentFile).isEqualTo(expectedDir) 67 artifact.deleteIfExists() 68 } 69 70 @Test buildArtifactWithFilesnull71 fun buildArtifactWithFiles() { 72 val expectedDescriptor = ResultArtifactDescriptor(TraceType.WM) 73 val expectedFiles = mapOf(expectedDescriptor to File.createTempFile("test", "")) 74 val artifact = 75 createDefaultArtifactBuilder(RunStatus.RUN_FAILED, files = expectedFiles).build() 76 Truth.assertWithMessage("Trace count").that(artifact.traceCount()).isEqualTo(1) 77 Truth.assertWithMessage("Trace type").that(artifact.hasTrace(expectedDescriptor)).isTrue() 78 artifact.deleteIfExists() 79 } 80 81 @Test buildArtifactAvoidDuplicatenull82 fun buildArtifactAvoidDuplicate() { 83 val builder = createDefaultArtifactBuilder(RunStatus.RUN_FAILED) 84 val artifact1 = builder.build() 85 val artifact2 = builder.build() 86 87 Truth.assertWithMessage("Artifact 1 exists").that(artifact1.file.exists()).isTrue() 88 Truth.assertWithMessage("Artifact 2 exists").that(artifact2.file.exists()).isTrue() 89 Truth.assertWithMessage("Different files") 90 .that(artifact2.file.name) 91 .isNotEqualTo(artifact1.file.name) 92 Truth.assertWithMessage("Artifact 2 name").that(artifact2.file.name).endsWith("_1.zip") 93 } 94 95 @Test buildTwoArtifactsThatAreDifferentWithSameStatusnull96 fun buildTwoArtifactsThatAreDifferentWithSameStatus() { 97 val builder = createDefaultArtifactBuilder(RunStatus.RUN_FAILED) 98 val artifact1 = builder.build() 99 val artifact2 = builder.build() 100 101 Truth.assertWithMessage("Artifacts are equal").that(artifact1).isNotEqualTo(artifact2) 102 } 103 104 @Test buildTwoArtifactsThatAreDifferentnull105 fun buildTwoArtifactsThatAreDifferent() { 106 val artifact1 = createDefaultArtifactBuilder(RunStatus.RUN_FAILED).build() 107 val artifact2 = createDefaultArtifactBuilder(RunStatus.PARSING_FAILURE).build() 108 Truth.assertWithMessage("Artifacts are equal").that(artifact1).isNotEqualTo(artifact2) 109 } 110 111 @Test(expected = IllegalStateException::class) failArtifactWithoutStatusnull112 fun failArtifactWithoutStatus() { 113 ArtifactBuilder() 114 .withScenario(TEST_SCENARIO) 115 .withOutputDir(createTempDirectory().toFile()) 116 .withFiles(emptyMap()) 117 .build() 118 } 119 120 @Test(expected = IllegalStateException::class) failArtifactWithoutScenarionull121 fun failArtifactWithoutScenario() { 122 ArtifactBuilder() 123 .withOutputDir(createTempDirectory().toFile()) 124 .withStatus(RunStatus.RUN_FAILED) 125 .withFiles(emptyMap()) 126 .build() 127 } 128 129 @Test(expected = IllegalStateException::class) failArtifactWithoutOutputDirnull130 fun failArtifactWithoutOutputDir() { 131 ArtifactBuilder() 132 .withScenario(TEST_SCENARIO) 133 .withStatus(RunStatus.RUN_FAILED) 134 .withFiles(emptyMap()) 135 .build() 136 } 137 138 companion object { 139 @ClassRule @JvmField val ENV_CLEANUP = CleanFlickerEnvironmentRule() 140 } 141 } 142