1 package com.android.internal.os; 2 3 import android.support.test.filters.SmallTest; 4 import android.util.LongSparseLongArray; 5 6 import junit.framework.TestCase; 7 8 import org.junit.Assert; 9 import org.mockito.Mockito; 10 11 import java.io.BufferedReader; 12 13 /** 14 * Tests for KernelMemoryBandwidthStats parsing and delta calculation, based on memory_state_time. 15 */ 16 public class KernelMemoryBandwidthStatsTest extends TestCase { 17 18 /** 19 * Standard example of parsing stats. 20 * @throws Exception 21 */ 22 @SmallTest 23 public void testParseStandard() throws Exception { 24 KernelMemoryBandwidthStats stats = new KernelMemoryBandwidthStats(); 25 BufferedReader mockStandardReader = Mockito.mock(BufferedReader.class); 26 Mockito.when(mockStandardReader.readLine()).thenReturn( 27 "99000000 0 0 0 0 0 0 0 0 0 0 0 0", 28 "149000000 0 0 0 0 0 0 0 0 0 0 0 0", 29 "199884800 7301000000 0 2000000 1000000 0 0 0 0 0 0 0 0", 30 "299892736 674000000 0 21000000 0 0 0 0 0 0 0 0 0", 31 "411959296 1146000000 0 221000000 1000000 0 0 0 0 0 0 0 0", 32 "546963456 744000000 0 420000000 0 0 0 1000000 0 0 0 0 0", 33 "680919040 182000000 0 1839000000 207000000 1000000 1000000 0 0 0 0 0 0", 34 "767950848 0 0 198000000 33000000 4000000 0 1000000 0 0 0 0 0", 35 "1016987648 0 0 339000000 362000000 3000000 0 0 0 0 0 0 16000000", 36 "1295908864 0 0 20000000 870000000 244000000 0 0 0 0 0 0 33000000", 37 "1554907136 0 0 6000000 32000000 631000000 115000000 0 0 0 1000000 0 0", 38 "1803943936 2496000000 0 17000000 2000000 377000000 1505000000 278000000 183000000 141000000 486000000 154000000 113000000", null); 39 stats.parseStats(mockStandardReader); 40 long[] expected = new long[] {12543L, 0L, 3083L, 1508L, 1260L, 1621L, 280L, 183L, 41 141L, 487L, 154L, 162L}; 42 LongSparseLongArray array = stats.getBandwidthEntries(); 43 for (int i = 2; i < array.size(); i++) { 44 assertEquals(i, array.keyAt(i)); 45 assertEquals(expected[i], array.valueAt(i)); 46 } 47 Mockito.verify(mockStandardReader, Mockito.times(13)).readLine(); 48 } 49 50 /** 51 * When the stats are populated with zeroes (unsupported device), checks that the stats are 52 * zero. 53 * @throws Exception 54 */ 55 @SmallTest 56 public void testParseBackwards() throws Exception { 57 KernelMemoryBandwidthStats zeroStats = new KernelMemoryBandwidthStats(); 58 BufferedReader mockZeroReader = Mockito.mock(BufferedReader.class); 59 Mockito.when(mockZeroReader.readLine()).thenReturn( 60 "99000000 0 0 0 0 0 0 0 0 0 0 0 0", 61 "149000000 0 0 0 0 0 0 0 0 0 0 0 0", 62 "199884800 0 0 0 0 0 0 0 0 0 0 0 0", 63 "299892736 0 0 0 0 0 0 0 0 0 0 0 0", 64 "411959296 0 0 0 0 0 0 0 0 0 0 0 0", 65 "546963456 0 0 0 0 0 0 0 0 0 0 0 0", 66 "680919040 0 0 0 0 0 0 0 0 0 0 0 0", 67 "767950848 0 0 0 0 0 0 0 0 0 0 0 0", 68 "1016987648 0 0 0 0 0 0 0 0 0 0 0 0", 69 "1295908864 0 0 0 0 0 0 0 0 0 0 0 0", 70 "1554907136 0 0 0 0 0 0 0 0 0 0 0 0", 71 "1803943936 0 0 0 0 0 0 0 0 0 0 0 0", null); 72 zeroStats.parseStats(mockZeroReader); 73 long[] expected = new long[] {0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L}; 74 LongSparseLongArray array = zeroStats.getBandwidthEntries(); 75 for (int i = 0; i < array.size(); i++) { 76 assertEquals(expected[i], array.valueAt(i)); 77 } 78 Mockito.verify(mockZeroReader, Mockito.times(13)).readLine(); 79 } 80 } 81