| 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 11 matching lines...) Expand all Loading... |
| 22 #include "chrome/common/chrome_switches.h" | 22 #include "chrome/common/chrome_switches.h" |
| 23 #include "chrome/common/pref_names.h" | 23 #include "chrome/common/pref_names.h" |
| 24 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | 24 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
| 25 #include "chrome/test/base/testing_profile.h" | 25 #include "chrome/test/base/testing_profile.h" |
| 26 #include "components/content_settings/core/browser/content_settings_observer.h" | 26 #include "components/content_settings/core/browser/content_settings_observer.h" |
| 27 #include "components/content_settings/core/browser/host_content_settings_map.h" | 27 #include "components/content_settings/core/browser/host_content_settings_map.h" |
| 28 #include "components/history/core/browser/history_database_params.h" | 28 #include "components/history/core/browser/history_database_params.h" |
| 29 #include "components/history/core/browser/history_service.h" | 29 #include "components/history/core/browser/history_service.h" |
| 30 #include "components/history/core/test/test_history_database.h" | 30 #include "components/history/core/test/test_history_database.h" |
| 31 #include "components/prefs/pref_service.h" | 31 #include "components/prefs/pref_service.h" |
| 32 #include "content/public/browser/browser_thread.h" |
| 32 #include "content/public/browser/navigation_entry.h" | 33 #include "content/public/browser/navigation_entry.h" |
| 33 #include "content/public/browser/page_navigator.h" | 34 #include "content/public/browser/page_navigator.h" |
| 34 #include "content/public/browser/web_contents.h" | 35 #include "content/public/browser/web_contents.h" |
| 36 #include "content/public/test/test_browser_thread_bundle.h" |
| 35 #include "content/public/test/web_contents_tester.h" | 37 #include "content/public/test/web_contents_tester.h" |
| 36 #include "testing/gtest/include/gtest/gtest.h" | 38 #include "testing/gtest/include/gtest/gtest.h" |
| 37 | 39 |
| 38 namespace { | 40 namespace { |
| 39 | 41 |
| 40 base::FilePath g_temp_history_dir; | 42 base::FilePath g_temp_history_dir; |
| 41 | 43 |
| 42 const int kMoreAccumulationsThanNeededToMaxDailyEngagement = 40; | 44 const int kMoreAccumulationsThanNeededToMaxDailyEngagement = 40; |
| 43 const int kMoreDaysThanNeededToMaxTotalEngagement = 40; | 45 const int kMoreDaysThanNeededToMaxTotalEngagement = 40; |
| 44 const int kMorePeriodsThanNeededToDecayMaxScore = 40; | 46 const int kMorePeriodsThanNeededToDecayMaxScore = 40; |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 137 GURL url_; | 139 GURL url_; |
| 138 double score_; | 140 double score_; |
| 139 bool callback_called_; | 141 bool callback_called_; |
| 140 base::RunLoop run_loop_; | 142 base::RunLoop run_loop_; |
| 141 | 143 |
| 142 DISALLOW_COPY_AND_ASSIGN(ObserverTester); | 144 DISALLOW_COPY_AND_ASSIGN(ObserverTester); |
| 143 }; | 145 }; |
| 144 | 146 |
| 145 class SiteEngagementServiceTest : public ChromeRenderViewHostTestHarness { | 147 class SiteEngagementServiceTest : public ChromeRenderViewHostTestHarness { |
| 146 public: | 148 public: |
| 149 SiteEngagementServiceTest() : ChromeRenderViewHostTestHarness() { |
| 150 SetThreadBundleOptions(content::TestBrowserThreadBundle::REAL_IO_THREAD | |
| 151 content::TestBrowserThreadBundle::REAL_DB_THREAD); |
| 152 } |
| 153 |
| 147 void SetUp() override { | 154 void SetUp() override { |
| 148 ChromeRenderViewHostTestHarness::SetUp(); | 155 ChromeRenderViewHostTestHarness::SetUp(); |
| 149 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 156 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 150 g_temp_history_dir = temp_dir_.GetPath(); | 157 g_temp_history_dir = temp_dir_.GetPath(); |
| 151 HistoryServiceFactory::GetInstance()->SetTestingFactory( | 158 HistoryServiceFactory::GetInstance()->SetTestingFactory( |
| 152 profile(), &BuildTestHistoryService); | 159 profile(), &BuildTestHistoryService); |
| 153 SiteEngagementScore::SetParamValuesForTesting(); | 160 SiteEngagementScore::SetParamValuesForTesting(); |
| 154 } | 161 } |
| 155 | 162 |
| 156 void NavigateWithTransitionAndExpectHigherScore( | 163 void NavigateWithTransitionAndExpectHigherScore( |
| (...skipping 23 matching lines...) Expand all Loading... |
| 180 } | 187 } |
| 181 | 188 |
| 182 void SetParamValue(SiteEngagementScore::Variation variation, double value) { | 189 void SetParamValue(SiteEngagementScore::Variation variation, double value) { |
| 183 SiteEngagementScore::GetParamValues()[variation].second = value; | 190 SiteEngagementScore::GetParamValues()[variation].second = value; |
| 184 } | 191 } |
| 185 | 192 |
| 186 void AssertInRange(double expected, double actual) { | 193 void AssertInRange(double expected, double actual) { |
| 187 EXPECT_NEAR(expected, actual, kMaxRoundingDeviation); | 194 EXPECT_NEAR(expected, actual, kMaxRoundingDeviation); |
| 188 } | 195 } |
| 189 | 196 |
| 197 double CheckScoreFromSettingsOnThread( |
| 198 content::BrowserThread::ID thread_id, |
| 199 HostContentSettingsMap* settings_map, |
| 200 const GURL& url) { |
| 201 double score = 0; |
| 202 base::RunLoop run_loop; |
| 203 content::BrowserThread::PostTaskAndReply( |
| 204 thread_id, FROM_HERE, |
| 205 base::Bind(&SiteEngagementServiceTest::CheckScoreFromSettings, |
| 206 base::Unretained(this), settings_map, url, &score), |
| 207 run_loop.QuitClosure()); |
| 208 run_loop.Run(); |
| 209 return score; |
| 210 } |
| 211 |
| 190 private: | 212 private: |
| 213 void CheckScoreFromSettings(HostContentSettingsMap* settings_map, |
| 214 const GURL& url, |
| 215 double *score) { |
| 216 *score = SiteEngagementService::GetScoreFromSettings(settings_map, url); |
| 217 } |
| 218 |
| 191 base::ScopedTempDir temp_dir_; | 219 base::ScopedTempDir temp_dir_; |
| 192 }; | 220 }; |
| 193 | 221 |
| 194 TEST_F(SiteEngagementServiceTest, GetMedianEngagement) { | 222 TEST_F(SiteEngagementServiceTest, GetMedianEngagement) { |
| 195 SiteEngagementService* service = SiteEngagementService::Get(profile()); | 223 SiteEngagementService* service = SiteEngagementService::Get(profile()); |
| 196 ASSERT_TRUE(service); | 224 ASSERT_TRUE(service); |
| 197 | 225 |
| 198 GURL url1("http://www.google.com/"); | 226 GURL url1("http://www.google.com/"); |
| 199 GURL url2("https://www.google.com/"); | 227 GURL url2("https://www.google.com/"); |
| 200 GURL url3("https://drive.google.com/"); | 228 GURL url3("https://drive.google.com/"); |
| (...skipping 1306 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1507 | 1535 |
| 1508 service->AddPoints(url3, 2); | 1536 service->AddPoints(url3, 2); |
| 1509 EXPECT_EQ(1, incognito_service->GetScore(url3)); | 1537 EXPECT_EQ(1, incognito_service->GetScore(url3)); |
| 1510 EXPECT_EQ(2, service->GetScore(url3)); | 1538 EXPECT_EQ(2, service->GetScore(url3)); |
| 1511 | 1539 |
| 1512 EXPECT_EQ(0, incognito_service->GetScore(url4)); | 1540 EXPECT_EQ(0, incognito_service->GetScore(url4)); |
| 1513 service->AddPoints(url4, 2); | 1541 service->AddPoints(url4, 2); |
| 1514 EXPECT_EQ(2, incognito_service->GetScore(url4)); | 1542 EXPECT_EQ(2, incognito_service->GetScore(url4)); |
| 1515 EXPECT_EQ(2, service->GetScore(url4)); | 1543 EXPECT_EQ(2, service->GetScore(url4)); |
| 1516 } | 1544 } |
| 1545 |
| 1546 TEST_F(SiteEngagementServiceTest, GetScoreFromSettings) { |
| 1547 GURL url1("http://www.google.com/"); |
| 1548 GURL url2("https://www.google.com/"); |
| 1549 |
| 1550 HostContentSettingsMap* settings_map = |
| 1551 HostContentSettingsMapFactory::GetForProfile(profile()); |
| 1552 HostContentSettingsMap* incognito_settings_map = |
| 1553 HostContentSettingsMapFactory::GetForProfile( |
| 1554 profile()->GetOffTheRecordProfile()); |
| 1555 |
| 1556 // All scores are 0 to start. |
| 1557 EXPECT_EQ(0, CheckScoreFromSettingsOnThread(content::BrowserThread::IO, |
| 1558 settings_map, url1)); |
| 1559 EXPECT_EQ(0, CheckScoreFromSettingsOnThread(content::BrowserThread::FILE, |
| 1560 settings_map, url2)); |
| 1561 EXPECT_EQ(0, CheckScoreFromSettingsOnThread(content::BrowserThread::FILE, |
| 1562 incognito_settings_map, url1)); |
| 1563 EXPECT_EQ(0, CheckScoreFromSettingsOnThread(content::BrowserThread::IO, |
| 1564 incognito_settings_map, url2)); |
| 1565 |
| 1566 SiteEngagementService* service = SiteEngagementService::Get(profile()); |
| 1567 ASSERT_TRUE(service); |
| 1568 service->AddPoints(url1, 1); |
| 1569 service->AddPoints(url2, 2); |
| 1570 |
| 1571 EXPECT_EQ(1, CheckScoreFromSettingsOnThread(content::BrowserThread::FILE, |
| 1572 settings_map, url1)); |
| 1573 EXPECT_EQ(2, CheckScoreFromSettingsOnThread(content::BrowserThread::IO, |
| 1574 settings_map, url2)); |
| 1575 EXPECT_EQ(1, CheckScoreFromSettingsOnThread(content::BrowserThread::FILE, |
| 1576 incognito_settings_map, url1)); |
| 1577 EXPECT_EQ(2, CheckScoreFromSettingsOnThread(content::BrowserThread::IO, |
| 1578 incognito_settings_map, url2)); |
| 1579 |
| 1580 SiteEngagementService* incognito_service = |
| 1581 SiteEngagementService::Get(profile()->GetOffTheRecordProfile()); |
| 1582 ASSERT_TRUE(incognito_service); |
| 1583 incognito_service->AddPoints(url1, 3); |
| 1584 incognito_service->AddPoints(url2, 1); |
| 1585 |
| 1586 EXPECT_EQ(1, CheckScoreFromSettingsOnThread(content::BrowserThread::IO, |
| 1587 settings_map, url1)); |
| 1588 EXPECT_EQ(2, CheckScoreFromSettingsOnThread(content::BrowserThread::FILE, |
| 1589 settings_map, url2)); |
| 1590 EXPECT_EQ(4, CheckScoreFromSettingsOnThread(content::BrowserThread::IO, |
| 1591 incognito_settings_map, url1)); |
| 1592 EXPECT_EQ(3, CheckScoreFromSettingsOnThread(content::BrowserThread::FILE, |
| 1593 incognito_settings_map, url2)); |
| 1594 |
| 1595 service->AddPoints(url1, 2); |
| 1596 service->AddPoints(url2, 1); |
| 1597 |
| 1598 EXPECT_EQ(3, CheckScoreFromSettingsOnThread(content::BrowserThread::IO, |
| 1599 settings_map, url1)); |
| 1600 EXPECT_EQ(3, CheckScoreFromSettingsOnThread(content::BrowserThread::FILE, |
| 1601 settings_map, url2)); |
| 1602 EXPECT_EQ(4, CheckScoreFromSettingsOnThread(content::BrowserThread::FILE, |
| 1603 incognito_settings_map, url1)); |
| 1604 EXPECT_EQ(3, CheckScoreFromSettingsOnThread(content::BrowserThread::IO, |
| 1605 incognito_settings_map, url2)); |
| 1606 } |
| OLD | NEW |