blob: 7b0664844df1c326571f6d2d9f25131fd408f4ef [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
Amith Yamasaniafbccb72017-11-27 10:44:24 -080019import static android.app.usage.UsageStatsManager.REASON_TIMEOUT;
20import static android.app.usage.UsageStatsManager.STANDBY_BUCKET_ACTIVE;
21import static android.app.usage.UsageStatsManager.STANDBY_BUCKET_FREQUENT;
22import static android.app.usage.UsageStatsManager.STANDBY_BUCKET_RARE;
23
Amith Yamasani53f06ea2018-01-05 17:53:46 -080024import static org.junit.Assert.assertEquals;
25import static org.junit.Assert.assertFalse;
26import static org.junit.Assert.assertTrue;
27
Amith Yamasaniafbccb72017-11-27 10:44:24 -080028import android.app.usage.UsageStatsManager;
Amith Yamasania93542f2016-02-03 18:02:06 -080029import android.os.FileUtils;
30import android.test.AndroidTestCase;
31
32import java.io.File;
33
34public 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 Yamasani17fffee2017-09-29 13:17:43 -070041 final static int USER_ID = 0;
42
Amith Yamasania93542f2016-02-03 18:02:06 -080043 @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 Yamasania93542f2016-02-03 18:02:06 -080057 AppIdleHistory aih = new AppIdleHistory(mStorageDir, 0);
58
Amith Yamasani61d5fd72017-02-24 11:02:07 -080059 aih.updateDisplay(true, /* elapsedRealtime= */ 1000);
60 aih.updateDisplay(false, /* elapsedRealtime= */ 2000);
Amith Yamasania93542f2016-02-03 18:02:06 -080061 // Screen On time file should be written right away
62 assertTrue(aih.getScreenOnTimeFile().exists());
63
Amith Yamasani17fffee2017-09-29 13:17:43 -070064 aih.writeAppIdleTimes(USER_ID);
Amith Yamasania93542f2016-02-03 18:02:06 -080065 // stats file should be written now
Amith Yamasani17fffee2017-09-29 13:17:43 -070066 assertTrue(new File(new File(mStorageDir, "users/" + USER_ID),
Amith Yamasania93542f2016-02-03 18:02:06 -080067 AppIdleHistory.APP_IDLE_FILENAME).exists());
68 }
69
70 public void testScreenOnTime() {
71 AppIdleHistory aih = new AppIdleHistory(mStorageDir, 1000);
Amith Yamasani61d5fd72017-02-24 11:02:07 -080072 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 Yamasania93542f2016-02-03 18:02:06 -080078 // Screen on time should not keep progressing with screen is off
Amith Yamasani61d5fd72017-02-24 11:02:07 -080079 assertEquals(aih.getScreenOnTime(7000), 3000);
80 assertEquals(aih.getScreenOnTime(8000), 3000);
81 aih.writeAppIdleDurations();
Amith Yamasania93542f2016-02-03 18:02:06 -080082
83 // Check if the screen on time is persisted across instantiations
84 AppIdleHistory aih2 = new AppIdleHistory(mStorageDir, 0);
Amith Yamasani61d5fd72017-02-24 11:02:07 -080085 assertEquals(aih2.getScreenOnTime(11000), 3000);
86 aih2.updateDisplay(true, 4000);
87 aih2.updateDisplay(false, 5000);
88 assertEquals(aih2.getScreenOnTime(13000), 4000);
Amith Yamasania93542f2016-02-03 18:02:06 -080089 }
90
Amith Yamasani17fffee2017-09-29 13:17:43 -070091 public void testBuckets() {
Amith Yamasania93542f2016-02-03 18:02:06 -080092 AppIdleHistory aih = new AppIdleHistory(mStorageDir, 1000);
Amith Yamasania93542f2016-02-03 18:02:06 -080093
Amith Yamasani17fffee2017-09-29 13:17:43 -070094 aih.setAppStandbyBucket(PACKAGE_1, USER_ID, 1000, STANDBY_BUCKET_ACTIVE,
Amith Yamasaniafbccb72017-11-27 10:44:24 -080095 UsageStatsManager.REASON_USAGE);
Amith Yamasani17fffee2017-09-29 13:17:43 -070096 // 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 Yamasaniafbccb72017-11-27 10:44:24 -0800100 UsageStatsManager.REASON_USAGE);
Amith Yamasani17fffee2017-09-29 13:17:43 -0700101 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 Yamasani84cd7b72017-11-07 13:59:37 -0800119
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 Yamasania93542f2016-02-03 18:02:06 -0800123 }
Amith Yamasani53f06ea2018-01-05 17:53:46 -0800124
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 Yamasania93542f2016-02-03 18:02:06 -0800136}