desktop_notification_service_unittest.cc revision 731df977c0511bca2206b5f333555b1205ff1f43
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 3// found in the LICENSE file. 4 5#include "chrome/browser/notifications/desktop_notification_service.h" 6 7#include "base/ref_counted.h" 8#include "base/waitable_event.h" 9#include "chrome/browser/notifications/notifications_prefs_cache.h" 10#include "chrome/browser/prefs/pref_service.h" 11#include "chrome/browser/prefs/scoped_pref_update.h" 12#include "chrome/browser/renderer_host/test/test_render_view_host.h" 13#include "chrome/common/pref_names.h" 14#include "chrome/test/testing_profile.h" 15#include "grit/generated_resources.h" 16#include "testing/gtest/include/gtest/gtest.h" 17#include "third_party/WebKit/WebKit/chromium/public/WebNotificationPresenter.h" 18 19namespace { 20 21// NotificationsPrefsCache wants to be called on the IO thread. This class 22// routes calls to the cache on the IO thread. 23class ThreadProxy : public base::RefCountedThreadSafe<ThreadProxy> { 24 public: 25 ThreadProxy() 26 : io_event_(false, false), 27 ui_event_(false, false), 28 permission_(0) { 29 // The current message loop was already initalized by the test superclass. 30 ui_thread_.reset( 31 new BrowserThread(BrowserThread::UI, MessageLoop::current())); 32 33 // Create IO thread, start its message loop. 34 io_thread_.reset(new BrowserThread(BrowserThread::IO)); 35 io_thread_->Start(); 36 37 // Calling PauseIOThread() here isn't safe, because the runnable method 38 // could complete before the constructor is done, deleting |this|. 39 } 40 41 int CacheHasPermission(NotificationsPrefsCache* cache, const GURL& url) { 42 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 43 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, 44 NewRunnableMethod(this, &ThreadProxy::CacheHasPermissionIO, 45 cache, url)); 46 io_event_.Signal(); 47 ui_event_.Wait(); // Wait for IO thread to be done. 48 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, 49 NewRunnableMethod(this, &ThreadProxy::PauseIOThreadIO)); 50 51 return permission_; 52 } 53 54 void PauseIOThread() { 55 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, 56 NewRunnableMethod(this, &ThreadProxy::PauseIOThreadIO)); 57 } 58 59 void DrainIOThread() { 60 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 61 io_event_.Signal(); 62 io_thread_->Stop(); 63 } 64 65 private: 66 friend class base::RefCountedThreadSafe<ThreadProxy>; 67 ~ThreadProxy() { 68 DrainIOThread(); 69 } 70 71 void PauseIOThreadIO() { 72 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 73 io_event_.Wait(); 74 } 75 76 void CacheHasPermissionIO(NotificationsPrefsCache* cache, const GURL& url) { 77 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 78 permission_ = cache->HasPermission(url); 79 ui_event_.Signal(); 80 } 81 82 base::WaitableEvent io_event_; 83 base::WaitableEvent ui_event_; 84 scoped_ptr<BrowserThread> ui_thread_; 85 scoped_ptr<BrowserThread> io_thread_; 86 87 int permission_; 88}; 89 90 91class DesktopNotificationServiceTest : public RenderViewHostTestHarness { 92 public: 93 DesktopNotificationServiceTest() { 94 } 95 96 virtual void SetUp() { 97 RenderViewHostTestHarness::SetUp(); 98 proxy_ = new ThreadProxy; 99 proxy_->PauseIOThread(); 100 101 // Creates the service, calls InitPrefs() on it which loads data from the 102 // profile into the cache and then puts the cache in io thread mode. 103 service_ = profile()->GetDesktopNotificationService(); 104 cache_ = service_->prefs_cache(); 105 } 106 107 virtual void TearDown() { 108 // The io thread's waiting on the io_event_ might hold a ref to |proxy_|, 109 // preventing its destruction. Clear that ref. 110 proxy_->DrainIOThread(); 111 RenderViewHostTestHarness::TearDown(); 112 } 113 114 DesktopNotificationService* service_; 115 NotificationsPrefsCache* cache_; 116 scoped_refptr<ThreadProxy> proxy_; 117}; 118 119TEST_F(DesktopNotificationServiceTest, DefaultContentSettingSentToCache) { 120 // The default pref registered in DesktopNotificationService is "ask", 121 // and that's what sent to the cache. 122 EXPECT_EQ(CONTENT_SETTING_ASK, cache_->CachedDefaultContentSetting()); 123 124 // Change the default content setting. This will post a task on the IO thread 125 // to update the cache. 126 service_->SetDefaultContentSetting(CONTENT_SETTING_BLOCK); 127 128 // The updated pref shouldn't be sent to the cache immediately. 129 EXPECT_EQ(CONTENT_SETTING_ASK, cache_->CachedDefaultContentSetting()); 130 131 // Run IO thread tasks. 132 proxy_->DrainIOThread(); 133 134 // Now that IO thread events have been processed, it should be there. 135 EXPECT_EQ(CONTENT_SETTING_BLOCK, cache_->CachedDefaultContentSetting()); 136} 137 138TEST_F(DesktopNotificationServiceTest, GrantPermissionSentToCache) { 139 GURL url("http://allowed.com"); 140 EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionNotAllowed, 141 proxy_->CacheHasPermission(cache_, url)); 142 143 service_->GrantPermission(url); 144 145 EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionAllowed, 146 proxy_->CacheHasPermission(cache_, url)); 147} 148 149TEST_F(DesktopNotificationServiceTest, DenyPermissionSentToCache) { 150 GURL url("http://denied.com"); 151 EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionNotAllowed, 152 proxy_->CacheHasPermission(cache_, url)); 153 154 service_->DenyPermission(url); 155 156 EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionDenied, 157 proxy_->CacheHasPermission(cache_, url)); 158} 159 160TEST_F(DesktopNotificationServiceTest, PrefChangesSentToCache) { 161 PrefService* prefs = profile()->GetPrefs(); 162 163 ListValue* allowed_sites = 164 prefs->GetMutableList(prefs::kDesktopNotificationAllowedOrigins); 165 { 166 allowed_sites->Append(new StringValue(GURL("http://allowed.com").spec())); 167 ScopedPrefUpdate updateAllowed( 168 prefs, prefs::kDesktopNotificationAllowedOrigins); 169 } 170 171 ListValue* denied_sites = 172 prefs->GetMutableList(prefs::kDesktopNotificationDeniedOrigins); 173 { 174 denied_sites->Append(new StringValue(GURL("http://denied.com").spec())); 175 ScopedPrefUpdate updateDenied( 176 prefs, prefs::kDesktopNotificationDeniedOrigins); 177 } 178 179 EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionAllowed, 180 proxy_->CacheHasPermission(cache_, GURL("http://allowed.com"))); 181 EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionDenied, 182 proxy_->CacheHasPermission(cache_, GURL("http://denied.com"))); 183} 184 185TEST_F(DesktopNotificationServiceTest, GetAllowedOrigins) { 186 service_->GrantPermission(GURL("http://allowed2.com")); 187 service_->GrantPermission(GURL("http://allowed.com")); 188 189 std::vector<GURL> allowed_origins(service_->GetAllowedOrigins()); 190 ASSERT_EQ(2u, allowed_origins.size()); 191 EXPECT_EQ(GURL("http://allowed2.com"), allowed_origins[0]); 192 EXPECT_EQ(GURL("http://allowed.com"), allowed_origins[1]); 193} 194 195TEST_F(DesktopNotificationServiceTest, GetBlockedOrigins) { 196 service_->DenyPermission(GURL("http://denied2.com")); 197 service_->DenyPermission(GURL("http://denied.com")); 198 199 std::vector<GURL> denied_origins(service_->GetBlockedOrigins()); 200 ASSERT_EQ(2u, denied_origins.size()); 201 EXPECT_EQ(GURL("http://denied2.com"), denied_origins[0]); 202 EXPECT_EQ(GURL("http://denied.com"), denied_origins[1]); 203} 204 205TEST_F(DesktopNotificationServiceTest, ResetAllSentToCache) { 206 GURL allowed_url("http://allowed.com"); 207 service_->GrantPermission(allowed_url); 208 EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionAllowed, 209 proxy_->CacheHasPermission(cache_, allowed_url)); 210 GURL denied_url("http://denied.com"); 211 service_->DenyPermission(denied_url); 212 EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionDenied, 213 proxy_->CacheHasPermission(cache_, denied_url)); 214 215 service_->ResetAllOrigins(); 216 217 EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionNotAllowed, 218 proxy_->CacheHasPermission(cache_, allowed_url)); 219 EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionNotAllowed, 220 proxy_->CacheHasPermission(cache_, denied_url)); 221} 222 223TEST_F(DesktopNotificationServiceTest, ResetAllowedSentToCache) { 224 GURL allowed_url("http://allowed.com"); 225 service_->GrantPermission(allowed_url); 226 EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionAllowed, 227 proxy_->CacheHasPermission(cache_, allowed_url)); 228 229 service_->ResetAllowedOrigin(allowed_url); 230 231 EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionNotAllowed, 232 proxy_->CacheHasPermission(cache_, allowed_url)); 233} 234 235TEST_F(DesktopNotificationServiceTest, ResetBlockedSentToCache) { 236 GURL denied_url("http://denied.com"); 237 service_->DenyPermission(denied_url); 238 EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionDenied, 239 proxy_->CacheHasPermission(cache_, denied_url)); 240 241 service_->ResetBlockedOrigin(denied_url); 242 243 EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionNotAllowed, 244 proxy_->CacheHasPermission(cache_, denied_url)); 245} 246 247} // namespace 248