Home | History | Annotate | Download | only in os
      1 /*
      2  * Copyright (C) 2016 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
      5  * use this file except in compliance with the License. You may obtain a copy of
      6  * 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, WITHOUT
     12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
     13  * License for the specific language governing permissions and limitations under
     14  * the License.
     15  */
     16 package com.android.internal.os;
     17 
     18 import android.support.test.filters.SmallTest;
     19 
     20 import junit.framework.TestCase;
     21 
     22 import java.nio.charset.Charset;
     23 
     24 public class KernelWakelockReaderTest extends TestCase {
     25     /**
     26      * Helper class that builds the mock Kernel module file /d/wakeup_sources.
     27      */
     28     private static class ProcFileBuilder {
     29         private final static String sHeader = "name\t\tactive_count\tevent_count\twakeup_count\t" +
     30                 "expire_count\tactive_since\ttotal_time\tmax_time\tlast_change\t" +
     31                 "prevent_suspend_time\n";
     32 
     33         private StringBuilder mStringBuilder;
     34 
     35         private void ensureHeader() {
     36             if (mStringBuilder == null) {
     37                 mStringBuilder = new StringBuilder();
     38                 mStringBuilder.append(sHeader);
     39             }
     40         }
     41 
     42         public ProcFileBuilder addLine(String name, int count, long timeMillis) {
     43             ensureHeader();
     44             mStringBuilder.append(name).append("\t").append(count).append("\t0\t0\t0\t0\t")
     45                     .append(timeMillis).append("\t0\t0\t0\n");
     46             return this;
     47         }
     48 
     49         public byte[] getBytes() throws Exception {
     50             ensureHeader();
     51             byte[] data = mStringBuilder.toString().getBytes(Charset.forName("UTF-8"));
     52 
     53             // The Kernel puts a \0 at the end of the data. Since each of our lines ends with \n,
     54             // we override the last \n with a \0.
     55             data[data.length - 1] = 0;
     56             return data;
     57         }
     58     }
     59 
     60     private KernelWakelockReader mReader;
     61 
     62     @Override
     63     public void setUp() throws Exception {
     64         super.setUp();
     65         mReader = new KernelWakelockReader();
     66     }
     67 
     68     @SmallTest
     69     public void testParseEmptyFile() throws Exception {
     70         KernelWakelockStats staleStats = mReader.parseProcWakelocks(new byte[0], 0, true,
     71                 new KernelWakelockStats());
     72         assertTrue(staleStats.isEmpty());
     73     }
     74 
     75     @SmallTest
     76     public void testOnlyHeader() throws Exception {
     77         byte[] buffer = new ProcFileBuilder().getBytes();
     78         KernelWakelockStats staleStats = mReader.parseProcWakelocks(buffer, buffer.length, true,
     79                 new KernelWakelockStats());
     80         assertTrue(staleStats.isEmpty());
     81     }
     82 
     83     @SmallTest
     84     public void testOneWakelock() throws Exception {
     85         byte[] buffer = new ProcFileBuilder()
     86                 .addLine("Wakelock", 34, 123) // Milliseconds
     87                 .getBytes();
     88         KernelWakelockStats staleStats = mReader.parseProcWakelocks(buffer, buffer.length, true,
     89                 new KernelWakelockStats());
     90         assertEquals(1, staleStats.size());
     91         assertTrue(staleStats.containsKey("Wakelock"));
     92 
     93         KernelWakelockStats.Entry entry = staleStats.get("Wakelock");
     94         assertEquals(34, entry.mCount);
     95         assertEquals(123 * 1000, entry.mTotalTime); // Microseconds
     96     }
     97 
     98     @SmallTest
     99     public void testTwoWakelocks() throws Exception {
    100         byte[] buffer = new ProcFileBuilder()
    101                 .addLine("Wakelock", 1, 10)
    102                 .addLine("Fakelock", 2, 20)
    103                 .getBytes();
    104         KernelWakelockStats staleStats = mReader.parseProcWakelocks(buffer, buffer.length, true,
    105                 new KernelWakelockStats());
    106         assertEquals(2, staleStats.size());
    107         assertTrue(staleStats.containsKey("Wakelock"));
    108         assertTrue(staleStats.containsKey("Fakelock"));
    109     }
    110 
    111     @SmallTest
    112     public void testDuplicateWakelocksAccumulate() throws Exception {
    113         byte[] buffer = new ProcFileBuilder()
    114                 .addLine("Wakelock", 1, 10) // Milliseconds
    115                 .addLine("Wakelock", 1, 10) // Milliseconds
    116                 .getBytes();
    117         KernelWakelockStats staleStats = mReader.parseProcWakelocks(buffer, buffer.length, true,
    118                 new KernelWakelockStats());
    119         assertEquals(1, staleStats.size());
    120         assertTrue(staleStats.containsKey("Wakelock"));
    121 
    122         KernelWakelockStats.Entry entry = staleStats.get("Wakelock");
    123         assertEquals(2, entry.mCount);
    124         assertEquals(20 * 1000, entry.mTotalTime); // Microseconds
    125     }
    126 
    127     @SmallTest
    128     public void testWakelocksBecomeStale() throws Exception {
    129         byte[] buffer = new ProcFileBuilder()
    130                 .addLine("Fakelock", 3, 30)
    131                 .getBytes();
    132         KernelWakelockStats staleStats = new KernelWakelockStats();
    133 
    134         staleStats = mReader.parseProcWakelocks(buffer, buffer.length, true, staleStats);
    135         assertEquals(1, staleStats.size());
    136         assertTrue(staleStats.containsKey("Fakelock"));
    137 
    138         buffer = new ProcFileBuilder()
    139                 .addLine("Wakelock", 1, 10)
    140                 .getBytes();
    141 
    142         staleStats = mReader.parseProcWakelocks(buffer, buffer.length, true, staleStats);
    143         assertEquals(1, staleStats.size());
    144         assertTrue(staleStats.containsKey("Wakelock"));
    145         assertFalse(staleStats.containsKey("Fakelock"));
    146     }
    147 }
    148