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_factory.h"
9 #include "chrome/browser/geolocation/geolocation_settings_state.h"
10 #include "chrome/test/base/testing_profile.h"
11 #include "components/content_settings/core/browser/host_content_settings_map.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 GeolocationSettingsStateTests
: public testing::Test
{
24 GeolocationSettingsStateTests()
25 : ui_thread_(BrowserThread::UI
, &message_loop_
) {
29 base::MessageLoop message_loop_
;
30 content::TestBrowserThread ui_thread_
;
33 TEST_F(GeolocationSettingsStateTests
, ClearOnNewOrigin
) {
34 TestingProfile profile
;
35 GeolocationSettingsState
state(&profile
);
36 GURL
url_0("http://www.example.com");
38 scoped_ptr
<NavigationEntry
> entry(NavigationEntry::Create());
40 content::LoadCommittedDetails load_committed_details
;
41 load_committed_details
.entry
= entry
.get();
42 state
.DidNavigate(load_committed_details
);
44 HostContentSettingsMapFactory::GetForProfile(profile
)->SetContentSetting(
45 ContentSettingsPattern::FromURLNoWildcard(url_0
),
46 ContentSettingsPattern::FromURLNoWildcard(url_0
),
47 CONTENT_SETTINGS_TYPE_GEOLOCATION
,
49 CONTENT_SETTING_ALLOW
);
50 state
.OnGeolocationPermissionSet(url_0
, true);
52 GURL
url_1("http://www.example1.com");
53 HostContentSettingsMapFactory::GetForProfile(profile
)->SetContentSetting(
54 ContentSettingsPattern::FromURLNoWildcard(url_1
),
55 ContentSettingsPattern::FromURLNoWildcard(url_0
),
56 CONTENT_SETTINGS_TYPE_GEOLOCATION
,
58 CONTENT_SETTING_BLOCK
);
59 state
.OnGeolocationPermissionSet(url_1
, false);
61 GeolocationSettingsState::StateMap state_map
=
63 EXPECT_EQ(2U, state_map
.size());
65 GeolocationSettingsState::FormattedHostsPerState formatted_host_per_state
;
66 unsigned int tab_state_flags
= 0;
67 state
.GetDetailedInfo(&formatted_host_per_state
, &tab_state_flags
);
68 EXPECT_TRUE(tab_state_flags
&
69 GeolocationSettingsState::TABSTATE_HAS_ANY_ALLOWED
)
71 EXPECT_TRUE(tab_state_flags
&
72 GeolocationSettingsState::TABSTATE_HAS_EXCEPTION
)
74 EXPECT_FALSE(tab_state_flags
&
75 GeolocationSettingsState::TABSTATE_HAS_CHANGED
)
77 EXPECT_TRUE(tab_state_flags
&
78 GeolocationSettingsState::TABSTATE_HAS_ANY_ICON
)
80 EXPECT_EQ(1U, formatted_host_per_state
[CONTENT_SETTING_ALLOW
].size());
82 formatted_host_per_state
[CONTENT_SETTING_ALLOW
].count(
85 EXPECT_EQ(1U, formatted_host_per_state
[CONTENT_SETTING_BLOCK
].size());
87 formatted_host_per_state
[CONTENT_SETTING_BLOCK
].count(
90 state
.OnGeolocationPermissionSet(url_0
, false);
92 formatted_host_per_state
.clear();
94 state
.GetDetailedInfo(&formatted_host_per_state
, &tab_state_flags
);
95 EXPECT_FALSE(tab_state_flags
&
96 GeolocationSettingsState::TABSTATE_HAS_ANY_ALLOWED
)
98 EXPECT_TRUE(tab_state_flags
&
99 GeolocationSettingsState::TABSTATE_HAS_EXCEPTION
)
101 EXPECT_TRUE(tab_state_flags
&
102 GeolocationSettingsState::TABSTATE_HAS_CHANGED
)
104 EXPECT_TRUE(tab_state_flags
&
105 GeolocationSettingsState::TABSTATE_HAS_ANY_ICON
)
107 EXPECT_EQ(0U, formatted_host_per_state
[CONTENT_SETTING_ALLOW
].size());
108 EXPECT_EQ(2U, formatted_host_per_state
[CONTENT_SETTING_BLOCK
].size());
110 formatted_host_per_state
[CONTENT_SETTING_BLOCK
].count(
113 formatted_host_per_state
[CONTENT_SETTING_BLOCK
].count(
116 state
.OnGeolocationPermissionSet(url_0
, true);
118 load_committed_details
.previous_url
= url_0
;
119 state
.DidNavigate(load_committed_details
);
121 GeolocationSettingsState::StateMap new_state_map
=
123 EXPECT_EQ(state_map
.size(), new_state_map
.size());
125 GURL
different_url("http://foo.com");
126 entry
->SetURL(different_url
);
127 state
.DidNavigate(load_committed_details
);
129 EXPECT_TRUE(state
.state_map().empty());
131 formatted_host_per_state
.clear();
133 state
.GetDetailedInfo(&formatted_host_per_state
, &tab_state_flags
);
134 EXPECT_TRUE(formatted_host_per_state
.empty());
135 EXPECT_EQ(0U, tab_state_flags
);
138 TEST_F(GeolocationSettingsStateTests
, ShowPortOnSameHost
) {
139 TestingProfile profile
;
140 GeolocationSettingsState
state(&profile
);
141 GURL
url_0("http://www.example.com");
143 scoped_ptr
<NavigationEntry
> entry(NavigationEntry::Create());
144 entry
->SetURL(url_0
);
145 content::LoadCommittedDetails load_committed_details
;
146 load_committed_details
.entry
= entry
.get();
147 state
.DidNavigate(load_committed_details
);
149 HostContentSettingsMapFactory::GetForProfile(profile
)->SetContentSetting(
150 ContentSettingsPattern::FromURLNoWildcard(url_0
),
151 ContentSettingsPattern::FromURLNoWildcard(url_0
),
152 CONTENT_SETTINGS_TYPE_GEOLOCATION
,
154 CONTENT_SETTING_ALLOW
);
155 state
.OnGeolocationPermissionSet(url_0
, true);
157 GURL
url_1("https://www.example.com");
158 HostContentSettingsMapFactory::GetForProfile(profile
)->SetContentSetting(
159 ContentSettingsPattern::FromURLNoWildcard(url_1
),
160 ContentSettingsPattern::FromURLNoWildcard(url_0
),
161 CONTENT_SETTINGS_TYPE_GEOLOCATION
,
163 CONTENT_SETTING_ALLOW
);
164 state
.OnGeolocationPermissionSet(url_1
, true);
166 GURL
url_2("http://www.example1.com");
167 HostContentSettingsMapFactory::GetForProfile(profile
)->SetContentSetting(
168 ContentSettingsPattern::FromURLNoWildcard(url_2
),
169 ContentSettingsPattern::FromURLNoWildcard(url_0
),
170 CONTENT_SETTINGS_TYPE_GEOLOCATION
,
172 CONTENT_SETTING_ALLOW
);
173 state
.OnGeolocationPermissionSet(url_2
, true);
175 GeolocationSettingsState::StateMap state_map
=
177 EXPECT_EQ(3U, state_map
.size());
179 GeolocationSettingsState::FormattedHostsPerState formatted_host_per_state
;
180 unsigned int tab_state_flags
= 0;
181 state
.GetDetailedInfo(&formatted_host_per_state
, &tab_state_flags
);
183 EXPECT_EQ(3U, formatted_host_per_state
[CONTENT_SETTING_ALLOW
].size());
185 formatted_host_per_state
[CONTENT_SETTING_ALLOW
].count(
188 formatted_host_per_state
[CONTENT_SETTING_ALLOW
].count(
191 formatted_host_per_state
[CONTENT_SETTING_ALLOW
].count(
194 state
.OnGeolocationPermissionSet(url_1
, false);
195 formatted_host_per_state
.clear();
197 state
.GetDetailedInfo(&formatted_host_per_state
, &tab_state_flags
);
199 EXPECT_EQ(2U, formatted_host_per_state
[CONTENT_SETTING_ALLOW
].size());
201 formatted_host_per_state
[CONTENT_SETTING_ALLOW
].count(
204 formatted_host_per_state
[CONTENT_SETTING_ALLOW
].count(
206 EXPECT_EQ(1U, formatted_host_per_state
[CONTENT_SETTING_BLOCK
].size());
208 formatted_host_per_state
[CONTENT_SETTING_BLOCK
].count(