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"
7 #include "chrome/browser/permissions/permission_request_id.h"
8 #include "chrome/test/base/testing_profile.h"
9 #include "components/content_settings/core/browser/host_content_settings_map.h"
10 #include "components/content_settings/core/common/content_settings.h"
11 #include "components/content_settings/core/common/content_settings_types.h"
12 #include "content/public/test/test_browser_thread_bundle.h"
13 #include "testing/gtest/include/gtest/gtest.h"
15 const char kOriginA
[] = "https://origina.org";
16 const char kOriginB
[] = "https://originb.org";
18 class TestPushMessagingPermissionContext
19 : public PushMessagingPermissionContext
{
21 explicit TestPushMessagingPermissionContext(Profile
* profile
)
22 : PushMessagingPermissionContext(profile
),
23 was_persisted_(false),
24 permission_granted_(false) {}
26 bool was_persisted() const { return was_persisted_
; }
27 bool was_granted() const { return permission_granted_
; }
30 // PushMessagingPermissionContext:
31 void NotifyPermissionSet(const PermissionRequestID
& id
,
32 const GURL
& requesting_origin
,
33 const GURL
& embedder_origin
,
34 const BrowserPermissionCallback
& callback
,
36 ContentSetting content_setting
) override
{
37 was_persisted_
= persist
;
38 permission_granted_
= content_setting
== CONTENT_SETTING_ALLOW
;
42 bool permission_granted_
;
45 class PushMessagingPermissionContextTest
: public testing::Test
{
47 PushMessagingPermissionContextTest() {}
50 void SetContentSetting(Profile
* profile
,
51 ContentSettingsType setting
,
52 ContentSetting value
) {
53 ContentSettingsPattern pattern
=
54 ContentSettingsPattern::FromString(kOriginA
);
55 HostContentSettingsMap
* host_content_settings_map
=
56 profile
->GetHostContentSettingsMap();
57 host_content_settings_map
->SetContentSetting(pattern
, pattern
, setting
,
58 std::string(), value
);
61 content::TestBrowserThreadBundle thread_bundle_
;
64 TEST_F(PushMessagingPermissionContextTest
, HasPermissionPrompt
) {
65 TestingProfile profile
;
66 PushMessagingPermissionContext
context(&profile
);
67 EXPECT_EQ(CONTENT_SETTING_ASK
,
68 context
.GetPermissionStatus(GURL(kOriginA
), GURL(kOriginA
)));
70 // Just granting notifications should still prompt
71 SetContentSetting(&profile
, CONTENT_SETTINGS_TYPE_NOTIFICATIONS
,
72 CONTENT_SETTING_ALLOW
);
74 EXPECT_EQ(CONTENT_SETTING_ASK
,
75 context
.GetPermissionStatus(GURL(kOriginA
), GURL(kOriginA
)));
77 // Just granting push should still prompt
78 SetContentSetting(&profile
, CONTENT_SETTINGS_TYPE_NOTIFICATIONS
,
80 SetContentSetting(&profile
, CONTENT_SETTINGS_TYPE_PUSH_MESSAGING
,
81 CONTENT_SETTING_ALLOW
);
83 EXPECT_EQ(CONTENT_SETTING_ASK
,
84 context
.GetPermissionStatus(GURL(kOriginA
), GURL(kOriginA
)));
87 TEST_F(PushMessagingPermissionContextTest
, HasPermissionDenySettingsMismatch
) {
88 TestingProfile profile
;
89 PushMessagingPermissionContext
context(&profile
);
90 SetContentSetting(&profile
, CONTENT_SETTINGS_TYPE_NOTIFICATIONS
,
91 CONTENT_SETTING_BLOCK
);
92 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
93 context
.GetPermissionStatus(GURL(kOriginA
), GURL(kOriginA
)));
94 SetContentSetting(&profile
, CONTENT_SETTINGS_TYPE_NOTIFICATIONS
,
96 SetContentSetting(&profile
, CONTENT_SETTINGS_TYPE_PUSH_MESSAGING
,
97 CONTENT_SETTING_BLOCK
);
98 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
99 context
.GetPermissionStatus(GURL(kOriginA
), GURL(kOriginA
)));
100 SetContentSetting(&profile
, CONTENT_SETTINGS_TYPE_NOTIFICATIONS
,
101 CONTENT_SETTING_ALLOW
);
102 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
103 context
.GetPermissionStatus(GURL(kOriginA
), GURL(kOriginA
)));
105 SetContentSetting(&profile
, CONTENT_SETTINGS_TYPE_NOTIFICATIONS
,
106 CONTENT_SETTING_ASK
);
107 SetContentSetting(&profile
, CONTENT_SETTINGS_TYPE_PUSH_MESSAGING
,
108 CONTENT_SETTING_BLOCK
);
109 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
110 context
.GetPermissionStatus(GURL(kOriginA
), GURL(kOriginA
)));
113 TEST_F(PushMessagingPermissionContextTest
, HasPermissionDenyDifferentOrigins
) {
114 TestingProfile profile
;
115 PushMessagingPermissionContext
context(&profile
);
116 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
117 context
.GetPermissionStatus(GURL(kOriginB
), GURL(kOriginA
)));
120 TEST_F(PushMessagingPermissionContextTest
, HasPermissionAccept
) {
121 TestingProfile profile
;
122 PushMessagingPermissionContext
context(&profile
);
124 SetContentSetting(&profile
, CONTENT_SETTINGS_TYPE_NOTIFICATIONS
,
125 CONTENT_SETTING_ALLOW
);
126 SetContentSetting(&profile
, CONTENT_SETTINGS_TYPE_PUSH_MESSAGING
,
127 CONTENT_SETTING_ALLOW
);
128 EXPECT_EQ(CONTENT_SETTING_ALLOW
,
129 context
.GetPermissionStatus(GURL(kOriginA
), GURL(kOriginA
)));
132 TEST_F(PushMessagingPermissionContextTest
, DecidePushPermission
) {
133 TestingProfile profile
;
134 TestPushMessagingPermissionContext
context(&profile
);
135 PermissionRequestID
request_id(-1, -1, -1, GURL(kOriginA
));
136 BrowserPermissionCallback callback
;
138 context
.DecidePushPermission(request_id
, GURL(kOriginA
), GURL(kOriginA
),
139 callback
, CONTENT_SETTING_DEFAULT
);
140 EXPECT_FALSE(context
.was_persisted());
141 EXPECT_FALSE(context
.was_granted());
143 SetContentSetting(&profile
, CONTENT_SETTINGS_TYPE_PUSH_MESSAGING
,
144 CONTENT_SETTING_ALLOW
);
145 context
.DecidePushPermission(request_id
, GURL(kOriginA
), GURL(kOriginA
),
146 callback
, CONTENT_SETTING_ALLOW
);
147 EXPECT_TRUE(context
.was_persisted());
148 EXPECT_TRUE(context
.was_granted());
150 SetContentSetting(&profile
, CONTENT_SETTINGS_TYPE_PUSH_MESSAGING
,
151 CONTENT_SETTING_BLOCK
);
152 context
.DecidePushPermission(request_id
, GURL(kOriginA
), GURL(kOriginA
),
153 callback
, CONTENT_SETTING_ALLOW
);
154 EXPECT_TRUE(context
.was_persisted());
155 EXPECT_FALSE(context
.was_granted());
157 SetContentSetting(&profile
, CONTENT_SETTINGS_TYPE_PUSH_MESSAGING
,
158 CONTENT_SETTING_ASK
);
159 context
.DecidePushPermission(request_id
, GURL(kOriginA
), GURL(kOriginA
),
160 callback
, CONTENT_SETTING_ALLOW
);
161 EXPECT_TRUE(context
.was_persisted());
162 EXPECT_TRUE(context
.was_granted());