OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/geolocation/geolocation_content_settings_map.h" | 5 #include "chrome/browser/geolocation/geolocation_content_settings_map.h" |
6 | 6 |
7 #include "base/message_loop.h" | 7 #include "base/message_loop.h" |
8 #include "chrome/browser/browser_thread.h" | 8 #include "chrome/browser/browser_thread.h" |
| 9 #include "chrome/browser/content_settings/content_settings_details.h" |
9 #include "chrome/browser/prefs/pref_service.h" | 10 #include "chrome/browser/prefs/pref_service.h" |
| 11 #include "chrome/common/notification_registrar.h" |
| 12 #include "chrome/common/notification_service.h" |
10 #include "chrome/common/pref_names.h" | 13 #include "chrome/common/pref_names.h" |
11 #include "chrome/test/testing_profile.h" | 14 #include "chrome/test/testing_profile.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
13 | 16 |
14 | 17 |
15 namespace { | 18 namespace { |
16 | 19 |
| 20 class StubSettingsObserver : public NotificationObserver { |
| 21 public: |
| 22 StubSettingsObserver() : last_notifier(NULL), counter(0) { |
| 23 registrar_.Add(this, NotificationType::GEOLOCATION_SETTINGS_CHANGED, |
| 24 NotificationService::AllSources()); |
| 25 } |
| 26 |
| 27 virtual void Observe(NotificationType type, |
| 28 const NotificationSource& source, |
| 29 const NotificationDetails& details) { |
| 30 ++counter; |
| 31 Source<GeolocationContentSettingsMap> content_settings(source); |
| 32 Details<ContentSettingsDetails> settings_details(details); |
| 33 last_notifier = content_settings.ptr(); |
| 34 last_pattern = settings_details.ptr()->pattern(); |
| 35 last_update_all = settings_details.ptr()->update_all(); |
| 36 last_update_all_types = settings_details.ptr()->update_all_types(); |
| 37 last_type = settings_details.ptr()->type(); |
| 38 // This checks that calling a Get function from an observer doesn't |
| 39 // deadlock. |
| 40 last_notifier->GetContentSetting(GURL("http://random-hostname.com/"), |
| 41 GURL("http://foo.random-hostname.com/")); |
| 42 } |
| 43 |
| 44 GeolocationContentSettingsMap* last_notifier; |
| 45 ContentSettingsPattern last_pattern; |
| 46 bool last_update_all; |
| 47 bool last_update_all_types; |
| 48 int counter; |
| 49 ContentSettingsType last_type; |
| 50 |
| 51 private: |
| 52 NotificationRegistrar registrar_; |
| 53 }; |
| 54 |
17 class GeolocationContentSettingsMapTests : public testing::Test { | 55 class GeolocationContentSettingsMapTests : public testing::Test { |
18 public: | 56 public: |
19 GeolocationContentSettingsMapTests() | 57 GeolocationContentSettingsMapTests() |
20 : ui_thread_(BrowserThread::UI, &message_loop_) { | 58 : ui_thread_(BrowserThread::UI, &message_loop_) { |
21 } | 59 } |
22 | 60 |
23 protected: | 61 protected: |
24 MessageLoop message_loop_; | 62 MessageLoop message_loop_; |
25 BrowserThread ui_thread_; | 63 BrowserThread ui_thread_; |
26 }; | 64 }; |
(...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
234 map->GetContentSetting(GURL("http://a/"), | 272 map->GetContentSetting(GURL("http://a/"), |
235 GURL("http://bad-embedder"))); | 273 GURL("http://bad-embedder"))); |
236 EXPECT_EQ(CONTENT_SETTING_ASK, | 274 EXPECT_EQ(CONTENT_SETTING_ASK, |
237 map->GetContentSetting(GURL("http://a/"), | 275 map->GetContentSetting(GURL("http://a/"), |
238 GURL("http://example.com"))); | 276 GURL("http://example.com"))); |
239 EXPECT_EQ(CONTENT_SETTING_ASK, | 277 EXPECT_EQ(CONTENT_SETTING_ASK, |
240 map->GetContentSetting(GURL("http://bad_requester/"), | 278 map->GetContentSetting(GURL("http://bad_requester/"), |
241 GURL("http://b/"))); | 279 GURL("http://b/"))); |
242 } | 280 } |
243 | 281 |
| 282 TEST_F(GeolocationContentSettingsMapTests, Observe) { |
| 283 TestingProfile profile; |
| 284 GeolocationContentSettingsMap* map = |
| 285 profile.GetGeolocationContentSettingsMap(); |
| 286 StubSettingsObserver observer; |
| 287 |
| 288 EXPECT_EQ(CONTENT_SETTING_ASK, map->GetDefaultContentSetting()); |
| 289 |
| 290 // Test if a CONTENT_SETTING_CHANGE notification is sent after the geolocation |
| 291 // default content setting was changed through calling the |
| 292 // SetDefaultContentSetting method. |
| 293 map->SetDefaultContentSetting(CONTENT_SETTING_BLOCK); |
| 294 EXPECT_EQ(map, observer.last_notifier); |
| 295 EXPECT_EQ(CONTENT_SETTINGS_TYPE_DEFAULT, observer.last_type); |
| 296 EXPECT_EQ(1, observer.counter); |
| 297 |
| 298 |
| 299 // Test if a CONTENT_SETTING_CHANGE notification is sent after the preference |
| 300 // GeolocationDefaultContentSetting was changed. |
| 301 PrefService* prefs = profile.GetPrefs(); |
| 302 prefs->SetInteger(prefs::kGeolocationDefaultContentSetting, |
| 303 CONTENT_SETTING_ALLOW); |
| 304 EXPECT_EQ(2, observer.counter); |
| 305 EXPECT_EQ(map, observer.last_notifier); |
| 306 EXPECT_EQ(CONTENT_SETTINGS_TYPE_DEFAULT, observer.last_type); |
| 307 EXPECT_EQ(CONTENT_SETTING_ALLOW, map->GetDefaultContentSetting()); |
| 308 } |
| 309 |
244 } // namespace | 310 } // namespace |
OLD | NEW |