Add GattDebugUtilsTest
Bug: 237467631
Test: atest GattDebugUtilsTest
Change-Id: Idd73132a5926b6e0aeebc64a78f2ca40cecbd6ac
diff --git a/android/app/src/com/android/bluetooth/gatt/GattDebugUtils.java b/android/app/src/com/android/bluetooth/gatt/GattDebugUtils.java
index 232477b..0a1bfb35 100644
--- a/android/app/src/com/android/bluetooth/gatt/GattDebugUtils.java
+++ b/android/app/src/com/android/bluetooth/gatt/GattDebugUtils.java
@@ -20,6 +20,9 @@
import android.os.Bundle;
import android.util.Log;
+import com.android.bluetooth.Utils;
+import com.android.internal.annotations.VisibleForTesting;
+
import java.util.UUID;
/**
@@ -29,17 +32,23 @@
private static final String TAG = GattServiceConfig.TAG_PREFIX + "DebugUtils";
private static final boolean DEBUG_ADMIN = GattServiceConfig.DEBUG_ADMIN;
- private static final String ACTION_GATT_PAIRING_CONFIG =
+ @VisibleForTesting
+ static final String ACTION_GATT_PAIRING_CONFIG =
"android.bluetooth.action.GATT_PAIRING_CONFIG";
- private static final String ACTION_GATT_TEST_USAGE = "android.bluetooth.action.GATT_TEST_USAGE";
- private static final String ACTION_GATT_TEST_ENABLE =
+ @VisibleForTesting
+ static final String ACTION_GATT_TEST_USAGE = "android.bluetooth.action.GATT_TEST_USAGE";
+ @VisibleForTesting
+ static final String ACTION_GATT_TEST_ENABLE =
"android.bluetooth.action.GATT_TEST_ENABLE";
- private static final String ACTION_GATT_TEST_CONNECT =
+ @VisibleForTesting
+ static final String ACTION_GATT_TEST_CONNECT =
"android.bluetooth.action.GATT_TEST_CONNECT";
- private static final String ACTION_GATT_TEST_DISCONNECT =
+ @VisibleForTesting
+ static final String ACTION_GATT_TEST_DISCONNECT =
"android.bluetooth.action.GATT_TEST_DISCONNECT";
- private static final String ACTION_GATT_TEST_DISCOVER =
+ @VisibleForTesting
+ static final String ACTION_GATT_TEST_DISCOVER =
"android.bluetooth.action.GATT_TEST_DISCOVER";
private static final String EXTRA_ENABLE = "enable";
@@ -69,7 +78,7 @@
* import com.android.bluetooth.gatt.GattService;
*/
static boolean handleDebugAction(GattService svc, Intent intent) {
- if (!DEBUG_ADMIN) {
+ if (!DEBUG_ADMIN && !Utils.isInstrumentationTestMode()) {
return false;
}
diff --git a/android/app/tests/unit/src/com/android/bluetooth/gatt/GattDebugUtilsTest.java b/android/app/tests/unit/src/com/android/bluetooth/gatt/GattDebugUtilsTest.java
new file mode 100644
index 0000000..ab83f1d
--- /dev/null
+++ b/android/app/tests/unit/src/com/android/bluetooth/gatt/GattDebugUtilsTest.java
@@ -0,0 +1,87 @@
+/*
+ * Copyright 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.bluetooth.gatt;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import static org.mockito.Mockito.verify;
+
+import android.content.Intent;
+
+import androidx.test.filters.SmallTest;
+import androidx.test.runner.AndroidJUnit4;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+
+/**
+ * Test cases for {@link GattDebugUtils}.
+ */
+@SmallTest
+@RunWith(AndroidJUnit4.class)
+public class GattDebugUtilsTest {
+
+ @Mock
+ private GattService mService;
+
+ @Before
+ public void setUp() throws Exception {
+ MockitoAnnotations.initMocks(this);
+ }
+
+ @Test
+ public void handleDebugAction() {
+ Intent intent = new Intent(GattDebugUtils.ACTION_GATT_TEST_USAGE);
+
+ boolean result = GattDebugUtils.handleDebugAction(mService, intent);
+ assertThat(result).isTrue();
+
+ intent = new Intent(GattDebugUtils.ACTION_GATT_TEST_ENABLE);
+ GattDebugUtils.handleDebugAction(mService, intent);
+ int bEnable = 1;
+ verify(mService).gattTestCommand(0x01, null, null, bEnable, 0, 0, 0, 0);
+
+ intent = new Intent(GattDebugUtils.ACTION_GATT_TEST_CONNECT);
+ GattDebugUtils.handleDebugAction(mService, intent);
+ int type = 2;
+ verify(mService).gattTestCommand(0x02, null, null, type, 0, 0, 0, 0);
+
+ intent = new Intent(GattDebugUtils.ACTION_GATT_TEST_DISCONNECT);
+ GattDebugUtils.handleDebugAction(mService, intent);
+ verify(mService).gattTestCommand(0x03, null, null, 0, 0, 0, 0, 0);
+
+ intent = new Intent(GattDebugUtils.ACTION_GATT_TEST_DISCOVER);
+ GattDebugUtils.handleDebugAction(mService, intent);
+ int typeDiscover = 1;
+ int shdl = 1;
+ int ehdl = 0xFFFF;
+ verify(mService).gattTestCommand(0x04, null, null, typeDiscover, shdl, ehdl, 0, 0);
+
+ intent = new Intent(GattDebugUtils.ACTION_GATT_PAIRING_CONFIG);
+ GattDebugUtils.handleDebugAction(mService, intent);
+ int authReq = 5;
+ int ioCap = 4;
+ int initKey = 7;
+ int respKey = 7;
+ int maxKey = 16;
+ verify(mService).gattTestCommand(0xF0, null, null, authReq, ioCap, initKey, respKey,
+ maxKey);
+ }
+}