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 17package com.android.server.usage; 18 19import android.os.FileUtils; 20import android.test.AndroidTestCase; 21 22import java.io.File; 23 24public 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.updateDisplay(true, /* elapsedRealtime= */ 1000); 49 aih.updateDisplay(false, /* elapsedRealtime= */ 2000); 50 // Screen On time file should be written right away 51 assertTrue(aih.getScreenOnTimeFile().exists()); 52 53 aih.writeAppIdleTimes(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.updateDisplay(false, 2000); 62 assertEquals(aih.getScreenOnTime(2000), 0); 63 aih.updateDisplay(true, 3000); 64 assertEquals(aih.getScreenOnTime(4000), 1000); 65 assertEquals(aih.getScreenOnTime(5000), 2000); 66 aih.updateDisplay(false, 6000); 67 // Screen on time should not keep progressing with screen is off 68 assertEquals(aih.getScreenOnTime(7000), 3000); 69 assertEquals(aih.getScreenOnTime(8000), 3000); 70 aih.writeAppIdleDurations(); 71 72 // Check if the screen on time is persisted across instantiations 73 AppIdleHistory aih2 = new AppIdleHistory(mStorageDir, 0); 74 assertEquals(aih2.getScreenOnTime(11000), 3000); 75 aih2.updateDisplay(true, 4000); 76 aih2.updateDisplay(false, 5000); 77 assertEquals(aih2.getScreenOnTime(13000), 4000); 78 } 79 80 public void testPackageEvents() { 81 AppIdleHistory aih = new AppIdleHistory(mStorageDir, 1000); 82 aih.setThresholds(4000, 1000); 83 aih.updateDisplay(true, 1000); 84 // App is not-idle by default 85 assertFalse(aih.isIdle(PACKAGE_1, 0, 1500)); 86 // Still not idle 87 assertFalse(aih.isIdle(PACKAGE_1, 0, 3000)); 88 // Idle now 89 assertTrue(aih.isIdle(PACKAGE_1, 0, 8000)); 90 // Not idle 91 assertFalse(aih.isIdle(PACKAGE_2, 0, 9000)); 92 93 // Screen off 94 aih.updateDisplay(false, 9100); 95 // Still idle after 10 seconds because screen hasn't been on long enough 96 assertFalse(aih.isIdle(PACKAGE_2, 0, 20000)); 97 aih.updateDisplay(true, 21000); 98 assertTrue(aih.isIdle(PACKAGE_2, 0, 23000)); 99 } 100}