Chromium Code Reviews| 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.h" | |
| 12 #include "testing/gtest/include/gtest/gtest.h" | |
| 13 | |
| 14 namespace { | |
| 15 static std::string EMBEDDER = "https://example.org"; | |
| 16 static GURL EMBEDDER_URL(EMBEDDER); | |
| 17 } | |
| 18 | |
| 19 namespace gcm { | |
| 20 | |
| 21 class TestPushMessagingPermissionContext | |
| 22 : public PushMessagingPermissionContext { | |
| 23 public: | |
| 24 explicit TestPushMessagingPermissionContext(Profile* profile) | |
| 25 : PushMessagingPermissionContext(profile), | |
| 26 was_persisted_(false), | |
| 27 permission_granted_(false) {} | |
| 28 | |
| 29 bool WasPersisted() { return was_persisted_; } | |
| 30 bool PermissionGranted() { return permission_granted_; } | |
| 31 | |
| 32 void DecidePermission(content::WebContents* web_contents, | |
|
johnme
2014/11/13 18:50:06
Nit: Similarly "// PushMessagingPermissionContext
Miguel Garcia
2014/11/14 11:34:36
Done.
| |
| 33 const PermissionRequestID& id, | |
| 34 const GURL& requesting_origin, | |
| 35 const GURL& embedder_origin, | |
| 36 bool user_gesture, | |
| 37 const BrowserPermissionCallback& callback) override { | |
| 38 PushMessagingPermissionContext::DecidePermission( | |
| 39 web_contents, id, requesting_origin, embedder_origin, user_gesture, | |
| 40 callback); | |
| 41 } | |
| 42 | |
| 43 private: | |
| 44 void NotifyPermissionSet(const PermissionRequestID& id, | |
|
johnme
2014/11/13 18:50:06
Nit: Ditto "// PermissionContextBase implementatio
Miguel Garcia
2014/11/14 11:34:36
I've gone with // PushMessagingPermissionContext:
| |
| 45 const GURL& requesting_origin, | |
| 46 const GURL& embedder_origin, | |
| 47 const BrowserPermissionCallback& callback, | |
| 48 bool persist, | |
| 49 bool allowed) override { | |
| 50 was_persisted_ = persist; | |
| 51 permission_granted_ = allowed; | |
| 52 } | |
| 53 | |
| 54 bool was_persisted_; | |
| 55 bool permission_granted_; | |
| 56 }; | |
| 57 | |
| 58 class PushMessagingPermissionContextTest : public testing::Test { | |
| 59 public: | |
| 60 PushMessagingPermissionContextTest() | |
| 61 : ui_thread_(content::BrowserThread::UI, &message_loop_) {} | |
| 62 | |
| 63 protected: | |
| 64 void SetContentSetting(ContentSettingsType setting, ContentSetting value) { | |
| 65 ContentSettingsPattern pattern = | |
| 66 ContentSettingsPattern::FromString(EMBEDDER); | |
| 67 HostContentSettingsMap* host_content_settings_map = | |
| 68 profile_.GetHostContentSettingsMap(); | |
| 69 host_content_settings_map->SetContentSetting(pattern, pattern, setting, | |
| 70 std::string(), value); | |
| 71 } | |
| 72 TestingProfile profile_; | |
| 73 base::MessageLoop message_loop_; | |
| 74 content::TestBrowserThread ui_thread_; | |
| 75 | |
| 76 private: | |
| 77 virtual void SetUp() { | |
| 78 HostContentSettingsMap* host_content_settings_map = | |
| 79 profile_.GetHostContentSettingsMap(); | |
| 80 host_content_settings_map->SetDefaultContentSetting( | |
| 81 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, CONTENT_SETTING_ASK); | |
| 82 host_content_settings_map->SetDefaultContentSetting( | |
| 83 CONTENT_SETTINGS_TYPE_PUSH_MESSAGING, CONTENT_SETTING_ASK); | |
| 84 } | |
| 85 }; | |
| 86 | |
| 87 TEST_F(PushMessagingPermissionContextTest, HasPermissionPrompt) { | |
| 88 PushMessagingPermissionContext context(&profile_); | |
| 89 EXPECT_EQ(CONTENT_SETTING_ASK, | |
| 90 context.GetPermissionStatus(EMBEDDER_URL, EMBEDDER_URL)); | |
| 91 | |
| 92 // Just granting notifications should still prompt | |
| 93 SetContentSetting(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, CONTENT_SETTING_ALLOW); | |
| 94 | |
| 95 EXPECT_EQ(CONTENT_SETTING_ASK, | |
| 96 context.GetPermissionStatus(EMBEDDER_URL, EMBEDDER_URL)); | |
| 97 | |
| 98 // Just granting push should still prompt | |
| 99 SetContentSetting(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, CONTENT_SETTING_ASK); | |
| 100 SetContentSetting(CONTENT_SETTINGS_TYPE_PUSH_MESSAGING, | |
| 101 CONTENT_SETTING_ALLOW); | |
| 102 | |
| 103 EXPECT_EQ(CONTENT_SETTING_ASK, | |
| 104 context.GetPermissionStatus(EMBEDDER_URL, EMBEDDER_URL)); | |
| 105 } | |
| 106 | |
| 107 TEST_F(PushMessagingPermissionContextTest, HasPermissionDeny) { | |
| 108 PushMessagingPermissionContext context(&profile_); | |
| 109 SetContentSetting(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, CONTENT_SETTING_BLOCK); | |
| 110 EXPECT_EQ(CONTENT_SETTING_BLOCK, | |
| 111 context.GetPermissionStatus(EMBEDDER_URL, EMBEDDER_URL)); | |
| 112 SetContentSetting(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, CONTENT_SETTING_ASK); | |
| 113 SetContentSetting(CONTENT_SETTINGS_TYPE_PUSH_MESSAGING, | |
| 114 CONTENT_SETTING_BLOCK); | |
| 115 EXPECT_EQ(CONTENT_SETTING_BLOCK, | |
| 116 context.GetPermissionStatus(EMBEDDER_URL, EMBEDDER_URL)); | |
| 117 SetContentSetting(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, CONTENT_SETTING_ALLOW); | |
| 118 EXPECT_EQ(CONTENT_SETTING_BLOCK, | |
| 119 context.GetPermissionStatus(EMBEDDER_URL, EMBEDDER_URL)); | |
| 120 | |
| 121 SetContentSetting(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, CONTENT_SETTING_ASK); | |
| 122 SetContentSetting(CONTENT_SETTINGS_TYPE_PUSH_MESSAGING, | |
| 123 CONTENT_SETTING_BLOCK); | |
| 124 EXPECT_EQ(CONTENT_SETTING_BLOCK, | |
| 125 context.GetPermissionStatus(EMBEDDER_URL, EMBEDDER_URL)); | |
| 126 SetContentSetting(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, CONTENT_SETTING_ALLOW); | |
| 127 SetContentSetting(CONTENT_SETTINGS_TYPE_PUSH_MESSAGING, | |
| 128 CONTENT_SETTING_BLOCK); | |
| 129 EXPECT_EQ(CONTENT_SETTING_BLOCK, | |
| 130 context.GetPermissionStatus(EMBEDDER_URL, EMBEDDER_URL)); | |
| 131 } | |
| 132 | |
| 133 TEST_F(PushMessagingPermissionContextTest, HasPermissionAccept) { | |
| 134 PushMessagingPermissionContext context(&profile_); | |
| 135 SetContentSetting(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, CONTENT_SETTING_ALLOW); | |
| 136 SetContentSetting(CONTENT_SETTINGS_TYPE_PUSH_MESSAGING, | |
| 137 CONTENT_SETTING_ALLOW); | |
| 138 EXPECT_EQ(CONTENT_SETTING_ALLOW, | |
| 139 context.GetPermissionStatus(EMBEDDER_URL, EMBEDDER_URL)); | |
| 140 } | |
| 141 | |
| 142 TEST_F(PushMessagingPermissionContextTest, DecidePermission) { | |
| 143 TestPushMessagingPermissionContext context(&profile_); | |
| 144 PermissionRequestID request_id(-1, -1, -1, EMBEDDER_URL); | |
| 145 BrowserPermissionCallback callback; | |
| 146 | |
| 147 context.DecidePermission(NULL, request_id, EMBEDDER_URL, EMBEDDER_URL, true, | |
| 148 callback); | |
| 149 EXPECT_EQ(false, context.WasPersisted()); | |
| 150 EXPECT_EQ(false, context.PermissionGranted()); | |
| 151 | |
| 152 SetContentSetting(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, CONTENT_SETTING_BLOCK); | |
| 153 context.DecidePermission(NULL, request_id, EMBEDDER_URL, EMBEDDER_URL, true, | |
| 154 callback); | |
| 155 EXPECT_EQ(false, context.WasPersisted()); | |
| 156 EXPECT_EQ(false, context.PermissionGranted()); | |
| 157 SetContentSetting(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, CONTENT_SETTING_ALLOW); | |
| 158 SetContentSetting(CONTENT_SETTINGS_TYPE_PUSH_MESSAGING, | |
| 159 CONTENT_SETTING_BLOCK); | |
| 160 context.DecidePermission(NULL, request_id, EMBEDDER_URL, EMBEDDER_URL, true, | |
| 161 callback); | |
| 162 EXPECT_EQ(false, context.WasPersisted()); | |
| 163 EXPECT_EQ(false, context.PermissionGranted()); | |
| 164 SetContentSetting(CONTENT_SETTINGS_TYPE_PUSH_MESSAGING, CONTENT_SETTING_ASK); | |
| 165 context.DecidePermission(NULL, request_id, EMBEDDER_URL, EMBEDDER_URL, true, | |
| 166 callback); | |
| 167 EXPECT_EQ(true, context.WasPersisted()); | |
| 168 EXPECT_EQ(true, context.PermissionGranted()); | |
| 169 } | |
| 170 | |
| 171 } // namespace gcm | |
| OLD | NEW |