1 /* 2 * Copyright (C) 2011 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 com.android.tradefed.util.xml.AbstractXmlParser.ParseException; 19 20 import org.kxml2.io.KXmlSerializer; 21 import org.xmlpull.v1.XmlPullParser; 22 import org.xmlpull.v1.XmlPullParserException; 23 24 import android.tests.getinfo.DeviceInfoConstants; 25 26 import java.io.IOException; 27 import java.io.StringReader; 28 import java.io.StringWriter; 29 import java.util.HashMap; 30 import java.util.Map; 31 32 import junit.framework.TestCase; 33 34 /** 35 * Unit tests for {@link DeviceInfoResult} 36 */ 37 public class DeviceInfoResultTest extends TestCase { 38 39 private DeviceInfoResult mDeserializingInfo; 40 41 @Override 42 protected void setUp() throws Exception { 43 mDeserializingInfo = new DeviceInfoResult() { 44 // override parent to advance xml parser to correct tag 45 @Override 46 void parse(XmlPullParser parser) throws XmlPullParserException, IOException { 47 int eventType = parser.getEventType(); 48 while (eventType != XmlPullParser.END_DOCUMENT) { 49 if (eventType == XmlPullParser.START_TAG && parser.getName().equals(TAG)) { 50 super.parse(parser); 51 return; 52 } 53 eventType = parser.next(); 54 } 55 throw new XmlPullParserException(String.format("Could not find tag %s", TAG)); 56 } 57 }; 58 } 59 60 public void testFeatures() throws Exception { 61 assertSerializeParse(DeviceInfoConstants.FEATURES, ""); 62 assertSerializeParse(DeviceInfoConstants.FEATURES, 63 "android.hardware.audio.low_latency:sdk:false;"); 64 assertSerializeParse(DeviceInfoConstants.FEATURES, "android.hardware.audio.low_latency:" 65 + "sdk:false;android.hardware.bluetooth:sdk:true;"); 66 } 67 68 public void testProcesses() throws Exception { 69 assertSerializeParse(DeviceInfoConstants.PROCESSES, ""); 70 assertSerializeParse(DeviceInfoConstants.PROCESSES, "ueventd:0;"); 71 assertSerializeParse(DeviceInfoConstants.PROCESSES, "ueventd:0;netd:0;"); 72 } 73 74 public void testOpenGlTextureFormats() throws Exception { 75 assertSerializeParse(DeviceInfoConstants.OPEN_GL_COMPRESSED_TEXTURE_FORMATS, ""); 76 assertSerializeParse(DeviceInfoConstants.OPEN_GL_COMPRESSED_TEXTURE_FORMATS, "texture1;"); 77 assertSerializeParse(DeviceInfoConstants.OPEN_GL_COMPRESSED_TEXTURE_FORMATS, 78 "texture1;texture2;"); 79 } 80 81 public void testSystemLibraries() throws Exception { 82 assertSerializeParse(DeviceInfoConstants.SYS_LIBRARIES, ""); 83 assertSerializeParse(DeviceInfoConstants.SYS_LIBRARIES, "lib1;"); 84 assertSerializeParse(DeviceInfoConstants.SYS_LIBRARIES, "lib1;lib2;"); 85 } 86 87 private void assertSerializeParse(String name, String value) 88 throws IOException, ParseException { 89 DeviceInfoResult serializedInfo = new DeviceInfoResult(); 90 addMetric(name, value, serializedInfo); 91 String serializedOutput = serialize(serializedInfo); 92 mDeserializingInfo.parse(new StringReader(serializedOutput)); 93 assertEquals(value, mDeserializingInfo.getMetrics().get(name)); 94 } 95 96 /** 97 * Test populating a combined metric like device serial 98 */ 99 public void testPopulateMetrics_combinedSerial() throws Exception { 100 DeviceInfoResult info = new DeviceInfoResult(); 101 // first add another metric to make hashmap non empty, so combined logic is triggered 102 addMetric(DeviceInfoConstants.PROCESSES, "proc", info); 103 addMetric(DeviceInfoConstants.SERIAL_NUMBER, "device1", info); 104 // ensure the stored serial number equals the value that was just set 105 assertEquals("device1", info.getMetrics().get( 106 DeviceInfoConstants.SERIAL_NUMBER)); 107 // now add it again 108 addMetric(DeviceInfoConstants.SERIAL_NUMBER, "device1", info); 109 // should still equal same value 110 assertEquals("device1", info.getMetrics().get( 111 DeviceInfoConstants.SERIAL_NUMBER)); 112 // now store different serial, and expect csv 113 addMetric(DeviceInfoConstants.SERIAL_NUMBER, "device2", info); 114 assertEquals("device1,device2", info.getMetrics().get( 115 DeviceInfoConstants.SERIAL_NUMBER)); 116 } 117 118 /** 119 * Test populating a verified-to-be-identical metric like DeviceInfoConstants.BUILD_FINGERPRINT 120 */ 121 public void testPopulateMetrics_verify() throws Exception { 122 DeviceInfoResult info = new DeviceInfoResult(); 123 addMetric(DeviceInfoConstants.BUILD_FINGERPRINT, "fingerprint1", info); 124 // ensure the stored fingerprint equals the value that was just set 125 assertEquals("fingerprint1", info.getMetrics().get( 126 DeviceInfoConstants.BUILD_FINGERPRINT)); 127 // now add it again 128 addMetric(DeviceInfoConstants.BUILD_FINGERPRINT, "fingerprint1", info); 129 // should still equal same value 130 assertEquals("fingerprint1", info.getMetrics().get( 131 DeviceInfoConstants.BUILD_FINGERPRINT)); 132 // now store different serial, and expect error message 133 addMetric(DeviceInfoConstants.BUILD_FINGERPRINT, "fingerprint2", info); 134 assertTrue(info.getMetrics().get( 135 DeviceInfoConstants.BUILD_FINGERPRINT).contains("ERROR")); 136 } 137 138 /** 139 * Helper method to add given metric to the {@link DeviceInfoResult} 140 */ 141 private void addMetric(String metricName, String metricValue, DeviceInfoResult serializedInfo) { 142 Map<String, String> collectedMetrics = new HashMap<String, String>(); 143 collectedMetrics.put(metricName, metricValue); 144 serializedInfo.populateMetrics(collectedMetrics); 145 } 146 147 /** 148 * Helper method to serialize given object to XML 149 */ 150 private String serialize(DeviceInfoResult serializedInfo) 151 throws IOException { 152 KXmlSerializer xmlSerializer = new KXmlSerializer(); 153 StringWriter serializedOutput = new StringWriter(); 154 xmlSerializer.setOutput(serializedOutput); 155 serializedInfo.serialize(xmlSerializer); 156 return serializedOutput.toString(); 157 } 158 } 159