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 android.database.sqlite.cts;
18 
19 import android.content.Context;
20 import android.util.Log;
21 
22 import androidx.test.InstrumentationRegistry;
23 import androidx.test.uiautomator.UiDevice;
24 
25 /**
26  * Common utility methods for testing
27  */
28 class DatabaseTestUtils {
29 
30     private static final String TAG = "SQLiteOpenHelperTest";
31 
waitForConnectionToClose(int maxAttempts, int pollIntervalMs)32     static boolean waitForConnectionToClose(int maxAttempts, int pollIntervalMs)
33             throws Exception {
34         for (int i = 0; i < maxAttempts; i++) {
35             String output = getDbInfoOutput();
36             Log.d(TAG, "waitForConnectionToClose #" + i + ": " + output);
37             if (!output.contains("Connection #0:")) {
38                 return true;
39             }
40             Thread.sleep(pollIntervalMs);
41         }
42         return false;
43     }
44 
getDbInfoOutput()45     static String getDbInfoOutput() throws Exception {
46         Context ctx = InstrumentationRegistry.getInstrumentation().getContext();
47         return executeShellCommand("dumpsys dbinfo " + ctx.getPackageName());
48     }
49 
executeShellCommand(String cmd)50     static String executeShellCommand(String cmd) throws Exception {
51         return UiDevice.getInstance(
52                 InstrumentationRegistry.getInstrumentation()).executeShellCommand(cmd);
53     }
54 }
55