1 /*
2  * Copyright (C) 2009 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.cts.process;
18 
19 import java.util.List;
20 
21 import android.app.ActivityManager;
22 import android.app.ActivityManager.RunningAppProcessInfo;
23 import android.content.Context;
24 import android.content.Intent;
25 import android.content.pm.PackageManager;
26 import android.test.AndroidTestCase;
27 
28 import com.android.cts.process.activity.NoSharePidActivity;
29 import com.android.cts.process.activity.SharePidActivity;
30 import com.android.cts.process.activity.SharePidSubActivity;
31 
32 public class ProcessTest extends AndroidTestCase {
33     private final int WAIT_TIME = 2000;
34 
testUid()35     public void testUid() throws Exception {
36         String enableApp = "com.android.cts.process.shareuidapp";
37         String disableApp = "com.android.cts.process.noshareuidapp";
38         String testApp = mContext.getPackageName();
39         int uid1 = mContext.getPackageManager().getApplicationInfo(enableApp,
40                 PackageManager.GET_META_DATA).uid;
41         int uid2 = mContext.getPackageManager().getApplicationInfo(disableApp,
42                 PackageManager.GET_META_DATA).uid;
43         int uid3 = mContext.getPackageManager().getApplicationInfo(testApp,
44                 PackageManager.GET_META_DATA).uid;
45         assertEquals(uid1, uid3);
46         assertNotSame(uid2, uid3);
47     }
48 
testPid()49     public void testPid() throws Exception {
50         ActivityManager am = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
51         String shareProcessName = mContext.getPackageName() + ":shareProcess";
52         String noShareProcessName = mContext.getPackageName() + ":noShareProcess";
53         List<RunningAppProcessInfo> list = am.getRunningAppProcesses();
54         assertEquals(-1, getPid(shareProcessName, list));
55         // share pid will use same process
56         Intent sharePidIntent = new Intent();
57         sharePidIntent.setClass(mContext, SharePidActivity.class);
58         sharePidIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
59         mContext.startActivity(sharePidIntent);
60         Thread.sleep(WAIT_TIME);
61         List<RunningAppProcessInfo> sharelist = am.getRunningAppProcesses();
62         int sharePid = getPid(shareProcessName, sharelist);
63         assertTrue(-1 != sharePid);
64         Intent sharePidStubIntent = new Intent();
65         sharePidStubIntent.setClass(mContext, SharePidSubActivity.class);
66         sharePidStubIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
67         mContext.startActivity(sharePidStubIntent);
68         Thread.sleep(WAIT_TIME);
69         List<RunningAppProcessInfo> shareStublist = am.getRunningAppProcesses();
70         int shareStubPid = getPid(shareProcessName, shareStublist);
71         assertTrue(-1 != shareStubPid);
72         assertEquals(sharePid, shareStubPid);
73         // no share pid will create a new process
74         Intent noSharePidIntent = new Intent();
75         noSharePidIntent.setClass(mContext, NoSharePidActivity.class);
76         noSharePidIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
77         mContext.startActivity(noSharePidIntent);
78         Thread.sleep(WAIT_TIME);
79         List<RunningAppProcessInfo> noShareStublist = am.getRunningAppProcesses();
80         int noSharePid = getPid(noShareProcessName, noShareStublist);
81         assertTrue(-1 != noSharePid);
82         assertTrue(sharePid != noSharePid);
83         // kill the process after test
84         android.os.Process.killProcess(noSharePid);
85         android.os.Process.killProcess(sharePid);
86     }
87 
getPid(String processName, List<RunningAppProcessInfo> list)88     private int getPid(String processName, List<RunningAppProcessInfo> list) {
89         for (RunningAppProcessInfo rai : list) {
90             if (processName.equals(rai.processName))
91                 return rai.pid;
92         }
93         return -1;
94     }
95 
96 }
97