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/content_settings/permission_context_base.h"
8 #include "chrome/browser/content_settings/host_content_settings_map.h"
9 #include "chrome/browser/content_settings/permission_queue_controller.h"
10 #include "chrome/browser/content_settings/permission_request_id.h"
11 #include "chrome/browser/infobars/infobar_service.h"
12 #include "chrome/common/content_settings.h"
13 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
14 #include "chrome/test/base/testing_profile.h"
15 #include "components/content_settings/core/common/content_settings_types.h"
16 #include "content/public/browser/web_contents.h"
17 #include "content/public/test/mock_render_process_host.h"
18 #include "content/public/test/web_contents_tester.h"
19 #include "testing/gtest/include/gtest/gtest.h"
21 class PermissionContextBaseTests
: public ChromeRenderViewHostTestHarness
{
23 PermissionContextBaseTests() {}
24 virtual ~PermissionContextBaseTests() {}
27 // ChromeRenderViewHostTestHarness:
28 virtual void SetUp() OVERRIDE
{
29 ChromeRenderViewHostTestHarness::SetUp();
30 InfoBarService::CreateForWebContents(web_contents());
33 DISALLOW_COPY_AND_ASSIGN(PermissionContextBaseTests
);
36 class TestPermissionContext
: public PermissionContextBase
{
38 TestPermissionContext(Profile
* profile
,
39 const ContentSettingsType permission_type
)
40 : PermissionContextBase(profile
, permission_type
),
41 permission_set_(false),
42 permission_granted_(false),
43 tab_context_updated_(false) {}
45 virtual ~TestPermissionContext() {}
47 PermissionQueueController
* GetInfoBarController() {
48 return GetQueueController();
51 bool permission_granted() {
52 return permission_granted_
;
55 bool permission_set() {
56 return permission_set_
;
59 bool tab_context_updated() {
60 return tab_context_updated_
;
63 void TrackPermissionDecision(bool granted
) {
64 permission_set_
= true;
65 permission_granted_
= granted
;
69 virtual void UpdateTabContext(const PermissionRequestID
& id
,
70 const GURL
& requesting_origin
,
71 bool allowed
) OVERRIDE
{
72 tab_context_updated_
= true;
77 bool permission_granted_
;
78 bool tab_context_updated_
;
81 // Simulates clicking Accept. The permission should be granted and
82 // saved for future use.
83 TEST_F(PermissionContextBaseTests
, TestAskAndGrant
) {
84 TestPermissionContext
permission_context(profile(),
85 CONTENT_SETTINGS_TYPE_PUSH_MESSAGING
);
86 GURL
url("http://www.google.com");
87 content::WebContentsTester::For(web_contents())->NavigateAndCommit(url
);
89 const PermissionRequestID
id(
90 web_contents()->GetRenderProcessHost()->GetID(),
91 web_contents()->GetRenderViewHost()->GetRoutingID(),
93 permission_context
.RequestPermission(
96 base::Bind(&TestPermissionContext::TrackPermissionDecision
,
97 base::Unretained(&permission_context
)));
99 permission_context
.GetInfoBarController()->OnPermissionSet(
100 id
, url
, url
, true, true);
101 EXPECT_TRUE(permission_context
.permission_set());
102 EXPECT_TRUE(permission_context
.permission_granted());
103 EXPECT_TRUE(permission_context
.tab_context_updated());
105 ContentSetting setting
=
106 profile()->GetHostContentSettingsMap()->GetContentSetting(
107 url
.GetOrigin(), url
.GetOrigin(),
108 CONTENT_SETTINGS_TYPE_PUSH_MESSAGING
, std::string());
109 EXPECT_EQ(CONTENT_SETTING_ALLOW
, setting
);
112 // Simulates clicking Dismiss (X in the infobar.
113 // The permission should be denied but not saved for future use.
114 TEST_F(PermissionContextBaseTests
, TestAskAndDismiss
) {
115 TestPermissionContext
permission_context(profile(),
116 CONTENT_SETTINGS_TYPE_MIDI_SYSEX
);
117 GURL
url("http://www.google.es");
118 content::WebContentsTester::For(web_contents())->NavigateAndCommit(url
);
120 const PermissionRequestID
id(
121 web_contents()->GetRenderProcessHost()->GetID(),
122 web_contents()->GetRenderViewHost()->GetRoutingID(),
124 permission_context
.RequestPermission(
127 base::Bind(&TestPermissionContext::TrackPermissionDecision
,
128 base::Unretained(&permission_context
)));
130 permission_context
.GetInfoBarController()->OnPermissionSet(
131 id
, url
, url
, false, false);
132 EXPECT_TRUE(permission_context
.permission_set());
133 EXPECT_FALSE(permission_context
.permission_granted());
134 EXPECT_TRUE(permission_context
.tab_context_updated());
136 ContentSetting setting
=
137 profile()->GetHostContentSettingsMap()->GetContentSetting(
138 url
.GetOrigin(), url
.GetOrigin(),
139 CONTENT_SETTINGS_TYPE_MIDI_SYSEX
, std::string());
140 EXPECT_EQ(CONTENT_SETTING_ASK
, setting
);