1 // Copyright (c) 2010 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 "chrome/browser/geolocation/geolocation_content_settings_map.h"
6 #include "chrome/browser/geolocation/geolocation_settings_state.h"
7 #include "chrome/test/testing_profile.h"
8 #include "content/browser/browser_thread.h"
9 #include "content/browser/tab_contents/navigation_entry.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11
12 namespace {
13
14 class GeolocationSettingsStateTests : public testing::Test {
15 public:
GeolocationSettingsStateTests()16 GeolocationSettingsStateTests()
17 : ui_thread_(BrowserThread::UI, &message_loop_) {
18 }
19
20 protected:
21 MessageLoop message_loop_;
22 BrowserThread ui_thread_;
23 };
24
TEST_F(GeolocationSettingsStateTests,ClearOnNewOrigin)25 TEST_F(GeolocationSettingsStateTests, ClearOnNewOrigin) {
26 TestingProfile profile;
27 GeolocationSettingsState state(&profile);
28 GURL url_0("http://www.example.com");
29
30 NavigationEntry entry;
31 entry.set_url(url_0);
32 NavigationController::LoadCommittedDetails load_committed_details;
33 load_committed_details.entry = &entry;
34 state.DidNavigate(load_committed_details);
35
36 profile.GetGeolocationContentSettingsMap()->SetContentSetting(
37 url_0, url_0, CONTENT_SETTING_ALLOW);
38 state.OnGeolocationPermissionSet(url_0, true);
39
40 GURL url_1("http://www.example1.com");
41 profile.GetGeolocationContentSettingsMap()->SetContentSetting(
42 url_1, url_0, CONTENT_SETTING_BLOCK);
43 state.OnGeolocationPermissionSet(url_1, false);
44
45 GeolocationSettingsState::StateMap state_map =
46 state.state_map();
47 EXPECT_EQ(2U, state_map.size());
48
49 GeolocationSettingsState::FormattedHostsPerState formatted_host_per_state;
50 unsigned int tab_state_flags = 0;
51 state.GetDetailedInfo(&formatted_host_per_state, &tab_state_flags);
52 EXPECT_TRUE(tab_state_flags &
53 GeolocationSettingsState::TABSTATE_HAS_ANY_ALLOWED)
54 << tab_state_flags;
55 EXPECT_TRUE(tab_state_flags &
56 GeolocationSettingsState::TABSTATE_HAS_EXCEPTION)
57 << tab_state_flags;
58 EXPECT_FALSE(tab_state_flags &
59 GeolocationSettingsState::TABSTATE_HAS_CHANGED)
60 << tab_state_flags;
61 EXPECT_TRUE(tab_state_flags &
62 GeolocationSettingsState::TABSTATE_HAS_ANY_ICON)
63 << tab_state_flags;
64 EXPECT_EQ(1U, formatted_host_per_state[CONTENT_SETTING_ALLOW].size());
65 EXPECT_EQ(1U,
66 formatted_host_per_state[CONTENT_SETTING_ALLOW].count(
67 url_0.host()));
68
69 EXPECT_EQ(1U, formatted_host_per_state[CONTENT_SETTING_BLOCK].size());
70 EXPECT_EQ(1U,
71 formatted_host_per_state[CONTENT_SETTING_BLOCK].count(
72 url_1.host()));
73
74 state.OnGeolocationPermissionSet(url_0, false);
75
76 formatted_host_per_state.clear();
77 tab_state_flags = 0;
78 state.GetDetailedInfo(&formatted_host_per_state, &tab_state_flags);
79 EXPECT_FALSE(tab_state_flags &
80 GeolocationSettingsState::TABSTATE_HAS_ANY_ALLOWED)
81 << tab_state_flags;
82 EXPECT_TRUE(tab_state_flags &
83 GeolocationSettingsState::TABSTATE_HAS_EXCEPTION)
84 << tab_state_flags;
85 EXPECT_TRUE(tab_state_flags &
86 GeolocationSettingsState::TABSTATE_HAS_CHANGED)
87 << tab_state_flags;
88 EXPECT_TRUE(tab_state_flags &
89 GeolocationSettingsState::TABSTATE_HAS_ANY_ICON)
90 << tab_state_flags;
91 EXPECT_EQ(0U, formatted_host_per_state[CONTENT_SETTING_ALLOW].size());
92 EXPECT_EQ(2U, formatted_host_per_state[CONTENT_SETTING_BLOCK].size());
93 EXPECT_EQ(1U,
94 formatted_host_per_state[CONTENT_SETTING_BLOCK].count(
95 url_0.host()));
96 EXPECT_EQ(1U,
97 formatted_host_per_state[CONTENT_SETTING_BLOCK].count(
98 url_1.host()));
99
100 state.OnGeolocationPermissionSet(url_0, true);
101
102 load_committed_details.previous_url = url_0;
103 state.DidNavigate(load_committed_details);
104
105 GeolocationSettingsState::StateMap new_state_map =
106 state.state_map();
107 EXPECT_EQ(state_map.size(), new_state_map.size());
108
109 GURL different_url("http://foo.com");
110 entry.set_url(different_url);
111 state.DidNavigate(load_committed_details);
112
113 EXPECT_TRUE(state.state_map().empty());
114
115 formatted_host_per_state.clear();
116 tab_state_flags = 0;
117 state.GetDetailedInfo(&formatted_host_per_state, &tab_state_flags);
118 EXPECT_TRUE(formatted_host_per_state.empty());
119 EXPECT_EQ(0U, tab_state_flags);
120 }
121
TEST_F(GeolocationSettingsStateTests,ShowPortOnSameHost)122 TEST_F(GeolocationSettingsStateTests, ShowPortOnSameHost) {
123 TestingProfile profile;
124 GeolocationSettingsState state(&profile);
125 GURL url_0("http://www.example.com");
126
127 NavigationEntry entry;
128 entry.set_url(url_0);
129 NavigationController::LoadCommittedDetails load_committed_details;
130 load_committed_details.entry = &entry;
131 state.DidNavigate(load_committed_details);
132
133 profile.GetGeolocationContentSettingsMap()->SetContentSetting(
134 url_0, url_0, CONTENT_SETTING_ALLOW);
135 state.OnGeolocationPermissionSet(url_0, true);
136
137 GURL url_1("https://www.example.com");
138 profile.GetGeolocationContentSettingsMap()->SetContentSetting(
139 url_1, url_0, CONTENT_SETTING_ALLOW);
140 state.OnGeolocationPermissionSet(url_1, true);
141
142 GURL url_2("http://www.example1.com");
143 profile.GetGeolocationContentSettingsMap()->SetContentSetting(
144 url_2, url_0, CONTENT_SETTING_ALLOW);
145 state.OnGeolocationPermissionSet(url_2, true);
146
147 GeolocationSettingsState::StateMap state_map =
148 state.state_map();
149 EXPECT_EQ(3U, state_map.size());
150
151 GeolocationSettingsState::FormattedHostsPerState formatted_host_per_state;
152 unsigned int tab_state_flags = 0;
153 state.GetDetailedInfo(&formatted_host_per_state, &tab_state_flags);
154
155 EXPECT_EQ(3U, formatted_host_per_state[CONTENT_SETTING_ALLOW].size());
156 EXPECT_EQ(1U,
157 formatted_host_per_state[CONTENT_SETTING_ALLOW].count(
158 url_0.spec()));
159 EXPECT_EQ(1U,
160 formatted_host_per_state[CONTENT_SETTING_ALLOW].count(
161 url_1.spec()));
162 EXPECT_EQ(1U,
163 formatted_host_per_state[CONTENT_SETTING_ALLOW].count(
164 url_2.host()));
165
166 state.OnGeolocationPermissionSet(url_1, false);
167 formatted_host_per_state.clear();
168 tab_state_flags = 0;
169 state.GetDetailedInfo(&formatted_host_per_state, &tab_state_flags);
170
171 EXPECT_EQ(2U, formatted_host_per_state[CONTENT_SETTING_ALLOW].size());
172 EXPECT_EQ(1U,
173 formatted_host_per_state[CONTENT_SETTING_ALLOW].count(
174 url_0.spec()));
175 EXPECT_EQ(1U,
176 formatted_host_per_state[CONTENT_SETTING_ALLOW].count(
177 url_2.host()));
178 EXPECT_EQ(1U, formatted_host_per_state[CONTENT_SETTING_BLOCK].size());
179 EXPECT_EQ(1U,
180 formatted_host_per_state[CONTENT_SETTING_BLOCK].count(
181 url_1.spec()));
182 }
183
184
185 } // namespace
186