1 /* 2 * Copyright (C) 2014 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 package com.android.cts.tradefed.result; 17 18 import org.kxml2.io.KXmlSerializer; 19 import org.xmlpull.v1.XmlPullParser; 20 21 import java.io.IOException; 22 23 import javax.annotation.Nullable; 24 25 /** 26 * TestLog describes a log for a test. It corresponds to the "TestLog" XML element. 27 */ 28 class TestLog { 29 30 private static final String TAG = "TestLog"; 31 private static final String TYPE_ATTR = "type"; 32 private static final String URL_ATTR = "url"; 33 34 /** Type of log. */ 35 public enum TestLogType { 36 LOGCAT("logcat-"), 37 BUGREPORT("bug-"), 38 39 ; 40 41 // This enum restricts the type of logs reported back to the server, 42 // because we do not intend to support them all. Using an enum somewhat 43 // assures that we will only see these expected types on the server side. 44 45 /** 46 * Returns the TestLogType from an ILogSaver data name or null 47 * if the data name is not supported. 48 */ 49 @Nullable fromDataName(String dataName)50 static TestLogType fromDataName(String dataName) { 51 if (dataName == null) { 52 return null; 53 } 54 55 for (TestLogType type : values()) { 56 if (dataName.startsWith(type.dataNamePrefix)) { 57 return type; 58 } 59 } 60 return null; 61 } 62 63 private final String dataNamePrefix; 64 TestLogType(String dataNamePrefix)65 private TestLogType(String dataNamePrefix) { 66 this.dataNamePrefix = dataNamePrefix; 67 } 68 getAttrValue()69 String getAttrValue() { 70 return name().toLowerCase(); 71 } 72 } 73 74 /** Type of the log like LOGCAT or BUGREPORT. */ 75 private final TestLogType mLogType; 76 77 /** Url pointing to the log file. */ 78 private final String mUrl; 79 80 /** Create a TestLog from an ILogSaver callback. */ 81 @Nullable fromDataName(String dataName, String url)82 static TestLog fromDataName(String dataName, String url) { 83 TestLogType logType = TestLogType.fromDataName(dataName); 84 if (logType == null) { 85 return null; 86 } 87 88 if (url == null) { 89 return null; 90 } 91 92 return new TestLog(logType, url); 93 } 94 95 /** Create a TestLog from XML given a XmlPullParser positioned at the TestLog tag. */ 96 @Nullable fromXml(XmlPullParser parser)97 static TestLog fromXml(XmlPullParser parser) { 98 String type = parser.getAttributeValue(null, TYPE_ATTR); 99 if (type == null) { 100 return null; 101 } 102 103 String url = parser.getAttributeValue(null, URL_ATTR); 104 if (url == null) { 105 return null; 106 } 107 108 try { 109 TestLogType logType = TestLogType.valueOf(type.toUpperCase()); 110 return new TestLog(logType, url); 111 } catch (IllegalArgumentException e) { 112 return null; 113 } 114 } 115 116 /** Create a TestLog directly given a log type and url. */ of(TestLogType logType, String url)117 public static TestLog of(TestLogType logType, String url) { 118 return new TestLog(logType, url); 119 } 120 TestLog(TestLogType logType, String url)121 private TestLog(TestLogType logType, String url) { 122 this.mLogType = logType; 123 this.mUrl = url; 124 } 125 126 /** Returns this TestLog's log type. */ getLogType()127 TestLogType getLogType() { 128 return mLogType; 129 } 130 131 /** Returns this TestLog's URL. */ getUrl()132 String getUrl() { 133 return mUrl; 134 } 135 136 /** Serialize the TestLog to XML. */ serialize(KXmlSerializer serializer)137 public void serialize(KXmlSerializer serializer) throws IOException { 138 serializer.startTag(CtsXmlResultReporter.ns, TAG); 139 serializer.attribute(CtsXmlResultReporter.ns, TYPE_ATTR, mLogType.getAttrValue()); 140 serializer.attribute(CtsXmlResultReporter.ns, URL_ATTR, mUrl); 141 serializer.endTag(CtsXmlResultReporter.ns, TAG); 142 } 143 144 /** Returns true if the tag is a TestLog tag. */ isTag(String tagName)145 public static boolean isTag(String tagName) { 146 return TAG.equals(tagName); 147 } 148 } 149