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 | |
Kweku Adams | c6a9b34 | 2020-01-08 18:37:26 -0800 | [diff] [blame] | 19 | import static android.app.usage.UsageStatsManager.REASON_MAIN_FORCED_BY_SYSTEM; |
| 20 | import static android.app.usage.UsageStatsManager.REASON_MAIN_FORCED_BY_USER; |
Amith Yamasani | 119be9a | 2018-02-18 22:23:00 -0800 | [diff] [blame] | 21 | import static android.app.usage.UsageStatsManager.REASON_MAIN_TIMEOUT; |
| 22 | import static android.app.usage.UsageStatsManager.REASON_MAIN_USAGE; |
Kweku Adams | aa46194 | 2020-03-16 11:59:05 -0700 | [diff] [blame^] | 23 | import static android.app.usage.UsageStatsManager.REASON_SUB_FORCED_SYSTEM_FLAG_BACKGROUND_RESOURCE_USAGE; |
Amith Yamasani | 119be9a | 2018-02-18 22:23:00 -0800 | [diff] [blame] | 24 | import static android.app.usage.UsageStatsManager.REASON_SUB_USAGE_MOVE_TO_FOREGROUND; |
Amith Yamasani | afbccb7 | 2017-11-27 10:44:24 -0800 | [diff] [blame] | 25 | import static android.app.usage.UsageStatsManager.STANDBY_BUCKET_ACTIVE; |
| 26 | import static android.app.usage.UsageStatsManager.STANDBY_BUCKET_FREQUENT; |
| 27 | import static android.app.usage.UsageStatsManager.STANDBY_BUCKET_RARE; |
Kweku Adams | c6a9b34 | 2020-01-08 18:37:26 -0800 | [diff] [blame] | 28 | import static android.app.usage.UsageStatsManager.STANDBY_BUCKET_RESTRICTED; |
Amith Yamasani | 119be9a | 2018-02-18 22:23:00 -0800 | [diff] [blame] | 29 | import static android.app.usage.UsageStatsManager.STANDBY_BUCKET_WORKING_SET; |
Amith Yamasani | afbccb7 | 2017-11-27 10:44:24 -0800 | [diff] [blame] | 30 | |
Amith Yamasani | a93542f | 2016-02-03 18:02:06 -0800 | [diff] [blame] | 31 | import android.os.FileUtils; |
| 32 | import android.test.AndroidTestCase; |
| 33 | |
| 34 | import java.io.File; |
| 35 | |
| 36 | public class AppIdleHistoryTests extends AndroidTestCase { |
| 37 | |
| 38 | File mStorageDir; |
| 39 | |
Kweku Adams | c6a9b34 | 2020-01-08 18:37:26 -0800 | [diff] [blame] | 40 | private static final String PACKAGE_1 = "com.android.testpackage1"; |
| 41 | private static final String PACKAGE_2 = "com.android.testpackage2"; |
| 42 | private static final String PACKAGE_3 = "com.android.testpackage3"; |
| 43 | private static final String PACKAGE_4 = "com.android.testpackage4"; |
Amith Yamasani | a93542f | 2016-02-03 18:02:06 -0800 | [diff] [blame] | 44 | |
Kweku Adams | c6a9b34 | 2020-01-08 18:37:26 -0800 | [diff] [blame] | 45 | private static final int USER_ID = 0; |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 46 | |
Amith Yamasani | a93542f | 2016-02-03 18:02:06 -0800 | [diff] [blame] | 47 | @Override |
| 48 | protected void setUp() throws Exception { |
| 49 | super.setUp(); |
| 50 | mStorageDir = new File(getContext().getFilesDir(), "appidle"); |
| 51 | mStorageDir.mkdirs(); |
| 52 | } |
| 53 | |
| 54 | @Override |
| 55 | protected void tearDown() throws Exception { |
| 56 | FileUtils.deleteContents(mStorageDir); |
| 57 | super.tearDown(); |
| 58 | } |
| 59 | |
| 60 | public void testFilesCreation() { |
Amith Yamasani | a93542f | 2016-02-03 18:02:06 -0800 | [diff] [blame] | 61 | AppIdleHistory aih = new AppIdleHistory(mStorageDir, 0); |
| 62 | |
Amith Yamasani | 61d5fd7 | 2017-02-24 11:02:07 -0800 | [diff] [blame] | 63 | aih.updateDisplay(true, /* elapsedRealtime= */ 1000); |
| 64 | aih.updateDisplay(false, /* elapsedRealtime= */ 2000); |
Amith Yamasani | a93542f | 2016-02-03 18:02:06 -0800 | [diff] [blame] | 65 | // Screen On time file should be written right away |
| 66 | assertTrue(aih.getScreenOnTimeFile().exists()); |
| 67 | |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 68 | aih.writeAppIdleTimes(USER_ID); |
Amith Yamasani | a93542f | 2016-02-03 18:02:06 -0800 | [diff] [blame] | 69 | // stats file should be written now |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 70 | assertTrue(new File(new File(mStorageDir, "users/" + USER_ID), |
Amith Yamasani | a93542f | 2016-02-03 18:02:06 -0800 | [diff] [blame] | 71 | AppIdleHistory.APP_IDLE_FILENAME).exists()); |
| 72 | } |
| 73 | |
| 74 | public void testScreenOnTime() { |
| 75 | AppIdleHistory aih = new AppIdleHistory(mStorageDir, 1000); |
Amith Yamasani | 61d5fd7 | 2017-02-24 11:02:07 -0800 | [diff] [blame] | 76 | aih.updateDisplay(false, 2000); |
| 77 | assertEquals(aih.getScreenOnTime(2000), 0); |
| 78 | aih.updateDisplay(true, 3000); |
| 79 | assertEquals(aih.getScreenOnTime(4000), 1000); |
| 80 | assertEquals(aih.getScreenOnTime(5000), 2000); |
| 81 | aih.updateDisplay(false, 6000); |
Amith Yamasani | a93542f | 2016-02-03 18:02:06 -0800 | [diff] [blame] | 82 | // Screen on time should not keep progressing with screen is off |
Amith Yamasani | 61d5fd7 | 2017-02-24 11:02:07 -0800 | [diff] [blame] | 83 | assertEquals(aih.getScreenOnTime(7000), 3000); |
| 84 | assertEquals(aih.getScreenOnTime(8000), 3000); |
| 85 | aih.writeAppIdleDurations(); |
Amith Yamasani | a93542f | 2016-02-03 18:02:06 -0800 | [diff] [blame] | 86 | |
| 87 | // Check if the screen on time is persisted across instantiations |
| 88 | AppIdleHistory aih2 = new AppIdleHistory(mStorageDir, 0); |
Amith Yamasani | 61d5fd7 | 2017-02-24 11:02:07 -0800 | [diff] [blame] | 89 | assertEquals(aih2.getScreenOnTime(11000), 3000); |
| 90 | aih2.updateDisplay(true, 4000); |
| 91 | aih2.updateDisplay(false, 5000); |
| 92 | assertEquals(aih2.getScreenOnTime(13000), 4000); |
Amith Yamasani | a93542f | 2016-02-03 18:02:06 -0800 | [diff] [blame] | 93 | } |
| 94 | |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 95 | public void testBuckets() { |
Amith Yamasani | a93542f | 2016-02-03 18:02:06 -0800 | [diff] [blame] | 96 | AppIdleHistory aih = new AppIdleHistory(mStorageDir, 1000); |
Amith Yamasani | a93542f | 2016-02-03 18:02:06 -0800 | [diff] [blame] | 97 | |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 98 | aih.setAppStandbyBucket(PACKAGE_1, USER_ID, 1000, STANDBY_BUCKET_ACTIVE, |
Amith Yamasani | 119be9a | 2018-02-18 22:23:00 -0800 | [diff] [blame] | 99 | REASON_MAIN_USAGE); |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 100 | // ACTIVE means not idle |
| 101 | assertFalse(aih.isIdle(PACKAGE_1, USER_ID, 2000)); |
| 102 | |
| 103 | aih.setAppStandbyBucket(PACKAGE_2, USER_ID, 2000, STANDBY_BUCKET_ACTIVE, |
Amith Yamasani | 119be9a | 2018-02-18 22:23:00 -0800 | [diff] [blame] | 104 | REASON_MAIN_USAGE); |
Kweku Adams | c6a9b34 | 2020-01-08 18:37:26 -0800 | [diff] [blame] | 105 | aih.setAppStandbyBucket(PACKAGE_3, USER_ID, 2500, STANDBY_BUCKET_RESTRICTED, |
Kweku Adams | aa46194 | 2020-03-16 11:59:05 -0700 | [diff] [blame^] | 106 | REASON_MAIN_FORCED_BY_SYSTEM |
| 107 | | REASON_SUB_FORCED_SYSTEM_FLAG_BACKGROUND_RESOURCE_USAGE); |
Kweku Adams | c6a9b34 | 2020-01-08 18:37:26 -0800 | [diff] [blame] | 108 | aih.setAppStandbyBucket(PACKAGE_4, USER_ID, 2750, STANDBY_BUCKET_RESTRICTED, |
| 109 | REASON_MAIN_FORCED_BY_USER); |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 110 | aih.setAppStandbyBucket(PACKAGE_1, USER_ID, 3000, STANDBY_BUCKET_RARE, |
Amith Yamasani | 119be9a | 2018-02-18 22:23:00 -0800 | [diff] [blame] | 111 | REASON_MAIN_TIMEOUT); |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 112 | |
| 113 | assertEquals(aih.getAppStandbyBucket(PACKAGE_1, USER_ID, 3000), STANDBY_BUCKET_RARE); |
| 114 | assertEquals(aih.getAppStandbyBucket(PACKAGE_2, USER_ID, 3000), STANDBY_BUCKET_ACTIVE); |
Amith Yamasani | 119be9a | 2018-02-18 22:23:00 -0800 | [diff] [blame] | 115 | assertEquals(aih.getAppStandbyReason(PACKAGE_1, USER_ID, 3000), REASON_MAIN_TIMEOUT); |
Kweku Adams | c6a9b34 | 2020-01-08 18:37:26 -0800 | [diff] [blame] | 116 | assertEquals(aih.getAppStandbyBucket(PACKAGE_3, USER_ID, 3000), STANDBY_BUCKET_RESTRICTED); |
| 117 | assertEquals(aih.getAppStandbyReason(PACKAGE_3, USER_ID, 3000), |
Kweku Adams | aa46194 | 2020-03-16 11:59:05 -0700 | [diff] [blame^] | 118 | REASON_MAIN_FORCED_BY_SYSTEM |
| 119 | | REASON_SUB_FORCED_SYSTEM_FLAG_BACKGROUND_RESOURCE_USAGE); |
Kweku Adams | c6a9b34 | 2020-01-08 18:37:26 -0800 | [diff] [blame] | 120 | assertEquals(aih.getAppStandbyReason(PACKAGE_4, USER_ID, 3000), |
| 121 | REASON_MAIN_FORCED_BY_USER); |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 122 | |
Kweku Adams | c6a9b34 | 2020-01-08 18:37:26 -0800 | [diff] [blame] | 123 | // RARE and RESTRICTED are considered idle |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 124 | assertTrue(aih.isIdle(PACKAGE_1, USER_ID, 3000)); |
| 125 | assertFalse(aih.isIdle(PACKAGE_2, USER_ID, 3000)); |
Kweku Adams | c6a9b34 | 2020-01-08 18:37:26 -0800 | [diff] [blame] | 126 | assertTrue(aih.isIdle(PACKAGE_3, USER_ID, 3000)); |
| 127 | assertTrue(aih.isIdle(PACKAGE_4, USER_ID, 3000)); |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 128 | |
| 129 | // Check persistence |
| 130 | aih.writeAppIdleDurations(); |
| 131 | aih.writeAppIdleTimes(USER_ID); |
| 132 | aih = new AppIdleHistory(mStorageDir, 4000); |
| 133 | assertEquals(aih.getAppStandbyBucket(PACKAGE_1, USER_ID, 5000), STANDBY_BUCKET_RARE); |
| 134 | assertEquals(aih.getAppStandbyBucket(PACKAGE_2, USER_ID, 5000), STANDBY_BUCKET_ACTIVE); |
Amith Yamasani | 119be9a | 2018-02-18 22:23:00 -0800 | [diff] [blame] | 135 | assertEquals(aih.getAppStandbyReason(PACKAGE_1, USER_ID, 5000), REASON_MAIN_TIMEOUT); |
Kweku Adams | c6a9b34 | 2020-01-08 18:37:26 -0800 | [diff] [blame] | 136 | assertEquals(aih.getAppStandbyBucket(PACKAGE_3, USER_ID, 3000), STANDBY_BUCKET_RESTRICTED); |
| 137 | assertEquals(aih.getAppStandbyReason(PACKAGE_3, USER_ID, 3000), |
Kweku Adams | aa46194 | 2020-03-16 11:59:05 -0700 | [diff] [blame^] | 138 | REASON_MAIN_FORCED_BY_SYSTEM |
| 139 | | REASON_SUB_FORCED_SYSTEM_FLAG_BACKGROUND_RESOURCE_USAGE); |
Kweku Adams | c6a9b34 | 2020-01-08 18:37:26 -0800 | [diff] [blame] | 140 | assertEquals(aih.getAppStandbyReason(PACKAGE_4, USER_ID, 3000), |
| 141 | REASON_MAIN_FORCED_BY_USER); |
Amith Yamasani | 84cd7b7 | 2017-11-07 13:59:37 -0800 | [diff] [blame] | 142 | |
| 143 | assertTrue(aih.shouldInformListeners(PACKAGE_1, USER_ID, 5000, STANDBY_BUCKET_RARE)); |
| 144 | assertFalse(aih.shouldInformListeners(PACKAGE_1, USER_ID, 5000, STANDBY_BUCKET_RARE)); |
| 145 | assertTrue(aih.shouldInformListeners(PACKAGE_1, USER_ID, 5000, STANDBY_BUCKET_FREQUENT)); |
Amith Yamasani | a93542f | 2016-02-03 18:02:06 -0800 | [diff] [blame] | 146 | } |
Amith Yamasani | 53f06ea | 2018-01-05 17:53:46 -0800 | [diff] [blame] | 147 | |
| 148 | public void testJobRunTime() throws Exception { |
| 149 | AppIdleHistory aih = new AppIdleHistory(mStorageDir, 1000); |
| 150 | |
| 151 | aih.setLastJobRunTime(PACKAGE_1, USER_ID, 2000); |
| 152 | assertEquals(Long.MAX_VALUE, aih.getTimeSinceLastJobRun(PACKAGE_2, USER_ID, 0)); |
| 153 | assertEquals(4000, aih.getTimeSinceLastJobRun(PACKAGE_1, USER_ID, 6000)); |
| 154 | |
| 155 | aih.setLastJobRunTime(PACKAGE_2, USER_ID, 6000); |
| 156 | assertEquals(1000, aih.getTimeSinceLastJobRun(PACKAGE_2, USER_ID, 7000)); |
| 157 | assertEquals(5000, aih.getTimeSinceLastJobRun(PACKAGE_1, USER_ID, 7000)); |
| 158 | } |
Amith Yamasani | 119be9a | 2018-02-18 22:23:00 -0800 | [diff] [blame] | 159 | |
| 160 | public void testReason() throws Exception { |
| 161 | AppIdleHistory aih = new AppIdleHistory(mStorageDir, 1000); |
| 162 | aih.reportUsage(PACKAGE_1, USER_ID, STANDBY_BUCKET_ACTIVE, |
| 163 | REASON_SUB_USAGE_MOVE_TO_FOREGROUND, 2000, 0); |
| 164 | assertEquals(REASON_MAIN_USAGE | REASON_SUB_USAGE_MOVE_TO_FOREGROUND, |
| 165 | aih.getAppStandbyReason(PACKAGE_1, USER_ID, 3000)); |
| 166 | aih.setAppStandbyBucket(PACKAGE_1, USER_ID, 4000, STANDBY_BUCKET_WORKING_SET, |
| 167 | REASON_MAIN_TIMEOUT); |
| 168 | aih.writeAppIdleTimes(USER_ID); |
| 169 | |
| 170 | aih = new AppIdleHistory(mStorageDir, 5000); |
| 171 | assertEquals(REASON_MAIN_TIMEOUT, aih.getAppStandbyReason(PACKAGE_1, USER_ID, 5000)); |
| 172 | } |
Michael Wachenschwanz | 16b2f2b | 2019-10-09 15:32:55 -0700 | [diff] [blame] | 173 | |
| 174 | public void testNullPackage() throws Exception { |
| 175 | AppIdleHistory aih = new AppIdleHistory(mStorageDir, 1000); |
| 176 | // Report usage of a package |
| 177 | aih.reportUsage(PACKAGE_1, USER_ID, STANDBY_BUCKET_ACTIVE, |
| 178 | REASON_SUB_USAGE_MOVE_TO_FOREGROUND, 2000, 0); |
| 179 | // "Accidentally" report usage against a null named package |
| 180 | aih.reportUsage(null, USER_ID, STANDBY_BUCKET_ACTIVE, |
| 181 | REASON_SUB_USAGE_MOVE_TO_FOREGROUND, 2000, 0); |
| 182 | // Persist data |
| 183 | aih.writeAppIdleTimes(USER_ID); |
| 184 | // Recover data from disk |
| 185 | aih = new AppIdleHistory(mStorageDir, 5000); |
| 186 | // Verify data is intact |
| 187 | assertEquals(REASON_MAIN_USAGE | REASON_SUB_USAGE_MOVE_TO_FOREGROUND, |
| 188 | aih.getAppStandbyReason(PACKAGE_1, USER_ID, 3000)); |
| 189 | } |
Amith Yamasani | a93542f | 2016-02-03 18:02:06 -0800 | [diff] [blame] | 190 | } |