1 /* 2 * Copyright (C) 2018 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.regression.tests; 17 18 import static org.junit.Assert.assertEquals; 19 import static org.mockito.ArgumentMatchers.anyString; 20 import static org.mockito.Mockito.spy; 21 import static org.mockito.Mockito.times; 22 import static org.mockito.Mockito.verify; 23 24 import com.android.tradefed.result.TestDescription; 25 import com.android.tradefed.util.Pair; 26 27 import com.google.common.primitives.Doubles; 28 29 import org.junit.Before; 30 import org.junit.Test; 31 import org.junit.runner.RunWith; 32 import org.junit.runners.JUnit4; 33 34 import java.util.Arrays; 35 import java.util.HashMap; 36 import java.util.List; 37 import java.util.Map; 38 39 /** Unit tests for {@link Metrics}. */ 40 @RunWith(JUnit4.class) 41 public class MetricsTest { 42 43 private Metrics mMetrics; 44 45 @Before setUp()46 public void setUp() throws Exception { 47 mMetrics = spy(new Metrics(false)); 48 } 49 50 @Test testAddRunMetrics()51 public void testAddRunMetrics() { 52 Map<String, List<String>> data = new HashMap<>(); 53 data.put("metric1", Arrays.asList("1.0", "1.1", "1.2")); 54 data.put("metric2", Arrays.asList("2.0", "2.1", "2.2")); 55 data.forEach((k, v) -> v.forEach(e -> mMetrics.addRunMetric(k, e))); 56 assertEquals(Doubles.asList(1.0, 1.1, 1.2), mMetrics.getRunMetrics().get("metric1")); 57 assertEquals(Doubles.asList(2.0, 2.1, 2.2), mMetrics.getRunMetrics().get("metric2")); 58 } 59 60 @Test testAddTestMetrics()61 public void testAddTestMetrics() { 62 TestDescription id1 = new TestDescription("class", "test1"); 63 Arrays.asList("1.0", "1.1", "1.2").forEach(e -> mMetrics.addTestMetric(id1, "metric1", e)); 64 TestDescription id2 = new TestDescription("class", "test2"); 65 Arrays.asList("2.0", "2.1", "2.2").forEach(e -> mMetrics.addTestMetric(id2, "metric1", e)); 66 Arrays.asList("3.0", "3.1", "3.2").forEach(e -> mMetrics.addTestMetric(id2, "metric2", e)); 67 68 assertEquals( 69 Doubles.asList(1.0, 1.1, 1.2), 70 mMetrics.getTestMetrics().get(new Pair<>(id1, "metric1"))); 71 assertEquals( 72 Doubles.asList(2.0, 2.1, 2.2), 73 mMetrics.getTestMetrics().get(new Pair<>(id2, "metric1"))); 74 assertEquals( 75 Doubles.asList(3.0, 3.1, 3.2), 76 mMetrics.getTestMetrics().get(new Pair<>(id2, "metric2"))); 77 } 78 79 @Test testValidate()80 public void testValidate() { 81 Map<String, List<String>> data = new HashMap<>(); 82 data.put("metric1", Arrays.asList("1.0", "1.1", "1.2")); 83 data.put("metric2", Arrays.asList("2.0", "2.1")); 84 data.forEach((k, v) -> v.forEach(e -> mMetrics.addRunMetric(k, e))); 85 TestDescription id1 = new TestDescription("class", "test1"); 86 Arrays.asList("1.0", "1.1", "1.2").forEach(e -> mMetrics.addTestMetric(id1, "metric1", e)); 87 TestDescription id2 = new TestDescription("class", "test2"); 88 Arrays.asList("2.0", "2.1", "2.2").forEach(e -> mMetrics.addTestMetric(id2, "metric1", e)); 89 Arrays.asList("3.0", "3.1").forEach(e -> mMetrics.addTestMetric(id2, "metric2", e)); 90 mMetrics.validate(3); 91 verify(mMetrics, times(2)).error(anyString()); 92 } 93 94 @Test testCrossValidate()95 public void testCrossValidate() { 96 Metrics other = new Metrics(false); 97 Arrays.asList("1.0", "1.1", "1.2") 98 .forEach( 99 e -> { 100 mMetrics.addRunMetric("metric1", e); 101 other.addRunMetric("metric1", e); 102 }); 103 Arrays.asList("2.0", "2.1", "2.2").forEach(e -> mMetrics.addRunMetric("metric2", e)); 104 Arrays.asList("2.0", "2.1", "2.2").forEach(e -> other.addRunMetric("metric5", e)); 105 TestDescription id1 = new TestDescription("class", "test1"); 106 Arrays.asList("1.0", "1.1", "1.2") 107 .forEach( 108 e -> { 109 mMetrics.addTestMetric(id1, "metric1", e); 110 other.addTestMetric(id1, "metric1", e); 111 }); 112 Arrays.asList("3.0", "3.1", "3.3").forEach(e -> mMetrics.addTestMetric(id1, "metric6", e)); 113 TestDescription id2 = new TestDescription("class", "test2"); 114 Arrays.asList("2.0", "2.1", "2.2") 115 .forEach( 116 e -> { 117 mMetrics.addTestMetric(id2, "metric1", e); 118 other.addTestMetric(id2, "metric1", e); 119 }); 120 Arrays.asList("3.0", "3.1", "3.3").forEach(e -> other.addTestMetric(id2, "metric2", e)); 121 mMetrics.crossValidate(other); 122 verify(mMetrics, times(1)).warn("Run metric \"metric2\" only in before-patch run."); 123 verify(mMetrics, times(1)).warn("Run metric \"metric5\" only in after-patch run."); 124 verify(mMetrics, times(1)) 125 .warn( 126 String.format( 127 "Test %s metric \"metric6\" only in before-patch run.", 128 id1.toString())); 129 verify(mMetrics, times(1)) 130 .warn( 131 String.format( 132 "Test %s metric \"metric2\" only in after-patch run.", 133 id2.toString())); 134 } 135 } 136