blob: b62d724152e3905ae886d60149716b12001bc64f [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
24import android.app.usage.UsageStatsManager;
Amith Yamasania93542f2016-02-03 18:02:06 -080025import android.os.FileUtils;
26import android.test.AndroidTestCase;
27
28import java.io.File;
29
30public class AppIdleHistoryTests extends AndroidTestCase {
31
32 File mStorageDir;
33
34 final static String PACKAGE_1 = "com.android.testpackage1";
35 final static String PACKAGE_2 = "com.android.testpackage2";
36
Amith Yamasani17fffee2017-09-29 13:17:43 -070037 final static int USER_ID = 0;
38
Amith Yamasania93542f2016-02-03 18:02:06 -080039 @Override
40 protected void setUp() throws Exception {
41 super.setUp();
42 mStorageDir = new File(getContext().getFilesDir(), "appidle");
43 mStorageDir.mkdirs();
44 }
45
46 @Override
47 protected void tearDown() throws Exception {
48 FileUtils.deleteContents(mStorageDir);
49 super.tearDown();
50 }
51
52 public void testFilesCreation() {
Amith Yamasania93542f2016-02-03 18:02:06 -080053 AppIdleHistory aih = new AppIdleHistory(mStorageDir, 0);
54
Amith Yamasani61d5fd72017-02-24 11:02:07 -080055 aih.updateDisplay(true, /* elapsedRealtime= */ 1000);
56 aih.updateDisplay(false, /* elapsedRealtime= */ 2000);
Amith Yamasania93542f2016-02-03 18:02:06 -080057 // Screen On time file should be written right away
58 assertTrue(aih.getScreenOnTimeFile().exists());
59
Amith Yamasani17fffee2017-09-29 13:17:43 -070060 aih.writeAppIdleTimes(USER_ID);
Amith Yamasania93542f2016-02-03 18:02:06 -080061 // stats file should be written now
Amith Yamasani17fffee2017-09-29 13:17:43 -070062 assertTrue(new File(new File(mStorageDir, "users/" + USER_ID),
Amith Yamasania93542f2016-02-03 18:02:06 -080063 AppIdleHistory.APP_IDLE_FILENAME).exists());
64 }
65
66 public void testScreenOnTime() {
67 AppIdleHistory aih = new AppIdleHistory(mStorageDir, 1000);
Amith Yamasani61d5fd72017-02-24 11:02:07 -080068 aih.updateDisplay(false, 2000);
69 assertEquals(aih.getScreenOnTime(2000), 0);
70 aih.updateDisplay(true, 3000);
71 assertEquals(aih.getScreenOnTime(4000), 1000);
72 assertEquals(aih.getScreenOnTime(5000), 2000);
73 aih.updateDisplay(false, 6000);
Amith Yamasania93542f2016-02-03 18:02:06 -080074 // Screen on time should not keep progressing with screen is off
Amith Yamasani61d5fd72017-02-24 11:02:07 -080075 assertEquals(aih.getScreenOnTime(7000), 3000);
76 assertEquals(aih.getScreenOnTime(8000), 3000);
77 aih.writeAppIdleDurations();
Amith Yamasania93542f2016-02-03 18:02:06 -080078
79 // Check if the screen on time is persisted across instantiations
80 AppIdleHistory aih2 = new AppIdleHistory(mStorageDir, 0);
Amith Yamasani61d5fd72017-02-24 11:02:07 -080081 assertEquals(aih2.getScreenOnTime(11000), 3000);
82 aih2.updateDisplay(true, 4000);
83 aih2.updateDisplay(false, 5000);
84 assertEquals(aih2.getScreenOnTime(13000), 4000);
Amith Yamasania93542f2016-02-03 18:02:06 -080085 }
86
Amith Yamasani17fffee2017-09-29 13:17:43 -070087 public void testBuckets() {
Amith Yamasania93542f2016-02-03 18:02:06 -080088 AppIdleHistory aih = new AppIdleHistory(mStorageDir, 1000);
Amith Yamasania93542f2016-02-03 18:02:06 -080089
Amith Yamasani17fffee2017-09-29 13:17:43 -070090 aih.setAppStandbyBucket(PACKAGE_1, USER_ID, 1000, STANDBY_BUCKET_ACTIVE,
Amith Yamasaniafbccb72017-11-27 10:44:24 -080091 UsageStatsManager.REASON_USAGE);
Amith Yamasani17fffee2017-09-29 13:17:43 -070092 // ACTIVE means not idle
93 assertFalse(aih.isIdle(PACKAGE_1, USER_ID, 2000));
94
95 aih.setAppStandbyBucket(PACKAGE_2, USER_ID, 2000, STANDBY_BUCKET_ACTIVE,
Amith Yamasaniafbccb72017-11-27 10:44:24 -080096 UsageStatsManager.REASON_USAGE);
Amith Yamasani17fffee2017-09-29 13:17:43 -070097 aih.setAppStandbyBucket(PACKAGE_1, USER_ID, 3000, STANDBY_BUCKET_RARE,
98 REASON_TIMEOUT);
99
100 assertEquals(aih.getAppStandbyBucket(PACKAGE_1, USER_ID, 3000), STANDBY_BUCKET_RARE);
101 assertEquals(aih.getAppStandbyBucket(PACKAGE_2, USER_ID, 3000), STANDBY_BUCKET_ACTIVE);
102 assertEquals(aih.getAppStandbyReason(PACKAGE_1, USER_ID, 3000), REASON_TIMEOUT);
103
104 // RARE is considered idle
105 assertTrue(aih.isIdle(PACKAGE_1, USER_ID, 3000));
106 assertFalse(aih.isIdle(PACKAGE_2, USER_ID, 3000));
107
108 // Check persistence
109 aih.writeAppIdleDurations();
110 aih.writeAppIdleTimes(USER_ID);
111 aih = new AppIdleHistory(mStorageDir, 4000);
112 assertEquals(aih.getAppStandbyBucket(PACKAGE_1, USER_ID, 5000), STANDBY_BUCKET_RARE);
113 assertEquals(aih.getAppStandbyBucket(PACKAGE_2, USER_ID, 5000), STANDBY_BUCKET_ACTIVE);
114 assertEquals(aih.getAppStandbyReason(PACKAGE_1, USER_ID, 5000), REASON_TIMEOUT);
Amith Yamasani84cd7b72017-11-07 13:59:37 -0800115
116 assertTrue(aih.shouldInformListeners(PACKAGE_1, USER_ID, 5000, STANDBY_BUCKET_RARE));
117 assertFalse(aih.shouldInformListeners(PACKAGE_1, USER_ID, 5000, STANDBY_BUCKET_RARE));
118 assertTrue(aih.shouldInformListeners(PACKAGE_1, USER_ID, 5000, STANDBY_BUCKET_FREQUENT));
Amith Yamasania93542f2016-02-03 18:02:06 -0800119 }
120}