From 5865ae749699783b062da932321056bea34ff31d Mon Sep 17 00:00:00 2001 From: Edwin Wu Date: Tue, 16 Sep 2025 18:12:35 -0700 Subject: [PATCH] [NC Apple coverage] Add unit tests for `nearby::apple::BlePeripheral`. PiperOrigin-RevId: 807925024 --- .../platform/implementation/apple/Tests/BUILD | 3 + .../apple/Tests/ble_peripheral_test.mm | 114 ++++++++++++++++++ 2 files changed, 117 insertions(+) create mode 100644 internal/platform/implementation/apple/Tests/ble_peripheral_test.mm diff --git a/internal/platform/implementation/apple/Tests/BUILD b/internal/platform/implementation/apple/Tests/BUILD index fe7598b7..32fc8778 100644 --- a/internal/platform/implementation/apple/Tests/BUILD +++ b/internal/platform/implementation/apple/Tests/BUILD @@ -36,6 +36,7 @@ objc_library( "GNCUtilsTest.m", "GNCWifiLanMediumTest.mm", "UtilsTest.mm", + "ble_peripheral_test.mm", ], deps = [ "//internal/platform:base", @@ -44,12 +45,14 @@ objc_library( "//internal/platform/implementation/apple", # buildcleaner: keep "//internal/platform/implementation/apple:Shared", "//internal/platform/implementation/apple:ble_v2", + "//internal/platform/implementation/apple/Mediums/BLE", "//internal/platform/implementation/apple/Mediums/WiFiCommon", "//internal/platform/implementation/apple/Mediums/WiFiCommon/Tests:FakeNWFramework", "//third_party/apple_frameworks:CommonCrypto", "//third_party/apple_frameworks:Foundation", "//third_party/apple_frameworks:XCTest", "@com_google_absl//absl/time", + "@com_google_googletest//:gtest", "@nlohmann_json//:json", ], ) diff --git a/internal/platform/implementation/apple/Tests/ble_peripheral_test.mm b/internal/platform/implementation/apple/Tests/ble_peripheral_test.mm new file mode 100644 index 00000000..efa9ea3b --- /dev/null +++ b/internal/platform/implementation/apple/Tests/ble_peripheral_test.mm @@ -0,0 +1,114 @@ +// Copyright 2025 Google LLC +// +// 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 +// +// https://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. + +#include "internal/platform/implementation/apple/ble_peripheral.h" + +#import +#import + +#include "gmock/gmock.h" +#include "gtest/gtest.h" +#import "internal/platform/implementation/apple/Mediums/BLE/GNCBLEGATTCharacteristic.h" +#import "internal/platform/implementation/apple/Mediums/BLE/GNCPeripheral.h" + +/** Fake @c GNCPeripheral for testing. */ +@interface FakePeripheral : NSObject + +@property(readwrite, copy) NSString *name; +@property(readwrite, nonatomic) NSUUID *identifier; +@property(nonatomic, readonly) CBPeripheralState state; +@property(nonatomic, weak) id peripheralDelegate; + +- (instancetype)initWithIdentifier:(NSUUID *)identifier name:(NSString *)name; + +@end + +@implementation FakePeripheral + +@synthesize peripheralDelegate = _peripheralDelegate; + +- (instancetype)initWithIdentifier:(NSUUID *)identifier name:(NSString *)name { + self = [super init]; + if (self) { + _identifier = identifier; + _name = [name copy]; + _state = CBPeripheralStateDisconnected; + } + return self; +} + +// Implement other GNCPeripheral methods as no-ops or with default return values. +- (void)discoverServices:(nullable NSArray *)serviceUUIDs { +} + +- (nullable NSArray *)services { + return @[]; +} + +- (void)discoverCharacteristics:(nullable NSArray *)characteristicUUIDs + forService:(CBService *)service { +} + +- (void)readValueForCharacteristic:(GNCBLEGATTCharacteristic *)characteristic { +} + +- (void)writeValue:(NSData *)data + forCharacteristic:(GNCBLEGATTCharacteristic *)characteristic + type:(CBCharacteristicWriteType)type { +} + +- (BOOL)canSendWriteWithoutResponse { + return YES; +} + +- (void)setNotifyValue:(BOOL)enabled forCharacteristic:(GNCBLEGATTCharacteristic *)characteristic { +} + +- (NSUInteger)maximumWriteLengthForType:(CBCharacteristicWriteType)type { + return 20; +} + +- (void)openL2CAPChannel:(CBL2CAPPSM)PSM { +} + +@end + +@interface GNCBLEPeripheralTest : XCTestCase +@end + +@implementation GNCBLEPeripheralTest + +- (void)testIsNull { + nearby::apple::BlePeripheral blePeripheral{nil}; + XCTAssertNil(blePeripheral.GetPeripheral(), @"Peripheral should be nil"); +} + +- (void)testDefaultBlePeripheral { + nearby::api::ble_v2::BlePeripheral &defaultPeripheral = + nearby::apple::BlePeripheral::DefaultBlePeripheral(); + // 0xffffffffffffffff is the default value from + // third_party/nearby/internal/platform/implementation/apple/ble_peripheral.mm when create static + // default BlePeripheral. + XCTAssertEqual(defaultPeripheral.GetUniqueId(), 0xffffffffffffffff); +} + +- (void)testGetPeripheral { + NSUUID *uuid = [NSUUID UUID]; + FakePeripheral *fakePeripheral = [[FakePeripheral alloc] initWithIdentifier:uuid + name:@"TestDevice"]; + nearby::apple::BlePeripheral blePeripheral{fakePeripheral}; + XCTAssertEqualObjects(blePeripheral.GetPeripheral(), fakePeripheral); +} + +@end