Skip to content

Commit

Permalink
Fixes #20.
Browse files Browse the repository at this point in the history
Adds a spec to show the issue.
  • Loading branch information
Guido Marucci Blas committed Feb 26, 2015
1 parent 55f6877 commit 1a39de2
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 3 deletions.
57 changes: 56 additions & 1 deletion Example/Tests/WLXBluetoothConnectionManagerSpec.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#import <WLXBluetoothDevice/WLXBluetoothConnectionManager.h>
#import <WLXBluetoothDevice/WLXBluetoothDeviceConnectionError.h>
#import <WLXBluetoothDevice/WLXBluetoothDeviceNotifications.h>
#import <WLXBluetoothDevice/WLXLinearReconnectionStrategy.h>


SpecBegin(WLXBluetoothConnectionManager)
Expand All @@ -21,13 +22,14 @@
__block id<WLXReconnectionStrategy> mockReconnectionStrategy;
__block WLXBluetoothConnectionManager * connectionManager;
__block id<WLXConnectionManagerDelegate> connectionManagerDelegate;
__block dispatch_queue_t queue;

beforeEach(^{
mockPeripheral = mock([CBPeripheral class]);
mockCentralManager = mock([CBCentralManager class]);
notificationCenter = [NSNotificationCenter defaultCenter];
mockReconnectionStrategy = mockProtocol(@protocol(WLXReconnectionStrategy));
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
connectionManager = [[WLXBluetoothConnectionManager alloc] initWithPeripheral:mockPeripheral
centralManager:mockCentralManager
notificationCenter:notificationCenter
Expand Down Expand Up @@ -291,6 +293,59 @@

});

context(@"when the connection manager is reconecting", ^{

beforeEach(^{
NSError * error = [NSError errorWithDomain:@"ar.com.wolox.WLXBluetoothDevice.Test" code:0 userInfo:nil];
id<WLXReconnectionStrategy> rs = [[WLXLinearReconnectionStrategy alloc] initWithWaitTime:0
maxReconnectionAttempts:10
connectionTimeout:0
queue:queue];
connectionManager = [[WLXBluetoothConnectionManager alloc] initWithPeripheral:mockPeripheral
centralManager:mockCentralManager
notificationCenter:notificationCenter
queue:queue
reconnectionStrategy:rs
bluetoohOn:NO];
connectionManager.delegate = connectionManagerDelegate;
[notificationCenter postNotificationName:WLXBluetoothDeviceBluetoothIsOn object:nil userInfo:nil];
[MKTGiven([mockReconnectionStrategy remainingConnectionAttempts]) willReturnUnsignedInteger:1];
[MKTGiven([mockReconnectionStrategy connectionTimeout]) willReturnUnsignedInteger:0];
[connectionManager connectWithTimeout:0 usingBlock:nil];
[connectionManager didConnect];
[connectionManager didDisconnect:error];
// This simulates the reconnection executed by the mock reconnection strategy
[connectionManager connectWithTimeout:0 usingBlock:nil];
});

it(@"invokes the delegate's connectionManagerDidReconnect:", ^{
[connectionManager didConnect];
[MKTVerify(connectionManagerDelegate) connecitonManagerDidReconnect:connectionManager];
});

it(@"changes the connecting attribute", ^{
[connectionManager connectWithTimeout:0 usingBlock:nil];
expect(connectionManager.connecting).to.beTruthy;
[connectionManager didConnect];
expect(connectionManager.connecting).to.beFalsy;
});

it(@"changes the connected attribute", ^{
[connectionManager connectWithTimeout:0 usingBlock:nil];
expect(connectionManager.connecting).to.beFalsy;
[connectionManager didConnect];
expect(connectionManager.connecting).to.beTruthy;
});

it(@"changes the reconnecting attribute", ^{
[connectionManager connectWithTimeout:0 usingBlock:nil];
expect(connectionManager.reconnecting).to.beTruthy;
[connectionManager didConnect];
expect(connectionManager.reconnecting).to.beFalsy;
});

});

});

describe(@"#didDisconnect:", ^{
Expand Down
16 changes: 14 additions & 2 deletions Pod/Classes/WLXBluetoothConnectionManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,18 @@ - (void)didConnect {
notificationName = WLXBluetoothDeviceConnectionEstablished;
}
[self.reconnectionStrategy reset];

// A local variable is used to be able
// to decide which method to invoke
// on the delegate a the end of this
// method.
//
// The reconnecting property is not used
// because we want to make sure that when
// the delegate's method is invoked the
// properties have the expected value.
BOOL reconnecting = _reconnecting;

_connected = YES;
_connecting = NO;
_reconnecting = NO;
Expand All @@ -180,9 +192,9 @@ - (void)didConnect {
}
NSDictionary * userInfo = @{ WLXBluetoothDevicePeripheral : self.peripheral };
[self.notificationCenter postNotificationName:notificationName object:self userInfo:userInfo];
if (self.reconnecting && [self.delegate respondsToSelector:@selector(connecitonManagerDidReconnect:)]) {
if (reconnecting && [self.delegate respondsToSelector:@selector(connecitonManagerDidReconnect:)]) {
[self.delegate connecitonManagerDidReconnect:self];
} else if (!self.reconnecting) {
} else if (!reconnecting) {
[self.delegate connectionManagerDidConnect:self];
}
}
Expand Down

0 comments on commit 1a39de2

Please sign in to comment.