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.nano.WifiMetricsProto.DeviceMobilityStatePnoScanStats;
24 import com.android.server.wifi.nano.WifiMetricsProto.HistogramBucketInt32;
25 import com.android.server.wifi.nano.WifiMetricsProto.Int32Count;
26 import com.android.server.wifi.nano.WifiMetricsProto.LinkProbeStats.ExperimentProbeCounts;
27 import com.android.server.wifi.nano.WifiMetricsProto.LinkProbeStats.LinkProbeFailureReasonCount;
28 import com.android.server.wifi.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         return probe;
189     }
190 
191     /**
192      * Creates a StaEvent of type TYPE_LINK_PROBE that failed.
193      */
buildLinkProbeFailureStaEvent(int reason)194     public static StaEvent buildLinkProbeFailureStaEvent(int reason) {
195         StaEvent probe = new StaEvent();
196         probe.type = StaEvent.TYPE_LINK_PROBE;
197         probe.linkProbeWasSuccess = false;
198         probe.linkProbeFailureReason = reason;
199         return probe;
200     }
201 
202     /**
203      * Asserts that the two arrays are equal, reporting any difference between them.
204      * Note that the order must match.
205      */
assertLinkProbeStaEventsEqual(StaEvent[] expected, StaEvent[] actual)206     public static void assertLinkProbeStaEventsEqual(StaEvent[] expected, StaEvent[] actual) {
207         assertEquals("Number of StaEvents do not match!", expected.length, actual.length);
208 
209         for (int i = 0; i < expected.length; i++) {
210             StaEvent expectedEvent = expected[i];
211             StaEvent actualEvent = actual[i];
212 
213             assertEquals(String.format("expected StaEvent[%d].type != TYPE_LINK_PROBE", i),
214                     StaEvent.TYPE_LINK_PROBE, expectedEvent.type);
215             assertEquals(String.format("actual StaEvent[%d].type != TYPE_LINK_PROBE", i),
216                     StaEvent.TYPE_LINK_PROBE, actualEvent.type);
217             assertEquals(String.format("StaEvent[%d].linkProbeWasSuccess does not match!", i),
218                     expectedEvent.linkProbeWasSuccess, actualEvent.linkProbeWasSuccess);
219             if (expectedEvent.linkProbeWasSuccess) {
220                 assertEquals(String.format(
221                         "StaEvent[%d].linkProbeSuccessElapsedTimeMs does not match!", i),
222                         expectedEvent.linkProbeSuccessElapsedTimeMs,
223                         actualEvent.linkProbeSuccessElapsedTimeMs);
224             } else {
225                 assertEquals(String.format(
226                         "StaEvent[%d].linkProbeFailureReason does not match!", i),
227                         expectedEvent.linkProbeFailureReason, actualEvent.linkProbeFailureReason);
228             }
229         }
230     }
231 
232     /**
233      * The constructor we wish ExperimentProbeCounts had.
234      */
buildExperimentProbeCounts( String experimentId, int probeCount)235     public static ExperimentProbeCounts buildExperimentProbeCounts(
236             String experimentId, int probeCount) {
237         ExperimentProbeCounts counts = new ExperimentProbeCounts();
238         counts.experimentId = experimentId;
239         counts.probeCount = probeCount;
240         return counts;
241     }
242 
243     /**
244      * Asserts that the two arrays are equal (ignoring order),
245      * reporting any difference between them.
246      */
assertExperimentProbeCountsEqual( ExperimentProbeCounts[] expected, ExperimentProbeCounts[] actual)247     public static void assertExperimentProbeCountsEqual(
248             ExperimentProbeCounts[] expected, ExperimentProbeCounts[] actual) {
249 
250         assertEquals("Number of ExperimentProbeCounts do not match!",
251                 expected.length, actual.length);
252 
253         Arrays.sort(expected, Comparator.comparing(x -> x.experimentId));
254         Arrays.sort(actual, Comparator.comparing(x -> x.experimentId));
255 
256         for (int i = 0; i < expected.length; i++) {
257             ExperimentProbeCounts expectedCounts = expected[i];
258             ExperimentProbeCounts actualCounts = actual[i];
259 
260             assertEquals(String.format(
261                     "ExperimentProbeCounts[%d].experimentId does not match!", i),
262                     expectedCounts.experimentId, actualCounts.experimentId);
263             assertEquals(String.format(
264                     "ExperimentProbeCounts[%d].probeCount does not match!", i),
265                     expectedCounts.probeCount, actualCounts.probeCount);
266         }
267     }
268 }
269