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/host_content_settings_map.h" | 5 #include "chrome/browser/host_content_settings_map.h" |
6 | 6 |
| 7 #include "chrome/common/notification_registrar.h" |
| 8 #include "chrome/common/notification_service.h" |
7 #include "chrome/test/testing_profile.h" | 9 #include "chrome/test/testing_profile.h" |
8 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
9 | 11 |
10 | 12 |
11 namespace { | 13 namespace { |
12 | 14 |
13 bool SettingsEqual(const ContentSettings& settings1, | 15 bool SettingsEqual(const ContentSettings& settings1, |
14 const ContentSettings& settings2) { | 16 const ContentSettings& settings2) { |
15 for (int i = 0; i < CONTENT_SETTINGS_NUM_TYPES; ++i) { | 17 for (int i = 0; i < CONTENT_SETTINGS_NUM_TYPES; ++i) { |
16 if (settings1.settings[i] != settings2.settings[i]) | 18 if (settings1.settings[i] != settings2.settings[i]) |
17 return false; | 19 return false; |
18 } | 20 } |
19 return true; | 21 return true; |
20 } | 22 } |
21 | 23 |
| 24 class StubSettingsObserver : public NotificationObserver { |
| 25 public: |
| 26 StubSettingsObserver() |
| 27 : last_notifier(NULL), counter(0) { |
| 28 registrar_.Add(this, NotificationType::CONTENT_SETTINGS_CHANGED, |
| 29 NotificationService::AllSources()); |
| 30 } |
| 31 |
| 32 virtual void Observe(NotificationType type, |
| 33 const NotificationSource& source, |
| 34 const NotificationDetails& details) { |
| 35 ++counter; |
| 36 Source<HostContentSettingsMap> content_settings(source); |
| 37 Details<HostContentSettingsMap::ContentSettingsDetails> |
| 38 settings_details(details); |
| 39 last_notifier = content_settings.ptr(); |
| 40 last_host = settings_details.ptr()->host(); |
| 41 // This checks that calling a Get function from an observer doesn't |
| 42 // deadlock. |
| 43 last_notifier->GetContentSettings("random-hostname"); |
| 44 } |
| 45 |
| 46 HostContentSettingsMap* last_notifier; |
| 47 std::string last_host; |
| 48 int counter; |
| 49 |
| 50 private: |
| 51 NotificationRegistrar registrar_; |
| 52 }; |
| 53 |
22 class HostContentSettingsMapTest : public testing::Test { | 54 class HostContentSettingsMapTest : public testing::Test { |
23 public: | 55 public: |
24 HostContentSettingsMapTest() | 56 HostContentSettingsMapTest() |
25 : ui_thread_(ChromeThread::UI, &message_loop_) {} | 57 : ui_thread_(ChromeThread::UI, &message_loop_) {} |
26 | 58 |
27 protected: | 59 protected: |
28 MessageLoop message_loop_; | 60 MessageLoop message_loop_; |
29 ChromeThread ui_thread_; | 61 ChromeThread ui_thread_; |
30 }; | 62 }; |
31 | 63 |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
130 host_content_settings_map->ClearSettingsForOneType( | 162 host_content_settings_map->ClearSettingsForOneType( |
131 CONTENT_SETTINGS_TYPE_IMAGES); | 163 CONTENT_SETTINGS_TYPE_IMAGES); |
132 host_content_settings_map->GetSettingsForOneType(CONTENT_SETTINGS_TYPE_IMAGES, | 164 host_content_settings_map->GetSettingsForOneType(CONTENT_SETTINGS_TYPE_IMAGES, |
133 &host_settings); | 165 &host_settings); |
134 EXPECT_EQ(0U, host_settings.size()); | 166 EXPECT_EQ(0U, host_settings.size()); |
135 host_content_settings_map->GetSettingsForOneType( | 167 host_content_settings_map->GetSettingsForOneType( |
136 CONTENT_SETTINGS_TYPE_PLUGINS, &host_settings); | 168 CONTENT_SETTINGS_TYPE_PLUGINS, &host_settings); |
137 EXPECT_EQ(1U, host_settings.size()); | 169 EXPECT_EQ(1U, host_settings.size()); |
138 } | 170 } |
139 | 171 |
| 172 TEST_F(HostContentSettingsMapTest, Observer) { |
| 173 TestingProfile profile; |
| 174 HostContentSettingsMap* host_content_settings_map = |
| 175 profile.GetHostContentSettingsMap(); |
| 176 StubSettingsObserver observer; |
| 177 |
| 178 std::string host("example.com"); |
| 179 host_content_settings_map->SetContentSetting(host, |
| 180 CONTENT_SETTINGS_TYPE_IMAGES, CONTENT_SETTING_ALLOW); |
| 181 EXPECT_EQ(host_content_settings_map, observer.last_notifier); |
| 182 EXPECT_EQ(host, observer.last_host); |
| 183 EXPECT_EQ(1, observer.counter); |
| 184 |
| 185 host_content_settings_map->ClearSettingsForOneType( |
| 186 CONTENT_SETTINGS_TYPE_IMAGES); |
| 187 EXPECT_EQ(host_content_settings_map, observer.last_notifier); |
| 188 EXPECT_EQ(std::string(), observer.last_host); |
| 189 EXPECT_EQ(2, observer.counter); |
| 190 |
| 191 host_content_settings_map->ResetToDefaults(); |
| 192 EXPECT_EQ(host_content_settings_map, observer.last_notifier); |
| 193 EXPECT_EQ(std::string(), observer.last_host); |
| 194 EXPECT_EQ(3, observer.counter); |
| 195 |
| 196 host_content_settings_map->SetDefaultContentSetting( |
| 197 CONTENT_SETTINGS_TYPE_IMAGES, CONTENT_SETTING_BLOCK); |
| 198 EXPECT_EQ(host_content_settings_map, observer.last_notifier); |
| 199 EXPECT_EQ(std::string(), observer.last_host); |
| 200 EXPECT_EQ(4, observer.counter); |
| 201 } |
| 202 |
140 } // namespace | 203 } // namespace |
OLD | NEW |