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 kOriginA[] = "https://origina.org"; | |
15 const char kOriginB[] = "https://originb.org"; | |
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() const { return was_persisted_; } | |
28 bool was_granted() const { return permission_granted_; } | |
29 | |
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; | |
40 } | |
41 | |
42 bool was_persisted_; | |
43 bool permission_granted_; | |
44 }; | |
45 | |
46 class PushMessagingPermissionContextTest : public testing::Test { | |
47 public: | |
48 PushMessagingPermissionContextTest() {} | |
49 | |
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 profile->GetHostContentSettingsMap(); | |
58 host_content_settings_map->SetContentSetting(pattern, pattern, setting, | |
59 std::string(), value); | |
60 } | |
61 | |
62 content::TestBrowserThreadBundle thread_bundle_; | |
63 }; | |
64 | |
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))); | |
70 | |
71 // Just granting notifications should still prompt | |
72 SetContentSetting(&profile, CONTENT_SETTINGS_TYPE_NOTIFICATIONS, | |
73 CONTENT_SETTING_ALLOW); | |
74 | |
75 EXPECT_EQ(CONTENT_SETTING_ASK, | |
76 context.GetPermissionStatus(GURL(kOriginA), GURL(kOriginA))); | |
77 | |
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); | |
83 | |
84 EXPECT_EQ(CONTENT_SETTING_ASK, | |
85 context.GetPermissionStatus(GURL(kOriginA), GURL(kOriginA))); | |
86 } | |
87 | |
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))); | |
105 | |
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))); | |
112 } | |
113 | |
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))); | |
119 } | |
120 | |
121 TEST_F(PushMessagingPermissionContextTest, HasPermissionAccept) { | |
122 TestingProfile profile; | |
123 PushMessagingPermissionContext context(&profile); | |
124 | |
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))); | |
131 } | |
132 | |
133 TEST_F(PushMessagingPermissionContextTest, DecidePushPermission) { | |
134 TestingProfile profile; | |
135 TestPushMessagingPermissionContext context(&profile); | |
136 PermissionRequestID request_id(-1, -1, -1, GURL(kOriginA)); | |
137 BrowserPermissionCallback callback; | |
138 | |
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()); | |
143 | |
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()); | |
150 | |
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()); | |
157 | |
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()); | |
164 } | |
165 | |
166 } // namespace gcm | |
OLD | NEW |