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"
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
;
21 ContentSettingsUsagesState::CommittedDetails
CreateDetailsWithURL(
23 ContentSettingsUsagesState::CommittedDetails details
;
24 details
.current_url_valid
= true;
25 details
.current_url
= url
;
29 class ContentSettingsUsagesStateTests
: public testing::Test
{
31 ContentSettingsUsagesStateTests()
32 : ui_thread_(BrowserThread::UI
, &message_loop_
) {
36 void ClearOnNewOrigin(ContentSettingsType type
) {
37 TestingProfile profile
;
38 ContentSettingsUsagesState
state(
39 HostContentSettingsMapFactory::GetForProfile(&profile
),
41 prefs::kAcceptLanguages
,
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
),
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
),
63 CONTENT_SETTING_BLOCK
);
64 state
.OnPermissionSet(url_1
, false);
66 ContentSettingsUsagesState::StateMap 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
)
76 EXPECT_TRUE(tab_state_flags
&
77 ContentSettingsUsagesState::TABSTATE_HAS_EXCEPTION
)
79 EXPECT_FALSE(tab_state_flags
&
80 ContentSettingsUsagesState::TABSTATE_HAS_CHANGED
)
82 EXPECT_TRUE(tab_state_flags
&
83 ContentSettingsUsagesState::TABSTATE_HAS_ANY_ICON
)
85 EXPECT_EQ(1U, formatted_host_per_state
[CONTENT_SETTING_ALLOW
].size());
87 formatted_host_per_state
[CONTENT_SETTING_ALLOW
].count(
90 EXPECT_EQ(1U, formatted_host_per_state
[CONTENT_SETTING_BLOCK
].size());
92 formatted_host_per_state
[CONTENT_SETTING_BLOCK
].count(
95 state
.OnPermissionSet(url_0
, false);
97 formatted_host_per_state
.clear();
99 state
.GetDetailedInfo(&formatted_host_per_state
, &tab_state_flags
);
100 EXPECT_FALSE(tab_state_flags
&
101 ContentSettingsUsagesState::TABSTATE_HAS_ANY_ALLOWED
)
103 EXPECT_TRUE(tab_state_flags
&
104 ContentSettingsUsagesState::TABSTATE_HAS_EXCEPTION
)
106 EXPECT_TRUE(tab_state_flags
&
107 ContentSettingsUsagesState::TABSTATE_HAS_CHANGED
)
109 EXPECT_TRUE(tab_state_flags
&
110 ContentSettingsUsagesState::TABSTATE_HAS_ANY_ICON
)
112 EXPECT_EQ(0U, formatted_host_per_state
[CONTENT_SETTING_ALLOW
].size());
113 EXPECT_EQ(2U, formatted_host_per_state
[CONTENT_SETTING_BLOCK
].size());
115 formatted_host_per_state
[CONTENT_SETTING_BLOCK
].count(
118 formatted_host_per_state
[CONTENT_SETTING_BLOCK
].count(
121 state
.OnPermissionSet(url_0
, true);
123 details
.previous_url
= url_0
;
124 state
.DidNavigate(details
);
126 ContentSettingsUsagesState::StateMap new_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();
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
),
147 prefs::kAcceptLanguages
,
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
),
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
),
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
),
178 CONTENT_SETTING_ALLOW
);
179 state
.OnPermissionSet(url_2
, true);
181 ContentSettingsUsagesState::StateMap 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());
191 formatted_host_per_state
[CONTENT_SETTING_ALLOW
].count(
194 formatted_host_per_state
[CONTENT_SETTING_ALLOW
].count(
197 formatted_host_per_state
[CONTENT_SETTING_ALLOW
].count(
200 state
.OnPermissionSet(url_1
, false);
201 formatted_host_per_state
.clear();
203 state
.GetDetailedInfo(&formatted_host_per_state
, &tab_state_flags
);
205 EXPECT_EQ(2U, formatted_host_per_state
[CONTENT_SETTING_ALLOW
].size());
207 formatted_host_per_state
[CONTENT_SETTING_ALLOW
].count(
210 formatted_host_per_state
[CONTENT_SETTING_ALLOW
].count(
212 EXPECT_EQ(1U, formatted_host_per_state
[CONTENT_SETTING_BLOCK
].size());
214 formatted_host_per_state
[CONTENT_SETTING_BLOCK
].count(
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
);