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
3// found in the LICENSE file.
4
5#include "chrome/browser/tab_contents/tab_specific_content_settings.h"
6
7#include "chrome/test/testing_profile.h"
8#include "net/base/cookie_monster.h"
9#include "testing/gtest/include/gtest/gtest.h"
10
11namespace {
12class TestContentSettingsDelegate
13    : public TabSpecificContentSettings::Delegate {
14 public:
15  TestContentSettingsDelegate()
16      : settings_changed_(false), content_blocked_(false) {}
17  virtual ~TestContentSettingsDelegate() {}
18
19  void Reset() { settings_changed_ = content_blocked_ = false; }
20
21  bool SettingsChanged() { return settings_changed_; }
22
23  bool ContentBlocked() { return content_blocked_; }
24
25  // TabSpecificContentSettings::Delegate implementation.
26  virtual void OnContentSettingsAccessed(bool content_was_blocked) {
27    settings_changed_ = true;
28    content_blocked_ = content_was_blocked;
29  }
30
31 private:
32  bool settings_changed_;
33  bool content_blocked_;
34
35  DISALLOW_COPY_AND_ASSIGN(TestContentSettingsDelegate);
36};
37}  // namespace
38
39TEST(TabSpecificContentSettingsTest, BlockedContent) {
40  TestContentSettingsDelegate test_delegate;
41  TestingProfile profile;
42  TabSpecificContentSettings content_settings(&test_delegate, &profile);
43  net::CookieOptions options;
44
45  // Check that after initializing, nothing is blocked.
46  EXPECT_FALSE(content_settings.IsContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES));
47  EXPECT_FALSE(
48      content_settings.IsContentBlocked(CONTENT_SETTINGS_TYPE_JAVASCRIPT));
49  EXPECT_FALSE(
50      content_settings.IsContentBlocked(CONTENT_SETTINGS_TYPE_PLUGINS));
51  EXPECT_FALSE(
52      content_settings.IsContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES));
53  EXPECT_FALSE(content_settings.IsContentBlocked(CONTENT_SETTINGS_TYPE_POPUPS));
54
55  // Set a cookie, block access to images, block a popup.
56  content_settings.OnCookieChanged(
57      GURL("http://google.com"), "A=B", options, false);
58  EXPECT_TRUE(test_delegate.SettingsChanged());
59  EXPECT_FALSE(test_delegate.ContentBlocked());
60  test_delegate.Reset();
61  content_settings.OnContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES,
62                                    std::string());
63  EXPECT_TRUE(test_delegate.SettingsChanged());
64  EXPECT_TRUE(test_delegate.ContentBlocked());
65  test_delegate.Reset();
66  content_settings.SetPopupsBlocked(true);
67  EXPECT_TRUE(test_delegate.SettingsChanged());
68  EXPECT_TRUE(test_delegate.ContentBlocked());
69  test_delegate.Reset();
70
71  // Check that only the respective content types are affected.
72  EXPECT_TRUE(content_settings.IsContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES));
73  EXPECT_FALSE(
74      content_settings.IsContentBlocked(CONTENT_SETTINGS_TYPE_JAVASCRIPT));
75  EXPECT_FALSE(
76      content_settings.IsContentBlocked(CONTENT_SETTINGS_TYPE_PLUGINS));
77  EXPECT_FALSE(
78      content_settings.IsContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES));
79  EXPECT_TRUE(content_settings.IsContentBlocked(CONTENT_SETTINGS_TYPE_POPUPS));
80  content_settings.OnCookieChanged(
81      GURL("http://google.com"), "A=B", options, false);
82
83  // Block a cookie.
84  content_settings.OnCookieChanged(
85      GURL("http://google.com"), "C=D", options, true);
86  EXPECT_TRUE(
87      content_settings.IsContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES));
88
89  // Reset blocked content settings.
90  content_settings.ClearBlockedContentSettingsExceptForCookies();
91  EXPECT_TRUE(test_delegate.SettingsChanged());
92  EXPECT_FALSE(test_delegate.ContentBlocked());
93  EXPECT_FALSE(content_settings.IsContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES));
94  EXPECT_FALSE(
95      content_settings.IsContentBlocked(CONTENT_SETTINGS_TYPE_JAVASCRIPT));
96  EXPECT_FALSE(
97      content_settings.IsContentBlocked(CONTENT_SETTINGS_TYPE_PLUGINS));
98  EXPECT_TRUE(
99      content_settings.IsContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES));
100  EXPECT_FALSE(content_settings.IsContentBlocked(CONTENT_SETTINGS_TYPE_POPUPS));
101
102  content_settings.ClearCookieSpecificContentSettings();
103  EXPECT_TRUE(test_delegate.SettingsChanged());
104  EXPECT_FALSE(test_delegate.ContentBlocked());
105  EXPECT_FALSE(content_settings.IsContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES));
106  EXPECT_FALSE(
107      content_settings.IsContentBlocked(CONTENT_SETTINGS_TYPE_JAVASCRIPT));
108  EXPECT_FALSE(
109      content_settings.IsContentBlocked(CONTENT_SETTINGS_TYPE_PLUGINS));
110  EXPECT_FALSE(
111      content_settings.IsContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES));
112  EXPECT_FALSE(content_settings.IsContentBlocked(CONTENT_SETTINGS_TYPE_POPUPS));
113}
114
115TEST(TabSpecificContentSettingsTest, AllowedContent) {
116  TestContentSettingsDelegate test_delegate;
117  TestingProfile profile;
118  TabSpecificContentSettings content_settings(&test_delegate, &profile);
119  net::CookieOptions options;
120
121  ASSERT_FALSE(
122      content_settings.IsContentAccessed(CONTENT_SETTINGS_TYPE_IMAGES));
123  ASSERT_FALSE(
124      content_settings.IsContentAccessed(CONTENT_SETTINGS_TYPE_COOKIES));
125  ASSERT_FALSE(
126      content_settings.IsContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES));
127  content_settings.OnCookieChanged(
128      GURL("http://google.com"), "A=B", options, false);
129  ASSERT_TRUE(
130      content_settings.IsContentAccessed(CONTENT_SETTINGS_TYPE_COOKIES));
131  ASSERT_FALSE(
132      content_settings.IsContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES));
133  content_settings.OnCookieChanged(
134      GURL("http://google.com"), "C=D", options, true);
135  ASSERT_TRUE(
136      content_settings.IsContentAccessed(CONTENT_SETTINGS_TYPE_COOKIES));
137  ASSERT_TRUE(
138      content_settings.IsContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES));
139}
140
141TEST(TabSpecificContentSettingsTest, EmptyCookieList) {
142  TestContentSettingsDelegate test_delegate;
143  TestingProfile profile;
144  TabSpecificContentSettings content_settings(&test_delegate, &profile);
145
146  ASSERT_FALSE(
147      content_settings.IsContentAccessed(CONTENT_SETTINGS_TYPE_COOKIES));
148  ASSERT_FALSE(
149      content_settings.IsContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES));
150  content_settings.OnCookiesRead(
151      GURL("http://google.com"), net::CookieList(), true);
152  ASSERT_FALSE(
153      content_settings.IsContentAccessed(CONTENT_SETTINGS_TYPE_COOKIES));
154  ASSERT_FALSE(
155      content_settings.IsContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES));
156}
157