Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 87db92d3 authored by David Duarte's avatar David Duarte
Browse files

BumbleBluetoothTests: Run google-java-format

Bug: 266884665
Test: atest BumbleBluetoothTests
Change-Id: I23d9badc980fdffaa7a25fab11e7d0dc67559521
parent e02e8cfe
Loading
Loading
Loading
Loading
+41 −23
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 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 android.bluetooth;

import static android.bluetooth.Utils.factoryResetAndCreateNewChannel;
import static com.google.common.truth.Truth.assertThat;

import android.bluetooth.le.AdvertiseData;
import android.bluetooth.le.AdvertisingSet;
@@ -14,7 +31,6 @@ import androidx.test.core.app.ApplicationProvider;
import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.runner.AndroidJUnit4;

import static com.google.common.truth.Truth.assertThat;
import com.google.protobuf.Empty;

import io.grpc.Context.CancellableContext;
@@ -22,23 +38,20 @@ import io.grpc.Deadline;
import io.grpc.ManagedChannel;
import io.grpc.stub.StreamObserver;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

import pandora.HostGrpc;
import pandora.HostProto.ScanRequest;
import pandora.HostProto.ScanningResponse;


/**
 * Test cases for {@link AdvertiseManager}.
 */
/** Test cases for {@link AdvertiseManager}. */
@RunWith(AndroidJUnit4.class)
public class LeAdvertisingTest {

@@ -54,7 +67,8 @@ public class LeAdvertisingTest {

    @BeforeClass
    public static void setUpClass() throws Exception {
        InstrumentationRegistry.getInstrumentation().getUiAutomation()
        InstrumentationRegistry.getInstrumentation()
                .getUiAutomation()
                .adoptShellPermissionIdentity();
    }

@@ -76,7 +90,8 @@ public class LeAdvertisingTest {

    @Test
    public void advertisingSet() throws Exception {
        ScanningResponse response = startAdvertising()
        ScanningResponse response =
                startAdvertising()
                        .thenCompose(advAddressPair -> scanWithBumble(advAddressPair))
                        .join();

@@ -94,8 +109,10 @@ public class LeAdvertisingTest {

        // Start advertising
        BluetoothLeAdvertiser leAdvertiser = bluetoothAdapter.getBluetoothLeAdvertiser();
        AdvertisingSetParameters parameters = new AdvertisingSetParameters.Builder().
             setOwnAddressType(AdvertisingSetParameters.ADDRESS_TYPE_RANDOM).build();
        AdvertisingSetParameters parameters =
                new AdvertisingSetParameters.Builder()
                        .setOwnAddressType(AdvertisingSetParameters.ADDRESS_TYPE_RANDOM)
                        .build();
        AdvertiseData advertiseData = new AdvertiseData.Builder().build();
        AdvertiseData scanResponse = new AdvertiseData.Builder().build();
        AdvertisingSetCallback advertisingSetCallback =
@@ -139,8 +156,8 @@ public class LeAdvertisingTest {
                        advertisingSet.getOwnAddress();
                    }
                };
        leAdvertiser.startAdvertisingSet(parameters, advertiseData, scanResponse,
          null, null, 0, 0, advertisingSetCallback);
        leAdvertiser.startAdvertisingSet(
                parameters, advertiseData, scanResponse, null, null, 0, 0, advertisingSetCallback);

        return future;
    }
@@ -184,10 +201,11 @@ public class LeAdvertisingTest {
                };

        Deadline initialDeadline = Deadline.after(TIMEOUT_ADVERTISING_MS, TimeUnit.MILLISECONDS);
        withCancellation.run(() -> mHostStub.withDeadline(initialDeadline)
            .scan(request, responseObserver));
        withCancellation.run(
                () -> mHostStub.withDeadline(initialDeadline).scan(request, responseObserver));

        return future.whenComplete((input, exception) -> {
        return future.whenComplete(
                (input, exception) -> {
                    withCancellation.cancel(null);
                });
    }
+27 −25
Original line number Diff line number Diff line
@@ -17,7 +17,6 @@
package android.bluetooth;

import static android.bluetooth.Utils.factoryResetAndCreateNewChannel;

import static com.google.common.truth.Truth.assertThat;

import android.bluetooth.le.BluetoothLeScanner;
@@ -34,6 +33,9 @@ import androidx.test.runner.AndroidJUnit4;

import com.google.protobuf.Empty;

import io.grpc.ManagedChannel;
import io.grpc.stub.StreamObserver;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
@@ -46,9 +48,6 @@ import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

import io.grpc.ManagedChannel;
import io.grpc.stub.StreamObserver;

import pandora.HostGrpc;
import pandora.HostProto;
import pandora.HostProto.AdvertiseRequest;
@@ -101,17 +100,17 @@ public class LeScanningTest {
    public void startBleScan_withCallbackTypeAllMatches() {
        advertiseWithBumble(TEST_UUID_STRING);

        List<ScanResult> results = startScanning(TEST_UUID_STRING,
                ScanSettings.CALLBACK_TYPE_ALL_MATCHES).join();
        List<ScanResult> results =
                startScanning(TEST_UUID_STRING, ScanSettings.CALLBACK_TYPE_ALL_MATCHES).join();

        assertThat(results.get(0).getScanRecord().getServiceUuids().get(0)).isEqualTo(
                ParcelUuid.fromString(TEST_UUID_STRING));
        assertThat(results.get(1).getScanRecord().getServiceUuids().get(0)).isEqualTo(
                ParcelUuid.fromString(TEST_UUID_STRING));
        assertThat(results.get(0).getScanRecord().getServiceUuids().get(0))
                .isEqualTo(ParcelUuid.fromString(TEST_UUID_STRING));
        assertThat(results.get(1).getScanRecord().getServiceUuids().get(0))
                .isEqualTo(ParcelUuid.fromString(TEST_UUID_STRING));
    }

    private CompletableFuture<List<ScanResult>> startScanning(String serviceUuid,
            int callbackType) {
    private CompletableFuture<List<ScanResult>> startScanning(
            String serviceUuid, int callbackType) {
        CompletableFuture<List<ScanResult>> future = new CompletableFuture<>();
        List<ScanResult> scanResults = new ArrayList<>();

@@ -129,17 +128,21 @@ public class LeScanningTest {
                        .build();

        List<ScanFilter> scanFilters = new ArrayList<>();
        ScanFilter scanFilter = new ScanFilter.Builder()
                .setServiceUuid(ParcelUuid.fromString(serviceUuid))
                .build();
        ScanFilter scanFilter =
                new ScanFilter.Builder().setServiceUuid(ParcelUuid.fromString(serviceUuid)).build();
        scanFilters.add(scanFilter);

        ScanCallback scanCallback =
                new ScanCallback() {
                    @Override
                    public void onScanResult(int callbackType, ScanResult result) {
                        Log.i(TAG, "onScanResult " + "callbackType: " + callbackType
                                + ", service uuids: " + result.getScanRecord().getServiceUuids());
                        Log.i(
                                TAG,
                                "onScanResult "
                                        + "callbackType: "
                                        + callbackType
                                        + ", service uuids: "
                                        + result.getScanRecord().getServiceUuids());
                        if (scanResults.size() < 2) {
                            scanResults.add(result);
                        } else {
@@ -161,14 +164,13 @@ public class LeScanningTest {
    }

    private void advertiseWithBumble(String serviceUuid) {
        HostProto.DataTypes dataType = HostProto.DataTypes.newBuilder()
        HostProto.DataTypes dataType =
                HostProto.DataTypes.newBuilder()
                        .addCompleteServiceClassUuids128(serviceUuid)
                        .build();

        AdvertiseRequest request = AdvertiseRequest.newBuilder()
                .setLegacy(true)
                .setData(dataType)
                .build();
        AdvertiseRequest request =
                AdvertiseRequest.newBuilder().setLegacy(true).setData(dataType).build();

        StreamObserver<AdvertiseResponse> responseObserver =
                new StreamObserver<>() {
+22 −7
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 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 android.bluetooth;

import com.google.protobuf.ByteString;
import com.google.protobuf.Empty;

import java.util.concurrent.TimeUnit;

import io.grpc.ManagedChannel;
import io.grpc.okhttp.OkHttpChannelBuilder;

import java.util.concurrent.TimeUnit;

import pandora.HostGrpc;

public final class Utils {
@@ -24,10 +41,8 @@ public final class Utils {
    public static ManagedChannel factoryResetAndCreateNewChannel() throws InterruptedException {
        // FactoryReset is killing the server and restarting all channels created before the server
        // restarted that cannot be reused
        ManagedChannel channel = OkHttpChannelBuilder
                .forAddress("localhost", 7999)
                .usePlaintext()
                .build();
        ManagedChannel channel =
                OkHttpChannelBuilder.forAddress("localhost", 7999).usePlaintext().build();

        HostGrpc.HostBlockingStub stub = HostGrpc.newBlockingStub(channel);
        stub.factoryReset(Empty.getDefaultInstance());