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.
5 #include "base/message_loop/message_loop.h"
6 #include "base/prefs/pref_service.h"
7 #include "chrome/browser/content_settings/cookie_settings_factory.h"
8 #include "chrome/test/base/testing_profile.h"
9 #include "components/content_settings/core/browser/cookie_settings.h"
10 #include "components/content_settings/core/common/content_settings_pattern.h"
11 #include "content/public/test/test_browser_thread.h"
12 #include "testing/gtest/include/gtest/gtest.h"
17 class CookieSettingsFactoryTest
: public testing::Test
{
19 CookieSettingsFactoryTest()
20 : ui_thread_(content::BrowserThread::UI
, &message_loop_
),
21 cookie_settings_(CookieSettingsFactory::GetForProfile(&profile_
).get()),
22 kBlockedSite("http://ads.thirdparty.com"),
23 kAllowedSite("http://good.allays.com"),
24 kFirstPartySite("http://cool.things.com"),
25 kHttpsSite("https://example.com") {}
28 base::MessageLoop message_loop_
;
29 content::TestBrowserThread ui_thread_
;
30 TestingProfile profile_
;
31 content_settings::CookieSettings
* cookie_settings_
;
32 const GURL kBlockedSite
;
33 const GURL kAllowedSite
;
34 const GURL kFirstPartySite
;
35 const GURL kHttpsSite
;
38 TEST_F(CookieSettingsFactoryTest
, IncognitoBehaviorOfBlockingRules
) {
39 scoped_refptr
<content_settings::CookieSettings
> incognito_settings
=
40 CookieSettingsFactory::GetForProfile(profile_
.GetOffTheRecordProfile());
42 // Modify the regular cookie settings after the incognito cookie settings have
44 cookie_settings_
->SetCookieSetting(
45 ContentSettingsPattern::FromURL(kBlockedSite
),
46 ContentSettingsPattern::Wildcard(), CONTENT_SETTING_BLOCK
);
48 // The modification should apply to the regular profile and incognito profile.
50 cookie_settings_
->IsReadingCookieAllowed(kBlockedSite
, kBlockedSite
));
52 incognito_settings
->IsReadingCookieAllowed(kBlockedSite
, kBlockedSite
));
54 // Modify an incognito cookie setting and check that this does not propagate
56 incognito_settings
->SetCookieSetting(
57 ContentSettingsPattern::FromURL(kHttpsSite
),
58 ContentSettingsPattern::Wildcard(), CONTENT_SETTING_BLOCK
);
59 EXPECT_TRUE(cookie_settings_
->IsReadingCookieAllowed(kHttpsSite
, kHttpsSite
));
61 incognito_settings
->IsReadingCookieAllowed(kHttpsSite
, kHttpsSite
));
64 TEST_F(CookieSettingsFactoryTest
, IncognitoBehaviorOfBlockingEverything
) {
65 scoped_refptr
<content_settings::CookieSettings
> incognito_settings
=
66 CookieSettingsFactory::GetForProfile(profile_
.GetOffTheRecordProfile());
68 // Apply the general blocking to the regular profile.
69 cookie_settings_
->SetDefaultCookieSetting(CONTENT_SETTING_BLOCK
);
71 // It should be effective for regular and incognito session.
72 EXPECT_FALSE(cookie_settings_
->IsReadingCookieAllowed(kFirstPartySite
,
74 EXPECT_FALSE(incognito_settings
->IsReadingCookieAllowed(kFirstPartySite
,
77 // A whitelisted item set in incognito mode should only apply to incognito
79 incognito_settings
->SetCookieSetting(
80 ContentSettingsPattern::FromURL(kAllowedSite
),
81 ContentSettingsPattern::Wildcard(), CONTENT_SETTING_ALLOW
);
83 incognito_settings
->IsReadingCookieAllowed(kAllowedSite
, kAllowedSite
));
85 cookie_settings_
->IsReadingCookieAllowed(kAllowedSite
, kAllowedSite
));
87 // A whitelisted item set in regular mode should apply to regular and
89 cookie_settings_
->SetCookieSetting(
90 ContentSettingsPattern::FromURL(kHttpsSite
),
91 ContentSettingsPattern::Wildcard(), CONTENT_SETTING_ALLOW
);
93 incognito_settings
->IsReadingCookieAllowed(kHttpsSite
, kHttpsSite
));
94 EXPECT_TRUE(cookie_settings_
->IsReadingCookieAllowed(kHttpsSite
, kHttpsSite
));