Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / geolocation / geolocation_settings_state_unittest.cc
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/geolocation/geolocation_settings_state.h"
9 #include "chrome/test/base/testing_profile.h"
10 #include "components/content_settings/core/browser/host_content_settings_map.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"
15
16 using content::BrowserThread;
17 using content::NavigationEntry;
18
19 namespace {
20
21 class GeolocationSettingsStateTests : public testing::Test {
22  public:
23   GeolocationSettingsStateTests()
24     : ui_thread_(BrowserThread::UI, &message_loop_) {
25   }
26
27  protected:
28   base::MessageLoop message_loop_;
29   content::TestBrowserThread ui_thread_;
30 };
31
32 TEST_F(GeolocationSettingsStateTests, ClearOnNewOrigin) {
33   TestingProfile profile;
34   GeolocationSettingsState state(&profile);
35   GURL url_0("http://www.example.com");
36
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);
42
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);
50
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);
59
60   GeolocationSettingsState::StateMap state_map =
61       state.state_map();
62   EXPECT_EQ(2U, state_map.size());
63
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()));
83
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()));
88
89   state.OnGeolocationPermissionSet(url_0, false);
90
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()));
114
115   state.OnGeolocationPermissionSet(url_0, true);
116
117   load_committed_details.previous_url = url_0;
118   state.DidNavigate(load_committed_details);
119
120   GeolocationSettingsState::StateMap new_state_map =
121       state.state_map();
122   EXPECT_EQ(state_map.size(), new_state_map.size());
123
124   GURL different_url("http://foo.com");
125   entry->SetURL(different_url);
126   state.DidNavigate(load_committed_details);
127
128   EXPECT_TRUE(state.state_map().empty());
129
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);
135 }
136
137 TEST_F(GeolocationSettingsStateTests, ShowPortOnSameHost) {
138   TestingProfile profile;
139   GeolocationSettingsState state(&profile);
140   GURL url_0("http://www.example.com");
141
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);
147
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);
155
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);
164
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);
173
174   GeolocationSettingsState::StateMap state_map =
175       state.state_map();
176   EXPECT_EQ(3U, state_map.size());
177
178   GeolocationSettingsState::FormattedHostsPerState formatted_host_per_state;
179   unsigned int tab_state_flags = 0;
180   state.GetDetailedInfo(&formatted_host_per_state, &tab_state_flags);
181
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()));
192
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);
197
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()));
209 }
210
211
212 }  // namespace