OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 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/services/gcm/push_messaging_permission_context.h" |
| 6 #include "chrome/test/base/testing_profile.h" |
| 7 #include "components/content_settings/core/browser/host_content_settings_map.h" |
| 8 #include "components/content_settings/core/common/content_settings.h" |
| 9 #include "components/content_settings/core/common/content_settings_types.h" |
| 10 #include "components/content_settings/core/common/permission_request_id.h" |
| 11 #include "content/public/test/test_browser_thread_bundle.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 |
| 14 const char kEmbedder[] = "https://example.org"; |
| 15 |
| 16 namespace gcm { |
| 17 |
| 18 class TestPushMessagingPermissionContext |
| 19 : public PushMessagingPermissionContext { |
| 20 public: |
| 21 explicit TestPushMessagingPermissionContext(Profile* profile) |
| 22 : PushMessagingPermissionContext(profile), |
| 23 was_persisted_(false), |
| 24 permission_granted_(false) {} |
| 25 |
| 26 bool was_persisted() const { return was_persisted_; } |
| 27 bool was_granted() const { return permission_granted_; } |
| 28 |
| 29 // PushMessagingPermissionContext: |
| 30 void DecidePermission(content::WebContents* web_contents, |
| 31 const PermissionRequestID& id, |
| 32 const GURL& requesting_origin, |
| 33 const GURL& embedder_origin, |
| 34 bool user_gesture, |
| 35 const BrowserPermissionCallback& callback) override { |
| 36 PushMessagingPermissionContext::DecidePermission( |
| 37 web_contents, id, requesting_origin, embedder_origin, user_gesture, |
| 38 callback); |
| 39 } |
| 40 |
| 41 private: |
| 42 // PushMessagingPermissionContext: |
| 43 void NotifyPermissionSet(const PermissionRequestID& id, |
| 44 const GURL& requesting_origin, |
| 45 const GURL& embedder_origin, |
| 46 const BrowserPermissionCallback& callback, |
| 47 bool persist, |
| 48 bool allowed) override { |
| 49 was_persisted_ = persist; |
| 50 permission_granted_ = allowed; |
| 51 } |
| 52 |
| 53 bool was_persisted_; |
| 54 bool permission_granted_; |
| 55 }; |
| 56 |
| 57 class PushMessagingPermissionContextTest : public testing::Test { |
| 58 public: |
| 59 PushMessagingPermissionContextTest() {} |
| 60 |
| 61 void SetUp() override { |
| 62 HostContentSettingsMap* host_content_settings_map = |
| 63 profile_.GetHostContentSettingsMap(); |
| 64 host_content_settings_map->SetDefaultContentSetting( |
| 65 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, CONTENT_SETTING_ASK); |
| 66 host_content_settings_map->SetDefaultContentSetting( |
| 67 CONTENT_SETTINGS_TYPE_PUSH_MESSAGING, CONTENT_SETTING_ASK); |
| 68 } |
| 69 |
| 70 protected: |
| 71 void SetContentSetting(ContentSettingsType setting, ContentSetting value) { |
| 72 ContentSettingsPattern pattern = |
| 73 ContentSettingsPattern::FromString(kEmbedder); |
| 74 HostContentSettingsMap* host_content_settings_map = |
| 75 profile_.GetHostContentSettingsMap(); |
| 76 host_content_settings_map->SetContentSetting(pattern, pattern, setting, |
| 77 std::string(), value); |
| 78 } |
| 79 |
| 80 TestingProfile profile_; |
| 81 content::TestBrowserThreadBundle thread_bundle_; |
| 82 }; |
| 83 |
| 84 TEST_F(PushMessagingPermissionContextTest, HasPermissionPrompt) { |
| 85 PushMessagingPermissionContext context(&profile_); |
| 86 EXPECT_EQ(CONTENT_SETTING_ASK, |
| 87 context.GetPermissionStatus(GURL(kEmbedder), GURL(kEmbedder))); |
| 88 |
| 89 // Just granting notifications should still prompt |
| 90 SetContentSetting(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, CONTENT_SETTING_ALLOW); |
| 91 |
| 92 EXPECT_EQ(CONTENT_SETTING_ASK, |
| 93 context.GetPermissionStatus(GURL(kEmbedder), GURL(kEmbedder))); |
| 94 |
| 95 // Just granting push should still prompt |
| 96 SetContentSetting(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, CONTENT_SETTING_ASK); |
| 97 SetContentSetting(CONTENT_SETTINGS_TYPE_PUSH_MESSAGING, |
| 98 CONTENT_SETTING_ALLOW); |
| 99 |
| 100 EXPECT_EQ(CONTENT_SETTING_ASK, |
| 101 context.GetPermissionStatus(GURL(kEmbedder), GURL(kEmbedder))); |
| 102 } |
| 103 |
| 104 TEST_F(PushMessagingPermissionContextTest, HasPermissionDeny) { |
| 105 PushMessagingPermissionContext context(&profile_); |
| 106 SetContentSetting(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, CONTENT_SETTING_BLOCK); |
| 107 EXPECT_EQ(CONTENT_SETTING_BLOCK, |
| 108 context.GetPermissionStatus(GURL(kEmbedder), GURL(kEmbedder))); |
| 109 SetContentSetting(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, CONTENT_SETTING_ASK); |
| 110 SetContentSetting(CONTENT_SETTINGS_TYPE_PUSH_MESSAGING, |
| 111 CONTENT_SETTING_BLOCK); |
| 112 EXPECT_EQ(CONTENT_SETTING_BLOCK, |
| 113 context.GetPermissionStatus(GURL(kEmbedder), GURL(kEmbedder))); |
| 114 SetContentSetting(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, CONTENT_SETTING_ALLOW); |
| 115 EXPECT_EQ(CONTENT_SETTING_BLOCK, |
| 116 context.GetPermissionStatus(GURL(kEmbedder), GURL(kEmbedder))); |
| 117 |
| 118 SetContentSetting(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, CONTENT_SETTING_ASK); |
| 119 SetContentSetting(CONTENT_SETTINGS_TYPE_PUSH_MESSAGING, |
| 120 CONTENT_SETTING_BLOCK); |
| 121 EXPECT_EQ(CONTENT_SETTING_BLOCK, |
| 122 context.GetPermissionStatus(GURL(kEmbedder), GURL(kEmbedder))); |
| 123 } |
| 124 |
| 125 TEST_F(PushMessagingPermissionContextTest, HasPermissionAccept) { |
| 126 PushMessagingPermissionContext context(&profile_); |
| 127 SetContentSetting(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, CONTENT_SETTING_ALLOW); |
| 128 SetContentSetting(CONTENT_SETTINGS_TYPE_PUSH_MESSAGING, |
| 129 CONTENT_SETTING_ALLOW); |
| 130 EXPECT_EQ(CONTENT_SETTING_ALLOW, |
| 131 context.GetPermissionStatus(GURL(kEmbedder), GURL(kEmbedder))); |
| 132 } |
| 133 |
| 134 TEST_F(PushMessagingPermissionContextTest, DecidePermission) { |
| 135 TestPushMessagingPermissionContext context(&profile_); |
| 136 PermissionRequestID request_id(-1, -1, -1, GURL(kEmbedder)); |
| 137 BrowserPermissionCallback callback; |
| 138 |
| 139 context.DecidePermission(NULL, request_id, GURL(kEmbedder), GURL(kEmbedder), |
| 140 true, callback); |
| 141 EXPECT_FALSE(context.was_persisted()); |
| 142 EXPECT_FALSE(context.was_granted()); |
| 143 |
| 144 SetContentSetting(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, CONTENT_SETTING_BLOCK); |
| 145 context.DecidePermission(NULL, request_id, GURL(kEmbedder), GURL(kEmbedder), |
| 146 true, callback); |
| 147 EXPECT_FALSE(context.was_persisted()); |
| 148 EXPECT_FALSE(context.was_granted()); |
| 149 SetContentSetting(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, CONTENT_SETTING_ALLOW); |
| 150 SetContentSetting(CONTENT_SETTINGS_TYPE_PUSH_MESSAGING, |
| 151 CONTENT_SETTING_BLOCK); |
| 152 context.DecidePermission(NULL, request_id, GURL(kEmbedder), GURL(kEmbedder), |
| 153 true, callback); |
| 154 EXPECT_FALSE(context.was_persisted()); |
| 155 EXPECT_FALSE(context.was_granted()); |
| 156 SetContentSetting(CONTENT_SETTINGS_TYPE_PUSH_MESSAGING, CONTENT_SETTING_ASK); |
| 157 context.DecidePermission(NULL, request_id, GURL(kEmbedder), GURL(kEmbedder), |
| 158 true, callback); |
| 159 EXPECT_TRUE(context.was_persisted()); |
| 160 EXPECT_TRUE(context.was_granted()); |
| 161 } |
| 162 |
| 163 } // namespace gcm |
OLD | NEW |