OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #import "ios/chrome/browser/ui/history/history_entry_inserter.h" |
| 6 |
| 7 #import "base/mac/foundation_util.h" |
| 8 #include "base/mac/scoped_nsobject.h" |
| 9 #include "base/strings/utf_string_conversions.h" |
| 10 #include "base/time/time.h" |
| 11 #import "ios/chrome/browser/ui/collection_view/collection_view_model.h" |
| 12 #import "ios/chrome/browser/ui/history/history_entry.h" |
| 13 #import "ios/chrome/browser/ui/history/history_entry_item.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 #include "testing/gtest_mac.h" |
| 16 #include "testing/platform_test.h" |
| 17 #import "third_party/ocmock/OCMock/OCMock.h" |
| 18 #import "third_party/ocmock/gtest_support.h" |
| 19 |
| 20 HistoryEntryItem* TestHistoryEntryItem(base::Time timestamp, |
| 21 const std::string& name) { |
| 22 history::HistoryEntry entry = history::HistoryEntry( |
| 23 history::HistoryEntry::LOCAL_ENTRY, GURL(("http://" + name).c_str()), |
| 24 base::UTF8ToUTF16(name.c_str()), timestamp, std::string(), false, |
| 25 base::string16(), false); |
| 26 return [[[HistoryEntryItem alloc] initWithType:kItemTypeEnumZero |
| 27 historyEntry:entry |
| 28 browserState:nil |
| 29 delegate:nil] autorelease]; |
| 30 } |
| 31 |
| 32 // Test fixture for HistoryEntryInserter. |
| 33 class HistoryEntryInserterTest : public PlatformTest { |
| 34 public: |
| 35 HistoryEntryInserterTest() { |
| 36 model_.reset([[CollectionViewModel alloc] init]); |
| 37 [model_ addSectionWithIdentifier:kSectionIdentifierEnumZero]; |
| 38 inserter_.reset([[HistoryEntryInserter alloc] initWithModel:model_]); |
| 39 mock_delegate_.reset([[OCMockObject |
| 40 mockForProtocol:@protocol(HistoryEntryInserterDelegate)] retain]); |
| 41 [inserter_ setDelegate:mock_delegate_]; |
| 42 } |
| 43 |
| 44 protected: |
| 45 base::scoped_nsobject<CollectionViewModel> model_; |
| 46 base::scoped_nsobject<HistoryEntryInserter> inserter_; |
| 47 base::scoped_nsprotocol<id<HistoryEntryInserterDelegate>> mock_delegate_; |
| 48 }; |
| 49 |
| 50 // Tests that history entry items added to CollectionViewModel are sorted by |
| 51 // timestamp. |
| 52 TEST_F(HistoryEntryInserterTest, AddItems) { |
| 53 base::Time today = |
| 54 base::Time::Now().LocalMidnight() + base::TimeDelta::FromHours(1); |
| 55 base::TimeDelta minute = base::TimeDelta::FromMinutes(1); |
| 56 HistoryEntryItem* entry1 = TestHistoryEntryItem(today, "entry1"); |
| 57 HistoryEntryItem* entry2 = TestHistoryEntryItem(today - minute, "entry2"); |
| 58 HistoryEntryItem* entry3 = |
| 59 TestHistoryEntryItem(today - 2 * (minute), "entry3"); |
| 60 |
| 61 OCMockObject* mock_delegate = (OCMockObject*)mock_delegate_.get(); |
| 62 [[mock_delegate expect] historyEntryInserter:inserter_ |
| 63 didInsertSectionAtIndex:1]; |
| 64 [[mock_delegate expect] |
| 65 historyEntryInserter:inserter_ |
| 66 didInsertItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:1]]; |
| 67 [inserter_ insertHistoryEntryItem:entry2]; |
| 68 EXPECT_OCMOCK_VERIFY(mock_delegate); |
| 69 |
| 70 [[mock_delegate expect] |
| 71 historyEntryInserter:inserter_ |
| 72 didInsertItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:1]]; |
| 73 [inserter_ insertHistoryEntryItem:entry1]; |
| 74 EXPECT_OCMOCK_VERIFY(mock_delegate); |
| 75 |
| 76 [[mock_delegate expect] |
| 77 historyEntryInserter:inserter_ |
| 78 didInsertItemAtIndexPath:[NSIndexPath indexPathForItem:2 inSection:1]]; |
| 79 [inserter_ insertHistoryEntryItem:entry3]; |
| 80 EXPECT_OCMOCK_VERIFY(mock_delegate); |
| 81 |
| 82 EXPECT_EQ(2, [model_ numberOfSections]); |
| 83 EXPECT_EQ(0, [model_ numberOfItemsInSection:0]); |
| 84 EXPECT_EQ(3, [model_ numberOfItemsInSection:1]); |
| 85 |
| 86 NSArray<HistoryEntryItem*>* section_1 = |
| 87 base::mac::ObjCCastStrict<NSArray<HistoryEntryItem*>>( |
| 88 [model_ itemsInSectionWithIdentifier:kSectionIdentifierEnumZero + 1]); |
| 89 EXPECT_NSEQ(@"entry1", section_1[0].text); |
| 90 EXPECT_NSEQ(@"entry2", section_1[1].text); |
| 91 EXPECT_NSEQ(@"entry3", section_1[2].text); |
| 92 EXPECT_OCMOCK_VERIFY(mock_delegate); |
| 93 } |
| 94 |
| 95 // Tests that items from different dates are added in correctly ordered |
| 96 // sections. |
| 97 TEST_F(HistoryEntryInserterTest, AddSections) { |
| 98 base::Time today = |
| 99 base::Time::Now().LocalMidnight() + base::TimeDelta::FromHours(1); |
| 100 base::TimeDelta day = base::TimeDelta::FromDays(1); |
| 101 base::TimeDelta minute = base::TimeDelta::FromMinutes(1); |
| 102 HistoryEntryItem* day1 = TestHistoryEntryItem(today, "day1"); |
| 103 HistoryEntryItem* day2_entry1 = |
| 104 TestHistoryEntryItem(today - day, "day2_entry1"); |
| 105 HistoryEntryItem* day2_entry2 = |
| 106 TestHistoryEntryItem(today - day - minute, "day2_entry2"); |
| 107 HistoryEntryItem* day3 = TestHistoryEntryItem(today - 2 * day, "day3"); |
| 108 |
| 109 OCMockObject* mock_delegate = (OCMockObject*)mock_delegate_.get(); |
| 110 |
| 111 [[mock_delegate expect] historyEntryInserter:inserter_ |
| 112 didInsertSectionAtIndex:1]; |
| 113 [[mock_delegate expect] |
| 114 historyEntryInserter:inserter_ |
| 115 didInsertItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:1]]; |
| 116 [inserter_ insertHistoryEntryItem:day2_entry2]; |
| 117 NSInteger day2_identifier = kSectionIdentifierEnumZero + 1; |
| 118 EXPECT_EQ(2, [model_ numberOfSections]); |
| 119 EXPECT_EQ(0, [model_ numberOfItemsInSection:0]); |
| 120 EXPECT_EQ(1, [model_ numberOfItemsInSection:1]); |
| 121 NSArray<HistoryEntryItem*>* section_1 = |
| 122 base::mac::ObjCCastStrict<NSArray<HistoryEntryItem*>>( |
| 123 [model_ itemsInSectionWithIdentifier:day2_identifier]); |
| 124 EXPECT_NSEQ(@"day2_entry2", section_1[0].text); |
| 125 EXPECT_OCMOCK_VERIFY(mock_delegate); |
| 126 |
| 127 [[mock_delegate expect] historyEntryInserter:inserter_ |
| 128 didInsertSectionAtIndex:1]; |
| 129 [[mock_delegate expect] |
| 130 historyEntryInserter:inserter_ |
| 131 didInsertItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:1]]; |
| 132 [inserter_ insertHistoryEntryItem:day1]; |
| 133 NSInteger day1_identifier = kSectionIdentifierEnumZero + 2; |
| 134 EXPECT_EQ(3, [model_ numberOfSections]); |
| 135 EXPECT_EQ(0, [model_ numberOfItemsInSection:0]); |
| 136 EXPECT_EQ(1, [model_ numberOfItemsInSection:1]); |
| 137 EXPECT_EQ(1, [model_ numberOfItemsInSection:2]); |
| 138 section_1 = base::mac::ObjCCastStrict<NSArray<HistoryEntryItem*>>( |
| 139 [model_ itemsInSectionWithIdentifier:day1_identifier]); |
| 140 EXPECT_NSEQ(@"day1", section_1[0].text); |
| 141 NSArray<HistoryEntryItem*>* section_2 = |
| 142 base::mac::ObjCCastStrict<NSArray<HistoryEntryItem*>>( |
| 143 [model_ itemsInSectionWithIdentifier:day2_identifier]); |
| 144 EXPECT_NSEQ(@"day2_entry2", section_2[0].text); |
| 145 EXPECT_OCMOCK_VERIFY(mock_delegate); |
| 146 |
| 147 [[mock_delegate expect] historyEntryInserter:inserter_ |
| 148 didInsertSectionAtIndex:3]; |
| 149 [[mock_delegate expect] |
| 150 historyEntryInserter:inserter_ |
| 151 didInsertItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:3]]; |
| 152 [inserter_ insertHistoryEntryItem:day3]; |
| 153 NSInteger day3_identifier = kSectionIdentifierEnumZero + 3; |
| 154 EXPECT_EQ(4, [model_ numberOfSections]); |
| 155 EXPECT_EQ(0, [model_ numberOfItemsInSection:0]); |
| 156 EXPECT_EQ(1, [model_ numberOfItemsInSection:1]); |
| 157 EXPECT_EQ(1, [model_ numberOfItemsInSection:2]); |
| 158 EXPECT_EQ(1, [model_ numberOfItemsInSection:3]); |
| 159 section_1 = base::mac::ObjCCastStrict<NSArray<HistoryEntryItem*>>( |
| 160 [model_ itemsInSectionWithIdentifier:day1_identifier]); |
| 161 EXPECT_NSEQ(@"day1", section_1[0].text); |
| 162 section_2 = base::mac::ObjCCastStrict<NSArray<HistoryEntryItem*>>( |
| 163 [model_ itemsInSectionWithIdentifier:day2_identifier]); |
| 164 EXPECT_NSEQ(@"day2_entry2", section_2[0].text); |
| 165 NSArray<HistoryEntryItem*>* section_3 = |
| 166 base::mac::ObjCCastStrict<NSArray<HistoryEntryItem*>>( |
| 167 [model_ itemsInSectionWithIdentifier:day3_identifier]); |
| 168 EXPECT_NSEQ(@"day3", section_3[0].text); |
| 169 EXPECT_OCMOCK_VERIFY(mock_delegate); |
| 170 |
| 171 [[mock_delegate expect] |
| 172 historyEntryInserter:inserter_ |
| 173 didInsertItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:2]]; |
| 174 [inserter_ insertHistoryEntryItem:day2_entry1]; |
| 175 EXPECT_EQ(4, [model_ numberOfSections]); |
| 176 EXPECT_EQ(0, [model_ numberOfItemsInSection:0]); |
| 177 EXPECT_EQ(1, [model_ numberOfItemsInSection:1]); |
| 178 EXPECT_EQ(2, [model_ numberOfItemsInSection:2]); |
| 179 EXPECT_EQ(1, [model_ numberOfItemsInSection:3]); |
| 180 section_1 = base::mac::ObjCCastStrict<NSArray<HistoryEntryItem*>>( |
| 181 [model_ itemsInSectionWithIdentifier:day1_identifier]); |
| 182 EXPECT_NSEQ(@"day1", section_1[0].text); |
| 183 section_2 = base::mac::ObjCCastStrict<NSArray<HistoryEntryItem*>>( |
| 184 [model_ itemsInSectionWithIdentifier:day2_identifier]); |
| 185 EXPECT_NSEQ(@"day2_entry1", section_2[0].text); |
| 186 EXPECT_NSEQ(@"day2_entry2", section_2[1].text); |
| 187 section_3 = base::mac::ObjCCastStrict<NSArray<HistoryEntryItem*>>( |
| 188 [model_ itemsInSectionWithIdentifier:day3_identifier]); |
| 189 EXPECT_NSEQ(@"day3", section_3[0].text); |
| 190 EXPECT_OCMOCK_VERIFY(mock_delegate); |
| 191 } |
| 192 |
| 193 // Tests that items are only ever added once. |
| 194 TEST_F(HistoryEntryInserterTest, AddDuplicateItems) { |
| 195 base::Time today = base::Time::Now(); |
| 196 HistoryEntryItem* entry1 = TestHistoryEntryItem(today, "entry"); |
| 197 HistoryEntryItem* entry2 = TestHistoryEntryItem(today, "entry"); |
| 198 |
| 199 OCMockObject* mock_delegate = (OCMockObject*)mock_delegate_.get(); |
| 200 [[mock_delegate expect] historyEntryInserter:inserter_ |
| 201 didInsertSectionAtIndex:1]; |
| 202 [[mock_delegate expect] |
| 203 historyEntryInserter:inserter_ |
| 204 didInsertItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:1]]; |
| 205 [inserter_ insertHistoryEntryItem:entry1]; |
| 206 [inserter_ insertHistoryEntryItem:entry2]; |
| 207 |
| 208 EXPECT_EQ(2, [model_ numberOfSections]); |
| 209 EXPECT_EQ(0, [model_ numberOfItemsInSection:0]); |
| 210 EXPECT_EQ(1, [model_ numberOfItemsInSection:1]); |
| 211 |
| 212 NSArray<HistoryEntryItem*>* section_1 = |
| 213 base::mac::ObjCCastStrict<NSArray<HistoryEntryItem*>>( |
| 214 [model_ itemsInSectionWithIdentifier:kSectionIdentifierEnumZero + 1]); |
| 215 EXPECT_NSEQ(@"entry", section_1[0].text); |
| 216 EXPECT_OCMOCK_VERIFY(mock_delegate); |
| 217 } |
| 218 |
| 219 // Tests that removing a section invokes the appropriate delegate callback. |
| 220 TEST_F(HistoryEntryInserterTest, RemoveSection) { |
| 221 base::Time today = |
| 222 base::Time::Now().LocalMidnight() + base::TimeDelta::FromHours(1); |
| 223 base::TimeDelta day = base::TimeDelta::FromDays(1); |
| 224 HistoryEntryItem* day1 = TestHistoryEntryItem(today, "day1"); |
| 225 HistoryEntryItem* day2 = TestHistoryEntryItem(today - day, "day2"); |
| 226 |
| 227 OCMockObject* mock_delegate = (OCMockObject*)mock_delegate_.get(); |
| 228 |
| 229 [[mock_delegate expect] historyEntryInserter:inserter_ |
| 230 didInsertSectionAtIndex:1]; |
| 231 [[mock_delegate expect] |
| 232 historyEntryInserter:inserter_ |
| 233 didInsertItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:1]]; |
| 234 [inserter_ insertHistoryEntryItem:day1]; |
| 235 NSInteger day1_identifier = kSectionIdentifierEnumZero + 1; |
| 236 EXPECT_OCMOCK_VERIFY(mock_delegate); |
| 237 |
| 238 [[mock_delegate expect] historyEntryInserter:inserter_ |
| 239 didInsertSectionAtIndex:2]; |
| 240 [[mock_delegate expect] |
| 241 historyEntryInserter:inserter_ |
| 242 didInsertItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:2]]; |
| 243 [inserter_ insertHistoryEntryItem:day2]; |
| 244 EXPECT_EQ(3, [model_ numberOfSections]); |
| 245 EXPECT_OCMOCK_VERIFY(mock_delegate); |
| 246 |
| 247 // Empty the section for day 1, and remove the section. |
| 248 [model_ removeItemWithType:kItemTypeEnumZero |
| 249 fromSectionWithIdentifier:day1_identifier]; |
| 250 [[mock_delegate expect] historyEntryInserter:inserter_ |
| 251 didRemoveSectionAtIndex:1]; |
| 252 [inserter_ removeSection:1]; |
| 253 |
| 254 EXPECT_EQ(2, [model_ numberOfSections]); |
| 255 EXPECT_OCMOCK_VERIFY(mock_delegate); |
| 256 } |
OLD | NEW |