• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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.settings.fuelgauge;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 import static org.mockito.Mockito.spy;
21 
22 import android.content.Context;
23 import android.text.format.DateUtils;
24 
25 import com.android.settings.fuelgauge.batterytip.AnomalyDatabaseHelper;
26 import com.android.settings.fuelgauge.batterytip.AppInfo;
27 import com.android.settings.fuelgauge.batterytip.BatteryDatabaseManager;
28 import com.android.settings.testutils.DatabaseTestUtils;
29 
30 import org.junit.After;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 import org.mockito.MockitoAnnotations;
35 import org.robolectric.RobolectricTestRunner;
36 import org.robolectric.RuntimeEnvironment;
37 
38 import java.util.ArrayList;
39 import java.util.List;
40 
41 @RunWith(RobolectricTestRunner.class)
42 public class BatteryDatabaseManagerTest {
43     private static String PACKAGE_NAME_NEW = "com.android.app1";
44     private static int UID_NEW = 345;
45     private static int TYPE_NEW = 1;
46     private static String PACKAGE_NAME_OLD = "com.android.app2";
47     private static int UID_OLD = 543;
48     private static int TYPE_OLD = 2;
49     private static long NOW = System.currentTimeMillis();
50     private static long ONE_DAY_BEFORE = NOW - DateUtils.DAY_IN_MILLIS;
51     private static long TWO_DAYS_BEFORE = NOW - 2 * DateUtils.DAY_IN_MILLIS;
52 
53     private Context mContext;
54     private BatteryDatabaseManager mBatteryDatabaseManager;
55     private AppInfo mNewAppInfo;
56     private AppInfo mOldAppInfo;
57     private AppInfo mCombinedAppInfo;
58 
59     @Before
setUp()60     public void setUp() {
61         MockitoAnnotations.initMocks(this);
62 
63         mContext = RuntimeEnvironment.application;
64         mBatteryDatabaseManager = spy(BatteryDatabaseManager.getInstance(mContext));
65 
66         mNewAppInfo = new AppInfo.Builder()
67                 .setUid(UID_NEW)
68                 .setPackageName(PACKAGE_NAME_NEW)
69                 .addAnomalyType(TYPE_NEW)
70                 .build();
71         mOldAppInfo = new AppInfo.Builder()
72                 .setUid(UID_OLD)
73                 .setPackageName(PACKAGE_NAME_OLD)
74                 .addAnomalyType(TYPE_OLD)
75                 .build();
76         mCombinedAppInfo = new AppInfo.Builder()
77                 .setUid(UID_NEW)
78                 .setPackageName(PACKAGE_NAME_NEW)
79                 .addAnomalyType(TYPE_NEW)
80                 .addAnomalyType(TYPE_OLD)
81                 .build();
82     }
83 
84     @After
cleanUp()85     public void cleanUp() {
86         DatabaseTestUtils.clearDb(mContext);
87     }
88 
89     @Test
testAllFunctions()90     public void testAllFunctions() {
91         mBatteryDatabaseManager.insertAnomaly(UID_NEW, PACKAGE_NAME_NEW, TYPE_NEW,
92                 AnomalyDatabaseHelper.State.NEW, NOW);
93         mBatteryDatabaseManager.insertAnomaly(UID_OLD, PACKAGE_NAME_OLD, TYPE_OLD,
94                 AnomalyDatabaseHelper.State.NEW, TWO_DAYS_BEFORE);
95 
96         // In database, it contains two record
97         List<AppInfo> totalAppInfos = mBatteryDatabaseManager.queryAllAnomalies(0 /* timeMsAfter */,
98                 AnomalyDatabaseHelper.State.NEW);
99         assertThat(totalAppInfos).containsExactly(mNewAppInfo, mOldAppInfo);
100 
101         // Only one record shows up if we query by timestamp
102         List<AppInfo> appInfos = mBatteryDatabaseManager.queryAllAnomalies(ONE_DAY_BEFORE,
103                 AnomalyDatabaseHelper.State.NEW);
104         assertThat(appInfos).containsExactly(mNewAppInfo);
105 
106         mBatteryDatabaseManager.deleteAllAnomaliesBeforeTimeStamp(ONE_DAY_BEFORE);
107 
108         // The obsolete record is removed from database
109         List<AppInfo> appInfos1 = mBatteryDatabaseManager.queryAllAnomalies(0 /* timeMsAfter */,
110                 AnomalyDatabaseHelper.State.NEW);
111         assertThat(appInfos1).containsExactly(mNewAppInfo);
112     }
113 
114     @Test
testUpdateAnomalies_updateSuccessfully()115     public void testUpdateAnomalies_updateSuccessfully() {
116         mBatteryDatabaseManager.insertAnomaly(UID_NEW, PACKAGE_NAME_NEW, TYPE_NEW,
117                 AnomalyDatabaseHelper.State.NEW, NOW);
118         mBatteryDatabaseManager.insertAnomaly(UID_OLD, PACKAGE_NAME_OLD, TYPE_OLD,
119                 AnomalyDatabaseHelper.State.NEW, NOW);
120         final AppInfo appInfo = new AppInfo.Builder().setPackageName(PACKAGE_NAME_OLD).build();
121         final List<AppInfo> updateAppInfos = new ArrayList<>();
122         updateAppInfos.add(appInfo);
123 
124         // Change state of PACKAGE_NAME_OLD to handled
125         mBatteryDatabaseManager.updateAnomalies(updateAppInfos,
126                 AnomalyDatabaseHelper.State.HANDLED);
127 
128         // The state of PACKAGE_NAME_NEW is still new
129         List<AppInfo> newAppInfos = mBatteryDatabaseManager.queryAllAnomalies(ONE_DAY_BEFORE,
130                 AnomalyDatabaseHelper.State.NEW);
131         assertThat(newAppInfos).containsExactly(mNewAppInfo);
132 
133         // The state of PACKAGE_NAME_OLD is changed to handled
134         List<AppInfo> handledAppInfos = mBatteryDatabaseManager.queryAllAnomalies(ONE_DAY_BEFORE,
135                 AnomalyDatabaseHelper.State.HANDLED);
136         assertThat(handledAppInfos).containsExactly(mOldAppInfo);
137     }
138 
139     @Test
testQueryAnomalies_removeDuplicateByUid()140     public void testQueryAnomalies_removeDuplicateByUid() {
141         mBatteryDatabaseManager.insertAnomaly(UID_NEW, PACKAGE_NAME_NEW, TYPE_NEW,
142                 AnomalyDatabaseHelper.State.NEW, NOW);
143         mBatteryDatabaseManager.insertAnomaly(UID_NEW, PACKAGE_NAME_NEW, TYPE_OLD,
144                 AnomalyDatabaseHelper.State.NEW, NOW);
145 
146         // Only contain one AppInfo with multiple types
147         List<AppInfo> newAppInfos = mBatteryDatabaseManager.queryAllAnomalies(ONE_DAY_BEFORE,
148                 AnomalyDatabaseHelper.State.NEW);
149         assertThat(newAppInfos).containsExactly(mCombinedAppInfo);
150     }
151 }
152