Refactor WebsiteSettings to operate on a SecurityInfo
[chromium-blink-merge.git] / chrome / browser / push_messaging / push_messaging_permission_context_unittest.cc
blob51ed900a886a7aee453371e0e9583c6c43312447
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/content_settings/host_content_settings_map_factory.h"
8 #include "chrome/browser/permissions/permission_request_id.h"
9 #include "chrome/test/base/testing_profile.h"
10 #include "components/content_settings/core/browser/host_content_settings_map.h"
11 #include "components/content_settings/core/common/content_settings.h"
12 #include "components/content_settings/core/common/content_settings_types.h"
13 #include "content/public/test/test_browser_thread_bundle.h"
14 #include "testing/gtest/include/gtest/gtest.h"
16 const char kOriginA[] = "https://origina.org";
17 const char kOriginB[] = "https://originb.org";
19 class TestPushMessagingPermissionContext
20 : public PushMessagingPermissionContext {
21 public:
22 explicit TestPushMessagingPermissionContext(Profile* profile)
23 : PushMessagingPermissionContext(profile),
24 was_persisted_(false),
25 permission_granted_(false) {}
27 bool was_persisted() const { return was_persisted_; }
28 bool was_granted() const { return permission_granted_; }
30 private:
31 // PushMessagingPermissionContext:
32 void NotifyPermissionSet(const PermissionRequestID& id,
33 const GURL& requesting_origin,
34 const GURL& embedder_origin,
35 const BrowserPermissionCallback& callback,
36 bool persist,
37 ContentSetting content_setting) override {
38 was_persisted_ = persist;
39 permission_granted_ = content_setting == CONTENT_SETTING_ALLOW;
42 bool was_persisted_;
43 bool permission_granted_;
46 class PushMessagingPermissionContextTest : public testing::Test {
47 public:
48 PushMessagingPermissionContextTest() {}
50 protected:
51 void SetContentSetting(Profile* profile,
52 ContentSettingsType setting,
53 ContentSetting value) {
54 ContentSettingsPattern pattern =
55 ContentSettingsPattern::FromString(kOriginA);
56 HostContentSettingsMap* host_content_settings_map =
57 HostContentSettingsMapFactory::GetForProfile(profile);
58 host_content_settings_map->SetContentSetting(pattern, pattern, setting,
59 std::string(), value);
62 content::TestBrowserThreadBundle thread_bundle_;
65 TEST_F(PushMessagingPermissionContextTest, HasPermissionPrompt) {
66 TestingProfile profile;
67 PushMessagingPermissionContext context(&profile);
68 EXPECT_EQ(CONTENT_SETTING_ASK,
69 context.GetPermissionStatus(GURL(kOriginA), GURL(kOriginA)));
71 // Just granting notifications should still prompt
72 SetContentSetting(&profile, CONTENT_SETTINGS_TYPE_NOTIFICATIONS,
73 CONTENT_SETTING_ALLOW);
75 EXPECT_EQ(CONTENT_SETTING_ASK,
76 context.GetPermissionStatus(GURL(kOriginA), GURL(kOriginA)));
78 // Just granting push should still prompt
79 SetContentSetting(&profile, CONTENT_SETTINGS_TYPE_NOTIFICATIONS,
80 CONTENT_SETTING_ASK);
81 SetContentSetting(&profile, CONTENT_SETTINGS_TYPE_PUSH_MESSAGING,
82 CONTENT_SETTING_ALLOW);
84 EXPECT_EQ(CONTENT_SETTING_ASK,
85 context.GetPermissionStatus(GURL(kOriginA), GURL(kOriginA)));
88 TEST_F(PushMessagingPermissionContextTest, HasPermissionDenySettingsMismatch) {
89 TestingProfile profile;
90 PushMessagingPermissionContext context(&profile);
91 SetContentSetting(&profile, CONTENT_SETTINGS_TYPE_NOTIFICATIONS,
92 CONTENT_SETTING_BLOCK);
93 EXPECT_EQ(CONTENT_SETTING_BLOCK,
94 context.GetPermissionStatus(GURL(kOriginA), GURL(kOriginA)));
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);
103 EXPECT_EQ(CONTENT_SETTING_BLOCK,
104 context.GetPermissionStatus(GURL(kOriginA), GURL(kOriginA)));
106 SetContentSetting(&profile, CONTENT_SETTINGS_TYPE_NOTIFICATIONS,
107 CONTENT_SETTING_ASK);
108 SetContentSetting(&profile, CONTENT_SETTINGS_TYPE_PUSH_MESSAGING,
109 CONTENT_SETTING_BLOCK);
110 EXPECT_EQ(CONTENT_SETTING_BLOCK,
111 context.GetPermissionStatus(GURL(kOriginA), GURL(kOriginA)));
114 TEST_F(PushMessagingPermissionContextTest, HasPermissionDenyDifferentOrigins) {
115 TestingProfile profile;
116 PushMessagingPermissionContext context(&profile);
117 EXPECT_EQ(CONTENT_SETTING_BLOCK,
118 context.GetPermissionStatus(GURL(kOriginB), GURL(kOriginA)));
121 TEST_F(PushMessagingPermissionContextTest, HasPermissionAccept) {
122 TestingProfile profile;
123 PushMessagingPermissionContext context(&profile);
125 SetContentSetting(&profile, CONTENT_SETTINGS_TYPE_NOTIFICATIONS,
126 CONTENT_SETTING_ALLOW);
127 SetContentSetting(&profile, CONTENT_SETTINGS_TYPE_PUSH_MESSAGING,
128 CONTENT_SETTING_ALLOW);
129 EXPECT_EQ(CONTENT_SETTING_ALLOW,
130 context.GetPermissionStatus(GURL(kOriginA), GURL(kOriginA)));
133 TEST_F(PushMessagingPermissionContextTest, DecidePushPermission) {
134 TestingProfile profile;
135 TestPushMessagingPermissionContext context(&profile);
136 PermissionRequestID request_id(-1, -1, -1);
137 BrowserPermissionCallback callback;
139 context.DecidePushPermission(request_id, GURL(kOriginA), GURL(kOriginA),
140 callback, CONTENT_SETTING_DEFAULT);
141 EXPECT_FALSE(context.was_persisted());
142 EXPECT_FALSE(context.was_granted());
144 SetContentSetting(&profile, CONTENT_SETTINGS_TYPE_PUSH_MESSAGING,
145 CONTENT_SETTING_ALLOW);
146 context.DecidePushPermission(request_id, GURL(kOriginA), GURL(kOriginA),
147 callback, CONTENT_SETTING_ALLOW);
148 EXPECT_TRUE(context.was_persisted());
149 EXPECT_TRUE(context.was_granted());
151 SetContentSetting(&profile, CONTENT_SETTINGS_TYPE_PUSH_MESSAGING,
152 CONTENT_SETTING_BLOCK);
153 context.DecidePushPermission(request_id, GURL(kOriginA), GURL(kOriginA),
154 callback, CONTENT_SETTING_ALLOW);
155 EXPECT_TRUE(context.was_persisted());
156 EXPECT_FALSE(context.was_granted());
158 SetContentSetting(&profile, CONTENT_SETTINGS_TYPE_PUSH_MESSAGING,
159 CONTENT_SETTING_ASK);
160 context.DecidePushPermission(request_id, GURL(kOriginA), GURL(kOriginA),
161 callback, CONTENT_SETTING_ALLOW);
162 EXPECT_TRUE(context.was_persisted());
163 EXPECT_TRUE(context.was_granted());