OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include <string> | |
6 | |
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" | |
16 | |
17 using content::BrowserThread; | |
18 using content::NavigationEntry; | |
19 | |
20 namespace { | |
21 | |
22 class GeolocationSettingsStateTests : public testing::Test { | |
23 public: | |
24 GeolocationSettingsStateTests() | |
25 : ui_thread_(BrowserThread::UI, &message_loop_) { | |
26 } | |
27 | |
28 protected: | |
29 base::MessageLoop message_loop_; | |
30 content::TestBrowserThread ui_thread_; | |
31 }; | |
32 | |
33 TEST_F(GeolocationSettingsStateTests, ClearOnNewOrigin) { | |
34 TestingProfile profile; | |
35 GeolocationSettingsState state(&profile); | |
36 GURL url_0("http://www.example.com"); | |
37 | |
38 std::unique_ptr<NavigationEntry> entry(NavigationEntry::Create()); | |
39 entry->SetURL(url_0); | |
40 content::LoadCommittedDetails load_committed_details; | |
41 load_committed_details.entry = entry.get(); | |
42 state.DidNavigate(load_committed_details); | |
43 | |
44 HostContentSettingsMapFactory::GetForProfile(profile) | |
45 ->SetContentSettingDefaultScope(url_0, url_0, | |
46 CONTENT_SETTINGS_TYPE_GEOLOCATION, | |
47 std::string(), CONTENT_SETTING_ALLOW); | |
48 state.OnGeolocationPermissionSet(url_0, true); | |
49 | |
50 GURL url_1("http://www.example1.com"); | |
51 HostContentSettingsMapFactory::GetForProfile(profile) | |
52 ->SetContentSettingDefaultScope(url_1, url_0, | |
53 CONTENT_SETTINGS_TYPE_GEOLOCATION, | |
54 std::string(), CONTENT_SETTING_BLOCK); | |
55 state.OnGeolocationPermissionSet(url_1, false); | |
56 | |
57 GeolocationSettingsState::StateMap state_map = | |
58 state.state_map(); | |
59 EXPECT_EQ(2U, state_map.size()); | |
60 | |
61 GeolocationSettingsState::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 GeolocationSettingsState::TABSTATE_HAS_ANY_ALLOWED) | |
66 << tab_state_flags; | |
67 EXPECT_TRUE(tab_state_flags & | |
68 GeolocationSettingsState::TABSTATE_HAS_EXCEPTION) | |
69 << tab_state_flags; | |
70 EXPECT_FALSE(tab_state_flags & | |
71 GeolocationSettingsState::TABSTATE_HAS_CHANGED) | |
72 << tab_state_flags; | |
73 EXPECT_TRUE(tab_state_flags & | |
74 GeolocationSettingsState::TABSTATE_HAS_ANY_ICON) | |
75 << tab_state_flags; | |
76 EXPECT_EQ(1U, formatted_host_per_state[CONTENT_SETTING_ALLOW].size()); | |
77 EXPECT_EQ(1U, | |
78 formatted_host_per_state[CONTENT_SETTING_ALLOW].count( | |
79 url_0.host())); | |
80 | |
81 EXPECT_EQ(1U, formatted_host_per_state[CONTENT_SETTING_BLOCK].size()); | |
82 EXPECT_EQ(1U, | |
83 formatted_host_per_state[CONTENT_SETTING_BLOCK].count( | |
84 url_1.host())); | |
85 | |
86 state.OnGeolocationPermissionSet(url_0, false); | |
87 | |
88 formatted_host_per_state.clear(); | |
89 tab_state_flags = 0; | |
90 state.GetDetailedInfo(&formatted_host_per_state, &tab_state_flags); | |
91 EXPECT_FALSE(tab_state_flags & | |
92 GeolocationSettingsState::TABSTATE_HAS_ANY_ALLOWED) | |
93 << tab_state_flags; | |
94 EXPECT_TRUE(tab_state_flags & | |
95 GeolocationSettingsState::TABSTATE_HAS_EXCEPTION) | |
96 << tab_state_flags; | |
97 EXPECT_TRUE(tab_state_flags & | |
98 GeolocationSettingsState::TABSTATE_HAS_CHANGED) | |
99 << tab_state_flags; | |
100 EXPECT_TRUE(tab_state_flags & | |
101 GeolocationSettingsState::TABSTATE_HAS_ANY_ICON) | |
102 << tab_state_flags; | |
103 EXPECT_EQ(0U, formatted_host_per_state[CONTENT_SETTING_ALLOW].size()); | |
104 EXPECT_EQ(2U, formatted_host_per_state[CONTENT_SETTING_BLOCK].size()); | |
105 EXPECT_EQ(1U, | |
106 formatted_host_per_state[CONTENT_SETTING_BLOCK].count( | |
107 url_0.host())); | |
108 EXPECT_EQ(1U, | |
109 formatted_host_per_state[CONTENT_SETTING_BLOCK].count( | |
110 url_1.host())); | |
111 | |
112 state.OnGeolocationPermissionSet(url_0, true); | |
113 | |
114 load_committed_details.previous_url = url_0; | |
115 state.DidNavigate(load_committed_details); | |
116 | |
117 GeolocationSettingsState::StateMap new_state_map = | |
118 state.state_map(); | |
119 EXPECT_EQ(state_map.size(), new_state_map.size()); | |
120 | |
121 GURL different_url("http://foo.com"); | |
122 entry->SetURL(different_url); | |
123 state.DidNavigate(load_committed_details); | |
124 | |
125 EXPECT_TRUE(state.state_map().empty()); | |
126 | |
127 formatted_host_per_state.clear(); | |
128 tab_state_flags = 0; | |
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); | |
132 } | |
133 | |
134 TEST_F(GeolocationSettingsStateTests, ShowPortOnSameHost) { | |
135 TestingProfile profile; | |
136 GeolocationSettingsState state(&profile); | |
137 GURL url_0("http://www.example.com"); | |
138 | |
139 std::unique_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); | |
144 | |
145 HostContentSettingsMapFactory::GetForProfile(profile) | |
146 ->SetContentSettingDefaultScope(url_0, url_0, | |
147 CONTENT_SETTINGS_TYPE_GEOLOCATION, | |
148 std::string(), CONTENT_SETTING_ALLOW); | |
149 state.OnGeolocationPermissionSet(url_0, true); | |
150 | |
151 GURL url_1("https://www.example.com"); | |
152 HostContentSettingsMapFactory::GetForProfile(profile) | |
153 ->SetContentSettingDefaultScope(url_1, url_0, | |
154 CONTENT_SETTINGS_TYPE_GEOLOCATION, | |
155 std::string(), CONTENT_SETTING_ALLOW); | |
156 state.OnGeolocationPermissionSet(url_1, true); | |
157 | |
158 GURL url_2("http://www.example1.com"); | |
159 HostContentSettingsMapFactory::GetForProfile(profile) | |
160 ->SetContentSettingDefaultScope(url_2, url_0, | |
161 CONTENT_SETTINGS_TYPE_GEOLOCATION, | |
162 std::string(), CONTENT_SETTING_ALLOW); | |
163 state.OnGeolocationPermissionSet(url_2, true); | |
164 | |
165 GeolocationSettingsState::StateMap state_map = | |
166 state.state_map(); | |
167 EXPECT_EQ(3U, state_map.size()); | |
168 | |
169 GeolocationSettingsState::FormattedHostsPerState formatted_host_per_state; | |
170 unsigned int tab_state_flags = 0; | |
171 state.GetDetailedInfo(&formatted_host_per_state, &tab_state_flags); | |
172 | |
173 EXPECT_EQ(3U, formatted_host_per_state[CONTENT_SETTING_ALLOW].size()); | |
174 EXPECT_EQ(1U, | |
175 formatted_host_per_state[CONTENT_SETTING_ALLOW].count( | |
176 url_0.spec())); | |
177 EXPECT_EQ(1U, | |
178 formatted_host_per_state[CONTENT_SETTING_ALLOW].count( | |
179 url_1.spec())); | |
180 EXPECT_EQ(1U, | |
181 formatted_host_per_state[CONTENT_SETTING_ALLOW].count( | |
182 url_2.host())); | |
183 | |
184 state.OnGeolocationPermissionSet(url_1, false); | |
185 formatted_host_per_state.clear(); | |
186 tab_state_flags = 0; | |
187 state.GetDetailedInfo(&formatted_host_per_state, &tab_state_flags); | |
188 | |
189 EXPECT_EQ(2U, formatted_host_per_state[CONTENT_SETTING_ALLOW].size()); | |
190 EXPECT_EQ(1U, | |
191 formatted_host_per_state[CONTENT_SETTING_ALLOW].count( | |
192 url_0.spec())); | |
193 EXPECT_EQ(1U, | |
194 formatted_host_per_state[CONTENT_SETTING_ALLOW].count( | |
195 url_2.host())); | |
196 EXPECT_EQ(1U, formatted_host_per_state[CONTENT_SETTING_BLOCK].size()); | |
197 EXPECT_EQ(1U, | |
198 formatted_host_per_state[CONTENT_SETTING_BLOCK].count( | |
199 url_1.spec())); | |
200 } | |
201 | |
202 | |
203 } // namespace | |
OLD | NEW |