Commit 3041e080 authored by sczs's avatar sczs Committed by Commit Bot

[ios] Fixes HistoryInserter item insertion index.

Fixes a TableVIew model inconsistency crash. Whenever an item was inserted somewhere other than
the end of the model, a crash occurred.

Example:
If the item was inserted second to last at index 9, then the tableViewDidInsert value was also 9.
This meant that tableViewDidInsert was called twice (once for the item just inserted and once for
the previous item) and that the item that used to be at 9 shifted to 10. The problem is that
tableViewDidInsert was never called for index10. This was causing an inconsistency crash at the
end of tableUpdates.

Now we always call tableViewDidInsert with the total count of items, in order to successfully
let the tableView a new row as been inserted.

Bug: 836562
Cq-Include-Trybots: luci.chromium.try:ios-simulator-full-configs;master.tryserver.chromium.mac:ios-simulator-cronet
Change-Id: I3705cf6bd78364f04f99f29393695c1b00d613bd
Reviewed-on: https://chromium-review.googlesource.com/1121792
Commit-Queue: Sergio Collazos <sczs@chromium.org>
Reviewed-by: default avatarRohit Rao <rohitrao@chromium.org>
Cr-Commit-Position: refs/heads/master@{#571942}
parent a3833d9f
......@@ -80,15 +80,22 @@
inSortedRange:range
options:NSBinarySearchingInsertionIndex
usingComparator:objectComparator];
// Calculate the new tableView indexPath row before inserting into the
// model. No matter where in the model the item is inserted, a new row will
// be created for the tableView. For this reason, make sure to insert a new
// index into the tableView after the item has been inserted into the model.
NSInteger section =
[_listModel sectionForSectionIdentifier:sectionIdentifier];
NSInteger tableViewRow = [_listModel numberOfItemsInSection:section];
NSIndexPath* tableIndexPath =
[NSIndexPath indexPathForRow:tableViewRow inSection:section];
[_listModel insertItem:item
inSectionWithIdentifier:sectionIdentifier
atIndex:index];
NSIndexPath* indexPath = [NSIndexPath
indexPathForItem:index
inSection:[_listModel
sectionForSectionIdentifier:sectionIdentifier]];
[self.delegate historyEntryInserter:self
didInsertItemAtIndexPath:indexPath];
didInsertItemAtIndexPath:tableIndexPath];
}
}
......@@ -120,6 +127,8 @@
usingComparator:comparator];
[_dates insertObject:date atIndex:index];
NSInteger insertionIndex = _firstSectionIndex + index;
[_listModel insertSectionWithIdentifier:sectionIdentifier
atIndex:insertionIndex];
if (IsUIRefreshPhase1Enabled()) {
TableViewTextHeaderFooterItem* header =
[[TableViewTextHeaderFooterItem alloc] initWithType:kItemTypeEnumZero];
......@@ -134,8 +143,6 @@
base::SysUTF16ToNSString(history::GetRelativeDateLocalized(timestamp));
[_listModel setHeader:header forSectionWithIdentifier:sectionIdentifier];
}
[_listModel insertSectionWithIdentifier:sectionIdentifier
atIndex:insertionIndex];
[self.delegate historyEntryInserter:self
didInsertSectionAtIndex:insertionIndex];
return sectionIdentifier;
......
......@@ -75,7 +75,7 @@ TEST_F(HistoryEntryInserterTest, AddItems) {
[[mock_delegate expect]
historyEntryInserter:inserter_
didInsertItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:1]];
didInsertItemAtIndexPath:[NSIndexPath indexPathForItem:1 inSection:1]];
[inserter_ insertHistoryEntryItem:entry1];
EXPECT_OCMOCK_VERIFY(mock_delegate);
......@@ -176,7 +176,7 @@ TEST_F(HistoryEntryInserterTest, AddSections) {
[[mock_delegate expect]
historyEntryInserter:inserter_
didInsertItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:2]];
didInsertItemAtIndexPath:[NSIndexPath indexPathForItem:1 inSection:2]];
[inserter_ insertHistoryEntryItem:day2_entry1];
EXPECT_EQ(4, [model_ numberOfSections]);
EXPECT_EQ(0, [model_ numberOfItemsInSection:0]);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment