| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/content_settings_pref_provider.h" | 5 #include "chrome/browser/content_settings/content_settings_pref_provider.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 "chrome/browser/content_settings/mock_settings_observer.h" | 9 #include "chrome/browser/content_settings/mock_settings_observer.h" |
| 10 #include "chrome/browser/prefs/browser_prefs.h" | 10 #include "chrome/browser/prefs/browser_prefs.h" |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 #include "googleurl/src/gurl.h" | 23 #include "googleurl/src/gurl.h" |
| 24 #include "testing/gtest/include/gtest/gtest.h" | 24 #include "testing/gtest/include/gtest/gtest.h" |
| 25 | 25 |
| 26 using ::testing::_; | 26 using ::testing::_; |
| 27 | 27 |
| 28 namespace content_settings { | 28 namespace content_settings { |
| 29 | 29 |
| 30 class PrefDefaultProviderTest : public TestingBrowserProcessTest { | 30 class PrefDefaultProviderTest : public TestingBrowserProcessTest { |
| 31 public: | 31 public: |
| 32 PrefDefaultProviderTest() | 32 PrefDefaultProviderTest() |
| 33 : ui_thread_(BrowserThread::UI, &message_loop_) { | 33 : ui_thread_(BrowserThread::UI, &message_loop_), |
| 34 provider_(profile_.GetHostContentSettingsMap(), |
| 35 profile_.GetPrefs(), |
| 36 false) { |
| 37 } |
| 38 ~PrefDefaultProviderTest() { |
| 39 provider_.ShutdownOnUIThread(); |
| 34 } | 40 } |
| 35 | 41 |
| 36 protected: | 42 protected: |
| 37 MessageLoop message_loop_; | 43 MessageLoop message_loop_; |
| 38 BrowserThread ui_thread_; | 44 BrowserThread ui_thread_; |
| 45 TestingProfile profile_; |
| 46 content_settings::PrefDefaultProvider provider_; |
| 39 }; | 47 }; |
| 40 | 48 |
| 41 TEST_F(PrefDefaultProviderTest, DefaultValues) { | 49 TEST_F(PrefDefaultProviderTest, DefaultValues) { |
| 42 TestingProfile profile; | |
| 43 content_settings::PrefDefaultProvider provider(&profile); | |
| 44 | |
| 45 ASSERT_FALSE( | 50 ASSERT_FALSE( |
| 46 provider.DefaultSettingIsManaged(CONTENT_SETTINGS_TYPE_COOKIES)); | 51 provider_.DefaultSettingIsManaged(CONTENT_SETTINGS_TYPE_COOKIES)); |
| 47 | 52 |
| 48 // Check setting defaults. | 53 // Check setting defaults. |
| 49 EXPECT_EQ(CONTENT_SETTING_ALLOW, | 54 EXPECT_EQ(CONTENT_SETTING_ALLOW, |
| 50 provider.ProvideDefaultSetting(CONTENT_SETTINGS_TYPE_COOKIES)); | 55 provider_.ProvideDefaultSetting(CONTENT_SETTINGS_TYPE_COOKIES)); |
| 51 provider.UpdateDefaultSetting(CONTENT_SETTINGS_TYPE_COOKIES, | 56 provider_.UpdateDefaultSetting(CONTENT_SETTINGS_TYPE_COOKIES, |
| 52 CONTENT_SETTING_BLOCK); | 57 CONTENT_SETTING_BLOCK); |
| 53 EXPECT_EQ(CONTENT_SETTING_BLOCK, | 58 EXPECT_EQ(CONTENT_SETTING_BLOCK, |
| 54 provider.ProvideDefaultSetting(CONTENT_SETTINGS_TYPE_COOKIES)); | 59 provider_.ProvideDefaultSetting(CONTENT_SETTINGS_TYPE_COOKIES)); |
| 55 provider.ResetToDefaults(); | 60 provider_.ResetToDefaults(); |
| 56 EXPECT_EQ(CONTENT_SETTING_ALLOW, | 61 EXPECT_EQ(CONTENT_SETTING_ALLOW, |
| 57 provider.ProvideDefaultSetting(CONTENT_SETTINGS_TYPE_COOKIES)); | 62 provider_.ProvideDefaultSetting(CONTENT_SETTINGS_TYPE_COOKIES)); |
| 58 } | 63 } |
| 59 | 64 |
| 60 TEST_F(PrefDefaultProviderTest, Observer) { | 65 TEST_F(PrefDefaultProviderTest, Observer) { |
| 61 TestingProfile profile; | |
| 62 PrefDefaultProvider provider(&profile); | |
| 63 MockSettingsObserver observer; | 66 MockSettingsObserver observer; |
| 64 | 67 |
| 65 EXPECT_CALL(observer, | 68 EXPECT_CALL(observer, |
| 66 OnContentSettingsChanged(profile.GetHostContentSettingsMap(), | 69 OnContentSettingsChanged(profile_.GetHostContentSettingsMap(), |
| 67 CONTENT_SETTINGS_TYPE_IMAGES, false, | 70 CONTENT_SETTINGS_TYPE_IMAGES, false, |
| 68 _, _, true)); | 71 _, _, true)); |
| 69 // Expect a second call because the PrefDefaultProvider in the TestingProfile | 72 // Expect a second call because the PrefDefaultProvider in the TestingProfile |
| 70 // also observes the default content settings preference. | 73 // also observes the default content settings preference. |
| 71 EXPECT_CALL(observer, | 74 EXPECT_CALL(observer, |
| 72 OnContentSettingsChanged(profile.GetHostContentSettingsMap(), | 75 OnContentSettingsChanged(profile_.GetHostContentSettingsMap(), |
| 73 CONTENT_SETTINGS_TYPE_DEFAULT, true, | 76 CONTENT_SETTINGS_TYPE_DEFAULT, true, |
| 74 _, _, true)); | 77 _, _, true)); |
| 75 provider.UpdateDefaultSetting( | 78 provider_.UpdateDefaultSetting( |
| 76 CONTENT_SETTINGS_TYPE_IMAGES, CONTENT_SETTING_BLOCK); | 79 CONTENT_SETTINGS_TYPE_IMAGES, CONTENT_SETTING_BLOCK); |
| 77 } | 80 } |
| 78 | 81 |
| 79 TEST_F(PrefDefaultProviderTest, ObserveDefaultPref) { | 82 TEST_F(PrefDefaultProviderTest, ObserveDefaultPref) { |
| 80 TestingProfile profile; | 83 PrefService* prefs = profile_.GetPrefs(); |
| 81 PrefDefaultProvider provider(&profile); | |
| 82 | |
| 83 PrefService* prefs = profile.GetPrefs(); | |
| 84 | 84 |
| 85 // Make a copy of the default pref value so we can reset it later. | 85 // Make a copy of the default pref value so we can reset it later. |
| 86 scoped_ptr<Value> default_value(prefs->FindPreference( | 86 scoped_ptr<Value> default_value(prefs->FindPreference( |
| 87 prefs::kDefaultContentSettings)->GetValue()->DeepCopy()); | 87 prefs::kDefaultContentSettings)->GetValue()->DeepCopy()); |
| 88 | 88 |
| 89 provider.UpdateDefaultSetting( | 89 provider_.UpdateDefaultSetting( |
| 90 CONTENT_SETTINGS_TYPE_COOKIES, CONTENT_SETTING_BLOCK); | 90 CONTENT_SETTINGS_TYPE_COOKIES, CONTENT_SETTING_BLOCK); |
| 91 EXPECT_EQ(CONTENT_SETTING_BLOCK, | 91 EXPECT_EQ(CONTENT_SETTING_BLOCK, |
| 92 provider.ProvideDefaultSetting(CONTENT_SETTINGS_TYPE_COOKIES)); | 92 provider_.ProvideDefaultSetting(CONTENT_SETTINGS_TYPE_COOKIES)); |
| 93 | 93 |
| 94 // Make a copy of the pref's new value so we can reset it later. | 94 // Make a copy of the pref's new value so we can reset it later. |
| 95 scoped_ptr<Value> new_value(prefs->FindPreference( | 95 scoped_ptr<Value> new_value(prefs->FindPreference( |
| 96 prefs::kDefaultContentSettings)->GetValue()->DeepCopy()); | 96 prefs::kDefaultContentSettings)->GetValue()->DeepCopy()); |
| 97 | 97 |
| 98 // Clearing the backing pref should also clear the internal cache. | 98 // Clearing the backing pref should also clear the internal cache. |
| 99 prefs->Set(prefs::kDefaultContentSettings, *default_value); | 99 prefs->Set(prefs::kDefaultContentSettings, *default_value); |
| 100 EXPECT_EQ(CONTENT_SETTING_ALLOW, | 100 EXPECT_EQ(CONTENT_SETTING_ALLOW, |
| 101 provider.ProvideDefaultSetting(CONTENT_SETTINGS_TYPE_COOKIES)); | 101 provider_.ProvideDefaultSetting(CONTENT_SETTINGS_TYPE_COOKIES)); |
| 102 | 102 |
| 103 // Reseting the pref to its previous value should update the cache. | 103 // Reseting the pref to its previous value should update the cache. |
| 104 prefs->Set(prefs::kDefaultContentSettings, *new_value); | 104 prefs->Set(prefs::kDefaultContentSettings, *new_value); |
| 105 EXPECT_EQ(CONTENT_SETTING_BLOCK, | 105 EXPECT_EQ(CONTENT_SETTING_BLOCK, |
| 106 provider.ProvideDefaultSetting(CONTENT_SETTINGS_TYPE_COOKIES)); | 106 provider_.ProvideDefaultSetting(CONTENT_SETTINGS_TYPE_COOKIES)); |
| 107 } | 107 } |
| 108 | 108 |
| 109 TEST_F(PrefDefaultProviderTest, OffTheRecord) { | 109 TEST_F(PrefDefaultProviderTest, OffTheRecord) { |
| 110 TestingProfile profile; | 110 PrefDefaultProvider otr_provider(profile_.GetHostContentSettingsMap(), |
| 111 PrefDefaultProvider provider(&profile); | 111 profile_.GetPrefs(), |
| 112 | 112 true); |
| 113 profile.set_incognito(true); | |
| 114 PrefDefaultProvider otr_provider(&profile); | |
| 115 profile.set_incognito(false); | |
| 116 | 113 |
| 117 EXPECT_EQ(CONTENT_SETTING_ALLOW, | 114 EXPECT_EQ(CONTENT_SETTING_ALLOW, |
| 118 provider.ProvideDefaultSetting(CONTENT_SETTINGS_TYPE_COOKIES)); | 115 provider_.ProvideDefaultSetting(CONTENT_SETTINGS_TYPE_COOKIES)); |
| 119 EXPECT_EQ(CONTENT_SETTING_ALLOW, | 116 EXPECT_EQ(CONTENT_SETTING_ALLOW, |
| 120 otr_provider.ProvideDefaultSetting(CONTENT_SETTINGS_TYPE_COOKIES)); | 117 otr_provider.ProvideDefaultSetting(CONTENT_SETTINGS_TYPE_COOKIES)); |
| 121 | 118 |
| 122 // Changing content settings on the main provider should also affect the | 119 // Changing content settings on the main provider should also affect the |
| 123 // incognito map. | 120 // incognito map. |
| 124 provider.UpdateDefaultSetting(CONTENT_SETTINGS_TYPE_COOKIES, | 121 provider_.UpdateDefaultSetting(CONTENT_SETTINGS_TYPE_COOKIES, |
| 125 CONTENT_SETTING_BLOCK); | 122 CONTENT_SETTING_BLOCK); |
| 126 EXPECT_EQ(CONTENT_SETTING_BLOCK, | 123 EXPECT_EQ(CONTENT_SETTING_BLOCK, |
| 127 provider.ProvideDefaultSetting(CONTENT_SETTINGS_TYPE_COOKIES)); | 124 provider_.ProvideDefaultSetting(CONTENT_SETTINGS_TYPE_COOKIES)); |
| 128 EXPECT_EQ(CONTENT_SETTING_BLOCK, | 125 EXPECT_EQ(CONTENT_SETTING_BLOCK, |
| 129 otr_provider.ProvideDefaultSetting(CONTENT_SETTINGS_TYPE_COOKIES)); | 126 otr_provider.ProvideDefaultSetting(CONTENT_SETTINGS_TYPE_COOKIES)); |
| 130 | 127 |
| 131 // Changing content settings on the incognito provider should be ignored. | 128 // Changing content settings on the incognito provider should be ignored. |
| 132 otr_provider.UpdateDefaultSetting(CONTENT_SETTINGS_TYPE_COOKIES, | 129 otr_provider.UpdateDefaultSetting(CONTENT_SETTINGS_TYPE_COOKIES, |
| 133 CONTENT_SETTING_ALLOW); | 130 CONTENT_SETTING_ALLOW); |
| 134 EXPECT_EQ(CONTENT_SETTING_BLOCK, | 131 EXPECT_EQ(CONTENT_SETTING_BLOCK, |
| 135 provider.ProvideDefaultSetting(CONTENT_SETTINGS_TYPE_COOKIES)); | 132 provider_.ProvideDefaultSetting(CONTENT_SETTINGS_TYPE_COOKIES)); |
| 136 EXPECT_EQ(CONTENT_SETTING_BLOCK, | 133 EXPECT_EQ(CONTENT_SETTING_BLOCK, |
| 137 otr_provider.ProvideDefaultSetting(CONTENT_SETTINGS_TYPE_COOKIES)); | 134 otr_provider.ProvideDefaultSetting(CONTENT_SETTINGS_TYPE_COOKIES)); |
| 135 |
| 136 otr_provider.ShutdownOnUIThread(); |
| 138 } | 137 } |
| 139 | 138 |
| 140 // //////////////////////////////////////////////////////////////////////////// | 139 // //////////////////////////////////////////////////////////////////////////// |
| 141 // PrefProviderTest | 140 // PrefProviderTest |
| 142 // | 141 // |
| 143 | 142 |
| 144 bool SettingsEqual(const ContentSettings& settings1, | 143 bool SettingsEqual(const ContentSettings& settings1, |
| 145 const ContentSettings& settings2) { | 144 const ContentSettings& settings2) { |
| 146 for (int i = 0; i < CONTENT_SETTINGS_NUM_TYPES; ++i) { | 145 for (int i = 0; i < CONTENT_SETTINGS_NUM_TYPES; ++i) { |
| 147 if (settings1.settings[i] != settings2.settings[i]) | 146 if (settings1.settings[i] != settings2.settings[i]) |
| 148 return false; | 147 return false; |
| 149 } | 148 } |
| 150 return true; | 149 return true; |
| 151 } | 150 } |
| 152 | 151 |
| 153 class PrefProviderTest : public TestingBrowserProcessTest { | 152 class PrefProviderTest : public TestingBrowserProcessTest { |
| 154 public: | 153 public: |
| 155 PrefProviderTest() : ui_thread_( | 154 PrefProviderTest() : ui_thread_( |
| 156 BrowserThread::UI, &message_loop_) { | 155 BrowserThread::UI, &message_loop_) { |
| 157 } | 156 } |
| 158 | 157 |
| 159 protected: | 158 protected: |
| 160 MessageLoop message_loop_; | 159 MessageLoop message_loop_; |
| 161 BrowserThread ui_thread_; | 160 BrowserThread ui_thread_; |
| 162 }; | 161 }; |
| 163 | 162 |
| 164 TEST_F(PrefProviderTest, Observer) { | 163 TEST_F(PrefProviderTest, Observer) { |
| 165 TestingProfile profile; | 164 TestingProfile profile; |
| 166 // Get the |HostContentSettingsMap| one time in order to initialize it. | 165 PrefProvider pref_content_settings_provider( |
| 167 // Otherwise we end up in a dead lock when the |PrefProvider| tries to notify | 166 profile.GetHostContentSettingsMap(), profile.GetPrefs(), false); |
| 168 // content settings change observers. The |PrefProvider| set's the | |
| 169 // |HostContentSettingsMap| as notification source and would trigger in | |
| 170 // infinite recursive instantiation loop. | |
| 171 // TODO(markusheintz): Let the HostContentSettingsMap sent out notifications. | |
| 172 // Providers and HostContentSettingsMap should communicate via a observer | |
| 173 // pattern. | |
| 174 profile.GetHostContentSettingsMap(); | |
| 175 Profile* p = &profile; | |
| 176 PrefProvider pref_content_settings_provider(p); | |
| 177 MockSettingsObserver observer; | 167 MockSettingsObserver observer; |
| 178 ContentSettingsPattern pattern = | 168 ContentSettingsPattern pattern = |
| 179 ContentSettingsPattern::FromString("[*.]example.com"); | 169 ContentSettingsPattern::FromString("[*.]example.com"); |
| 180 | 170 |
| 181 // There are two PrefProvider instances, one in the host content settings map | 171 // There are two PrefProvider instances, one in the host content settings map |
| 182 // and one instance that is created here. Setting a content setting will fire | 172 // and one instance that is created here. Setting a content setting will fire |
| 183 // one notification. This will change the underlying preferences which will | 173 // one notification. This will change the underlying preferences which will |
| 184 // cause the second PrefProvider instance to reload these and to | 174 // cause the second PrefProvider instance to reload these and to |
| 185 // sync the obsolete prefences. Hence we get two more notifications which we | 175 // sync the obsolete prefences. Hence we get two more notifications which we |
| 186 // will not get in the real world. | 176 // will not get in the real world. |
| 187 EXPECT_CALL(observer, | 177 EXPECT_CALL(observer, |
| 188 OnContentSettingsChanged(profile.GetHostContentSettingsMap(), | 178 OnContentSettingsChanged(profile.GetHostContentSettingsMap(), |
| 189 CONTENT_SETTINGS_TYPE_IMAGES, | 179 CONTENT_SETTINGS_TYPE_IMAGES, |
| 190 false, | 180 false, |
| 191 pattern, | 181 pattern, |
| 192 ContentSettingsPattern::Wildcard(), | 182 ContentSettingsPattern::Wildcard(), |
| 193 false)); | 183 false)); |
| 194 EXPECT_CALL(observer, | 184 EXPECT_CALL(observer, |
| 195 OnContentSettingsChanged(profile.GetHostContentSettingsMap(), | 185 OnContentSettingsChanged(profile.GetHostContentSettingsMap(), |
| 196 CONTENT_SETTINGS_TYPE_DEFAULT, true, | 186 CONTENT_SETTINGS_TYPE_DEFAULT, true, |
| 197 _, _, true)).Times(2); | 187 _, _, true)).Times(2); |
| 198 pref_content_settings_provider.SetContentSetting( | 188 pref_content_settings_provider.SetContentSetting( |
| 199 pattern, | 189 pattern, |
| 200 ContentSettingsPattern::Wildcard(), | 190 ContentSettingsPattern::Wildcard(), |
| 201 CONTENT_SETTINGS_TYPE_IMAGES, | 191 CONTENT_SETTINGS_TYPE_IMAGES, |
| 202 "", | 192 "", |
| 203 CONTENT_SETTING_ALLOW); | 193 CONTENT_SETTING_ALLOW); |
| 194 |
| 195 pref_content_settings_provider.ShutdownOnUIThread(); |
| 204 } | 196 } |
| 205 | 197 |
| 206 // Test for regression in which the PrefProvider modified the user pref store | 198 // Test for regression in which the PrefProvider modified the user pref store |
| 207 // of the OTR unintentionally: http://crbug.com/74466. | 199 // of the OTR unintentionally: http://crbug.com/74466. |
| 208 TEST_F(PrefProviderTest, Incognito) { | 200 TEST_F(PrefProviderTest, Incognito) { |
| 209 PersistentPrefStore* user_prefs = new TestingPrefStore(); | 201 PersistentPrefStore* user_prefs = new TestingPrefStore(); |
| 210 OverlayPersistentPrefStore* otr_user_prefs = | 202 OverlayPersistentPrefStore* otr_user_prefs = |
| 211 new OverlayPersistentPrefStore(user_prefs); | 203 new OverlayPersistentPrefStore(user_prefs); |
| 212 | 204 |
| 213 PrefServiceMockBuilder builder; | 205 PrefServiceMockBuilder builder; |
| 214 PrefService* regular_prefs = builder.WithUserPrefs(user_prefs).Create(); | 206 PrefService* regular_prefs = builder.WithUserPrefs(user_prefs).Create(); |
| 215 | 207 |
| 216 Profile::RegisterUserPrefs(regular_prefs); | 208 Profile::RegisterUserPrefs(regular_prefs); |
| 217 browser::RegisterUserPrefs(regular_prefs); | 209 browser::RegisterUserPrefs(regular_prefs); |
| 218 | 210 |
| 219 PrefService* otr_prefs = builder.WithUserPrefs(otr_user_prefs).Create(); | 211 PrefService* otr_prefs = builder.WithUserPrefs(otr_user_prefs).Create(); |
| 220 | 212 |
| 221 Profile::RegisterUserPrefs(otr_prefs); | 213 Profile::RegisterUserPrefs(otr_prefs); |
| 222 browser::RegisterUserPrefs(otr_prefs); | 214 browser::RegisterUserPrefs(otr_prefs); |
| 223 | 215 |
| 224 TestingProfile profile; | 216 TestingProfile profile; |
| 225 TestingProfile* otr_profile = new TestingProfile; | 217 TestingProfile* otr_profile = new TestingProfile; |
| 226 profile.SetOffTheRecordProfile(otr_profile); | 218 profile.SetOffTheRecordProfile(otr_profile); |
| 227 profile.SetPrefService(regular_prefs); | 219 profile.SetPrefService(regular_prefs); |
| 228 otr_profile->set_incognito(true); | 220 otr_profile->set_incognito(true); |
| 229 otr_profile->SetPrefService(otr_prefs); | 221 otr_profile->SetPrefService(otr_prefs); |
| 230 profile.GetHostContentSettingsMap(); | 222 HostContentSettingsMap* map = profile.GetHostContentSettingsMap(); |
| 231 | 223 |
| 232 PrefProvider pref_content_settings_provider(&profile); | 224 PrefProvider pref_content_settings_provider(map, regular_prefs, false); |
| 233 PrefProvider pref_content_settings_provider_incognito(otr_profile); | 225 PrefProvider pref_content_settings_provider_incognito( |
| 226 otr_profile->GetHostContentSettingsMap(), otr_prefs, true); |
| 234 ContentSettingsPattern pattern = | 227 ContentSettingsPattern pattern = |
| 235 ContentSettingsPattern::FromString("[*.]example.com"); | 228 ContentSettingsPattern::FromString("[*.]example.com"); |
| 236 pref_content_settings_provider.SetContentSetting( | 229 pref_content_settings_provider.SetContentSetting( |
| 237 pattern, | 230 pattern, |
| 238 pattern, | 231 pattern, |
| 239 CONTENT_SETTINGS_TYPE_IMAGES, | 232 CONTENT_SETTINGS_TYPE_IMAGES, |
| 240 "", | 233 "", |
| 241 CONTENT_SETTING_ALLOW); | 234 CONTENT_SETTING_ALLOW); |
| 242 | 235 |
| 243 GURL host("http://example.com/"); | 236 GURL host("http://example.com/"); |
| 244 // The value should of course be visible in the regular PrefProvider. | 237 // The value should of course be visible in the regular PrefProvider. |
| 245 EXPECT_EQ(CONTENT_SETTING_ALLOW, | 238 EXPECT_EQ(CONTENT_SETTING_ALLOW, |
| 246 pref_content_settings_provider.GetContentSetting( | 239 pref_content_settings_provider.GetContentSetting( |
| 247 host, host, CONTENT_SETTINGS_TYPE_IMAGES, "")); | 240 host, host, CONTENT_SETTINGS_TYPE_IMAGES, "")); |
| 248 // And also in the OTR version. | 241 // And also in the OTR version. |
| 249 EXPECT_EQ(CONTENT_SETTING_ALLOW, | 242 EXPECT_EQ(CONTENT_SETTING_ALLOW, |
| 250 pref_content_settings_provider_incognito.GetContentSetting( | 243 pref_content_settings_provider_incognito.GetContentSetting( |
| 251 host, host, CONTENT_SETTINGS_TYPE_IMAGES, "")); | 244 host, host, CONTENT_SETTINGS_TYPE_IMAGES, "")); |
| 252 // But the value should not be overridden in the OTR user prefs accidentally. | 245 // But the value should not be overridden in the OTR user prefs accidentally. |
| 253 EXPECT_FALSE(otr_user_prefs->IsSetInOverlay(prefs::kContentSettingsPatterns)); | 246 EXPECT_FALSE(otr_user_prefs->IsSetInOverlay(prefs::kContentSettingsPatterns)); |
| 247 |
| 248 pref_content_settings_provider.ShutdownOnUIThread(); |
| 249 pref_content_settings_provider_incognito.ShutdownOnUIThread(); |
| 254 } | 250 } |
| 255 | 251 |
| 256 TEST_F(PrefProviderTest, Patterns) { | 252 TEST_F(PrefProviderTest, Patterns) { |
| 257 TestingProfile testing_profile; | 253 TestingProfile testing_profile; |
| 258 testing_profile.GetHostContentSettingsMap(); | |
| 259 PrefProvider pref_content_settings_provider( | 254 PrefProvider pref_content_settings_provider( |
| 260 testing_profile.GetOriginalProfile()); | 255 testing_profile.GetHostContentSettingsMap(), |
| 256 testing_profile.GetPrefs(), false); |
| 261 | 257 |
| 262 GURL host1("http://example.com/"); | 258 GURL host1("http://example.com/"); |
| 263 GURL host2("http://www.example.com/"); | 259 GURL host2("http://www.example.com/"); |
| 264 GURL host3("http://example.org/"); | 260 GURL host3("http://example.org/"); |
| 265 GURL host4("file:///tmp/test.html"); | 261 GURL host4("file:///tmp/test.html"); |
| 266 ContentSettingsPattern pattern1 = | 262 ContentSettingsPattern pattern1 = |
| 267 ContentSettingsPattern::FromString("[*.]example.com"); | 263 ContentSettingsPattern::FromString("[*.]example.com"); |
| 268 ContentSettingsPattern pattern2 = | 264 ContentSettingsPattern pattern2 = |
| 269 ContentSettingsPattern::FromString("example.org"); | 265 ContentSettingsPattern::FromString("example.org"); |
| 270 ContentSettingsPattern pattern3 = | 266 ContentSettingsPattern pattern3 = |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 304 host4, host4, CONTENT_SETTINGS_TYPE_IMAGES, "")); | 300 host4, host4, CONTENT_SETTINGS_TYPE_IMAGES, "")); |
| 305 pref_content_settings_provider.SetContentSetting( | 301 pref_content_settings_provider.SetContentSetting( |
| 306 pattern3, | 302 pattern3, |
| 307 pattern3, | 303 pattern3, |
| 308 CONTENT_SETTINGS_TYPE_IMAGES, | 304 CONTENT_SETTINGS_TYPE_IMAGES, |
| 309 "", | 305 "", |
| 310 CONTENT_SETTING_BLOCK); | 306 CONTENT_SETTING_BLOCK); |
| 311 EXPECT_EQ(CONTENT_SETTING_BLOCK, | 307 EXPECT_EQ(CONTENT_SETTING_BLOCK, |
| 312 pref_content_settings_provider.GetContentSetting( | 308 pref_content_settings_provider.GetContentSetting( |
| 313 host4, host4, CONTENT_SETTINGS_TYPE_IMAGES, "")); | 309 host4, host4, CONTENT_SETTINGS_TYPE_IMAGES, "")); |
| 310 |
| 311 pref_content_settings_provider.ShutdownOnUIThread(); |
| 314 } | 312 } |
| 315 | 313 |
| 316 TEST_F(PrefProviderTest, ResourceIdentifier) { | 314 TEST_F(PrefProviderTest, ResourceIdentifier) { |
| 317 // This feature is currently behind a flag. | 315 // This feature is currently behind a flag. |
| 318 CommandLine* cmd = CommandLine::ForCurrentProcess(); | 316 CommandLine* cmd = CommandLine::ForCurrentProcess(); |
| 319 AutoReset<CommandLine> auto_reset(cmd, *cmd); | 317 AutoReset<CommandLine> auto_reset(cmd, *cmd); |
| 320 cmd->AppendSwitch(switches::kEnableResourceContentSettings); | 318 cmd->AppendSwitch(switches::kEnableResourceContentSettings); |
| 321 | 319 |
| 322 TestingProfile testing_profile; | 320 TestingProfile testing_profile; |
| 323 testing_profile.GetHostContentSettingsMap(); | |
| 324 PrefProvider pref_content_settings_provider( | 321 PrefProvider pref_content_settings_provider( |
| 325 testing_profile.GetOriginalProfile()); | 322 testing_profile.GetHostContentSettingsMap(), |
| 323 testing_profile.GetPrefs(), |
| 324 false); |
| 326 | 325 |
| 327 GURL host("http://example.com/"); | 326 GURL host("http://example.com/"); |
| 328 ContentSettingsPattern pattern = | 327 ContentSettingsPattern pattern = |
| 329 ContentSettingsPattern::FromString("[*.]example.com"); | 328 ContentSettingsPattern::FromString("[*.]example.com"); |
| 330 std::string resource1("someplugin"); | 329 std::string resource1("someplugin"); |
| 331 std::string resource2("otherplugin"); | 330 std::string resource2("otherplugin"); |
| 332 | 331 |
| 333 EXPECT_EQ(CONTENT_SETTING_DEFAULT, | 332 EXPECT_EQ(CONTENT_SETTING_DEFAULT, |
| 334 pref_content_settings_provider.GetContentSetting( | 333 pref_content_settings_provider.GetContentSetting( |
| 335 host, host, CONTENT_SETTINGS_TYPE_PLUGINS, resource1)); | 334 host, host, CONTENT_SETTINGS_TYPE_PLUGINS, resource1)); |
| 336 pref_content_settings_provider.SetContentSetting( | 335 pref_content_settings_provider.SetContentSetting( |
| 337 pattern, | 336 pattern, |
| 338 pattern, | 337 pattern, |
| 339 CONTENT_SETTINGS_TYPE_PLUGINS, | 338 CONTENT_SETTINGS_TYPE_PLUGINS, |
| 340 resource1, | 339 resource1, |
| 341 CONTENT_SETTING_BLOCK); | 340 CONTENT_SETTING_BLOCK); |
| 342 EXPECT_EQ(CONTENT_SETTING_BLOCK, | 341 EXPECT_EQ(CONTENT_SETTING_BLOCK, |
| 343 pref_content_settings_provider.GetContentSetting( | 342 pref_content_settings_provider.GetContentSetting( |
| 344 host, host, CONTENT_SETTINGS_TYPE_PLUGINS, resource1)); | 343 host, host, CONTENT_SETTINGS_TYPE_PLUGINS, resource1)); |
| 345 EXPECT_EQ(CONTENT_SETTING_DEFAULT, | 344 EXPECT_EQ(CONTENT_SETTING_DEFAULT, |
| 346 pref_content_settings_provider.GetContentSetting( | 345 pref_content_settings_provider.GetContentSetting( |
| 347 host, host, CONTENT_SETTINGS_TYPE_PLUGINS, resource2)); | 346 host, host, CONTENT_SETTINGS_TYPE_PLUGINS, resource2)); |
| 347 |
| 348 pref_content_settings_provider.ShutdownOnUIThread(); |
| 348 } | 349 } |
| 349 | 350 |
| 350 TEST_F(PrefProviderTest, MigrateSinglePatternSettings) { | 351 TEST_F(PrefProviderTest, MigrateSinglePatternSettings) { |
| 351 // Setup single pattern settings. | 352 // Setup single pattern settings. |
| 352 TestingProfile profile; | 353 TestingProfile profile; |
| 353 PrefService* prefs = profile.GetPrefs(); | 354 PrefService* prefs = profile.GetPrefs(); |
| 354 | 355 |
| 355 DictionaryValue* settings_dictionary = new DictionaryValue(); | 356 DictionaryValue* settings_dictionary = new DictionaryValue(); |
| 356 settings_dictionary->SetInteger("cookies", 2); | 357 settings_dictionary->SetInteger("cookies", 2); |
| 357 settings_dictionary->SetInteger("images", 2); | 358 settings_dictionary->SetInteger("images", 2); |
| 358 settings_dictionary->SetInteger("popups", 2); | 359 settings_dictionary->SetInteger("popups", 2); |
| 359 | 360 |
| 360 ContentSettingsPattern pattern = | 361 ContentSettingsPattern pattern = |
| 361 ContentSettingsPattern::FromString("http://www.example.com"); | 362 ContentSettingsPattern::FromString("http://www.example.com"); |
| 362 scoped_ptr<DictionaryValue> all_settings_dictionary(new DictionaryValue()); | 363 scoped_ptr<DictionaryValue> all_settings_dictionary(new DictionaryValue()); |
| 363 all_settings_dictionary->SetWithoutPathExpansion( | 364 all_settings_dictionary->SetWithoutPathExpansion( |
| 364 pattern.ToString(), settings_dictionary); | 365 pattern.ToString(), settings_dictionary); |
| 365 | 366 |
| 366 // Set Obsolete preference. | 367 // Set Obsolete preference. |
| 367 prefs->Set(prefs::kContentSettingsPatterns, *all_settings_dictionary); | 368 prefs->Set(prefs::kContentSettingsPatterns, *all_settings_dictionary); |
| 368 | 369 |
| 369 // Test if single pattern settings are properly migrated. | 370 // Test if single pattern settings are properly migrated. |
| 370 content_settings::PrefProvider provider(profile.GetOriginalProfile()); | 371 content_settings::PrefProvider provider(profile.GetHostContentSettingsMap(), |
| 372 prefs, false); |
| 371 | 373 |
| 372 // Validate migrated preferences | 374 // Validate migrated preferences |
| 373 const DictionaryValue* const_all_settings_dictionary = | 375 const DictionaryValue* const_all_settings_dictionary = |
| 374 prefs->GetDictionary(prefs::kContentSettingsPatternPairs); | 376 prefs->GetDictionary(prefs::kContentSettingsPatternPairs); |
| 375 EXPECT_EQ(1U, const_all_settings_dictionary->size()); | 377 EXPECT_EQ(1U, const_all_settings_dictionary->size()); |
| 376 EXPECT_FALSE(const_all_settings_dictionary->HasKey(pattern.ToString())); | 378 EXPECT_FALSE(const_all_settings_dictionary->HasKey(pattern.ToString())); |
| 377 EXPECT_TRUE(const_all_settings_dictionary->HasKey( | 379 EXPECT_TRUE(const_all_settings_dictionary->HasKey( |
| 378 pattern.ToString() + "," + | 380 pattern.ToString() + "," + |
| 379 ContentSettingsPattern::Wildcard().ToString())); | 381 ContentSettingsPattern::Wildcard().ToString())); |
| 380 | 382 |
| 381 EXPECT_EQ(CONTENT_SETTING_BLOCK, provider.GetContentSetting( | 383 EXPECT_EQ(CONTENT_SETTING_BLOCK, provider.GetContentSetting( |
| 382 GURL("http://www.example.com"), | 384 GURL("http://www.example.com"), |
| 383 GURL("http://www.example.com"), | 385 GURL("http://www.example.com"), |
| 384 CONTENT_SETTINGS_TYPE_IMAGES, | 386 CONTENT_SETTINGS_TYPE_IMAGES, |
| 385 "")); | 387 "")); |
| 386 | 388 |
| 387 EXPECT_EQ(CONTENT_SETTING_BLOCK, provider.GetContentSetting( | 389 EXPECT_EQ(CONTENT_SETTING_BLOCK, provider.GetContentSetting( |
| 388 GURL("http://www.example.com"), | 390 GURL("http://www.example.com"), |
| 389 GURL("http://www.example.com"), | 391 GURL("http://www.example.com"), |
| 390 CONTENT_SETTINGS_TYPE_POPUPS, | 392 CONTENT_SETTINGS_TYPE_POPUPS, |
| 391 "")); | 393 "")); |
| 394 |
| 395 provider.ShutdownOnUIThread(); |
| 392 } | 396 } |
| 393 | 397 |
| 394 } // namespace content_settings | 398 } // namespace content_settings |
| OLD | NEW |