Home | History | Annotate | Download | only in cts
      1 /*
      2  * Copyright (C) 2016 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.os.health.cts;
     18 
     19 import android.content.Context;
     20 import android.os.Process;
     21 import android.os.health.HealthStats;
     22 import android.os.health.SystemHealthManager;
     23 import android.test.InstrumentationTestCase;
     24 import android.test.suitebuilder.annotation.SmallTest;
     25 
     26 import junit.framework.Assert;
     27 
     28 /**
     29  * Provides test cases for android.os.health.TimerStat.
     30  */
     31 public class SystemHealthManagerTest extends InstrumentationTestCase {
     32     /**
     33      * Tests that takeMyUidSnapshot returns a HealthStats object.
     34      */
     35     @SmallTest
     36     public void testTakeMyUidSnapshot() throws Exception {
     37         final Context context = getInstrumentation().getTargetContext();
     38         final SystemHealthManager healthy = context.getSystemService(SystemHealthManager.class);
     39 
     40         Assert.assertNotNull(healthy.takeMyUidSnapshot());
     41     }
     42 
     43     /**
     44      * Tests that takeUidSnapshot with this uid returns a HealthStats object.
     45      */
     46     @SmallTest
     47     public void testTakeUidSnapshotWithMe() throws Exception {
     48         final Context context = getInstrumentation().getTargetContext();
     49         final SystemHealthManager healthy = context.getSystemService(SystemHealthManager.class);
     50 
     51         Assert.assertNotNull(healthy.takeUidSnapshot(Process.myUid()));
     52     }
     53 
     54     /**
     55      * Tests that takeUidSnapshot on the system process throws a SecurityException.
     56      */
     57     @SmallTest
     58     public void testTakeMyUidSnapshotWithSystem() throws Exception {
     59         final Context context = getInstrumentation().getTargetContext();
     60         final SystemHealthManager healthy = context.getSystemService(SystemHealthManager.class);
     61 
     62         boolean threw = false;
     63         try {
     64             healthy.takeUidSnapshot(Process.SYSTEM_UID);
     65         } catch (SecurityException ex) {
     66             threw = true;
     67         }
     68 
     69         Assert.assertTrue(threw);
     70     }
     71 
     72     /**
     73      * Tests that takeUidSnapshots with an empty array returns an empty array.
     74      */
     75     @SmallTest
     76     public void testTakeUidSnapshotsWithEmptyArray() throws Exception {
     77         final Context context = getInstrumentation().getTargetContext();
     78         final SystemHealthManager healthy = context.getSystemService(SystemHealthManager.class);
     79 
     80         final HealthStats[] result = healthy.takeUidSnapshots(new int[0]);
     81         Assert.assertEquals(0, result.length);
     82     }
     83 
     84     /**
     85      * Tests that takeUidSnapshots with this uid returns a HealthStats object.
     86      */
     87     @SmallTest
     88     public void testTakeUidSnapshotsWithMe() throws Exception {
     89         final Context context = getInstrumentation().getTargetContext();
     90         final SystemHealthManager healthy = context.getSystemService(SystemHealthManager.class);
     91 
     92         final HealthStats[] result = healthy.takeUidSnapshots(new int[] {
     93                     Process.myUid(),
     94                     Process.myUid(),
     95                 });
     96         Assert.assertEquals(2, result.length);
     97         Assert.assertNotNull(result[0]);
     98         Assert.assertNotNull(result[1]);
     99     }
    100 
    101     /**
    102      * Tests that takeUidSnapshot on the system process throws a SecurityException.
    103      */
    104     @SmallTest
    105     public void testTakeMyUidSnapshotsWithSystem() throws Exception {
    106         final Context context = getInstrumentation().getTargetContext();
    107         final SystemHealthManager healthy = context.getSystemService(SystemHealthManager.class);
    108 
    109         boolean threw = false;
    110         try {
    111             healthy.takeUidSnapshots(new int[] {
    112                         Process.myUid(),
    113                         Process.SYSTEM_UID,
    114                     });
    115         } catch (SecurityException ex) {
    116             threw = true;
    117         }
    118 
    119         Assert.assertTrue(threw);
    120     }
    121 }
    122 
    123