Index: chrome/browser/notifications/desktop_notification_service_unittest.cc |
=================================================================== |
--- chrome/browser/notifications/desktop_notification_service_unittest.cc (revision 98940) |
+++ chrome/browser/notifications/desktop_notification_service_unittest.cc (working copy) |
@@ -4,27 +4,31 @@ |
#include "chrome/browser/notifications/desktop_notification_service.h" |
-#include "base/memory/scoped_ptr.h" |
+#include "base/memory/ref_counted.h" |
#include "base/message_loop.h" |
#include "base/synchronization/waitable_event.h" |
-#include "chrome/browser/content_settings/host_content_settings_map.h" |
#include "chrome/browser/notifications/desktop_notification_service_factory.h" |
+#include "chrome/browser/notifications/notifications_prefs_cache.h" |
+#include "chrome/browser/prefs/pref_service.h" |
+#include "chrome/browser/prefs/scoped_user_pref_update.h" |
+#include "chrome/common/pref_names.h" |
#include "chrome/test/base/testing_profile.h" |
#include "content/browser/browser_thread.h" |
#include "content/browser/renderer_host/test_render_view_host.h" |
+#include "grit/generated_resources.h" |
#include "testing/gtest/include/gtest/gtest.h" |
#include "third_party/WebKit/Source/WebKit/chromium/public/WebNotificationPresenter.h" |
namespace { |
-// The HasPermission method of the DesktopNotificationService wants to be called |
-// on the IO thread. This class routes calls to the cache on the IO thread. |
+// NotificationsPrefsCache wants to be called on the IO thread. This class |
+// routes calls to the cache on the IO thread. |
class ThreadProxy : public base::RefCountedThreadSafe<ThreadProxy> { |
public: |
ThreadProxy() |
: io_event_(false, false), |
ui_event_(false, false), |
- permission_(WebKit::WebNotificationPresenter::PermissionAllowed) { |
+ permission_(0) { |
// The current message loop was already initalized by the test superclass. |
ui_thread_.reset( |
new BrowserThread(BrowserThread::UI, MessageLoop::current())); |
@@ -37,15 +41,11 @@ |
// could complete before the constructor is done, deleting |this|. |
} |
- // Call the HasPermission method of the DesktopNotificationService on the IO |
- // thread and returns the permission setting. |
- WebKit::WebNotificationPresenter::Permission ServiceHasPermission( |
- DesktopNotificationService* service, |
- const GURL& url) { |
+ int CacheHasPermission(NotificationsPrefsCache* cache, const GURL& url) { |
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
- NewRunnableMethod(this, &ThreadProxy::ServiceHasPermissionIO, |
- service, url)); |
+ NewRunnableMethod(this, &ThreadProxy::CacheHasPermissionIO, |
+ make_scoped_refptr(cache), url)); |
io_event_.Signal(); |
ui_event_.Wait(); // Wait for IO thread to be done. |
BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
@@ -76,10 +76,9 @@ |
io_event_.Wait(); |
} |
- void ServiceHasPermissionIO(DesktopNotificationService* service, |
- const GURL& url) { |
+ void CacheHasPermissionIO(NotificationsPrefsCache* cache, const GURL& url) { |
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
- permission_ = service->HasPermission(url); |
+ permission_ = cache->HasPermission(url); |
ui_event_.Signal(); |
} |
@@ -88,10 +87,9 @@ |
scoped_ptr<BrowserThread> ui_thread_; |
scoped_ptr<BrowserThread> io_thread_; |
- WebKit::WebNotificationPresenter::Permission permission_; |
+ int permission_; |
}; |
-} // namespace |
class DesktopNotificationServiceTest : public RenderViewHostTestHarness { |
public: |
@@ -103,8 +101,10 @@ |
proxy_ = new ThreadProxy; |
proxy_->PauseIOThread(); |
- // Creates the destop notification service. |
+ // Creates the service, calls InitPrefs() on it which loads data from the |
+ // profile into the cache and then puts the cache in io thread mode. |
service_ = DesktopNotificationServiceFactory::GetForProfile(profile()); |
+ cache_ = service_->prefs_cache(); |
} |
virtual void TearDown() { |
@@ -115,75 +115,170 @@ |
} |
DesktopNotificationService* service_; |
+ NotificationsPrefsCache* cache_; |
scoped_refptr<ThreadProxy> proxy_; |
}; |
+TEST_F(DesktopNotificationServiceTest, DefaultContentSettingSentToCache) { |
+ // The default pref registered in DesktopNotificationService is "ask", |
+ // and that's what sent to the cache. |
+ EXPECT_EQ(CONTENT_SETTING_ASK, cache_->CachedDefaultContentSetting()); |
+ |
+ // Change the default content setting. This will post a task on the IO thread |
+ // to update the cache. |
+ service_->SetDefaultContentSetting(CONTENT_SETTING_BLOCK); |
+ |
+ // The updated pref shouldn't be sent to the cache immediately. |
+ EXPECT_EQ(CONTENT_SETTING_ASK, cache_->CachedDefaultContentSetting()); |
+ |
+ // Run IO thread tasks. |
+ proxy_->DrainIOThread(); |
+ |
+ // Now that IO thread events have been processed, it should be there. |
+ EXPECT_EQ(CONTENT_SETTING_BLOCK, cache_->CachedDefaultContentSetting()); |
+} |
+ |
TEST_F(DesktopNotificationServiceTest, SettingsForSchemes) { |
GURL url("file:///html/test.html"); |
- EXPECT_EQ(CONTENT_SETTING_ASK, |
- service_->GetDefaultContentSetting()); |
+ EXPECT_EQ(CONTENT_SETTING_ASK, cache_->CachedDefaultContentSetting()); |
EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionNotAllowed, |
- proxy_->ServiceHasPermission(service_, url)); |
+ proxy_->CacheHasPermission(cache_, url)); |
service_->GrantPermission(url); |
EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionAllowed, |
- proxy_->ServiceHasPermission(service_, url)); |
+ proxy_->CacheHasPermission(cache_, url)); |
service_->DenyPermission(url); |
EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionDenied, |
- proxy_->ServiceHasPermission(service_, url)); |
+ proxy_->CacheHasPermission(cache_, url)); |
GURL https_url("https://testurl"); |
GURL http_url("http://testurl"); |
- EXPECT_EQ(CONTENT_SETTING_ASK, |
- service_->GetDefaultContentSetting()); |
+ EXPECT_EQ(CONTENT_SETTING_ASK, cache_->CachedDefaultContentSetting()); |
EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionNotAllowed, |
- proxy_->ServiceHasPermission(service_, http_url)); |
+ proxy_->CacheHasPermission(cache_, http_url)); |
EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionNotAllowed, |
- proxy_->ServiceHasPermission(service_, https_url)); |
+ proxy_->CacheHasPermission(cache_, https_url)); |
service_->GrantPermission(https_url); |
+ EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionAllowed, |
+ proxy_->CacheHasPermission(cache_, https_url)); |
EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionNotAllowed, |
- proxy_->ServiceHasPermission(service_, http_url)); |
- EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionAllowed, |
- proxy_->ServiceHasPermission(service_, https_url)); |
+ proxy_->CacheHasPermission(cache_, http_url)); |
service_->DenyPermission(http_url); |
EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionDenied, |
- proxy_->ServiceHasPermission(service_, http_url)); |
+ proxy_->CacheHasPermission(cache_, http_url)); |
EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionAllowed, |
- proxy_->ServiceHasPermission(service_, https_url)); |
+ proxy_->CacheHasPermission(cache_, https_url)); |
} |
-TEST_F(DesktopNotificationServiceTest, GetNotificationsSettings) { |
+TEST_F(DesktopNotificationServiceTest, GrantPermissionSentToCache) { |
+ GURL url("http://allowed.com"); |
+ EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionNotAllowed, |
+ proxy_->CacheHasPermission(cache_, url)); |
+ |
+ service_->GrantPermission(url); |
+ |
+ EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionAllowed, |
+ proxy_->CacheHasPermission(cache_, url)); |
+} |
+ |
+TEST_F(DesktopNotificationServiceTest, DenyPermissionSentToCache) { |
+ GURL url("http://denied.com"); |
+ EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionNotAllowed, |
+ proxy_->CacheHasPermission(cache_, url)); |
+ |
+ service_->DenyPermission(url); |
+ |
+ EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionDenied, |
+ proxy_->CacheHasPermission(cache_, url)); |
+} |
+ |
+TEST_F(DesktopNotificationServiceTest, PrefChangesSentToCache) { |
+ PrefService* prefs = profile()->GetPrefs(); |
+ |
+ { |
+ ListPrefUpdate update_allowed_origins( |
+ prefs, prefs::kDesktopNotificationAllowedOrigins); |
+ ListValue* allowed_origins = update_allowed_origins.Get(); |
+ allowed_origins->Append(new StringValue(GURL("http://allowed.com").spec())); |
+ } |
+ |
+ { |
+ ListPrefUpdate update_denied_origins( |
+ prefs, prefs::kDesktopNotificationDeniedOrigins); |
+ ListValue* denied_origins = update_denied_origins.Get(); |
+ denied_origins->Append(new StringValue(GURL("http://denied.com").spec())); |
+ } |
+ |
+ EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionAllowed, |
+ proxy_->CacheHasPermission(cache_, GURL("http://allowed.com"))); |
+ EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionDenied, |
+ proxy_->CacheHasPermission(cache_, GURL("http://denied.com"))); |
+} |
+ |
+TEST_F(DesktopNotificationServiceTest, GetAllowedOrigins) { |
service_->GrantPermission(GURL("http://allowed2.com")); |
service_->GrantPermission(GURL("http://allowed.com")); |
+ |
+ std::vector<GURL> allowed_origins(service_->GetAllowedOrigins()); |
+ ASSERT_EQ(2u, allowed_origins.size()); |
+ EXPECT_EQ(GURL("http://allowed2.com"), allowed_origins[0]); |
+ EXPECT_EQ(GURL("http://allowed.com"), allowed_origins[1]); |
+} |
+ |
+TEST_F(DesktopNotificationServiceTest, GetBlockedOrigins) { |
service_->DenyPermission(GURL("http://denied2.com")); |
service_->DenyPermission(GURL("http://denied.com")); |
- HostContentSettingsMap::SettingsForOneType settings; |
- service_->GetNotificationsSettings(&settings); |
- ASSERT_EQ(4u, settings.size()); |
+ std::vector<GURL> denied_origins(service_->GetBlockedOrigins()); |
+ ASSERT_EQ(2u, denied_origins.size()); |
+ EXPECT_EQ(GURL("http://denied2.com"), denied_origins[0]); |
+ EXPECT_EQ(GURL("http://denied.com"), denied_origins[1]); |
+} |
- EXPECT_EQ(ContentSettingsPattern::FromURLNoWildcard( |
- GURL("http://allowed.com")), |
- settings[0].a); |
- EXPECT_EQ(CONTENT_SETTING_ALLOW, |
- settings[0].c); |
- EXPECT_EQ(ContentSettingsPattern::FromURLNoWildcard( |
- GURL("http://allowed2.com")), |
- settings[1].a); |
- EXPECT_EQ(CONTENT_SETTING_ALLOW, |
- settings[1].c); |
- EXPECT_EQ(ContentSettingsPattern::FromURLNoWildcard( |
- GURL("http://denied.com")), |
- settings[2].a); |
- EXPECT_EQ(CONTENT_SETTING_BLOCK, |
- settings[2].c); |
- EXPECT_EQ(ContentSettingsPattern::FromURLNoWildcard( |
- GURL("http://denied2.com")), |
- settings[3].a); |
- EXPECT_EQ(CONTENT_SETTING_BLOCK, |
- settings[3].c); |
+TEST_F(DesktopNotificationServiceTest, ResetAllSentToCache) { |
+ GURL allowed_url("http://allowed.com"); |
+ service_->GrantPermission(allowed_url); |
+ EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionAllowed, |
+ proxy_->CacheHasPermission(cache_, allowed_url)); |
+ GURL denied_url("http://denied.com"); |
+ service_->DenyPermission(denied_url); |
+ EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionDenied, |
+ proxy_->CacheHasPermission(cache_, denied_url)); |
+ |
+ service_->ResetAllOrigins(); |
+ |
+ EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionNotAllowed, |
+ proxy_->CacheHasPermission(cache_, allowed_url)); |
+ EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionNotAllowed, |
+ proxy_->CacheHasPermission(cache_, denied_url)); |
} |
+ |
+TEST_F(DesktopNotificationServiceTest, ResetAllowedSentToCache) { |
+ GURL allowed_url("http://allowed.com"); |
+ service_->GrantPermission(allowed_url); |
+ EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionAllowed, |
+ proxy_->CacheHasPermission(cache_, allowed_url)); |
+ |
+ service_->ResetAllowedOrigin(allowed_url); |
+ |
+ EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionNotAllowed, |
+ proxy_->CacheHasPermission(cache_, allowed_url)); |
+} |
+ |
+TEST_F(DesktopNotificationServiceTest, ResetBlockedSentToCache) { |
+ GURL denied_url("http://denied.com"); |
+ service_->DenyPermission(denied_url); |
+ EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionDenied, |
+ proxy_->CacheHasPermission(cache_, denied_url)); |
+ |
+ service_->ResetBlockedOrigin(denied_url); |
+ |
+ EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionNotAllowed, |
+ proxy_->CacheHasPermission(cache_, denied_url)); |
+} |
+ |
+} // namespace |