1 /* 2 * Copyright (C) 2019 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.server.wifi; 18 19 import static org.junit.Assert.assertEquals; 20 21 import android.net.wifi.WifiManager; 22 23 import com.android.server.wifi.proto.nano.WifiMetricsProto.DeviceMobilityStatePnoScanStats; 24 import com.android.server.wifi.proto.nano.WifiMetricsProto.HistogramBucketInt32; 25 import com.android.server.wifi.proto.nano.WifiMetricsProto.Int32Count; 26 import com.android.server.wifi.proto.nano.WifiMetricsProto.LinkProbeStats.ExperimentProbeCounts; 27 import com.android.server.wifi.proto.nano.WifiMetricsProto.LinkProbeStats.LinkProbeFailureReasonCount; 28 import com.android.server.wifi.proto.nano.WifiMetricsProto.StaEvent; 29 30 import java.util.Arrays; 31 import java.util.Comparator; 32 33 /** 34 * Utility functions for {@link WifiMetricsTest}. 35 */ 36 public class WifiMetricsTestUtil { 37 38 /** 39 * Asserts that the two arrays are equal, reporting any difference between them. 40 * Note: The order of buckets in each array must match! 41 */ assertHistogramBucketsEqual(HistogramBucketInt32[] expected, HistogramBucketInt32[] actual)42 public static void assertHistogramBucketsEqual(HistogramBucketInt32[] expected, 43 HistogramBucketInt32[] actual) { 44 assertEquals("Number of buckets do not match!", expected.length, actual.length); 45 46 for (int i = 0; i < expected.length; i++) { 47 HistogramBucketInt32 expectedBucket = expected[i]; 48 HistogramBucketInt32 actualBucket = actual[i]; 49 50 assertEquals(String.format("Bucket[%d].start does not match!", i), 51 expectedBucket.start, actualBucket.start); 52 assertEquals(String.format("Bucket[%d].end does not match!", i), 53 expectedBucket.end, actualBucket.end); 54 assertEquals(String.format("Bucket[%d].count does not match!", i), 55 expectedBucket.count, actualBucket.count); 56 } 57 } 58 59 /** 60 * The constructor we wish HistogramBucketInt32 had. 61 */ buildHistogramBucketInt32(int start, int end, int count)62 public static HistogramBucketInt32 buildHistogramBucketInt32(int start, int end, int count) { 63 HistogramBucketInt32 bucket = new HistogramBucketInt32(); 64 bucket.start = start; 65 bucket.end = end; 66 bucket.count = count; 67 return bucket; 68 } 69 70 /** 71 * Asserts that the two arrays are equal, reporting any difference between them. 72 * Note: The order of key counts in each array must match! 73 */ assertKeyCountsEqual(Int32Count[] expected, Int32Count[] actual)74 public static void assertKeyCountsEqual(Int32Count[] expected, Int32Count[] actual) { 75 assertEquals("Number of key counts do not match!", expected.length, actual.length); 76 77 for (int i = 0; i < expected.length; i++) { 78 Int32Count expectedKeyCount = expected[i]; 79 Int32Count actualKeyCount = actual[i]; 80 81 assertEquals(String.format("KeyCount[%d].key does not match!", i), 82 expectedKeyCount.key, actualKeyCount.key); 83 assertEquals(String.format("KeyCount[%d].count does not match!", i), 84 expectedKeyCount.count, actualKeyCount.count); 85 } 86 } 87 88 /** 89 * The constructor we wish Int32Count had. 90 */ buildInt32Count(int key, int count)91 public static Int32Count buildInt32Count(int key, int count) { 92 Int32Count keyCount = new Int32Count(); 93 keyCount.key = key; 94 keyCount.count = count; 95 return keyCount; 96 } 97 98 /** 99 * Asserts that the two arrays are equal (ignoring order), 100 * reporting any difference between them. 101 */ assertLinkProbeFailureReasonCountsEqual( LinkProbeFailureReasonCount[] expected, LinkProbeFailureReasonCount[] actual)102 public static void assertLinkProbeFailureReasonCountsEqual( 103 LinkProbeFailureReasonCount[] expected, LinkProbeFailureReasonCount[] actual) { 104 assertEquals("Number of LinkProbeFailureReasonCounts do not match!", 105 expected.length, actual.length); 106 107 Arrays.sort(expected, Comparator.comparingInt(x -> x.failureReason)); 108 Arrays.sort(actual, Comparator.comparingInt(x -> x.failureReason)); 109 110 for (int i = 0; i < expected.length; i++) { 111 LinkProbeFailureReasonCount expectedFailureReasonCount = expected[i]; 112 LinkProbeFailureReasonCount actualFailureReasonCount = actual[i]; 113 114 assertEquals(String.format( 115 "LinkProbeFailureReasonCount[%d].failureReason does not match!", i), 116 expectedFailureReasonCount.failureReason, 117 actualFailureReasonCount.failureReason); 118 assertEquals(String.format("LinkProbeFailureReasonCount[%d].count does not match!", i), 119 expectedFailureReasonCount.count, actualFailureReasonCount.count); 120 } 121 } 122 123 /** 124 * The constructor we wish LinkProbeFailureReasonCount had. 125 */ buildLinkProbeFailureReasonCount(int failureReason, int count)126 public static LinkProbeFailureReasonCount buildLinkProbeFailureReasonCount(int failureReason, 127 int count) { 128 LinkProbeFailureReasonCount failureReasonCount = new LinkProbeFailureReasonCount(); 129 failureReasonCount.failureReason = failureReason; 130 failureReasonCount.count = count; 131 return failureReasonCount; 132 } 133 134 /** 135 * The constructor we wish DeviceMobilityStatePnoScanStats had. 136 */ buildDeviceMobilityStatePnoScanStats( @ifiManager.DeviceMobilityState int deviceMobilityState, int numTimesEnteredState, long totalDurationMs, long pnoDurationMs)137 public static DeviceMobilityStatePnoScanStats buildDeviceMobilityStatePnoScanStats( 138 @WifiManager.DeviceMobilityState int deviceMobilityState, int numTimesEnteredState, 139 long totalDurationMs, long pnoDurationMs) { 140 DeviceMobilityStatePnoScanStats stats = new DeviceMobilityStatePnoScanStats(); 141 stats.deviceMobilityState = deviceMobilityState; 142 stats.numTimesEnteredState = numTimesEnteredState; 143 stats.totalDurationMs = totalDurationMs; 144 stats.pnoDurationMs = pnoDurationMs; 145 return stats; 146 } 147 148 /** 149 * Asserts that the two arrays are equal (ignoring order), 150 * reporting any difference between them. 151 */ assertDeviceMobilityStatePnoScanStatsEqual( DeviceMobilityStatePnoScanStats[] expected, DeviceMobilityStatePnoScanStats[] actual)152 public static void assertDeviceMobilityStatePnoScanStatsEqual( 153 DeviceMobilityStatePnoScanStats[] expected, DeviceMobilityStatePnoScanStats[] actual) { 154 155 assertEquals("Number of DeviceMobilityStatePnoScanStats do not match!", 156 expected.length, actual.length); 157 158 Arrays.sort(expected, Comparator.comparingInt(x -> x.deviceMobilityState)); 159 Arrays.sort(actual, Comparator.comparingInt(x -> x.deviceMobilityState)); 160 161 for (int i = 0; i < expected.length; i++) { 162 DeviceMobilityStatePnoScanStats expectedStats = expected[i]; 163 DeviceMobilityStatePnoScanStats actualStats = actual[i]; 164 165 assertEquals(String.format( 166 "DeviceMobilityStatePnoScanStats[%d].deviceMobilityState does not match!", i), 167 expectedStats.deviceMobilityState, actualStats.deviceMobilityState); 168 assertEquals(String.format( 169 "DeviceMobilityStatePnoScanStats[%d].numTimesEnteredState does not match!", i), 170 expectedStats.numTimesEnteredState, actualStats.numTimesEnteredState); 171 assertEquals(String.format( 172 "DeviceMobilityStatePnoScanStats[%d].totalDurationMs does not match!", i), 173 expectedStats.totalDurationMs, actualStats.totalDurationMs); 174 assertEquals(String.format( 175 "DeviceMobilityStatePnoScanStats[%d].pnoDurationMs does not match!", i), 176 expectedStats.pnoDurationMs, actualStats.pnoDurationMs); 177 } 178 } 179 180 /** 181 * Creates a StaEvent of type TYPE_LINK_PROBE that was successful. 182 */ buildLinkProbeSuccessStaEvent(int elapsedTimeMs)183 public static StaEvent buildLinkProbeSuccessStaEvent(int elapsedTimeMs) { 184 StaEvent probe = new StaEvent(); 185 probe.type = StaEvent.TYPE_LINK_PROBE; 186 probe.linkProbeWasSuccess = true; 187 probe.linkProbeSuccessElapsedTimeMs = elapsedTimeMs; 188 probe.interfaceName = WifiMetricsTest.TEST_IFACE_NAME; 189 return probe; 190 } 191 192 /** 193 * Creates a StaEvent of type TYPE_LINK_PROBE that failed. 194 */ buildLinkProbeFailureStaEvent(int reason)195 public static StaEvent buildLinkProbeFailureStaEvent(int reason) { 196 StaEvent probe = new StaEvent(); 197 probe.type = StaEvent.TYPE_LINK_PROBE; 198 probe.linkProbeWasSuccess = false; 199 probe.linkProbeFailureReason = reason; 200 probe.interfaceName = WifiMetricsTest.TEST_IFACE_NAME; 201 return probe; 202 } 203 204 /** 205 * Asserts that the two arrays are equal, reporting any difference between them. 206 * Note that the order must match. 207 */ assertLinkProbeStaEventsEqual(StaEvent[] expected, StaEvent[] actual)208 public static void assertLinkProbeStaEventsEqual(StaEvent[] expected, StaEvent[] actual) { 209 assertEquals("Number of StaEvents do not match!", expected.length, actual.length); 210 211 for (int i = 0; i < expected.length; i++) { 212 StaEvent expectedEvent = expected[i]; 213 StaEvent actualEvent = actual[i]; 214 215 assertEquals(String.format("expected StaEvent[%d].type != TYPE_LINK_PROBE", i), 216 StaEvent.TYPE_LINK_PROBE, expectedEvent.type); 217 assertEquals(String.format("actual StaEvent[%d].type != TYPE_LINK_PROBE", i), 218 StaEvent.TYPE_LINK_PROBE, actualEvent.type); 219 assertEquals(String.format("StaEvent[%d].linkProbeWasSuccess does not match!", i), 220 expectedEvent.linkProbeWasSuccess, actualEvent.linkProbeWasSuccess); 221 if (expectedEvent.linkProbeWasSuccess) { 222 assertEquals(String.format( 223 "StaEvent[%d].linkProbeSuccessElapsedTimeMs does not match!", i), 224 expectedEvent.linkProbeSuccessElapsedTimeMs, 225 actualEvent.linkProbeSuccessElapsedTimeMs); 226 } else { 227 assertEquals(String.format( 228 "StaEvent[%d].linkProbeFailureReason does not match!", i), 229 expectedEvent.linkProbeFailureReason, actualEvent.linkProbeFailureReason); 230 } 231 } 232 } 233 234 /** 235 * The constructor we wish ExperimentProbeCounts had. 236 */ buildExperimentProbeCounts( String experimentId, int probeCount)237 public static ExperimentProbeCounts buildExperimentProbeCounts( 238 String experimentId, int probeCount) { 239 ExperimentProbeCounts counts = new ExperimentProbeCounts(); 240 counts.experimentId = experimentId; 241 counts.probeCount = probeCount; 242 return counts; 243 } 244 245 /** 246 * Asserts that the two arrays are equal (ignoring order), 247 * reporting any difference between them. 248 */ assertExperimentProbeCountsEqual( ExperimentProbeCounts[] expected, ExperimentProbeCounts[] actual)249 public static void assertExperimentProbeCountsEqual( 250 ExperimentProbeCounts[] expected, ExperimentProbeCounts[] actual) { 251 252 assertEquals("Number of ExperimentProbeCounts do not match!", 253 expected.length, actual.length); 254 255 Arrays.sort(expected, Comparator.comparing(x -> x.experimentId)); 256 Arrays.sort(actual, Comparator.comparing(x -> x.experimentId)); 257 258 for (int i = 0; i < expected.length; i++) { 259 ExperimentProbeCounts expectedCounts = expected[i]; 260 ExperimentProbeCounts actualCounts = actual[i]; 261 262 assertEquals(String.format( 263 "ExperimentProbeCounts[%d].experimentId does not match!", i), 264 expectedCounts.experimentId, actualCounts.experimentId); 265 assertEquals(String.format( 266 "ExperimentProbeCounts[%d].probeCount does not match!", i), 267 expectedCounts.probeCount, actualCounts.probeCount); 268 } 269 } 270 } 271