- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / content_settings / host_content_settings_map_unittest.cc
1 // Copyright (c) 2012 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 "base/auto_reset.h"
6 #include "base/command_line.h"
7 #include "base/json/json_reader.h"
8 #include "base/json/json_writer.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/prefs/pref_service.h"
11 #include "base/prefs/scoped_user_pref_update.h"
12 #include "chrome/browser/content_settings/content_settings_details.h"
13 #include "chrome/browser/content_settings/cookie_settings.h"
14 #include "chrome/browser/content_settings/host_content_settings_map.h"
15 #include "chrome/browser/content_settings/mock_settings_observer.h"
16 #include "chrome/common/chrome_switches.h"
17 #include "chrome/common/pref_names.h"
18 #include "chrome/common/url_constants.h"
19 #include "chrome/test/base/testing_pref_service_syncable.h"
20 #include "chrome/test/base/testing_profile.h"
21 #include "content/public/test/test_browser_thread.h"
22 #include "net/base/static_cookie_policy.h"
23 #include "testing/gtest/include/gtest/gtest.h"
24 #include "url/gurl.h"
25
26 using content::BrowserThread;
27
28 using ::testing::_;
29
30 class HostContentSettingsMapTest : public testing::Test {
31  public:
32   HostContentSettingsMapTest() : ui_thread_(BrowserThread::UI, &message_loop_) {
33   }
34
35  protected:
36   base::MessageLoop message_loop_;
37   content::TestBrowserThread ui_thread_;
38 };
39
40 TEST_F(HostContentSettingsMapTest, DefaultValues) {
41   TestingProfile profile;
42   HostContentSettingsMap* host_content_settings_map =
43       profile.GetHostContentSettingsMap();
44
45   // Check setting defaults.
46   EXPECT_EQ(CONTENT_SETTING_ALLOW,
47             host_content_settings_map->GetDefaultContentSetting(
48                 CONTENT_SETTINGS_TYPE_JAVASCRIPT, NULL));
49   host_content_settings_map->SetDefaultContentSetting(
50       CONTENT_SETTINGS_TYPE_IMAGES, CONTENT_SETTING_BLOCK);
51   EXPECT_EQ(CONTENT_SETTING_BLOCK,
52             host_content_settings_map->GetDefaultContentSetting(
53                 CONTENT_SETTINGS_TYPE_IMAGES, NULL));
54   EXPECT_EQ(CONTENT_SETTING_ALLOW, host_content_settings_map->GetContentSetting(
55                 GURL(chrome::kChromeUINewTabURL),
56                 GURL(chrome::kChromeUINewTabURL),
57                 CONTENT_SETTINGS_TYPE_IMAGES,
58                 std::string()));
59   {
60     host_content_settings_map->SetDefaultContentSetting(
61         CONTENT_SETTINGS_TYPE_PLUGINS, CONTENT_SETTING_ASK);
62     EXPECT_EQ(CONTENT_SETTING_ASK,
63               host_content_settings_map->GetDefaultContentSetting(
64                   CONTENT_SETTINGS_TYPE_PLUGINS, NULL));
65   }
66   host_content_settings_map->SetDefaultContentSetting(
67       CONTENT_SETTINGS_TYPE_POPUPS, CONTENT_SETTING_ALLOW);
68   EXPECT_EQ(CONTENT_SETTING_ALLOW,
69             host_content_settings_map->GetDefaultContentSetting(
70                 CONTENT_SETTINGS_TYPE_POPUPS, NULL));
71 }
72
73 TEST_F(HostContentSettingsMapTest, IndividualSettings) {
74   TestingProfile profile;
75   HostContentSettingsMap* host_content_settings_map =
76       profile.GetHostContentSettingsMap();
77
78   // Check returning individual settings.
79   GURL host("http://example.com/");
80   ContentSettingsPattern pattern =
81        ContentSettingsPattern::FromString("[*.]example.com");
82   EXPECT_EQ(CONTENT_SETTING_ALLOW,
83             host_content_settings_map->GetContentSetting(
84                 host, host, CONTENT_SETTINGS_TYPE_IMAGES, std::string()));
85   host_content_settings_map->SetContentSetting(
86       pattern,
87       ContentSettingsPattern::Wildcard(),
88       CONTENT_SETTINGS_TYPE_IMAGES,
89       std::string(),
90       CONTENT_SETTING_DEFAULT);
91   EXPECT_EQ(CONTENT_SETTING_ALLOW,
92             host_content_settings_map->GetContentSetting(
93                 host, host, CONTENT_SETTINGS_TYPE_IMAGES, std::string()));
94   host_content_settings_map->SetContentSetting(
95       pattern,
96       ContentSettingsPattern::Wildcard(),
97       CONTENT_SETTINGS_TYPE_IMAGES,
98       std::string(),
99       CONTENT_SETTING_BLOCK);
100   EXPECT_EQ(CONTENT_SETTING_BLOCK,
101             host_content_settings_map->GetContentSetting(
102                 host, host, CONTENT_SETTINGS_TYPE_IMAGES, std::string()));
103   EXPECT_EQ(CONTENT_SETTING_ALLOW,
104             host_content_settings_map->GetContentSetting(
105                 host, host, CONTENT_SETTINGS_TYPE_PLUGINS, std::string()));
106
107   // Check returning all settings for a host.
108   host_content_settings_map->SetContentSetting(
109       pattern,
110       ContentSettingsPattern::Wildcard(),
111       CONTENT_SETTINGS_TYPE_IMAGES,
112       std::string(),
113       CONTENT_SETTING_DEFAULT);
114   EXPECT_EQ(CONTENT_SETTING_ALLOW,
115             host_content_settings_map->GetContentSetting(
116                 host, host, CONTENT_SETTINGS_TYPE_IMAGES, std::string()));
117   host_content_settings_map->SetContentSetting(
118       pattern,
119       ContentSettingsPattern::Wildcard(),
120       CONTENT_SETTINGS_TYPE_JAVASCRIPT,
121       std::string(),
122       CONTENT_SETTING_BLOCK);
123   EXPECT_EQ(CONTENT_SETTING_BLOCK,
124             host_content_settings_map->GetContentSetting(
125                 host, host, CONTENT_SETTINGS_TYPE_JAVASCRIPT, std::string()));
126   host_content_settings_map->SetContentSetting(
127       pattern,
128       ContentSettingsPattern::Wildcard(),
129       CONTENT_SETTINGS_TYPE_PLUGINS,
130       std::string(),
131       CONTENT_SETTING_ALLOW);
132   EXPECT_EQ(CONTENT_SETTING_ALLOW,
133             host_content_settings_map->GetContentSetting(
134                 host, host, CONTENT_SETTINGS_TYPE_PLUGINS, std::string()));
135   EXPECT_EQ(CONTENT_SETTING_BLOCK,
136             host_content_settings_map->GetContentSetting(
137                 host, host, CONTENT_SETTINGS_TYPE_POPUPS, std::string()));
138   EXPECT_EQ(CONTENT_SETTING_ASK,
139             host_content_settings_map->GetContentSetting(
140                 host, host, CONTENT_SETTINGS_TYPE_GEOLOCATION, std::string()));
141   EXPECT_EQ(
142       CONTENT_SETTING_ASK,
143       host_content_settings_map->GetContentSetting(
144           host, host, CONTENT_SETTINGS_TYPE_NOTIFICATIONS, std::string()));
145   EXPECT_EQ(CONTENT_SETTING_ASK,
146             host_content_settings_map->GetContentSetting(
147                 host, host, CONTENT_SETTINGS_TYPE_FULLSCREEN, std::string()));
148   EXPECT_EQ(CONTENT_SETTING_ASK,
149             host_content_settings_map->GetContentSetting(
150                 host, host, CONTENT_SETTINGS_TYPE_MOUSELOCK, std::string()));
151
152   // Check returning all hosts for a setting.
153   ContentSettingsPattern pattern2 =
154        ContentSettingsPattern::FromString("[*.]example.org");
155   host_content_settings_map->SetContentSetting(
156       pattern2,
157       ContentSettingsPattern::Wildcard(),
158       CONTENT_SETTINGS_TYPE_IMAGES,
159       std::string(),
160       CONTENT_SETTING_BLOCK);
161   host_content_settings_map->SetContentSetting(
162       pattern2,
163       ContentSettingsPattern::Wildcard(),
164       CONTENT_SETTINGS_TYPE_PLUGINS,
165       std::string(),
166       CONTENT_SETTING_BLOCK);
167   ContentSettingsForOneType host_settings;
168   host_content_settings_map->GetSettingsForOneType(
169       CONTENT_SETTINGS_TYPE_IMAGES, std::string(), &host_settings);
170   // |host_settings| contains the default setting and an exception.
171   EXPECT_EQ(2U, host_settings.size());
172   host_content_settings_map->GetSettingsForOneType(
173       CONTENT_SETTINGS_TYPE_PLUGINS, std::string(), &host_settings);
174   // |host_settings| contains the default setting and 2 exceptions.
175   EXPECT_EQ(3U, host_settings.size());
176   host_content_settings_map->GetSettingsForOneType(
177       CONTENT_SETTINGS_TYPE_POPUPS, std::string(), &host_settings);
178   // |host_settings| contains only the default setting.
179   EXPECT_EQ(1U, host_settings.size());
180 }
181
182 TEST_F(HostContentSettingsMapTest, Clear) {
183   TestingProfile profile;
184   HostContentSettingsMap* host_content_settings_map =
185       profile.GetHostContentSettingsMap();
186
187   // Check clearing one type.
188   ContentSettingsPattern pattern =
189        ContentSettingsPattern::FromString("[*.]example.org");
190   ContentSettingsPattern pattern2 =
191       ContentSettingsPattern::FromString("[*.]example.net");
192   host_content_settings_map->SetContentSetting(
193       pattern2,
194       ContentSettingsPattern::Wildcard(),
195       CONTENT_SETTINGS_TYPE_IMAGES,
196       std::string(),
197       CONTENT_SETTING_BLOCK);
198   host_content_settings_map->SetContentSetting(
199       pattern,
200       ContentSettingsPattern::Wildcard(),
201       CONTENT_SETTINGS_TYPE_IMAGES,
202       std::string(),
203       CONTENT_SETTING_BLOCK);
204   host_content_settings_map->SetContentSetting(
205       pattern,
206       ContentSettingsPattern::Wildcard(),
207       CONTENT_SETTINGS_TYPE_PLUGINS,
208       std::string(),
209       CONTENT_SETTING_BLOCK);
210   host_content_settings_map->SetContentSetting(
211       pattern2,
212       ContentSettingsPattern::Wildcard(),
213       CONTENT_SETTINGS_TYPE_IMAGES,
214       std::string(),
215       CONTENT_SETTING_BLOCK);
216   host_content_settings_map->ClearSettingsForOneType(
217       CONTENT_SETTINGS_TYPE_IMAGES);
218   ContentSettingsForOneType host_settings;
219   host_content_settings_map->GetSettingsForOneType(
220       CONTENT_SETTINGS_TYPE_IMAGES, std::string(), &host_settings);
221   // |host_settings| contains only the default setting.
222   EXPECT_EQ(1U, host_settings.size());
223   host_content_settings_map->GetSettingsForOneType(
224       CONTENT_SETTINGS_TYPE_PLUGINS, std::string(), &host_settings);
225   // |host_settings| contains the default setting and an exception.
226   EXPECT_EQ(2U, host_settings.size());
227 }
228
229 TEST_F(HostContentSettingsMapTest, Patterns) {
230   TestingProfile profile;
231   HostContentSettingsMap* host_content_settings_map =
232       profile.GetHostContentSettingsMap();
233
234   GURL host1("http://example.com/");
235   GURL host2("http://www.example.com/");
236   GURL host3("http://example.org/");
237   ContentSettingsPattern pattern1 =
238        ContentSettingsPattern::FromString("[*.]example.com");
239   ContentSettingsPattern pattern2 =
240        ContentSettingsPattern::FromString("example.org");
241   EXPECT_EQ(CONTENT_SETTING_ALLOW,
242             host_content_settings_map->GetContentSetting(
243                 host1, host1, CONTENT_SETTINGS_TYPE_IMAGES, std::string()));
244   host_content_settings_map->SetContentSetting(
245       pattern1,
246       ContentSettingsPattern::Wildcard(),
247       CONTENT_SETTINGS_TYPE_IMAGES,
248       std::string(),
249       CONTENT_SETTING_BLOCK);
250   EXPECT_EQ(CONTENT_SETTING_BLOCK,
251             host_content_settings_map->GetContentSetting(
252                 host1, host1, CONTENT_SETTINGS_TYPE_IMAGES, std::string()));
253   EXPECT_EQ(CONTENT_SETTING_BLOCK,
254             host_content_settings_map->GetContentSetting(
255                 host2, host2, CONTENT_SETTINGS_TYPE_IMAGES, std::string()));
256   EXPECT_EQ(CONTENT_SETTING_ALLOW,
257             host_content_settings_map->GetContentSetting(
258                 host3, host3, CONTENT_SETTINGS_TYPE_IMAGES, std::string()));
259   host_content_settings_map->SetContentSetting(
260       pattern2,
261       ContentSettingsPattern::Wildcard(),
262       CONTENT_SETTINGS_TYPE_IMAGES,
263       std::string(),
264       CONTENT_SETTING_BLOCK);
265   EXPECT_EQ(CONTENT_SETTING_BLOCK,
266             host_content_settings_map->GetContentSetting(
267                 host3, host3, CONTENT_SETTINGS_TYPE_IMAGES, std::string()));
268 }
269
270 TEST_F(HostContentSettingsMapTest, Observer) {
271   TestingProfile profile;
272   HostContentSettingsMap* host_content_settings_map =
273       profile.GetHostContentSettingsMap();
274   MockSettingsObserver observer;
275
276   ContentSettingsPattern primary_pattern =
277       ContentSettingsPattern::FromString("[*.]example.com");
278   ContentSettingsPattern secondary_pattern =
279       ContentSettingsPattern::Wildcard();
280   EXPECT_CALL(observer,
281               OnContentSettingsChanged(host_content_settings_map,
282                                        CONTENT_SETTINGS_TYPE_IMAGES,
283                                        false,
284                                        primary_pattern,
285                                        secondary_pattern,
286                                        false));
287   host_content_settings_map->SetContentSetting(
288       primary_pattern,
289       secondary_pattern,
290       CONTENT_SETTINGS_TYPE_IMAGES,
291       std::string(),
292       CONTENT_SETTING_ALLOW);
293   ::testing::Mock::VerifyAndClearExpectations(&observer);
294
295   EXPECT_CALL(observer,
296               OnContentSettingsChanged(host_content_settings_map,
297                                        CONTENT_SETTINGS_TYPE_IMAGES, false,
298                                        _, _, true));
299   host_content_settings_map->ClearSettingsForOneType(
300       CONTENT_SETTINGS_TYPE_IMAGES);
301   ::testing::Mock::VerifyAndClearExpectations(&observer);
302
303   EXPECT_CALL(observer,
304               OnContentSettingsChanged(host_content_settings_map,
305                                        CONTENT_SETTINGS_TYPE_IMAGES, false,
306                                        _, _, true));
307   host_content_settings_map->SetDefaultContentSetting(
308       CONTENT_SETTINGS_TYPE_IMAGES, CONTENT_SETTING_BLOCK);
309 }
310
311 TEST_F(HostContentSettingsMapTest, ObserveDefaultPref) {
312   TestingProfile profile;
313   HostContentSettingsMap* host_content_settings_map =
314       profile.GetHostContentSettingsMap();
315
316   PrefService* prefs = profile.GetPrefs();
317
318   // Make a copy of the default pref value so we can reset it later.
319   scoped_ptr<Value> default_value(prefs->FindPreference(
320       prefs::kDefaultContentSettings)->GetValue()->DeepCopy());
321
322   GURL host("http://example.com");
323
324   host_content_settings_map->SetDefaultContentSetting(
325       CONTENT_SETTINGS_TYPE_IMAGES, CONTENT_SETTING_BLOCK);
326   EXPECT_EQ(CONTENT_SETTING_BLOCK,
327             host_content_settings_map->GetContentSetting(
328                 host, host, CONTENT_SETTINGS_TYPE_IMAGES, std::string()));
329
330   // Make a copy of the pref's new value so we can reset it later.
331   scoped_ptr<Value> new_value(prefs->FindPreference(
332       prefs::kDefaultContentSettings)->GetValue()->DeepCopy());
333
334   // Clearing the backing pref should also clear the internal cache.
335   prefs->Set(prefs::kDefaultContentSettings, *default_value);
336   EXPECT_EQ(CONTENT_SETTING_ALLOW,
337             host_content_settings_map->GetContentSetting(
338                 host, host, CONTENT_SETTINGS_TYPE_IMAGES, std::string()));
339
340   // Reseting the pref to its previous value should update the cache.
341   prefs->Set(prefs::kDefaultContentSettings, *new_value);
342   EXPECT_EQ(CONTENT_SETTING_BLOCK,
343             host_content_settings_map->GetContentSetting(
344                 host, host, CONTENT_SETTINGS_TYPE_IMAGES, std::string()));
345 }
346
347 TEST_F(HostContentSettingsMapTest, ObserveExceptionPref) {
348   TestingProfile profile;
349   HostContentSettingsMap* host_content_settings_map =
350       profile.GetHostContentSettingsMap();
351
352   PrefService* prefs = profile.GetPrefs();
353
354   // Make a copy of the default pref value so we can reset it later.
355   scoped_ptr<Value> default_value(prefs->FindPreference(
356       prefs::kContentSettingsPatternPairs)->GetValue()->DeepCopy());
357
358   ContentSettingsPattern pattern =
359        ContentSettingsPattern::FromString("[*.]example.com");
360   GURL host("http://example.com");
361
362   EXPECT_EQ(CONTENT_SETTING_ALLOW,
363             host_content_settings_map->GetContentSetting(
364                 host, host, CONTENT_SETTINGS_TYPE_IMAGES, std::string()));
365
366   host_content_settings_map->SetContentSetting(
367       pattern,
368       ContentSettingsPattern::Wildcard(),
369       CONTENT_SETTINGS_TYPE_IMAGES,
370       std::string(),
371       CONTENT_SETTING_BLOCK);
372   EXPECT_EQ(CONTENT_SETTING_BLOCK,
373             host_content_settings_map->GetContentSetting(
374                 host, host, CONTENT_SETTINGS_TYPE_IMAGES, std::string()));
375
376   // Make a copy of the pref's new value so we can reset it later.
377   scoped_ptr<Value> new_value(prefs->FindPreference(
378       prefs::kContentSettingsPatternPairs)->GetValue()->DeepCopy());
379
380   // Clearing the backing pref should also clear the internal cache.
381   prefs->Set(prefs::kContentSettingsPatternPairs, *default_value);
382   EXPECT_EQ(CONTENT_SETTING_ALLOW,
383             host_content_settings_map->GetContentSetting(
384                 host, host, CONTENT_SETTINGS_TYPE_IMAGES, std::string()));
385
386   // Reseting the pref to its previous value should update the cache.
387   prefs->Set(prefs::kContentSettingsPatternPairs, *new_value);
388   EXPECT_EQ(CONTENT_SETTING_BLOCK,
389             host_content_settings_map->GetContentSetting(
390                 host, host, CONTENT_SETTINGS_TYPE_IMAGES, std::string()));
391 }
392
393 TEST_F(HostContentSettingsMapTest, HostTrimEndingDotCheck) {
394   TestingProfile profile;
395   HostContentSettingsMap* host_content_settings_map =
396       profile.GetHostContentSettingsMap();
397   CookieSettings* cookie_settings =
398       CookieSettings::Factory::GetForProfile(&profile).get();
399
400   ContentSettingsPattern pattern =
401        ContentSettingsPattern::FromString("[*.]example.com");
402   GURL host_ending_with_dot("http://example.com./");
403
404   EXPECT_EQ(CONTENT_SETTING_ALLOW,
405             host_content_settings_map->GetContentSetting(
406                 host_ending_with_dot,
407                 host_ending_with_dot,
408                 CONTENT_SETTINGS_TYPE_IMAGES,
409                 std::string()));
410   host_content_settings_map->SetContentSetting(
411       pattern,
412       ContentSettingsPattern::Wildcard(),
413       CONTENT_SETTINGS_TYPE_IMAGES,
414       std::string(),
415       CONTENT_SETTING_DEFAULT);
416   EXPECT_EQ(
417       CONTENT_SETTING_ALLOW,
418       host_content_settings_map->GetContentSetting(host_ending_with_dot,
419                                                    host_ending_with_dot,
420                                                    CONTENT_SETTINGS_TYPE_IMAGES,
421                                                    std::string()));
422   host_content_settings_map->SetContentSetting(
423       pattern,
424       ContentSettingsPattern::Wildcard(),
425       CONTENT_SETTINGS_TYPE_IMAGES,
426       std::string(),
427       CONTENT_SETTING_BLOCK);
428   EXPECT_EQ(
429       CONTENT_SETTING_BLOCK,
430       host_content_settings_map->GetContentSetting(host_ending_with_dot,
431                                                    host_ending_with_dot,
432                                                    CONTENT_SETTINGS_TYPE_IMAGES,
433                                                    std::string()));
434
435   EXPECT_TRUE(cookie_settings->IsSettingCookieAllowed(
436       host_ending_with_dot, host_ending_with_dot));
437   host_content_settings_map->SetContentSetting(
438       pattern,
439       ContentSettingsPattern::Wildcard(),
440       CONTENT_SETTINGS_TYPE_COOKIES,
441       std::string(),
442       CONTENT_SETTING_DEFAULT);
443   EXPECT_TRUE(cookie_settings->IsSettingCookieAllowed(
444       host_ending_with_dot, host_ending_with_dot));
445   host_content_settings_map->SetContentSetting(
446       pattern,
447       ContentSettingsPattern::Wildcard(),
448       CONTENT_SETTINGS_TYPE_COOKIES,
449       std::string(),
450       CONTENT_SETTING_BLOCK);
451   EXPECT_FALSE(cookie_settings->IsSettingCookieAllowed(
452       host_ending_with_dot, host_ending_with_dot));
453
454   EXPECT_EQ(CONTENT_SETTING_ALLOW,
455             host_content_settings_map->GetContentSetting(
456                 host_ending_with_dot,
457                 host_ending_with_dot,
458                 CONTENT_SETTINGS_TYPE_JAVASCRIPT,
459                 std::string()));
460   host_content_settings_map->SetContentSetting(
461       pattern,
462       ContentSettingsPattern::Wildcard(),
463       CONTENT_SETTINGS_TYPE_JAVASCRIPT,
464       std::string(),
465       CONTENT_SETTING_DEFAULT);
466   EXPECT_EQ(CONTENT_SETTING_ALLOW,
467             host_content_settings_map->GetContentSetting(
468                 host_ending_with_dot,
469                 host_ending_with_dot,
470                 CONTENT_SETTINGS_TYPE_JAVASCRIPT,
471                 std::string()));
472   host_content_settings_map->SetContentSetting(
473       pattern,
474       ContentSettingsPattern::Wildcard(),
475       CONTENT_SETTINGS_TYPE_JAVASCRIPT,
476       std::string(),
477       CONTENT_SETTING_BLOCK);
478   EXPECT_EQ(CONTENT_SETTING_BLOCK,
479             host_content_settings_map->GetContentSetting(
480                 host_ending_with_dot,
481                 host_ending_with_dot,
482                 CONTENT_SETTINGS_TYPE_JAVASCRIPT,
483                 std::string()));
484
485   EXPECT_EQ(CONTENT_SETTING_ALLOW,
486             host_content_settings_map->GetContentSetting(
487                 host_ending_with_dot,
488                 host_ending_with_dot,
489                 CONTENT_SETTINGS_TYPE_PLUGINS,
490                 std::string()));
491   host_content_settings_map->SetContentSetting(
492       pattern,
493       ContentSettingsPattern::Wildcard(),
494       CONTENT_SETTINGS_TYPE_PLUGINS,
495       std::string(),
496       CONTENT_SETTING_DEFAULT);
497   EXPECT_EQ(CONTENT_SETTING_ALLOW,
498             host_content_settings_map->GetContentSetting(
499                 host_ending_with_dot,
500                 host_ending_with_dot,
501                 CONTENT_SETTINGS_TYPE_PLUGINS,
502                 std::string()));
503   host_content_settings_map->SetContentSetting(
504       pattern,
505       ContentSettingsPattern::Wildcard(),
506       CONTENT_SETTINGS_TYPE_PLUGINS,
507       std::string(),
508       CONTENT_SETTING_BLOCK);
509   EXPECT_EQ(CONTENT_SETTING_BLOCK,
510             host_content_settings_map->GetContentSetting(
511                 host_ending_with_dot,
512                 host_ending_with_dot,
513                 CONTENT_SETTINGS_TYPE_PLUGINS,
514                 std::string()));
515
516   EXPECT_EQ(
517       CONTENT_SETTING_BLOCK,
518       host_content_settings_map->GetContentSetting(host_ending_with_dot,
519                                                    host_ending_with_dot,
520                                                    CONTENT_SETTINGS_TYPE_POPUPS,
521                                                    std::string()));
522   host_content_settings_map->SetContentSetting(
523       pattern,
524       ContentSettingsPattern::Wildcard(),
525       CONTENT_SETTINGS_TYPE_POPUPS,
526       std::string(),
527       CONTENT_SETTING_DEFAULT);
528   EXPECT_EQ(
529       CONTENT_SETTING_BLOCK,
530       host_content_settings_map->GetContentSetting(host_ending_with_dot,
531                                                    host_ending_with_dot,
532                                                    CONTENT_SETTINGS_TYPE_POPUPS,
533                                                    std::string()));
534   host_content_settings_map->SetContentSetting(
535       pattern,
536       ContentSettingsPattern::Wildcard(),
537       CONTENT_SETTINGS_TYPE_POPUPS,
538       std::string(),
539       CONTENT_SETTING_ALLOW);
540   EXPECT_EQ(
541       CONTENT_SETTING_ALLOW,
542       host_content_settings_map->GetContentSetting(host_ending_with_dot,
543                                                    host_ending_with_dot,
544                                                    CONTENT_SETTINGS_TYPE_POPUPS,
545                                                    std::string()));
546 }
547
548 TEST_F(HostContentSettingsMapTest, NestedSettings) {
549   TestingProfile profile;
550   HostContentSettingsMap* host_content_settings_map =
551       profile.GetHostContentSettingsMap();
552
553   GURL host("http://a.b.example.com/");
554   ContentSettingsPattern pattern1 =
555        ContentSettingsPattern::FromString("[*.]example.com");
556   ContentSettingsPattern pattern2 =
557        ContentSettingsPattern::FromString("[*.]b.example.com");
558   ContentSettingsPattern pattern3 =
559        ContentSettingsPattern::FromString("a.b.example.com");
560
561   host_content_settings_map->SetContentSetting(
562       pattern1,
563       ContentSettingsPattern::Wildcard(),
564       CONTENT_SETTINGS_TYPE_IMAGES,
565       std::string(),
566       CONTENT_SETTING_BLOCK);
567
568   host_content_settings_map->SetContentSetting(
569       pattern2,
570       ContentSettingsPattern::Wildcard(),
571       CONTENT_SETTINGS_TYPE_COOKIES,
572       std::string(),
573       CONTENT_SETTING_BLOCK);
574
575   host_content_settings_map->SetContentSetting(
576       pattern3,
577       ContentSettingsPattern::Wildcard(),
578       CONTENT_SETTINGS_TYPE_PLUGINS,
579       std::string(),
580       CONTENT_SETTING_BLOCK);
581   host_content_settings_map->SetDefaultContentSetting(
582       CONTENT_SETTINGS_TYPE_JAVASCRIPT, CONTENT_SETTING_BLOCK);
583
584   EXPECT_EQ(CONTENT_SETTING_BLOCK,
585             host_content_settings_map->GetContentSetting(
586                 host, host, CONTENT_SETTINGS_TYPE_COOKIES, std::string()));
587   EXPECT_EQ(CONTENT_SETTING_BLOCK,
588             host_content_settings_map->GetContentSetting(
589                 host, host, CONTENT_SETTINGS_TYPE_IMAGES, std::string()));
590   EXPECT_EQ(CONTENT_SETTING_BLOCK,
591             host_content_settings_map->GetContentSetting(
592                 host, host, CONTENT_SETTINGS_TYPE_JAVASCRIPT, std::string()));
593   EXPECT_EQ(CONTENT_SETTING_BLOCK,
594             host_content_settings_map->GetContentSetting(
595                 host, host, CONTENT_SETTINGS_TYPE_PLUGINS, std::string()));
596   EXPECT_EQ(CONTENT_SETTING_BLOCK,
597             host_content_settings_map->GetContentSetting(
598                 host, host, CONTENT_SETTINGS_TYPE_POPUPS, std::string()));
599   EXPECT_EQ(CONTENT_SETTING_ASK,
600             host_content_settings_map->GetContentSetting(
601                 host, host, CONTENT_SETTINGS_TYPE_GEOLOCATION, std::string()));
602   EXPECT_EQ(
603       CONTENT_SETTING_ASK,
604       host_content_settings_map->GetContentSetting(
605           host, host, CONTENT_SETTINGS_TYPE_NOTIFICATIONS, std::string()));
606   EXPECT_EQ(CONTENT_SETTING_ASK,
607             host_content_settings_map->GetContentSetting(
608                 host, host, CONTENT_SETTINGS_TYPE_FULLSCREEN, std::string()));
609   EXPECT_EQ(CONTENT_SETTING_ASK,
610             host_content_settings_map->GetContentSetting(
611                 host, host, CONTENT_SETTINGS_TYPE_MOUSELOCK, std::string()));
612 }
613
614 TEST_F(HostContentSettingsMapTest, OffTheRecord) {
615   TestingProfile profile;
616   HostContentSettingsMap* host_content_settings_map =
617       profile.GetHostContentSettingsMap();
618   scoped_refptr<HostContentSettingsMap> otr_map(
619       new HostContentSettingsMap(profile.GetPrefs(),
620                                  true));
621
622   GURL host("http://example.com/");
623   ContentSettingsPattern pattern =
624        ContentSettingsPattern::FromString("[*.]example.com");
625
626   EXPECT_EQ(CONTENT_SETTING_ALLOW,
627             host_content_settings_map->GetContentSetting(
628                 host, host, CONTENT_SETTINGS_TYPE_IMAGES, std::string()));
629   EXPECT_EQ(CONTENT_SETTING_ALLOW,
630             otr_map->GetContentSetting(
631                 host, host, CONTENT_SETTINGS_TYPE_IMAGES, std::string()));
632
633   // Changing content settings on the main map should also affect the
634   // incognito map.
635   host_content_settings_map->SetContentSetting(
636       pattern,
637       ContentSettingsPattern::Wildcard(),
638       CONTENT_SETTINGS_TYPE_IMAGES,
639       std::string(),
640       CONTENT_SETTING_BLOCK);
641   EXPECT_EQ(CONTENT_SETTING_BLOCK,
642             host_content_settings_map->GetContentSetting(
643                 host, host, CONTENT_SETTINGS_TYPE_IMAGES, std::string()));
644   EXPECT_EQ(CONTENT_SETTING_BLOCK,
645             otr_map->GetContentSetting(
646                 host, host, CONTENT_SETTINGS_TYPE_IMAGES, std::string()));
647
648   // Changing content settings on the incognito map should NOT affect the
649   // main map.
650   otr_map->SetContentSetting(pattern,
651                              ContentSettingsPattern::Wildcard(),
652                              CONTENT_SETTINGS_TYPE_IMAGES,
653                              std::string(),
654                              CONTENT_SETTING_ALLOW);
655   EXPECT_EQ(CONTENT_SETTING_BLOCK,
656             host_content_settings_map->GetContentSetting(
657                 host, host, CONTENT_SETTINGS_TYPE_IMAGES, std::string()));
658   EXPECT_EQ(CONTENT_SETTING_ALLOW,
659             otr_map->GetContentSetting(
660                 host, host, CONTENT_SETTINGS_TYPE_IMAGES, std::string()));
661
662   otr_map->ShutdownOnUIThread();
663 }
664
665 // For a single Unicode encoded pattern, check if it gets converted to punycode
666 // and old pattern gets deleted.
667 TEST_F(HostContentSettingsMapTest, CanonicalizeExceptionsUnicodeOnly) {
668   TestingProfile profile;
669   PrefService* prefs = profile.GetPrefs();
670
671   // Set utf-8 data.
672   {
673     DictionaryPrefUpdate update(prefs, prefs::kContentSettingsPatternPairs);
674     DictionaryValue* all_settings_dictionary = update.Get();
675     ASSERT_TRUE(NULL != all_settings_dictionary);
676
677     DictionaryValue* dummy_payload = new DictionaryValue;
678     dummy_payload->SetInteger("images", CONTENT_SETTING_ALLOW);
679     all_settings_dictionary->SetWithoutPathExpansion("[*.]\xC4\x87ira.com,*",
680                                                      dummy_payload);
681   }
682   profile.GetHostContentSettingsMap();
683
684   const DictionaryValue* all_settings_dictionary =
685       prefs->GetDictionary(prefs::kContentSettingsPatternPairs);
686   const DictionaryValue* result = NULL;
687   EXPECT_FALSE(all_settings_dictionary->GetDictionaryWithoutPathExpansion(
688       "[*.]\xC4\x87ira.com,*", &result));
689   EXPECT_TRUE(all_settings_dictionary->GetDictionaryWithoutPathExpansion(
690       "[*.]xn--ira-ppa.com,*", &result));
691 }
692
693 // If both Unicode and its punycode pattern exist, make sure we don't touch the
694 // settings for the punycode, and that Unicode pattern gets deleted.
695 TEST_F(HostContentSettingsMapTest, CanonicalizeExceptionsUnicodeAndPunycode) {
696   TestingProfile profile;
697
698   scoped_ptr<Value> value(base::JSONReader::Read(
699       "{\"[*.]\\xC4\\x87ira.com,*\":{\"images\":1}}"));
700   profile.GetPrefs()->Set(prefs::kContentSettingsPatternPairs, *value);
701
702   // Set punycode equivalent, with different setting.
703   scoped_ptr<Value> puny_value(base::JSONReader::Read(
704       "{\"[*.]xn--ira-ppa.com,*\":{\"images\":2}}"));
705   profile.GetPrefs()->Set(prefs::kContentSettingsPatternPairs, *puny_value);
706
707   // Initialize the content map.
708   profile.GetHostContentSettingsMap();
709
710   const DictionaryValue* content_setting_prefs =
711       profile.GetPrefs()->GetDictionary(prefs::kContentSettingsPatternPairs);
712   std::string prefs_as_json;
713   base::JSONWriter::Write(content_setting_prefs, &prefs_as_json);
714   EXPECT_STREQ("{\"[*.]xn--ira-ppa.com,*\":{\"images\":2}}",
715                prefs_as_json.c_str());
716 }
717
718 TEST_F(HostContentSettingsMapTest, ResourceIdentifier) {
719   // This feature is currently behind a flag.
720   CommandLine* cmd = CommandLine::ForCurrentProcess();
721   base::AutoReset<CommandLine> auto_reset(cmd, *cmd);
722   cmd->AppendSwitch(switches::kEnableResourceContentSettings);
723
724   TestingProfile profile;
725   HostContentSettingsMap* host_content_settings_map =
726       profile.GetHostContentSettingsMap();
727
728   GURL host("http://example.com/");
729   ContentSettingsPattern pattern =
730        ContentSettingsPattern::FromString("[*.]example.com");
731   std::string resource1("someplugin");
732   std::string resource2("otherplugin");
733
734   // If resource content settings are enabled, GetContentSettings should return
735   // the default values for all plugins
736   ContentSetting default_plugin_setting =
737       host_content_settings_map->GetDefaultContentSetting(
738           CONTENT_SETTINGS_TYPE_PLUGINS, NULL);
739   EXPECT_EQ(default_plugin_setting,
740             host_content_settings_map->GetContentSetting(
741                 host, host, CONTENT_SETTINGS_TYPE_PLUGINS, std::string()));
742
743   // If no resource-specific content settings are defined, the setting should be
744   // DEFAULT.
745   EXPECT_EQ(CONTENT_SETTING_DEFAULT,
746             host_content_settings_map->GetContentSetting(
747                 host, host, CONTENT_SETTINGS_TYPE_PLUGINS, resource1));
748
749   host_content_settings_map->SetContentSetting(
750       pattern,
751       ContentSettingsPattern::Wildcard(),
752       CONTENT_SETTINGS_TYPE_PLUGINS,
753       resource1,
754       CONTENT_SETTING_BLOCK);
755   EXPECT_EQ(CONTENT_SETTING_BLOCK,
756             host_content_settings_map->GetContentSetting(
757                 host, host, CONTENT_SETTINGS_TYPE_PLUGINS, resource1));
758   EXPECT_EQ(CONTENT_SETTING_DEFAULT,
759             host_content_settings_map->GetContentSetting(
760                 host, host, CONTENT_SETTINGS_TYPE_PLUGINS, resource2));
761 }
762
763 TEST_F(HostContentSettingsMapTest, ResourceIdentifierPrefs) {
764   // This feature is currently behind a flag.
765   CommandLine* cmd = CommandLine::ForCurrentProcess();
766   base::AutoReset<CommandLine> auto_reset(cmd, *cmd);
767   cmd->AppendSwitch(switches::kEnableResourceContentSettings);
768
769   TestingProfile profile;
770   scoped_ptr<Value> value(base::JSONReader::Read(
771       "{\"[*.]example.com,*\":{\"per_plugin\":{\"someplugin\":2}}}"));
772   profile.GetPrefs()->Set(prefs::kContentSettingsPatternPairs, *value);
773   HostContentSettingsMap* host_content_settings_map =
774       profile.GetHostContentSettingsMap();
775
776   GURL host("http://example.com/");
777   ContentSettingsPattern item_pattern =
778       ContentSettingsPattern::FromString("[*.]example.com");
779   ContentSettingsPattern top_level_frame_pattern =
780       ContentSettingsPattern::Wildcard();
781   std::string resource1("someplugin");
782   std::string resource2("otherplugin");
783
784   EXPECT_EQ(CONTENT_SETTING_BLOCK,
785             host_content_settings_map->GetContentSetting(
786                 host, host, CONTENT_SETTINGS_TYPE_PLUGINS, resource1));
787
788   host_content_settings_map->SetContentSetting(
789       item_pattern,
790       top_level_frame_pattern,
791       CONTENT_SETTINGS_TYPE_PLUGINS,
792       resource1,
793       CONTENT_SETTING_DEFAULT);
794
795   const DictionaryValue* content_setting_prefs =
796       profile.GetPrefs()->GetDictionary(prefs::kContentSettingsPatternPairs);
797   std::string prefs_as_json;
798   base::JSONWriter::Write(content_setting_prefs, &prefs_as_json);
799   EXPECT_EQ("{}", prefs_as_json);
800
801   host_content_settings_map->SetContentSetting(
802       item_pattern,
803       top_level_frame_pattern,
804       CONTENT_SETTINGS_TYPE_PLUGINS,
805       resource2,
806       CONTENT_SETTING_BLOCK);
807
808   content_setting_prefs =
809       profile.GetPrefs()->GetDictionary(prefs::kContentSettingsPatternPairs);
810   base::JSONWriter::Write(content_setting_prefs, &prefs_as_json);
811   EXPECT_EQ("{\"[*.]example.com,*\":{\"per_plugin\":{\"otherplugin\":2}}}",
812             prefs_as_json);
813 }
814
815 // If a default-content-setting is managed, the managed value should be used
816 // instead of the default value.
817 TEST_F(HostContentSettingsMapTest, ManagedDefaultContentSetting) {
818   TestingProfile profile;
819   HostContentSettingsMap* host_content_settings_map =
820       profile.GetHostContentSettingsMap();
821   TestingPrefServiceSyncable* prefs = profile.GetTestingPrefService();
822
823   EXPECT_EQ(CONTENT_SETTING_ALLOW,
824             host_content_settings_map->GetDefaultContentSetting(
825                 CONTENT_SETTINGS_TYPE_JAVASCRIPT, NULL));
826
827   // Set managed-default-content-setting through the coresponding preferences.
828   prefs->SetManagedPref(prefs::kManagedDefaultJavaScriptSetting,
829                         Value::CreateIntegerValue(CONTENT_SETTING_BLOCK));
830   EXPECT_EQ(CONTENT_SETTING_BLOCK,
831             host_content_settings_map->GetDefaultContentSetting(
832                 CONTENT_SETTINGS_TYPE_JAVASCRIPT, NULL));
833
834   // Remove managed-default-content-settings-preferences.
835   prefs->RemoveManagedPref(prefs::kManagedDefaultJavaScriptSetting);
836   EXPECT_EQ(CONTENT_SETTING_ALLOW,
837             host_content_settings_map->GetDefaultContentSetting(
838                 CONTENT_SETTINGS_TYPE_JAVASCRIPT, NULL));
839
840   // Set preference to manage the default-content-setting for Plugins.
841   prefs->SetManagedPref(prefs::kManagedDefaultPluginsSetting,
842                         Value::CreateIntegerValue(CONTENT_SETTING_BLOCK));
843   EXPECT_EQ(CONTENT_SETTING_BLOCK,
844             host_content_settings_map->GetDefaultContentSetting(
845                 CONTENT_SETTINGS_TYPE_PLUGINS, NULL));
846
847   // Remove the preference to manage the default-content-setting for Plugins.
848   prefs->RemoveManagedPref(prefs::kManagedDefaultPluginsSetting);
849   EXPECT_EQ(CONTENT_SETTING_ALLOW,
850             host_content_settings_map->GetDefaultContentSetting(
851                 CONTENT_SETTINGS_TYPE_PLUGINS, NULL));
852 }
853
854 TEST_F(HostContentSettingsMapTest,
855        GetNonDefaultContentSettingsIfTypeManaged) {
856   TestingProfile profile;
857   HostContentSettingsMap* host_content_settings_map =
858       profile.GetHostContentSettingsMap();
859   TestingPrefServiceSyncable* prefs = profile.GetTestingPrefService();
860
861   // Set pattern for JavaScript setting.
862   ContentSettingsPattern pattern =
863        ContentSettingsPattern::FromString("[*.]example.com");
864   host_content_settings_map->SetContentSetting(
865       pattern,
866       ContentSettingsPattern::Wildcard(),
867       CONTENT_SETTINGS_TYPE_JAVASCRIPT,
868       std::string(),
869       CONTENT_SETTING_BLOCK);
870
871   EXPECT_EQ(CONTENT_SETTING_ALLOW,
872             host_content_settings_map->GetDefaultContentSetting(
873                 CONTENT_SETTINGS_TYPE_JAVASCRIPT, NULL));
874
875   GURL host("http://example.com/");
876   EXPECT_EQ(CONTENT_SETTING_BLOCK,
877             host_content_settings_map->GetContentSetting(
878                 host, host, CONTENT_SETTINGS_TYPE_JAVASCRIPT, std::string()));
879
880   // Set managed-default-content-setting for content-settings-type JavaScript.
881   prefs->SetManagedPref(prefs::kManagedDefaultJavaScriptSetting,
882                         Value::CreateIntegerValue(CONTENT_SETTING_ALLOW));
883   EXPECT_EQ(CONTENT_SETTING_ALLOW,
884             host_content_settings_map->GetContentSetting(
885                 host, host, CONTENT_SETTINGS_TYPE_JAVASCRIPT, std::string()));
886 }
887
888 // Managed default content setting should have higher priority
889 // than user defined patterns.
890 TEST_F(HostContentSettingsMapTest,
891        ManagedDefaultContentSettingIgnoreUserPattern) {
892   TestingProfile profile;
893   HostContentSettingsMap* host_content_settings_map =
894       profile.GetHostContentSettingsMap();
895   TestingPrefServiceSyncable* prefs = profile.GetTestingPrefService();
896
897   // Block all JavaScript.
898   host_content_settings_map->SetDefaultContentSetting(
899       CONTENT_SETTINGS_TYPE_JAVASCRIPT, CONTENT_SETTING_BLOCK);
900
901   // Set an exception to allow "[*.]example.com"
902   ContentSettingsPattern pattern =
903       ContentSettingsPattern::FromString("[*.]example.com");
904
905   host_content_settings_map->SetContentSetting(
906       pattern,
907       ContentSettingsPattern::Wildcard(),
908       CONTENT_SETTINGS_TYPE_JAVASCRIPT,
909       std::string(),
910       CONTENT_SETTING_ALLOW);
911
912   EXPECT_EQ(CONTENT_SETTING_BLOCK,
913             host_content_settings_map->GetDefaultContentSetting(
914                 CONTENT_SETTINGS_TYPE_JAVASCRIPT, NULL));
915   GURL host("http://example.com/");
916   EXPECT_EQ(CONTENT_SETTING_ALLOW,
917             host_content_settings_map->GetContentSetting(
918                 host, host, CONTENT_SETTINGS_TYPE_JAVASCRIPT, std::string()));
919
920   // Set managed-default-content-settings-preferences.
921   prefs->SetManagedPref(prefs::kManagedDefaultJavaScriptSetting,
922                         Value::CreateIntegerValue(CONTENT_SETTING_BLOCK));
923   EXPECT_EQ(CONTENT_SETTING_BLOCK,
924             host_content_settings_map->GetContentSetting(
925                 host, host, CONTENT_SETTINGS_TYPE_JAVASCRIPT, std::string()));
926
927   // Remove managed-default-content-settings-preferences.
928   prefs->RemoveManagedPref(prefs::kManagedDefaultJavaScriptSetting);
929   EXPECT_EQ(CONTENT_SETTING_ALLOW,
930             host_content_settings_map->GetContentSetting(
931                 host, host, CONTENT_SETTINGS_TYPE_JAVASCRIPT, std::string()));
932 }
933
934 // If a default-content-setting is set to managed setting, the user defined
935 // setting should be preserved.
936 TEST_F(HostContentSettingsMapTest, OverwrittenDefaultContentSetting) {
937   TestingProfile profile;
938   HostContentSettingsMap* host_content_settings_map =
939       profile.GetHostContentSettingsMap();
940   TestingPrefServiceSyncable* prefs = profile.GetTestingPrefService();
941
942   // Set user defined default-content-setting for Cookies.
943   host_content_settings_map->SetDefaultContentSetting(
944       CONTENT_SETTINGS_TYPE_COOKIES, CONTENT_SETTING_BLOCK);
945   EXPECT_EQ(CONTENT_SETTING_BLOCK,
946             host_content_settings_map->GetDefaultContentSetting(
947                 CONTENT_SETTINGS_TYPE_COOKIES, NULL));
948
949   // Set preference to manage the default-content-setting for Cookies.
950   prefs->SetManagedPref(prefs::kManagedDefaultCookiesSetting,
951                         Value::CreateIntegerValue(CONTENT_SETTING_ALLOW));
952   EXPECT_EQ(CONTENT_SETTING_ALLOW,
953             host_content_settings_map->GetDefaultContentSetting(
954                 CONTENT_SETTINGS_TYPE_COOKIES, NULL));
955
956   // Remove the preference to manage the default-content-setting for Cookies.
957   prefs->RemoveManagedPref(prefs::kManagedDefaultCookiesSetting);
958   EXPECT_EQ(CONTENT_SETTING_BLOCK,
959             host_content_settings_map->GetDefaultContentSetting(
960                 CONTENT_SETTINGS_TYPE_COOKIES, NULL));
961 }
962
963 // If a setting for a default-content-setting-type is set while the type is
964 // managed, then the new setting should be preserved and used after the
965 // default-content-setting-type is not managed anymore.
966 TEST_F(HostContentSettingsMapTest, SettingDefaultContentSettingsWhenManaged) {
967   TestingProfile profile;
968   HostContentSettingsMap* host_content_settings_map =
969       profile.GetHostContentSettingsMap();
970   TestingPrefServiceSyncable* prefs = profile.GetTestingPrefService();
971
972   prefs->SetManagedPref(prefs::kManagedDefaultPluginsSetting,
973                         Value::CreateIntegerValue(CONTENT_SETTING_ALLOW));
974   EXPECT_EQ(CONTENT_SETTING_ALLOW,
975             host_content_settings_map->GetDefaultContentSetting(
976                 CONTENT_SETTINGS_TYPE_PLUGINS, NULL));
977
978   host_content_settings_map->SetDefaultContentSetting(
979       CONTENT_SETTINGS_TYPE_PLUGINS, CONTENT_SETTING_BLOCK);
980   EXPECT_EQ(CONTENT_SETTING_ALLOW,
981             host_content_settings_map->GetDefaultContentSetting(
982                 CONTENT_SETTINGS_TYPE_PLUGINS, NULL));
983
984   prefs->RemoveManagedPref(prefs::kManagedDefaultPluginsSetting);
985   EXPECT_EQ(CONTENT_SETTING_BLOCK,
986             host_content_settings_map->GetDefaultContentSetting(
987                 CONTENT_SETTINGS_TYPE_PLUGINS, NULL));
988 }
989
990 TEST_F(HostContentSettingsMapTest, GetContentSetting) {
991   TestingProfile profile;
992   HostContentSettingsMap* host_content_settings_map =
993       profile.GetHostContentSettingsMap();
994
995   GURL host("http://example.com/");
996   GURL embedder("chrome://foo");
997   ContentSettingsPattern pattern =
998        ContentSettingsPattern::FromString("[*.]example.com");
999   host_content_settings_map->SetContentSetting(
1000       pattern,
1001       ContentSettingsPattern::Wildcard(),
1002       CONTENT_SETTINGS_TYPE_IMAGES,
1003       std::string(),
1004       CONTENT_SETTING_BLOCK);
1005   EXPECT_EQ(CONTENT_SETTING_BLOCK,
1006             host_content_settings_map->GetContentSetting(
1007                 host, host, CONTENT_SETTINGS_TYPE_IMAGES, std::string()));
1008   EXPECT_EQ(CONTENT_SETTING_ALLOW,
1009             host_content_settings_map->GetContentSetting(
1010                 embedder, host, CONTENT_SETTINGS_TYPE_IMAGES, std::string()));
1011 }
1012
1013 TEST_F(HostContentSettingsMapTest, ShouldAllowAllContent) {
1014   TestingProfile profile;
1015   HostContentSettingsMap* host_content_settings_map =
1016       profile.GetHostContentSettingsMap();
1017
1018   GURL http_host("http://example.com/");
1019   GURL https_host("https://example.com/");
1020   GURL embedder("chrome://foo");
1021   GURL extension("chrome-extension://foo");
1022   EXPECT_FALSE(host_content_settings_map->ShouldAllowAllContent(
1023                    http_host, embedder, CONTENT_SETTINGS_TYPE_NOTIFICATIONS));
1024   EXPECT_FALSE(host_content_settings_map->ShouldAllowAllContent(
1025                    http_host, embedder, CONTENT_SETTINGS_TYPE_GEOLOCATION));
1026   EXPECT_FALSE(host_content_settings_map->ShouldAllowAllContent(
1027                    http_host, embedder, CONTENT_SETTINGS_TYPE_COOKIES));
1028   EXPECT_TRUE(host_content_settings_map->ShouldAllowAllContent(
1029                   https_host, embedder, CONTENT_SETTINGS_TYPE_COOKIES));
1030   EXPECT_TRUE(host_content_settings_map->ShouldAllowAllContent(
1031                   https_host, embedder, CONTENT_SETTINGS_TYPE_COOKIES));
1032   EXPECT_TRUE(host_content_settings_map->ShouldAllowAllContent(
1033                   embedder, http_host, CONTENT_SETTINGS_TYPE_COOKIES));
1034   EXPECT_TRUE(host_content_settings_map->ShouldAllowAllContent(
1035                   extension, extension, CONTENT_SETTINGS_TYPE_COOKIES));
1036   EXPECT_FALSE(host_content_settings_map->ShouldAllowAllContent(
1037                    extension, extension, CONTENT_SETTINGS_TYPE_PLUGINS));
1038   EXPECT_FALSE(host_content_settings_map->ShouldAllowAllContent(
1039                    extension, http_host, CONTENT_SETTINGS_TYPE_COOKIES));
1040 }
1041
1042 TEST_F(HostContentSettingsMapTest, MigrateClearOnExit) {
1043   TestingProfile profile;
1044   TestingPrefServiceSyncable* prefs = profile.GetTestingPrefService();
1045
1046   prefs->SetBoolean(prefs::kClearSiteDataOnExit, true);
1047
1048   scoped_ptr<Value> patterns(base::JSONReader::Read(
1049       "{\"[*.]example.com,*\":{\"cookies\": 1},"
1050       " \"[*.]other.com,*\":{\"cookies\": 2},"
1051       " \"[*.]third.com,*\":{\"cookies\": 4}}"));
1052   profile.GetPrefs()->Set(prefs::kContentSettingsPatternPairs, *patterns);
1053
1054   scoped_ptr<Value> defaults(base::JSONReader::Read("{\"cookies\": 1}"));
1055   profile.GetPrefs()->Set(prefs::kDefaultContentSettings, *defaults);
1056
1057   HostContentSettingsMap* host_content_settings_map =
1058       profile.GetHostContentSettingsMap();
1059
1060   EXPECT_EQ(CONTENT_SETTING_SESSION_ONLY,
1061             host_content_settings_map->GetDefaultContentSetting(
1062                 CONTENT_SETTINGS_TYPE_COOKIES, NULL));
1063   EXPECT_EQ(CONTENT_SETTING_SESSION_ONLY,
1064             host_content_settings_map->GetContentSetting(
1065                 GURL("http://example.com"),
1066                 GURL("http://example.com"),
1067                 CONTENT_SETTINGS_TYPE_COOKIES,
1068                 std::string()));
1069   EXPECT_EQ(CONTENT_SETTING_BLOCK,
1070             host_content_settings_map->GetContentSetting(
1071                 GURL("http://other.com"),
1072                 GURL("http://other.com"),
1073                 CONTENT_SETTINGS_TYPE_COOKIES,
1074                 std::string()));
1075   EXPECT_EQ(CONTENT_SETTING_SESSION_ONLY,
1076             host_content_settings_map->GetContentSetting(
1077                 GURL("http://third.com"),
1078                 GURL("http://third.com"),
1079                 CONTENT_SETTINGS_TYPE_COOKIES,
1080                 std::string()));
1081 }