forked from Rightpoint/RZBluetooth
-
Notifications
You must be signed in to change notification settings - Fork 1
/
RZBProfileBatteryTests.m
96 lines (82 loc) · 3.01 KB
/
RZBProfileBatteryTests.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
//
// RZBProfileBatteryTests.m
// RZBluetooth
//
// Created by Brian King on 8/6/15.
// Copyright (c) 2015 Raizlabs. All rights reserved.
//
#import "RZBSimulatedTestCase.h"
#import "RZBPeripheral+RZBBattery.h"
#import "RZBSimulatedDevice+RZBBatteryLevel.h"
#import "CBUUID+RZBPublic.h"
#import "RZBLog.h"
@interface RZBProfileBatteryTests : RZBSimulatedTestCase
@end
@implementation RZBProfileBatteryTests
- (void)setUp
{
[super setUp];
[self.device addBatteryService];
RZBSetLogHandler(^(RZBLogLevel logLevel, NSString *format, va_list args) {
NSLog(@"%@", [[NSString alloc] initWithFormat:format arguments:args]);
});
}
- (void)testRead
{
XCTestExpectation *read = [self expectationWithDescription:@"Read battery level"];
self.device.batteryLevel = 80;
[self.peripheral fetchBatteryLevel:^(NSUInteger level, NSError *error) {
[read fulfill];
XCTAssertNil(error);
XCTAssert(level == 80);
}];
[self waitForExpectationsWithTimeout:5.0 handler:nil];
}
- (void)testServiceDiscovery
{
XCTestExpectation *discover = [self expectationWithDescription:@"Discover Battery Service"];
self.device.batteryLevel = 80;
[self.peripheral discoverServiceUUIDs:@[[CBUUID rzb_UUIDForBatteryService]] completion:^(NSError * _Nullable error) {
[discover fulfill];
XCTAssertNil(error);
}];
[self waitForExpectationsWithTimeout:5.0 handler:nil];
}
- (void)testCharacteristicDiscovery
{
XCTestExpectation *discover = [self expectationWithDescription:@"Discover Battery Service"];
self.device.batteryLevel = 80;
[self.peripheral discoverCharacteristicUUIDs:@[[CBUUID rzb_UUIDForBatteryLevelCharacteristic]] serviceUUID:[CBUUID rzb_UUIDForBatteryService] completion:^(CBService * _Nullable service, NSError * _Nullable error) {
[discover fulfill];
XCTAssertNil(error);
}];
[self waitForExpectationsWithTimeout:5.0 handler:nil];
}
- (void)testMonitor
{
XCTestExpectation *addMonitor = [self expectationWithDescription:@"Monitor battery level"];
NSMutableArray *values = [NSMutableArray array];
[self.peripheral addBatteryLevelObserver:^(NSUInteger level, NSError *error) {
[values addObject:@(level)];
} completion:^(NSError *error) {
[addMonitor fulfill];
}];
[self waitForExpectationsWithTimeout:5.0 handler:nil];
NSArray *transmittedValues = @[@(10), @(20), @(30)];
for (NSNumber *level in transmittedValues) {
self.device.batteryLevel = [level unsignedIntegerValue];
[self waitForQueueFlush];
}
XCTAssertEqualObjects(transmittedValues, values);
[values removeAllObjects];
XCTestExpectation *removeMonitor = [self expectationWithDescription:@"Monitor battery level"];
[self.peripheral removeBatteryLevelObserver:^(NSError *error) {
XCTAssertNil(error);
[removeMonitor fulfill];
}];
self.device.batteryLevel = 33;
[self waitForQueueFlush];
[self waitForExpectationsWithTimeout:1 handler:nil];
XCTAssert(values.count == 0);
}
@end