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.
5 #include "components/sync_driver/revisit/offset_tab_matcher.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "base/test/histogram_tester.h"
9 #include "base/time/time.h"
10 #include "components/sessions/serialized_navigation_entry.h"
11 #include "components/sessions/serialized_navigation_entry_test_helper.h"
12 #include "components/sessions/session_types.h"
13 #include "components/sync_driver/revisit/page_equality.h"
14 #include "testing/gtest/include/gtest/gtest.h"
17 using sessions::SessionTab
;
19 namespace sync_driver
{
23 static const std::string kExampleUrl
= "http://www.example.com";
24 static const std::string kDifferentUrl
= "http://www.different.com";
26 sessions::SerializedNavigationEntry
Entry(const std::string
& url
) {
27 return sessions::SerializedNavigationEntryTestHelper::CreateNavigation(url
,
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
;
39 void VerifyMatch(OffsetTabMatcher
& matcher
, const int offset
) {
40 base::HistogramTester histogram_tester
;
41 matcher
.Emit(PageVisitObserver::kTransitionPage
);
42 histogram_tester
.ExpectUniqueSample("Sync.PageRevisitNavigationMatchOffset",
44 histogram_tester
.ExpectUniqueSample(
45 "Sync.PageRevisitNavigationMatchTransition",
46 PageVisitObserver::kTransitionPage
, 1);
47 histogram_tester
.ExpectTotalCount("Sync.PageRevisitNavigationMatchAge", 1);
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);
60 TEST(OffsetTabMatcherTest
, NoCheck
) {
61 OffsetTabMatcher
matcher((PageEquality(GURL(kExampleUrl
))));
65 TEST(OffsetTabMatcherTest
, EmptyTab
) {
66 scoped_ptr
<SessionTab
> tab
= Tab(0);
67 OffsetTabMatcher
matcher((PageEquality(GURL(kExampleUrl
))));
68 matcher
.Check(tab
.get());
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
));
77 OffsetTabMatcher
matcher((PageEquality(GURL(kExampleUrl
))));
78 matcher
.Check(tab
.get());
79 VerifyMatch(matcher
, 1);
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
));
87 OffsetTabMatcher
matcher((PageEquality(GURL(kExampleUrl
))));
88 matcher
.Check(tab
.get());
89 VerifyMatch(matcher
, -1);
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
));
97 OffsetTabMatcher
matcher((PageEquality(GURL(kExampleUrl
))));
98 matcher
.Check(tab
.get());
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.
110 OffsetTabMatcher
matcher((PageEquality(GURL(kExampleUrl
))));
111 matcher
.Check(tab
.get());
112 VerifyMatch(matcher
, -2);
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
));
123 OffsetTabMatcher
matcher((PageEquality(GURL(kExampleUrl
))));
124 matcher
.Check(tab
.get());
125 VerifyMatch(matcher
, 2);
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
));
133 tab
->navigations
.push_back(Entry(kExampleUrl
));
135 OffsetTabMatcher
matcher((PageEquality(GURL(kExampleUrl
))));
136 matcher
.Check(tab
.get());
137 // Expect the offset to be clamped to +10.
138 VerifyMatch(matcher
, 10);
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
));
148 OffsetTabMatcher
matcher((PageEquality(GURL(kExampleUrl
))));
149 matcher
.Check(tab
.get());
150 // Expect the offset to be clamped to -10.
151 VerifyMatch(matcher
, -10);
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
));
159 scoped_ptr
<SessionTab
> tab2
= Tab(1, base::Time::Now());
160 tab2
->navigations
.push_back(Entry(kExampleUrl
));
161 tab2
->navigations
.push_back(Entry(kExampleUrl
));
163 OffsetTabMatcher
matcher((PageEquality(GURL(kExampleUrl
))));
164 matcher
.Check(tab1
.get());
165 matcher
.Check(tab2
.get());
166 VerifyMatch(matcher
, -1);
169 TEST(OffsetTabMatcherTest
, MultipleTabsSameTime
) {
170 base::Time shared_now
= base::Time::Now();
172 scoped_ptr
<SessionTab
> tab1
= Tab(0, shared_now
);
173 tab1
->navigations
.push_back(Entry(kExampleUrl
));
174 tab1
->navigations
.push_back(Entry(kExampleUrl
));
176 scoped_ptr
<SessionTab
> tab2
= Tab(1, shared_now
);
177 tab2
->navigations
.push_back(Entry(kExampleUrl
));
178 tab2
->navigations
.push_back(Entry(kExampleUrl
));
180 OffsetTabMatcher
matcher((PageEquality(GURL(kExampleUrl
))));
181 matcher
.Check(tab1
.get());
182 matcher
.Check(tab2
.get());
183 VerifyMatch(matcher
, 1);
186 } // namespace sync_driver