1 /* 2 * Copyright (C) 2015 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 com.android.compatibility.common.util.ReportLog.Metric; 20 21 import junit.framework.TestCase; 22 23 import java.util.Arrays; 24 25 /** 26 * Unit tests for {@link ReportLog} 27 */ 28 public class ReportLogTest extends TestCase { 29 30 private static final String SOURCE = "Source"; 31 private static final String MESSAGE = "Message"; 32 private static final double[] VALUES = new double[] {.1, 124, 4736, 835.683, 98, 395}; 33 private static final String HEADER_XML = 34 "<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>"; 35 private static final String SUMMARY_XML = 36 HEADER_XML + "\r\n" + 37 "<Summary>\r\n" + 38 " <Metric source=\"com.android.compatibility.common.util.ReportLogTest#%s\" " 39 + "message=\"Sample\" score_type=\"higher_better\" score_unit=\"byte\">\r\n" + 40 " <Value>1.0</Value>\r\n" + 41 " </Metric>\r\n" + 42 "</Summary>"; 43 private static final String FULL_XML = SUMMARY_XML; 44 45 private ReportLog mReportLog; 46 47 @Override setUp()48 protected void setUp() throws Exception { 49 mReportLog = new ReportLog(); 50 } 51 testSerialize_null()52 public void testSerialize_null() throws Exception { 53 try { 54 ReportLog.serialize(null); 55 fail("Expected IllegalArgumentException when serializing an empty report"); 56 } catch (IllegalArgumentException e) { 57 // Expected 58 } 59 } 60 testSerialize_noData()61 public void testSerialize_noData() throws Exception { 62 ReportLog.serialize(mReportLog); 63 } 64 testSerialize_summaryOnly()65 public void testSerialize_summaryOnly() throws Exception { 66 mReportLog.setSummary("Sample", 1.0, ResultType.HIGHER_BETTER, ResultUnit.BYTE); 67 assertEquals(String.format(SUMMARY_XML, "testSerialize_summaryOnly:66"), 68 ReportLog.serialize(mReportLog)); 69 } 70 testSerialize_detailOnly()71 public void testSerialize_detailOnly() throws Exception { 72 mReportLog.addValues("Details", VALUES, ResultType.NEUTRAL, ResultUnit.FPS); 73 assertEquals(HEADER_XML, ReportLog.serialize(mReportLog)); 74 } 75 testSerialize_full()76 public void testSerialize_full() throws Exception { 77 mReportLog.setSummary("Sample", 1.0, ResultType.HIGHER_BETTER, ResultUnit.BYTE); 78 mReportLog.addValues("Details", VALUES, ResultType.NEUTRAL, ResultUnit.FPS); 79 assertEquals(String.format(FULL_XML, "testSerialize_full:77"), 80 ReportLog.serialize(mReportLog)); 81 } 82 testParse_null()83 public void testParse_null() throws Exception { 84 try { 85 ReportLog.parse((String) null); 86 fail("Expected IllegalArgumentException when passing a null report"); 87 } catch(IllegalArgumentException e) { 88 // Expected 89 } 90 } 91 testParse_noData()92 public void testParse_noData() throws Exception { 93 ReportLog report = ReportLog.parse(HEADER_XML); 94 assertNull(report.getSummary()); 95 } 96 testParse_summaryOnly()97 public void testParse_summaryOnly() throws Exception { 98 ReportLog report = ReportLog.parse(String.format(SUMMARY_XML, "testParse_summaryOnly:125")); 99 assertNotNull(report); 100 assertEquals("Sample", report.getSummary().getMessage()); 101 } 102 testParse_full()103 public void testParse_full() throws Exception { 104 ReportLog report = ReportLog.parse(String.format(FULL_XML, "testParse_full:140")); 105 assertNotNull(report); 106 assertEquals("Sample", report.getSummary().getMessage()); 107 } 108 testLimits_source()109 public void testLimits_source() throws Exception { 110 // Should pass with a short source. 111 Metric metric = new Metric(SOURCE, MESSAGE, 1.0, ResultType.HIGHER_BETTER, ResultUnit.BYTE); 112 assertEquals("Expected message to be ok", SOURCE, metric.getSource()); 113 // Make a long source. 114 StringBuilder sb = new StringBuilder(); 115 // 40 x "Source" = 240 character string 116 for (int i = 0; i < 40; i++) sb.append(SOURCE); 117 String source = sb.toString(); 118 // Should be trimmed because source is too long. 119 metric = new Metric(source, MESSAGE, 1.0, ResultType.HIGHER_BETTER, ResultUnit.BYTE); 120 assertEquals("Expected source to be trimmed", source.substring(source.length() - 200), 121 metric.getSource()); 122 } 123 testLimits_message()124 public void testLimits_message() throws Exception { 125 // Should pass with a short message. 126 Metric metric = new Metric(SOURCE, MESSAGE, 1.0, ResultType.HIGHER_BETTER, ResultUnit.BYTE); 127 assertEquals("Expected message to be ok", MESSAGE, metric.getMessage()); 128 // Make a long message. 129 StringBuilder sb = new StringBuilder(); 130 // 40 x "Message" = 280 character string 131 for (int i = 0; i < 40; i++) sb.append(MESSAGE); 132 String message = sb.toString(); 133 // Should be trimmed because message is too long. 134 metric = new Metric(SOURCE, message, 1.0, ResultType.HIGHER_BETTER, ResultUnit.BYTE); 135 assertEquals("Expected message to be trimmed", message.substring(0, 200), 136 metric.getMessage()); 137 } 138 testLimits_values()139 public void testLimits_values() throws Exception { 140 // Should pass with a small array. 141 Metric metric = new Metric(SOURCE, MESSAGE, VALUES, ResultType.HIGHER_BETTER, 142 ResultUnit.BYTE); 143 assertTrue("Expected values to be ok", Arrays.equals(VALUES, metric.getValues())); 144 // Make a big array. 145 double[] values = new double[1001]; 146 for (int i = 0; i < values.length; i++) values[i] = i; 147 // Should be trimmed because array is too big. 148 metric = new Metric(SOURCE, MESSAGE, values, ResultType.HIGHER_BETTER, ResultUnit.BYTE); 149 assertTrue("Expected values to be trimmed", Arrays.equals(Arrays.copyOf(values, 1000), 150 metric.getValues())); 151 } 152 153 } 154