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 const GURL kEmbedderUrl(kEmbedder); | |
Bernhard Bauer
2014/11/17 11:53:37
This will still create a static constructor. Sorry
Miguel Garcia
2014/11/17 12:50:42
Of course, sorry about that.
| |
16 | |
17 namespace gcm { | |
18 | |
19 class TestPushMessagingPermissionContext | |
20 : public PushMessagingPermissionContext { | |
21 public: | |
22 explicit TestPushMessagingPermissionContext(Profile* profile) | |
23 : PushMessagingPermissionContext(profile), | |
24 was_persisted_(false), | |
25 permission_granted_(false) {} | |
26 | |
27 bool was_persisted() { return was_persisted_; } | |
28 bool was_granted() { return permission_granted_; } | |
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: | |
43 // PushMessagingPermissionContext: | |
44 void NotifyPermissionSet(const PermissionRequestID& id, | |
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 | |
62 protected: | |
63 void SetContentSetting(ContentSettingsType setting, ContentSetting value) { | |
64 ContentSettingsPattern pattern = | |
65 ContentSettingsPattern::FromString(kEmbedder); | |
66 HostContentSettingsMap* host_content_settings_map = | |
67 profile_.GetHostContentSettingsMap(); | |
68 host_content_settings_map->SetContentSetting(pattern, pattern, setting, | |
69 std::string(), value); | |
70 } | |
71 TestingProfile profile_; | |
72 content::TestBrowserThreadBundle thread_bundle_; | |
73 | |
74 private: | |
75 void SetUp() override { | |
76 HostContentSettingsMap* host_content_settings_map = | |
77 profile_.GetHostContentSettingsMap(); | |
78 host_content_settings_map->SetDefaultContentSetting( | |
79 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, CONTENT_SETTING_ASK); | |
80 host_content_settings_map->SetDefaultContentSetting( | |
81 CONTENT_SETTINGS_TYPE_PUSH_MESSAGING, CONTENT_SETTING_ASK); | |
82 } | |
83 }; | |
84 | |
85 TEST_F(PushMessagingPermissionContextTest, HasPermissionPrompt) { | |
86 PushMessagingPermissionContext context(&profile_); | |
87 EXPECT_EQ(CONTENT_SETTING_ASK, | |
88 context.GetPermissionStatus(kEmbedderUrl, kEmbedderUrl)); | |
89 | |
90 // Just granting notifications should still prompt | |
91 SetContentSetting(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, CONTENT_SETTING_ALLOW); | |
92 | |
93 EXPECT_EQ(CONTENT_SETTING_ASK, | |
94 context.GetPermissionStatus(kEmbedderUrl, kEmbedderUrl)); | |
95 | |
96 // Just granting push should still prompt | |
97 SetContentSetting(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, CONTENT_SETTING_ASK); | |
98 SetContentSetting(CONTENT_SETTINGS_TYPE_PUSH_MESSAGING, | |
99 CONTENT_SETTING_ALLOW); | |
100 | |
101 EXPECT_EQ(CONTENT_SETTING_ASK, | |
102 context.GetPermissionStatus(kEmbedderUrl, kEmbedderUrl)); | |
103 } | |
104 | |
105 TEST_F(PushMessagingPermissionContextTest, HasPermissionDeny) { | |
106 PushMessagingPermissionContext context(&profile_); | |
107 SetContentSetting(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, CONTENT_SETTING_BLOCK); | |
108 EXPECT_EQ(CONTENT_SETTING_BLOCK, | |
109 context.GetPermissionStatus(kEmbedderUrl, kEmbedderUrl)); | |
110 SetContentSetting(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, CONTENT_SETTING_ASK); | |
111 SetContentSetting(CONTENT_SETTINGS_TYPE_PUSH_MESSAGING, | |
112 CONTENT_SETTING_BLOCK); | |
113 EXPECT_EQ(CONTENT_SETTING_BLOCK, | |
114 context.GetPermissionStatus(kEmbedderUrl, kEmbedderUrl)); | |
115 SetContentSetting(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, CONTENT_SETTING_ALLOW); | |
116 EXPECT_EQ(CONTENT_SETTING_BLOCK, | |
117 context.GetPermissionStatus(kEmbedderUrl, kEmbedderUrl)); | |
118 | |
119 SetContentSetting(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, CONTENT_SETTING_ASK); | |
120 SetContentSetting(CONTENT_SETTINGS_TYPE_PUSH_MESSAGING, | |
121 CONTENT_SETTING_BLOCK); | |
122 EXPECT_EQ(CONTENT_SETTING_BLOCK, | |
123 context.GetPermissionStatus(kEmbedderUrl, kEmbedderUrl)); | |
124 SetContentSetting(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, CONTENT_SETTING_ALLOW); | |
125 SetContentSetting(CONTENT_SETTINGS_TYPE_PUSH_MESSAGING, | |
126 CONTENT_SETTING_BLOCK); | |
127 EXPECT_EQ(CONTENT_SETTING_BLOCK, | |
128 context.GetPermissionStatus(kEmbedderUrl, kEmbedderUrl)); | |
129 } | |
130 | |
131 TEST_F(PushMessagingPermissionContextTest, HasPermissionAccept) { | |
132 PushMessagingPermissionContext context(&profile_); | |
133 SetContentSetting(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, CONTENT_SETTING_ALLOW); | |
134 SetContentSetting(CONTENT_SETTINGS_TYPE_PUSH_MESSAGING, | |
135 CONTENT_SETTING_ALLOW); | |
136 EXPECT_EQ(CONTENT_SETTING_ALLOW, | |
137 context.GetPermissionStatus(kEmbedderUrl, kEmbedderUrl)); | |
138 } | |
139 | |
140 TEST_F(PushMessagingPermissionContextTest, DecidePermission) { | |
141 TestPushMessagingPermissionContext context(&profile_); | |
142 PermissionRequestID request_id(-1, -1, -1, kEmbedderUrl); | |
143 BrowserPermissionCallback callback; | |
144 | |
145 context.DecidePermission(NULL, request_id, kEmbedderUrl, kEmbedderUrl, true, | |
146 callback); | |
147 EXPECT_FALSE(context.was_persisted()); | |
148 EXPECT_FALSE(context.was_granted()); | |
149 | |
150 SetContentSetting(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, CONTENT_SETTING_BLOCK); | |
151 context.DecidePermission(NULL, request_id, kEmbedderUrl, kEmbedderUrl, true, | |
152 callback); | |
153 EXPECT_FALSE(context.was_persisted()); | |
154 EXPECT_FALSE(context.was_granted()); | |
155 SetContentSetting(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, CONTENT_SETTING_ALLOW); | |
156 SetContentSetting(CONTENT_SETTINGS_TYPE_PUSH_MESSAGING, | |
157 CONTENT_SETTING_BLOCK); | |
158 context.DecidePermission(NULL, request_id, kEmbedderUrl, kEmbedderUrl, true, | |
159 callback); | |
160 EXPECT_FALSE(context.was_persisted()); | |
161 EXPECT_FALSE(context.was_granted()); | |
162 SetContentSetting(CONTENT_SETTINGS_TYPE_PUSH_MESSAGING, CONTENT_SETTING_ASK); | |
163 context.DecidePermission(NULL, request_id, kEmbedderUrl, kEmbedderUrl, true, | |
164 callback); | |
165 EXPECT_TRUE(context.was_persisted()); | |
166 EXPECT_TRUE(context.was_granted()); | |
167 } | |
168 | |
169 } // namespace gcm | |
OLD | NEW |