Home | History | Annotate | Download | only in notifications
      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
      3 // found in the LICENSE file.
      4 
      5 #include "chrome/browser/notifications/desktop_notification_service.h"
      6 
      7 #include "base/bind.h"
      8 #include "base/memory/scoped_ptr.h"
      9 #include "base/message_loop/message_loop.h"
     10 #include "base/synchronization/waitable_event.h"
     11 #include "chrome/browser/notifications/desktop_notification_service_factory.h"
     12 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
     13 #include "chrome/test/base/testing_profile.h"
     14 #include "content/public/test/test_browser_thread.h"
     15 #include "testing/gtest/include/gtest/gtest.h"
     16 #include "third_party/WebKit/public/web/WebNotificationPresenter.h"
     17 
     18 class DesktopNotificationServiceTest : public ChromeRenderViewHostTestHarness {
     19  protected:
     20   virtual void SetUp() {
     21     ChromeRenderViewHostTestHarness::SetUp();
     22 
     23     // Creates the destop notification service.
     24     service_ = DesktopNotificationServiceFactory::GetForProfile(profile());
     25   }
     26 
     27   DesktopNotificationService* service_;
     28 };
     29 
     30 TEST_F(DesktopNotificationServiceTest, SettingsForSchemes) {
     31   GURL url("file:///html/test.html");
     32 
     33   EXPECT_EQ(CONTENT_SETTING_ASK,
     34             service_->GetDefaultContentSetting(NULL));
     35   EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionNotAllowed,
     36             service_->HasPermission(url));
     37 
     38   service_->GrantPermission(url);
     39   EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionAllowed,
     40             service_->HasPermission(url));
     41 
     42   service_->DenyPermission(url);
     43   EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionDenied,
     44             service_->HasPermission(url));
     45 
     46   GURL https_url("https://testurl");
     47   GURL http_url("http://testurl");
     48   EXPECT_EQ(CONTENT_SETTING_ASK,
     49             service_->GetDefaultContentSetting(NULL));
     50   EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionNotAllowed,
     51             service_->HasPermission(http_url));
     52   EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionNotAllowed,
     53             service_->HasPermission(https_url));
     54 
     55   service_->GrantPermission(https_url);
     56   EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionNotAllowed,
     57             service_->HasPermission(http_url));
     58   EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionAllowed,
     59             service_->HasPermission(https_url));
     60 
     61   service_->DenyPermission(http_url);
     62   EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionDenied,
     63             service_->HasPermission(http_url));
     64   EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionAllowed,
     65             service_->HasPermission(https_url));
     66 }
     67 
     68 TEST_F(DesktopNotificationServiceTest, GetNotificationsSettings) {
     69   service_->GrantPermission(GURL("http://allowed2.com"));
     70   service_->GrantPermission(GURL("http://allowed.com"));
     71   service_->DenyPermission(GURL("http://denied2.com"));
     72   service_->DenyPermission(GURL("http://denied.com"));
     73 
     74   ContentSettingsForOneType settings;
     75   service_->GetNotificationsSettings(&settings);
     76   // |settings| contains the default setting and 4 exceptions.
     77   ASSERT_EQ(5u, settings.size());
     78 
     79   EXPECT_EQ(ContentSettingsPattern::FromURLNoWildcard(
     80                 GURL("http://allowed.com")),
     81             settings[0].primary_pattern);
     82   EXPECT_EQ(CONTENT_SETTING_ALLOW,
     83             settings[0].setting);
     84   EXPECT_EQ(ContentSettingsPattern::FromURLNoWildcard(
     85                 GURL("http://allowed2.com")),
     86             settings[1].primary_pattern);
     87   EXPECT_EQ(CONTENT_SETTING_ALLOW,
     88             settings[1].setting);
     89   EXPECT_EQ(ContentSettingsPattern::FromURLNoWildcard(
     90                 GURL("http://denied.com")),
     91             settings[2].primary_pattern);
     92   EXPECT_EQ(CONTENT_SETTING_BLOCK,
     93             settings[2].setting);
     94   EXPECT_EQ(ContentSettingsPattern::FromURLNoWildcard(
     95                 GURL("http://denied2.com")),
     96             settings[3].primary_pattern);
     97   EXPECT_EQ(CONTENT_SETTING_BLOCK,
     98             settings[3].setting);
     99   EXPECT_EQ(ContentSettingsPattern::Wildcard(),
    100             settings[4].primary_pattern);
    101   EXPECT_EQ(CONTENT_SETTING_ASK,
    102             settings[4].setting);
    103 }
    104