OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/android/preferences/important_sites_util.h" |
| 6 |
| 7 #include <memory> |
| 8 #include <utility> |
| 9 |
| 10 #include "base/files/scoped_temp_dir.h" |
| 11 #include "base/macros.h" |
| 12 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" |
| 13 #include "chrome/browser/engagement/site_engagement_helper.h" |
| 14 #include "chrome/browser/engagement/site_engagement_metrics.h" |
| 15 #include "chrome/browser/engagement/site_engagement_service.h" |
| 16 #include "chrome/browser/engagement/site_engagement_service_factory.h" |
| 17 #include "chrome/browser/history/history_service_factory.h" |
| 18 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
| 19 #include "chrome/test/base/testing_profile.h" |
| 20 #include "components/content_settings/core/browser/host_content_settings_map.h" |
| 21 #include "components/content_settings/core/common/content_settings.h" |
| 22 #include "components/content_settings/core/common/content_settings_pattern.h" |
| 23 #include "components/history/core/browser/history_database_params.h" |
| 24 #include "components/history/core/browser/history_service.h" |
| 25 #include "components/history/core/test/test_history_database.h" |
| 26 #include "components/keyed_service/core/keyed_service.h" |
| 27 #include "content/public/browser/web_contents.h" |
| 28 #include "testing/gmock/include/gmock/gmock.h" |
| 29 #include "testing/gtest/include/gtest/gtest.h" |
| 30 |
| 31 namespace { |
| 32 |
| 33 const size_t kNumImportantSites = 5; |
| 34 base::FilePath g_temp_history_dir; |
| 35 |
| 36 scoped_ptr<KeyedService> BuildTestHistoryService( |
| 37 content::BrowserContext* context) { |
| 38 scoped_ptr<history::HistoryService> service(new history::HistoryService()); |
| 39 service->Init(history::TestHistoryDatabaseParamsForPath(g_temp_history_dir)); |
| 40 return std::move(service); |
| 41 } |
| 42 |
| 43 } // namespace |
| 44 |
| 45 class ImportantSitesUtilTest : public ChromeRenderViewHostTestHarness { |
| 46 public: |
| 47 void SetUp() override { |
| 48 ChromeRenderViewHostTestHarness::SetUp(); |
| 49 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 50 g_temp_history_dir = temp_dir_.path(); |
| 51 HistoryServiceFactory::GetInstance()->SetTestingFactory( |
| 52 profile(), &BuildTestHistoryService); |
| 53 SiteEngagementScore::SetParamValuesForTesting(); |
| 54 } |
| 55 |
| 56 void AddContentSetting(ContentSettingsType type, |
| 57 ContentSetting setting, |
| 58 const GURL& origin) { |
| 59 ContentSettingsForOneType settings_list; |
| 60 |
| 61 HostContentSettingsMapFactory::GetForProfile(profile()) |
| 62 ->SetContentSettingCustomScope( |
| 63 ContentSettingsPattern::FromURLNoWildcard(origin), |
| 64 ContentSettingsPattern::Wildcard(), |
| 65 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, |
| 66 content_settings::ResourceIdentifier(), setting); |
| 67 } |
| 68 |
| 69 private: |
| 70 base::ScopedTempDir temp_dir_; |
| 71 }; |
| 72 |
| 73 TEST_F(ImportantSitesUtilTest, TestNoImportantSites) { |
| 74 EXPECT_TRUE(ImportantSitesUtil::GetImportantRegisterableDomains( |
| 75 profile(), kNumImportantSites) |
| 76 .empty()); |
| 77 } |
| 78 |
| 79 TEST_F(ImportantSitesUtilTest, NotificationsThenEngagement) { |
| 80 SiteEngagementService* service = |
| 81 SiteEngagementServiceFactory::GetForProfile(profile()); |
| 82 ASSERT_TRUE(service); |
| 83 |
| 84 GURL url1("http://www.google.com/"); |
| 85 GURL url2("https://www.google.com/"); |
| 86 GURL url3("https://drive.google.com/"); |
| 87 GURL url4("https://www.chrome.com/"); |
| 88 GURL url5("https://www.example.com/"); |
| 89 GURL url6("https://youtube.com/"); |
| 90 GURL url7("https://foo.bar/"); |
| 91 |
| 92 service->ResetScoreForURL(url1, 5); |
| 93 service->ResetScoreForURL(url2, 2); // Below medium engagement (5). |
| 94 service->ResetScoreForURL(url3, 7); |
| 95 service->ResetScoreForURL(url4, 8); |
| 96 service->ResetScoreForURL(url5, 9); |
| 97 service->ResetScoreForURL(url6, 1); // Below the medium engagement (5). |
| 98 service->ResetScoreForURL(url7, 11); |
| 99 |
| 100 // Here we should have: |
| 101 // 1: removed domains below minimum engagement, |
| 102 // 2: combined the google.com entries, and |
| 103 // 3: sorted by the score. |
| 104 std::vector<std::string> expected_sorted_domains = { |
| 105 "foo.bar", "example.com", "chrome.com", "google.com"}; |
| 106 EXPECT_THAT(ImportantSitesUtil::GetImportantRegisterableDomains( |
| 107 profile(), kNumImportantSites), |
| 108 ::testing::ElementsAreArray(expected_sorted_domains)); |
| 109 |
| 110 // Test that notifications get moved to the front. |
| 111 AddContentSetting(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, CONTENT_SETTING_ALLOW, |
| 112 url6); |
| 113 // BLOCK'ed sites don't count. We want to make sure we only bump sites that |
| 114 // were granted the permsion. |
| 115 AddContentSetting(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, CONTENT_SETTING_BLOCK, |
| 116 url1); |
| 117 |
| 118 // Same as above, but the site with notifications should be at the front. |
| 119 expected_sorted_domains = {"youtube.com", "foo.bar", "example.com", |
| 120 "chrome.com", "google.com"}; |
| 121 EXPECT_THAT(ImportantSitesUtil::GetImportantRegisterableDomains( |
| 122 profile(), kNumImportantSites), |
| 123 ::testing::ElementsAreArray(expected_sorted_domains)); |
| 124 } |
OLD | NEW |