Roll src/third_party/WebKit f298044:aa8346d (svn 202628:202629)
[chromium-blink-merge.git] / components / sync_driver / revisit / current_tab_matcher_unittest.cc
blob8c245d978494db9d1772424988a30196f3f9d7fa
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/current_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"
15 #include "url/gurl.h"
17 using sessions::SessionTab;
19 namespace sync_driver {
21 namespace {
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,
28 "");
31 scoped_ptr<SessionTab> Tab(const int index, const base::Time timestamp) {
32 scoped_ptr<SessionTab> tab(new SessionTab());
33 tab->current_navigation_index = index;
34 tab->timestamp = timestamp;
35 return tab;
38 void VerifyMatch(CurrentTabMatcher& matcher) {
39 base::HistogramTester histogram_tester;
40 matcher.Emit(PageVisitObserver::kTransitionPage);
41 histogram_tester.ExpectUniqueSample("Sync.PageRevisitTabMatchTransition",
42 PageVisitObserver::kTransitionPage, 1);
43 histogram_tester.ExpectTotalCount("Sync.PageRevisitTabMatchAge", 1);
46 void VerifyMiss(CurrentTabMatcher& matcher) {
47 base::HistogramTester histogram_tester;
48 matcher.Emit(PageVisitObserver::kTransitionPage);
49 histogram_tester.ExpectUniqueSample("Sync.PageRevisitTabMissTransition",
50 PageVisitObserver::kTransitionPage, 1);
53 } // namespace
55 TEST(CurrentTabMatcherTest, NoCheck) {
56 CurrentTabMatcher matcher((PageEquality(GURL(kExampleUrl))));
57 VerifyMiss(matcher);
60 TEST(CurrentTabMatcherTest, EmptyTab) {
61 scoped_ptr<SessionTab> tab = Tab(0, base::Time::Now());
62 CurrentTabMatcher matcher((PageEquality(GURL(kExampleUrl))));
63 matcher.Check(tab.get());
64 VerifyMiss(matcher);
67 TEST(CurrentTabMatcherTest, SameUrl) {
68 scoped_ptr<SessionTab> tab = Tab(0, base::Time::Now());
69 tab->navigations.push_back(Entry(kExampleUrl));
71 CurrentTabMatcher matcher((PageEquality(GURL(kExampleUrl))));
72 matcher.Check(tab.get());
73 VerifyMatch(matcher);
76 TEST(CurrentTabMatcherTest, DifferentUrl) {
77 scoped_ptr<SessionTab> tab = Tab(0, base::Time::Now());
78 tab->navigations.push_back(Entry(kDifferentUrl));
80 CurrentTabMatcher matcher((PageEquality(GURL(kExampleUrl))));
81 matcher.Check(tab.get());
82 VerifyMiss(matcher);
85 TEST(CurrentTabMatcherTest, DifferentIndex) {
86 scoped_ptr<SessionTab> tab = Tab(0, base::Time::Now());
87 tab->navigations.push_back(Entry(kDifferentUrl));
88 tab->navigations.push_back(Entry(kExampleUrl));
90 CurrentTabMatcher matcher((PageEquality(GURL(kExampleUrl))));
91 matcher.Check(tab.get());
92 VerifyMiss(matcher);
95 TEST(CurrentTabMatcherTest, Timestamp) {
96 scoped_ptr<SessionTab> tab1 = Tab(0, base::Time::UnixEpoch());
97 tab1->navigations.push_back(Entry(kExampleUrl));
99 scoped_ptr<SessionTab> tab2 = Tab(0, base::Time::Now());
100 tab2->navigations.push_back(Entry(kExampleUrl));
102 CurrentTabMatcher matcher1((PageEquality(GURL(kExampleUrl))));
103 matcher1.Check(tab1.get());
104 matcher1.Check(tab2.get());
105 ASSERT_EQ(tab2.get(), matcher1.most_recent_match_);
107 // Now repeat the same test but check the tabs in the opposite order.
108 CurrentTabMatcher matcher2((PageEquality(GURL(kExampleUrl))));
109 matcher2.Check(tab2.get());
110 matcher2.Check(tab1.get());
111 ASSERT_EQ(tab2.get(), matcher2.most_recent_match_);
114 } // namespace sync_driver