Amith Yamasani | a93542f | 2016-02-03 18:02:06 -0800 | [diff] [blame] | 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 | |
Amith Yamasani | afbccb7 | 2017-11-27 10:44:24 -0800 | [diff] [blame] | 19 | import static android.app.usage.UsageStatsManager.REASON_TIMEOUT; |
| 20 | import static android.app.usage.UsageStatsManager.STANDBY_BUCKET_ACTIVE; |
| 21 | import static android.app.usage.UsageStatsManager.STANDBY_BUCKET_FREQUENT; |
| 22 | import static android.app.usage.UsageStatsManager.STANDBY_BUCKET_RARE; |
| 23 | |
Amith Yamasani | 53f06ea | 2018-01-05 17:53:46 -0800 | [diff] [blame] | 24 | import static org.junit.Assert.assertEquals; |
| 25 | import static org.junit.Assert.assertFalse; |
| 26 | import static org.junit.Assert.assertTrue; |
| 27 | |
Amith Yamasani | afbccb7 | 2017-11-27 10:44:24 -0800 | [diff] [blame] | 28 | import android.app.usage.UsageStatsManager; |
Amith Yamasani | a93542f | 2016-02-03 18:02:06 -0800 | [diff] [blame] | 29 | import android.os.FileUtils; |
| 30 | import android.test.AndroidTestCase; |
| 31 | |
| 32 | import java.io.File; |
| 33 | |
| 34 | public class AppIdleHistoryTests extends AndroidTestCase { |
| 35 | |
| 36 | File mStorageDir; |
| 37 | |
| 38 | final static String PACKAGE_1 = "com.android.testpackage1"; |
| 39 | final static String PACKAGE_2 = "com.android.testpackage2"; |
| 40 | |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 41 | final static int USER_ID = 0; |
| 42 | |
Amith Yamasani | a93542f | 2016-02-03 18:02:06 -0800 | [diff] [blame] | 43 | @Override |
| 44 | protected void setUp() throws Exception { |
| 45 | super.setUp(); |
| 46 | mStorageDir = new File(getContext().getFilesDir(), "appidle"); |
| 47 | mStorageDir.mkdirs(); |
| 48 | } |
| 49 | |
| 50 | @Override |
| 51 | protected void tearDown() throws Exception { |
| 52 | FileUtils.deleteContents(mStorageDir); |
| 53 | super.tearDown(); |
| 54 | } |
| 55 | |
| 56 | public void testFilesCreation() { |
Amith Yamasani | a93542f | 2016-02-03 18:02:06 -0800 | [diff] [blame] | 57 | AppIdleHistory aih = new AppIdleHistory(mStorageDir, 0); |
| 58 | |
Amith Yamasani | 61d5fd7 | 2017-02-24 11:02:07 -0800 | [diff] [blame] | 59 | aih.updateDisplay(true, /* elapsedRealtime= */ 1000); |
| 60 | aih.updateDisplay(false, /* elapsedRealtime= */ 2000); |
Amith Yamasani | a93542f | 2016-02-03 18:02:06 -0800 | [diff] [blame] | 61 | // Screen On time file should be written right away |
| 62 | assertTrue(aih.getScreenOnTimeFile().exists()); |
| 63 | |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 64 | aih.writeAppIdleTimes(USER_ID); |
Amith Yamasani | a93542f | 2016-02-03 18:02:06 -0800 | [diff] [blame] | 65 | // stats file should be written now |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 66 | assertTrue(new File(new File(mStorageDir, "users/" + USER_ID), |
Amith Yamasani | a93542f | 2016-02-03 18:02:06 -0800 | [diff] [blame] | 67 | AppIdleHistory.APP_IDLE_FILENAME).exists()); |
| 68 | } |
| 69 | |
| 70 | public void testScreenOnTime() { |
| 71 | AppIdleHistory aih = new AppIdleHistory(mStorageDir, 1000); |
Amith Yamasani | 61d5fd7 | 2017-02-24 11:02:07 -0800 | [diff] [blame] | 72 | aih.updateDisplay(false, 2000); |
| 73 | assertEquals(aih.getScreenOnTime(2000), 0); |
| 74 | aih.updateDisplay(true, 3000); |
| 75 | assertEquals(aih.getScreenOnTime(4000), 1000); |
| 76 | assertEquals(aih.getScreenOnTime(5000), 2000); |
| 77 | aih.updateDisplay(false, 6000); |
Amith Yamasani | a93542f | 2016-02-03 18:02:06 -0800 | [diff] [blame] | 78 | // Screen on time should not keep progressing with screen is off |
Amith Yamasani | 61d5fd7 | 2017-02-24 11:02:07 -0800 | [diff] [blame] | 79 | assertEquals(aih.getScreenOnTime(7000), 3000); |
| 80 | assertEquals(aih.getScreenOnTime(8000), 3000); |
| 81 | aih.writeAppIdleDurations(); |
Amith Yamasani | a93542f | 2016-02-03 18:02:06 -0800 | [diff] [blame] | 82 | |
| 83 | // Check if the screen on time is persisted across instantiations |
| 84 | AppIdleHistory aih2 = new AppIdleHistory(mStorageDir, 0); |
Amith Yamasani | 61d5fd7 | 2017-02-24 11:02:07 -0800 | [diff] [blame] | 85 | assertEquals(aih2.getScreenOnTime(11000), 3000); |
| 86 | aih2.updateDisplay(true, 4000); |
| 87 | aih2.updateDisplay(false, 5000); |
| 88 | assertEquals(aih2.getScreenOnTime(13000), 4000); |
Amith Yamasani | a93542f | 2016-02-03 18:02:06 -0800 | [diff] [blame] | 89 | } |
| 90 | |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 91 | public void testBuckets() { |
Amith Yamasani | a93542f | 2016-02-03 18:02:06 -0800 | [diff] [blame] | 92 | AppIdleHistory aih = new AppIdleHistory(mStorageDir, 1000); |
Amith Yamasani | a93542f | 2016-02-03 18:02:06 -0800 | [diff] [blame] | 93 | |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 94 | aih.setAppStandbyBucket(PACKAGE_1, USER_ID, 1000, STANDBY_BUCKET_ACTIVE, |
Amith Yamasani | afbccb7 | 2017-11-27 10:44:24 -0800 | [diff] [blame] | 95 | UsageStatsManager.REASON_USAGE); |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 96 | // ACTIVE means not idle |
| 97 | assertFalse(aih.isIdle(PACKAGE_1, USER_ID, 2000)); |
| 98 | |
| 99 | aih.setAppStandbyBucket(PACKAGE_2, USER_ID, 2000, STANDBY_BUCKET_ACTIVE, |
Amith Yamasani | afbccb7 | 2017-11-27 10:44:24 -0800 | [diff] [blame] | 100 | UsageStatsManager.REASON_USAGE); |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 101 | aih.setAppStandbyBucket(PACKAGE_1, USER_ID, 3000, STANDBY_BUCKET_RARE, |
| 102 | REASON_TIMEOUT); |
| 103 | |
| 104 | assertEquals(aih.getAppStandbyBucket(PACKAGE_1, USER_ID, 3000), STANDBY_BUCKET_RARE); |
| 105 | assertEquals(aih.getAppStandbyBucket(PACKAGE_2, USER_ID, 3000), STANDBY_BUCKET_ACTIVE); |
| 106 | assertEquals(aih.getAppStandbyReason(PACKAGE_1, USER_ID, 3000), REASON_TIMEOUT); |
| 107 | |
| 108 | // RARE is considered idle |
| 109 | assertTrue(aih.isIdle(PACKAGE_1, USER_ID, 3000)); |
| 110 | assertFalse(aih.isIdle(PACKAGE_2, USER_ID, 3000)); |
| 111 | |
| 112 | // Check persistence |
| 113 | aih.writeAppIdleDurations(); |
| 114 | aih.writeAppIdleTimes(USER_ID); |
| 115 | aih = new AppIdleHistory(mStorageDir, 4000); |
| 116 | assertEquals(aih.getAppStandbyBucket(PACKAGE_1, USER_ID, 5000), STANDBY_BUCKET_RARE); |
| 117 | assertEquals(aih.getAppStandbyBucket(PACKAGE_2, USER_ID, 5000), STANDBY_BUCKET_ACTIVE); |
| 118 | assertEquals(aih.getAppStandbyReason(PACKAGE_1, USER_ID, 5000), REASON_TIMEOUT); |
Amith Yamasani | 84cd7b7 | 2017-11-07 13:59:37 -0800 | [diff] [blame] | 119 | |
| 120 | assertTrue(aih.shouldInformListeners(PACKAGE_1, USER_ID, 5000, STANDBY_BUCKET_RARE)); |
| 121 | assertFalse(aih.shouldInformListeners(PACKAGE_1, USER_ID, 5000, STANDBY_BUCKET_RARE)); |
| 122 | assertTrue(aih.shouldInformListeners(PACKAGE_1, USER_ID, 5000, STANDBY_BUCKET_FREQUENT)); |
Amith Yamasani | a93542f | 2016-02-03 18:02:06 -0800 | [diff] [blame] | 123 | } |
Amith Yamasani | 53f06ea | 2018-01-05 17:53:46 -0800 | [diff] [blame] | 124 | |
| 125 | public void testJobRunTime() throws Exception { |
| 126 | AppIdleHistory aih = new AppIdleHistory(mStorageDir, 1000); |
| 127 | |
| 128 | aih.setLastJobRunTime(PACKAGE_1, USER_ID, 2000); |
| 129 | assertEquals(Long.MAX_VALUE, aih.getTimeSinceLastJobRun(PACKAGE_2, USER_ID, 0)); |
| 130 | assertEquals(4000, aih.getTimeSinceLastJobRun(PACKAGE_1, USER_ID, 6000)); |
| 131 | |
| 132 | aih.setLastJobRunTime(PACKAGE_2, USER_ID, 6000); |
| 133 | assertEquals(1000, aih.getTimeSinceLastJobRun(PACKAGE_2, USER_ID, 7000)); |
| 134 | assertEquals(5000, aih.getTimeSinceLastJobRun(PACKAGE_1, USER_ID, 7000)); |
| 135 | } |
Amith Yamasani | a93542f | 2016-02-03 18:02:06 -0800 | [diff] [blame] | 136 | } |