blob: 88f368e403a66fcb66a005025da7fc1e5bbb4d9b [file] [log] [blame]
Amith Yamasania93542f2016-02-03 18:02:06 -08001/*
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
Kweku Adamsc6a9b342020-01-08 18:37:26 -080019import static android.app.usage.UsageStatsManager.REASON_MAIN_FORCED_BY_SYSTEM;
20import static android.app.usage.UsageStatsManager.REASON_MAIN_FORCED_BY_USER;
Amith Yamasani119be9a2018-02-18 22:23:00 -080021import static android.app.usage.UsageStatsManager.REASON_MAIN_TIMEOUT;
22import static android.app.usage.UsageStatsManager.REASON_MAIN_USAGE;
Kweku Adamsaa461942020-03-16 11:59:05 -070023import static android.app.usage.UsageStatsManager.REASON_SUB_FORCED_SYSTEM_FLAG_BACKGROUND_RESOURCE_USAGE;
Amith Yamasani119be9a2018-02-18 22:23:00 -080024import static android.app.usage.UsageStatsManager.REASON_SUB_USAGE_MOVE_TO_FOREGROUND;
Amith Yamasaniafbccb72017-11-27 10:44:24 -080025import static android.app.usage.UsageStatsManager.STANDBY_BUCKET_ACTIVE;
26import static android.app.usage.UsageStatsManager.STANDBY_BUCKET_FREQUENT;
27import static android.app.usage.UsageStatsManager.STANDBY_BUCKET_RARE;
Kweku Adamsc6a9b342020-01-08 18:37:26 -080028import static android.app.usage.UsageStatsManager.STANDBY_BUCKET_RESTRICTED;
Amith Yamasani119be9a2018-02-18 22:23:00 -080029import static android.app.usage.UsageStatsManager.STANDBY_BUCKET_WORKING_SET;
Amith Yamasaniafbccb72017-11-27 10:44:24 -080030
Amith Yamasania93542f2016-02-03 18:02:06 -080031import android.os.FileUtils;
32import android.test.AndroidTestCase;
33
34import java.io.File;
35
36public class AppIdleHistoryTests extends AndroidTestCase {
37
38 File mStorageDir;
39
Kweku Adamsc6a9b342020-01-08 18:37:26 -080040 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 Yamasania93542f2016-02-03 18:02:06 -080044
Kweku Adamsc6a9b342020-01-08 18:37:26 -080045 private static final int USER_ID = 0;
Amith Yamasani17fffee2017-09-29 13:17:43 -070046
Amith Yamasania93542f2016-02-03 18:02:06 -080047 @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 Yamasania93542f2016-02-03 18:02:06 -080061 AppIdleHistory aih = new AppIdleHistory(mStorageDir, 0);
62
Amith Yamasani61d5fd72017-02-24 11:02:07 -080063 aih.updateDisplay(true, /* elapsedRealtime= */ 1000);
64 aih.updateDisplay(false, /* elapsedRealtime= */ 2000);
Amith Yamasania93542f2016-02-03 18:02:06 -080065 // Screen On time file should be written right away
66 assertTrue(aih.getScreenOnTimeFile().exists());
67
Amith Yamasani17fffee2017-09-29 13:17:43 -070068 aih.writeAppIdleTimes(USER_ID);
Amith Yamasania93542f2016-02-03 18:02:06 -080069 // stats file should be written now
Amith Yamasani17fffee2017-09-29 13:17:43 -070070 assertTrue(new File(new File(mStorageDir, "users/" + USER_ID),
Amith Yamasania93542f2016-02-03 18:02:06 -080071 AppIdleHistory.APP_IDLE_FILENAME).exists());
72 }
73
74 public void testScreenOnTime() {
75 AppIdleHistory aih = new AppIdleHistory(mStorageDir, 1000);
Amith Yamasani61d5fd72017-02-24 11:02:07 -080076 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 Yamasania93542f2016-02-03 18:02:06 -080082 // Screen on time should not keep progressing with screen is off
Amith Yamasani61d5fd72017-02-24 11:02:07 -080083 assertEquals(aih.getScreenOnTime(7000), 3000);
84 assertEquals(aih.getScreenOnTime(8000), 3000);
85 aih.writeAppIdleDurations();
Amith Yamasania93542f2016-02-03 18:02:06 -080086
87 // Check if the screen on time is persisted across instantiations
88 AppIdleHistory aih2 = new AppIdleHistory(mStorageDir, 0);
Amith Yamasani61d5fd72017-02-24 11:02:07 -080089 assertEquals(aih2.getScreenOnTime(11000), 3000);
90 aih2.updateDisplay(true, 4000);
91 aih2.updateDisplay(false, 5000);
92 assertEquals(aih2.getScreenOnTime(13000), 4000);
Amith Yamasania93542f2016-02-03 18:02:06 -080093 }
94
Amith Yamasani17fffee2017-09-29 13:17:43 -070095 public void testBuckets() {
Amith Yamasania93542f2016-02-03 18:02:06 -080096 AppIdleHistory aih = new AppIdleHistory(mStorageDir, 1000);
Amith Yamasania93542f2016-02-03 18:02:06 -080097
Amith Yamasani17fffee2017-09-29 13:17:43 -070098 aih.setAppStandbyBucket(PACKAGE_1, USER_ID, 1000, STANDBY_BUCKET_ACTIVE,
Amith Yamasani119be9a2018-02-18 22:23:00 -080099 REASON_MAIN_USAGE);
Amith Yamasani17fffee2017-09-29 13:17:43 -0700100 // 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 Yamasani119be9a2018-02-18 22:23:00 -0800104 REASON_MAIN_USAGE);
Kweku Adamsc6a9b342020-01-08 18:37:26 -0800105 aih.setAppStandbyBucket(PACKAGE_3, USER_ID, 2500, STANDBY_BUCKET_RESTRICTED,
Kweku Adamsaa461942020-03-16 11:59:05 -0700106 REASON_MAIN_FORCED_BY_SYSTEM
107 | REASON_SUB_FORCED_SYSTEM_FLAG_BACKGROUND_RESOURCE_USAGE);
Kweku Adamsc6a9b342020-01-08 18:37:26 -0800108 aih.setAppStandbyBucket(PACKAGE_4, USER_ID, 2750, STANDBY_BUCKET_RESTRICTED,
109 REASON_MAIN_FORCED_BY_USER);
Amith Yamasani17fffee2017-09-29 13:17:43 -0700110 aih.setAppStandbyBucket(PACKAGE_1, USER_ID, 3000, STANDBY_BUCKET_RARE,
Amith Yamasani119be9a2018-02-18 22:23:00 -0800111 REASON_MAIN_TIMEOUT);
Amith Yamasani17fffee2017-09-29 13:17:43 -0700112
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 Yamasani119be9a2018-02-18 22:23:00 -0800115 assertEquals(aih.getAppStandbyReason(PACKAGE_1, USER_ID, 3000), REASON_MAIN_TIMEOUT);
Kweku Adamsc6a9b342020-01-08 18:37:26 -0800116 assertEquals(aih.getAppStandbyBucket(PACKAGE_3, USER_ID, 3000), STANDBY_BUCKET_RESTRICTED);
117 assertEquals(aih.getAppStandbyReason(PACKAGE_3, USER_ID, 3000),
Kweku Adamsaa461942020-03-16 11:59:05 -0700118 REASON_MAIN_FORCED_BY_SYSTEM
119 | REASON_SUB_FORCED_SYSTEM_FLAG_BACKGROUND_RESOURCE_USAGE);
Kweku Adamsc6a9b342020-01-08 18:37:26 -0800120 assertEquals(aih.getAppStandbyReason(PACKAGE_4, USER_ID, 3000),
121 REASON_MAIN_FORCED_BY_USER);
Amith Yamasani17fffee2017-09-29 13:17:43 -0700122
Kweku Adamsc6a9b342020-01-08 18:37:26 -0800123 // RARE and RESTRICTED are considered idle
Amith Yamasani17fffee2017-09-29 13:17:43 -0700124 assertTrue(aih.isIdle(PACKAGE_1, USER_ID, 3000));
125 assertFalse(aih.isIdle(PACKAGE_2, USER_ID, 3000));
Kweku Adamsc6a9b342020-01-08 18:37:26 -0800126 assertTrue(aih.isIdle(PACKAGE_3, USER_ID, 3000));
127 assertTrue(aih.isIdle(PACKAGE_4, USER_ID, 3000));
Amith Yamasani17fffee2017-09-29 13:17:43 -0700128
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 Yamasani119be9a2018-02-18 22:23:00 -0800135 assertEquals(aih.getAppStandbyReason(PACKAGE_1, USER_ID, 5000), REASON_MAIN_TIMEOUT);
Kweku Adamsc6a9b342020-01-08 18:37:26 -0800136 assertEquals(aih.getAppStandbyBucket(PACKAGE_3, USER_ID, 3000), STANDBY_BUCKET_RESTRICTED);
137 assertEquals(aih.getAppStandbyReason(PACKAGE_3, USER_ID, 3000),
Kweku Adamsaa461942020-03-16 11:59:05 -0700138 REASON_MAIN_FORCED_BY_SYSTEM
139 | REASON_SUB_FORCED_SYSTEM_FLAG_BACKGROUND_RESOURCE_USAGE);
Kweku Adamsc6a9b342020-01-08 18:37:26 -0800140 assertEquals(aih.getAppStandbyReason(PACKAGE_4, USER_ID, 3000),
141 REASON_MAIN_FORCED_BY_USER);
Amith Yamasani84cd7b72017-11-07 13:59:37 -0800142
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 Yamasania93542f2016-02-03 18:02:06 -0800146 }
Amith Yamasani53f06ea2018-01-05 17:53:46 -0800147
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 Yamasani119be9a2018-02-18 22:23:00 -0800159
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 Wachenschwanz16b2f2b2019-10-09 15:32:55 -0700173
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 Yamasania93542f2016-02-03 18:02:06 -0800190}