Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix initializing soft button object error states #2068

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 16 additions & 3 deletions SmartDeviceLink/public/SDLSoftButtonObject.m
Expand Up @@ -36,8 +36,21 @@ - (instancetype)initWithName:(NSString *)name states:(NSArray<SDLSoftButtonState
self = [super init];
if (!self) { return nil; }

// Make sure there aren't two states with the same name
if ([self sdl_hasTwoStatesOfSameName:states]) {
BOOL hasStateWithInitialName = NO;
for (SDLSoftButtonState *state in states) {
if ([state.name isEqualToString:initialStateName]) {
hasStateWithInitialName = YES;
break;
}
}
NSAssert(![SDLSoftButtonObject sdl_hasTwoStatesOfSameName:states], @"A SoftButtonObject must have states with different names.");
NSAssert(hasStateWithInitialName, @"A SoftButtonObject must have a state with initialStateName.");
if ([SDLSoftButtonObject sdl_hasTwoStatesOfSameName:states]) {
SDLLogE(@"Error creating soft button object: the soft button object was created with two states of the same name. Soft button object name: %@, states: %@, initialStateName: %@", name, states, initialStateName);
return nil;
}
if (!hasStateWithInitialName) {
SDLLogE(@"Error creating soft button object: the soft button object does not have a state with the specified initial state name. Soft button object name: %@, states: %@, initialStateName: %@", name, states, initialStateName);
return nil;
}

Expand Down Expand Up @@ -121,7 +134,7 @@ - (nullable SDLSoftButtonState *)stateWithName:(NSString *)stateName {
return nil;
}

- (BOOL)sdl_hasTwoStatesOfSameName:(NSArray<SDLSoftButtonState *> *)states {
+ (BOOL)sdl_hasTwoStatesOfSameName:(NSArray<SDLSoftButtonState *> *)states {
for (NSUInteger i = 0; i < states.count; i++) {
NSString *stateName = states[i].name;
for (NSUInteger j = (i + 1); j < states.count; j++) {
Expand Down
8 changes: 8 additions & 0 deletions SmartDeviceLink/public/SDLSoftButtonState.m
Expand Up @@ -33,6 +33,10 @@ @implementation SDLSoftButtonState

- (instancetype)initWithStateName:(NSString *)stateName text:(nullable NSString *)text image:(nullable UIImage *)image {
NSParameterAssert((text != nil) || (image != nil));
if ((text == nil) && (image == nil)) {
SDLLogE(@"Error creating soft button state: the state requires either text or an image, or both. StateName: %@", stateName);
return nil;
}

SDLArtwork *artwork = [[SDLArtwork alloc] initWithImage:image persistent:YES asImageFormat:SDLArtworkImageFormatPNG];
return [self initWithStateName:stateName text:text artwork:artwork];
Expand All @@ -43,6 +47,10 @@ - (instancetype)initWithStateName:(NSString *)stateName text:(nullable NSString
if (!self) { return nil; }

NSParameterAssert((text != nil) || (artwork != nil));
if ((text == nil) && (artwork == nil)) {
SDLLogE(@"Error creating soft button state: the state requires either text or an image, or both. StateName: %@", stateName);
return nil;
}

_name = stateName;
_text = text;
Expand Down
14 changes: 3 additions & 11 deletions SmartDeviceLinkTests/SDLSoftButtonObjectSpec.m
Expand Up @@ -14,14 +14,11 @@
__block NSString *testObjectName = @"Test Object Name";

context(@"with a single state", ^{
__block SDLSoftButtonState *testSingleState = OCMClassMock([SDLSoftButtonState class]);
__block NSString *testSingleStateName = @"Test state name";
__block SDLSoftButtonState *testSingleState = [[SDLSoftButtonState alloc] initWithStateName:testSingleStateName text:@"Some Text" image:nil];
__block SDLSoftButton *testSingleStateSoftButton = [[SDLSoftButton alloc] initWithType:SDLSoftButtonTypeText text:@"Some Text" image:nil highlighted:NO buttonId:0 systemAction:SDLSystemActionDefaultAction handler:nil];

beforeEach(^{
OCMStub(testSingleState.name).andReturn(testSingleStateName);
OCMStub(testSingleState.softButton).andReturn(testSingleStateSoftButton);

testObject = [[SDLSoftButtonObject alloc] initWithName:testObjectName state:testSingleState handler:nil];
});

Expand Down Expand Up @@ -77,19 +74,14 @@
});

context(@"with multiple states", ^{
__block SDLSoftButtonState *testFirstState = OCMClassMock([SDLSoftButtonState class]);
__block NSString *testFirstStateName = @"Test First Name";
__block SDLSoftButtonState *testFirstState = [[SDLSoftButtonState alloc] initWithStateName:testFirstStateName text:@"Some Text" image:nil];
__block SDLSoftButton *testFirstStateSoftButton = [[SDLSoftButton alloc] initWithType:SDLSoftButtonTypeText text:@"Some Text" image:nil highlighted:NO buttonId:0 systemAction:SDLSystemActionDefaultAction handler:nil];

__block SDLSoftButtonState *testSecondState = OCMClassMock([SDLSoftButtonState class]);
__block NSString *testSecondStateName = @"Test Second Name";
__block SDLSoftButtonState *testSecondState = [[SDLSoftButtonState alloc] initWithStateName:testSecondStateName text:@"Some Second Text" image:nil];

beforeEach(^{
OCMStub(testFirstState.name).andReturn(testFirstStateName);
OCMStub(testFirstState.softButton).andReturn(testFirstStateSoftButton);

OCMStub(testSecondState.name).andReturn(testSecondStateName);

testObject = [[SDLSoftButtonObject alloc] initWithName:testObjectName states:@[testFirstState, testSecondState] initialStateName:testFirstStateName handler:nil];
});

Expand Down