Home | History | Annotate | Download | only in cts
      1 /*
      2  * Copyright (C) 2008 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 package android.app.cts;
     17 
     18 import android.app.ActivityManager;
     19 import android.os.Debug;
     20 import android.os.Parcel;
     21 import android.os.Process;
     22 import android.test.AndroidTestCase;
     23 
     24 public class ActivityManagerMemoryInfoTest extends AndroidTestCase {
     25     protected ActivityManager.MemoryInfo mMemory;
     26 
     27     @Override
     28     protected void setUp() throws Exception {
     29         super.setUp();
     30         mMemory = new ActivityManager.MemoryInfo();
     31     }
     32 
     33     public void testDescribeContents() {
     34         assertEquals(0, mMemory.describeContents());
     35     }
     36 
     37     public void testWriteToParcel() throws Exception {
     38         final long AVAILMEM = 1000l;
     39         mMemory.availMem = AVAILMEM;
     40         final long THRESHOLD = 500l;
     41         mMemory.threshold = THRESHOLD;
     42         final boolean LOWMEMORY = true;
     43         mMemory.lowMemory = LOWMEMORY;
     44         Parcel parcel = Parcel.obtain();
     45         mMemory.writeToParcel(parcel, 0);
     46         parcel.setDataPosition(0);
     47         ActivityManager.MemoryInfo values =
     48             ActivityManager.MemoryInfo.CREATOR.createFromParcel(parcel);
     49         assertEquals(AVAILMEM, values.availMem);
     50         assertEquals(THRESHOLD, values.threshold);
     51         assertEquals(LOWMEMORY, values.lowMemory);
     52 
     53         // test null condition.
     54         try {
     55             mMemory.writeToParcel(null, 0);
     56             fail("writeToParcel should throw out NullPointerException when Parcel is null");
     57         } catch (NullPointerException e) {
     58             // expected
     59         }
     60     }
     61 
     62     public void testReadFromParcel() throws Exception {
     63         final long AVAILMEM = 1000l;
     64         mMemory.availMem = AVAILMEM;
     65         final long THRESHOLD = 500l;
     66         mMemory.threshold = THRESHOLD;
     67         final boolean LOWMEMORY = true;
     68         mMemory.lowMemory = LOWMEMORY;
     69         Parcel parcel = Parcel.obtain();
     70         mMemory.writeToParcel(parcel, 0);
     71         parcel.setDataPosition(0);
     72         ActivityManager.MemoryInfo result = new ActivityManager.MemoryInfo();
     73         result.readFromParcel(parcel);
     74         assertEquals(AVAILMEM, result.availMem);
     75         assertEquals(THRESHOLD, result.threshold);
     76         assertEquals(LOWMEMORY, result.lowMemory);
     77 
     78         // test null condition.
     79         result = new ActivityManager.MemoryInfo();
     80         try {
     81             result.readFromParcel(null);
     82             fail("readFromParcel should throw out NullPointerException when Parcel is null");
     83         } catch (NullPointerException e) {
     84             // expected
     85         }
     86     }
     87 
     88     public void testGetProcessMemoryInfo() {
     89         // PID == 1 is the init process.
     90         Debug.MemoryInfo[] result = getContext().getSystemService(ActivityManager.class)
     91                 .getProcessMemoryInfo(new int[]{1, Process.myPid(), 1});
     92         assertEquals(3, result.length);
     93         isEmpty(result[0]);
     94         isEmpty(result[2]);
     95         isNotEmpty(result[1]);
     96     }
     97 
     98     private static void isEmpty(Debug.MemoryInfo mi) {
     99         assertEquals(0, mi.dalvikPss);
    100         assertEquals(0, mi.nativePss);
    101     }
    102 
    103     private static void isNotEmpty(Debug.MemoryInfo mi) {
    104         assertTrue(mi.dalvikPss > 0);
    105         assertTrue(mi.nativePss > 0);
    106     }
    107 }
    108