1818import android .app .ActivityManager ;
1919import android .app .ActivityManager .ProcessErrorStateInfo ;
2020import android .app .ActivityManager .RunningAppProcessInfo ;
21+ import android .app .ActivityManagerNative ;
22+ import android .app .IActivityManager ;
2123import android .content .Context ;
2224import android .content .Intent ;
23- import android .content .pm .PackageInfo ;
2425import android .content .pm .PackageManager ;
2526import android .content .pm .PackageManager .NameNotFoundException ;
2627import android .content .pm .ResolveInfo ;
2728import android .os .Bundle ;
2829import android .os .Debug .MemoryInfo ;
30+ import android .os .RemoteException ;
31+ import android .os .UserHandle ;
2932import android .test .InstrumentationTestCase ;
3033import android .util .Log ;
3134
@@ -48,8 +51,9 @@ public class MemoryUsageTest extends InstrumentationTestCase {
4851
4952 private static final int SLEEP_TIME = 1000 ;
5053 private static final int THRESHOLD = 1024 ;
51- private static final int MAX_ITERATIONS = 10 ;
52- private static final int MIN_ITERATIONS = 4 ;
54+ private static final int MAX_ITERATIONS = 20 ;
55+ private static final int MIN_ITERATIONS = 6 ;
56+ private static final int JOIN_TIMEOUT = 10000 ;
5357
5458 private static final String TAG = "MemoryUsageInstrumentation" ;
5559 private static final String KEY_APPS = "apps" ;
@@ -58,10 +62,13 @@ public class MemoryUsageTest extends InstrumentationTestCase {
5862 private Map <String , String > mNameToProcess ;
5963 private Map <String , String > mNameToResultKey ;
6064
65+ private IActivityManager mAm ;
66+
6167 public void testMemory () {
6268 MemoryUsageInstrumentation instrumentation =
63- (MemoryUsageInstrumentation ) getInstrumentation ();
69+ (MemoryUsageInstrumentation ) getInstrumentation ();
6470 Bundle args = instrumentation .getBundle ();
71+ mAm = ActivityManagerNative .getDefault ();
6572
6673 createMappings ();
6774 parseArgs (args );
@@ -136,7 +143,16 @@ private String startApp(String appName) throws NameNotFoundException {
136143
137144 String process = mNameToProcess .get (appName );
138145 Intent startIntent = mNameToIntent .get (appName );
139- getInstrumentation ().getContext ().startActivity (startIntent );
146+
147+ AppLaunchRunnable runnable = new AppLaunchRunnable (startIntent );
148+ Thread t = new Thread (runnable );
149+ t .start ();
150+ try {
151+ t .join (JOIN_TIMEOUT );
152+ } catch (InterruptedException e ) {
153+ // ignore
154+ }
155+
140156 return process ;
141157 }
142158
@@ -234,12 +250,37 @@ private int getPss(String processName) {
234250 }
235251
236252 int [] pids = {
237- proc .pid };
253+ proc .pid };
238254
239255 MemoryInfo meminfo = am .getProcessMemoryInfo (pids )[0 ];
240256 return meminfo .getTotalPss ();
241257
242258 }
243259 return -1 ;
244260 }
261+
262+ private class AppLaunchRunnable implements Runnable {
263+ private Intent mLaunchIntent ;
264+
265+ public AppLaunchRunnable (Intent intent ) {
266+ mLaunchIntent = intent ;
267+ }
268+
269+ public void run () {
270+ try {
271+ String mimeType = mLaunchIntent .getType ();
272+ if (mimeType == null && mLaunchIntent .getData () != null
273+ && "content" .equals (mLaunchIntent .getData ().getScheme ())) {
274+ mimeType = mAm .getProviderMimeType (mLaunchIntent .getData (),
275+ UserHandle .USER_CURRENT );
276+ }
277+
278+ mAm .startActivityAndWait (null , mLaunchIntent , mimeType ,
279+ null , null , 0 , mLaunchIntent .getFlags (), null , null , null ,
280+ UserHandle .USER_CURRENT_OR_SELF );
281+ } catch (RemoteException e ) {
282+ Log .w (TAG , "Error launching app" , e );
283+ }
284+ }
285+ }
245286}
0 commit comments