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/command_line.h" | 9 #include "base/files/scoped_temp_dir.h" |
10 #include "base/macros.h" | 10 #include "base/macros.h" |
11 #include "base/run_loop.h" | 11 #include "base/run_loop.h" |
12 #include "base/test/histogram_tester.h" | 12 #include "base/test/histogram_tester.h" |
13 #include "base/test/simple_test_clock.h" | 13 #include "base/test/simple_test_clock.h" |
14 #include "base/values.h" | 14 #include "base/values.h" |
15 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" | 15 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" |
16 #include "chrome/browser/engagement/site_engagement_helper.h" | 16 #include "chrome/browser/engagement/site_engagement_helper.h" |
17 #include "chrome/browser/engagement/site_engagement_metrics.h" | 17 #include "chrome/browser/engagement/site_engagement_metrics.h" |
18 #include "chrome/browser/engagement/site_engagement_service_factory.h" | 18 #include "chrome/browser/engagement/site_engagement_service_factory.h" |
19 #include "chrome/browser/history/history_service_factory.h" | 19 #include "chrome/browser/history/history_service_factory.h" |
20 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 20 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
21 #include "chrome/common/chrome_switches.h" | 21 #include "chrome/common/chrome_switches.h" |
22 #include "chrome/test/base/browser_with_test_window_test.h" | 22 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
23 #include "chrome/test/base/testing_profile.h" | |
23 #include "components/content_settings/core/browser/content_settings_observer.h" | 24 #include "components/content_settings/core/browser/content_settings_observer.h" |
24 #include "components/content_settings/core/browser/host_content_settings_map.h" | 25 #include "components/content_settings/core/browser/host_content_settings_map.h" |
25 #include "components/history/core/browser/history_database_params.h" | 26 #include "components/history/core/browser/history_database_params.h" |
26 #include "components/history/core/browser/history_service.h" | 27 #include "components/history/core/browser/history_service.h" |
27 #include "components/history/core/test/test_history_database.h" | 28 #include "components/history/core/test/test_history_database.h" |
29 #include "content/public/browser/navigation_entry.h" | |
28 #include "content/public/browser/page_navigator.h" | 30 #include "content/public/browser/page_navigator.h" |
31 #include "content/public/browser/web_contents.h" | |
32 #include "content/public/test/web_contents_tester.h" | |
29 #include "testing/gtest/include/gtest/gtest.h" | 33 #include "testing/gtest/include/gtest/gtest.h" |
30 | 34 |
31 namespace { | 35 namespace { |
32 | 36 |
33 base::FilePath g_temp_history_dir; | 37 base::FilePath g_temp_history_dir; |
34 | 38 |
35 const int kLessAccumulationsThanNeededToMaxDailyEngagement = 2; | 39 const int kLessAccumulationsThanNeededToMaxDailyEngagement = 2; |
36 const int kMoreAccumulationsThanNeededToMaxDailyEngagement = 40; | 40 const int kMoreAccumulationsThanNeededToMaxDailyEngagement = 40; |
37 const int kMoreAccumulationsThanNeededToMaxTotalEngagement = 200; | 41 const int kMoreAccumulationsThanNeededToMaxTotalEngagement = 200; |
38 const int kLessDaysThanNeededToMaxTotalEngagement = 4; | 42 const int kLessDaysThanNeededToMaxTotalEngagement = 4; |
(...skipping 320 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
359 TEST_F(SiteEngagementScoreTest, PopulatedDictionary) { | 363 TEST_F(SiteEngagementScoreTest, PopulatedDictionary) { |
360 base::DictionaryValue dict; | 364 base::DictionaryValue dict; |
361 dict.SetDouble(SiteEngagementScore::kRawScoreKey, 1); | 365 dict.SetDouble(SiteEngagementScore::kRawScoreKey, 1); |
362 dict.SetDouble(SiteEngagementScore::kPointsAddedTodayKey, 2); | 366 dict.SetDouble(SiteEngagementScore::kPointsAddedTodayKey, 2); |
363 dict.SetDouble(SiteEngagementScore::kLastEngagementTimeKey, | 367 dict.SetDouble(SiteEngagementScore::kLastEngagementTimeKey, |
364 GetReferenceTime().ToInternalValue()); | 368 GetReferenceTime().ToInternalValue()); |
365 | 369 |
366 TestScoreInitializesAndUpdates(&dict, 1, 2, GetReferenceTime()); | 370 TestScoreInitializesAndUpdates(&dict, 1, 2, GetReferenceTime()); |
367 } | 371 } |
368 | 372 |
369 class SiteEngagementServiceTest : public BrowserWithTestWindowTest { | 373 class SiteEngagementServiceTest : public ChromeRenderViewHostTestHarness { |
370 public: | 374 public: |
371 SiteEngagementServiceTest() {} | |
372 | |
373 void SetUp() override { | 375 void SetUp() override { |
374 BrowserWithTestWindowTest::SetUp(); | 376 ChromeRenderViewHostTestHarness::SetUp(); |
375 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 377 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
376 g_temp_history_dir = temp_dir_.path(); | 378 g_temp_history_dir = temp_dir_.path(); |
377 HistoryServiceFactory::GetInstance()->SetTestingFactory( | 379 HistoryServiceFactory::GetInstance()->SetTestingFactory( |
378 profile(), &BuildTestHistoryService); | 380 profile(), &BuildTestHistoryService); |
379 base::CommandLine::ForCurrentProcess()->AppendSwitch( | |
benwells
2016/01/13 23:14:32
How come the flag isn't needed?
dominickn
2016/01/14 01:17:41
The service is normally kicked off in tab_helpers.
| |
380 switches::kEnableSiteEngagementService); | |
381 } | 381 } |
382 | 382 |
383 void NavigateWithTransitionAndExpectHigherScore( | 383 void NavigateWithTransitionAndExpectHigherScore( |
384 SiteEngagementService* service, | 384 SiteEngagementService* service, |
385 GURL& url, | 385 const GURL& url, |
386 ui::PageTransition transition) { | 386 ui::PageTransition transition) { |
387 double prev_score = service->GetScore(url); | 387 double prev_score = service->GetScore(url); |
388 content::NavigationController* controller = | 388 controller().LoadURL(url, content::Referrer(), transition, std::string()); |
389 &browser()->tab_strip_model()->GetActiveWebContents()->GetController(); | 389 int pending_id = controller().GetPendingEntry()->GetUniqueID(); |
390 | 390 content::WebContentsTester::For(web_contents()) |
391 browser()->OpenURL(content::OpenURLParams(url, content::Referrer(), | 391 ->TestDidNavigate(web_contents()->GetMainFrame(), 1, pending_id, true, |
392 CURRENT_TAB, transition, false)); | 392 url, transition); |
393 CommitPendingLoad(controller); | |
394 EXPECT_LT(prev_score, service->GetScore(url)); | 393 EXPECT_LT(prev_score, service->GetScore(url)); |
395 } | 394 } |
396 | 395 |
397 void NavigateWithTransitionAndExpectEqualScore( | 396 void NavigateWithTransitionAndExpectEqualScore( |
398 SiteEngagementService* service, | 397 SiteEngagementService* service, |
399 GURL& url, | 398 GURL& url, |
benwells
2016/01/13 23:14:32
Nit: Should this be const& as well?
dominickn
2016/01/14 01:17:41
Done.
| |
400 ui::PageTransition transition) { | 399 ui::PageTransition transition) { |
401 double prev_score = service->GetScore(url); | 400 double prev_score = service->GetScore(url); |
402 content::NavigationController* controller = | 401 controller().LoadURL(url, content::Referrer(), transition, std::string()); |
403 &browser()->tab_strip_model()->GetActiveWebContents()->GetController(); | 402 int pending_id = controller().GetPendingEntry()->GetUniqueID(); |
404 | 403 content::WebContentsTester::For(web_contents()) |
405 browser()->OpenURL(content::OpenURLParams(url, content::Referrer(), | 404 ->TestDidNavigate(web_contents()->GetMainFrame(), 1, pending_id, true, |
406 CURRENT_TAB, transition, false)); | 405 url, transition); |
407 CommitPendingLoad(controller); | |
408 EXPECT_EQ(prev_score, service->GetScore(url)); | 406 EXPECT_EQ(prev_score, service->GetScore(url)); |
409 } | 407 } |
410 | 408 |
411 private: | 409 private: |
412 base::ScopedTempDir temp_dir_; | 410 base::ScopedTempDir temp_dir_; |
413 }; | 411 }; |
414 | 412 |
415 TEST_F(SiteEngagementServiceTest, GetMedianEngagement) { | 413 TEST_F(SiteEngagementServiceTest, GetMedianEngagement) { |
416 SiteEngagementService* service = | 414 SiteEngagementService* service = |
417 SiteEngagementServiceFactory::GetForProfile(profile()); | 415 SiteEngagementServiceFactory::GetForProfile(profile()); |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
477 } | 475 } |
478 | 476 |
479 // Tests that the Site Engagement service is hooked up properly to navigations | 477 // Tests that the Site Engagement service is hooked up properly to navigations |
480 // by performing two navigations and checking the engagement score increases | 478 // by performing two navigations and checking the engagement score increases |
481 // both times. | 479 // both times. |
482 TEST_F(SiteEngagementServiceTest, ScoreIncrementsOnPageRequest) { | 480 TEST_F(SiteEngagementServiceTest, ScoreIncrementsOnPageRequest) { |
483 SiteEngagementService* service = | 481 SiteEngagementService* service = |
484 SiteEngagementServiceFactory::GetForProfile(profile()); | 482 SiteEngagementServiceFactory::GetForProfile(profile()); |
485 ASSERT_TRUE(service); | 483 ASSERT_TRUE(service); |
486 | 484 |
485 // Create the helper manually since it isn't present when a tab isn't created. | |
486 SiteEngagementHelper::CreateForWebContents(web_contents()); | |
487 | |
487 GURL url("http://www.google.com/"); | 488 GURL url("http://www.google.com/"); |
488 EXPECT_EQ(0, service->GetScore(url)); | 489 EXPECT_EQ(0, service->GetScore(url)); |
489 AddTab(browser(), GURL("about:blank")); | |
490 | |
491 NavigateWithTransitionAndExpectHigherScore(service, url, | 490 NavigateWithTransitionAndExpectHigherScore(service, url, |
492 ui::PAGE_TRANSITION_TYPED); | 491 ui::PAGE_TRANSITION_TYPED); |
493 | |
494 NavigateWithTransitionAndExpectHigherScore(service, url, | 492 NavigateWithTransitionAndExpectHigherScore(service, url, |
495 ui::PAGE_TRANSITION_AUTO_BOOKMARK); | 493 ui::PAGE_TRANSITION_AUTO_BOOKMARK); |
496 } | 494 } |
497 | 495 |
498 // Expect that site engagement scores for several sites are correctly | 496 // Expect that site engagement scores for several sites are correctly |
499 // aggregated during navigation events. | 497 // aggregated during navigation events. |
500 TEST_F(SiteEngagementServiceTest, GetTotalNavigationPoints) { | 498 TEST_F(SiteEngagementServiceTest, GetTotalNavigationPoints) { |
501 SiteEngagementService* service = | 499 SiteEngagementService* service = |
502 SiteEngagementServiceFactory::GetForProfile(profile()); | 500 SiteEngagementServiceFactory::GetForProfile(profile()); |
503 ASSERT_TRUE(service); | 501 ASSERT_TRUE(service); |
(...skipping 317 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
821 | 819 |
822 service->CleanupEngagementScores(); | 820 service->CleanupEngagementScores(); |
823 | 821 |
824 score_map = service->GetScoreMap(); | 822 score_map = service->GetScoreMap(); |
825 EXPECT_EQ(0u, score_map.size()); | 823 EXPECT_EQ(0u, score_map.size()); |
826 EXPECT_EQ(0, service->GetScore(url1)); | 824 EXPECT_EQ(0, service->GetScore(url1)); |
827 } | 825 } |
828 } | 826 } |
829 | 827 |
830 TEST_F(SiteEngagementServiceTest, NavigationAccumulation) { | 828 TEST_F(SiteEngagementServiceTest, NavigationAccumulation) { |
831 AddTab(browser(), GURL("about:blank")); | |
832 GURL url("https://www.google.com/"); | 829 GURL url("https://www.google.com/"); |
833 | 830 |
834 SiteEngagementService* service = | 831 SiteEngagementService* service = |
835 SiteEngagementServiceFactory::GetForProfile(browser()->profile()); | 832 SiteEngagementServiceFactory::GetForProfile(profile()); |
836 ASSERT_TRUE(service); | 833 ASSERT_TRUE(service); |
837 | 834 |
835 // Create the helper manually since it isn't present when a tab isn't created. | |
836 SiteEngagementHelper::CreateForWebContents(web_contents()); | |
837 | |
838 // Only direct navigation should trigger engagement. | 838 // Only direct navigation should trigger engagement. |
839 NavigateWithTransitionAndExpectHigherScore(service, url, | 839 NavigateWithTransitionAndExpectHigherScore(service, url, |
840 ui::PAGE_TRANSITION_TYPED); | 840 ui::PAGE_TRANSITION_TYPED); |
841 NavigateWithTransitionAndExpectHigherScore(service, url, | 841 NavigateWithTransitionAndExpectHigherScore(service, url, |
842 ui::PAGE_TRANSITION_GENERATED); | 842 ui::PAGE_TRANSITION_GENERATED); |
843 NavigateWithTransitionAndExpectHigherScore(service, url, | 843 NavigateWithTransitionAndExpectHigherScore(service, url, |
844 ui::PAGE_TRANSITION_AUTO_BOOKMARK); | 844 ui::PAGE_TRANSITION_AUTO_BOOKMARK); |
845 NavigateWithTransitionAndExpectHigherScore( | 845 NavigateWithTransitionAndExpectHigherScore( |
846 service, url, ui::PAGE_TRANSITION_KEYWORD_GENERATED); | 846 service, url, ui::PAGE_TRANSITION_KEYWORD_GENERATED); |
847 | 847 |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
923 &task_tracker); | 923 &task_tracker); |
924 waiter.Wait(); | 924 waiter.Wait(); |
925 | 925 |
926 // Only origin3 remains. | 926 // Only origin3 remains. |
927 EXPECT_EQ(0, engagement->GetScore(origin1)); | 927 EXPECT_EQ(0, engagement->GetScore(origin1)); |
928 EXPECT_EQ(0, engagement->GetScore(origin2)); | 928 EXPECT_EQ(0, engagement->GetScore(origin2)); |
929 EXPECT_EQ(5.0, engagement->GetScore(origin3)); | 929 EXPECT_EQ(5.0, engagement->GetScore(origin3)); |
930 EXPECT_EQ(5.0, engagement->GetTotalEngagementPoints()); | 930 EXPECT_EQ(5.0, engagement->GetTotalEngagementPoints()); |
931 } | 931 } |
932 } | 932 } |
OLD | NEW |