OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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 "base/message_loop.h" | 5 #include "base/message_loop.h" |
6 #include "chrome/browser/content_settings/content_settings_details.h" | 6 #include "chrome/browser/content_settings/content_settings_details.h" |
7 #include "chrome/browser/geolocation/geolocation_content_settings_map.h" | 7 #include "chrome/browser/geolocation/geolocation_content_settings_map.h" |
8 #include "chrome/browser/prefs/pref_service.h" | 8 #include "chrome/browser/prefs/pref_service.h" |
9 #include "chrome/browser/prefs/scoped_user_pref_update.h" | 9 #include "chrome/browser/prefs/scoped_user_pref_update.h" |
10 #include "chrome/common/pref_names.h" | 10 #include "chrome/common/pref_names.h" |
11 #include "chrome/test/testing_profile.h" | 11 #include "chrome/test/testing_profile.h" |
12 #include "content/browser/browser_thread.h" | 12 #include "content/browser/browser_thread.h" |
13 #include "content/common/notification_registrar.h" | 13 #include "content/common/notification_registrar.h" |
14 #include "content/common/notification_service.h" | 14 #include "content/common/notification_service.h" |
15 #include "testing/gmock/include/gmock/gmock.h" | |
15 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
16 | 17 |
18 using ::testing::_; | |
17 | 19 |
18 namespace { | 20 namespace { |
19 | 21 |
20 class StubSettingsObserver : public NotificationObserver { | 22 class MockGeolocationSettingsObserver : public NotificationObserver { |
21 public: | 23 public: |
22 StubSettingsObserver() : last_notifier(NULL), counter(0) { | 24 MockGeolocationSettingsObserver(); |
23 registrar_.Add(this, NotificationType::GEOLOCATION_SETTINGS_CHANGED, | |
24 NotificationService::AllSources()); | |
25 } | |
26 | 25 |
27 virtual void Observe(NotificationType type, | 26 virtual void Observe(NotificationType type, |
28 const NotificationSource& source, | 27 const NotificationSource& source, |
29 const NotificationDetails& details) { | 28 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 | 29 |
44 GeolocationContentSettingsMap* last_notifier; | 30 MOCK_METHOD2(OnContentSettingsChanged, |
45 ContentSettingsPattern last_pattern; | 31 void(GeolocationContentSettingsMap*, |
46 bool last_update_all; | 32 ContentSettingsType)); |
47 bool last_update_all_types; | |
48 int counter; | |
49 ContentSettingsType last_type; | |
50 | 33 |
51 private: | 34 private: |
52 NotificationRegistrar registrar_; | 35 NotificationRegistrar registrar_; |
53 }; | 36 }; |
54 | 37 |
38 MockGeolocationSettingsObserver::MockGeolocationSettingsObserver() { | |
39 registrar_.Add(this, NotificationType::GEOLOCATION_SETTINGS_CHANGED, | |
40 NotificationService::AllSources()); | |
41 } | |
42 | |
43 void MockGeolocationSettingsObserver::Observe( | |
44 NotificationType type, | |
45 const NotificationSource& source, | |
46 const NotificationDetails& details) { | |
47 GeolocationContentSettingsMap* map = | |
48 Source<GeolocationContentSettingsMap>(source).ptr(); | |
49 ContentSettingsDetails* settings_details = | |
50 Details<ContentSettingsDetails>(details).ptr(); | |
51 OnContentSettingsChanged(map, | |
52 settings_details->type()); | |
53 // This checks that calling a Get function from an observer doesn't | |
54 // deadlock. | |
55 // TODO(bauerb): Move into expectation setup. | |
56 map->GetContentSetting(GURL("http://random-hostname.com/"), | |
57 GURL("http://foo.random-hostname.com/")); | |
58 } | |
59 | |
55 class GeolocationContentSettingsMapTests : public testing::Test { | 60 class GeolocationContentSettingsMapTests : public testing::Test { |
56 public: | 61 public: |
57 GeolocationContentSettingsMapTests() | 62 GeolocationContentSettingsMapTests() |
58 : ui_thread_(BrowserThread::UI, &message_loop_) { | 63 : ui_thread_(BrowserThread::UI, &message_loop_) { |
59 } | 64 } |
60 | 65 |
61 protected: | 66 protected: |
62 MessageLoop message_loop_; | 67 MessageLoop message_loop_; |
63 BrowserThread ui_thread_; | 68 BrowserThread ui_thread_; |
64 }; | 69 }; |
(...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
278 GURL("http://example.com"))); | 283 GURL("http://example.com"))); |
279 EXPECT_EQ(CONTENT_SETTING_ASK, | 284 EXPECT_EQ(CONTENT_SETTING_ASK, |
280 map->GetContentSetting(GURL("http://bad_requester/"), | 285 map->GetContentSetting(GURL("http://bad_requester/"), |
281 GURL("http://b/"))); | 286 GURL("http://b/"))); |
282 } | 287 } |
283 | 288 |
284 TEST_F(GeolocationContentSettingsMapTests, Observe) { | 289 TEST_F(GeolocationContentSettingsMapTests, Observe) { |
285 TestingProfile profile; | 290 TestingProfile profile; |
286 GeolocationContentSettingsMap* map = | 291 GeolocationContentSettingsMap* map = |
287 profile.GetGeolocationContentSettingsMap(); | 292 profile.GetGeolocationContentSettingsMap(); |
288 StubSettingsObserver observer; | 293 MockGeolocationSettingsObserver observer; |
289 | 294 |
290 EXPECT_EQ(CONTENT_SETTING_ASK, map->GetDefaultContentSetting()); | 295 EXPECT_EQ(CONTENT_SETTING_ASK, map->GetDefaultContentSetting()); |
291 | 296 |
292 // Test if a CONTENT_SETTING_CHANGE notification is sent after the geolocation | 297 // Test if a GEOLOCATION_CONTENT_SETTINGS_CHANGED notification is sent after |
battre
2011/06/03 18:48:36
The event is called "GEOLOCATION_SETTINGS_CHANGED"
Bernhard Bauer
2011/06/03 20:34:28
Hooray for consistent naming! Done.
| |
293 // default content setting was changed through calling the | 298 // the geolocation default content setting was changed through calling the |
294 // SetDefaultContentSetting method. | 299 // SetDefaultContentSetting method. |
300 EXPECT_CALL( | |
301 observer, | |
302 OnContentSettingsChanged(map, CONTENT_SETTINGS_TYPE_DEFAULT)); | |
295 map->SetDefaultContentSetting(CONTENT_SETTING_BLOCK); | 303 map->SetDefaultContentSetting(CONTENT_SETTING_BLOCK); |
296 EXPECT_EQ(map, observer.last_notifier); | 304 ::testing::Mock::VerifyAndClearExpectations(&observer); |
297 EXPECT_EQ(CONTENT_SETTINGS_TYPE_DEFAULT, observer.last_type); | |
298 EXPECT_EQ(1, observer.counter); | |
299 | 305 |
300 | 306 // Test if a GEOLOCATION_CONTENT_SETTINGS_CHANGED notification is sent after |
battre
2011/06/03 18:48:36
same here
| |
301 // Test if a CONTENT_SETTING_CHANGE notification is sent after the preference | 307 // the preference kGeolocationDefaultContentSetting was changed. |
302 // GeolocationDefaultContentSetting was changed. | |
303 PrefService* prefs = profile.GetPrefs(); | 308 PrefService* prefs = profile.GetPrefs(); |
309 EXPECT_CALL( | |
310 observer, | |
311 OnContentSettingsChanged(map, CONTENT_SETTINGS_TYPE_DEFAULT)); | |
304 prefs->SetInteger(prefs::kGeolocationDefaultContentSetting, | 312 prefs->SetInteger(prefs::kGeolocationDefaultContentSetting, |
305 CONTENT_SETTING_ALLOW); | 313 CONTENT_SETTING_ALLOW); |
306 EXPECT_EQ(2, observer.counter); | |
307 EXPECT_EQ(map, observer.last_notifier); | |
308 EXPECT_EQ(CONTENT_SETTINGS_TYPE_DEFAULT, observer.last_type); | |
309 EXPECT_EQ(CONTENT_SETTING_ALLOW, map->GetDefaultContentSetting()); | 314 EXPECT_EQ(CONTENT_SETTING_ALLOW, map->GetDefaultContentSetting()); |
310 } | 315 } |
311 | 316 |
312 } // namespace | 317 } // namespace |
OLD | NEW |