Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(16)

Side by Side Diff: chrome/browser/notifications/desktop_notification_service_unittest.cc

Issue 7810017: Revert 98938 - Migrate Obsolete NotificationsSettings and remove content_settings::NotificationsP... (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 9 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 "chrome/browser/notifications/desktop_notification_service.h" 5 #include "chrome/browser/notifications/desktop_notification_service.h"
6 6
7 #include "base/memory/scoped_ptr.h" 7 #include "base/memory/ref_counted.h"
8 #include "base/message_loop.h" 8 #include "base/message_loop.h"
9 #include "base/synchronization/waitable_event.h" 9 #include "base/synchronization/waitable_event.h"
10 #include "chrome/browser/content_settings/host_content_settings_map.h"
11 #include "chrome/browser/notifications/desktop_notification_service_factory.h" 10 #include "chrome/browser/notifications/desktop_notification_service_factory.h"
11 #include "chrome/browser/notifications/notifications_prefs_cache.h"
12 #include "chrome/browser/prefs/pref_service.h"
13 #include "chrome/browser/prefs/scoped_user_pref_update.h"
14 #include "chrome/common/pref_names.h"
12 #include "chrome/test/base/testing_profile.h" 15 #include "chrome/test/base/testing_profile.h"
13 #include "content/browser/browser_thread.h" 16 #include "content/browser/browser_thread.h"
14 #include "content/browser/renderer_host/test_render_view_host.h" 17 #include "content/browser/renderer_host/test_render_view_host.h"
18 #include "grit/generated_resources.h"
15 #include "testing/gtest/include/gtest/gtest.h" 19 #include "testing/gtest/include/gtest/gtest.h"
16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebNotificationPresen ter.h" 20 #include "third_party/WebKit/Source/WebKit/chromium/public/WebNotificationPresen ter.h"
17 21
18 namespace { 22 namespace {
19 23
20 // The HasPermission method of the DesktopNotificationService wants to be called 24 // NotificationsPrefsCache wants to be called on the IO thread. This class
21 // on the IO thread. This class routes calls to the cache on the IO thread. 25 // routes calls to the cache on the IO thread.
22 class ThreadProxy : public base::RefCountedThreadSafe<ThreadProxy> { 26 class ThreadProxy : public base::RefCountedThreadSafe<ThreadProxy> {
23 public: 27 public:
24 ThreadProxy() 28 ThreadProxy()
25 : io_event_(false, false), 29 : io_event_(false, false),
26 ui_event_(false, false), 30 ui_event_(false, false),
27 permission_(WebKit::WebNotificationPresenter::PermissionAllowed) { 31 permission_(0) {
28 // The current message loop was already initalized by the test superclass. 32 // The current message loop was already initalized by the test superclass.
29 ui_thread_.reset( 33 ui_thread_.reset(
30 new BrowserThread(BrowserThread::UI, MessageLoop::current())); 34 new BrowserThread(BrowserThread::UI, MessageLoop::current()));
31 35
32 // Create IO thread, start its message loop. 36 // Create IO thread, start its message loop.
33 io_thread_.reset(new BrowserThread(BrowserThread::IO)); 37 io_thread_.reset(new BrowserThread(BrowserThread::IO));
34 io_thread_->Start(); 38 io_thread_->Start();
35 39
36 // Calling PauseIOThread() here isn't safe, because the runnable method 40 // Calling PauseIOThread() here isn't safe, because the runnable method
37 // could complete before the constructor is done, deleting |this|. 41 // could complete before the constructor is done, deleting |this|.
38 } 42 }
39 43
40 // Call the HasPermission method of the DesktopNotificationService on the IO 44 int CacheHasPermission(NotificationsPrefsCache* cache, const GURL& url) {
41 // thread and returns the permission setting.
42 WebKit::WebNotificationPresenter::Permission ServiceHasPermission(
43 DesktopNotificationService* service,
44 const GURL& url) {
45 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 45 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
46 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, 46 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
47 NewRunnableMethod(this, &ThreadProxy::ServiceHasPermissionIO, 47 NewRunnableMethod(this, &ThreadProxy::CacheHasPermissionIO,
48 service, url)); 48 make_scoped_refptr(cache), url));
49 io_event_.Signal(); 49 io_event_.Signal();
50 ui_event_.Wait(); // Wait for IO thread to be done. 50 ui_event_.Wait(); // Wait for IO thread to be done.
51 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, 51 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
52 NewRunnableMethod(this, &ThreadProxy::PauseIOThreadIO)); 52 NewRunnableMethod(this, &ThreadProxy::PauseIOThreadIO));
53 53
54 return permission_; 54 return permission_;
55 } 55 }
56 56
57 void PauseIOThread() { 57 void PauseIOThread() {
58 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, 58 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
(...skipping 10 matching lines...) Expand all
69 friend class base::RefCountedThreadSafe<ThreadProxy>; 69 friend class base::RefCountedThreadSafe<ThreadProxy>;
70 ~ThreadProxy() { 70 ~ThreadProxy() {
71 DrainIOThread(); 71 DrainIOThread();
72 } 72 }
73 73
74 void PauseIOThreadIO() { 74 void PauseIOThreadIO() {
75 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 75 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
76 io_event_.Wait(); 76 io_event_.Wait();
77 } 77 }
78 78
79 void ServiceHasPermissionIO(DesktopNotificationService* service, 79 void CacheHasPermissionIO(NotificationsPrefsCache* cache, const GURL& url) {
80 const GURL& url) {
81 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 80 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
82 permission_ = service->HasPermission(url); 81 permission_ = cache->HasPermission(url);
83 ui_event_.Signal(); 82 ui_event_.Signal();
84 } 83 }
85 84
86 base::WaitableEvent io_event_; 85 base::WaitableEvent io_event_;
87 base::WaitableEvent ui_event_; 86 base::WaitableEvent ui_event_;
88 scoped_ptr<BrowserThread> ui_thread_; 87 scoped_ptr<BrowserThread> ui_thread_;
89 scoped_ptr<BrowserThread> io_thread_; 88 scoped_ptr<BrowserThread> io_thread_;
90 89
91 WebKit::WebNotificationPresenter::Permission permission_; 90 int permission_;
92 }; 91 };
93 92
94 } // namespace
95 93
96 class DesktopNotificationServiceTest : public RenderViewHostTestHarness { 94 class DesktopNotificationServiceTest : public RenderViewHostTestHarness {
97 public: 95 public:
98 DesktopNotificationServiceTest() { 96 DesktopNotificationServiceTest() {
99 } 97 }
100 98
101 virtual void SetUp() { 99 virtual void SetUp() {
102 RenderViewHostTestHarness::SetUp(); 100 RenderViewHostTestHarness::SetUp();
103 proxy_ = new ThreadProxy; 101 proxy_ = new ThreadProxy;
104 proxy_->PauseIOThread(); 102 proxy_->PauseIOThread();
105 103
106 // Creates the destop notification service. 104 // Creates the service, calls InitPrefs() on it which loads data from the
105 // profile into the cache and then puts the cache in io thread mode.
107 service_ = DesktopNotificationServiceFactory::GetForProfile(profile()); 106 service_ = DesktopNotificationServiceFactory::GetForProfile(profile());
107 cache_ = service_->prefs_cache();
108 } 108 }
109 109
110 virtual void TearDown() { 110 virtual void TearDown() {
111 // The io thread's waiting on the io_event_ might hold a ref to |proxy_|, 111 // The io thread's waiting on the io_event_ might hold a ref to |proxy_|,
112 // preventing its destruction. Clear that ref. 112 // preventing its destruction. Clear that ref.
113 proxy_->DrainIOThread(); 113 proxy_->DrainIOThread();
114 RenderViewHostTestHarness::TearDown(); 114 RenderViewHostTestHarness::TearDown();
115 } 115 }
116 116
117 DesktopNotificationService* service_; 117 DesktopNotificationService* service_;
118 NotificationsPrefsCache* cache_;
118 scoped_refptr<ThreadProxy> proxy_; 119 scoped_refptr<ThreadProxy> proxy_;
119 }; 120 };
120 121
122 TEST_F(DesktopNotificationServiceTest, DefaultContentSettingSentToCache) {
123 // The default pref registered in DesktopNotificationService is "ask",
124 // and that's what sent to the cache.
125 EXPECT_EQ(CONTENT_SETTING_ASK, cache_->CachedDefaultContentSetting());
126
127 // Change the default content setting. This will post a task on the IO thread
128 // to update the cache.
129 service_->SetDefaultContentSetting(CONTENT_SETTING_BLOCK);
130
131 // The updated pref shouldn't be sent to the cache immediately.
132 EXPECT_EQ(CONTENT_SETTING_ASK, cache_->CachedDefaultContentSetting());
133
134 // Run IO thread tasks.
135 proxy_->DrainIOThread();
136
137 // Now that IO thread events have been processed, it should be there.
138 EXPECT_EQ(CONTENT_SETTING_BLOCK, cache_->CachedDefaultContentSetting());
139 }
140
121 TEST_F(DesktopNotificationServiceTest, SettingsForSchemes) { 141 TEST_F(DesktopNotificationServiceTest, SettingsForSchemes) {
122 GURL url("file:///html/test.html"); 142 GURL url("file:///html/test.html");
123 143
124 EXPECT_EQ(CONTENT_SETTING_ASK, 144 EXPECT_EQ(CONTENT_SETTING_ASK, cache_->CachedDefaultContentSetting());
125 service_->GetDefaultContentSetting());
126 EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionNotAllowed, 145 EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionNotAllowed,
127 proxy_->ServiceHasPermission(service_, url)); 146 proxy_->CacheHasPermission(cache_, url));
128 147
129 service_->GrantPermission(url); 148 service_->GrantPermission(url);
130 EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionAllowed, 149 EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionAllowed,
131 proxy_->ServiceHasPermission(service_, url)); 150 proxy_->CacheHasPermission(cache_, url));
132 151
133 service_->DenyPermission(url); 152 service_->DenyPermission(url);
134 EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionDenied, 153 EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionDenied,
135 proxy_->ServiceHasPermission(service_, url)); 154 proxy_->CacheHasPermission(cache_, url));
136 155
137 GURL https_url("https://testurl"); 156 GURL https_url("https://testurl");
138 GURL http_url("http://testurl"); 157 GURL http_url("http://testurl");
139 EXPECT_EQ(CONTENT_SETTING_ASK, 158 EXPECT_EQ(CONTENT_SETTING_ASK, cache_->CachedDefaultContentSetting());
140 service_->GetDefaultContentSetting());
141 EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionNotAllowed, 159 EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionNotAllowed,
142 proxy_->ServiceHasPermission(service_, http_url)); 160 proxy_->CacheHasPermission(cache_, http_url));
143 EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionNotAllowed, 161 EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionNotAllowed,
144 proxy_->ServiceHasPermission(service_, https_url)); 162 proxy_->CacheHasPermission(cache_, https_url));
145 163
146 service_->GrantPermission(https_url); 164 service_->GrantPermission(https_url);
165 EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionAllowed,
166 proxy_->CacheHasPermission(cache_, https_url));
147 EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionNotAllowed, 167 EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionNotAllowed,
148 proxy_->ServiceHasPermission(service_, http_url)); 168 proxy_->CacheHasPermission(cache_, http_url));
149 EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionAllowed,
150 proxy_->ServiceHasPermission(service_, https_url));
151 169
152 service_->DenyPermission(http_url); 170 service_->DenyPermission(http_url);
153 EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionDenied, 171 EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionDenied,
154 proxy_->ServiceHasPermission(service_, http_url)); 172 proxy_->CacheHasPermission(cache_, http_url));
155 EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionAllowed, 173 EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionAllowed,
156 proxy_->ServiceHasPermission(service_, https_url)); 174 proxy_->CacheHasPermission(cache_, https_url));
157 } 175 }
158 176
159 TEST_F(DesktopNotificationServiceTest, GetNotificationsSettings) { 177 TEST_F(DesktopNotificationServiceTest, GrantPermissionSentToCache) {
178 GURL url("http://allowed.com");
179 EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionNotAllowed,
180 proxy_->CacheHasPermission(cache_, url));
181
182 service_->GrantPermission(url);
183
184 EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionAllowed,
185 proxy_->CacheHasPermission(cache_, url));
186 }
187
188 TEST_F(DesktopNotificationServiceTest, DenyPermissionSentToCache) {
189 GURL url("http://denied.com");
190 EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionNotAllowed,
191 proxy_->CacheHasPermission(cache_, url));
192
193 service_->DenyPermission(url);
194
195 EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionDenied,
196 proxy_->CacheHasPermission(cache_, url));
197 }
198
199 TEST_F(DesktopNotificationServiceTest, PrefChangesSentToCache) {
200 PrefService* prefs = profile()->GetPrefs();
201
202 {
203 ListPrefUpdate update_allowed_origins(
204 prefs, prefs::kDesktopNotificationAllowedOrigins);
205 ListValue* allowed_origins = update_allowed_origins.Get();
206 allowed_origins->Append(new StringValue(GURL("http://allowed.com").spec()));
207 }
208
209 {
210 ListPrefUpdate update_denied_origins(
211 prefs, prefs::kDesktopNotificationDeniedOrigins);
212 ListValue* denied_origins = update_denied_origins.Get();
213 denied_origins->Append(new StringValue(GURL("http://denied.com").spec()));
214 }
215
216 EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionAllowed,
217 proxy_->CacheHasPermission(cache_, GURL("http://allowed.com")));
218 EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionDenied,
219 proxy_->CacheHasPermission(cache_, GURL("http://denied.com")));
220 }
221
222 TEST_F(DesktopNotificationServiceTest, GetAllowedOrigins) {
160 service_->GrantPermission(GURL("http://allowed2.com")); 223 service_->GrantPermission(GURL("http://allowed2.com"));
161 service_->GrantPermission(GURL("http://allowed.com")); 224 service_->GrantPermission(GURL("http://allowed.com"));
225
226 std::vector<GURL> allowed_origins(service_->GetAllowedOrigins());
227 ASSERT_EQ(2u, allowed_origins.size());
228 EXPECT_EQ(GURL("http://allowed2.com"), allowed_origins[0]);
229 EXPECT_EQ(GURL("http://allowed.com"), allowed_origins[1]);
230 }
231
232 TEST_F(DesktopNotificationServiceTest, GetBlockedOrigins) {
162 service_->DenyPermission(GURL("http://denied2.com")); 233 service_->DenyPermission(GURL("http://denied2.com"));
163 service_->DenyPermission(GURL("http://denied.com")); 234 service_->DenyPermission(GURL("http://denied.com"));
164 235
165 HostContentSettingsMap::SettingsForOneType settings; 236 std::vector<GURL> denied_origins(service_->GetBlockedOrigins());
166 service_->GetNotificationsSettings(&settings); 237 ASSERT_EQ(2u, denied_origins.size());
167 ASSERT_EQ(4u, settings.size()); 238 EXPECT_EQ(GURL("http://denied2.com"), denied_origins[0]);
239 EXPECT_EQ(GURL("http://denied.com"), denied_origins[1]);
240 }
168 241
169 EXPECT_EQ(ContentSettingsPattern::FromURLNoWildcard( 242 TEST_F(DesktopNotificationServiceTest, ResetAllSentToCache) {
170 GURL("http://allowed.com")), 243 GURL allowed_url("http://allowed.com");
171 settings[0].a); 244 service_->GrantPermission(allowed_url);
172 EXPECT_EQ(CONTENT_SETTING_ALLOW, 245 EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionAllowed,
173 settings[0].c); 246 proxy_->CacheHasPermission(cache_, allowed_url));
174 EXPECT_EQ(ContentSettingsPattern::FromURLNoWildcard( 247 GURL denied_url("http://denied.com");
175 GURL("http://allowed2.com")), 248 service_->DenyPermission(denied_url);
176 settings[1].a); 249 EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionDenied,
177 EXPECT_EQ(CONTENT_SETTING_ALLOW, 250 proxy_->CacheHasPermission(cache_, denied_url));
178 settings[1].c); 251
179 EXPECT_EQ(ContentSettingsPattern::FromURLNoWildcard( 252 service_->ResetAllOrigins();
180 GURL("http://denied.com")), 253
181 settings[2].a); 254 EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionNotAllowed,
182 EXPECT_EQ(CONTENT_SETTING_BLOCK, 255 proxy_->CacheHasPermission(cache_, allowed_url));
183 settings[2].c); 256 EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionNotAllowed,
184 EXPECT_EQ(ContentSettingsPattern::FromURLNoWildcard( 257 proxy_->CacheHasPermission(cache_, denied_url));
185 GURL("http://denied2.com")),
186 settings[3].a);
187 EXPECT_EQ(CONTENT_SETTING_BLOCK,
188 settings[3].c);
189 } 258 }
259
260 TEST_F(DesktopNotificationServiceTest, ResetAllowedSentToCache) {
261 GURL allowed_url("http://allowed.com");
262 service_->GrantPermission(allowed_url);
263 EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionAllowed,
264 proxy_->CacheHasPermission(cache_, allowed_url));
265
266 service_->ResetAllowedOrigin(allowed_url);
267
268 EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionNotAllowed,
269 proxy_->CacheHasPermission(cache_, allowed_url));
270 }
271
272 TEST_F(DesktopNotificationServiceTest, ResetBlockedSentToCache) {
273 GURL denied_url("http://denied.com");
274 service_->DenyPermission(denied_url);
275 EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionDenied,
276 proxy_->CacheHasPermission(cache_, denied_url));
277
278 service_->ResetBlockedOrigin(denied_url);
279
280 EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionNotAllowed,
281 proxy_->CacheHasPermission(cache_, denied_url));
282 }
283
284 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698