1 // Copyright 2015 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/media/midi_permission_context.h"
8 #include "chrome/browser/infobars/infobar_service.h"
9 #include "chrome/browser/permissions/permission_queue_controller.h"
10 #include "chrome/browser/permissions/permission_request_id.h"
11 #include "chrome/browser/ui/website_settings/permission_bubble_manager.h"
12 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
13 #include "chrome/test/base/testing_profile.h"
14 #include "components/content_settings/core/browser/host_content_settings_map.h"
15 #include "components/content_settings/core/common/content_settings.h"
16 #include "components/content_settings/core/common/content_settings_types.h"
17 #include "content/public/browser/web_contents.h"
18 #include "content/public/test/mock_render_process_host.h"
19 #include "content/public/test/web_contents_tester.h"
20 #include "testing/gtest/include/gtest/gtest.h"
24 class TestPermissionContext
: public MidiPermissionContext
{
26 explicit TestPermissionContext(Profile
* profile
)
27 : MidiPermissionContext(profile
),
28 permission_set_(false),
29 permission_granted_(false),
30 tab_context_updated_(false) {}
32 ~TestPermissionContext() override
{}
34 PermissionQueueController
* GetInfoBarController() {
35 return GetQueueController();
38 bool permission_granted() {
39 return permission_granted_
;
42 bool permission_set() {
43 return permission_set_
;
46 bool tab_context_updated() {
47 return tab_context_updated_
;
50 void TrackPermissionDecision(ContentSetting content_setting
) {
51 permission_set_
= true;
52 permission_granted_
= content_setting
== CONTENT_SETTING_ALLOW
;
56 void UpdateTabContext(const PermissionRequestID
& id
,
57 const GURL
& requesting_origin
,
58 bool allowed
) override
{
59 tab_context_updated_
= true;
64 bool permission_granted_
;
65 bool tab_context_updated_
;
68 } // anonymous namespace
70 class MidiPermissionContextTests
: public ChromeRenderViewHostTestHarness
{
72 MidiPermissionContextTests() = default;
75 // ChromeRenderViewHostTestHarness:
76 void SetUp() override
{
77 ChromeRenderViewHostTestHarness::SetUp();
78 InfoBarService::CreateForWebContents(web_contents());
79 PermissionBubbleManager::CreateForWebContents(web_contents());
82 DISALLOW_COPY_AND_ASSIGN(MidiPermissionContextTests
);
85 // Web MIDI permission should be denied for insecure origin.
86 TEST_F(MidiPermissionContextTests
, TestInsecureRequestingUrl
) {
87 TestPermissionContext
permission_context(profile());
88 GURL
url("http://www.example.com");
89 content::WebContentsTester::For(web_contents())->NavigateAndCommit(url
);
91 const PermissionRequestID
id(
92 web_contents()->GetRenderProcessHost()->GetID(),
93 web_contents()->GetMainFrame()->GetRoutingID(),
95 permission_context
.RequestPermission(
98 base::Bind(&TestPermissionContext::TrackPermissionDecision
,
99 base::Unretained(&permission_context
)));
101 EXPECT_TRUE(permission_context
.permission_set());
102 EXPECT_FALSE(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_MIDI_SYSEX
, std::string());
109 EXPECT_EQ(CONTENT_SETTING_ASK
, setting
);
112 // Web MIDI permission status should be denied for insecure origin.
113 TEST_F(MidiPermissionContextTests
, TestInsecureQueryingUrl
) {
114 TestPermissionContext
permission_context(profile());
115 GURL
insecure_url("http://www.example.com");
116 GURL
secure_url("https://www.example.com");
118 // Check that there is no saved content settings.
119 EXPECT_EQ(CONTENT_SETTING_ASK
,
120 profile()->GetHostContentSettingsMap()->GetContentSetting(
121 insecure_url
.GetOrigin(), insecure_url
.GetOrigin(),
122 CONTENT_SETTINGS_TYPE_MIDI_SYSEX
, std::string()));
123 EXPECT_EQ(CONTENT_SETTING_ASK
,
124 profile()->GetHostContentSettingsMap()->GetContentSetting(
125 secure_url
.GetOrigin(), insecure_url
.GetOrigin(),
126 CONTENT_SETTINGS_TYPE_MIDI_SYSEX
, std::string()));
127 EXPECT_EQ(CONTENT_SETTING_ASK
,
128 profile()->GetHostContentSettingsMap()->GetContentSetting(
129 insecure_url
.GetOrigin(), secure_url
.GetOrigin(),
130 CONTENT_SETTINGS_TYPE_MIDI_SYSEX
, std::string()));
132 EXPECT_EQ(CONTENT_SETTING_BLOCK
, permission_context
.GetPermissionStatus(
133 insecure_url
, insecure_url
));
134 EXPECT_EQ(CONTENT_SETTING_BLOCK
, permission_context
.GetPermissionStatus(
135 insecure_url
, secure_url
));