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 17 package com.android.compatibility.common.util; 18 19 import junit.framework.TestCase; 20 21 import org.xmlpull.v1.XmlPullParserFactory; 22 import org.xmlpull.v1.XmlSerializer; 23 24 import java.io.ByteArrayOutputStream; 25 import java.io.IOException; 26 27 //TODO(stuartscott): Delete file for v2, ReportLog can serialize itself. 28 /** 29 * Unit tests for {@link MetricsXmlSerializer} 30 */ 31 public class MetricsXmlSerializerTest extends TestCase { 32 33 static class LocalReportLog extends ReportLog {} 34 private static final double[] VALUES = new double[] {1, 11, 21, 1211, 111221}; 35 private static final String HEADER = "<?xml version='1.0' encoding='utf-8' standalone='yes' ?>"; 36 private static final String EXPECTED_XML = HEADER 37 + "<Summary message=\"Sample\" scoreType=\"higher_better\" unit=\"byte\">1.0</Summary>"; 38 39 private LocalReportLog mLocalReportLog; 40 private MetricsXmlSerializer mMetricsXmlSerializer; 41 private ByteArrayOutputStream mByteArrayOutputStream; 42 private XmlSerializer xmlSerializer; 43 44 @Override setUp()45 public void setUp() throws Exception { 46 mLocalReportLog = new LocalReportLog(); 47 mByteArrayOutputStream = new ByteArrayOutputStream(); 48 XmlPullParserFactory factory = XmlPullParserFactory.newInstance(null, null); 49 xmlSerializer = factory.newSerializer(); 50 xmlSerializer.setOutput(mByteArrayOutputStream, "utf-8"); 51 52 this.mMetricsXmlSerializer = new MetricsXmlSerializer(xmlSerializer); 53 } 54 testSerialize_null()55 public void testSerialize_null() throws IOException { 56 xmlSerializer.startDocument("utf-8", true); 57 mMetricsXmlSerializer.serialize(null); 58 xmlSerializer.endDocument(); 59 60 assertEquals(HEADER.length(), mByteArrayOutputStream.toByteArray().length); 61 } 62 testSerialize_noData()63 public void testSerialize_noData() throws IOException { 64 xmlSerializer.startDocument("utf-8", true); 65 mMetricsXmlSerializer.serialize(mLocalReportLog); 66 xmlSerializer.endDocument(); 67 68 assertEquals(HEADER.length(), mByteArrayOutputStream.toByteArray().length); 69 } 70 testSerialize()71 public void testSerialize() throws IOException { 72 mLocalReportLog.setSummary("Sample", 1.0, ResultType.HIGHER_BETTER, ResultUnit.BYTE); 73 mLocalReportLog.addValues("Details", VALUES, ResultType.NEUTRAL, ResultUnit.FPS); 74 75 xmlSerializer.startDocument("utf-8", true); 76 mMetricsXmlSerializer.serialize(mLocalReportLog); 77 xmlSerializer.endDocument(); 78 79 assertEquals(EXPECTED_XML, mByteArrayOutputStream.toString("utf-8")); 80 } 81 }