NetworkStatsTest.java revision 3f3913550c10792edb8aecf66cc83c3db5c8b311
1/*
2 * Copyright (C) 2011 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
17package android.net;
18
19import android.os.SystemClock;
20import android.test.suitebuilder.annotation.SmallTest;
21
22import junit.framework.TestCase;
23
24@SmallTest
25public class NetworkStatsTest extends TestCase {
26
27    private static final String TEST_IFACE = "test0";
28
29    public void testFindIndex() throws Exception {
30        final NetworkStats stats = new NetworkStats.Builder(SystemClock.elapsedRealtime(), 3)
31                .addEntry(TEST_IFACE, 100, 1024, 0)
32                .addEntry(TEST_IFACE, 101, 0, 1024)
33                .addEntry(TEST_IFACE, 102, 1024, 1024).build();
34
35        assertEquals(2, stats.findIndex(TEST_IFACE, 102));
36        assertEquals(2, stats.findIndex(TEST_IFACE, 102));
37        assertEquals(0, stats.findIndex(TEST_IFACE, 100));
38        assertEquals(-1, stats.findIndex(TEST_IFACE, 6));
39    }
40
41    public void testSubtractIdenticalData() throws Exception {
42        final NetworkStats before = new NetworkStats.Builder(SystemClock.elapsedRealtime(), 2)
43                .addEntry(TEST_IFACE, 100, 1024, 0)
44                .addEntry(TEST_IFACE, 101, 0, 1024).build();
45
46        final NetworkStats after = new NetworkStats.Builder(SystemClock.elapsedRealtime(), 2)
47                .addEntry(TEST_IFACE, 100, 1024, 0)
48                .addEntry(TEST_IFACE, 101, 0, 1024).build();
49
50        final NetworkStats result = after.subtract(before);
51
52        // identical data should result in zero delta
53        assertEquals(0, result.rx[0]);
54        assertEquals(0, result.tx[0]);
55        assertEquals(0, result.rx[1]);
56        assertEquals(0, result.tx[1]);
57    }
58
59    public void testSubtractIdenticalRows() throws Exception {
60        final NetworkStats before = new NetworkStats.Builder(SystemClock.elapsedRealtime(), 2)
61                .addEntry(TEST_IFACE, 100, 1024, 0)
62                .addEntry(TEST_IFACE, 101, 0, 1024).build();
63
64        final NetworkStats after = new NetworkStats.Builder(SystemClock.elapsedRealtime(), 2)
65                .addEntry(TEST_IFACE, 100, 1025, 2)
66                .addEntry(TEST_IFACE, 101, 3, 1028).build();
67
68        final NetworkStats result = after.subtract(before);
69
70        // expect delta between measurements
71        assertEquals(1, result.rx[0]);
72        assertEquals(2, result.tx[0]);
73        assertEquals(3, result.rx[1]);
74        assertEquals(4, result.tx[1]);
75    }
76
77    public void testSubtractNewRows() throws Exception {
78        final NetworkStats before = new NetworkStats.Builder(SystemClock.elapsedRealtime(), 2)
79                .addEntry(TEST_IFACE, 100, 1024, 0)
80                .addEntry(TEST_IFACE, 101, 0, 1024).build();
81
82        final NetworkStats after = new NetworkStats.Builder(SystemClock.elapsedRealtime(), 3)
83                .addEntry(TEST_IFACE, 100, 1024, 0)
84                .addEntry(TEST_IFACE, 101, 0, 1024)
85                .addEntry(TEST_IFACE, 102, 1024, 1024).build();
86
87        final NetworkStats result = after.subtract(before);
88
89        // its okay to have new rows
90        assertEquals(0, result.rx[0]);
91        assertEquals(0, result.tx[0]);
92        assertEquals(0, result.rx[1]);
93        assertEquals(0, result.tx[1]);
94        assertEquals(1024, result.rx[2]);
95        assertEquals(1024, result.tx[2]);
96    }
97
98}
99