Skip to content

Commit

Permalink
Send cells after image have been uploaded if no images just send cell…
Browse files Browse the repository at this point in the history
…s. modified some tests to match code changes
  • Loading branch information
Justin Gluck committed Jun 24, 2019
1 parent 06dc401 commit d9dee5d
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 70 deletions.
35 changes: 12 additions & 23 deletions SmartDeviceLink/SDLMenuManager.m
Expand Up @@ -261,7 +261,6 @@ - (void)sdl_startDynamicMenuUpdate {

// Upload the artworks
NSArray<SDLArtwork *> *artworksToBeUploaded = [self sdl_findAllArtworksToBeUploadedFromCells:cellsToAdd];
NSArray<SDLMenuCell *> *cellWithArtWork = [self sdl_findAllCellsWithArtwork:cellsToAdd];
if (artworksToBeUploaded.count > 0) {
[self.fileManager uploadArtworks:artworksToBeUploaded completionHandler:^(NSArray<NSString *> * _Nonnull artworkNames, NSError * _Nullable error) {
if (error != nil) {
Expand All @@ -270,17 +269,17 @@ - (void)sdl_startDynamicMenuUpdate {
SDLLogD(@"Menu artworks uploaded");
// Update cells with artworks once they're uploaded
__weak typeof(self) weakself = self;
weakself.inProgressUpdate = nil;
[self sdl_updateMenuWithCellsToDelete:cellWithArtWork cellsToAdd:cellWithArtWork completionHandler:^(NSError * _Nullable error) {
[self sdl_updateMenuWithCellsToDelete:cellsToDelete cellsToAdd:cellsToAdd completionHandler:^(NSError * _Nullable error) {
[weakself sdl_startSubMenuUpdatesWithOldKeptCells:oldKeeps newKeptCells:newKeeps atIndex:0];
}];
}];
} else {
// Cells have no artwork to load
__weak typeof(self) weakself = self;
[self sdl_updateMenuWithCellsToDelete:cellsToDelete cellsToAdd:cellsToAdd completionHandler:^(NSError * _Nullable error) {
[weakself sdl_startSubMenuUpdatesWithOldKeptCells:oldKeeps newKeptCells:newKeeps atIndex:0];
}];
}
// Update cells without artworks
__weak typeof(self) weakself = self;
[self sdl_updateMenuWithCellsToDelete:cellsToDelete cellsToAdd:cellsToAdd completionHandler:^(NSError * _Nullable error) {
[weakself sdl_startSubMenuUpdatesWithOldKeptCells:oldKeeps newKeptCells:newKeeps atIndex:0];
}];
}

- (void)sdl_startNonDynamicMenuUpdate {
Expand All @@ -295,23 +294,12 @@ - (void)sdl_startNonDynamicMenuUpdate {
}

SDLLogD(@"Menu artworks uploaded");
// Update cells with artworks once they're uploaded
[self sdl_updateMenuWithCellsToDelete:self.menuCells cellsToAdd:self.menuCells completionHandler:nil];
[self sdl_updateMenuWithCellsToDelete:self.oldMenuCells cellsToAdd:self.menuCells completionHandler:nil];
}];
} else {
// Cells have no artwork to load
[self sdl_updateMenuWithCellsToDelete:self.oldMenuCells cellsToAdd:self.menuCells completionHandler:nil];
}
// Update cells without artworks
[self sdl_updateMenuWithCellsToDelete:self.oldMenuCells cellsToAdd:self.menuCells completionHandler:nil];
}

- (NSArray<SDLMenuCell *> *)sdl_findAllCellsWithArtwork:(NSArray<SDLMenuCell *> *)cells {
NSMutableArray<SDLMenuCell *> *cellWithArtWork = [[NSMutableArray alloc] init];

for (SDLMenuCell *cell in cells) {
if(cell.icon != nil) {
[cellWithArtWork addObject:cell];
}
}
return [cellWithArtWork copy];
}

- (void)sdl_updateMenuWithCellsToDelete:(NSArray<SDLMenuCell *> *)deleteCells cellsToAdd:(NSArray<SDLMenuCell *> *)addCells completionHandler:(nullable SDLMenuUpdateCompletionHandler)completionHandler {
Expand All @@ -331,6 +319,7 @@ - (void)sdl_updateMenuWithCellsToDelete:(NSArray<SDLMenuCell *> *)deleteCells ce
__weak typeof(self) weakself = self;
[self sdl_sendDeleteCurrentMenu:deleteCells withCompletionHandler:^(NSError * _Nullable error) {
[weakself sdl_sendUpdatedMenu:addCells usingMenu:weakself.menuCells withCompletionHandler:^(NSError * _Nullable error) {

weakself.inProgressUpdate = nil;

if (completionHandler != nil) {
Expand Down
80 changes: 33 additions & 47 deletions SmartDeviceLinkTests/DevAPISpecs/SDLMenuManagerSpec.m
Expand Up @@ -241,6 +241,7 @@ @interface SDLMenuManager()
});

it(@"should properly update with subcells", ^{
OCMStub([mockFileManager uploadArtworks:[OCMArg any] completionHandler:[OCMArg invokeBlock]]);
testManager.menuCells = @[submenuCell];
[mockConnectionManager respondToLastMultipleRequestsWithSuccess:YES];

Expand Down Expand Up @@ -278,12 +279,14 @@ @interface SDLMenuManager()
});
});

// No longer a valid unit test
context(@"when the image is not on the head unit", ^{
beforeEach(^{
OCMStub([mockFileManager hasUploadedFile:[OCMArg isNotNil]]).andReturn(NO);
testManager.dynamicMenuUpdatesMode = SDLDynamicMenuUpdatesModeForceOff;
OCMStub([mockFileManager uploadArtworks:[OCMArg any] completionHandler:[OCMArg invokeBlock]]);
});

it(@"should immediately attempt to update without the image", ^{
it(@"should wait till image is on head unit and attempt to update without the image", ^{
testManager.menuCells = @[textAndImageCell, submenuImageCell];

NSPredicate *addCommandPredicate = [NSPredicate predicateWithFormat:@"self isMemberOfClass: %@", [SDLAddCommand class]];
Expand Down Expand Up @@ -314,27 +317,23 @@ @interface SDLMenuManager()
[mockConnectionManager respondToLastMultipleRequestsWithSuccess:YES];

testManager.menuCells = @[textAndImageCell];
[mockConnectionManager respondToLastMultipleRequestsWithSuccess:YES]; // Without Artwork
[mockConnectionManager respondToLastMultipleRequestsWithSuccess:YES]; // Without Artwork
[mockConnectionManager respondToLastMultipleRequestsWithSuccess:YES]; // With Artwork
[mockConnectionManager respondToLastMultipleRequestsWithSuccess:YES]; // With Artwork
[mockConnectionManager respondToLastMultipleRequestsWithSuccess:YES];
[mockConnectionManager respondToLastMultipleRequestsWithSuccess:YES];

NSPredicate *deleteCommandPredicate = [NSPredicate predicateWithFormat:@"self isMemberOfClass:%@", [SDLDeleteCommand class]];
NSArray *deletes = [[mockConnectionManager.receivedRequests copy] filteredArrayUsingPredicate:deleteCommandPredicate];

NSPredicate *addCommandPredicate = [NSPredicate predicateWithFormat:@"self isMemberOfClass:%@", [SDLAddCommand class]];
NSArray *adds = [[mockConnectionManager.receivedRequests copy] filteredArrayUsingPredicate:addCommandPredicate];

expect(deletes).to(haveCount(2));
expect(adds).to(haveCount(3));
expect(deletes).to(haveCount(1));
expect(adds).to(haveCount(2));
});

it(@"should send dynamic deletes first then dynamic adds case with 2 submenu cells", ^{
testManager.menuCells = @[textOnlyCell, submenuCell, submenuImageCell];
[mockConnectionManager respondToLastMultipleRequestsWithSuccess:YES]; // Without Artwork
[mockConnectionManager respondToLastMultipleRequestsWithSuccess:YES]; // Without Artwork
[mockConnectionManager respondToLastMultipleRequestsWithSuccess:YES]; // With Artwork
[mockConnectionManager respondToLastMultipleRequestsWithSuccess:YES]; // With Artwork
[mockConnectionManager respondToLastMultipleRequestsWithSuccess:YES];
[mockConnectionManager respondToLastMultipleRequestsWithSuccess:YES];
[mockConnectionManager respondToLastMultipleRequestsWithSuccess:YES];

testManager.menuCells = @[submenuCell, submenuImageCell, textOnlyCell];
Expand All @@ -350,21 +349,14 @@ @interface SDLMenuManager()
NSPredicate *addSubmenuPredicate = [NSPredicate predicateWithFormat:@"self isMemberOfClass: %@", [SDLAddSubMenu class]];
NSArray *submenu = [[mockConnectionManager.receivedRequests copy] filteredArrayUsingPredicate:addSubmenuPredicate];

NSPredicate *deletesubmenuPredicate = [NSPredicate predicateWithFormat:@"self isMemberOfClass: %@", [SDLDeleteSubMenu class]];
NSArray *subDeletes = [[mockConnectionManager.receivedRequests copy] filteredArrayUsingPredicate:deletesubmenuPredicate];

expect(deletes).to(haveCount(1));
expect(adds).to(haveCount(6));
expect(submenu).to(haveCount(3));
expect(subDeletes).to(haveCount(1));
expect(adds).to(haveCount(5));
expect(submenu).to(haveCount(2));
});

it(@"should send dynamic deletes first then dynamic adds when removing one submenu cell", ^{
testManager.menuCells = @[textOnlyCell, textAndImageCell, submenuCell, submenuImageCell];
[mockConnectionManager respondToLastMultipleRequestsWithSuccess:YES]; // Without Artwork
[mockConnectionManager respondToLastMultipleRequestsWithSuccess:YES]; // Without Artwork
[mockConnectionManager respondToLastMultipleRequestsWithSuccess:YES]; // With Artwork
[mockConnectionManager respondToLastMultipleRequestsWithSuccess:YES]; // With Artwork
[mockConnectionManager respondToLastMultipleRequestsWithSuccess:YES];
[mockConnectionManager respondToLastMultipleRequestsWithSuccess:YES];

testManager.menuCells = @[textOnlyCell, textAndImageCell, submenuCell];
Expand All @@ -383,20 +375,16 @@ @interface SDLMenuManager()
NSPredicate *addSubmenuPredicate = [NSPredicate predicateWithFormat:@"self isMemberOfClass: %@", [SDLAddSubMenu class]];
NSArray *submenu = [[mockConnectionManager.receivedRequests copy] filteredArrayUsingPredicate:addSubmenuPredicate];

expect(deletes).to(haveCount(1));
expect(subDeletes).to(haveCount(2));
expect(adds).to(haveCount(7));
expect(submenu).to(haveCount(3));
expect(deletes).to(haveCount(0));
expect(subDeletes).to(haveCount(1));
expect(adds).to(haveCount(5));
expect(submenu).to(haveCount(2));
});

it(@"should send dynamic deletes first then dynamic adds when adding one new cell", ^{
testManager.menuCells = @[textOnlyCell, textAndImageCell, submenuCell, submenuImageCell];
[mockConnectionManager respondToLastMultipleRequestsWithSuccess:YES]; // Without Artwork
[mockConnectionManager respondToLastMultipleRequestsWithSuccess:YES]; // Without Artwork
[mockConnectionManager respondToLastMultipleRequestsWithSuccess:YES]; // With Artwork
[mockConnectionManager respondToLastMultipleRequestsWithSuccess:YES]; // With Artwork
[mockConnectionManager respondToLastMultipleRequestsWithSuccess:YES];

[mockConnectionManager respondToLastMultipleRequestsWithSuccess:YES];

testManager.menuCells = @[textOnlyCell, textAndImageCell, submenuCell, submenuImageCell, textOnlyCell2];
[mockConnectionManager respondToLastMultipleRequestsWithSuccess:YES];
Expand All @@ -411,49 +399,45 @@ @interface SDLMenuManager()
NSPredicate *addSubmenuPredicate = [NSPredicate predicateWithFormat:@"self isMemberOfClass: %@", [SDLAddSubMenu class]];
NSArray *submenu = [[mockConnectionManager.receivedRequests copy] filteredArrayUsingPredicate:addSubmenuPredicate];

NSPredicate *deleteSubCommandPredicate = [NSPredicate predicateWithFormat:@"self isMemberOfClass:%@", [SDLDeleteSubMenu class]];
NSArray *subDeletes = [[mockConnectionManager.receivedRequests copy] filteredArrayUsingPredicate:deleteSubCommandPredicate];


expect(deletes).to(haveCount(1));
expect(adds).to(haveCount(8));
expect(submenu).to(haveCount(3));
expect(subDeletes).to(haveCount(1));
expect(deletes).to(haveCount(0));
expect(adds).to(haveCount(6));
expect(submenu).to(haveCount(2));
});

it(@"should send dynamic deletes first then dynamic adds when cells stay the same", ^{
testManager.menuCells = @[textOnlyCell, textOnlyCell2, textAndImageCell];
[mockConnectionManager respondToLastMultipleRequestsWithSuccess:YES];
[mockConnectionManager respondToLastMultipleRequestsWithSuccess:YES];
[mockConnectionManager respondToLastMultipleRequestsWithSuccess:YES];
[mockConnectionManager respondToLastMultipleRequestsWithSuccess:YES];
[mockConnectionManager respondToLastMultipleRequestsWithSuccess:YES];

testManager.menuCells = @[textOnlyCell, textOnlyCell2, textAndImageCell];
[mockConnectionManager respondToLastMultipleRequestsWithSuccess:YES];
[mockConnectionManager respondToLastMultipleRequestsWithSuccess:YES];

NSPredicate *deleteCommandPredicate = [NSPredicate predicateWithFormat:@"self isMemberOfClass:%@", [SDLDeleteCommand class]];
NSArray *deletes = [[mockConnectionManager.receivedRequests copy] filteredArrayUsingPredicate:deleteCommandPredicate];

NSPredicate *addCommandPredicate = [NSPredicate predicateWithFormat:@"self isMemberOfClass:%@", [SDLAddCommand class]];
NSArray *adds = [[mockConnectionManager.receivedRequests copy] filteredArrayUsingPredicate:addCommandPredicate];

expect(deletes).to(haveCount(1));
expect(adds).to(haveCount(4));
expect(deletes).to(haveCount(0));
expect(adds).to(haveCount(3));
});
});

describe(@"updating when a menu already exists with dynamic updates off", ^{
beforeEach(^{
testManager.dynamicMenuUpdatesMode = SDLDynamicMenuUpdatesModeForceOff;
testManager.dynamicMenuUpdatesMode = SDLDynamicMenuUpdatesModeForceOff;
OCMStub([mockFileManager uploadArtworks:[OCMArg any] completionHandler:[OCMArg invokeBlock]]);
});

it(@"should send deletes first", ^{
testManager.menuCells = @[textOnlyCell];
[mockConnectionManager respondToLastMultipleRequestsWithSuccess:YES]; // Adds
[mockConnectionManager respondToLastMultipleRequestsWithSuccess:YES]; // Submenu
[mockConnectionManager respondToLastMultipleRequestsWithSuccess:YES];
[mockConnectionManager respondToLastMultipleRequestsWithSuccess:YES];

testManager.menuCells = @[textAndImageCell];
[mockConnectionManager respondToLastMultipleRequestsWithSuccess:YES];
[mockConnectionManager respondToLastMultipleRequestsWithSuccess:YES];

NSPredicate *deleteCommandPredicate = [NSPredicate predicateWithFormat:@"self isMemberOfClass:%@", [SDLDeleteCommand class]];
NSArray *deletes = [[mockConnectionManager.receivedRequests copy] filteredArrayUsingPredicate:deleteCommandPredicate];
Expand All @@ -472,6 +456,7 @@ @interface SDLMenuManager()

testManager.menuCells = @[textAndImageCell, textOnlyCell];
[mockConnectionManager respondToLastMultipleRequestsWithSuccess:YES];
[mockConnectionManager respondToLastMultipleRequestsWithSuccess:YES];

NSPredicate *deleteCommandPredicate = [NSPredicate predicateWithFormat:@"self isMemberOfClass:%@", [SDLDeleteCommand class]];
NSArray *deletes = [[mockConnectionManager.receivedRequests copy] filteredArrayUsingPredicate:deleteCommandPredicate];
Expand Down Expand Up @@ -545,6 +530,7 @@ @interface SDLMenuManager()

testManager.menuCells = @[textOnlyCell, textOnlyCell2, textAndImageCell];
[mockConnectionManager respondToLastMultipleRequestsWithSuccess:YES];
[mockConnectionManager respondToLastMultipleRequestsWithSuccess:YES];

NSPredicate *deleteCommandPredicate = [NSPredicate predicateWithFormat:@"self isMemberOfClass:%@", [SDLDeleteCommand class]];
NSArray *deletes = [[mockConnectionManager.receivedRequests copy] filteredArrayUsingPredicate:deleteCommandPredicate];
Expand Down

0 comments on commit d9dee5d

Please sign in to comment.