Skip to content

Commit

Permalink
Merge pull request #2068 from smartdevicelink/bugfix/issue-2067-softb…
Browse files Browse the repository at this point in the history
…uttonobject-invalid-config

Fix initializing soft button object error states
  • Loading branch information
joeljfischer committed Feb 2, 2022
2 parents 6d4a41a + d14d3dd commit 937d3f4
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 14 deletions.
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

0 comments on commit 937d3f4

Please sign in to comment.