Roll src/third_party/WebKit eac3800:0237a66 (svn 202606:202607)
[chromium-blink-merge.git] / chrome / browser / permissions / permission_context_base_unittest.cc
blob3e672099546186d4a3ad21d8c9283c85d7a81367
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/permissions/permission_context_base.h"
7 #include "base/bind.h"
8 #include "base/command_line.h"
9 #include "chrome/browser/content_settings/host_content_settings_map_factory.h"
10 #include "chrome/browser/infobars/infobar_service.h"
11 #include "chrome/browser/permissions/permission_queue_controller.h"
12 #include "chrome/browser/permissions/permission_request_id.h"
13 #include "chrome/browser/ui/website_settings/permission_bubble_manager.h"
14 #include "chrome/common/chrome_switches.h"
15 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
16 #include "chrome/test/base/testing_profile.h"
17 #include "components/content_settings/core/browser/host_content_settings_map.h"
18 #include "components/content_settings/core/common/content_settings.h"
19 #include "components/content_settings/core/common/content_settings_types.h"
20 #include "content/public/browser/render_frame_host.h"
21 #include "content/public/browser/web_contents.h"
22 #include "content/public/test/mock_render_process_host.h"
23 #include "content/public/test/web_contents_tester.h"
24 #include "testing/gtest/include/gtest/gtest.h"
26 class TestPermissionContext : public PermissionContextBase {
27 public:
28 TestPermissionContext(Profile* profile,
29 const ContentSettingsType permission_type)
30 : PermissionContextBase(profile, permission_type),
31 permission_set_(false),
32 permission_granted_(false),
33 tab_context_updated_(false) {}
35 ~TestPermissionContext() override {}
37 PermissionQueueController* GetInfoBarController() {
38 return GetQueueController();
41 bool permission_granted() {
42 return permission_granted_;
45 bool permission_set() {
46 return permission_set_;
49 bool tab_context_updated() {
50 return tab_context_updated_;
53 void TrackPermissionDecision(ContentSetting content_setting) {
54 permission_set_ = true;
55 permission_granted_ = content_setting == CONTENT_SETTING_ALLOW;
58 protected:
59 void UpdateTabContext(const PermissionRequestID& id,
60 const GURL& requesting_origin,
61 bool allowed) override {
62 tab_context_updated_ = true;
65 bool IsRestrictedToSecureOrigins() const override {
66 return false;
69 private:
70 bool permission_set_;
71 bool permission_granted_;
72 bool tab_context_updated_;
75 class PermissionContextBaseTests : public ChromeRenderViewHostTestHarness {
76 protected:
77 PermissionContextBaseTests() {}
79 // Accept or dismiss the permission bubble or infobar.
80 void RespondToPermission(TestPermissionContext* context,
81 const PermissionRequestID& id,
82 const GURL& url,
83 bool accept) {
84 if (!PermissionBubbleManager::Enabled()) {
85 context->GetInfoBarController()->OnPermissionSet(
86 id, url, url, accept, accept);
87 return;
90 PermissionBubbleManager* manager =
91 PermissionBubbleManager::FromWebContents(web_contents());
92 if (accept)
93 manager->Accept();
94 else
95 manager->Closing();
98 void TestAskAndGrant_TestContent() {
99 TestPermissionContext permission_context(
100 profile(), CONTENT_SETTINGS_TYPE_NOTIFICATIONS);
101 GURL url("http://www.google.com");
102 content::WebContentsTester::For(web_contents())->NavigateAndCommit(url);
104 const PermissionRequestID id(
105 web_contents()->GetRenderProcessHost()->GetID(),
106 web_contents()->GetMainFrame()->GetRoutingID(),
107 -1);
108 permission_context.RequestPermission(
109 web_contents(),
110 id, url, true,
111 base::Bind(&TestPermissionContext::TrackPermissionDecision,
112 base::Unretained(&permission_context)));
114 RespondToPermission(&permission_context, id, url, true);
115 EXPECT_TRUE(permission_context.permission_set());
116 EXPECT_TRUE(permission_context.permission_granted());
117 EXPECT_TRUE(permission_context.tab_context_updated());
119 ContentSetting setting =
120 HostContentSettingsMapFactory::GetForProfile(profile())
121 ->GetContentSetting(url.GetOrigin(),
122 url.GetOrigin(),
123 CONTENT_SETTINGS_TYPE_NOTIFICATIONS,
124 std::string());
125 EXPECT_EQ(CONTENT_SETTING_ALLOW, setting);
128 void TestAskAndDismiss_TestContent() {
129 TestPermissionContext permission_context(
130 profile(), CONTENT_SETTINGS_TYPE_MIDI_SYSEX);
131 GURL url("http://www.google.es");
132 content::WebContentsTester::For(web_contents())->NavigateAndCommit(url);
134 const PermissionRequestID id(
135 web_contents()->GetRenderProcessHost()->GetID(),
136 web_contents()->GetMainFrame()->GetRoutingID(),
137 -1);
138 permission_context.RequestPermission(
139 web_contents(),
140 id, url, true,
141 base::Bind(&TestPermissionContext::TrackPermissionDecision,
142 base::Unretained(&permission_context)));
144 RespondToPermission(&permission_context, id, url, false);
145 EXPECT_TRUE(permission_context.permission_set());
146 EXPECT_FALSE(permission_context.permission_granted());
147 EXPECT_TRUE(permission_context.tab_context_updated());
149 ContentSetting setting =
150 HostContentSettingsMapFactory::GetForProfile(profile())
151 ->GetContentSetting(url.GetOrigin(),
152 url.GetOrigin(),
153 CONTENT_SETTINGS_TYPE_MIDI_SYSEX,
154 std::string());
155 EXPECT_EQ(CONTENT_SETTING_ASK, setting);
158 void TestRequestPermissionInvalidUrl(ContentSettingsType type) {
159 TestPermissionContext permission_context(profile(), type);
160 GURL url;
161 ASSERT_FALSE(url.is_valid());
162 content::WebContentsTester::For(web_contents())->NavigateAndCommit(url);
164 const PermissionRequestID id(
165 web_contents()->GetRenderProcessHost()->GetID(),
166 web_contents()->GetMainFrame()->GetRoutingID(),
167 -1);
168 permission_context.RequestPermission(
169 web_contents(),
170 id, url, true,
171 base::Bind(&TestPermissionContext::TrackPermissionDecision,
172 base::Unretained(&permission_context)));
174 EXPECT_TRUE(permission_context.permission_set());
175 EXPECT_FALSE(permission_context.permission_granted());
176 EXPECT_TRUE(permission_context.tab_context_updated());
178 ContentSetting setting =
179 HostContentSettingsMapFactory::GetForProfile(profile())
180 ->GetContentSetting(url.GetOrigin(),
181 url.GetOrigin(),
182 type,
183 std::string());
184 EXPECT_EQ(CONTENT_SETTING_ASK, setting);
187 void TestGrantAndRevoke_TestContent(ContentSettingsType type,
188 ContentSetting expected_default) {
189 TestPermissionContext permission_context(profile(), type);
190 GURL url("https://www.google.com");
191 content::WebContentsTester::For(web_contents())->NavigateAndCommit(url);
193 const PermissionRequestID id(
194 web_contents()->GetRenderProcessHost()->GetID(),
195 web_contents()->GetMainFrame()->GetRoutingID(),
196 -1);
197 permission_context.RequestPermission(
198 web_contents(),
199 id, url, true,
200 base::Bind(&TestPermissionContext::TrackPermissionDecision,
201 base::Unretained(&permission_context)));
203 RespondToPermission(&permission_context, id, url, true);
204 EXPECT_TRUE(permission_context.permission_set());
205 EXPECT_TRUE(permission_context.permission_granted());
206 EXPECT_TRUE(permission_context.tab_context_updated());
208 ContentSetting setting =
209 HostContentSettingsMapFactory::GetForProfile(profile())
210 ->GetContentSetting(url.GetOrigin(),
211 url.GetOrigin(),
212 type,
213 std::string());
214 EXPECT_EQ(CONTENT_SETTING_ALLOW, setting);
216 // Try to reset permission.
217 permission_context.ResetPermission(url.GetOrigin(), url.GetOrigin());
218 ContentSetting setting_after_reset =
219 HostContentSettingsMapFactory::GetForProfile(profile())
220 ->GetContentSetting(url.GetOrigin(),
221 url.GetOrigin(),
222 type,
223 std::string());
224 ContentSetting default_setting =
225 HostContentSettingsMapFactory::GetForProfile(profile())
226 ->GetDefaultContentSetting(type, nullptr);
227 EXPECT_EQ(default_setting, setting_after_reset);
230 private:
231 // ChromeRenderViewHostTestHarness:
232 void SetUp() override {
233 ChromeRenderViewHostTestHarness::SetUp();
234 InfoBarService::CreateForWebContents(web_contents());
235 PermissionBubbleManager::CreateForWebContents(web_contents());
238 DISALLOW_COPY_AND_ASSIGN(PermissionContextBaseTests);
241 // Simulates clicking Accept. The permission should be granted and
242 // saved for future use.
243 TEST_F(PermissionContextBaseTests, TestAskAndGrant) {
244 TestAskAndGrant_TestContent();
247 // Simulates clicking Dismiss (X) in the infobar/bubble.
248 // The permission should be denied but not saved for future use.
249 TEST_F(PermissionContextBaseTests, TestAskAndDismiss) {
250 TestAskAndDismiss_TestContent();
253 // Simulates non-valid requesting URL.
254 // The permission should be denied but not saved for future use.
255 TEST_F(PermissionContextBaseTests, TestNonValidRequestingUrl) {
256 TestRequestPermissionInvalidUrl(CONTENT_SETTINGS_TYPE_GEOLOCATION);
257 TestRequestPermissionInvalidUrl(CONTENT_SETTINGS_TYPE_NOTIFICATIONS);
258 TestRequestPermissionInvalidUrl(CONTENT_SETTINGS_TYPE_MIDI_SYSEX);
259 TestRequestPermissionInvalidUrl(CONTENT_SETTINGS_TYPE_PUSH_MESSAGING);
260 #if defined(OS_ANDROID) || defined(OS_CHROMEOS)
261 TestRequestPermissionInvalidUrl(
262 CONTENT_SETTINGS_TYPE_PROTECTED_MEDIA_IDENTIFIER);
263 #endif
266 #if defined(OS_ANDROID)
267 // This test is specific to Android because other platforms use bubbles.
268 TEST_F(PermissionContextBaseTests, TestGrantAndRevokeWithInfobars) {
269 TestGrantAndRevoke_TestContent(CONTENT_SETTINGS_TYPE_GEOLOCATION,
270 CONTENT_SETTING_ASK);
271 TestGrantAndRevoke_TestContent(CONTENT_SETTINGS_TYPE_MIDI_SYSEX,
272 CONTENT_SETTING_ASK);
273 TestGrantAndRevoke_TestContent(
274 CONTENT_SETTINGS_TYPE_PROTECTED_MEDIA_IDENTIFIER, CONTENT_SETTING_ASK);
275 // TODO(timvolodine): currently no test for
276 // CONTENT_SETTINGS_TYPE_NOTIFICATIONS because notification permissions work
277 // differently with infobars as compared to bubbles (crbug.com/453784).
278 // TODO(timvolodine): currently no test for
279 // CONTENT_SETTINGS_TYPE_PUSH_MESSAGING because infobars do not implement push
280 // messaging permissions (crbug.com/453788).
282 #endif
284 #if !defined(OS_ANDROID) && !defined(OS_IOS)
285 // Simulates granting and revoking of permissions using permission bubbles.
286 // This test shouldn't run on mobile because mobile platforms use infobars.
287 TEST_F(PermissionContextBaseTests, TestGrantAndRevokeWithBubbles) {
288 TestGrantAndRevoke_TestContent(CONTENT_SETTINGS_TYPE_GEOLOCATION,
289 CONTENT_SETTING_ASK);
290 #if defined(ENABLE_NOTIFICATIONS)
291 TestGrantAndRevoke_TestContent(CONTENT_SETTINGS_TYPE_NOTIFICATIONS,
292 CONTENT_SETTING_ASK);
293 #endif
294 TestGrantAndRevoke_TestContent(CONTENT_SETTINGS_TYPE_MIDI_SYSEX,
295 CONTENT_SETTING_ASK);
296 TestGrantAndRevoke_TestContent(CONTENT_SETTINGS_TYPE_PUSH_MESSAGING,
297 CONTENT_SETTING_ASK);
299 #endif