Merge "Add test for progresshandlerbase" into tm-dev
diff --git a/nearby/service/java/com/android/server/nearby/fastpair/cache/DiscoveryItem.java b/nearby/service/java/com/android/server/nearby/fastpair/cache/DiscoveryItem.java
index 4f279a5..b8a9796 100644
--- a/nearby/service/java/com/android/server/nearby/fastpair/cache/DiscoveryItem.java
+++ b/nearby/service/java/com/android/server/nearby/fastpair/cache/DiscoveryItem.java
@@ -31,6 +31,7 @@
import com.android.server.nearby.common.ble.util.RangingUtils;
import com.android.server.nearby.common.fastpair.IconUtils;
import com.android.server.nearby.common.locator.Locator;
+import com.android.server.nearby.common.locator.LocatorContextWrapper;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@@ -67,6 +68,15 @@
@Retention(RetentionPolicy.SOURCE)
public @interface ItemState {}
+ public DiscoveryItem(LocatorContextWrapper locatorContextWrapper,
+ Cache.StoredDiscoveryItem mStoredDiscoveryItem) {
+ this.mFastPairCacheManager =
+ locatorContextWrapper.getLocator().get(FastPairCacheManager.class);
+ this.mClock =
+ locatorContextWrapper.getLocator().get(Clock.class);
+ this.mStoredDiscoveryItem = mStoredDiscoveryItem;
+ }
+
public DiscoveryItem(Context context, Cache.StoredDiscoveryItem mStoredDiscoveryItem) {
this.mFastPairCacheManager = Locator.get(context, FastPairCacheManager.class);
this.mClock = Locator.get(context, Clock.class);
diff --git a/nearby/tests/unit/src/com/android/server/nearby/fastpair/pairinghandler/PairingProgressHandlerBaseTest.java b/nearby/tests/unit/src/com/android/server/nearby/fastpair/pairinghandler/PairingProgressHandlerBaseTest.java
new file mode 100644
index 0000000..2ade5f2
--- /dev/null
+++ b/nearby/tests/unit/src/com/android/server/nearby/fastpair/pairinghandler/PairingProgressHandlerBaseTest.java
@@ -0,0 +1,123 @@
+/*
+ * Copyright (C) 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.server.nearby.fastpair.pairinghandler;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import static org.mockito.Mockito.when;
+
+import androidx.annotation.Nullable;
+
+import com.android.server.nearby.common.locator.Locator;
+import com.android.server.nearby.common.locator.LocatorContextWrapper;
+import com.android.server.nearby.fastpair.cache.DiscoveryItem;
+import com.android.server.nearby.fastpair.cache.FastPairCacheManager;
+import com.android.server.nearby.fastpair.footprint.FootprintsDeviceManager;
+import com.android.server.nearby.fastpair.halfsheet.FastPairHalfSheetManager;
+import com.android.server.nearby.fastpair.notification.FastPairNotificationManager;
+import com.android.server.nearby.fastpair.testing.FakeDiscoveryItems;
+
+import com.google.protobuf.ByteString;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+
+import java.time.Clock;
+
+import service.proto.Cache;
+import service.proto.Rpcs;
+
+public class PairingProgressHandlerBaseTest {
+ @Mock
+ Locator mLocator;
+ @Mock
+ LocatorContextWrapper mContextWrapper;
+ @Mock
+ Clock mClock;
+ @Mock
+ FastPairCacheManager mFastPairCacheManager;
+ @Mock
+ FootprintsDeviceManager mFootprintsDeviceManager;
+ private static final byte[] ACCOUNT_KEY = new byte[]{0x01, 0x02};
+
+ @Before
+ public void setup() {
+
+ MockitoAnnotations.initMocks(this);
+ when(mContextWrapper.getLocator()).thenReturn(mLocator);
+ mLocator.overrideBindingForTest(FastPairCacheManager.class,
+ mFastPairCacheManager);
+ mLocator.overrideBindingForTest(Clock.class, mClock);
+ }
+
+ @Test
+ public void createHandler_halfSheetSubsequentPairing_notificationPairingHandlerCreated() {
+
+ DiscoveryItem discoveryItem = FakeDiscoveryItems.newFastPairDiscoveryItem(mContextWrapper);
+ discoveryItem.setStoredItemForTest(
+ discoveryItem.getStoredItemForTest().toBuilder()
+ .setAuthenticationPublicKeySecp256R1(ByteString.copyFrom(ACCOUNT_KEY))
+ .setFastPairInformation(
+ Cache.FastPairInformation.newBuilder()
+ .setDeviceType(Rpcs.DeviceType.HEADPHONES).build())
+ .build());
+
+ PairingProgressHandlerBase progressHandler =
+ createProgressHandler(ACCOUNT_KEY, discoveryItem, /* isRetroactivePair= */ false);
+
+ assertThat(progressHandler).isInstanceOf(NotificationPairingProgressHandler.class);
+ }
+
+ @Test
+ public void createHandler_halfSheetInitialPairing_halfSheetPairingHandlerCreated() {
+ // No account key
+ DiscoveryItem discoveryItem = FakeDiscoveryItems.newFastPairDiscoveryItem(mContextWrapper);
+ discoveryItem.setStoredItemForTest(
+ discoveryItem.getStoredItemForTest().toBuilder()
+ .setFastPairInformation(
+ Cache.FastPairInformation.newBuilder()
+ .setDeviceType(Rpcs.DeviceType.HEADPHONES).build())
+ .build());
+
+ PairingProgressHandlerBase progressHandler =
+ createProgressHandler(null, discoveryItem, /* isRetroactivePair= */ false);
+
+ assertThat(progressHandler).isInstanceOf(HalfSheetPairingProgressHandler.class);
+ }
+
+ private PairingProgressHandlerBase createProgressHandler(
+ @Nullable byte[] accountKey, DiscoveryItem fastPairItem, boolean isRetroactivePair) {
+ FastPairNotificationManager fastPairNotificationManager =
+ new FastPairNotificationManager(mContextWrapper, fastPairItem, true);
+ FastPairHalfSheetManager fastPairHalfSheetManager =
+ new FastPairHalfSheetManager(mContextWrapper);
+ mLocator.overrideBindingForTest(FastPairHalfSheetManager.class, fastPairHalfSheetManager);
+ PairingProgressHandlerBase pairingProgressHandlerBase =
+ PairingProgressHandlerBase.create(
+ mContextWrapper,
+ fastPairItem,
+ fastPairItem.getAppPackageName(),
+ accountKey,
+ mFootprintsDeviceManager,
+ fastPairNotificationManager,
+ fastPairHalfSheetManager,
+ isRetroactivePair);
+ return pairingProgressHandlerBase;
+ }
+}
diff --git a/nearby/tests/unit/src/com/android/server/nearby/fastpair/testing/FakeDiscoveryItems.java b/nearby/tests/unit/src/com/android/server/nearby/fastpair/testing/FakeDiscoveryItems.java
new file mode 100644
index 0000000..aa7e6f6
--- /dev/null
+++ b/nearby/tests/unit/src/com/android/server/nearby/fastpair/testing/FakeDiscoveryItems.java
@@ -0,0 +1,55 @@
+/*
+ * Copyright (C) 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.server.nearby.fastpair.testing;
+
+import com.android.server.nearby.common.locator.LocatorContextWrapper;
+import com.android.server.nearby.fastpair.cache.DiscoveryItem;
+
+import service.proto.Cache;
+
+public class FakeDiscoveryItems {
+ public static final String DEFAULT_MAC_ADDRESS = "00:11:22:33:44:55";
+ public static final long DEFAULT_TIMESTAMP = 1000000000L;
+ public static final String DEFAULT_DESCRIPITON = "description";
+ public static final String TRIGGER_ID = "trigger.id";
+ private static final String FAST_PAIR_ID = "id";
+ private static final int RSSI = -80;
+ private static final int TX_POWER = -10;
+ public static DiscoveryItem newFastPairDiscoveryItem(LocatorContextWrapper contextWrapper) {
+ return new DiscoveryItem(contextWrapper, newFastPairDeviceStoredItem());
+ }
+
+ public static Cache.StoredDiscoveryItem newFastPairDeviceStoredItem() {
+ return newFastPairDeviceStoredItem(TRIGGER_ID);
+ }
+
+ public static Cache.StoredDiscoveryItem newFastPairDeviceStoredItem(String triggerId) {
+ Cache.StoredDiscoveryItem.Builder item = Cache.StoredDiscoveryItem.newBuilder();
+ item.setState(Cache.StoredDiscoveryItem.State.STATE_ENABLED);
+ item.setId(FAST_PAIR_ID);
+ item.setDescription(DEFAULT_DESCRIPITON);
+ item.setType(Cache.NearbyType.NEARBY_DEVICE);
+ item.setTriggerId(triggerId);
+ item.setMacAddress(DEFAULT_MAC_ADDRESS);
+ item.setFirstObservationTimestampMillis(DEFAULT_TIMESTAMP);
+ item.setLastObservationTimestampMillis(DEFAULT_TIMESTAMP);
+ item.setRssi(RSSI);
+ item.setTxPower(TX_POWER);
+ return item.build();
+ }
+
+}