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/media_stream_device_permission_context.h"
8 #include "chrome/browser/content_settings/host_content_settings_map_factory.h"
9 #include "chrome/browser/infobars/infobar_service.h"
10 #include "chrome/browser/permissions/permission_queue_controller.h"
11 #include "chrome/browser/permissions/permission_request_id.h"
12 #include "chrome/browser/ui/website_settings/permission_bubble_manager.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/browser/host_content_settings_map.h"
16 #include "components/content_settings/core/common/content_settings.h"
17 #include "components/content_settings/core/common/content_settings_types.h"
18 #include "content/public/browser/web_contents.h"
19 #include "content/public/test/mock_render_process_host.h"
20 #include "content/public/test/web_contents_tester.h"
21 #include "testing/gtest/include/gtest/gtest.h"
24 class TestPermissionContext
: public MediaStreamDevicePermissionContext
{
26 TestPermissionContext(Profile
* profile
,
27 const ContentSettingsType permission_type
)
28 : MediaStreamDevicePermissionContext(profile
, permission_type
) {}
30 ~TestPermissionContext() override
{}
33 } // anonymous namespace
35 // TODO(raymes): many tests in MediaStreamDevicesControllerTest should be
36 // converted to tests in this file.
37 class MediaStreamDevicePermissionContextTests
38 : public ChromeRenderViewHostTestHarness
{
40 MediaStreamDevicePermissionContextTests() = default;
42 void TestInsecureQueryingUrl(ContentSettingsType permission_type
) {
43 TestPermissionContext
permission_context(profile(), permission_type
);
44 GURL
insecure_url("http://www.example.com");
45 GURL
secure_url("https://www.example.com");
47 // Check that there is no saved content settings.
48 EXPECT_EQ(CONTENT_SETTING_ASK
,
49 HostContentSettingsMapFactory::GetForProfile(profile())
50 ->GetContentSetting(insecure_url
.GetOrigin(),
51 insecure_url
.GetOrigin(),
54 EXPECT_EQ(CONTENT_SETTING_ASK
,
55 HostContentSettingsMapFactory::GetForProfile(profile())
56 ->GetContentSetting(secure_url
.GetOrigin(),
57 insecure_url
.GetOrigin(),
60 EXPECT_EQ(CONTENT_SETTING_ASK
,
61 HostContentSettingsMapFactory::GetForProfile(profile())
62 ->GetContentSetting(insecure_url
.GetOrigin(),
63 secure_url
.GetOrigin(),
67 EXPECT_EQ(CONTENT_SETTING_ASK
, permission_context
.GetPermissionStatus(
68 insecure_url
, insecure_url
));
69 EXPECT_EQ(CONTENT_SETTING_ASK
,
70 permission_context
.GetPermissionStatus(insecure_url
, secure_url
));
73 void TestSecureQueryingUrl(ContentSettingsType permission_type
) {
74 TestPermissionContext
permission_context(profile(), permission_type
);
75 GURL
secure_url("https://www.example.com");
77 // Check that there is no saved content settings.
78 EXPECT_EQ(CONTENT_SETTING_ASK
,
79 HostContentSettingsMapFactory::GetForProfile(profile())
80 ->GetContentSetting(secure_url
.GetOrigin(),
81 secure_url
.GetOrigin(),
85 EXPECT_EQ(CONTENT_SETTING_ASK
,
86 permission_context
.GetPermissionStatus(secure_url
, secure_url
));
90 // ChromeRenderViewHostTestHarness:
91 void SetUp() override
{
92 ChromeRenderViewHostTestHarness::SetUp();
93 InfoBarService::CreateForWebContents(web_contents());
94 PermissionBubbleManager::CreateForWebContents(web_contents());
97 DISALLOW_COPY_AND_ASSIGN(MediaStreamDevicePermissionContextTests
);
100 // MEDIASTREAM_MIC permission status should be ask for insecure origin to
101 // accommodate the usage case of Flash.
102 TEST_F(MediaStreamDevicePermissionContextTests
, TestMicInsecureQueryingUrl
) {
103 TestInsecureQueryingUrl(CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC
);
106 // MEDIASTREAM_CAMERA permission status should be ask for insecure origin to
107 // accommodate the usage case of Flash.
108 TEST_F(MediaStreamDevicePermissionContextTests
, TestCameraInsecureQueryingUrl
) {
109 TestInsecureQueryingUrl(CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA
);
112 // MEDIASTREAM_MIC permission status should be ask for Secure origin.
113 TEST_F(MediaStreamDevicePermissionContextTests
, TestMicSecureQueryingUrl
) {
114 TestSecureQueryingUrl(CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC
);
117 // MEDIASTREAM_CAMERA permission status should be ask for Secure origin.
118 TEST_F(MediaStreamDevicePermissionContextTests
, TestCameraSecureQueryingUrl
) {
119 TestSecureQueryingUrl(CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA
);