| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #import "ios/chrome/browser/ui/history/history_entry_inserter.h" | 5 #import "ios/chrome/browser/ui/history/history_entry_inserter.h" |
| 6 | 6 |
| 7 #import "base/mac/foundation_util.h" | 7 #import "base/mac/foundation_util.h" |
| 8 #include "base/mac/scoped_nsobject.h" | |
| 9 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
| 10 #include "base/time/time.h" | 9 #include "base/time/time.h" |
| 11 #import "ios/chrome/browser/ui/collection_view/collection_view_model.h" | 10 #import "ios/chrome/browser/ui/collection_view/collection_view_model.h" |
| 12 #import "ios/chrome/browser/ui/history/history_entry.h" | 11 #import "ios/chrome/browser/ui/history/history_entry.h" |
| 13 #import "ios/chrome/browser/ui/history/history_entry_item.h" | 12 #import "ios/chrome/browser/ui/history/history_entry_item.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 15 #include "testing/gtest_mac.h" | 14 #include "testing/gtest_mac.h" |
| 16 #include "testing/platform_test.h" | 15 #include "testing/platform_test.h" |
| 17 #import "third_party/ocmock/OCMock/OCMock.h" | 16 #import "third_party/ocmock/OCMock/OCMock.h" |
| 18 #import "third_party/ocmock/gtest_support.h" | 17 #import "third_party/ocmock/gtest_support.h" |
| 19 | 18 |
| 19 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 20 #error "This file requires ARC support." |
| 21 #endif |
| 22 |
| 20 HistoryEntryItem* TestHistoryEntryItem(base::Time timestamp, | 23 HistoryEntryItem* TestHistoryEntryItem(base::Time timestamp, |
| 21 const std::string& name) { | 24 const std::string& name) { |
| 22 history::HistoryEntry entry = history::HistoryEntry( | 25 history::HistoryEntry entry = history::HistoryEntry( |
| 23 history::HistoryEntry::LOCAL_ENTRY, GURL(("http://" + name).c_str()), | 26 history::HistoryEntry::LOCAL_ENTRY, GURL(("http://" + name).c_str()), |
| 24 base::UTF8ToUTF16(name.c_str()), timestamp, std::string(), false, | 27 base::UTF8ToUTF16(name.c_str()), timestamp, std::string(), false, |
| 25 base::string16(), false); | 28 base::string16(), false); |
| 26 return [[[HistoryEntryItem alloc] initWithType:kItemTypeEnumZero | 29 return [[HistoryEntryItem alloc] initWithType:kItemTypeEnumZero |
| 27 historyEntry:entry | 30 historyEntry:entry |
| 28 browserState:nil | 31 browserState:nil |
| 29 delegate:nil] autorelease]; | 32 delegate:nil]; |
| 30 } | 33 } |
| 31 | 34 |
| 32 // Test fixture for HistoryEntryInserter. | 35 // Test fixture for HistoryEntryInserter. |
| 33 class HistoryEntryInserterTest : public PlatformTest { | 36 class HistoryEntryInserterTest : public PlatformTest { |
| 34 public: | 37 public: |
| 35 HistoryEntryInserterTest() { | 38 HistoryEntryInserterTest() { |
| 36 model_.reset([[CollectionViewModel alloc] init]); | 39 model_ = [[CollectionViewModel alloc] init]; |
| 37 [model_ addSectionWithIdentifier:kSectionIdentifierEnumZero]; | 40 [model_ addSectionWithIdentifier:kSectionIdentifierEnumZero]; |
| 38 inserter_.reset([[HistoryEntryInserter alloc] initWithModel:model_]); | 41 inserter_ = [[HistoryEntryInserter alloc] initWithModel:model_]; |
| 39 mock_delegate_.reset([[OCMockObject | 42 mock_delegate_ = |
| 40 mockForProtocol:@protocol(HistoryEntryInserterDelegate)] retain]); | 43 [OCMockObject mockForProtocol:@protocol(HistoryEntryInserterDelegate)]; |
| 41 [inserter_ setDelegate:mock_delegate_]; | 44 [inserter_ setDelegate:mock_delegate_]; |
| 42 } | 45 } |
| 43 | 46 |
| 44 protected: | 47 protected: |
| 45 base::scoped_nsobject<CollectionViewModel> model_; | 48 __strong CollectionViewModel* model_; |
| 46 base::scoped_nsobject<HistoryEntryInserter> inserter_; | 49 __strong HistoryEntryInserter* inserter_; |
| 47 base::scoped_nsprotocol<id<HistoryEntryInserterDelegate>> mock_delegate_; | 50 __strong id<HistoryEntryInserterDelegate> mock_delegate_; |
| 48 }; | 51 }; |
| 49 | 52 |
| 50 // Tests that history entry items added to CollectionViewModel are sorted by | 53 // Tests that history entry items added to CollectionViewModel are sorted by |
| 51 // timestamp. | 54 // timestamp. |
| 52 TEST_F(HistoryEntryInserterTest, AddItems) { | 55 TEST_F(HistoryEntryInserterTest, AddItems) { |
| 53 base::Time today = | 56 base::Time today = |
| 54 base::Time::Now().LocalMidnight() + base::TimeDelta::FromHours(1); | 57 base::Time::Now().LocalMidnight() + base::TimeDelta::FromHours(1); |
| 55 base::TimeDelta minute = base::TimeDelta::FromMinutes(1); | 58 base::TimeDelta minute = base::TimeDelta::FromMinutes(1); |
| 56 HistoryEntryItem* entry1 = TestHistoryEntryItem(today, "entry1"); | 59 HistoryEntryItem* entry1 = TestHistoryEntryItem(today, "entry1"); |
| 57 HistoryEntryItem* entry2 = TestHistoryEntryItem(today - minute, "entry2"); | 60 HistoryEntryItem* entry2 = TestHistoryEntryItem(today - minute, "entry2"); |
| 58 HistoryEntryItem* entry3 = | 61 HistoryEntryItem* entry3 = |
| 59 TestHistoryEntryItem(today - 2 * (minute), "entry3"); | 62 TestHistoryEntryItem(today - 2 * (minute), "entry3"); |
| 60 | 63 |
| 61 OCMockObject* mock_delegate = (OCMockObject*)mock_delegate_.get(); | 64 OCMockObject* mock_delegate = (OCMockObject*)mock_delegate_; |
| 62 [[mock_delegate expect] historyEntryInserter:inserter_ | 65 [[mock_delegate expect] historyEntryInserter:inserter_ |
| 63 didInsertSectionAtIndex:1]; | 66 didInsertSectionAtIndex:1]; |
| 64 [[mock_delegate expect] | 67 [[mock_delegate expect] |
| 65 historyEntryInserter:inserter_ | 68 historyEntryInserter:inserter_ |
| 66 didInsertItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:1]]; | 69 didInsertItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:1]]; |
| 67 [inserter_ insertHistoryEntryItem:entry2]; | 70 [inserter_ insertHistoryEntryItem:entry2]; |
| 68 EXPECT_OCMOCK_VERIFY(mock_delegate); | 71 EXPECT_OCMOCK_VERIFY(mock_delegate); |
| 69 | 72 |
| 70 [[mock_delegate expect] | 73 [[mock_delegate expect] |
| 71 historyEntryInserter:inserter_ | 74 historyEntryInserter:inserter_ |
| (...skipping 27 matching lines...) Expand all Loading... |
| 99 base::Time::Now().LocalMidnight() + base::TimeDelta::FromHours(12); | 102 base::Time::Now().LocalMidnight() + base::TimeDelta::FromHours(12); |
| 100 base::TimeDelta day = base::TimeDelta::FromDays(1); | 103 base::TimeDelta day = base::TimeDelta::FromDays(1); |
| 101 base::TimeDelta minute = base::TimeDelta::FromMinutes(1); | 104 base::TimeDelta minute = base::TimeDelta::FromMinutes(1); |
| 102 HistoryEntryItem* day1 = TestHistoryEntryItem(today, "day1"); | 105 HistoryEntryItem* day1 = TestHistoryEntryItem(today, "day1"); |
| 103 HistoryEntryItem* day2_entry1 = | 106 HistoryEntryItem* day2_entry1 = |
| 104 TestHistoryEntryItem(today - day, "day2_entry1"); | 107 TestHistoryEntryItem(today - day, "day2_entry1"); |
| 105 HistoryEntryItem* day2_entry2 = | 108 HistoryEntryItem* day2_entry2 = |
| 106 TestHistoryEntryItem(today - day - minute, "day2_entry2"); | 109 TestHistoryEntryItem(today - day - minute, "day2_entry2"); |
| 107 HistoryEntryItem* day3 = TestHistoryEntryItem(today - 2 * day, "day3"); | 110 HistoryEntryItem* day3 = TestHistoryEntryItem(today - 2 * day, "day3"); |
| 108 | 111 |
| 109 OCMockObject* mock_delegate = (OCMockObject*)mock_delegate_.get(); | 112 OCMockObject* mock_delegate = (OCMockObject*)mock_delegate_; |
| 110 | 113 |
| 111 [[mock_delegate expect] historyEntryInserter:inserter_ | 114 [[mock_delegate expect] historyEntryInserter:inserter_ |
| 112 didInsertSectionAtIndex:1]; | 115 didInsertSectionAtIndex:1]; |
| 113 [[mock_delegate expect] | 116 [[mock_delegate expect] |
| 114 historyEntryInserter:inserter_ | 117 historyEntryInserter:inserter_ |
| 115 didInsertItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:1]]; | 118 didInsertItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:1]]; |
| 116 [inserter_ insertHistoryEntryItem:day2_entry2]; | 119 [inserter_ insertHistoryEntryItem:day2_entry2]; |
| 117 NSInteger day2_identifier = kSectionIdentifierEnumZero + 1; | 120 NSInteger day2_identifier = kSectionIdentifierEnumZero + 1; |
| 118 EXPECT_EQ(2, [model_ numberOfSections]); | 121 EXPECT_EQ(2, [model_ numberOfSections]); |
| 119 EXPECT_EQ(0, [model_ numberOfItemsInSection:0]); | 122 EXPECT_EQ(0, [model_ numberOfItemsInSection:0]); |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 189 EXPECT_NSEQ(@"day3", section_3[0].text); | 192 EXPECT_NSEQ(@"day3", section_3[0].text); |
| 190 EXPECT_OCMOCK_VERIFY(mock_delegate); | 193 EXPECT_OCMOCK_VERIFY(mock_delegate); |
| 191 } | 194 } |
| 192 | 195 |
| 193 // Tests that items are only ever added once. | 196 // Tests that items are only ever added once. |
| 194 TEST_F(HistoryEntryInserterTest, AddDuplicateItems) { | 197 TEST_F(HistoryEntryInserterTest, AddDuplicateItems) { |
| 195 base::Time today = base::Time::Now(); | 198 base::Time today = base::Time::Now(); |
| 196 HistoryEntryItem* entry1 = TestHistoryEntryItem(today, "entry"); | 199 HistoryEntryItem* entry1 = TestHistoryEntryItem(today, "entry"); |
| 197 HistoryEntryItem* entry2 = TestHistoryEntryItem(today, "entry"); | 200 HistoryEntryItem* entry2 = TestHistoryEntryItem(today, "entry"); |
| 198 | 201 |
| 199 OCMockObject* mock_delegate = (OCMockObject*)mock_delegate_.get(); | 202 OCMockObject* mock_delegate = (OCMockObject*)mock_delegate_; |
| 200 [[mock_delegate expect] historyEntryInserter:inserter_ | 203 [[mock_delegate expect] historyEntryInserter:inserter_ |
| 201 didInsertSectionAtIndex:1]; | 204 didInsertSectionAtIndex:1]; |
| 202 [[mock_delegate expect] | 205 [[mock_delegate expect] |
| 203 historyEntryInserter:inserter_ | 206 historyEntryInserter:inserter_ |
| 204 didInsertItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:1]]; | 207 didInsertItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:1]]; |
| 205 [inserter_ insertHistoryEntryItem:entry1]; | 208 [inserter_ insertHistoryEntryItem:entry1]; |
| 206 [inserter_ insertHistoryEntryItem:entry2]; | 209 [inserter_ insertHistoryEntryItem:entry2]; |
| 207 | 210 |
| 208 EXPECT_EQ(2, [model_ numberOfSections]); | 211 EXPECT_EQ(2, [model_ numberOfSections]); |
| 209 EXPECT_EQ(0, [model_ numberOfItemsInSection:0]); | 212 EXPECT_EQ(0, [model_ numberOfItemsInSection:0]); |
| 210 EXPECT_EQ(1, [model_ numberOfItemsInSection:1]); | 213 EXPECT_EQ(1, [model_ numberOfItemsInSection:1]); |
| 211 | 214 |
| 212 NSArray<HistoryEntryItem*>* section_1 = | 215 NSArray<HistoryEntryItem*>* section_1 = |
| 213 base::mac::ObjCCastStrict<NSArray<HistoryEntryItem*>>( | 216 base::mac::ObjCCastStrict<NSArray<HistoryEntryItem*>>( |
| 214 [model_ itemsInSectionWithIdentifier:kSectionIdentifierEnumZero + 1]); | 217 [model_ itemsInSectionWithIdentifier:kSectionIdentifierEnumZero + 1]); |
| 215 EXPECT_NSEQ(@"entry", section_1[0].text); | 218 EXPECT_NSEQ(@"entry", section_1[0].text); |
| 216 EXPECT_OCMOCK_VERIFY(mock_delegate); | 219 EXPECT_OCMOCK_VERIFY(mock_delegate); |
| 217 } | 220 } |
| 218 | 221 |
| 219 // Tests that removing a section invokes the appropriate delegate callback. | 222 // Tests that removing a section invokes the appropriate delegate callback. |
| 220 TEST_F(HistoryEntryInserterTest, RemoveSection) { | 223 TEST_F(HistoryEntryInserterTest, RemoveSection) { |
| 221 base::Time today = | 224 base::Time today = |
| 222 base::Time::Now().LocalMidnight() + base::TimeDelta::FromHours(1); | 225 base::Time::Now().LocalMidnight() + base::TimeDelta::FromHours(1); |
| 223 base::TimeDelta day = base::TimeDelta::FromDays(1); | 226 base::TimeDelta day = base::TimeDelta::FromDays(1); |
| 224 HistoryEntryItem* day1 = TestHistoryEntryItem(today, "day1"); | 227 HistoryEntryItem* day1 = TestHistoryEntryItem(today, "day1"); |
| 225 HistoryEntryItem* day2 = TestHistoryEntryItem(today - day, "day2"); | 228 HistoryEntryItem* day2 = TestHistoryEntryItem(today - day, "day2"); |
| 226 | 229 |
| 227 OCMockObject* mock_delegate = (OCMockObject*)mock_delegate_.get(); | 230 OCMockObject* mock_delegate = (OCMockObject*)mock_delegate_; |
| 228 | 231 |
| 229 [[mock_delegate expect] historyEntryInserter:inserter_ | 232 [[mock_delegate expect] historyEntryInserter:inserter_ |
| 230 didInsertSectionAtIndex:1]; | 233 didInsertSectionAtIndex:1]; |
| 231 [[mock_delegate expect] | 234 [[mock_delegate expect] |
| 232 historyEntryInserter:inserter_ | 235 historyEntryInserter:inserter_ |
| 233 didInsertItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:1]]; | 236 didInsertItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:1]]; |
| 234 [inserter_ insertHistoryEntryItem:day1]; | 237 [inserter_ insertHistoryEntryItem:day1]; |
| 235 NSInteger day1_identifier = kSectionIdentifierEnumZero + 1; | 238 NSInteger day1_identifier = kSectionIdentifierEnumZero + 1; |
| 236 EXPECT_OCMOCK_VERIFY(mock_delegate); | 239 EXPECT_OCMOCK_VERIFY(mock_delegate); |
| 237 | 240 |
| 238 [[mock_delegate expect] historyEntryInserter:inserter_ | 241 [[mock_delegate expect] historyEntryInserter:inserter_ |
| 239 didInsertSectionAtIndex:2]; | 242 didInsertSectionAtIndex:2]; |
| 240 [[mock_delegate expect] | 243 [[mock_delegate expect] |
| 241 historyEntryInserter:inserter_ | 244 historyEntryInserter:inserter_ |
| 242 didInsertItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:2]]; | 245 didInsertItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:2]]; |
| 243 [inserter_ insertHistoryEntryItem:day2]; | 246 [inserter_ insertHistoryEntryItem:day2]; |
| 244 EXPECT_EQ(3, [model_ numberOfSections]); | 247 EXPECT_EQ(3, [model_ numberOfSections]); |
| 245 EXPECT_OCMOCK_VERIFY(mock_delegate); | 248 EXPECT_OCMOCK_VERIFY(mock_delegate); |
| 246 | 249 |
| 247 // Empty the section for day 1, and remove the section. | 250 // Empty the section for day 1, and remove the section. |
| 248 [model_ removeItemWithType:kItemTypeEnumZero | 251 [model_ removeItemWithType:kItemTypeEnumZero |
| 249 fromSectionWithIdentifier:day1_identifier]; | 252 fromSectionWithIdentifier:day1_identifier]; |
| 250 [[mock_delegate expect] historyEntryInserter:inserter_ | 253 [[mock_delegate expect] historyEntryInserter:inserter_ |
| 251 didRemoveSectionAtIndex:1]; | 254 didRemoveSectionAtIndex:1]; |
| 252 [inserter_ removeSection:1]; | 255 [inserter_ removeSection:1]; |
| 253 | 256 |
| 254 EXPECT_EQ(2, [model_ numberOfSections]); | 257 EXPECT_EQ(2, [model_ numberOfSections]); |
| 255 EXPECT_OCMOCK_VERIFY(mock_delegate); | 258 EXPECT_OCMOCK_VERIFY(mock_delegate); |
| 256 } | 259 } |
| OLD | NEW |