Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / geolocation / geolocation_settings_state_unittest.cc
blobbc489a803622b09b57ef7155d72ce363d4ff3636
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.
5 #include <string>
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;
19 namespace {
21 class GeolocationSettingsStateTests : public testing::Test {
22 public:
23 GeolocationSettingsStateTests()
24 : ui_thread_(BrowserThread::UI, &message_loop_) {
27 protected:
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());
38 entry->SetURL(url_0);
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,
47 std::string(),
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,
56 std::string(),
57 CONTENT_SETTING_BLOCK);
58 state.OnGeolocationPermissionSet(url_1, false);
60 GeolocationSettingsState::StateMap state_map =
61 state.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)
69 << tab_state_flags;
70 EXPECT_TRUE(tab_state_flags &
71 GeolocationSettingsState::TABSTATE_HAS_EXCEPTION)
72 << tab_state_flags;
73 EXPECT_FALSE(tab_state_flags &
74 GeolocationSettingsState::TABSTATE_HAS_CHANGED)
75 << tab_state_flags;
76 EXPECT_TRUE(tab_state_flags &
77 GeolocationSettingsState::TABSTATE_HAS_ANY_ICON)
78 << tab_state_flags;
79 EXPECT_EQ(1U, formatted_host_per_state[CONTENT_SETTING_ALLOW].size());
80 EXPECT_EQ(1U,
81 formatted_host_per_state[CONTENT_SETTING_ALLOW].count(
82 url_0.host()));
84 EXPECT_EQ(1U, formatted_host_per_state[CONTENT_SETTING_BLOCK].size());
85 EXPECT_EQ(1U,
86 formatted_host_per_state[CONTENT_SETTING_BLOCK].count(
87 url_1.host()));
89 state.OnGeolocationPermissionSet(url_0, false);
91 formatted_host_per_state.clear();
92 tab_state_flags = 0;
93 state.GetDetailedInfo(&formatted_host_per_state, &tab_state_flags);
94 EXPECT_FALSE(tab_state_flags &
95 GeolocationSettingsState::TABSTATE_HAS_ANY_ALLOWED)
96 << tab_state_flags;
97 EXPECT_TRUE(tab_state_flags &
98 GeolocationSettingsState::TABSTATE_HAS_EXCEPTION)
99 << tab_state_flags;
100 EXPECT_TRUE(tab_state_flags &
101 GeolocationSettingsState::TABSTATE_HAS_CHANGED)
102 << tab_state_flags;
103 EXPECT_TRUE(tab_state_flags &
104 GeolocationSettingsState::TABSTATE_HAS_ANY_ICON)
105 << tab_state_flags;
106 EXPECT_EQ(0U, formatted_host_per_state[CONTENT_SETTING_ALLOW].size());
107 EXPECT_EQ(2U, formatted_host_per_state[CONTENT_SETTING_BLOCK].size());
108 EXPECT_EQ(1U,
109 formatted_host_per_state[CONTENT_SETTING_BLOCK].count(
110 url_0.host()));
111 EXPECT_EQ(1U,
112 formatted_host_per_state[CONTENT_SETTING_BLOCK].count(
113 url_1.host()));
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 =
121 state.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();
131 tab_state_flags = 0;
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,
152 std::string(),
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,
161 std::string(),
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,
170 std::string(),
171 CONTENT_SETTING_ALLOW);
172 state.OnGeolocationPermissionSet(url_2, true);
174 GeolocationSettingsState::StateMap state_map =
175 state.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());
183 EXPECT_EQ(1U,
184 formatted_host_per_state[CONTENT_SETTING_ALLOW].count(
185 url_0.spec()));
186 EXPECT_EQ(1U,
187 formatted_host_per_state[CONTENT_SETTING_ALLOW].count(
188 url_1.spec()));
189 EXPECT_EQ(1U,
190 formatted_host_per_state[CONTENT_SETTING_ALLOW].count(
191 url_2.host()));
193 state.OnGeolocationPermissionSet(url_1, false);
194 formatted_host_per_state.clear();
195 tab_state_flags = 0;
196 state.GetDetailedInfo(&formatted_host_per_state, &tab_state_flags);
198 EXPECT_EQ(2U, formatted_host_per_state[CONTENT_SETTING_ALLOW].size());
199 EXPECT_EQ(1U,
200 formatted_host_per_state[CONTENT_SETTING_ALLOW].count(
201 url_0.spec()));
202 EXPECT_EQ(1U,
203 formatted_host_per_state[CONTENT_SETTING_ALLOW].count(
204 url_2.host()));
205 EXPECT_EQ(1U, formatted_host_per_state[CONTENT_SETTING_BLOCK].size());
206 EXPECT_EQ(1U,
207 formatted_host_per_state[CONTENT_SETTING_BLOCK].count(
208 url_1.spec()));
212 } // namespace