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.
5 #include "chrome/browser/push_messaging/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"
14 const char kOriginA
[] = "https://origina.org";
15 const char kOriginB
[] = "https://originb.org";
17 class TestPushMessagingPermissionContext
18 : public PushMessagingPermissionContext
{
20 explicit TestPushMessagingPermissionContext(Profile
* profile
)
21 : PushMessagingPermissionContext(profile
),
22 was_persisted_(false),
23 permission_granted_(false) {}
25 bool was_persisted() const { return was_persisted_
; }
26 bool was_granted() const { return permission_granted_
; }
29 // PushMessagingPermissionContext:
30 void NotifyPermissionSet(const PermissionRequestID
& id
,
31 const GURL
& requesting_origin
,
32 const GURL
& embedder_origin
,
33 const BrowserPermissionCallback
& callback
,
35 ContentSetting content_setting
) override
{
36 was_persisted_
= persist
;
37 permission_granted_
= content_setting
== CONTENT_SETTING_ALLOW
;
41 bool permission_granted_
;
44 class PushMessagingPermissionContextTest
: public testing::Test
{
46 PushMessagingPermissionContextTest() {}
49 void SetContentSetting(Profile
* profile
,
50 ContentSettingsType setting
,
51 ContentSetting value
) {
52 ContentSettingsPattern pattern
=
53 ContentSettingsPattern::FromString(kOriginA
);
54 HostContentSettingsMap
* host_content_settings_map
=
55 profile
->GetHostContentSettingsMap();
56 host_content_settings_map
->SetContentSetting(pattern
, pattern
, setting
,
57 std::string(), value
);
60 content::TestBrowserThreadBundle thread_bundle_
;
63 TEST_F(PushMessagingPermissionContextTest
, HasPermissionPrompt
) {
64 TestingProfile profile
;
65 PushMessagingPermissionContext
context(&profile
);
66 EXPECT_EQ(CONTENT_SETTING_ASK
,
67 context
.GetPermissionStatus(GURL(kOriginA
), GURL(kOriginA
)));
69 // Just granting notifications should still prompt
70 SetContentSetting(&profile
, CONTENT_SETTINGS_TYPE_NOTIFICATIONS
,
71 CONTENT_SETTING_ALLOW
);
73 EXPECT_EQ(CONTENT_SETTING_ASK
,
74 context
.GetPermissionStatus(GURL(kOriginA
), GURL(kOriginA
)));
76 // Just granting push should still prompt
77 SetContentSetting(&profile
, CONTENT_SETTINGS_TYPE_NOTIFICATIONS
,
79 SetContentSetting(&profile
, CONTENT_SETTINGS_TYPE_PUSH_MESSAGING
,
80 CONTENT_SETTING_ALLOW
);
82 EXPECT_EQ(CONTENT_SETTING_ASK
,
83 context
.GetPermissionStatus(GURL(kOriginA
), GURL(kOriginA
)));
86 TEST_F(PushMessagingPermissionContextTest
, HasPermissionDenySettingsMismatch
) {
87 TestingProfile profile
;
88 PushMessagingPermissionContext
context(&profile
);
89 SetContentSetting(&profile
, CONTENT_SETTINGS_TYPE_NOTIFICATIONS
,
90 CONTENT_SETTING_BLOCK
);
91 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
92 context
.GetPermissionStatus(GURL(kOriginA
), GURL(kOriginA
)));
93 SetContentSetting(&profile
, CONTENT_SETTINGS_TYPE_NOTIFICATIONS
,
95 SetContentSetting(&profile
, CONTENT_SETTINGS_TYPE_PUSH_MESSAGING
,
96 CONTENT_SETTING_BLOCK
);
97 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
98 context
.GetPermissionStatus(GURL(kOriginA
), GURL(kOriginA
)));
99 SetContentSetting(&profile
, CONTENT_SETTINGS_TYPE_NOTIFICATIONS
,
100 CONTENT_SETTING_ALLOW
);
101 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
102 context
.GetPermissionStatus(GURL(kOriginA
), GURL(kOriginA
)));
104 SetContentSetting(&profile
, CONTENT_SETTINGS_TYPE_NOTIFICATIONS
,
105 CONTENT_SETTING_ASK
);
106 SetContentSetting(&profile
, CONTENT_SETTINGS_TYPE_PUSH_MESSAGING
,
107 CONTENT_SETTING_BLOCK
);
108 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
109 context
.GetPermissionStatus(GURL(kOriginA
), GURL(kOriginA
)));
112 TEST_F(PushMessagingPermissionContextTest
, HasPermissionDenyDifferentOrigins
) {
113 TestingProfile profile
;
114 PushMessagingPermissionContext
context(&profile
);
115 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
116 context
.GetPermissionStatus(GURL(kOriginB
), GURL(kOriginA
)));
119 TEST_F(PushMessagingPermissionContextTest
, HasPermissionAccept
) {
120 TestingProfile profile
;
121 PushMessagingPermissionContext
context(&profile
);
123 SetContentSetting(&profile
, CONTENT_SETTINGS_TYPE_NOTIFICATIONS
,
124 CONTENT_SETTING_ALLOW
);
125 SetContentSetting(&profile
, CONTENT_SETTINGS_TYPE_PUSH_MESSAGING
,
126 CONTENT_SETTING_ALLOW
);
127 EXPECT_EQ(CONTENT_SETTING_ALLOW
,
128 context
.GetPermissionStatus(GURL(kOriginA
), GURL(kOriginA
)));
131 TEST_F(PushMessagingPermissionContextTest
, DecidePushPermission
) {
132 TestingProfile profile
;
133 TestPushMessagingPermissionContext
context(&profile
);
134 PermissionRequestID
request_id(-1, -1, -1, GURL(kOriginA
));
135 BrowserPermissionCallback callback
;
137 context
.DecidePushPermission(request_id
, GURL(kOriginA
), GURL(kOriginA
),
138 callback
, CONTENT_SETTING_DEFAULT
);
139 EXPECT_FALSE(context
.was_persisted());
140 EXPECT_FALSE(context
.was_granted());
142 SetContentSetting(&profile
, CONTENT_SETTINGS_TYPE_PUSH_MESSAGING
,
143 CONTENT_SETTING_ALLOW
);
144 context
.DecidePushPermission(request_id
, GURL(kOriginA
), GURL(kOriginA
),
145 callback
, CONTENT_SETTING_ALLOW
);
146 EXPECT_TRUE(context
.was_persisted());
147 EXPECT_TRUE(context
.was_granted());
149 SetContentSetting(&profile
, CONTENT_SETTINGS_TYPE_PUSH_MESSAGING
,
150 CONTENT_SETTING_BLOCK
);
151 context
.DecidePushPermission(request_id
, GURL(kOriginA
), GURL(kOriginA
),
152 callback
, CONTENT_SETTING_ALLOW
);
153 EXPECT_TRUE(context
.was_persisted());
154 EXPECT_FALSE(context
.was_granted());
156 SetContentSetting(&profile
, CONTENT_SETTINGS_TYPE_PUSH_MESSAGING
,
157 CONTENT_SETTING_ASK
);
158 context
.DecidePushPermission(request_id
, GURL(kOriginA
), GURL(kOriginA
),
159 callback
, CONTENT_SETTING_ALLOW
);
160 EXPECT_TRUE(context
.was_persisted());
161 EXPECT_TRUE(context
.was_granted());