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"; | |
|
Bernhard Bauer
2014/11/14 12:31:33
This is going to create a static constructor, whic
Miguel Garcia
2014/11/17 11:43:23
All done, thanks for the long explanation!
On 201
| |
| 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_; } | |
|
Bernhard Bauer
2014/11/14 12:31:34
Make this unix_hacker_style?
Miguel Garcia
2014/11/17 11:43:23
Done.
| |
| 30 bool PermissionGranted() { return permission_granted_; } | |
| 31 | |
| 32 // PushMessagingPermissionContext: | |
| 33 void DecidePermission(content::WebContents* web_contents, | |
| 34 const PermissionRequestID& id, | |
| 35 const GURL& requesting_origin, | |
| 36 const GURL& embedder_origin, | |
| 37 bool user_gesture, | |
| 38 const BrowserPermissionCallback& callback) override { | |
| 39 PushMessagingPermissionContext::DecidePermission( | |
| 40 web_contents, id, requesting_origin, embedder_origin, user_gesture, | |
| 41 callback); | |
| 42 } | |
| 43 | |
| 44 private: | |
| 45 // PushMessagingPermissionContext: | |
| 46 void NotifyPermissionSet(const PermissionRequestID& id, | |
| 47 const GURL& requesting_origin, | |
| 48 const GURL& embedder_origin, | |
| 49 const BrowserPermissionCallback& callback, | |
| 50 bool persist, | |
| 51 bool allowed) override { | |
| 52 was_persisted_ = persist; | |
| 53 permission_granted_ = allowed; | |
| 54 } | |
| 55 | |
| 56 bool was_persisted_; | |
| 57 bool permission_granted_; | |
| 58 }; | |
| 59 | |
| 60 class PushMessagingPermissionContextTest : public testing::Test { | |
| 61 public: | |
| 62 PushMessagingPermissionContextTest() | |
| 63 : ui_thread_(content::BrowserThread::UI, &message_loop_) {} | |
| 64 | |
| 65 protected: | |
| 66 void SetContentSetting(ContentSettingsType setting, ContentSetting value) { | |
| 67 ContentSettingsPattern pattern = | |
| 68 ContentSettingsPattern::FromString(EMBEDDER); | |
| 69 HostContentSettingsMap* host_content_settings_map = | |
| 70 profile_.GetHostContentSettingsMap(); | |
| 71 host_content_settings_map->SetContentSetting(pattern, pattern, setting, | |
| 72 std::string(), value); | |
| 73 } | |
| 74 TestingProfile profile_; | |
| 75 base::MessageLoop message_loop_; | |
|
Bernhard Bauer
2014/11/14 12:31:33
Use a TestBrowserThreadBundle? It's one less line,
| |
| 76 content::TestBrowserThread ui_thread_; | |
| 77 | |
| 78 private: | |
| 79 virtual void SetUp() { | |
|
Bernhard Bauer
2014/11/14 12:31:34
I think this method overrides testing::Test::SetUp
Miguel Garcia
2014/11/17 11:43:22
Done.
| |
| 80 HostContentSettingsMap* host_content_settings_map = | |
| 81 profile_.GetHostContentSettingsMap(); | |
| 82 host_content_settings_map->SetDefaultContentSetting( | |
| 83 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, CONTENT_SETTING_ASK); | |
| 84 host_content_settings_map->SetDefaultContentSetting( | |
| 85 CONTENT_SETTINGS_TYPE_PUSH_MESSAGING, CONTENT_SETTING_ASK); | |
| 86 } | |
| 87 }; | |
| 88 | |
| 89 TEST_F(PushMessagingPermissionContextTest, HasPermissionPrompt) { | |
| 90 PushMessagingPermissionContext context(&profile_); | |
| 91 EXPECT_EQ(CONTENT_SETTING_ASK, | |
| 92 context.GetPermissionStatus(EMBEDDER_URL, EMBEDDER_URL)); | |
| 93 | |
| 94 // Just granting notifications should still prompt | |
| 95 SetContentSetting(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, CONTENT_SETTING_ALLOW); | |
| 96 | |
| 97 EXPECT_EQ(CONTENT_SETTING_ASK, | |
| 98 context.GetPermissionStatus(EMBEDDER_URL, EMBEDDER_URL)); | |
| 99 | |
| 100 // Just granting push should still prompt | |
| 101 SetContentSetting(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, CONTENT_SETTING_ASK); | |
| 102 SetContentSetting(CONTENT_SETTINGS_TYPE_PUSH_MESSAGING, | |
| 103 CONTENT_SETTING_ALLOW); | |
| 104 | |
| 105 EXPECT_EQ(CONTENT_SETTING_ASK, | |
| 106 context.GetPermissionStatus(EMBEDDER_URL, EMBEDDER_URL)); | |
| 107 } | |
| 108 | |
| 109 TEST_F(PushMessagingPermissionContextTest, HasPermissionDeny) { | |
| 110 PushMessagingPermissionContext context(&profile_); | |
| 111 SetContentSetting(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, CONTENT_SETTING_BLOCK); | |
| 112 EXPECT_EQ(CONTENT_SETTING_BLOCK, | |
| 113 context.GetPermissionStatus(EMBEDDER_URL, EMBEDDER_URL)); | |
| 114 SetContentSetting(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, CONTENT_SETTING_ASK); | |
| 115 SetContentSetting(CONTENT_SETTINGS_TYPE_PUSH_MESSAGING, | |
| 116 CONTENT_SETTING_BLOCK); | |
| 117 EXPECT_EQ(CONTENT_SETTING_BLOCK, | |
| 118 context.GetPermissionStatus(EMBEDDER_URL, EMBEDDER_URL)); | |
| 119 SetContentSetting(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, CONTENT_SETTING_ALLOW); | |
| 120 EXPECT_EQ(CONTENT_SETTING_BLOCK, | |
| 121 context.GetPermissionStatus(EMBEDDER_URL, EMBEDDER_URL)); | |
| 122 | |
| 123 SetContentSetting(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, CONTENT_SETTING_ASK); | |
| 124 SetContentSetting(CONTENT_SETTINGS_TYPE_PUSH_MESSAGING, | |
| 125 CONTENT_SETTING_BLOCK); | |
| 126 EXPECT_EQ(CONTENT_SETTING_BLOCK, | |
| 127 context.GetPermissionStatus(EMBEDDER_URL, EMBEDDER_URL)); | |
| 128 SetContentSetting(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, CONTENT_SETTING_ALLOW); | |
| 129 SetContentSetting(CONTENT_SETTINGS_TYPE_PUSH_MESSAGING, | |
| 130 CONTENT_SETTING_BLOCK); | |
| 131 EXPECT_EQ(CONTENT_SETTING_BLOCK, | |
| 132 context.GetPermissionStatus(EMBEDDER_URL, EMBEDDER_URL)); | |
| 133 } | |
| 134 | |
| 135 TEST_F(PushMessagingPermissionContextTest, HasPermissionAccept) { | |
| 136 PushMessagingPermissionContext context(&profile_); | |
| 137 SetContentSetting(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, CONTENT_SETTING_ALLOW); | |
| 138 SetContentSetting(CONTENT_SETTINGS_TYPE_PUSH_MESSAGING, | |
| 139 CONTENT_SETTING_ALLOW); | |
| 140 EXPECT_EQ(CONTENT_SETTING_ALLOW, | |
| 141 context.GetPermissionStatus(EMBEDDER_URL, EMBEDDER_URL)); | |
| 142 } | |
| 143 | |
| 144 TEST_F(PushMessagingPermissionContextTest, DecidePermission) { | |
| 145 TestPushMessagingPermissionContext context(&profile_); | |
| 146 PermissionRequestID request_id(-1, -1, -1, EMBEDDER_URL); | |
| 147 BrowserPermissionCallback callback; | |
| 148 | |
| 149 context.DecidePermission(NULL, request_id, EMBEDDER_URL, EMBEDDER_URL, true, | |
| 150 callback); | |
| 151 EXPECT_EQ(false, context.WasPersisted()); | |
| 152 EXPECT_EQ(false, context.PermissionGranted()); | |
| 153 | |
| 154 SetContentSetting(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, CONTENT_SETTING_BLOCK); | |
| 155 context.DecidePermission(NULL, request_id, EMBEDDER_URL, EMBEDDER_URL, true, | |
| 156 callback); | |
| 157 EXPECT_EQ(false, context.WasPersisted()); | |
| 158 EXPECT_EQ(false, context.PermissionGranted()); | |
| 159 SetContentSetting(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, CONTENT_SETTING_ALLOW); | |
| 160 SetContentSetting(CONTENT_SETTINGS_TYPE_PUSH_MESSAGING, | |
| 161 CONTENT_SETTING_BLOCK); | |
| 162 context.DecidePermission(NULL, request_id, EMBEDDER_URL, EMBEDDER_URL, true, | |
| 163 callback); | |
| 164 EXPECT_EQ(false, context.WasPersisted()); | |
| 165 EXPECT_EQ(false, context.PermissionGranted()); | |
| 166 SetContentSetting(CONTENT_SETTINGS_TYPE_PUSH_MESSAGING, CONTENT_SETTING_ASK); | |
| 167 context.DecidePermission(NULL, request_id, EMBEDDER_URL, EMBEDDER_URL, true, | |
| 168 callback); | |
| 169 EXPECT_EQ(true, context.WasPersisted()); | |
| 170 EXPECT_EQ(true, context.PermissionGranted()); | |
| 171 } | |
| 172 | |
| 173 } // namespace gcm | |
| OLD | NEW |