• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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.testutils;
18 
19 import android.content.Context;
20 
21 import com.android.settings.fuelgauge.batterytip.AnomalyDatabaseHelper;
22 import com.android.settings.fuelgauge.batterytip.BatteryDatabaseManager;
23 import com.android.settings.search.IndexDatabaseHelper;
24 import com.android.settings.slices.SlicesDatabaseHelper;
25 
26 import java.lang.reflect.Field;
27 
28 public class DatabaseTestUtils {
29 
clearDb(Context context)30     public static void clearDb(Context context) {
31         clearSearchDb(context);
32         clearSlicesDb(context);
33         clearAnomalyDb(context);
34         clearAnomalyDbManager();
35     }
36 
clearSlicesDb(Context context)37     private static void clearSlicesDb(Context context) {
38         SlicesDatabaseHelper helper = SlicesDatabaseHelper.getInstance(context);
39         helper.close();
40 
41         Field instance;
42         Class clazz = SlicesDatabaseHelper.class;
43         try {
44             instance = clazz.getDeclaredField("sSingleton");
45             instance.setAccessible(true);
46             instance.set(null, null);
47         } catch (Exception e) {
48             throw new RuntimeException();
49         }
50     }
51 
clearAnomalyDb(Context context)52     private static void clearAnomalyDb(Context context) {
53         AnomalyDatabaseHelper helper = AnomalyDatabaseHelper.getInstance(context);
54         helper.close();
55 
56         Field instance;
57         Class clazz = AnomalyDatabaseHelper.class;
58         try {
59             instance = clazz.getDeclaredField("sSingleton");
60             instance.setAccessible(true);
61             instance.set(null, null);
62         } catch (Exception e) {
63             throw new RuntimeException();
64         }
65     }
66 
clearSearchDb(Context context)67     private static void clearSearchDb(Context context) {
68         IndexDatabaseHelper helper = IndexDatabaseHelper.getInstance(context);
69         helper.close();
70 
71         Field instance;
72         Class clazz = IndexDatabaseHelper.class;
73         try {
74             instance = clazz.getDeclaredField("sSingleton");
75             instance.setAccessible(true);
76             instance.set(null, null);
77         } catch (Exception e) {
78             throw new RuntimeException();
79         }
80     }
81 
clearAnomalyDbManager()82     private static void clearAnomalyDbManager() {
83         Field instance;
84         Class clazz = BatteryDatabaseManager.class;
85         try {
86             instance = clazz.getDeclaredField("sSingleton");
87             instance.setAccessible(true);
88             instance.set(null, null);
89         } catch (Exception e) {
90             throw new RuntimeException();
91         }
92     }
93 }
94