1 // Copyright (c) 2011 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.
7 #include "base/message_loop/message_loop.h"
8 #include "chrome/browser/content_settings/host_content_settings_map.h"
9 #include "chrome/browser/geolocation/geolocation_settings_state.h"
10 #include "chrome/test/base/testing_profile.h"
11 #include "content/public/browser/navigation_details.h"
12 #include "content/public/browser/navigation_entry.h"
13 #include "content/public/test/test_browser_thread.h"
14 #include "testing/gtest/include/gtest/gtest.h"
16 using content::BrowserThread
;
17 using content::NavigationEntry
;
21 class GeolocationSettingsStateTests
: public testing::Test
{
23 GeolocationSettingsStateTests()
24 : ui_thread_(BrowserThread::UI
, &message_loop_
) {
28 base::MessageLoop message_loop_
;
29 content::TestBrowserThread ui_thread_
;
32 TEST_F(GeolocationSettingsStateTests
, ClearOnNewOrigin
) {
33 TestingProfile profile
;
34 GeolocationSettingsState
state(&profile
);
35 GURL
url_0("http://www.example.com");
37 scoped_ptr
<NavigationEntry
> entry(NavigationEntry::Create());
39 content::LoadCommittedDetails load_committed_details
;
40 load_committed_details
.entry
= entry
.get();
41 state
.DidNavigate(load_committed_details
);
43 profile
.GetHostContentSettingsMap()->SetContentSetting(
44 ContentSettingsPattern::FromURLNoWildcard(url_0
),
45 ContentSettingsPattern::FromURLNoWildcard(url_0
),
46 CONTENT_SETTINGS_TYPE_GEOLOCATION
,
48 CONTENT_SETTING_ALLOW
);
49 state
.OnGeolocationPermissionSet(url_0
, true);
51 GURL
url_1("http://www.example1.com");
52 profile
.GetHostContentSettingsMap()->SetContentSetting(
53 ContentSettingsPattern::FromURLNoWildcard(url_1
),
54 ContentSettingsPattern::FromURLNoWildcard(url_0
),
55 CONTENT_SETTINGS_TYPE_GEOLOCATION
,
57 CONTENT_SETTING_BLOCK
);
58 state
.OnGeolocationPermissionSet(url_1
, false);
60 GeolocationSettingsState::StateMap state_map
=
62 EXPECT_EQ(2U, state_map
.size());
64 GeolocationSettingsState::FormattedHostsPerState formatted_host_per_state
;
65 unsigned int tab_state_flags
= 0;
66 state
.GetDetailedInfo(&formatted_host_per_state
, &tab_state_flags
);
67 EXPECT_TRUE(tab_state_flags
&
68 GeolocationSettingsState::TABSTATE_HAS_ANY_ALLOWED
)
70 EXPECT_TRUE(tab_state_flags
&
71 GeolocationSettingsState::TABSTATE_HAS_EXCEPTION
)
73 EXPECT_FALSE(tab_state_flags
&
74 GeolocationSettingsState::TABSTATE_HAS_CHANGED
)
76 EXPECT_TRUE(tab_state_flags
&
77 GeolocationSettingsState::TABSTATE_HAS_ANY_ICON
)
79 EXPECT_EQ(1U, formatted_host_per_state
[CONTENT_SETTING_ALLOW
].size());
81 formatted_host_per_state
[CONTENT_SETTING_ALLOW
].count(
84 EXPECT_EQ(1U, formatted_host_per_state
[CONTENT_SETTING_BLOCK
].size());
86 formatted_host_per_state
[CONTENT_SETTING_BLOCK
].count(
89 state
.OnGeolocationPermissionSet(url_0
, false);
91 formatted_host_per_state
.clear();
93 state
.GetDetailedInfo(&formatted_host_per_state
, &tab_state_flags
);
94 EXPECT_FALSE(tab_state_flags
&
95 GeolocationSettingsState::TABSTATE_HAS_ANY_ALLOWED
)
97 EXPECT_TRUE(tab_state_flags
&
98 GeolocationSettingsState::TABSTATE_HAS_EXCEPTION
)
100 EXPECT_TRUE(tab_state_flags
&
101 GeolocationSettingsState::TABSTATE_HAS_CHANGED
)
103 EXPECT_TRUE(tab_state_flags
&
104 GeolocationSettingsState::TABSTATE_HAS_ANY_ICON
)
106 EXPECT_EQ(0U, formatted_host_per_state
[CONTENT_SETTING_ALLOW
].size());
107 EXPECT_EQ(2U, formatted_host_per_state
[CONTENT_SETTING_BLOCK
].size());
109 formatted_host_per_state
[CONTENT_SETTING_BLOCK
].count(
112 formatted_host_per_state
[CONTENT_SETTING_BLOCK
].count(
115 state
.OnGeolocationPermissionSet(url_0
, true);
117 load_committed_details
.previous_url
= url_0
;
118 state
.DidNavigate(load_committed_details
);
120 GeolocationSettingsState::StateMap new_state_map
=
122 EXPECT_EQ(state_map
.size(), new_state_map
.size());
124 GURL
different_url("http://foo.com");
125 entry
->SetURL(different_url
);
126 state
.DidNavigate(load_committed_details
);
128 EXPECT_TRUE(state
.state_map().empty());
130 formatted_host_per_state
.clear();
132 state
.GetDetailedInfo(&formatted_host_per_state
, &tab_state_flags
);
133 EXPECT_TRUE(formatted_host_per_state
.empty());
134 EXPECT_EQ(0U, tab_state_flags
);
137 TEST_F(GeolocationSettingsStateTests
, ShowPortOnSameHost
) {
138 TestingProfile profile
;
139 GeolocationSettingsState
state(&profile
);
140 GURL
url_0("http://www.example.com");
142 scoped_ptr
<NavigationEntry
> entry(NavigationEntry::Create());
143 entry
->SetURL(url_0
);
144 content::LoadCommittedDetails load_committed_details
;
145 load_committed_details
.entry
= entry
.get();
146 state
.DidNavigate(load_committed_details
);
148 profile
.GetHostContentSettingsMap()->SetContentSetting(
149 ContentSettingsPattern::FromURLNoWildcard(url_0
),
150 ContentSettingsPattern::FromURLNoWildcard(url_0
),
151 CONTENT_SETTINGS_TYPE_GEOLOCATION
,
153 CONTENT_SETTING_ALLOW
);
154 state
.OnGeolocationPermissionSet(url_0
, true);
156 GURL
url_1("https://www.example.com");
157 profile
.GetHostContentSettingsMap()->SetContentSetting(
158 ContentSettingsPattern::FromURLNoWildcard(url_1
),
159 ContentSettingsPattern::FromURLNoWildcard(url_0
),
160 CONTENT_SETTINGS_TYPE_GEOLOCATION
,
162 CONTENT_SETTING_ALLOW
);
163 state
.OnGeolocationPermissionSet(url_1
, true);
165 GURL
url_2("http://www.example1.com");
166 profile
.GetHostContentSettingsMap()->SetContentSetting(
167 ContentSettingsPattern::FromURLNoWildcard(url_2
),
168 ContentSettingsPattern::FromURLNoWildcard(url_0
),
169 CONTENT_SETTINGS_TYPE_GEOLOCATION
,
171 CONTENT_SETTING_ALLOW
);
172 state
.OnGeolocationPermissionSet(url_2
, true);
174 GeolocationSettingsState::StateMap state_map
=
176 EXPECT_EQ(3U, state_map
.size());
178 GeolocationSettingsState::FormattedHostsPerState formatted_host_per_state
;
179 unsigned int tab_state_flags
= 0;
180 state
.GetDetailedInfo(&formatted_host_per_state
, &tab_state_flags
);
182 EXPECT_EQ(3U, formatted_host_per_state
[CONTENT_SETTING_ALLOW
].size());
184 formatted_host_per_state
[CONTENT_SETTING_ALLOW
].count(
187 formatted_host_per_state
[CONTENT_SETTING_ALLOW
].count(
190 formatted_host_per_state
[CONTENT_SETTING_ALLOW
].count(
193 state
.OnGeolocationPermissionSet(url_1
, false);
194 formatted_host_per_state
.clear();
196 state
.GetDetailedInfo(&formatted_host_per_state
, &tab_state_flags
);
198 EXPECT_EQ(2U, formatted_host_per_state
[CONTENT_SETTING_ALLOW
].size());
200 formatted_host_per_state
[CONTENT_SETTING_ALLOW
].count(
203 formatted_host_per_state
[CONTENT_SETTING_ALLOW
].count(
205 EXPECT_EQ(1U, formatted_host_per_state
[CONTENT_SETTING_BLOCK
].size());
207 formatted_host_per_state
[CONTENT_SETTING_BLOCK
].count(