1 /*
<lambda>null2  * Copyright (C) 2020 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.testutils
18 
19 import android.net.NetworkStats
20 import kotlin.test.assertTrue
21 
22 @JvmOverloads
23 fun orderInsensitiveEquals(
24     leftStats: NetworkStats,
25     rightStats: NetworkStats,
26     compareTime: Boolean = false
27 ): Boolean {
28     if (leftStats == rightStats) return true
29     if (compareTime && leftStats.getElapsedRealtime() != rightStats.getElapsedRealtime()) {
30         return false
31     }
32 
33     // While operations such as add/subtract will preserve empty entries. This will make
34     // the result be hard to verify during test. Remove them before comparing since they
35     // are not really affect correctness.
36     // TODO (b/152827872): Remove empty entries after addition/subtraction.
37     val leftTrimmedEmpty = leftStats.removeEmptyEntries()
38     val rightTrimmedEmpty = rightStats.removeEmptyEntries()
39 
40     if (leftTrimmedEmpty.size() != rightTrimmedEmpty.size()) return false
41     val left = NetworkStats.Entry()
42     val right = NetworkStats.Entry()
43     // Order insensitive compare.
44     for (i in 0 until leftTrimmedEmpty.size()) {
45         leftTrimmedEmpty.getValues(i, left)
46         val j: Int = rightTrimmedEmpty.findIndexHinted(left.iface, left.uid, left.set, left.tag,
47                 left.metered, left.roaming, left.defaultNetwork, i)
48         if (j == -1) return false
49         rightTrimmedEmpty.getValues(j, right)
50         if (left != right) return false
51     }
52     return true
53 }
54 
55 /**
56  * Assert that two {@link NetworkStats} are equals, assuming the order of the records are not
57  * necessarily the same.
58  *
59  * @note {@code elapsedRealtime} is not compared by default, given that in test cases that is not
60  *       usually used.
61  */
62 @JvmOverloads
assertNetworkStatsEqualsnull63 fun assertNetworkStatsEquals(
64     expected: NetworkStats,
65     actual: NetworkStats,
66     compareTime: Boolean = false
67 ) {
68     assertTrue(orderInsensitiveEquals(expected, actual, compareTime),
69             "expected: " + expected + " but was: " + actual)
70 }
71 
72 /**
73  * Assert that after being parceled then unparceled, {@link NetworkStats} is equal to the original
74  * object.
75  */
assertParcelingIsLosslessnull76 fun assertParcelingIsLossless(stats: NetworkStats) {
77     assertParcelingIsLossless(stats, { a, b -> orderInsensitiveEquals(a, b) })
78 }
79