Roll src/third_party/WebKit f298044:aa8346d (svn 202628:202629)
[chromium-blink-merge.git] / chrome / browser / content_settings / content_settings_usages_state_unittest.cc
blob8fcbb67ca79ab1afbd1c19c029d667661b462d1b
1 // Copyright 2013 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 "components/content_settings/core/browser/content_settings_usages_state.h"
7 #include <string>
9 #include "base/message_loop/message_loop.h"
10 #include "chrome/browser/content_settings/host_content_settings_map_factory.h"
11 #include "chrome/common/pref_names.h"
12 #include "chrome/test/base/testing_profile.h"
13 #include "components/content_settings/core/browser/host_content_settings_map.h"
14 #include "content/public/test/test_browser_thread.h"
15 #include "testing/gtest/include/gtest/gtest.h"
17 using content::BrowserThread;
19 namespace {
21 ContentSettingsUsagesState::CommittedDetails CreateDetailsWithURL(
22 const GURL& url) {
23 ContentSettingsUsagesState::CommittedDetails details;
24 details.current_url_valid = true;
25 details.current_url = url;
26 return details;
29 class ContentSettingsUsagesStateTests : public testing::Test {
30 public:
31 ContentSettingsUsagesStateTests()
32 : ui_thread_(BrowserThread::UI, &message_loop_) {
35 protected:
36 void ClearOnNewOrigin(ContentSettingsType type) {
37 TestingProfile profile;
38 ContentSettingsUsagesState state(
39 HostContentSettingsMapFactory::GetForProfile(&profile),
40 type,
41 prefs::kAcceptLanguages,
42 profile.GetPrefs());
43 GURL url_0("http://www.example.com");
45 ContentSettingsUsagesState::CommittedDetails details =
46 CreateDetailsWithURL(url_0);
47 state.DidNavigate(details);
49 HostContentSettingsMapFactory::GetForProfile(&profile)->SetContentSetting(
50 ContentSettingsPattern::FromURLNoWildcard(url_0),
51 ContentSettingsPattern::FromURLNoWildcard(url_0),
52 type,
53 std::string(),
54 CONTENT_SETTING_ALLOW);
55 state.OnPermissionSet(url_0, true);
57 GURL url_1("http://www.example1.com");
58 HostContentSettingsMapFactory::GetForProfile(&profile)->SetContentSetting(
59 ContentSettingsPattern::FromURLNoWildcard(url_1),
60 ContentSettingsPattern::FromURLNoWildcard(url_0),
61 type,
62 std::string(),
63 CONTENT_SETTING_BLOCK);
64 state.OnPermissionSet(url_1, false);
66 ContentSettingsUsagesState::StateMap state_map =
67 state.state_map();
68 EXPECT_EQ(2U, state_map.size());
70 ContentSettingsUsagesState::FormattedHostsPerState formatted_host_per_state;
71 unsigned int tab_state_flags = 0;
72 state.GetDetailedInfo(&formatted_host_per_state, &tab_state_flags);
73 EXPECT_TRUE(tab_state_flags &
74 ContentSettingsUsagesState::TABSTATE_HAS_ANY_ALLOWED)
75 << tab_state_flags;
76 EXPECT_TRUE(tab_state_flags &
77 ContentSettingsUsagesState::TABSTATE_HAS_EXCEPTION)
78 << tab_state_flags;
79 EXPECT_FALSE(tab_state_flags &
80 ContentSettingsUsagesState::TABSTATE_HAS_CHANGED)
81 << tab_state_flags;
82 EXPECT_TRUE(tab_state_flags &
83 ContentSettingsUsagesState::TABSTATE_HAS_ANY_ICON)
84 << tab_state_flags;
85 EXPECT_EQ(1U, formatted_host_per_state[CONTENT_SETTING_ALLOW].size());
86 EXPECT_EQ(1U,
87 formatted_host_per_state[CONTENT_SETTING_ALLOW].count(
88 url_0.host()));
90 EXPECT_EQ(1U, formatted_host_per_state[CONTENT_SETTING_BLOCK].size());
91 EXPECT_EQ(1U,
92 formatted_host_per_state[CONTENT_SETTING_BLOCK].count(
93 url_1.host()));
95 state.OnPermissionSet(url_0, false);
97 formatted_host_per_state.clear();
98 tab_state_flags = 0;
99 state.GetDetailedInfo(&formatted_host_per_state, &tab_state_flags);
100 EXPECT_FALSE(tab_state_flags &
101 ContentSettingsUsagesState::TABSTATE_HAS_ANY_ALLOWED)
102 << tab_state_flags;
103 EXPECT_TRUE(tab_state_flags &
104 ContentSettingsUsagesState::TABSTATE_HAS_EXCEPTION)
105 << tab_state_flags;
106 EXPECT_TRUE(tab_state_flags &
107 ContentSettingsUsagesState::TABSTATE_HAS_CHANGED)
108 << tab_state_flags;
109 EXPECT_TRUE(tab_state_flags &
110 ContentSettingsUsagesState::TABSTATE_HAS_ANY_ICON)
111 << tab_state_flags;
112 EXPECT_EQ(0U, formatted_host_per_state[CONTENT_SETTING_ALLOW].size());
113 EXPECT_EQ(2U, formatted_host_per_state[CONTENT_SETTING_BLOCK].size());
114 EXPECT_EQ(1U,
115 formatted_host_per_state[CONTENT_SETTING_BLOCK].count(
116 url_0.host()));
117 EXPECT_EQ(1U,
118 formatted_host_per_state[CONTENT_SETTING_BLOCK].count(
119 url_1.host()));
121 state.OnPermissionSet(url_0, true);
123 details.previous_url = url_0;
124 state.DidNavigate(details);
126 ContentSettingsUsagesState::StateMap new_state_map =
127 state.state_map();
128 EXPECT_EQ(state_map.size(), new_state_map.size());
130 details.current_url = GURL("http://foo.com");
131 state.DidNavigate(details);
133 EXPECT_TRUE(state.state_map().empty());
135 formatted_host_per_state.clear();
136 tab_state_flags = 0;
137 state.GetDetailedInfo(&formatted_host_per_state, &tab_state_flags);
138 EXPECT_TRUE(formatted_host_per_state.empty());
139 EXPECT_EQ(0U, tab_state_flags);
142 void ShowPortOnSameHost(ContentSettingsType type) {
143 TestingProfile profile;
144 ContentSettingsUsagesState state(
145 HostContentSettingsMapFactory::GetForProfile(&profile),
146 type,
147 prefs::kAcceptLanguages,
148 profile.GetPrefs());
149 GURL url_0("http://www.example.com");
151 ContentSettingsUsagesState::CommittedDetails details =
152 CreateDetailsWithURL(url_0);
153 state.DidNavigate(details);
155 HostContentSettingsMapFactory::GetForProfile(&profile)->SetContentSetting(
156 ContentSettingsPattern::FromURLNoWildcard(url_0),
157 ContentSettingsPattern::FromURLNoWildcard(url_0),
158 type,
159 std::string(),
160 CONTENT_SETTING_ALLOW);
161 state.OnPermissionSet(url_0, true);
163 GURL url_1("https://www.example.com");
164 HostContentSettingsMapFactory::GetForProfile(&profile)->SetContentSetting(
165 ContentSettingsPattern::FromURLNoWildcard(url_1),
166 ContentSettingsPattern::FromURLNoWildcard(url_0),
167 type,
168 std::string(),
169 CONTENT_SETTING_ALLOW);
170 state.OnPermissionSet(url_1, true);
172 GURL url_2("http://www.example1.com");
173 HostContentSettingsMapFactory::GetForProfile(&profile)->SetContentSetting(
174 ContentSettingsPattern::FromURLNoWildcard(url_2),
175 ContentSettingsPattern::FromURLNoWildcard(url_0),
176 type,
177 std::string(),
178 CONTENT_SETTING_ALLOW);
179 state.OnPermissionSet(url_2, true);
181 ContentSettingsUsagesState::StateMap state_map =
182 state.state_map();
183 EXPECT_EQ(3U, state_map.size());
185 ContentSettingsUsagesState::FormattedHostsPerState formatted_host_per_state;
186 unsigned int tab_state_flags = 0;
187 state.GetDetailedInfo(&formatted_host_per_state, &tab_state_flags);
189 EXPECT_EQ(3U, formatted_host_per_state[CONTENT_SETTING_ALLOW].size());
190 EXPECT_EQ(1U,
191 formatted_host_per_state[CONTENT_SETTING_ALLOW].count(
192 url_0.spec()));
193 EXPECT_EQ(1U,
194 formatted_host_per_state[CONTENT_SETTING_ALLOW].count(
195 url_1.spec()));
196 EXPECT_EQ(1U,
197 formatted_host_per_state[CONTENT_SETTING_ALLOW].count(
198 url_2.host()));
200 state.OnPermissionSet(url_1, false);
201 formatted_host_per_state.clear();
202 tab_state_flags = 0;
203 state.GetDetailedInfo(&formatted_host_per_state, &tab_state_flags);
205 EXPECT_EQ(2U, formatted_host_per_state[CONTENT_SETTING_ALLOW].size());
206 EXPECT_EQ(1U,
207 formatted_host_per_state[CONTENT_SETTING_ALLOW].count(
208 url_0.spec()));
209 EXPECT_EQ(1U,
210 formatted_host_per_state[CONTENT_SETTING_ALLOW].count(
211 url_2.host()));
212 EXPECT_EQ(1U, formatted_host_per_state[CONTENT_SETTING_BLOCK].size());
213 EXPECT_EQ(1U,
214 formatted_host_per_state[CONTENT_SETTING_BLOCK].count(
215 url_1.spec()));
218 protected:
219 base::MessageLoop message_loop_;
220 content::TestBrowserThread ui_thread_;
223 TEST_F(ContentSettingsUsagesStateTests, ClearOnNewOriginForGeolocation) {
224 ClearOnNewOrigin(CONTENT_SETTINGS_TYPE_GEOLOCATION);
227 TEST_F(ContentSettingsUsagesStateTests, ClearOnNewOriginForMidi) {
228 ClearOnNewOrigin(CONTENT_SETTINGS_TYPE_MIDI_SYSEX);
231 TEST_F(ContentSettingsUsagesStateTests, ShowPortOnSameHostForGeolocation) {
232 ShowPortOnSameHost(CONTENT_SETTINGS_TYPE_GEOLOCATION);
235 TEST_F(ContentSettingsUsagesStateTests, ShowPortOnSameHostForMidi) {
236 ShowPortOnSameHost(CONTENT_SETTINGS_TYPE_MIDI_SYSEX);
239 } // namespace