Home | History | Annotate | Download | only in util
      1 /*
      2  * Copyright (C) 2014 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 com.android.compatibility.common.util;
     18 
     19 import static org.junit.Assert.assertTrue;
     20 import static org.junit.Assert.fail;
     21 
     22 import android.app.ActivityManager;
     23 import android.app.ActivityManager.MemoryInfo;
     24 import android.app.Instrumentation;
     25 import android.content.Context;
     26 import android.os.ParcelFileDescriptor;
     27 import android.os.StatFs;
     28 import android.support.test.InstrumentationRegistry;
     29 import android.util.Log;
     30 
     31 import java.io.FileInputStream;
     32 import java.io.IOException;
     33 import java.util.function.Predicate;
     34 
     35 public class SystemUtil {
     36     private static final String TAG = "CtsSystemUtil";
     37 
     38     public static long getFreeDiskSize(Context context) {
     39         final StatFs statFs = new StatFs(context.getFilesDir().getAbsolutePath());
     40         return (long)statFs.getAvailableBlocks() * statFs.getBlockSize();
     41     }
     42 
     43     public static long getFreeMemory(Context context) {
     44         final MemoryInfo info = new MemoryInfo();
     45         ((ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE)).getMemoryInfo(info);
     46         return info.availMem;
     47     }
     48 
     49     public static long getTotalMemory(Context context) {
     50         final MemoryInfo info = new MemoryInfo();
     51         ((ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE)).getMemoryInfo(info);
     52         return info.totalMem;
     53     }
     54 
     55     /**
     56      * Executes a shell command using shell user identity, and return the standard output in string
     57      * <p>Note: calling this function requires API level 21 or above
     58      * @param instrumentation {@link Instrumentation} instance, obtained from a test running in
     59      * instrumentation framework
     60      * @param cmd the command to run
     61      * @return the standard output of the command
     62      * @throws Exception
     63      */
     64     public static String runShellCommand(Instrumentation instrumentation, String cmd)
     65             throws IOException {
     66         Log.v(TAG, "Running command: " + cmd);
     67         if (cmd.startsWith("pm grant ") || cmd.startsWith("pm revoke ")) {
     68             throw new UnsupportedOperationException("Use UiAutomation.grantRuntimePermission() "
     69                     + "or revokeRuntimePermission() directly, which are more robust.");
     70         }
     71         ParcelFileDescriptor pfd = instrumentation.getUiAutomation().executeShellCommand(cmd);
     72         byte[] buf = new byte[512];
     73         int bytesRead;
     74         FileInputStream fis = new ParcelFileDescriptor.AutoCloseInputStream(pfd);
     75         StringBuffer stdout = new StringBuffer();
     76         while ((bytesRead = fis.read(buf)) != -1) {
     77             stdout.append(new String(buf, 0, bytesRead));
     78         }
     79         fis.close();
     80         return stdout.toString();
     81     }
     82 
     83     /**
     84      * Simpler version of {@link #runShellCommand(Instrumentation, String)}.
     85      */
     86     public static String runShellCommand(String cmd) {
     87         try {
     88             return runShellCommand(InstrumentationRegistry.getInstrumentation(), cmd);
     89         } catch (IOException e) {
     90             fail("Failed reading command output: " + e);
     91             return "";
     92         }
     93     }
     94 
     95     /**
     96      * Same as {@link #runShellCommand(String)}, with optionally
     97      * check the result using {@code resultChecker}.
     98      */
     99     public static String runShellCommand(String cmd, Predicate<String> resultChecker) {
    100         final String result = runShellCommand(cmd);
    101         if (resultChecker != null) {
    102             assertTrue("Assertion failed. Command was: " + cmd + "\n"
    103                     + "Output was:\n" + result,
    104                     resultChecker.test(result));
    105         }
    106         return result;
    107     }
    108 
    109     /**
    110      * Same as {@link #runShellCommand(String)}, but fails if the output is not empty.
    111      */
    112     public static String runShellCommandForNoOutput(String cmd) {
    113         final String result = runShellCommand(cmd);
    114         assertTrue("Command failed. Command was: " + cmd + "\n"
    115                 + "Didn't expect any output, but the output was:\n" + result,
    116                 result.length() == 0);
    117         return result;
    118     }
    119 
    120     /**
    121      * Run a command and print the result on logcat.
    122      */
    123     public static void runCommandAndPrintOnLogcat(String logtag, String cmd) {
    124         Log.i(logtag, "Executing: " + cmd);
    125         final String output = runShellCommand(cmd);
    126         for (String line : output.split("\\n", -1)) {
    127             Log.i(logtag, line);
    128         }
    129     }
    130 
    131     /**
    132      * Run a command and return the section matching the patterns.
    133      *
    134      * @see TextUtils#extractSection
    135      */
    136     public static String runCommandAndExtractSection(String cmd,
    137             String extractionStartRegex, boolean startInclusive,
    138             String extractionEndRegex, boolean endInclusive) {
    139         return TextUtils.extractSection(runShellCommand(cmd), extractionStartRegex, startInclusive,
    140                 extractionEndRegex, endInclusive);
    141     }
    142 }
    143