Skip to content

Commit d85b8d5

Browse files
author
Maxim Siniavine
committed
Switched to another method for launching apps.
Started using a separate thread which calls startActivityAndWait for starting apps. Also increased the minimum and maximum lengths of time to wait for apps to stabilize. Change-Id: I49935a0ed1d1c230e58dc1629e5e4da6b3887903
1 parent 89ac38b commit d85b8d5

File tree

2 files changed

+49
-7
lines changed

2 files changed

+49
-7
lines changed

tests/MemoryUsage/Android.mk

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ LOCAL_SRC_FILES := $(call all-java-files-under, src)
88

99
LOCAL_PACKAGE_NAME := MemoryUsage
1010

11-
LOCAL_SDK_VERSION := 7
11+
LOCAL_CERTIFICATE := platform
12+
LOCAL_JAVA_LIBRARIES := android.test.runner
1213

1314
include $(BUILD_PACKAGE)
1415

tests/MemoryUsage/src/com/android/tests/memoryusage/MemoryUsageTest.java

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,17 @@
1818
import android.app.ActivityManager;
1919
import android.app.ActivityManager.ProcessErrorStateInfo;
2020
import android.app.ActivityManager.RunningAppProcessInfo;
21+
import android.app.ActivityManagerNative;
22+
import android.app.IActivityManager;
2123
import android.content.Context;
2224
import android.content.Intent;
23-
import android.content.pm.PackageInfo;
2425
import android.content.pm.PackageManager;
2526
import android.content.pm.PackageManager.NameNotFoundException;
2627
import android.content.pm.ResolveInfo;
2728
import android.os.Bundle;
2829
import android.os.Debug.MemoryInfo;
30+
import android.os.RemoteException;
31+
import android.os.UserHandle;
2932
import android.test.InstrumentationTestCase;
3033
import 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

Comments
 (0)