NetworkStatsTest.java revision 4a97122ebf4d92a3f94402041729d77905e6c0c0
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.test.suitebuilder.annotation.SmallTest; 20 21import junit.framework.TestCase; 22 23@SmallTest 24public class NetworkStatsTest extends TestCase { 25 26 private static final String TEST_IFACE = "test0"; 27 private static final int TEST_UID = 1001; 28 private static final long TEST_START = 1194220800000L; 29 30 public void testFindIndex() throws Exception { 31 final NetworkStats stats = new NetworkStats(TEST_START, 3) 32 .addEntry(TEST_IFACE, 100, 1024, 0) 33 .addEntry(TEST_IFACE, 101, 0, 1024) 34 .addEntry(TEST_IFACE, 102, 1024, 1024); 35 36 assertEquals(2, stats.findIndex(TEST_IFACE, 102)); 37 assertEquals(2, stats.findIndex(TEST_IFACE, 102)); 38 assertEquals(0, stats.findIndex(TEST_IFACE, 100)); 39 assertEquals(-1, stats.findIndex(TEST_IFACE, 6)); 40 } 41 42 public void testAddEntryGrow() throws Exception { 43 final NetworkStats stats = new NetworkStats(TEST_START, 2); 44 45 assertEquals(0, stats.size); 46 assertEquals(2, stats.iface.length); 47 48 stats.addEntry(TEST_IFACE, TEST_UID, 1L, 2L); 49 stats.addEntry(TEST_IFACE, TEST_UID, 2L, 2L); 50 51 assertEquals(2, stats.size); 52 assertEquals(2, stats.iface.length); 53 54 stats.addEntry(TEST_IFACE, TEST_UID, 3L, 4L); 55 stats.addEntry(TEST_IFACE, TEST_UID, 4L, 4L); 56 stats.addEntry(TEST_IFACE, TEST_UID, 5L, 5L); 57 58 assertEquals(5, stats.size); 59 assertTrue(stats.iface.length >= 5); 60 61 assertEquals(1L, stats.rx[0]); 62 assertEquals(2L, stats.rx[1]); 63 assertEquals(3L, stats.rx[2]); 64 assertEquals(4L, stats.rx[3]); 65 assertEquals(5L, stats.rx[4]); 66 } 67 68 public void testSubtractIdenticalData() throws Exception { 69 final NetworkStats before = new NetworkStats(TEST_START, 2) 70 .addEntry(TEST_IFACE, 100, 1024, 0) 71 .addEntry(TEST_IFACE, 101, 0, 1024); 72 73 final NetworkStats after = new NetworkStats(TEST_START, 2) 74 .addEntry(TEST_IFACE, 100, 1024, 0) 75 .addEntry(TEST_IFACE, 101, 0, 1024); 76 77 final NetworkStats result = after.subtract(before); 78 79 // identical data should result in zero delta 80 assertEquals(0, result.rx[0]); 81 assertEquals(0, result.tx[0]); 82 assertEquals(0, result.rx[1]); 83 assertEquals(0, result.tx[1]); 84 } 85 86 public void testSubtractIdenticalRows() throws Exception { 87 final NetworkStats before = new NetworkStats(TEST_START, 2) 88 .addEntry(TEST_IFACE, 100, 1024, 0) 89 .addEntry(TEST_IFACE, 101, 0, 1024); 90 91 final NetworkStats after = new NetworkStats(TEST_START, 2) 92 .addEntry(TEST_IFACE, 100, 1025, 2) 93 .addEntry(TEST_IFACE, 101, 3, 1028); 94 95 final NetworkStats result = after.subtract(before); 96 97 // expect delta between measurements 98 assertEquals(1, result.rx[0]); 99 assertEquals(2, result.tx[0]); 100 assertEquals(3, result.rx[1]); 101 assertEquals(4, result.tx[1]); 102 } 103 104 public void testSubtractNewRows() throws Exception { 105 final NetworkStats before = new NetworkStats(TEST_START, 2) 106 .addEntry(TEST_IFACE, 100, 1024, 0) 107 .addEntry(TEST_IFACE, 101, 0, 1024); 108 109 final NetworkStats after = new NetworkStats(TEST_START, 3) 110 .addEntry(TEST_IFACE, 100, 1024, 0) 111 .addEntry(TEST_IFACE, 101, 0, 1024) 112 .addEntry(TEST_IFACE, 102, 1024, 1024); 113 114 final NetworkStats result = after.subtract(before); 115 116 // its okay to have new rows 117 assertEquals(0, result.rx[0]); 118 assertEquals(0, result.tx[0]); 119 assertEquals(0, result.rx[1]); 120 assertEquals(0, result.tx[1]); 121 assertEquals(1024, result.rx[2]); 122 assertEquals(1024, result.tx[2]); 123 } 124 125} 126