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 void CheckScoreFromSettingsOnThread( | |
calamity
2016/09/13 04:14:37
Does this actually send things to a different thre
dominickn
2016/09/13 04:42:14
I think if I set the thread options as REAL_IO_THR
calamity
2016/09/13 06:33:34
Ah cool.
| |
192 content::BrowserThread::ID thread_id, | |
193 HostContentSettingsMap* settings_map, | |
194 const GURL& url, | |
195 double expected_score) { | |
196 base::RunLoop run_loop; | |
197 content::BrowserThread::PostTask( | |
198 thread_id, FROM_HERE, | |
199 base::Bind(&SiteEngagementServiceTest::CheckScoreFromSettings, | |
200 base::Unretained(this), settings_map, url, expected_score, | |
201 run_loop.QuitClosure())); | |
202 run_loop.Run(); | |
203 } | |
204 | |
190 private: | 205 private: |
206 void CheckScoreFromSettings(HostContentSettingsMap* settings_map, | |
207 const GURL& url, | |
208 double expected_score, | |
209 const base::Closure& quit_closure) { | |
210 EXPECT_EQ(expected_score, | |
211 SiteEngagementService::GetScoreFromSettings(settings_map, url)); | |
212 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, | |
213 quit_closure); | |
214 } | |
215 | |
191 base::ScopedTempDir temp_dir_; | 216 base::ScopedTempDir temp_dir_; |
192 }; | 217 }; |
193 | 218 |
194 TEST_F(SiteEngagementServiceTest, GetMedianEngagement) { | 219 TEST_F(SiteEngagementServiceTest, GetMedianEngagement) { |
195 SiteEngagementService* service = SiteEngagementService::Get(profile()); | 220 SiteEngagementService* service = SiteEngagementService::Get(profile()); |
196 ASSERT_TRUE(service); | 221 ASSERT_TRUE(service); |
197 | 222 |
198 GURL url1("http://www.google.com/"); | 223 GURL url1("http://www.google.com/"); |
199 GURL url2("https://www.google.com/"); | 224 GURL url2("https://www.google.com/"); |
200 GURL url3("https://drive.google.com/"); | 225 GURL url3("https://drive.google.com/"); |
(...skipping 1306 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1507 | 1532 |
1508 service->AddPoints(url3, 2); | 1533 service->AddPoints(url3, 2); |
1509 EXPECT_EQ(1, incognito_service->GetScore(url3)); | 1534 EXPECT_EQ(1, incognito_service->GetScore(url3)); |
1510 EXPECT_EQ(2, service->GetScore(url3)); | 1535 EXPECT_EQ(2, service->GetScore(url3)); |
1511 | 1536 |
1512 EXPECT_EQ(0, incognito_service->GetScore(url4)); | 1537 EXPECT_EQ(0, incognito_service->GetScore(url4)); |
1513 service->AddPoints(url4, 2); | 1538 service->AddPoints(url4, 2); |
1514 EXPECT_EQ(2, incognito_service->GetScore(url4)); | 1539 EXPECT_EQ(2, incognito_service->GetScore(url4)); |
1515 EXPECT_EQ(2, service->GetScore(url4)); | 1540 EXPECT_EQ(2, service->GetScore(url4)); |
1516 } | 1541 } |
1542 | |
1543 TEST_F(SiteEngagementServiceTest, GetScoreFromSettings) { | |
1544 GURL url1("http://www.google.com/"); | |
1545 GURL url2("https://www.google.com/"); | |
1546 | |
1547 HostContentSettingsMap* settings_map = | |
1548 HostContentSettingsMapFactory::GetForProfile(profile()); | |
1549 HostContentSettingsMap* incognito_settings_map = | |
1550 HostContentSettingsMapFactory::GetForProfile( | |
1551 profile()->GetOffTheRecordProfile()); | |
1552 | |
1553 // All scores are 0 to start. | |
1554 CheckScoreFromSettingsOnThread(content::BrowserThread::IO, settings_map, url1, | |
1555 0); | |
1556 CheckScoreFromSettingsOnThread(content::BrowserThread::FILE, settings_map, | |
1557 url2, 0); | |
1558 CheckScoreFromSettingsOnThread(content::BrowserThread::FILE, | |
1559 incognito_settings_map, url1, 0); | |
1560 CheckScoreFromSettingsOnThread(content::BrowserThread::IO, | |
1561 incognito_settings_map, url2, 0); | |
1562 | |
1563 SiteEngagementService* service = SiteEngagementService::Get(profile()); | |
1564 ASSERT_TRUE(service); | |
1565 service->AddPoints(url1, 1); | |
1566 service->AddPoints(url2, 2); | |
1567 | |
1568 CheckScoreFromSettingsOnThread(content::BrowserThread::FILE, settings_map, | |
1569 url1, 1); | |
1570 CheckScoreFromSettingsOnThread(content::BrowserThread::IO, settings_map, url2, | |
1571 2); | |
1572 CheckScoreFromSettingsOnThread(content::BrowserThread::FILE, | |
1573 incognito_settings_map, url1, 1); | |
1574 CheckScoreFromSettingsOnThread(content::BrowserThread::IO, | |
1575 incognito_settings_map, url2, 2); | |
1576 | |
1577 SiteEngagementService* incognito_service = | |
1578 SiteEngagementService::Get(profile()->GetOffTheRecordProfile()); | |
1579 ASSERT_TRUE(incognito_service); | |
1580 incognito_service->AddPoints(url1, 3); | |
1581 incognito_service->AddPoints(url2, 1); | |
1582 | |
1583 CheckScoreFromSettingsOnThread(content::BrowserThread::IO, | |
1584 incognito_settings_map, url1, 4); | |
1585 CheckScoreFromSettingsOnThread(content::BrowserThread::FILE, | |
1586 incognito_settings_map, url2, 3); | |
1587 CheckScoreFromSettingsOnThread(content::BrowserThread::FILE, settings_map, | |
1588 url1, 1); | |
1589 CheckScoreFromSettingsOnThread(content::BrowserThread::IO, settings_map, url2, | |
1590 2); | |
1591 | |
1592 service->AddPoints(url1, 2); | |
1593 service->AddPoints(url2, 1); | |
1594 | |
1595 CheckScoreFromSettingsOnThread(content::BrowserThread::IO, | |
1596 incognito_settings_map, url1, 4); | |
1597 CheckScoreFromSettingsOnThread(content::BrowserThread::FILE, | |
1598 incognito_settings_map, url2, 3); | |
1599 CheckScoreFromSettingsOnThread(content::BrowserThread::IO, settings_map, url1, | |
1600 3); | |
1601 CheckScoreFromSettingsOnThread(content::BrowserThread::FILE, settings_map, | |
1602 url2, 3); | |
1603 } | |
OLD | NEW |