| OLD | NEW |
| (Empty) |
| 1 // Copyright 2015 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 #include "components/sync_driver/revisit/offset_tab_matcher.h" | |
| 6 | |
| 7 #include "base/memory/scoped_ptr.h" | |
| 8 #include "base/test/histogram_tester.h" | |
| 9 #include "base/time/time.h" | |
| 10 #include "components/sessions/core/serialized_navigation_entry.h" | |
| 11 #include "components/sessions/core/serialized_navigation_entry_test_helper.h" | |
| 12 #include "components/sessions/core/session_types.h" | |
| 13 #include "components/sync_driver/revisit/page_equality.h" | |
| 14 #include "testing/gtest/include/gtest/gtest.h" | |
| 15 #include "url/gurl.h" | |
| 16 | |
| 17 using sessions::SessionTab; | |
| 18 | |
| 19 namespace sync_driver { | |
| 20 | |
| 21 namespace { | |
| 22 | |
| 23 static const std::string kExampleUrl = "http://www.example.com"; | |
| 24 static const std::string kDifferentUrl = "http://www.different.com"; | |
| 25 | |
| 26 sessions::SerializedNavigationEntry Entry(const std::string& url) { | |
| 27 return sessions::SerializedNavigationEntryTestHelper::CreateNavigation(url, | |
| 28 ""); | |
| 29 } | |
| 30 | |
| 31 scoped_ptr<SessionTab> Tab(const int index, | |
| 32 const base::Time timestamp = base::Time::Now()) { | |
| 33 scoped_ptr<SessionTab> tab(new SessionTab()); | |
| 34 tab->current_navigation_index = index; | |
| 35 tab->timestamp = timestamp; | |
| 36 return tab; | |
| 37 } | |
| 38 | |
| 39 void VerifyMatch(OffsetTabMatcher& matcher, const int offset) { | |
| 40 base::HistogramTester histogram_tester; | |
| 41 matcher.Emit(PageVisitObserver::kTransitionPage); | |
| 42 histogram_tester.ExpectUniqueSample("Sync.PageRevisitNavigationMatchOffset", | |
| 43 offset, 1); | |
| 44 histogram_tester.ExpectUniqueSample( | |
| 45 "Sync.PageRevisitNavigationMatchTransition", | |
| 46 PageVisitObserver::kTransitionPage, 1); | |
| 47 histogram_tester.ExpectTotalCount("Sync.PageRevisitNavigationMatchAge", 1); | |
| 48 } | |
| 49 | |
| 50 void VerifyMiss(OffsetTabMatcher& matcher) { | |
| 51 base::HistogramTester histogram_tester; | |
| 52 matcher.Emit(PageVisitObserver::kTransitionPage); | |
| 53 histogram_tester.ExpectUniqueSample( | |
| 54 "Sync.PageRevisitNavigationMissTransition", | |
| 55 PageVisitObserver::kTransitionPage, 1); | |
| 56 } | |
| 57 | |
| 58 } // namespace | |
| 59 | |
| 60 TEST(OffsetTabMatcherTest, NoCheck) { | |
| 61 OffsetTabMatcher matcher((PageEquality(GURL(kExampleUrl)))); | |
| 62 VerifyMiss(matcher); | |
| 63 } | |
| 64 | |
| 65 TEST(OffsetTabMatcherTest, EmptyTab) { | |
| 66 scoped_ptr<SessionTab> tab = Tab(0); | |
| 67 OffsetTabMatcher matcher((PageEquality(GURL(kExampleUrl)))); | |
| 68 matcher.Check(tab.get()); | |
| 69 VerifyMiss(matcher); | |
| 70 } | |
| 71 | |
| 72 TEST(OffsetTabMatcherTest, HasMatchForward) { | |
| 73 scoped_ptr<SessionTab> tab = Tab(0); | |
| 74 tab->navigations.push_back(Entry(kDifferentUrl)); | |
| 75 tab->navigations.push_back(Entry(kExampleUrl)); | |
| 76 | |
| 77 OffsetTabMatcher matcher((PageEquality(GURL(kExampleUrl)))); | |
| 78 matcher.Check(tab.get()); | |
| 79 VerifyMatch(matcher, 1); | |
| 80 } | |
| 81 | |
| 82 TEST(OffsetTabMatcherTest, HasMatchBackward) { | |
| 83 scoped_ptr<SessionTab> tab = Tab(1); | |
| 84 tab->navigations.push_back(Entry(kExampleUrl)); | |
| 85 tab->navigations.push_back(Entry(kDifferentUrl)); | |
| 86 | |
| 87 OffsetTabMatcher matcher((PageEquality(GURL(kExampleUrl)))); | |
| 88 matcher.Check(tab.get()); | |
| 89 VerifyMatch(matcher, -1); | |
| 90 } | |
| 91 | |
| 92 TEST(OffsetTabMatcherTest, NoMatch) { | |
| 93 scoped_ptr<SessionTab> tab = Tab(0); | |
| 94 tab->navigations.push_back(Entry(kExampleUrl)); | |
| 95 tab->navigations.push_back(Entry(kDifferentUrl)); | |
| 96 | |
| 97 OffsetTabMatcher matcher((PageEquality(GURL(kExampleUrl)))); | |
| 98 matcher.Check(tab.get()); | |
| 99 VerifyMiss(matcher); | |
| 100 } | |
| 101 | |
| 102 TEST(OffsetTabMatcherTest, MultipleBackwardOffsets) { | |
| 103 scoped_ptr<SessionTab> tab = Tab(4); | |
| 104 tab->navigations.push_back(Entry(kExampleUrl)); | |
| 105 tab->navigations.push_back(Entry(kExampleUrl)); | |
| 106 tab->navigations.push_back(Entry(kExampleUrl)); // Expected. | |
| 107 tab->navigations.push_back(Entry(kDifferentUrl)); | |
| 108 tab->navigations.push_back(Entry(kExampleUrl)); // Current. | |
| 109 | |
| 110 OffsetTabMatcher matcher((PageEquality(GURL(kExampleUrl)))); | |
| 111 matcher.Check(tab.get()); | |
| 112 VerifyMatch(matcher, -2); | |
| 113 } | |
| 114 | |
| 115 TEST(OffsetTabMatcherTest, MultipleOffsets) { | |
| 116 scoped_ptr<SessionTab> tab = Tab(1); | |
| 117 tab->navigations.push_back(Entry(kExampleUrl)); | |
| 118 tab->navigations.push_back(Entry(kExampleUrl)); // Current. | |
| 119 tab->navigations.push_back(Entry(kExampleUrl)); | |
| 120 tab->navigations.push_back(Entry(kExampleUrl)); // Expected. | |
| 121 tab->navigations.push_back(Entry(kDifferentUrl)); | |
| 122 | |
| 123 OffsetTabMatcher matcher((PageEquality(GURL(kExampleUrl)))); | |
| 124 matcher.Check(tab.get()); | |
| 125 VerifyMatch(matcher, 2); | |
| 126 } | |
| 127 | |
| 128 TEST(OffsetTabMatcherTest, VeryForwardOffset) { | |
| 129 scoped_ptr<SessionTab> tab = Tab(0); | |
| 130 for (int i = 0; i < 20; i++) { | |
| 131 tab->navigations.push_back(Entry(kDifferentUrl)); | |
| 132 } | |
| 133 tab->navigations.push_back(Entry(kExampleUrl)); | |
| 134 | |
| 135 OffsetTabMatcher matcher((PageEquality(GURL(kExampleUrl)))); | |
| 136 matcher.Check(tab.get()); | |
| 137 // Expect the offset to be clamped to +10. | |
| 138 VerifyMatch(matcher, 10); | |
| 139 } | |
| 140 | |
| 141 TEST(OffsetTabMatcherTest, VeryBackwardOffset) { | |
| 142 scoped_ptr<SessionTab> tab = Tab(20); | |
| 143 tab->navigations.push_back(Entry(kExampleUrl)); | |
| 144 for (int i = 0; i < 20; i++) { | |
| 145 tab->navigations.push_back(Entry(kDifferentUrl)); | |
| 146 } | |
| 147 | |
| 148 OffsetTabMatcher matcher((PageEquality(GURL(kExampleUrl)))); | |
| 149 matcher.Check(tab.get()); | |
| 150 // Expect the offset to be clamped to -10. | |
| 151 VerifyMatch(matcher, -10); | |
| 152 } | |
| 153 | |
| 154 TEST(OffsetTabMatcherTest, MultipleTabs) { | |
| 155 scoped_ptr<SessionTab> tab1 = Tab(0, base::Time::UnixEpoch()); | |
| 156 tab1->navigations.push_back(Entry(kExampleUrl)); | |
| 157 tab1->navigations.push_back(Entry(kExampleUrl)); | |
| 158 | |
| 159 scoped_ptr<SessionTab> tab2 = Tab(1, base::Time::Now()); | |
| 160 tab2->navigations.push_back(Entry(kExampleUrl)); | |
| 161 tab2->navigations.push_back(Entry(kExampleUrl)); | |
| 162 | |
| 163 OffsetTabMatcher matcher((PageEquality(GURL(kExampleUrl)))); | |
| 164 matcher.Check(tab1.get()); | |
| 165 matcher.Check(tab2.get()); | |
| 166 VerifyMatch(matcher, -1); | |
| 167 } | |
| 168 | |
| 169 TEST(OffsetTabMatcherTest, MultipleTabsSameTime) { | |
| 170 base::Time shared_now = base::Time::Now(); | |
| 171 | |
| 172 scoped_ptr<SessionTab> tab1 = Tab(0, shared_now); | |
| 173 tab1->navigations.push_back(Entry(kExampleUrl)); | |
| 174 tab1->navigations.push_back(Entry(kExampleUrl)); | |
| 175 | |
| 176 scoped_ptr<SessionTab> tab2 = Tab(1, shared_now); | |
| 177 tab2->navigations.push_back(Entry(kExampleUrl)); | |
| 178 tab2->navigations.push_back(Entry(kExampleUrl)); | |
| 179 | |
| 180 OffsetTabMatcher matcher((PageEquality(GURL(kExampleUrl)))); | |
| 181 matcher.Check(tab1.get()); | |
| 182 matcher.Check(tab2.get()); | |
| 183 VerifyMatch(matcher, 1); | |
| 184 } | |
| 185 | |
| 186 } // namespace sync_driver | |
| OLD | NEW |