OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/content_settings/host_content_settings_map.h" | 5 #include "chrome/browser/content_settings/host_content_settings_map_unittest.h" |
6 | 6 |
7 #include "base/auto_reset.h" | 7 #include "base/auto_reset.h" |
8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
9 #include "base/json/json_reader.h" | 9 #include "base/json/json_reader.h" |
10 #include "base/json/json_writer.h" | 10 #include "base/json/json_writer.h" |
11 #include "chrome/browser/content_settings/content_settings_details.h" | 11 #include "chrome/browser/content_settings/content_settings_details.h" |
12 #include "chrome/browser/prefs/pref_service.h" | 12 #include "chrome/browser/prefs/pref_service.h" |
13 #include "chrome/common/chrome_switches.h" | 13 #include "chrome/common/chrome_switches.h" |
14 #include "chrome/common/notification_registrar.h" | 14 #include "chrome/common/notification_registrar.h" |
15 #include "chrome/common/notification_service.h" | 15 #include "chrome/common/notification_service.h" |
(...skipping 10 matching lines...) Expand all Loading... |
26 | 26 |
27 bool SettingsEqual(const ContentSettings& settings1, | 27 bool SettingsEqual(const ContentSettings& settings1, |
28 const ContentSettings& settings2) { | 28 const ContentSettings& settings2) { |
29 for (int i = 0; i < CONTENT_SETTINGS_NUM_TYPES; ++i) { | 29 for (int i = 0; i < CONTENT_SETTINGS_NUM_TYPES; ++i) { |
30 if (settings1.settings[i] != settings2.settings[i]) | 30 if (settings1.settings[i] != settings2.settings[i]) |
31 return false; | 31 return false; |
32 } | 32 } |
33 return true; | 33 return true; |
34 } | 34 } |
35 | 35 |
36 class StubSettingsObserver : public NotificationObserver { | |
37 public: | |
38 StubSettingsObserver() : last_notifier(NULL), counter(0) { | |
39 registrar_.Add(this, NotificationType::CONTENT_SETTINGS_CHANGED, | |
40 NotificationService::AllSources()); | |
41 } | |
42 | |
43 virtual void Observe(NotificationType type, | |
44 const NotificationSource& source, | |
45 const NotificationDetails& details) { | |
46 ++counter; | |
47 Source<HostContentSettingsMap> content_settings(source); | |
48 Details<ContentSettingsDetails> settings_details(details); | |
49 last_notifier = content_settings.ptr(); | |
50 last_pattern = settings_details.ptr()->pattern(); | |
51 last_update_all = settings_details.ptr()->update_all(); | |
52 last_update_all_types = settings_details.ptr()->update_all_types(); | |
53 last_type = settings_details.ptr()->type(); | |
54 // This checks that calling a Get function from an observer doesn't | |
55 // deadlock. | |
56 last_notifier->GetContentSettings(GURL("http://random-hostname.com/")); | |
57 } | |
58 | |
59 HostContentSettingsMap* last_notifier; | |
60 ContentSettingsPattern last_pattern; | |
61 bool last_update_all; | |
62 bool last_update_all_types; | |
63 int counter; | |
64 ContentSettingsType last_type; | |
65 | |
66 private: | |
67 NotificationRegistrar registrar_; | |
68 }; | |
69 | |
70 class HostContentSettingsMapTest : public testing::Test { | 36 class HostContentSettingsMapTest : public testing::Test { |
71 public: | 37 public: |
72 HostContentSettingsMapTest() : ui_thread_(BrowserThread::UI, &message_loop_) { | 38 HostContentSettingsMapTest() : ui_thread_(BrowserThread::UI, &message_loop_) { |
73 } | 39 } |
74 | 40 |
75 protected: | 41 protected: |
76 MessageLoop message_loop_; | 42 MessageLoop message_loop_; |
77 BrowserThread ui_thread_; | 43 BrowserThread ui_thread_; |
78 }; | 44 }; |
79 | 45 |
(...skipping 744 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
824 host_content_settings_map->GetDefaultContentSetting( | 790 host_content_settings_map->GetDefaultContentSetting( |
825 CONTENT_SETTINGS_TYPE_COOKIES)); | 791 CONTENT_SETTINGS_TYPE_COOKIES)); |
826 | 792 |
827 // Remove the preference to manage the default-content-setting for Cookies. | 793 // Remove the preference to manage the default-content-setting for Cookies. |
828 prefs->RemoveManagedPref(prefs::kManagedDefaultCookiesSetting); | 794 prefs->RemoveManagedPref(prefs::kManagedDefaultCookiesSetting); |
829 EXPECT_EQ(CONTENT_SETTING_BLOCK, | 795 EXPECT_EQ(CONTENT_SETTING_BLOCK, |
830 host_content_settings_map->GetDefaultContentSetting( | 796 host_content_settings_map->GetDefaultContentSetting( |
831 CONTENT_SETTINGS_TYPE_COOKIES)); | 797 CONTENT_SETTINGS_TYPE_COOKIES)); |
832 } | 798 } |
833 | 799 |
834 // When a default-content-setting is set to a managed setting a | |
835 // CONTENT_SETTINGS_CHANGED notification should be fired. The same should happen | |
836 // if the managed setting is removed. | |
837 TEST_F(HostContentSettingsMapTest, ObserveManagedSettingsChange) { | |
838 TestingProfile profile; | |
839 HostContentSettingsMap* host_content_settings_map = | |
840 profile.GetHostContentSettingsMap(); | |
841 StubSettingsObserver observer; | |
842 TestingPrefService* prefs = profile.GetTestingPrefService(); | |
843 | |
844 // TODO(markusheintz): I think it would be better to send notifications only | |
845 // for a specific content-settings-type. | |
846 | |
847 // Set the managed default-content-setting. | |
848 prefs->SetManagedPref(prefs::kManagedDefaultImagesSetting, | |
849 Value::CreateIntegerValue(CONTENT_SETTING_BLOCK)); | |
850 EXPECT_EQ(host_content_settings_map, observer.last_notifier); | |
851 EXPECT_EQ(ContentSettingsPattern(), observer.last_pattern); | |
852 EXPECT_EQ(CONTENT_SETTINGS_TYPE_DEFAULT, observer.last_type); | |
853 EXPECT_TRUE(observer.last_update_all); | |
854 EXPECT_TRUE(observer.last_update_all_types); | |
855 EXPECT_EQ(1, observer.counter); | |
856 | |
857 // Remove the managed default-content-setting. | |
858 prefs->RemoveManagedPref(prefs::kManagedDefaultImagesSetting); | |
859 EXPECT_EQ(host_content_settings_map, observer.last_notifier); | |
860 EXPECT_EQ(CONTENT_SETTINGS_TYPE_DEFAULT, observer.last_type); | |
861 EXPECT_EQ(ContentSettingsPattern(), observer.last_pattern); | |
862 EXPECT_TRUE(observer.last_update_all); | |
863 EXPECT_TRUE(observer.last_update_all_types); | |
864 EXPECT_EQ(2, observer.counter); | |
865 } | |
866 | |
867 // When a default-content-setting is set to a managed setting a | |
868 // CONTENT_SETTINGS_CHANGED notification should be fired. The same should happen | |
869 // if the managed setting is removed. In this test-case the actual managed | |
870 // setting is the same. Just the managed status of the default-content-setting | |
871 // changes. | |
872 TEST_F(HostContentSettingsMapTest, ObserveManagedSettingsNoChange) { | |
873 TestingProfile profile; | |
874 HostContentSettingsMap* host_content_settings_map = | |
875 profile.GetHostContentSettingsMap(); | |
876 StubSettingsObserver observer; | |
877 TestingPrefService* prefs = profile.GetTestingPrefService(); | |
878 | |
879 // TODO(markusheintz): I think it would be better to send notifications only | |
880 // for a specific content-settings-type. | |
881 | |
882 // Set the managed default-content-setting. In this case the actual setting | |
883 // does not change. | |
884 prefs->SetManagedPref(prefs::kManagedDefaultImagesSetting, | |
885 Value::CreateIntegerValue(CONTENT_SETTING_ALLOW)); | |
886 EXPECT_EQ(host_content_settings_map, observer.last_notifier); | |
887 EXPECT_EQ(ContentSettingsPattern(), observer.last_pattern); | |
888 EXPECT_EQ(CONTENT_SETTINGS_TYPE_DEFAULT, observer.last_type); | |
889 EXPECT_TRUE(observer.last_update_all); | |
890 EXPECT_TRUE(observer.last_update_all_types); | |
891 EXPECT_EQ(1, observer.counter); | |
892 | |
893 // Remove the managed default-content-setting. | |
894 prefs->RemoveManagedPref(prefs::kManagedDefaultImagesSetting); | |
895 EXPECT_EQ(host_content_settings_map, observer.last_notifier); | |
896 EXPECT_EQ(CONTENT_SETTINGS_TYPE_DEFAULT, observer.last_type); | |
897 EXPECT_EQ(ContentSettingsPattern(), observer.last_pattern); | |
898 EXPECT_TRUE(observer.last_update_all); | |
899 EXPECT_TRUE(observer.last_update_all_types); | |
900 EXPECT_EQ(2, observer.counter); | |
901 } | |
902 | |
903 // If a setting for a default-content-setting-type is set while the type is | 800 // If a setting for a default-content-setting-type is set while the type is |
904 // managed, then the new setting should be preserved and used after the | 801 // managed, then the new setting should be preserved and used after the |
905 // default-content-setting-type is not managed anymore. | 802 // default-content-setting-type is not managed anymore. |
906 TEST_F(HostContentSettingsMapTest, SettingDefaultContentSettingsWhenManaged) { | 803 TEST_F(HostContentSettingsMapTest, SettingDefaultContentSettingsWhenManaged) { |
907 TestingProfile profile; | 804 TestingProfile profile; |
908 HostContentSettingsMap* host_content_settings_map = | 805 HostContentSettingsMap* host_content_settings_map = |
909 profile.GetHostContentSettingsMap(); | 806 profile.GetHostContentSettingsMap(); |
910 TestingPrefService* prefs = profile.GetTestingPrefService(); | 807 TestingPrefService* prefs = profile.GetTestingPrefService(); |
911 | 808 |
912 prefs->SetManagedPref(prefs::kManagedDefaultPluginsSetting, | 809 prefs->SetManagedPref(prefs::kManagedDefaultPluginsSetting, |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
949 EXPECT_TRUE(host_content_settings_map->BlockThirdPartyCookies()); | 846 EXPECT_TRUE(host_content_settings_map->BlockThirdPartyCookies()); |
950 | 847 |
951 // After unsetting the managed value for the preference BlockThirdPartyCookies | 848 // After unsetting the managed value for the preference BlockThirdPartyCookies |
952 // the default value should be returned now. | 849 // the default value should be returned now. |
953 prefs->RemoveManagedPref(prefs::kBlockThirdPartyCookies); | 850 prefs->RemoveManagedPref(prefs::kBlockThirdPartyCookies); |
954 EXPECT_FALSE(host_content_settings_map->IsBlockThirdPartyCookiesManaged()); | 851 EXPECT_FALSE(host_content_settings_map->IsBlockThirdPartyCookiesManaged()); |
955 EXPECT_FALSE(host_content_settings_map->BlockThirdPartyCookies()); | 852 EXPECT_FALSE(host_content_settings_map->BlockThirdPartyCookies()); |
956 } | 853 } |
957 | 854 |
958 } // namespace | 855 } // namespace |
OLD | NEW |