1 /* 2 * Copyright (C) 2022 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 com.android.compatibility.common.util; 18 19 import org.xmlpull.v1.XmlSerializer; 20 21 import java.io.IOException; 22 import java.io.Serializable; 23 import java.util.ArrayList; 24 import java.util.Objects; 25 import java.util.Set; 26 27 /** 28 * Utility class to add test case screenshot metadata to the report. This class records per-case 29 * screenshot metadata for CTS Verifier. If this field is used for large test suites like CTS, it 30 * may cause performance issues in APFE. Thus please do not use this class in other test suites. 31 */ 32 public class TestScreenshotsMetadata implements Serializable { 33 34 // XML constants 35 private static final String SCREENSHOTS_TAG = "Screenshots"; 36 private static final String SCREENSHOT_TAG = "Screenshot"; 37 private static final String NAME_ATTR = "name"; 38 private static final String DESCRIPTION_ATTR = "description"; 39 40 private final String mTestName; 41 private final Set<TestScreenshotsMetadata.ScreenshotMetadata> mScreenshotMetadataSet; 42 43 /** 44 * Constructor of test screenshots metadata. 45 * 46 * @param screenshots a Set of ScreenshotMetadata. 47 */ TestScreenshotsMetadata(String testName, Set<TestScreenshotsMetadata.ScreenshotMetadata> screenshots)48 public TestScreenshotsMetadata(String testName, 49 Set<TestScreenshotsMetadata.ScreenshotMetadata> screenshots) { 50 mTestName = testName; 51 mScreenshotMetadataSet = screenshots; 52 } 53 54 /** Get test name */ getTestName()55 public String getTestName() { 56 return mTestName; 57 } 58 59 /** Get a set of ScreenshotMetadata. */ getScreenshotMetadataSet()60 public Set<TestScreenshotsMetadata.ScreenshotMetadata> getScreenshotMetadataSet() { 61 return mScreenshotMetadataSet; 62 } 63 64 @Override toString()65 public String toString() { 66 ArrayList<String> arr = new ArrayList<>(); 67 for (TestScreenshotsMetadata.ScreenshotMetadata e : mScreenshotMetadataSet) { 68 arr.add(e.toString()); 69 } 70 return String.join(", ", arr); 71 } 72 73 /** {@inheritDoc} */ 74 @Override equals(Object o)75 public boolean equals(Object o) { 76 if (this == o) { 77 return true; 78 } 79 if (!(o instanceof TestScreenshotsMetadata)) { 80 return false; 81 } 82 TestScreenshotsMetadata that = (TestScreenshotsMetadata) o; 83 return Objects.equals(mTestName, that.mTestName) 84 && Objects.equals(mScreenshotMetadataSet, that.mScreenshotMetadataSet); 85 } 86 87 /** {@inheritDoc} */ 88 @Override hashCode()89 public int hashCode() { 90 return Objects.hash(mTestName, mScreenshotMetadataSet); 91 } 92 93 /** 94 * Serializes a given {@link TestScreenshotsMetadata} to XML. 95 * 96 * @param serializer given serializer. 97 * @param screenshotsMetadata test screenshots metadata. 98 */ serialize( XmlSerializer serializer, TestScreenshotsMetadata screenshotsMetadata)99 public static void serialize( 100 XmlSerializer serializer, TestScreenshotsMetadata screenshotsMetadata) 101 throws IOException { 102 if (screenshotsMetadata == null) { 103 throw new IllegalArgumentException("Test screenshots metadata was null"); 104 } 105 106 serializer.startTag(null, SCREENSHOTS_TAG); 107 108 for (TestScreenshotsMetadata.ScreenshotMetadata screenshotMetadata : 109 screenshotsMetadata.getScreenshotMetadataSet()) { 110 serializer.startTag(null, SCREENSHOT_TAG); 111 serializer.attribute(null, 112 NAME_ATTR, String.valueOf(screenshotMetadata.getScreenshotName())); 113 serializer.attribute( 114 null, DESCRIPTION_ATTR, String.valueOf(screenshotMetadata.getDescription())); 115 serializer.endTag(null, SCREENSHOT_TAG); 116 } 117 serializer.endTag(null, SCREENSHOTS_TAG); 118 } 119 120 /** Single screenshot information */ 121 public static class ScreenshotMetadata implements Serializable { 122 String mScreenshotName; 123 String mDescription; 124 setDescription(String description)125 public void setDescription(String description) { 126 mDescription = description; 127 } 128 getDescription()129 public String getDescription() { 130 return mDescription; 131 } 132 setScreenshotName(String screenshotName)133 public void setScreenshotName(String screenshotName) { 134 mScreenshotName = screenshotName; 135 } 136 getScreenshotName()137 public String getScreenshotName() { 138 return mScreenshotName; 139 } 140 141 @Override equals(Object o)142 public boolean equals(Object o) { 143 if (this == o) { 144 return true; 145 } 146 if (!(o instanceof ScreenshotMetadata)) { 147 return false; 148 } 149 TestScreenshotsMetadata.ScreenshotMetadata that = 150 (TestScreenshotsMetadata.ScreenshotMetadata) o; 151 return mScreenshotName.equals(that.mScreenshotName) 152 && mDescription.equals(that.mDescription); 153 } 154 155 @Override hashCode()156 public int hashCode() { 157 return Objects.hash(mScreenshotName, mDescription); 158 } 159 160 @Override toString()161 public String toString() { 162 return "[" + mScreenshotName + ", " + mDescription + "]"; 163 } 164 } 165 } 166