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 protected: | |
62 void SetContentSetting(ContentSettingsType setting, ContentSetting value) { | |
63 ContentSettingsPattern pattern = | |
64 ContentSettingsPattern::FromString(kEmbedder); | |
65 HostContentSettingsMap* host_content_settings_map = | |
66 profile_.GetHostContentSettingsMap(); | |
67 host_content_settings_map->SetContentSetting(pattern, pattern, setting, | |
fgorski
2014/11/17 18:24:06
nit: move all of the parameters to the new line
Miguel Garcia
2014/11/18 10:47:30
this is what clank cl format has decided...
| |
68 std::string(), value); | |
69 } | |
fgorski
2014/11/17 18:24:06
nit: new line
Miguel Garcia
2014/11/18 10:47:30
Done.
| |
70 TestingProfile profile_; | |
71 content::TestBrowserThreadBundle thread_bundle_; | |
72 | |
73 private: | |
74 void SetUp() override { | |
fgorski
2014/11/17 18:24:07
SetUp is typically public
Miguel Garcia
2014/11/18 10:47:30
Ok, I made it public but would like to understand
| |
75 HostContentSettingsMap* host_content_settings_map = | |
76 profile_.GetHostContentSettingsMap(); | |
77 host_content_settings_map->SetDefaultContentSetting( | |
78 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, CONTENT_SETTING_ASK); | |
79 host_content_settings_map->SetDefaultContentSetting( | |
80 CONTENT_SETTINGS_TYPE_PUSH_MESSAGING, CONTENT_SETTING_ASK); | |
81 } | |
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 SetContentSetting(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, CONTENT_SETTING_ALLOW); | |
fgorski
2014/11/17 18:24:06
I think you are already testing for that in 114-11
Miguel Garcia
2014/11/18 10:47:30
Indeed, removed this duplicated check.
| |
124 SetContentSetting(CONTENT_SETTINGS_TYPE_PUSH_MESSAGING, | |
125 CONTENT_SETTING_BLOCK); | |
126 EXPECT_EQ(CONTENT_SETTING_BLOCK, | |
127 context.GetPermissionStatus(GURL(kEmbedder), GURL(kEmbedder))); | |
128 } | |
129 | |
130 TEST_F(PushMessagingPermissionContextTest, HasPermissionAccept) { | |
131 PushMessagingPermissionContext context(&profile_); | |
132 SetContentSetting(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, CONTENT_SETTING_ALLOW); | |
133 SetContentSetting(CONTENT_SETTINGS_TYPE_PUSH_MESSAGING, | |
134 CONTENT_SETTING_ALLOW); | |
135 EXPECT_EQ(CONTENT_SETTING_ALLOW, | |
136 context.GetPermissionStatus(GURL(kEmbedder), GURL(kEmbedder))); | |
137 } | |
138 | |
139 TEST_F(PushMessagingPermissionContextTest, DecidePermission) { | |
140 TestPushMessagingPermissionContext context(&profile_); | |
141 PermissionRequestID request_id(-1, -1, -1, GURL(kEmbedder)); | |
142 BrowserPermissionCallback callback; | |
143 | |
144 context.DecidePermission(NULL, request_id, GURL(kEmbedder), GURL(kEmbedder), | |
145 true, callback); | |
146 EXPECT_FALSE(context.was_persisted()); | |
147 EXPECT_FALSE(context.was_granted()); | |
148 | |
149 SetContentSetting(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, CONTENT_SETTING_BLOCK); | |
150 context.DecidePermission(NULL, request_id, GURL(kEmbedder), GURL(kEmbedder), | |
151 true, callback); | |
152 EXPECT_FALSE(context.was_persisted()); | |
153 EXPECT_FALSE(context.was_granted()); | |
154 SetContentSetting(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, CONTENT_SETTING_ALLOW); | |
155 SetContentSetting(CONTENT_SETTINGS_TYPE_PUSH_MESSAGING, | |
156 CONTENT_SETTING_BLOCK); | |
157 context.DecidePermission(NULL, request_id, GURL(kEmbedder), GURL(kEmbedder), | |
158 true, callback); | |
159 EXPECT_FALSE(context.was_persisted()); | |
160 EXPECT_FALSE(context.was_granted()); | |
161 SetContentSetting(CONTENT_SETTINGS_TYPE_PUSH_MESSAGING, CONTENT_SETTING_ASK); | |
162 context.DecidePermission(NULL, request_id, GURL(kEmbedder), GURL(kEmbedder), | |
163 true, callback); | |
164 EXPECT_TRUE(context.was_persisted()); | |
165 EXPECT_TRUE(context.was_granted()); | |
166 } | |
167 | |
168 } // namespace gcm | |
OLD | NEW |