OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/engagement/site_engagement_service.h" | 5 #include "chrome/browser/engagement/site_engagement_service.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/files/scoped_temp_dir.h" | 9 #include "base/files/scoped_temp_dir.h" |
10 #include "base/macros.h" | 10 #include "base/macros.h" |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
90 std::unique_ptr<KeyedService> BuildTestHistoryService( | 90 std::unique_ptr<KeyedService> BuildTestHistoryService( |
91 content::BrowserContext* context) { | 91 content::BrowserContext* context) { |
92 std::unique_ptr<history::HistoryService> service( | 92 std::unique_ptr<history::HistoryService> service( |
93 new history::HistoryService()); | 93 new history::HistoryService()); |
94 service->Init(history::TestHistoryDatabaseParamsForPath(g_temp_history_dir)); | 94 service->Init(history::TestHistoryDatabaseParamsForPath(g_temp_history_dir)); |
95 return std::move(service); | 95 return std::move(service); |
96 } | 96 } |
97 | 97 |
98 } // namespace | 98 } // namespace |
99 | 99 |
| 100 class ObserverTester : public SiteEngagementObserver { |
| 101 public: |
| 102 ObserverTester(SiteEngagementService* service, |
| 103 content::WebContents* web_contents, |
| 104 const GURL& url, |
| 105 double score) |
| 106 : SiteEngagementObserver(service), |
| 107 web_contents_(web_contents), |
| 108 url_(url), |
| 109 score_(score), |
| 110 callback_called_(false), |
| 111 run_loop_() {} |
| 112 |
| 113 void OnEngagementIncreased(content::WebContents* web_contents, |
| 114 const GURL& url, |
| 115 double score) override { |
| 116 EXPECT_EQ(web_contents_, web_contents); |
| 117 EXPECT_EQ(url_, url); |
| 118 EXPECT_DOUBLE_EQ(score_, score); |
| 119 set_callback_called(true); |
| 120 run_loop_.Quit(); |
| 121 } |
| 122 |
| 123 void Wait() { run_loop_.Run(); } |
| 124 |
| 125 bool callback_called() { return callback_called_; } |
| 126 void set_callback_called(bool callback_called) { |
| 127 callback_called_ = callback_called; |
| 128 } |
| 129 |
| 130 private: |
| 131 content::WebContents* web_contents_; |
| 132 GURL url_; |
| 133 double score_; |
| 134 bool callback_called_; |
| 135 base::RunLoop run_loop_; |
| 136 |
| 137 DISALLOW_COPY_AND_ASSIGN(ObserverTester); |
| 138 }; |
| 139 |
100 class SiteEngagementServiceTest : public ChromeRenderViewHostTestHarness { | 140 class SiteEngagementServiceTest : public ChromeRenderViewHostTestHarness { |
101 public: | 141 public: |
102 void SetUp() override { | 142 void SetUp() override { |
103 ChromeRenderViewHostTestHarness::SetUp(); | 143 ChromeRenderViewHostTestHarness::SetUp(); |
104 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 144 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
105 g_temp_history_dir = temp_dir_.path(); | 145 g_temp_history_dir = temp_dir_.path(); |
106 HistoryServiceFactory::GetInstance()->SetTestingFactory( | 146 HistoryServiceFactory::GetInstance()->SetTestingFactory( |
107 profile(), &BuildTestHistoryService); | 147 profile(), &BuildTestHistoryService); |
108 SiteEngagementScore::SetParamValuesForTesting(); | 148 SiteEngagementScore::SetParamValuesForTesting(); |
109 } | 149 } |
(...skipping 899 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1009 EXPECT_TRUE(service->IsEngagementAtLeast( | 1049 EXPECT_TRUE(service->IsEngagementAtLeast( |
1010 url2, SiteEngagementService::ENGAGEMENT_LEVEL_LOW)); | 1050 url2, SiteEngagementService::ENGAGEMENT_LEVEL_LOW)); |
1011 EXPECT_TRUE(service->IsEngagementAtLeast( | 1051 EXPECT_TRUE(service->IsEngagementAtLeast( |
1012 url2, SiteEngagementService::ENGAGEMENT_LEVEL_MEDIUM)); | 1052 url2, SiteEngagementService::ENGAGEMENT_LEVEL_MEDIUM)); |
1013 EXPECT_TRUE(service->IsEngagementAtLeast( | 1053 EXPECT_TRUE(service->IsEngagementAtLeast( |
1014 url2, SiteEngagementService::ENGAGEMENT_LEVEL_HIGH)); | 1054 url2, SiteEngagementService::ENGAGEMENT_LEVEL_HIGH)); |
1015 EXPECT_TRUE(service->IsEngagementAtLeast( | 1055 EXPECT_TRUE(service->IsEngagementAtLeast( |
1016 url2, SiteEngagementService::ENGAGEMENT_LEVEL_MAX)); | 1056 url2, SiteEngagementService::ENGAGEMENT_LEVEL_MAX)); |
1017 } | 1057 } |
1018 | 1058 |
| 1059 TEST_F(SiteEngagementServiceTest, Observers) { |
| 1060 SiteEngagementService* service = SiteEngagementService::Get(profile()); |
| 1061 |
| 1062 GURL url_score_1("http://www.google.com/maps"); |
| 1063 GURL url_score_2("http://www.google.com/drive"); |
| 1064 GURL url_score_3("http://www.google.com/"); |
| 1065 GURL url_not_called("https://www.google.com/"); |
| 1066 |
| 1067 // Create an observer and Observe(nullptr). |
| 1068 ObserverTester tester_not_called(service, web_contents(), url_not_called, 1); |
| 1069 tester_not_called.Observe(nullptr); |
| 1070 |
| 1071 { |
| 1072 // Create an observer for navigation. |
| 1073 ObserverTester tester(service, web_contents(), url_score_1, 0.5); |
| 1074 NavigateAndCommit(url_score_1); |
| 1075 service->HandleNavigation(web_contents(), ui::PAGE_TRANSITION_TYPED); |
| 1076 tester.Wait(); |
| 1077 EXPECT_TRUE(tester.callback_called()); |
| 1078 EXPECT_FALSE(tester_not_called.callback_called()); |
| 1079 tester.Observe(nullptr); |
| 1080 } |
| 1081 |
| 1082 { |
| 1083 // Update observer for a user input. |
| 1084 ObserverTester tester(service, web_contents(), url_score_2, 0.55); |
| 1085 NavigateAndCommit(url_score_2); |
| 1086 service->HandleUserInput(web_contents(), |
| 1087 SiteEngagementMetrics::ENGAGEMENT_MOUSE); |
| 1088 tester.Wait(); |
| 1089 EXPECT_TRUE(tester.callback_called()); |
| 1090 EXPECT_FALSE(tester_not_called.callback_called()); |
| 1091 tester.Observe(nullptr); |
| 1092 } |
| 1093 |
| 1094 // Add two observers for media playing in the foreground. |
| 1095 { |
| 1096 ObserverTester tester_1(service, web_contents(), url_score_3, 0.57); |
| 1097 ObserverTester tester_2(service, web_contents(), url_score_3, 0.57); |
| 1098 NavigateAndCommit(url_score_3); |
| 1099 service->HandleMediaPlaying(web_contents(), false); |
| 1100 tester_1.Wait(); |
| 1101 tester_2.Wait(); |
| 1102 |
| 1103 EXPECT_TRUE(tester_1.callback_called()); |
| 1104 EXPECT_TRUE(tester_2.callback_called()); |
| 1105 EXPECT_FALSE(tester_not_called.callback_called()); |
| 1106 tester_1.Observe(nullptr); |
| 1107 tester_2.Observe(nullptr); |
| 1108 } |
| 1109 |
| 1110 // Add an observer for media playing in the background. |
| 1111 { |
| 1112 ObserverTester tester(service, web_contents(), url_score_3, 0.58); |
| 1113 service->HandleMediaPlaying(web_contents(), true); |
| 1114 tester.Wait(); |
| 1115 |
| 1116 EXPECT_TRUE(tester.callback_called()); |
| 1117 EXPECT_FALSE(tester_not_called.callback_called()); |
| 1118 tester.Observe(nullptr); |
| 1119 } |
| 1120 } |
| 1121 |
1019 TEST_F(SiteEngagementServiceTest, ScoreDecayHistograms) { | 1122 TEST_F(SiteEngagementServiceTest, ScoreDecayHistograms) { |
1020 base::SimpleTestClock* clock = new base::SimpleTestClock(); | 1123 base::SimpleTestClock* clock = new base::SimpleTestClock(); |
1021 std::unique_ptr<SiteEngagementService> service( | 1124 std::unique_ptr<SiteEngagementService> service( |
1022 new SiteEngagementService(profile(), base::WrapUnique(clock))); | 1125 new SiteEngagementService(profile(), base::WrapUnique(clock))); |
1023 | 1126 |
1024 base::Time current_day = GetReferenceTime(); | 1127 base::Time current_day = GetReferenceTime(); |
1025 clock->SetNow(current_day); | 1128 clock->SetNow(current_day); |
1026 base::HistogramTester histograms; | 1129 base::HistogramTester histograms; |
1027 GURL origin1("http://www.google.com/"); | 1130 GURL origin1("http://www.google.com/"); |
1028 GURL origin2("http://drive.google.com/"); | 1131 GURL origin2("http://drive.google.com/"); |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1098 0, 2); | 1201 0, 2); |
1099 | 1202 |
1100 // Add more points and ensure no more samples are present. | 1203 // Add more points and ensure no more samples are present. |
1101 service->AddPoints(origin1, 0.01); | 1204 service->AddPoints(origin1, 0.01); |
1102 service->AddPoints(origin2, 0.01); | 1205 service->AddPoints(origin2, 0.01); |
1103 histograms.ExpectTotalCount(SiteEngagementMetrics::kScoreDecayedFromHistogram, | 1206 histograms.ExpectTotalCount(SiteEngagementMetrics::kScoreDecayedFromHistogram, |
1104 4); | 1207 4); |
1105 histograms.ExpectTotalCount(SiteEngagementMetrics::kScoreDecayedToHistogram, | 1208 histograms.ExpectTotalCount(SiteEngagementMetrics::kScoreDecayedToHistogram, |
1106 4); | 1209 4); |
1107 } | 1210 } |
OLD | NEW |