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/tab_switcher/tab_model_snapshot.h" |
| 6 |
| 7 #import "base/mac/scoped_nsobject.h" |
| 8 #import "ios/chrome/browser/tabs/tab.h" |
| 9 #include "testing/platform_test.h" |
| 10 #import "third_party/ocmock/OCMock/OCMock.h" |
| 11 |
| 12 @interface TabModelMock : NSObject<NSFastEnumeration> { |
| 13 base::scoped_nsobject<NSArray> tabs_; |
| 14 } |
| 15 @end |
| 16 |
| 17 @implementation TabModelMock |
| 18 |
| 19 - (id)initWithTabs:(NSArray*)tabs { |
| 20 if ((self = [super init])) { |
| 21 tabs_.reset([tabs retain]); |
| 22 } |
| 23 return self; |
| 24 } |
| 25 |
| 26 - (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState*)state |
| 27 objects:(id*)stackbuf |
| 28 count:(NSUInteger)len { |
| 29 return [tabs_ countByEnumeratingWithState:state objects:stackbuf count:len]; |
| 30 } |
| 31 |
| 32 @end |
| 33 |
| 34 namespace { |
| 35 |
| 36 class TabModelSnapshotTest : public PlatformTest { |
| 37 protected: |
| 38 Tab* TabMock(NSString* tabId, NSString* url, double time) { |
| 39 id tabMock = [OCMockObject mockForClass:[Tab class]]; |
| 40 [[[tabMock stub] andReturn:tabId] tabId]; |
| 41 [[[tabMock stub] andReturn:url] urlDisplayString]; |
| 42 [[[tabMock stub] andReturnValue:OCMOCK_VALUE(time)] lastVisitedTimestamp]; |
| 43 return (Tab*)tabMock; |
| 44 } |
| 45 }; |
| 46 |
| 47 TEST_F(TabModelSnapshotTest, TestSingleHash) { |
| 48 Tab* tab1 = TabMock(@"id1", @"url1", 12345.6789); |
| 49 Tab* tab2 = TabMock(@"id2", @"url1", 12345.6789); |
| 50 Tab* tab3 = TabMock(@"id1", @"url2", 12345.6789); |
| 51 Tab* tab4 = TabMock(@"id1", @"url1", 12345); |
| 52 |
| 53 // Same tab |
| 54 size_t hash1 = TabModelSnapshot::hashOfTheVisiblePropertiesOfATab(tab1); |
| 55 size_t hash2 = TabModelSnapshot::hashOfTheVisiblePropertiesOfATab(tab1); |
| 56 EXPECT_EQ(hash1, hash2); |
| 57 |
| 58 // Different ids |
| 59 size_t hash3 = TabModelSnapshot::hashOfTheVisiblePropertiesOfATab(tab1); |
| 60 size_t hash4 = TabModelSnapshot::hashOfTheVisiblePropertiesOfATab(tab2); |
| 61 EXPECT_NE(hash3, hash4); |
| 62 |
| 63 // Different urls |
| 64 size_t hash5 = TabModelSnapshot::hashOfTheVisiblePropertiesOfATab(tab1); |
| 65 size_t hash6 = TabModelSnapshot::hashOfTheVisiblePropertiesOfATab(tab3); |
| 66 EXPECT_NE(hash5, hash6); |
| 67 |
| 68 // Different timestamps |
| 69 size_t hash7 = TabModelSnapshot::hashOfTheVisiblePropertiesOfATab(tab1); |
| 70 size_t hash8 = TabModelSnapshot::hashOfTheVisiblePropertiesOfATab(tab4); |
| 71 EXPECT_NE(hash7, hash8); |
| 72 } |
| 73 |
| 74 TEST_F(TabModelSnapshotTest, TestSnapshotHashes) { |
| 75 Tab* tab1 = TabMock(@"id1", @"url1", 12345.6789); |
| 76 Tab* tab2 = TabMock(@"id2", @"url1", 12345.6789); |
| 77 |
| 78 base::scoped_nsobject<TabModelMock> tabModel( |
| 79 [[TabModelMock alloc] initWithTabs:@[ tab1, tab2 ]]); |
| 80 TabModelSnapshot tabModelSnapshot((TabModel*)tabModel.get()); |
| 81 tabModel.reset(); |
| 82 |
| 83 EXPECT_EQ(tabModelSnapshot.hashes().size(), 2UL); |
| 84 EXPECT_EQ(tabModelSnapshot.hashes()[0], |
| 85 TabModelSnapshot::hashOfTheVisiblePropertiesOfATab(tab1)); |
| 86 EXPECT_EQ(tabModelSnapshot.hashes()[1], |
| 87 TabModelSnapshot::hashOfTheVisiblePropertiesOfATab(tab2)); |
| 88 } |
| 89 |
| 90 } // namespace |
OLD | NEW |