Skip to content

Commit

Permalink
Issue #2109 - Track uploaded image names
Browse files Browse the repository at this point in the history
* If the alert icon image wasn't successfully uploaded, don't show it (fixes a case where an overwriting image could fail and the old image could show)
* Fix tests
  • Loading branch information
joeljfischer committed Sep 21, 2022
1 parent 9aec1f6 commit 3c6d4a0
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 14 deletions.
25 changes: 20 additions & 5 deletions SmartDeviceLink/private/SDLPresentAlertOperation.m
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ @interface SDLPresentAlertOperation()
@property (copy, nonatomic, nullable) NSError *internalError;
@property (assign, atomic) BOOL isAlertPresented;

@property (strong, nonatomic) NSMutableSet<NSString *> *uploadedImageNames;

@end

@implementation SDLPresentAlertOperation
Expand All @@ -90,6 +92,7 @@ - (instancetype)initWithConnectionManager:(id<SDLConnectionManagerType>)connecti
_cancelId = cancelID;
_operationId = [NSUUID UUID];
_currentWindowCapability = currentWindowCapability;
_uploadedImageNames = [NSMutableSet set];

return self;
}
Expand Down Expand Up @@ -193,15 +196,25 @@ - (void)sdl_uploadAudioFilesWithCompletionHandler:(void (^)(void))handler {
/// @param handler Called when all images have been uploaded.
- (void)sdl_uploadImagesWithCompletionHandler:(void (^)(void))handler {
NSMutableArray<SDLArtwork *> *artworksToBeUploaded = [NSMutableArray array];
if ([self sdl_supportsAlertIcon] && [self.fileManager fileNeedsUpload:self.alertView.icon]) {
[artworksToBeUploaded addObject:self.alertView.icon];
if ([self sdl_supportsAlertIcon] && (self.alertView.icon != nil)) {
if ([self.fileManager fileNeedsUpload:self.alertView.icon]) {
// If the file is not uploaded, attempt to upload it
[artworksToBeUploaded addObject:self.alertView.icon];
} else if ([self.fileManager hasUploadedFile:self.alertView.icon] || self.alertView.icon.isStaticIcon) {
// If the file is already uploaded, add it to the uploaded set so we can show it
[self.uploadedImageNames addObject:self.alertView.icon.name];
}
}

// Don't upload artworks for buttons that will not be shown.
for (NSUInteger i = 0; i < [self sdl_softButtonCount]; i++) {
SDLSoftButtonObject *object = self.alertView.softButtons[i];
if ([self sdl_supportsSoftButtonImages] && [self.fileManager fileNeedsUpload:object.currentState.artwork]) {
[artworksToBeUploaded addObject:object.currentState.artwork];
if ([self sdl_supportsSoftButtonImages]) {
if ([self.fileManager fileNeedsUpload:object.currentState.artwork]) {
[artworksToBeUploaded addObject:object.currentState.artwork];
} else if ([self.fileManager hasUploadedFile:object.currentState.artwork] || object.currentState.artwork.isStaticIcon) {
[self.uploadedImageNames addObject:self.alertView.icon.name];
}
}
}

Expand All @@ -219,12 +232,14 @@ - (void)sdl_uploadImagesWithCompletionHandler:(void (^)(void))handler {

return YES;
} completionHandler:^(NSArray<NSString *> * _Nonnull artworkNames, NSError * _Nullable error) {
__strong typeof(weakself) strongself = weakself;
if (error != nil) {
SDLLogE(@"Error uploading alert images: %@", error);
} else {
SDLLogD(@"All alert images uploaded");
}

[strongself.uploadedImageNames addObjectsFromArray:artworkNames];
return handler();
}];
}
Expand Down Expand Up @@ -293,7 +308,7 @@ - (SDLAlert *)alertRPC {
SDLAlert *alert = [[SDLAlert alloc] init];
[self sdl_assembleAlertText:alert];
alert.duration = @((NSUInteger)(self.alertView.timeout * 1000));
alert.alertIcon = ([self sdl_supportsAlertIcon] && ![self.fileManager fileNeedsUpload:self.alertView.icon]) ? self.alertView.icon.imageRPC : nil;
alert.alertIcon = [self.uploadedImageNames containsObject:self.alertView.icon.name] ? self.alertView.icon.imageRPC : nil;
alert.progressIndicator = @(self.alertView.showWaitIndicator);
alert.cancelID = @(self.cancelId);

Expand Down
20 changes: 11 additions & 9 deletions SmartDeviceLinkTests/SDLPresentAlertOperationSpec.m
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ @interface SDLPresentAlertOperation()
@property (strong, nonatomic, readwrite) SDLAlertView *alertView;
@property (assign, nonatomic) UInt16 cancelId;
@property (copy, nonatomic, nullable) NSError *internalError;
@property (strong, nonatomic) NSSet<NSString *> *uploadedImageNames;

- (nullable NSError *)sdl_isValidAlertViewData:(SDLAlertView *)alertView;
- (SDLAlert *)alertRPC;
Expand Down Expand Up @@ -365,16 +366,16 @@ - (SDLAlert *)alertRPC;
testPresentAlertOperation = [[SDLPresentAlertOperation alloc] initWithConnectionManager:mockConnectionManager fileManager:mockFileManager systemCapabilityManager:mockSystemCapabilityManager currentWindowCapability:mockCurrentWindowCapability alertView:testAlertView cancelID:testCancelID];
});

it(@"should set the image if icons are supported on the module", ^{
OCMStub([mockCurrentWindowCapability hasImageFieldOfName:SDLImageFieldNameAlertIcon]).andReturn(YES);
it(@"should not set the image if it fails to upload to the module", ^{
SDLAlert *testAlert = testPresentAlertOperation.alertRPC;
expect(testAlert.alertIcon.value).to(equal(testAlertView.icon.name));
expect(testAlert.alertIcon.value).to(beNil());
});

it(@"should not set the image if icons are not supported on the module", ^{
OCMStub([mockCurrentWindowCapability hasImageFieldOfName:SDLImageFieldNameAlertIcon]).andReturn(NO);
it(@"should set the image if it is uploaded to the module", ^{
testPresentAlertOperation.uploadedImageNames = [NSSet setWithObject:testAlertView.icon.name];

SDLAlert *testAlert = testPresentAlertOperation.alertRPC;
expect(testAlert.alertIcon).to(beNil());
expect(testAlert.alertIcon.value).to(equal(testAlertView.icon.name));
});
});
});
Expand Down Expand Up @@ -781,6 +782,7 @@ - (SDLAlert *)alertRPC;

testAlertViewWithExtraSoftButtons = [[SDLAlertView alloc] initWithText:@"text" secondaryText:@"secondaryText" tertiaryText:@"tertiaryText" timeout:@(4) showWaitIndicator:@(YES) audioIndication:testAlertAudioData buttons:@[testAlertSoftButton1, testAlertSoftButton2, testAlertSoftButton3, testAlertSoftButton4, testAlertSoftButton5, testAlertSoftButton6] icon:testAlertIcon];
testPresentAlertOperation = [[SDLPresentAlertOperation alloc] initWithConnectionManager:mockConnectionManager fileManager:mockFileManager systemCapabilityManager:mockSystemCapabilityManager currentWindowCapability:mockCurrentWindowCapability alertView:testAlertViewWithExtraSoftButtons cancelID:testCancelID];
testPresentAlertOperation.uploadedImageNames = [NSSet setWithObject:testAlertViewWithExtraSoftButtons.icon.name];

testPresentAlertOperation.completionBlock = ^{
hasCalledOperationCompletionHandler = YES;
Expand Down Expand Up @@ -829,7 +831,7 @@ - (SDLAlert *)alertRPC;
testSoftButtonCapabilities.imageSupported = @YES;
OCMStub([mockCurrentWindowCapability softButtonCapabilities]).andReturn(@[testSoftButtonCapabilities]);
OCMStub([mockFileManager fileNeedsUpload:[OCMArg any]]).andReturn(YES);
OCMStub([mockFileManager uploadArtworks:[OCMArg any] progressHandler:[OCMArg invokeBlock] completionHandler:([OCMArg invokeBlockWithArgs: @[testAlertView.icon.name], [NSNull null], nil])]);
OCMStub([mockFileManager uploadArtworks:[OCMArg any] progressHandler:[OCMArg invokeBlock] completionHandler:([OCMArg invokeBlockWithArgs: @[], [NSNull null], nil])]);
OCMStub([mockFileManager uploadFiles:[OCMArg any] progressHandler:[OCMArg invokeBlock] completionHandler:[OCMArg invokeBlock]]);

SDLVersion *supportedVersion = [SDLVersion versionWithMajor:6 minor:3 patch:0];
Expand Down Expand Up @@ -883,8 +885,8 @@ - (SDLAlert *)alertRPC;
SDLSoftButtonCapabilities *testSoftButtonCapabilities = [[SDLSoftButtonCapabilities alloc] init];
testSoftButtonCapabilities.imageSupported = @YES;
OCMStub([mockCurrentWindowCapability softButtonCapabilities]).andReturn(@[testSoftButtonCapabilities]);
OCMStub([mockFileManager fileNeedsUpload:[OCMArg any]]).andReturn(NO);
OCMStub([mockFileManager uploadArtworks:[OCMArg any] progressHandler:[OCMArg invokeBlock] completionHandler:[OCMArg invokeBlock]]);
OCMStub([mockFileManager fileNeedsUpload:[OCMArg any]]).andReturn(YES);
OCMStub([mockFileManager uploadArtworks:[OCMArg any] progressHandler:[OCMArg invokeBlock] completionHandler:([OCMArg invokeBlockWithArgs: @[testAlertView.icon.name], [NSNull null], nil])]);
OCMStub([mockFileManager uploadFiles:[OCMArg any] progressHandler:[OCMArg invokeBlock] completionHandler:[OCMArg invokeBlock]]);

SDLVersion *supportedVersion = [SDLVersion versionWithMajor:6 minor:3 patch:0];
Expand Down

0 comments on commit 3c6d4a0

Please # to comment.