forked from Rightpoint/RZBluetooth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRZBProfileBatteryTests.m
70 lines (59 loc) · 1.96 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
//
// RZBProfileBatteryTests.m
// RZBluetooth
//
// Created by Brian King on 8/6/15.
// Copyright (c) 2015 Raizlabs. All rights reserved.
//
#import "RZBSimulatedTestCase.h"
#import "CBPeripheral+RZBBattery.h"
#import "RZBSimulatedDevice+RZBBatteryLevel.h"
@interface RZBProfileBatteryTests : RZBSimulatedTestCase
@end
@implementation RZBProfileBatteryTests
- (void)setUp
{
[super setUp];
[self.device addBatteryService];
}
- (void)testRead
{
XCTestExpectation *read = [self expectationWithDescription:@"Read battery level"];
self.device.batteryLevel = 80;
[self.peripheral rzb_fetchBatteryLevel:^(NSUInteger level, NSError *error) {
[read fulfill];
XCTAssertNil(error);
XCTAssert(level == 80);
}];
[self waitForExpectationsWithTimeout:1.0 handler:nil];
}
- (void)testMonitor
{
XCTestExpectation *addMonitor = [self expectationWithDescription:@"Monitor battery level"];
NSMutableArray *values = [NSMutableArray array];
[self.peripheral rzb_addBatteryLevelObserver:^(NSUInteger level, NSError *error) {
XCTAssertNil(error);
[values addObject:@(level)];
} completion:^(NSError *error) {
XCTAssertNil(error);
[addMonitor fulfill];
}];
[self waitForExpectationsWithTimeout:1.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 rzb_removeBatteryLevelObserver:^(NSError *error) {
XCTAssertNil(error);
[removeMonitor fulfill];
}];
self.device.batteryLevel = 33;
[self waitForQueueFlush];
[self waitForExpectationsWithTimeout:1 handler:nil];
XCTAssert(values.count == 0);
}
@end