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/sessions_page_revisit_observer.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "base/test/histogram_tester.h"
9 #include "components/sessions/serialized_navigation_entry.h"
10 #include "components/sessions/serialized_navigation_entry_test_helper.h"
11 #include "components/sessions/session_types.h"
12 #include "components/sync_driver/glue/synced_session.h"
13 #include "components/sync_driver/revisit/page_visit_observer.h"
14 #include "testing/gtest/include/gtest/gtest.h"
17 using sessions::SessionTab
;
18 using sessions::SessionWindow
;
20 namespace sync_driver
{
24 static const std::string kExampleUrl
= "http://www.example.com";
25 static const std::string kDifferentUrl
= "http://www.different.com";
27 class TestForeignSessionsProvider
: public ForeignSessionsProvider
{
29 TestForeignSessionsProvider(const std::vector
<const SyncedSession
*>& sessions
,
31 : sessions_(sessions
), return_value_(return_value
) {}
32 ~TestForeignSessionsProvider() override
{};
34 bool GetAllForeignSessions(
35 std::vector
<const SyncedSession
*>* sessions
) override
{
37 *sessions
= sessions_
;
42 const std::vector
<const SyncedSession
*>& sessions_
;
43 const bool return_value_
;
48 class SessionsPageRevisitObserverTest
: public ::testing::Test
{
50 void CheckAndExpect(SessionsPageRevisitObserver
& observer
,
52 const bool current_match
,
53 const bool offset_match
) {
54 base::HistogramTester histogram_tester
;
55 observer
.CheckForRevisit(url
, PageVisitObserver::kTransitionPage
);
57 histogram_tester
.ExpectTotalCount("Sync.PageRevisitTabMatchTransition",
58 current_match
? 1 : 0);
59 histogram_tester
.ExpectTotalCount("Sync.PageRevisitTabMissTransition",
60 current_match
? 0 : 1);
61 histogram_tester
.ExpectTotalCount(
62 "Sync.PageRevisitNavigationMatchTransition", offset_match
? 1 : 0);
63 histogram_tester
.ExpectTotalCount(
64 "Sync.PageRevisitNavigationMissTransition", offset_match
? 0 : 1);
65 histogram_tester
.ExpectTotalCount("Sync.PageRevisitSessionDuration", 1);
68 void CheckAndExpect(const SyncedSession
* session
,
70 const bool current_match
,
71 const bool offset_match
) {
72 std::vector
<const SyncedSession
*> sessions
;
73 sessions
.push_back(session
);
74 SessionsPageRevisitObserver
observer(
75 make_scoped_ptr(new TestForeignSessionsProvider(sessions
, true)));
76 CheckAndExpect(observer
, url
, current_match
, offset_match
);
80 TEST_F(SessionsPageRevisitObserverTest
, RunMatchersNoSessions
) {
81 std::vector
<const SyncedSession
*> sessions
;
82 SessionsPageRevisitObserver
observer(
83 make_scoped_ptr(new TestForeignSessionsProvider(sessions
, true)));
84 CheckAndExpect(observer
, GURL(kExampleUrl
), false, false);
87 TEST_F(SessionsPageRevisitObserverTest
, RunMatchersNoWindows
) {
88 scoped_ptr
<SyncedSession
> session(new SyncedSession());
89 CheckAndExpect(session
.get(), GURL(kExampleUrl
), false, false);
92 TEST_F(SessionsPageRevisitObserverTest
, RunMatchersNoTabs
) {
93 scoped_ptr
<SyncedSession
> session(new SyncedSession());
94 session
->windows
[0] = new SessionWindow();
95 CheckAndExpect(session
.get(), GURL(kExampleUrl
), false, false);
98 TEST_F(SessionsPageRevisitObserverTest
, RunMatchersNoEntries
) {
99 scoped_ptr
<SessionWindow
> window(new SessionWindow());
100 window
->tabs
.push_back(new SessionTab());
101 scoped_ptr
<SyncedSession
> session(new SyncedSession());
102 session
->windows
[0] = window
.release();
103 CheckAndExpect(session
.get(), GURL(kExampleUrl
), false, false);
106 TEST_F(SessionsPageRevisitObserverTest
, RunMatchersSingle
) {
107 scoped_ptr
<SessionTab
> tab(new SessionTab());
108 tab
->navigations
.push_back(
109 sessions::SerializedNavigationEntryTestHelper::CreateNavigation(
111 tab
->current_navigation_index
= 0;
112 scoped_ptr
<SessionWindow
> window(new SessionWindow());
113 window
->tabs
.push_back(tab
.release());
114 scoped_ptr
<SyncedSession
> session(new SyncedSession());
115 session
->windows
[0] = window
.release();
116 CheckAndExpect(session
.get(), GURL(kExampleUrl
), true, false);
119 TEST_F(SessionsPageRevisitObserverTest
, RunMatchersFalseProvider
) {
120 scoped_ptr
<SessionTab
> tab(new SessionTab());
121 tab
->navigations
.push_back(
122 sessions::SerializedNavigationEntryTestHelper::CreateNavigation(
124 tab
->navigations
.push_back(
125 sessions::SerializedNavigationEntryTestHelper::CreateNavigation(
127 tab
->current_navigation_index
= 1;
128 scoped_ptr
<SessionWindow
> window(new SessionWindow());
129 window
->tabs
.push_back(tab
.release());
130 scoped_ptr
<SyncedSession
> session(new SyncedSession());
131 session
->windows
[0] = window
.release();
133 // The provider returns false when asked for foreign sessions, even though
134 // it has has a valid tab.
135 std::vector
<const SyncedSession
*> sessions
;
136 sessions
.push_back(session
.get());
137 SessionsPageRevisitObserver
observer(
138 make_scoped_ptr(new TestForeignSessionsProvider(sessions
, false)));
139 CheckAndExpect(observer
, GURL(kExampleUrl
), false, false);
142 TEST_F(SessionsPageRevisitObserverTest
, RunMatchersMany
) {
143 scoped_ptr
<SessionTab
> tab1(new SessionTab());
144 tab1
->navigations
.push_back(
145 sessions::SerializedNavigationEntryTestHelper::CreateNavigation(
147 tab1
->current_navigation_index
= 0;
149 scoped_ptr
<SessionTab
> tab2(new SessionTab());
150 tab2
->navigations
.push_back(
151 sessions::SerializedNavigationEntryTestHelper::CreateNavigation(
153 tab2
->current_navigation_index
= 0;
155 scoped_ptr
<SessionTab
> tab3(new SessionTab());
156 tab3
->navigations
.push_back(
157 sessions::SerializedNavigationEntryTestHelper::CreateNavigation(
159 tab3
->current_navigation_index
= 0;
161 scoped_ptr
<SessionTab
> tab4(new SessionTab());
162 tab4
->navigations
.push_back(
163 sessions::SerializedNavigationEntryTestHelper::CreateNavigation(
165 tab4
->navigations
.push_back(
166 sessions::SerializedNavigationEntryTestHelper::CreateNavigation(
168 tab4
->current_navigation_index
= 1;
170 scoped_ptr
<SessionWindow
> window1(new SessionWindow());
171 window1
->tabs
.push_back(tab1
.release());
172 scoped_ptr
<SessionWindow
> window2(new SessionWindow());
173 window2
->tabs
.push_back(tab2
.release());
174 scoped_ptr
<SessionWindow
> window3(new SessionWindow());
175 window3
->tabs
.push_back(tab3
.release());
176 window3
->tabs
.push_back(tab4
.release());
178 scoped_ptr
<SyncedSession
> session1(new SyncedSession());
179 session1
->windows
[1] = window1
.release();
180 scoped_ptr
<SyncedSession
> session2(new SyncedSession());
181 session2
->windows
[2] = window2
.release();
182 session2
->windows
[3] = window3
.release();
184 std::vector
<const SyncedSession
*> sessions
;
185 sessions
.push_back(session1
.get());
186 sessions
.push_back(session2
.get());
187 SessionsPageRevisitObserver
observer(
188 make_scoped_ptr(new TestForeignSessionsProvider(sessions
, true)));
190 base::HistogramTester histogram_tester
;
191 CheckAndExpect(observer
, GURL(kExampleUrl
), true, true);
194 } // namespace sync_driver