Chromium Code Reviews| 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 void CheckScoreFromSettingsOnThread( | |
| 198 content::BrowserThread::ID thread_id, | |
| 199 HostContentSettingsMap* settings_map, | |
| 200 const GURL& url, | |
| 201 double expected_score) { | |
| 202 base::RunLoop run_loop; | |
| 203 content::BrowserThread::PostTask( | |
| 204 thread_id, FROM_HERE, | |
| 205 base::Bind(&SiteEngagementServiceTest::CheckScoreFromSettings, | |
| 206 base::Unretained(this), settings_map, url, expected_score, | |
| 207 run_loop.QuitClosure())); | |
| 208 run_loop.Run(); | |
| 209 } | |
| 210 | |
| 190 private: | 211 private: |
| 212 void CheckScoreFromSettings(HostContentSettingsMap* settings_map, | |
| 213 const GURL& url, | |
| 214 double expected_score, | |
| 215 const base::Closure& quit_closure) { | |
| 216 EXPECT_EQ(expected_score, | |
| 217 SiteEngagementService::GetScoreFromSettings(settings_map, url)); | |
| 218 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, | |
|
Bernhard Bauer
2016/09/15 09:05:15
You can also use PostTaskAndReply here.
dominickn
2016/09/15 12:16:00
Done.
| |
| 219 quit_closure); | |
| 220 } | |
| 221 | |
| 191 base::ScopedTempDir temp_dir_; | 222 base::ScopedTempDir temp_dir_; |
| 192 }; | 223 }; |
| 193 | 224 |
| 194 TEST_F(SiteEngagementServiceTest, GetMedianEngagement) { | 225 TEST_F(SiteEngagementServiceTest, GetMedianEngagement) { |
| 195 SiteEngagementService* service = SiteEngagementService::Get(profile()); | 226 SiteEngagementService* service = SiteEngagementService::Get(profile()); |
| 196 ASSERT_TRUE(service); | 227 ASSERT_TRUE(service); |
| 197 | 228 |
| 198 GURL url1("http://www.google.com/"); | 229 GURL url1("http://www.google.com/"); |
| 199 GURL url2("https://www.google.com/"); | 230 GURL url2("https://www.google.com/"); |
| 200 GURL url3("https://drive.google.com/"); | 231 GURL url3("https://drive.google.com/"); |
| (...skipping 1306 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1507 | 1538 |
| 1508 service->AddPoints(url3, 2); | 1539 service->AddPoints(url3, 2); |
| 1509 EXPECT_EQ(1, incognito_service->GetScore(url3)); | 1540 EXPECT_EQ(1, incognito_service->GetScore(url3)); |
| 1510 EXPECT_EQ(2, service->GetScore(url3)); | 1541 EXPECT_EQ(2, service->GetScore(url3)); |
| 1511 | 1542 |
| 1512 EXPECT_EQ(0, incognito_service->GetScore(url4)); | 1543 EXPECT_EQ(0, incognito_service->GetScore(url4)); |
| 1513 service->AddPoints(url4, 2); | 1544 service->AddPoints(url4, 2); |
| 1514 EXPECT_EQ(2, incognito_service->GetScore(url4)); | 1545 EXPECT_EQ(2, incognito_service->GetScore(url4)); |
| 1515 EXPECT_EQ(2, service->GetScore(url4)); | 1546 EXPECT_EQ(2, service->GetScore(url4)); |
| 1516 } | 1547 } |
| 1548 | |
| 1549 TEST_F(SiteEngagementServiceTest, GetScoreFromSettings) { | |
| 1550 GURL url1("http://www.google.com/"); | |
| 1551 GURL url2("https://www.google.com/"); | |
| 1552 | |
| 1553 HostContentSettingsMap* settings_map = | |
| 1554 HostContentSettingsMapFactory::GetForProfile(profile()); | |
| 1555 HostContentSettingsMap* incognito_settings_map = | |
| 1556 HostContentSettingsMapFactory::GetForProfile( | |
| 1557 profile()->GetOffTheRecordProfile()); | |
| 1558 | |
| 1559 // All scores are 0 to start. | |
| 1560 CheckScoreFromSettingsOnThread(content::BrowserThread::IO, settings_map, url1, | |
| 1561 0); | |
| 1562 CheckScoreFromSettingsOnThread(content::BrowserThread::FILE, settings_map, | |
| 1563 url2, 0); | |
| 1564 CheckScoreFromSettingsOnThread(content::BrowserThread::FILE, | |
| 1565 incognito_settings_map, url1, 0); | |
| 1566 CheckScoreFromSettingsOnThread(content::BrowserThread::IO, | |
| 1567 incognito_settings_map, url2, 0); | |
| 1568 | |
| 1569 SiteEngagementService* service = SiteEngagementService::Get(profile()); | |
| 1570 ASSERT_TRUE(service); | |
| 1571 service->AddPoints(url1, 1); | |
| 1572 service->AddPoints(url2, 2); | |
| 1573 | |
| 1574 CheckScoreFromSettingsOnThread(content::BrowserThread::FILE, settings_map, | |
| 1575 url1, 1); | |
| 1576 CheckScoreFromSettingsOnThread(content::BrowserThread::IO, settings_map, url2, | |
| 1577 2); | |
| 1578 CheckScoreFromSettingsOnThread(content::BrowserThread::FILE, | |
| 1579 incognito_settings_map, url1, 1); | |
| 1580 CheckScoreFromSettingsOnThread(content::BrowserThread::IO, | |
| 1581 incognito_settings_map, url2, 2); | |
| 1582 | |
| 1583 SiteEngagementService* incognito_service = | |
| 1584 SiteEngagementService::Get(profile()->GetOffTheRecordProfile()); | |
| 1585 ASSERT_TRUE(incognito_service); | |
| 1586 incognito_service->AddPoints(url1, 3); | |
| 1587 incognito_service->AddPoints(url2, 1); | |
| 1588 | |
| 1589 CheckScoreFromSettingsOnThread(content::BrowserThread::IO, | |
| 1590 incognito_settings_map, url1, 4); | |
| 1591 CheckScoreFromSettingsOnThread(content::BrowserThread::FILE, | |
| 1592 incognito_settings_map, url2, 3); | |
| 1593 CheckScoreFromSettingsOnThread(content::BrowserThread::FILE, settings_map, | |
| 1594 url1, 1); | |
| 1595 CheckScoreFromSettingsOnThread(content::BrowserThread::IO, settings_map, url2, | |
| 1596 2); | |
| 1597 | |
| 1598 service->AddPoints(url1, 2); | |
| 1599 service->AddPoints(url2, 1); | |
| 1600 | |
| 1601 CheckScoreFromSettingsOnThread(content::BrowserThread::IO, | |
| 1602 incognito_settings_map, url1, 4); | |
| 1603 CheckScoreFromSettingsOnThread(content::BrowserThread::FILE, | |
| 1604 incognito_settings_map, url2, 3); | |
| 1605 CheckScoreFromSettingsOnThread(content::BrowserThread::IO, settings_map, url1, | |
| 1606 3); | |
| 1607 CheckScoreFromSettingsOnThread(content::BrowserThread::FILE, settings_map, | |
| 1608 url2, 3); | |
| 1609 } | |
| OLD | NEW |