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 | |
| 24 | import android.app.usage.UsageStatsManager; |
Amith Yamasani | a93542f | 2016-02-03 18:02:06 -0800 | [diff] [blame] | 25 | import android.os.FileUtils; |
| 26 | import android.test.AndroidTestCase; |
| 27 | |
| 28 | import java.io.File; |
| 29 | |
| 30 | public 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 Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 37 | final static int USER_ID = 0; |
| 38 | |
Amith Yamasani | a93542f | 2016-02-03 18:02:06 -0800 | [diff] [blame] | 39 | @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 Yamasani | a93542f | 2016-02-03 18:02:06 -0800 | [diff] [blame] | 53 | AppIdleHistory aih = new AppIdleHistory(mStorageDir, 0); |
| 54 | |
Amith Yamasani | 61d5fd7 | 2017-02-24 11:02:07 -0800 | [diff] [blame] | 55 | aih.updateDisplay(true, /* elapsedRealtime= */ 1000); |
| 56 | aih.updateDisplay(false, /* elapsedRealtime= */ 2000); |
Amith Yamasani | a93542f | 2016-02-03 18:02:06 -0800 | [diff] [blame] | 57 | // Screen On time file should be written right away |
| 58 | assertTrue(aih.getScreenOnTimeFile().exists()); |
| 59 | |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 60 | aih.writeAppIdleTimes(USER_ID); |
Amith Yamasani | a93542f | 2016-02-03 18:02:06 -0800 | [diff] [blame] | 61 | // stats file should be written now |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 62 | assertTrue(new File(new File(mStorageDir, "users/" + USER_ID), |
Amith Yamasani | a93542f | 2016-02-03 18:02:06 -0800 | [diff] [blame] | 63 | AppIdleHistory.APP_IDLE_FILENAME).exists()); |
| 64 | } |
| 65 | |
| 66 | public void testScreenOnTime() { |
| 67 | AppIdleHistory aih = new AppIdleHistory(mStorageDir, 1000); |
Amith Yamasani | 61d5fd7 | 2017-02-24 11:02:07 -0800 | [diff] [blame] | 68 | 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 Yamasani | a93542f | 2016-02-03 18:02:06 -0800 | [diff] [blame] | 74 | // Screen on time should not keep progressing with screen is off |
Amith Yamasani | 61d5fd7 | 2017-02-24 11:02:07 -0800 | [diff] [blame] | 75 | assertEquals(aih.getScreenOnTime(7000), 3000); |
| 76 | assertEquals(aih.getScreenOnTime(8000), 3000); |
| 77 | aih.writeAppIdleDurations(); |
Amith Yamasani | a93542f | 2016-02-03 18:02:06 -0800 | [diff] [blame] | 78 | |
| 79 | // Check if the screen on time is persisted across instantiations |
| 80 | AppIdleHistory aih2 = new AppIdleHistory(mStorageDir, 0); |
Amith Yamasani | 61d5fd7 | 2017-02-24 11:02:07 -0800 | [diff] [blame] | 81 | assertEquals(aih2.getScreenOnTime(11000), 3000); |
| 82 | aih2.updateDisplay(true, 4000); |
| 83 | aih2.updateDisplay(false, 5000); |
| 84 | assertEquals(aih2.getScreenOnTime(13000), 4000); |
Amith Yamasani | a93542f | 2016-02-03 18:02:06 -0800 | [diff] [blame] | 85 | } |
| 86 | |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 87 | public void testBuckets() { |
Amith Yamasani | a93542f | 2016-02-03 18:02:06 -0800 | [diff] [blame] | 88 | AppIdleHistory aih = new AppIdleHistory(mStorageDir, 1000); |
Amith Yamasani | a93542f | 2016-02-03 18:02:06 -0800 | [diff] [blame] | 89 | |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 90 | aih.setAppStandbyBucket(PACKAGE_1, USER_ID, 1000, STANDBY_BUCKET_ACTIVE, |
Amith Yamasani | afbccb7 | 2017-11-27 10:44:24 -0800 | [diff] [blame] | 91 | UsageStatsManager.REASON_USAGE); |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 92 | // 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 Yamasani | afbccb7 | 2017-11-27 10:44:24 -0800 | [diff] [blame] | 96 | UsageStatsManager.REASON_USAGE); |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 97 | 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 Yamasani | 84cd7b7 | 2017-11-07 13:59:37 -0800 | [diff] [blame] | 115 | |
| 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 Yamasani | a93542f | 2016-02-03 18:02:06 -0800 | [diff] [blame] | 119 | } |
| 120 | } |