Index: chrome/browser/notifications/desktop_notification_service_unittest.cc |
diff --git a/chrome/browser/notifications/desktop_notification_service_unittest.cc b/chrome/browser/notifications/desktop_notification_service_unittest.cc |
index 17e8f1cfa634c50cf21b21781c0fa6880d80b797..84d1b06d5930e6540a12bd56917050eb028448e0 100644 |
--- a/chrome/browser/notifications/desktop_notification_service_unittest.cc |
+++ b/chrome/browser/notifications/desktop_notification_service_unittest.cc |
@@ -4,31 +4,27 @@ |
#include "chrome/browser/notifications/desktop_notification_service.h" |
-#include "base/memory/ref_counted.h" |
+#include "base/memory/scoped_ptr.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 { |
-// NotificationsPrefsCache wants to be called on the IO thread. This class |
-// routes calls to the cache on the IO thread. |
+// 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. |
class ThreadProxy : public base::RefCountedThreadSafe<ThreadProxy> { |
public: |
ThreadProxy() |
: io_event_(false, false), |
ui_event_(false, false), |
- permission_(0) { |
+ permission_(WebKit::WebNotificationPresenter::PermissionAllowed) { |
// The current message loop was already initalized by the test superclass. |
ui_thread_.reset( |
new BrowserThread(BrowserThread::UI, MessageLoop::current())); |
@@ -41,11 +37,15 @@ class ThreadProxy : public base::RefCountedThreadSafe<ThreadProxy> { |
// could complete before the constructor is done, deleting |this|. |
} |
- int CacheHasPermission(NotificationsPrefsCache* cache, const GURL& url) { |
+ // 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) { |
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
- NewRunnableMethod(this, &ThreadProxy::CacheHasPermissionIO, |
- make_scoped_refptr(cache), url)); |
+ NewRunnableMethod(this, &ThreadProxy::ServiceHasPermissionIO, |
+ service, url)); |
io_event_.Signal(); |
ui_event_.Wait(); // Wait for IO thread to be done. |
BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
@@ -76,9 +76,10 @@ class ThreadProxy : public base::RefCountedThreadSafe<ThreadProxy> { |
io_event_.Wait(); |
} |
- void CacheHasPermissionIO(NotificationsPrefsCache* cache, const GURL& url) { |
+ void ServiceHasPermissionIO(DesktopNotificationService* service, |
+ const GURL& url) { |
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
- permission_ = cache->HasPermission(url); |
+ permission_ = service->HasPermission(url); |
ui_event_.Signal(); |
} |
@@ -87,9 +88,10 @@ class ThreadProxy : public base::RefCountedThreadSafe<ThreadProxy> { |
scoped_ptr<BrowserThread> ui_thread_; |
scoped_ptr<BrowserThread> io_thread_; |
- int permission_; |
+ WebKit::WebNotificationPresenter::Permission permission_; |
}; |
+} // namespace |
class DesktopNotificationServiceTest : public RenderViewHostTestHarness { |
public: |
@@ -101,10 +103,8 @@ class DesktopNotificationServiceTest : public RenderViewHostTestHarness { |
proxy_ = new ThreadProxy; |
proxy_->PauseIOThread(); |
- // 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. |
+ // Creates the destop notification service. |
service_ = DesktopNotificationServiceFactory::GetForProfile(profile()); |
- cache_ = service_->prefs_cache(); |
} |
virtual void TearDown() { |
@@ -115,170 +115,75 @@ class DesktopNotificationServiceTest : public RenderViewHostTestHarness { |
} |
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, cache_->CachedDefaultContentSetting()); |
+ EXPECT_EQ(CONTENT_SETTING_ASK, |
+ service_->GetDefaultContentSetting()); |
EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionNotAllowed, |
- proxy_->CacheHasPermission(cache_, url)); |
+ proxy_->ServiceHasPermission(service_, url)); |
service_->GrantPermission(url); |
EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionAllowed, |
- proxy_->CacheHasPermission(cache_, url)); |
+ proxy_->ServiceHasPermission(service_, url)); |
service_->DenyPermission(url); |
EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionDenied, |
- proxy_->CacheHasPermission(cache_, url)); |
+ proxy_->ServiceHasPermission(service_, url)); |
GURL https_url("https://testurl"); |
GURL http_url("http://testurl"); |
- EXPECT_EQ(CONTENT_SETTING_ASK, cache_->CachedDefaultContentSetting()); |
+ EXPECT_EQ(CONTENT_SETTING_ASK, |
+ service_->GetDefaultContentSetting()); |
EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionNotAllowed, |
- proxy_->CacheHasPermission(cache_, http_url)); |
+ proxy_->ServiceHasPermission(service_, http_url)); |
EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionNotAllowed, |
- proxy_->CacheHasPermission(cache_, https_url)); |
+ proxy_->ServiceHasPermission(service_, https_url)); |
service_->GrantPermission(https_url); |
- EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionAllowed, |
- proxy_->CacheHasPermission(cache_, https_url)); |
EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionNotAllowed, |
- proxy_->CacheHasPermission(cache_, http_url)); |
+ proxy_->ServiceHasPermission(service_, http_url)); |
+ EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionAllowed, |
+ proxy_->ServiceHasPermission(service_, https_url)); |
service_->DenyPermission(http_url); |
EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionDenied, |
- proxy_->CacheHasPermission(cache_, http_url)); |
+ proxy_->ServiceHasPermission(service_, http_url)); |
EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionAllowed, |
- proxy_->CacheHasPermission(cache_, https_url)); |
+ proxy_->ServiceHasPermission(service_, https_url)); |
} |
-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) { |
+TEST_F(DesktopNotificationServiceTest, GetNotificationsSettings) { |
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")); |
- 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]); |
-} |
- |
-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)); |
+ HostContentSettingsMap::SettingsForOneType settings; |
+ service_->GetNotificationsSettings(&settings); |
+ ASSERT_EQ(4u, settings.size()); |
+ |
+ 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); |
} |
- |
-} // namespace |