1 /*
2  * Copyright (C) 2022 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 android.net.netstats
18 
19 import android.net.NetworkIdentity
20 import android.net.NetworkStatsCollection
21 import android.net.NetworkStatsHistory
22 import androidx.test.filters.SmallTest
23 import com.android.testutils.ConnectivityModuleTest
24 import com.android.testutils.DevSdkIgnoreRule
25 import com.android.testutils.SC_V2
26 import org.junit.Rule
27 import org.junit.Test
28 import org.junit.runner.RunWith
29 import org.junit.runners.JUnit4
30 import kotlin.test.assertEquals
31 import kotlin.test.fail
32 
33 @ConnectivityModuleTest
34 @RunWith(JUnit4::class)
35 @SmallTest
36 class NetworkStatsCollectionTest {
37     @Rule
38     @JvmField
39     val ignoreRule = DevSdkIgnoreRule(ignoreClassUpTo = SC_V2)
40 
41     @Test
testBuildernull42     fun testBuilder() {
43         val ident = setOf<NetworkIdentity>()
44         val key1 = NetworkStatsCollection.Key(ident, /* uid */ 0, /* set */ 0, /* tag */ 0)
45         val key2 = NetworkStatsCollection.Key(ident, /* uid */ 1, /* set */ 0, /* tag */ 0)
46         val bucketDuration = 10L
47         val entry1 = NetworkStatsHistory.Entry(10, 10, 40, 4, 50, 5, 60)
48         val entry2 = NetworkStatsHistory.Entry(30, 10, 3, 41, 7, 1, 0)
49         val history1 = NetworkStatsHistory.Builder(10, 5)
50                 .addEntry(entry1)
51                 .addEntry(entry2)
52                 .build()
53         val history2 = NetworkStatsHistory(10, 5)
54         val actualCollection = NetworkStatsCollection.Builder(bucketDuration)
55                 .addEntry(key1, history1)
56                 .addEntry(key2, history2)
57                 .build()
58 
59         // The builder will omit any entry with empty history. Thus, only history1
60         // is expected in the result collection.
61         val actualEntries = actualCollection.entries
62         assertEquals(1, actualEntries.size)
63         val actualHistory = actualEntries[key1] ?: fail("There should be an entry for $key1")
64         assertEquals(history1.entries, actualHistory.entries)
65     }
66 }
67