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