Home | History | Annotate | Download | only in cts
      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 android.permission.cts;
     18 
     19 
     20 import android.app.Activity;
     21 import android.app.ActivityManager;
     22 import android.app.AlertDialog;
     23 import android.content.Context;
     24 import android.content.Intent;
     25 import android.test.ActivityInstrumentationTestCase2;
     26 import android.test.suitebuilder.annotation.MediumTest;
     27 import android.test.suitebuilder.annotation.Suppress;
     28 import android.view.WindowManager;
     29 import android.view.WindowManager.BadTokenException;
     30 
     31 import java.util.List;
     32 
     33 /**
     34  * Verify the Activity related operations require specific permissions.
     35  */
     36 public class NoActivityRelatedPermissionTest
     37         extends ActivityInstrumentationTestCase2<PermissionStubActivity> {
     38 
     39     private PermissionStubActivity mActivity;
     40 
     41     public NoActivityRelatedPermissionTest() {
     42         super("android.permission.cts", PermissionStubActivity.class);
     43     }
     44 
     45     @Override
     46     protected void setUp() throws Exception {
     47         super.setUp();
     48         mActivity = getActivity();
     49     }
     50 
     51     /**
     52      * Verify that get task requires permissions.
     53      * <p>Requires Permission:
     54      *   {@link android.Manifest.permission#GET_TASKS}
     55      */
     56     @MediumTest
     57     public void testGetTask() {
     58         ActivityManager manager = (ActivityManager) getActivity()
     59                 .getSystemService(Context.ACTIVITY_SERVICE);
     60         List<ActivityManager.RunningTaskInfo> runningTasks =  manager.getRunningTasks(10);
     61         // Current implementation should only return tasks for home and the caller.
     62         // We'll be done and task this to mean it shouldn't return more than 2.
     63         assertTrue("Found tasks: " + runningTasks,
     64                 runningTasks == null || runningTasks.size() <= 2);
     65 
     66         List<ActivityManager.RecentTaskInfo> recentTasks = manager.getRecentTasks(10,
     67                 ActivityManager.RECENT_WITH_EXCLUDED);
     68         // Current implementation should only return tasks for home and the caller. Since there can
     69         // be multiple home tasks, we remove them from the list and then check that there is one or
     70         // less task left in the list.
     71         removeHomeTasks(recentTasks);
     72         assertTrue("Found tasks: " + recentTasks, recentTasks == null || recentTasks.size() <= 1);
     73     }
     74 
     75     private void removeHomeTasks(List<ActivityManager.RecentTaskInfo> tasks) {
     76         for (int i = tasks.size() -1; i >= 0; i--) {
     77             ActivityManager.RecentTaskInfo task = tasks.get(i);
     78             if (task.baseIntent != null && isHomeIntent(task.baseIntent)) {
     79                 tasks.remove(i);
     80             }
     81         }
     82     }
     83 
     84     private boolean isHomeIntent(Intent intent) {
     85         return Intent.ACTION_MAIN.equals(intent.getAction())
     86                 && intent.hasCategory(Intent.CATEGORY_HOME)
     87                 && intent.getCategories().size() == 1
     88                 && intent.getData() == null
     89                 && intent.getType() == null;
     90     }
     91 }
     92