1 /*
2  * Copyright (C) 2020 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.bluetooth.cts;
18 
19 import android.bluetooth.BluetoothAdapter;
20 import android.bluetooth.le.ScanRecord;
21 import android.content.BroadcastReceiver;
22 import android.content.Context;
23 import android.content.Intent;
24 import android.content.IntentFilter;
25 import android.content.pm.PackageManager;
26 import android.provider.Settings;
27 import android.util.Log;
28 
29 import junit.framework.Assert;
30 
31 import java.lang.reflect.InvocationTargetException;
32 import java.lang.reflect.Method;
33 import java.util.Arrays;
34 import java.util.concurrent.TimeUnit;
35 import java.util.concurrent.locks.Condition;
36 import java.util.concurrent.locks.ReentrantLock;
37 
38 /**
39  * Utility class for Bluetooth CTS test.
40  */
41 class TestUtils {
42     /**
43      * Utility method to call hidden ScanRecord.parseFromBytes method.
44      */
parseScanRecord(byte[] bytes)45     static ScanRecord parseScanRecord(byte[] bytes) {
46         Class<?> scanRecordClass = ScanRecord.class;
47         try {
48             Method method = scanRecordClass.getDeclaredMethod("parseFromBytes", byte[].class);
49             return (ScanRecord) method.invoke(null, bytes);
50         } catch (NoSuchMethodException | IllegalAccessException | IllegalArgumentException
51                 | InvocationTargetException e) {
52             return null;
53         }
54     }
55 
56     /**
57      * Assert two byte arrays are equal.
58      */
assertArrayEquals(byte[] expected, byte[] actual)59     static void assertArrayEquals(byte[] expected, byte[] actual) {
60         if (!Arrays.equals(expected, actual)) {
61             Assert.fail("expected:<" + Arrays.toString(expected) +
62                     "> but was:<" + Arrays.toString(actual) + ">");
63         }
64     }
65 
66     /**
67      * Get current location mode settings.
68      */
getLocationMode(Context context)69     static int getLocationMode(Context context) {
70         return Settings.Secure.getInt(context.getContentResolver(),
71                 Settings.Secure.LOCATION_MODE, Settings.Secure.LOCATION_MODE_OFF);
72     }
73 
74     /**
75      * Set location settings mode.
76      */
setLocationMode(Context context, int mode)77     static void setLocationMode(Context context, int mode) {
78         Settings.Secure.putInt(context.getContentResolver(), Settings.Secure.LOCATION_MODE,
79                 mode);
80     }
81 
82     /**
83      * Return true if location is on.
84      */
isLocationOn(Context context)85     static boolean isLocationOn(Context context) {
86         return getLocationMode(context) != Settings.Secure.LOCATION_MODE_OFF;
87     }
88 
89     /**
90      * Enable location and set the mode to GPS only.
91      */
enableLocation(Context context)92     static void enableLocation(Context context) {
93         setLocationMode(context, Settings.Secure.LOCATION_MODE_SENSORS_ONLY);
94     }
95 
96     /**
97      * Disable location.
98      */
disableLocation(Context context)99     static void disableLocation(Context context) {
100         setLocationMode(context, Settings.Secure.LOCATION_MODE_OFF);
101     }
102 
103     /**
104      * Check if BLE is supported by this platform
105      * @param context current device context
106      * @return true if BLE is supported, false otherwise
107      */
isBleSupported(Context context)108     static boolean isBleSupported(Context context) {
109         return context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE);
110     }
111 
112     /**
113      * Put the current thread to sleep.
114      * @param sleepMillis number of milliseconds to sleep for
115      */
sleep(int sleepMillis)116     static void sleep(int sleepMillis) {
117         try {
118             Thread.sleep(sleepMillis);
119         } catch (InterruptedException e) {
120             Log.e(TestUtils.class.getSimpleName(), "interrupted", e);
121         }
122     }
123 }