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 "chrome/browser/content_settings/content_settings_usages_state.h"
9 #include "base/message_loop/message_loop.h"
10 #include "chrome/browser/content_settings/host_content_settings_map.h"
11 #include "chrome/test/base/testing_profile.h"
12 #include "content/public/browser/navigation_details.h"
13 #include "content/public/browser/navigation_entry.h"
14 #include "content/public/test/test_browser_thread.h"
15 #include "testing/gtest/include/gtest/gtest.h"
17 using content::BrowserThread
;
18 using content::NavigationEntry
;
22 class ContentSettingsUsagesStateTests
: public testing::Test
{
24 ContentSettingsUsagesStateTests()
25 : ui_thread_(BrowserThread::UI
, &message_loop_
) {
29 void ClearOnNewOrigin(ContentSettingsType type
) {
30 TestingProfile profile
;
31 ContentSettingsUsagesState
state(&profile
, type
);
32 GURL
url_0("http://www.example.com");
34 scoped_ptr
<NavigationEntry
> entry(NavigationEntry::Create());
36 content::LoadCommittedDetails load_committed_details
;
37 load_committed_details
.entry
= entry
.get();
38 state
.DidNavigate(load_committed_details
);
40 profile
.GetHostContentSettingsMap()->SetContentSetting(
41 ContentSettingsPattern::FromURLNoWildcard(url_0
),
42 ContentSettingsPattern::FromURLNoWildcard(url_0
),
45 CONTENT_SETTING_ALLOW
);
46 state
.OnPermissionSet(url_0
, true);
48 GURL
url_1("http://www.example1.com");
49 profile
.GetHostContentSettingsMap()->SetContentSetting(
50 ContentSettingsPattern::FromURLNoWildcard(url_1
),
51 ContentSettingsPattern::FromURLNoWildcard(url_0
),
54 CONTENT_SETTING_BLOCK
);
55 state
.OnPermissionSet(url_1
, false);
57 ContentSettingsUsagesState::StateMap state_map
=
59 EXPECT_EQ(2U, state_map
.size());
61 ContentSettingsUsagesState::FormattedHostsPerState formatted_host_per_state
;
62 unsigned int tab_state_flags
= 0;
63 state
.GetDetailedInfo(&formatted_host_per_state
, &tab_state_flags
);
64 EXPECT_TRUE(tab_state_flags
&
65 ContentSettingsUsagesState::TABSTATE_HAS_ANY_ALLOWED
)
67 EXPECT_TRUE(tab_state_flags
&
68 ContentSettingsUsagesState::TABSTATE_HAS_EXCEPTION
)
70 EXPECT_FALSE(tab_state_flags
&
71 ContentSettingsUsagesState::TABSTATE_HAS_CHANGED
)
73 EXPECT_TRUE(tab_state_flags
&
74 ContentSettingsUsagesState::TABSTATE_HAS_ANY_ICON
)
76 EXPECT_EQ(1U, formatted_host_per_state
[CONTENT_SETTING_ALLOW
].size());
78 formatted_host_per_state
[CONTENT_SETTING_ALLOW
].count(
81 EXPECT_EQ(1U, formatted_host_per_state
[CONTENT_SETTING_BLOCK
].size());
83 formatted_host_per_state
[CONTENT_SETTING_BLOCK
].count(
86 state
.OnPermissionSet(url_0
, false);
88 formatted_host_per_state
.clear();
90 state
.GetDetailedInfo(&formatted_host_per_state
, &tab_state_flags
);
91 EXPECT_FALSE(tab_state_flags
&
92 ContentSettingsUsagesState::TABSTATE_HAS_ANY_ALLOWED
)
94 EXPECT_TRUE(tab_state_flags
&
95 ContentSettingsUsagesState::TABSTATE_HAS_EXCEPTION
)
97 EXPECT_TRUE(tab_state_flags
&
98 ContentSettingsUsagesState::TABSTATE_HAS_CHANGED
)
100 EXPECT_TRUE(tab_state_flags
&
101 ContentSettingsUsagesState::TABSTATE_HAS_ANY_ICON
)
103 EXPECT_EQ(0U, formatted_host_per_state
[CONTENT_SETTING_ALLOW
].size());
104 EXPECT_EQ(2U, formatted_host_per_state
[CONTENT_SETTING_BLOCK
].size());
106 formatted_host_per_state
[CONTENT_SETTING_BLOCK
].count(
109 formatted_host_per_state
[CONTENT_SETTING_BLOCK
].count(
112 state
.OnPermissionSet(url_0
, true);
114 load_committed_details
.previous_url
= url_0
;
115 state
.DidNavigate(load_committed_details
);
117 ContentSettingsUsagesState::StateMap new_state_map
=
119 EXPECT_EQ(state_map
.size(), new_state_map
.size());
121 GURL
different_url("http://foo.com");
122 entry
->SetURL(different_url
);
123 state
.DidNavigate(load_committed_details
);
125 EXPECT_TRUE(state
.state_map().empty());
127 formatted_host_per_state
.clear();
129 state
.GetDetailedInfo(&formatted_host_per_state
, &tab_state_flags
);
130 EXPECT_TRUE(formatted_host_per_state
.empty());
131 EXPECT_EQ(0U, tab_state_flags
);
134 void ShowPortOnSameHost(ContentSettingsType type
) {
135 TestingProfile profile
;
136 ContentSettingsUsagesState
state(&profile
, type
);
137 GURL
url_0("http://www.example.com");
139 scoped_ptr
<NavigationEntry
> entry(NavigationEntry::Create());
140 entry
->SetURL(url_0
);
141 content::LoadCommittedDetails load_committed_details
;
142 load_committed_details
.entry
= entry
.get();
143 state
.DidNavigate(load_committed_details
);
145 profile
.GetHostContentSettingsMap()->SetContentSetting(
146 ContentSettingsPattern::FromURLNoWildcard(url_0
),
147 ContentSettingsPattern::FromURLNoWildcard(url_0
),
150 CONTENT_SETTING_ALLOW
);
151 state
.OnPermissionSet(url_0
, true);
153 GURL
url_1("https://www.example.com");
154 profile
.GetHostContentSettingsMap()->SetContentSetting(
155 ContentSettingsPattern::FromURLNoWildcard(url_1
),
156 ContentSettingsPattern::FromURLNoWildcard(url_0
),
159 CONTENT_SETTING_ALLOW
);
160 state
.OnPermissionSet(url_1
, true);
162 GURL
url_2("http://www.example1.com");
163 profile
.GetHostContentSettingsMap()->SetContentSetting(
164 ContentSettingsPattern::FromURLNoWildcard(url_2
),
165 ContentSettingsPattern::FromURLNoWildcard(url_0
),
168 CONTENT_SETTING_ALLOW
);
169 state
.OnPermissionSet(url_2
, true);
171 ContentSettingsUsagesState::StateMap state_map
=
173 EXPECT_EQ(3U, state_map
.size());
175 ContentSettingsUsagesState::FormattedHostsPerState formatted_host_per_state
;
176 unsigned int tab_state_flags
= 0;
177 state
.GetDetailedInfo(&formatted_host_per_state
, &tab_state_flags
);
179 EXPECT_EQ(3U, formatted_host_per_state
[CONTENT_SETTING_ALLOW
].size());
181 formatted_host_per_state
[CONTENT_SETTING_ALLOW
].count(
184 formatted_host_per_state
[CONTENT_SETTING_ALLOW
].count(
187 formatted_host_per_state
[CONTENT_SETTING_ALLOW
].count(
190 state
.OnPermissionSet(url_1
, false);
191 formatted_host_per_state
.clear();
193 state
.GetDetailedInfo(&formatted_host_per_state
, &tab_state_flags
);
195 EXPECT_EQ(2U, formatted_host_per_state
[CONTENT_SETTING_ALLOW
].size());
197 formatted_host_per_state
[CONTENT_SETTING_ALLOW
].count(
200 formatted_host_per_state
[CONTENT_SETTING_ALLOW
].count(
202 EXPECT_EQ(1U, formatted_host_per_state
[CONTENT_SETTING_BLOCK
].size());
204 formatted_host_per_state
[CONTENT_SETTING_BLOCK
].count(
209 base::MessageLoop message_loop_
;
210 content::TestBrowserThread ui_thread_
;
213 TEST_F(ContentSettingsUsagesStateTests
, ClearOnNewOriginForGeolocation
) {
214 ClearOnNewOrigin(CONTENT_SETTINGS_TYPE_GEOLOCATION
);
217 TEST_F(ContentSettingsUsagesStateTests
, ClearOnNewOriginForMidi
) {
218 ClearOnNewOrigin(CONTENT_SETTINGS_TYPE_MIDI_SYSEX
);
221 TEST_F(ContentSettingsUsagesStateTests
, ShowPortOnSameHostForGeolocation
) {
222 ShowPortOnSameHost(CONTENT_SETTINGS_TYPE_GEOLOCATION
);
225 TEST_F(ContentSettingsUsagesStateTests
, ShowPortOnSameHostForMidi
) {
226 ShowPortOnSameHost(CONTENT_SETTINGS_TYPE_MIDI_SYSEX
);