Merge "Add test for GattConnectionManager." into tm-dev
diff --git a/nearby/service/java/com/android/server/nearby/common/bluetooth/fastpair/GattConnectionManager.java b/nearby/service/java/com/android/server/nearby/common/bluetooth/fastpair/GattConnectionManager.java
index ff0efc7..e7ce4bf 100644
--- a/nearby/service/java/com/android/server/nearby/common/bluetooth/fastpair/GattConnectionManager.java
+++ b/nearby/service/java/com/android/server/nearby/common/bluetooth/fastpair/GattConnectionManager.java
@@ -70,6 +70,11 @@
private final FastPairConnection.FastPairSignalChecker mFastPairSignalChecker;
@Nullable
private BluetoothGattConnection mGattConnection;
+ private static boolean sTestMode = false;
+
+ static void enableTestMode() {
+ sTestMode = true;
+ }
GattConnectionManager(
Context context,
@@ -147,6 +152,9 @@
try (ScopedTiming scopedTiming =
new ScopedTiming(mTimingLogger, "Connect GATT #" + i)) {
Log.i(TAG, "Connecting to GATT server at " + maskBluetoothAddress(address));
+ if (sTestMode) {
+ return null;
+ }
BluetoothGattConnection connection =
new BluetoothGattHelper(mContext, mBluetoothAdapter)
.connect(
diff --git a/nearby/tests/unit/src/com/android/server/nearby/common/bluetooth/fastpair/GattConnectionManagerTest.java b/nearby/tests/unit/src/com/android/server/nearby/common/bluetooth/fastpair/GattConnectionManagerTest.java
new file mode 100644
index 0000000..2f80a30
--- /dev/null
+++ b/nearby/tests/unit/src/com/android/server/nearby/common/bluetooth/fastpair/GattConnectionManagerTest.java
@@ -0,0 +1,154 @@
+/*
+ * Copyright (C) 2021 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.server.nearby.common.bluetooth.fastpair;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import android.platform.test.annotations.Presubmit;
+
+import androidx.annotation.Nullable;
+import androidx.test.core.app.ApplicationProvider;
+import androidx.test.filters.SdkSuppress;
+import androidx.test.filters.SmallTest;
+
+import com.android.server.nearby.common.bluetooth.BluetoothException;
+import com.android.server.nearby.common.bluetooth.BluetoothGattException;
+import com.android.server.nearby.common.bluetooth.testability.android.bluetooth.BluetoothAdapter;
+import com.android.server.nearby.common.bluetooth.util.BluetoothOperationExecutor.BluetoothOperationTimeoutException;
+
+import com.google.common.collect.ImmutableSet;
+
+import junit.framework.TestCase;
+
+import java.time.Duration;
+import java.util.concurrent.ExecutionException;
+
+/**
+ * Unit tests for {@link GattConnectionManager}.
+ */
+@Presubmit
+@SmallTest
+public class GattConnectionManagerTest extends TestCase {
+
+ private static final String FAST_PAIR_ADDRESS = "BB:BB:BB:BB:BB:1E";
+
+ @Override
+ public void setUp() throws Exception {
+ super.setUp();
+
+ GattConnectionManager.enableTestMode();
+ }
+
+ @SdkSuppress(minSdkVersion = 32, codeName = "T")
+ public void testGattConnectionManagerConstructor() throws Exception {
+ GattConnectionManager manager = createManager(Preferences.builder());
+ try {
+ manager.getConnection();
+ } catch (ExecutionException e) {
+ }
+ }
+
+ @SdkSuppress(minSdkVersion = 32, codeName = "T")
+ public void testIsNoRetryError() {
+ Preferences preferences =
+ Preferences.builder()
+ .setGattConnectionAndSecretHandshakeNoRetryGattError(
+ ImmutableSet.of(257, 999))
+ .build();
+
+ assertThat(
+ GattConnectionManager.isNoRetryError(
+ preferences, new BluetoothGattException("Test", 133)))
+ .isFalse();
+ assertThat(
+ GattConnectionManager.isNoRetryError(
+ preferences, new BluetoothGattException("Test", 257)))
+ .isTrue();
+ assertThat(
+ GattConnectionManager.isNoRetryError(
+ preferences, new BluetoothGattException("Test", 999)))
+ .isTrue();
+ assertThat(GattConnectionManager.isNoRetryError(
+ preferences, new BluetoothException("Test")))
+ .isFalse();
+ assertThat(
+ GattConnectionManager.isNoRetryError(
+ preferences, new BluetoothOperationTimeoutException("Test")))
+ .isFalse();
+ }
+
+ @SdkSuppress(minSdkVersion = 32, codeName = "T")
+ public void testGetTimeoutNotOverShortRetryMaxSpentTimeGetShort() {
+ Preferences preferences = Preferences.builder().build();
+
+ assertThat(
+ createManager(Preferences.builder(), () -> {})
+ .getTimeoutMs(
+ preferences.getGattConnectShortTimeoutRetryMaxSpentTimeMs() - 1))
+ .isEqualTo(preferences.getGattConnectShortTimeoutMs());
+ }
+
+ @SdkSuppress(minSdkVersion = 32, codeName = "T")
+ public void testGetTimeoutOverShortRetryMaxSpentTimeGetLong() {
+ Preferences preferences = Preferences.builder().build();
+
+ assertThat(
+ createManager(Preferences.builder(), () -> {})
+ .getTimeoutMs(
+ preferences.getGattConnectShortTimeoutRetryMaxSpentTimeMs() + 1))
+ .isEqualTo(preferences.getGattConnectLongTimeoutMs());
+ }
+
+ @SdkSuppress(minSdkVersion = 32, codeName = "T")
+ public void testGetTimeoutRetryNotEnabledGetOrigin() {
+ Preferences preferences = Preferences.builder().build();
+
+ assertThat(
+ createManager(
+ Preferences.builder().setRetryGattConnectionAndSecretHandshake(false),
+ () -> {})
+ .getTimeoutMs(0))
+ .isEqualTo(Duration.ofSeconds(
+ preferences.getGattConnectionTimeoutSeconds()).toMillis());
+ }
+
+ private GattConnectionManager createManager(Preferences.Builder prefs) {
+ return createManager(prefs, () -> {});
+ }
+
+ private GattConnectionManager createManager(
+ Preferences.Builder prefs, ToggleBluetoothTask toggleBluetooth) {
+ return createManager(prefs, toggleBluetooth,
+ /* fastPairSignalChecker= */ null);
+ }
+
+ private GattConnectionManager createManager(
+ Preferences.Builder prefs,
+ ToggleBluetoothTask toggleBluetooth,
+ @Nullable FastPairConnection.FastPairSignalChecker fastPairSignalChecker) {
+ return new GattConnectionManager(
+ ApplicationProvider.getApplicationContext(),
+ prefs.build(),
+ new EventLoggerWrapper(null),
+ BluetoothAdapter.getDefaultAdapter(),
+ toggleBluetooth,
+ FAST_PAIR_ADDRESS,
+ new TimingLogger("GattConnectionManager", prefs.build()),
+ fastPairSignalChecker,
+ /* setMtu= */ false);
+ }
+}