blob: 5e700d5187b5df5077d56a91438b9fc0513fc601 [file] [log] [blame]
[email protected]0c6c4be2011-03-23 12:31:201// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]78dcb902010-07-03 02:29:522// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "chrome/browser/notifications/desktop_notification_service.h"
6
[email protected]3b63f8f42011-03-28 01:54:157#include "base/memory/ref_counted.h"
[email protected]39d9f62c2010-12-03 10:48:508#include "base/message_loop.h"
[email protected]44f9c952011-01-02 06:05:399#include "base/synchronization/waitable_event.h"
[email protected]78dcb902010-07-03 02:29:5210#include "chrome/browser/notifications/notifications_prefs_cache.h"
[email protected]37858e52010-08-26 00:22:0211#include "chrome/browser/prefs/pref_service.h"
[email protected]f89ee342011-03-07 09:28:2712#include "chrome/browser/prefs/scoped_user_pref_update.h"
[email protected]f4192f22010-07-06 16:45:2213#include "chrome/common/pref_names.h"
[email protected]78dcb902010-07-03 02:29:5214#include "chrome/test/testing_profile.h"
[email protected]1bda97552011-03-01 20:11:5215#include "content/browser/browser_thread.h"
[email protected]79ea4862011-02-24 00:46:4416#include "content/browser/renderer_host/test_render_view_host.h"
[email protected]78dcb902010-07-03 02:29:5217#include "grit/generated_resources.h"
18#include "testing/gtest/include/gtest/gtest.h"
[email protected]8bd0fe62011-01-17 06:44:3719#include "third_party/WebKit/Source/WebKit/chromium/public/WebNotificationPresenter.h"
[email protected]78dcb902010-07-03 02:29:5220
[email protected]afcd76b2010-07-03 03:14:4421namespace {
22
[email protected]f4192f22010-07-06 16:45:2223// NotificationsPrefsCache wants to be called on the IO thread. This class
24// routes calls to the cache on the IO thread.
25class ThreadProxy : public base::RefCountedThreadSafe<ThreadProxy> {
[email protected]afcd76b2010-07-03 03:14:4426 public:
[email protected]f4192f22010-07-06 16:45:2227 ThreadProxy()
28 : io_event_(false, false),
[email protected]4617b212010-07-23 22:41:4129 ui_event_(false, false),
30 permission_(0) {
[email protected]f4192f22010-07-06 16:45:2231 // The current message loop was already initalized by the test superclass.
32 ui_thread_.reset(
[email protected]9d9a80a2010-10-10 13:18:1033 new BrowserThread(BrowserThread::UI, MessageLoop::current()));
[email protected]f4192f22010-07-06 16:45:2234
35 // Create IO thread, start its message loop.
[email protected]9d9a80a2010-10-10 13:18:1036 io_thread_.reset(new BrowserThread(BrowserThread::IO));
[email protected]f4192f22010-07-06 16:45:2237 io_thread_->Start();
38
39 // Calling PauseIOThread() here isn't safe, because the runnable method
40 // could complete before the constructor is done, deleting |this|.
[email protected]afcd76b2010-07-03 03:14:4441 }
[email protected]f4192f22010-07-06 16:45:2242
43 int CacheHasPermission(NotificationsPrefsCache* cache, const GURL& url) {
[email protected]9d9a80a2010-10-10 13:18:1044 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
45 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
[email protected]0d7e79fa2010-10-08 23:35:4746 NewRunnableMethod(this, &ThreadProxy::CacheHasPermissionIO,
[email protected]8be8294d2010-11-01 16:24:5547 make_scoped_refptr(cache), url));
[email protected]f4192f22010-07-06 16:45:2248 io_event_.Signal();
49 ui_event_.Wait(); // Wait for IO thread to be done.
[email protected]9d9a80a2010-10-10 13:18:1050 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
[email protected]f4192f22010-07-06 16:45:2251 NewRunnableMethod(this, &ThreadProxy::PauseIOThreadIO));
52
53 return permission_;
54 }
55
56 void PauseIOThread() {
[email protected]9d9a80a2010-10-10 13:18:1057 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
[email protected]f4192f22010-07-06 16:45:2258 NewRunnableMethod(this, &ThreadProxy::PauseIOThreadIO));
59 }
60
61 void DrainIOThread() {
[email protected]9d9a80a2010-10-10 13:18:1062 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]f4192f22010-07-06 16:45:2263 io_event_.Signal();
64 io_thread_->Stop();
[email protected]afcd76b2010-07-03 03:14:4465 }
66
67 private:
[email protected]f4192f22010-07-06 16:45:2268 friend class base::RefCountedThreadSafe<ThreadProxy>;
69 ~ThreadProxy() {
70 DrainIOThread();
71 }
72
73 void PauseIOThreadIO() {
[email protected]9d9a80a2010-10-10 13:18:1074 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
[email protected]f4192f22010-07-06 16:45:2275 io_event_.Wait();
76 }
77
78 void CacheHasPermissionIO(NotificationsPrefsCache* cache, const GURL& url) {
[email protected]9d9a80a2010-10-10 13:18:1079 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
[email protected]f4192f22010-07-06 16:45:2280 permission_ = cache->HasPermission(url);
81 ui_event_.Signal();
82 }
83
84 base::WaitableEvent io_event_;
85 base::WaitableEvent ui_event_;
[email protected]9d9a80a2010-10-10 13:18:1086 scoped_ptr<BrowserThread> ui_thread_;
87 scoped_ptr<BrowserThread> io_thread_;
[email protected]f4192f22010-07-06 16:45:2288
89 int permission_;
[email protected]afcd76b2010-07-03 03:14:4490};
91
92
[email protected]78dcb902010-07-03 02:29:5293class DesktopNotificationServiceTest : public RenderViewHostTestHarness {
94 public:
[email protected]f4192f22010-07-06 16:45:2295 DesktopNotificationServiceTest() {
[email protected]78dcb902010-07-03 02:29:5296 }
[email protected]f4192f22010-07-06 16:45:2297
98 virtual void SetUp() {
99 RenderViewHostTestHarness::SetUp();
100 proxy_ = new ThreadProxy;
101 proxy_->PauseIOThread();
102
103 // Creates the service, calls InitPrefs() on it which loads data from the
104 // profile into the cache and then puts the cache in io thread mode.
105 service_ = profile()->GetDesktopNotificationService();
106 cache_ = service_->prefs_cache();
107 }
108
109 virtual void TearDown() {
110 // The io thread's waiting on the io_event_ might hold a ref to |proxy_|,
111 // preventing its destruction. Clear that ref.
112 proxy_->DrainIOThread();
113 RenderViewHostTestHarness::TearDown();
114 }
115
116 DesktopNotificationService* service_;
117 NotificationsPrefsCache* cache_;
118 scoped_refptr<ThreadProxy> proxy_;
[email protected]78dcb902010-07-03 02:29:52119};
120
121TEST_F(DesktopNotificationServiceTest, DefaultContentSettingSentToCache) {
[email protected]78dcb902010-07-03 02:29:52122 // The default pref registered in DesktopNotificationService is "ask",
123 // and that's what sent to the cache.
[email protected]f4192f22010-07-06 16:45:22124 EXPECT_EQ(CONTENT_SETTING_ASK, cache_->CachedDefaultContentSetting());
[email protected]78dcb902010-07-03 02:29:52125
126 // Change the default content setting. This will post a task on the IO thread
127 // to update the cache.
[email protected]f4192f22010-07-06 16:45:22128 service_->SetDefaultContentSetting(CONTENT_SETTING_BLOCK);
[email protected]78dcb902010-07-03 02:29:52129
130 // The updated pref shouldn't be sent to the cache immediately.
[email protected]f4192f22010-07-06 16:45:22131 EXPECT_EQ(CONTENT_SETTING_ASK, cache_->CachedDefaultContentSetting());
[email protected]78dcb902010-07-03 02:29:52132
133 // Run IO thread tasks.
[email protected]f4192f22010-07-06 16:45:22134 proxy_->DrainIOThread();
[email protected]78dcb902010-07-03 02:29:52135
136 // Now that IO thread events have been processed, it should be there.
[email protected]f4192f22010-07-06 16:45:22137 EXPECT_EQ(CONTENT_SETTING_BLOCK, cache_->CachedDefaultContentSetting());
138}
139
[email protected]0c6c4be2011-03-23 12:31:20140TEST_F(DesktopNotificationServiceTest, SettingsForSchemes) {
141 GURL url("file:///html/test.html");
142
143 EXPECT_EQ(CONTENT_SETTING_ASK, cache_->CachedDefaultContentSetting());
144 EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionNotAllowed,
145 proxy_->CacheHasPermission(cache_, url));
146
147 service_->GrantPermission(url);
148 EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionAllowed,
149 proxy_->CacheHasPermission(cache_, url));
150
151 service_->DenyPermission(url);
152 EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionDenied,
153 proxy_->CacheHasPermission(cache_, url));
154
155 GURL https_url("https://testurl");
156 GURL http_url("http://testurl");
157 EXPECT_EQ(CONTENT_SETTING_ASK, cache_->CachedDefaultContentSetting());
158 EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionNotAllowed,
159 proxy_->CacheHasPermission(cache_, http_url));
160 EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionNotAllowed,
161 proxy_->CacheHasPermission(cache_, https_url));
162
163 service_->GrantPermission(https_url);
164 EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionAllowed,
165 proxy_->CacheHasPermission(cache_, https_url));
166 EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionNotAllowed,
167 proxy_->CacheHasPermission(cache_, http_url));
168
169 service_->DenyPermission(http_url);
170 EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionDenied,
171 proxy_->CacheHasPermission(cache_, http_url));
172 EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionAllowed,
173 proxy_->CacheHasPermission(cache_, https_url));
174}
175
[email protected]f4192f22010-07-06 16:45:22176TEST_F(DesktopNotificationServiceTest, GrantPermissionSentToCache) {
177 GURL url("http://allowed.com");
178 EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionNotAllowed,
179 proxy_->CacheHasPermission(cache_, url));
180
181 service_->GrantPermission(url);
182
183 EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionAllowed,
184 proxy_->CacheHasPermission(cache_, url));
185}
186
187TEST_F(DesktopNotificationServiceTest, DenyPermissionSentToCache) {
188 GURL url("http://denied.com");
189 EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionNotAllowed,
190 proxy_->CacheHasPermission(cache_, url));
191
192 service_->DenyPermission(url);
193
194 EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionDenied,
195 proxy_->CacheHasPermission(cache_, url));
196}
197
198TEST_F(DesktopNotificationServiceTest, PrefChangesSentToCache) {
199 PrefService* prefs = profile()->GetPrefs();
200
201 ListValue* allowed_sites =
202 prefs->GetMutableList(prefs::kDesktopNotificationAllowedOrigins);
203 {
204 allowed_sites->Append(new StringValue(GURL("http://allowed.com").spec()));
[email protected]f89ee342011-03-07 09:28:27205 ScopedUserPrefUpdate updateAllowed(
[email protected]f4192f22010-07-06 16:45:22206 prefs, prefs::kDesktopNotificationAllowedOrigins);
207 }
208
209 ListValue* denied_sites =
210 prefs->GetMutableList(prefs::kDesktopNotificationDeniedOrigins);
211 {
212 denied_sites->Append(new StringValue(GURL("http://denied.com").spec()));
[email protected]f89ee342011-03-07 09:28:27213 ScopedUserPrefUpdate updateDenied(
[email protected]f4192f22010-07-06 16:45:22214 prefs, prefs::kDesktopNotificationDeniedOrigins);
215 }
216
217 EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionAllowed,
218 proxy_->CacheHasPermission(cache_, GURL("http://allowed.com")));
219 EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionDenied,
220 proxy_->CacheHasPermission(cache_, GURL("http://denied.com")));
221}
222
223TEST_F(DesktopNotificationServiceTest, GetAllowedOrigins) {
224 service_->GrantPermission(GURL("http://allowed2.com"));
225 service_->GrantPermission(GURL("http://allowed.com"));
226
227 std::vector<GURL> allowed_origins(service_->GetAllowedOrigins());
228 ASSERT_EQ(2u, allowed_origins.size());
229 EXPECT_EQ(GURL("http://allowed2.com"), allowed_origins[0]);
230 EXPECT_EQ(GURL("http://allowed.com"), allowed_origins[1]);
231}
232
233TEST_F(DesktopNotificationServiceTest, GetBlockedOrigins) {
234 service_->DenyPermission(GURL("http://denied2.com"));
235 service_->DenyPermission(GURL("http://denied.com"));
236
237 std::vector<GURL> denied_origins(service_->GetBlockedOrigins());
238 ASSERT_EQ(2u, denied_origins.size());
239 EXPECT_EQ(GURL("http://denied2.com"), denied_origins[0]);
240 EXPECT_EQ(GURL("http://denied.com"), denied_origins[1]);
241}
242
243TEST_F(DesktopNotificationServiceTest, ResetAllSentToCache) {
244 GURL allowed_url("http://allowed.com");
245 service_->GrantPermission(allowed_url);
246 EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionAllowed,
247 proxy_->CacheHasPermission(cache_, allowed_url));
248 GURL denied_url("http://denied.com");
249 service_->DenyPermission(denied_url);
250 EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionDenied,
251 proxy_->CacheHasPermission(cache_, denied_url));
252
253 service_->ResetAllOrigins();
254
255 EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionNotAllowed,
256 proxy_->CacheHasPermission(cache_, allowed_url));
257 EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionNotAllowed,
258 proxy_->CacheHasPermission(cache_, denied_url));
259}
260
261TEST_F(DesktopNotificationServiceTest, ResetAllowedSentToCache) {
262 GURL allowed_url("http://allowed.com");
263 service_->GrantPermission(allowed_url);
264 EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionAllowed,
265 proxy_->CacheHasPermission(cache_, allowed_url));
266
267 service_->ResetAllowedOrigin(allowed_url);
268
269 EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionNotAllowed,
270 proxy_->CacheHasPermission(cache_, allowed_url));
271}
272
273TEST_F(DesktopNotificationServiceTest, ResetBlockedSentToCache) {
274 GURL denied_url("http://denied.com");
275 service_->DenyPermission(denied_url);
276 EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionDenied,
277 proxy_->CacheHasPermission(cache_, denied_url));
278
279 service_->ResetBlockedOrigin(denied_url);
280
281 EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionNotAllowed,
282 proxy_->CacheHasPermission(cache_, denied_url));
[email protected]78dcb902010-07-03 02:29:52283}
284
[email protected]afcd76b2010-07-03 03:14:44285} // namespace