summaryrefslogtreecommitdiffstats
path: root/chrome/browser/tab_contents/tab_specific_content_settings_unittest.cc
blob: 5be081cd417ac78e9ab1d78314ef8bb3a46b11c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/tab_contents/tab_specific_content_settings.h"

#include "chrome/test/testing_profile.h"
#include "net/base/cookie_options.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace {
class TestContentSettingsDelegate
    : public TabSpecificContentSettings::Delegate {
 public:
  TestContentSettingsDelegate()
      : settings_changed_(false), content_blocked_(false) {}
  virtual ~TestContentSettingsDelegate() {}

  void Reset() { settings_changed_ = content_blocked_ = false; }

  bool SettingsChanged() { return settings_changed_; }

  bool ContentBlocked() { return content_blocked_; }

  // TabSpecificContentSettings::Delegate implementation.
  virtual void OnContentSettingsAccessed(bool content_was_blocked) {
    settings_changed_ = true;
    content_blocked_ = content_was_blocked;
  }

 private:
  bool settings_changed_;
  bool content_blocked_;

  DISALLOW_COPY_AND_ASSIGN(TestContentSettingsDelegate);
};
}  // namespace

TEST(TabSpecificContentSettingsTest, BlockedContent) {
  TestContentSettingsDelegate test_delegate;
  TestingProfile profile;
  TabSpecificContentSettings content_settings(&test_delegate, &profile);
  net::CookieOptions options;

  // Check that after initializing, nothing is blocked.
  EXPECT_FALSE(content_settings.IsContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES));
  EXPECT_FALSE(
      content_settings.IsContentBlocked(CONTENT_SETTINGS_TYPE_JAVASCRIPT));
  EXPECT_FALSE(
      content_settings.IsContentBlocked(CONTENT_SETTINGS_TYPE_PLUGINS));
  EXPECT_FALSE(
      content_settings.IsContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES));
  EXPECT_FALSE(content_settings.IsContentBlocked(CONTENT_SETTINGS_TYPE_POPUPS));

  // Set a cookie, block access to images, block a popup.
  content_settings.OnCookieAccessed(
      GURL("http://google.com"), "A=B", options, false);
  EXPECT_TRUE(test_delegate.SettingsChanged());
  EXPECT_FALSE(test_delegate.ContentBlocked());
  test_delegate.Reset();
  content_settings.OnContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES,
                                    std::string());
  EXPECT_TRUE(test_delegate.SettingsChanged());
  EXPECT_TRUE(test_delegate.ContentBlocked());
  test_delegate.Reset();
  content_settings.SetPopupsBlocked(true);
  EXPECT_TRUE(test_delegate.SettingsChanged());
  EXPECT_TRUE(test_delegate.ContentBlocked());
  test_delegate.Reset();

  // Check that only the respective content types are affected.
  EXPECT_TRUE(content_settings.IsContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES));
  EXPECT_FALSE(
      content_settings.IsContentBlocked(CONTENT_SETTINGS_TYPE_JAVASCRIPT));
  EXPECT_FALSE(
      content_settings.IsContentBlocked(CONTENT_SETTINGS_TYPE_PLUGINS));
  EXPECT_FALSE(
      content_settings.IsContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES));
  EXPECT_TRUE(content_settings.IsContentBlocked(CONTENT_SETTINGS_TYPE_POPUPS));
  content_settings.OnCookieAccessed(
      GURL("http://google.com"), "A=B", options, false);

  // Block a cookie.
  content_settings.OnCookieAccessed(
      GURL("http://google.com"), "C=D", options, true);
  EXPECT_TRUE(
      content_settings.IsContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES));

  // Reset blocked content settings.
  content_settings.ClearBlockedContentSettingsExceptForCookies();
  EXPECT_TRUE(test_delegate.SettingsChanged());
  EXPECT_FALSE(test_delegate.ContentBlocked());
  EXPECT_FALSE(content_settings.IsContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES));
  EXPECT_FALSE(
      content_settings.IsContentBlocked(CONTENT_SETTINGS_TYPE_JAVASCRIPT));
  EXPECT_FALSE(
      content_settings.IsContentBlocked(CONTENT_SETTINGS_TYPE_PLUGINS));
  EXPECT_TRUE(
      content_settings.IsContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES));
  EXPECT_FALSE(content_settings.IsContentBlocked(CONTENT_SETTINGS_TYPE_POPUPS));

  content_settings.ClearCookieSpecificContentSettings();
  EXPECT_TRUE(test_delegate.SettingsChanged());
  EXPECT_FALSE(test_delegate.ContentBlocked());
  EXPECT_FALSE(content_settings.IsContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES));
  EXPECT_FALSE(
      content_settings.IsContentBlocked(CONTENT_SETTINGS_TYPE_JAVASCRIPT));
  EXPECT_FALSE(
      content_settings.IsContentBlocked(CONTENT_SETTINGS_TYPE_PLUGINS));
  EXPECT_FALSE(
      content_settings.IsContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES));
  EXPECT_FALSE(content_settings.IsContentBlocked(CONTENT_SETTINGS_TYPE_POPUPS));
}

TEST(TabSpecificContentSettingsTest, AllowedContent) {
  TestContentSettingsDelegate test_delegate;
  TestingProfile profile;
  TabSpecificContentSettings content_settings(&test_delegate, &profile);
  net::CookieOptions options;

  ASSERT_FALSE(
      content_settings.IsContentAccessed(CONTENT_SETTINGS_TYPE_IMAGES));
  ASSERT_FALSE(
      content_settings.IsContentAccessed(CONTENT_SETTINGS_TYPE_COOKIES));
  ASSERT_FALSE(
      content_settings.IsContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES));
  content_settings.OnCookieAccessed(
      GURL("http://google.com"), "A=B", options, false);
  ASSERT_TRUE(
      content_settings.IsContentAccessed(CONTENT_SETTINGS_TYPE_COOKIES));
  ASSERT_FALSE(
      content_settings.IsContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES));
  content_settings.OnCookieAccessed(
      GURL("http://google.com"), "C=D", options, true);
  ASSERT_TRUE(
      content_settings.IsContentAccessed(CONTENT_SETTINGS_TYPE_COOKIES));
  ASSERT_TRUE(
      content_settings.IsContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES));
}