1 /* 2 * Copyright (C) 2017 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 package com.android.server.cts.procstats; 17 18 import static junit.framework.TestCase.fail; 19 20 import android.content.ComponentName; 21 import android.content.Intent; 22 import android.os.ParcelFileDescriptor; 23 import android.util.Log; 24 25 import androidx.test.InstrumentationRegistry; 26 import androidx.test.runner.AndroidJUnit4; 27 28 import org.junit.After; 29 import org.junit.Test; 30 import org.junit.runner.RunWith; 31 32 import java.io.BufferedReader; 33 import java.io.FileReader; 34 import java.io.IOException; 35 import java.util.ArrayList; 36 import java.util.List; 37 import java.util.regex.Pattern; 38 39 @RunWith(AndroidJUnit4.class) 40 public class ProcStatsTest { 41 private static final String TAG = "ProcStatsTest"; 42 private static final String HELPER_PACKAGE = "com.android.server.cts.procstatshelper"; 43 44 @After tearDown()45 public void tearDown() { 46 runCommand("dumpsys procstats --stop-pretend-screen", "^$"); 47 } 48 buildIntent(String component)49 private static final Intent buildIntent(String component) { 50 return new Intent() 51 .setComponent(ComponentName.unflattenFromString(component)); 52 } 53 54 @Test testLaunchApp()55 public void testLaunchApp() throws Exception { 56 57 InstrumentationRegistry.getContext().startActivity( 58 buildIntent(HELPER_PACKAGE + "/.MainActivity")); 59 60 Thread.sleep(4000); 61 62 InstrumentationRegistry.getContext().startService( 63 buildIntent(HELPER_PACKAGE + "/.ProcStatsHelperServiceMain")); 64 65 Thread.sleep(4000); 66 67 // Now run something with the screen off. 68 runCommand("dumpsys procstats --pretend-screen-off", "^$"); 69 70 InstrumentationRegistry.getContext().startActivity( 71 buildIntent(HELPER_PACKAGE + "/.MainActivity")); 72 73 Thread.sleep(4000); 74 75 InstrumentationRegistry.getContext().startService( 76 buildIntent(HELPER_PACKAGE + "/.ProcStatsHelperServiceSub")); 77 78 Thread.sleep(4000); 79 80 // run "dumpsys meminfo" to update the PSS stats. 81 runCommand("dumpsys meminfo " + HELPER_PACKAGE, 82 "MEMINFO in pid"); 83 runCommand("dumpsys meminfo " + HELPER_PACKAGE + ":proc2", 84 "MEMINFO in pid"); 85 } 86 readAll(ParcelFileDescriptor pfd)87 static List<String> readAll(ParcelFileDescriptor pfd) { 88 try { 89 try { 90 final ArrayList<String> ret = new ArrayList<>(); 91 try (BufferedReader r = new BufferedReader( 92 new FileReader(pfd.getFileDescriptor()))) { 93 String line; 94 while ((line = r.readLine()) != null) { 95 ret.add(line); 96 } 97 r.readLine(); 98 } 99 return ret; 100 } finally { 101 pfd.close(); 102 } 103 } catch (IOException e) { 104 throw new RuntimeException(e); 105 } 106 } 107 concatResult(List<String> result)108 static String concatResult(List<String> result) { 109 final StringBuilder sb = new StringBuilder(); 110 for (String s : result) { 111 sb.append(s); 112 sb.append("\n"); 113 } 114 return sb.toString().trim(); 115 } 116 runCommand(String command, String expectedOutputRegex)117 private void runCommand(String command, String expectedOutputRegex) { 118 Log.i(TAG, "Running comamnd: " + command); 119 final String result = concatResult(readAll( 120 InstrumentationRegistry.getInstrumentation().getUiAutomation().executeShellCommand( 121 command))); 122 Log.i(TAG, "Output:"); 123 Log.i(TAG, result); 124 if (!Pattern.compile(expectedOutputRegex).matcher(result).find()) { 125 fail("Expected=" + expectedOutputRegex + "\nBut was=" + result); 126 } 127 } 128 } 129 130