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" |
35 #include "content/public/test/web_contents_tester.h" | 36 #include "content/public/test/web_contents_tester.h" |
36 #include "testing/gtest/include/gtest/gtest.h" | 37 #include "testing/gtest/include/gtest/gtest.h" |
37 | 38 |
38 namespace { | 39 namespace { |
39 | 40 |
40 base::FilePath g_temp_history_dir; | 41 base::FilePath g_temp_history_dir; |
41 | 42 |
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
180 } | 181 } |
181 | 182 |
182 void SetParamValue(SiteEngagementScore::Variation variation, double value) { | 183 void SetParamValue(SiteEngagementScore::Variation variation, double value) { |
183 SiteEngagementScore::GetParamValues()[variation].second = value; | 184 SiteEngagementScore::GetParamValues()[variation].second = value; |
184 } | 185 } |
185 | 186 |
186 void AssertInRange(double expected, double actual) { | 187 void AssertInRange(double expected, double actual) { |
187 EXPECT_NEAR(expected, actual, kMaxRoundingDeviation); | 188 EXPECT_NEAR(expected, actual, kMaxRoundingDeviation); |
188 } | 189 } |
189 | 190 |
| 191 double CheckScoreFromSettingsOnThread( |
| 192 content::BrowserThread::ID thread_id, |
| 193 HostContentSettingsMap* settings_map, |
| 194 const GURL& url) { |
| 195 double score = 0; |
| 196 base::RunLoop run_loop; |
| 197 content::BrowserThread::PostTaskAndReply( |
| 198 thread_id, FROM_HERE, |
| 199 base::Bind(&SiteEngagementServiceTest::CheckScoreFromSettings, |
| 200 base::Unretained(this), settings_map, url, &score), |
| 201 run_loop.QuitClosure()); |
| 202 run_loop.Run(); |
| 203 return score; |
| 204 } |
| 205 |
190 private: | 206 private: |
| 207 void CheckScoreFromSettings(HostContentSettingsMap* settings_map, |
| 208 const GURL& url, |
| 209 double *score) { |
| 210 *score = SiteEngagementService::GetScoreFromSettings(settings_map, url); |
| 211 } |
| 212 |
191 base::ScopedTempDir temp_dir_; | 213 base::ScopedTempDir temp_dir_; |
192 }; | 214 }; |
193 | 215 |
194 TEST_F(SiteEngagementServiceTest, GetMedianEngagement) { | 216 TEST_F(SiteEngagementServiceTest, GetMedianEngagement) { |
195 SiteEngagementService* service = SiteEngagementService::Get(profile()); | 217 SiteEngagementService* service = SiteEngagementService::Get(profile()); |
196 ASSERT_TRUE(service); | 218 ASSERT_TRUE(service); |
197 | 219 |
198 GURL url1("http://www.google.com/"); | 220 GURL url1("http://www.google.com/"); |
199 GURL url2("https://www.google.com/"); | 221 GURL url2("https://www.google.com/"); |
200 GURL url3("https://drive.google.com/"); | 222 GURL url3("https://drive.google.com/"); |
(...skipping 1306 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1507 | 1529 |
1508 service->AddPoints(url3, 2); | 1530 service->AddPoints(url3, 2); |
1509 EXPECT_EQ(1, incognito_service->GetScore(url3)); | 1531 EXPECT_EQ(1, incognito_service->GetScore(url3)); |
1510 EXPECT_EQ(2, service->GetScore(url3)); | 1532 EXPECT_EQ(2, service->GetScore(url3)); |
1511 | 1533 |
1512 EXPECT_EQ(0, incognito_service->GetScore(url4)); | 1534 EXPECT_EQ(0, incognito_service->GetScore(url4)); |
1513 service->AddPoints(url4, 2); | 1535 service->AddPoints(url4, 2); |
1514 EXPECT_EQ(2, incognito_service->GetScore(url4)); | 1536 EXPECT_EQ(2, incognito_service->GetScore(url4)); |
1515 EXPECT_EQ(2, service->GetScore(url4)); | 1537 EXPECT_EQ(2, service->GetScore(url4)); |
1516 } | 1538 } |
| 1539 |
| 1540 TEST_F(SiteEngagementServiceTest, GetScoreFromSettings) { |
| 1541 GURL url1("http://www.google.com/"); |
| 1542 GURL url2("https://www.google.com/"); |
| 1543 |
| 1544 HostContentSettingsMap* settings_map = |
| 1545 HostContentSettingsMapFactory::GetForProfile(profile()); |
| 1546 HostContentSettingsMap* incognito_settings_map = |
| 1547 HostContentSettingsMapFactory::GetForProfile( |
| 1548 profile()->GetOffTheRecordProfile()); |
| 1549 |
| 1550 // All scores are 0 to start. |
| 1551 EXPECT_EQ(0, CheckScoreFromSettingsOnThread(content::BrowserThread::IO, |
| 1552 settings_map, url1)); |
| 1553 EXPECT_EQ(0, CheckScoreFromSettingsOnThread(content::BrowserThread::FILE, |
| 1554 settings_map, url2)); |
| 1555 EXPECT_EQ(0, CheckScoreFromSettingsOnThread(content::BrowserThread::FILE, |
| 1556 incognito_settings_map, url1)); |
| 1557 EXPECT_EQ(0, CheckScoreFromSettingsOnThread(content::BrowserThread::IO, |
| 1558 incognito_settings_map, url2)); |
| 1559 |
| 1560 SiteEngagementService* service = SiteEngagementService::Get(profile()); |
| 1561 ASSERT_TRUE(service); |
| 1562 service->AddPoints(url1, 1); |
| 1563 service->AddPoints(url2, 2); |
| 1564 |
| 1565 EXPECT_EQ(1, CheckScoreFromSettingsOnThread(content::BrowserThread::FILE, |
| 1566 settings_map, url1)); |
| 1567 EXPECT_EQ(2, CheckScoreFromSettingsOnThread(content::BrowserThread::IO, |
| 1568 settings_map, url2)); |
| 1569 EXPECT_EQ(1, CheckScoreFromSettingsOnThread(content::BrowserThread::FILE, |
| 1570 incognito_settings_map, url1)); |
| 1571 EXPECT_EQ(2, CheckScoreFromSettingsOnThread(content::BrowserThread::IO, |
| 1572 incognito_settings_map, url2)); |
| 1573 |
| 1574 SiteEngagementService* incognito_service = |
| 1575 SiteEngagementService::Get(profile()->GetOffTheRecordProfile()); |
| 1576 ASSERT_TRUE(incognito_service); |
| 1577 incognito_service->AddPoints(url1, 3); |
| 1578 incognito_service->AddPoints(url2, 1); |
| 1579 |
| 1580 EXPECT_EQ(1, CheckScoreFromSettingsOnThread(content::BrowserThread::IO, |
| 1581 settings_map, url1)); |
| 1582 EXPECT_EQ(2, CheckScoreFromSettingsOnThread(content::BrowserThread::FILE, |
| 1583 settings_map, url2)); |
| 1584 EXPECT_EQ(4, CheckScoreFromSettingsOnThread(content::BrowserThread::IO, |
| 1585 incognito_settings_map, url1)); |
| 1586 EXPECT_EQ(3, CheckScoreFromSettingsOnThread(content::BrowserThread::FILE, |
| 1587 incognito_settings_map, url2)); |
| 1588 |
| 1589 service->AddPoints(url1, 2); |
| 1590 service->AddPoints(url2, 1); |
| 1591 |
| 1592 EXPECT_EQ(3, CheckScoreFromSettingsOnThread(content::BrowserThread::IO, |
| 1593 settings_map, url1)); |
| 1594 EXPECT_EQ(3, CheckScoreFromSettingsOnThread(content::BrowserThread::FILE, |
| 1595 settings_map, url2)); |
| 1596 EXPECT_EQ(4, CheckScoreFromSettingsOnThread(content::BrowserThread::FILE, |
| 1597 incognito_settings_map, url1)); |
| 1598 EXPECT_EQ(3, CheckScoreFromSettingsOnThread(content::BrowserThread::IO, |
| 1599 incognito_settings_map, url2)); |
| 1600 } |
OLD | NEW |