| 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/current_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, const base::Time timestamp) { | |
| 32 scoped_ptr<SessionTab> tab(new SessionTab()); | |
| 33 tab->current_navigation_index = index; | |
| 34 tab->timestamp = timestamp; | |
| 35 return tab; | |
| 36 } | |
| 37 | |
| 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); | |
| 44 } | |
| 45 | |
| 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); | |
| 51 } | |
| 52 | |
| 53 } // namespace | |
| 54 | |
| 55 TEST(CurrentTabMatcherTest, NoCheck) { | |
| 56 CurrentTabMatcher matcher((PageEquality(GURL(kExampleUrl)))); | |
| 57 VerifyMiss(matcher); | |
| 58 } | |
| 59 | |
| 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); | |
| 65 } | |
| 66 | |
| 67 TEST(CurrentTabMatcherTest, SameUrl) { | |
| 68 scoped_ptr<SessionTab> tab = Tab(0, base::Time::Now()); | |
| 69 tab->navigations.push_back(Entry(kExampleUrl)); | |
| 70 | |
| 71 CurrentTabMatcher matcher((PageEquality(GURL(kExampleUrl)))); | |
| 72 matcher.Check(tab.get()); | |
| 73 VerifyMatch(matcher); | |
| 74 } | |
| 75 | |
| 76 TEST(CurrentTabMatcherTest, DifferentUrl) { | |
| 77 scoped_ptr<SessionTab> tab = Tab(0, base::Time::Now()); | |
| 78 tab->navigations.push_back(Entry(kDifferentUrl)); | |
| 79 | |
| 80 CurrentTabMatcher matcher((PageEquality(GURL(kExampleUrl)))); | |
| 81 matcher.Check(tab.get()); | |
| 82 VerifyMiss(matcher); | |
| 83 } | |
| 84 | |
| 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)); | |
| 89 | |
| 90 CurrentTabMatcher matcher((PageEquality(GURL(kExampleUrl)))); | |
| 91 matcher.Check(tab.get()); | |
| 92 VerifyMiss(matcher); | |
| 93 } | |
| 94 | |
| 95 TEST(CurrentTabMatcherTest, Timestamp) { | |
| 96 scoped_ptr<SessionTab> tab1 = Tab(0, base::Time::UnixEpoch()); | |
| 97 tab1->navigations.push_back(Entry(kExampleUrl)); | |
| 98 | |
| 99 scoped_ptr<SessionTab> tab2 = Tab(0, base::Time::Now()); | |
| 100 tab2->navigations.push_back(Entry(kExampleUrl)); | |
| 101 | |
| 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_); | |
| 106 | |
| 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_); | |
| 112 } | |
| 113 | |
| 114 } // namespace sync_driver | |
| OLD | NEW |