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/common/pref_names.h"
11 #include "chrome/test/base/testing_profile.h"
12 #include "components/content_settings/core/browser/host_content_settings_map.h"
13 #include "content/public/test/test_browser_thread.h"
14 #include "testing/gtest/include/gtest/gtest.h"
16 using content::BrowserThread
;
20 ContentSettingsUsagesState::CommittedDetails
CreateDetailsWithURL(
22 ContentSettingsUsagesState::CommittedDetails details
;
23 details
.current_url_valid
= true;
24 details
.current_url
= url
;
28 class ContentSettingsUsagesStateTests
: public testing::Test
{
30 ContentSettingsUsagesStateTests()
31 : ui_thread_(BrowserThread::UI
, &message_loop_
) {
35 void ClearOnNewOrigin(ContentSettingsType type
) {
36 TestingProfile profile
;
37 ContentSettingsUsagesState
state(profile
.GetHostContentSettingsMap(), type
,
38 prefs::kAcceptLanguages
,
40 GURL
url_0("http://www.example.com");
42 ContentSettingsUsagesState::CommittedDetails details
=
43 CreateDetailsWithURL(url_0
);
44 state
.DidNavigate(details
);
46 profile
.GetHostContentSettingsMap()->SetContentSetting(
47 ContentSettingsPattern::FromURLNoWildcard(url_0
),
48 ContentSettingsPattern::FromURLNoWildcard(url_0
),
51 CONTENT_SETTING_ALLOW
);
52 state
.OnPermissionSet(url_0
, true);
54 GURL
url_1("http://www.example1.com");
55 profile
.GetHostContentSettingsMap()->SetContentSetting(
56 ContentSettingsPattern::FromURLNoWildcard(url_1
),
57 ContentSettingsPattern::FromURLNoWildcard(url_0
),
60 CONTENT_SETTING_BLOCK
);
61 state
.OnPermissionSet(url_1
, false);
63 ContentSettingsUsagesState::StateMap state_map
=
65 EXPECT_EQ(2U, state_map
.size());
67 ContentSettingsUsagesState::FormattedHostsPerState formatted_host_per_state
;
68 unsigned int tab_state_flags
= 0;
69 state
.GetDetailedInfo(&formatted_host_per_state
, &tab_state_flags
);
70 EXPECT_TRUE(tab_state_flags
&
71 ContentSettingsUsagesState::TABSTATE_HAS_ANY_ALLOWED
)
73 EXPECT_TRUE(tab_state_flags
&
74 ContentSettingsUsagesState::TABSTATE_HAS_EXCEPTION
)
76 EXPECT_FALSE(tab_state_flags
&
77 ContentSettingsUsagesState::TABSTATE_HAS_CHANGED
)
79 EXPECT_TRUE(tab_state_flags
&
80 ContentSettingsUsagesState::TABSTATE_HAS_ANY_ICON
)
82 EXPECT_EQ(1U, formatted_host_per_state
[CONTENT_SETTING_ALLOW
].size());
84 formatted_host_per_state
[CONTENT_SETTING_ALLOW
].count(
87 EXPECT_EQ(1U, formatted_host_per_state
[CONTENT_SETTING_BLOCK
].size());
89 formatted_host_per_state
[CONTENT_SETTING_BLOCK
].count(
92 state
.OnPermissionSet(url_0
, false);
94 formatted_host_per_state
.clear();
96 state
.GetDetailedInfo(&formatted_host_per_state
, &tab_state_flags
);
97 EXPECT_FALSE(tab_state_flags
&
98 ContentSettingsUsagesState::TABSTATE_HAS_ANY_ALLOWED
)
100 EXPECT_TRUE(tab_state_flags
&
101 ContentSettingsUsagesState::TABSTATE_HAS_EXCEPTION
)
103 EXPECT_TRUE(tab_state_flags
&
104 ContentSettingsUsagesState::TABSTATE_HAS_CHANGED
)
106 EXPECT_TRUE(tab_state_flags
&
107 ContentSettingsUsagesState::TABSTATE_HAS_ANY_ICON
)
109 EXPECT_EQ(0U, formatted_host_per_state
[CONTENT_SETTING_ALLOW
].size());
110 EXPECT_EQ(2U, formatted_host_per_state
[CONTENT_SETTING_BLOCK
].size());
112 formatted_host_per_state
[CONTENT_SETTING_BLOCK
].count(
115 formatted_host_per_state
[CONTENT_SETTING_BLOCK
].count(
118 state
.OnPermissionSet(url_0
, true);
120 details
.previous_url
= url_0
;
121 state
.DidNavigate(details
);
123 ContentSettingsUsagesState::StateMap new_state_map
=
125 EXPECT_EQ(state_map
.size(), new_state_map
.size());
127 details
.current_url
= GURL("http://foo.com");
128 state
.DidNavigate(details
);
130 EXPECT_TRUE(state
.state_map().empty());
132 formatted_host_per_state
.clear();
134 state
.GetDetailedInfo(&formatted_host_per_state
, &tab_state_flags
);
135 EXPECT_TRUE(formatted_host_per_state
.empty());
136 EXPECT_EQ(0U, tab_state_flags
);
139 void ShowPortOnSameHost(ContentSettingsType type
) {
140 TestingProfile profile
;
141 ContentSettingsUsagesState
state(profile
.GetHostContentSettingsMap(), type
,
142 prefs::kAcceptLanguages
,
144 GURL
url_0("http://www.example.com");
146 ContentSettingsUsagesState::CommittedDetails details
=
147 CreateDetailsWithURL(url_0
);
148 state
.DidNavigate(details
);
150 profile
.GetHostContentSettingsMap()->SetContentSetting(
151 ContentSettingsPattern::FromURLNoWildcard(url_0
),
152 ContentSettingsPattern::FromURLNoWildcard(url_0
),
155 CONTENT_SETTING_ALLOW
);
156 state
.OnPermissionSet(url_0
, true);
158 GURL
url_1("https://www.example.com");
159 profile
.GetHostContentSettingsMap()->SetContentSetting(
160 ContentSettingsPattern::FromURLNoWildcard(url_1
),
161 ContentSettingsPattern::FromURLNoWildcard(url_0
),
164 CONTENT_SETTING_ALLOW
);
165 state
.OnPermissionSet(url_1
, true);
167 GURL
url_2("http://www.example1.com");
168 profile
.GetHostContentSettingsMap()->SetContentSetting(
169 ContentSettingsPattern::FromURLNoWildcard(url_2
),
170 ContentSettingsPattern::FromURLNoWildcard(url_0
),
173 CONTENT_SETTING_ALLOW
);
174 state
.OnPermissionSet(url_2
, true);
176 ContentSettingsUsagesState::StateMap state_map
=
178 EXPECT_EQ(3U, state_map
.size());
180 ContentSettingsUsagesState::FormattedHostsPerState formatted_host_per_state
;
181 unsigned int tab_state_flags
= 0;
182 state
.GetDetailedInfo(&formatted_host_per_state
, &tab_state_flags
);
184 EXPECT_EQ(3U, formatted_host_per_state
[CONTENT_SETTING_ALLOW
].size());
186 formatted_host_per_state
[CONTENT_SETTING_ALLOW
].count(
189 formatted_host_per_state
[CONTENT_SETTING_ALLOW
].count(
192 formatted_host_per_state
[CONTENT_SETTING_ALLOW
].count(
195 state
.OnPermissionSet(url_1
, false);
196 formatted_host_per_state
.clear();
198 state
.GetDetailedInfo(&formatted_host_per_state
, &tab_state_flags
);
200 EXPECT_EQ(2U, formatted_host_per_state
[CONTENT_SETTING_ALLOW
].size());
202 formatted_host_per_state
[CONTENT_SETTING_ALLOW
].count(
205 formatted_host_per_state
[CONTENT_SETTING_ALLOW
].count(
207 EXPECT_EQ(1U, formatted_host_per_state
[CONTENT_SETTING_BLOCK
].size());
209 formatted_host_per_state
[CONTENT_SETTING_BLOCK
].count(
214 base::MessageLoop message_loop_
;
215 content::TestBrowserThread ui_thread_
;
218 TEST_F(ContentSettingsUsagesStateTests
, ClearOnNewOriginForGeolocation
) {
219 ClearOnNewOrigin(CONTENT_SETTINGS_TYPE_GEOLOCATION
);
222 TEST_F(ContentSettingsUsagesStateTests
, ClearOnNewOriginForMidi
) {
223 ClearOnNewOrigin(CONTENT_SETTINGS_TYPE_MIDI_SYSEX
);
226 TEST_F(ContentSettingsUsagesStateTests
, ShowPortOnSameHostForGeolocation
) {
227 ShowPortOnSameHost(CONTENT_SETTINGS_TYPE_GEOLOCATION
);
230 TEST_F(ContentSettingsUsagesStateTests
, ShowPortOnSameHostForMidi
) {
231 ShowPortOnSameHost(CONTENT_SETTINGS_TYPE_MIDI_SYSEX
);