Home | History | Annotate | Download | only in usage
      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 com.android.server.usage;
     18 
     19 import android.os.FileUtils;
     20 import android.test.AndroidTestCase;
     21 
     22 import java.io.File;
     23 
     24 public class AppIdleHistoryTests extends AndroidTestCase {
     25 
     26     File mStorageDir;
     27 
     28     final static String PACKAGE_1 = "com.android.testpackage1";
     29     final static String PACKAGE_2 = "com.android.testpackage2";
     30 
     31     @Override
     32     protected void setUp() throws Exception {
     33         super.setUp();
     34         mStorageDir = new File(getContext().getFilesDir(), "appidle");
     35         mStorageDir.mkdirs();
     36     }
     37 
     38     @Override
     39     protected void tearDown() throws Exception {
     40         FileUtils.deleteContents(mStorageDir);
     41         super.tearDown();
     42     }
     43 
     44     public void testFilesCreation() {
     45         final int userId = 0;
     46         AppIdleHistory aih = new AppIdleHistory(mStorageDir, 0);
     47 
     48         aih.updateDisplayLocked(true, /* elapsedRealtime= */ 1000);
     49         aih.updateDisplayLocked(false, /* elapsedRealtime= */ 2000);
     50         // Screen On time file should be written right away
     51         assertTrue(aih.getScreenOnTimeFile().exists());
     52 
     53         aih.writeAppIdleTimesLocked(userId);
     54         // stats file should be written now
     55         assertTrue(new File(new File(mStorageDir, "users/" + userId),
     56                 AppIdleHistory.APP_IDLE_FILENAME).exists());
     57     }
     58 
     59     public void testScreenOnTime() {
     60         AppIdleHistory aih = new AppIdleHistory(mStorageDir, 1000);
     61         aih.updateDisplayLocked(false, 2000);
     62         assertEquals(aih.getScreenOnTimeLocked(2000), 0);
     63         aih.updateDisplayLocked(true, 3000);
     64         assertEquals(aih.getScreenOnTimeLocked(4000), 1000);
     65         assertEquals(aih.getScreenOnTimeLocked(5000), 2000);
     66         aih.updateDisplayLocked(false, 6000);
     67         // Screen on time should not keep progressing with screen is off
     68         assertEquals(aih.getScreenOnTimeLocked(7000), 3000);
     69         assertEquals(aih.getScreenOnTimeLocked(8000), 3000);
     70         aih.writeAppIdleDurationsLocked();
     71 
     72         // Check if the screen on time is persisted across instantiations
     73         AppIdleHistory aih2 = new AppIdleHistory(mStorageDir, 0);
     74         assertEquals(aih2.getScreenOnTimeLocked(11000), 3000);
     75         aih2.updateDisplayLocked(true, 4000);
     76         aih2.updateDisplayLocked(false, 5000);
     77         assertEquals(aih2.getScreenOnTimeLocked(13000), 4000);
     78     }
     79 
     80     public void testPackageEvents() {
     81         AppIdleHistory aih = new AppIdleHistory(mStorageDir, 1000);
     82         aih.setThresholds(4000, 1000);
     83         aih.updateDisplayLocked(true, 1000);
     84         // App is not-idle by default
     85         assertFalse(aih.isIdleLocked(PACKAGE_1, 0, 1500));
     86         // Still not idle
     87         assertFalse(aih.isIdleLocked(PACKAGE_1, 0, 3000));
     88         // Idle now
     89         assertTrue(aih.isIdleLocked(PACKAGE_1, 0, 8000));
     90         // Not idle
     91         assertFalse(aih.isIdleLocked(PACKAGE_2, 0, 9000));
     92 
     93         // Screen off
     94         aih.updateDisplayLocked(false, 9100);
     95         // Still idle after 10 seconds because screen hasn't been on long enough
     96         assertFalse(aih.isIdleLocked(PACKAGE_2, 0, 20000));
     97         aih.updateDisplayLocked(true, 21000);
     98         assertTrue(aih.isIdleLocked(PACKAGE_2, 0, 23000));
     99     }
    100 }