OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "chrome/browser/media/media_stream_device_permission_context.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/macros.h" | |
9 #include "build/build_config.h" | |
10 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" | |
11 #include "chrome/browser/infobars/infobar_service.h" | |
12 #include "chrome/browser/permissions/permission_request_id.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/permission_type.h" | |
19 #include "content/public/browser/web_contents.h" | |
20 #include "content/public/test/mock_render_process_host.h" | |
21 #include "content/public/test/web_contents_tester.h" | |
22 #include "testing/gtest/include/gtest/gtest.h" | |
23 | |
24 #if !defined(OS_ANDROID) | |
25 #include "chrome/browser/permissions/permission_request_manager.h" | |
26 #endif | |
27 | |
28 namespace { | |
29 class TestPermissionContext : public MediaStreamDevicePermissionContext { | |
30 public: | |
31 TestPermissionContext(Profile* profile, | |
32 const ContentSettingsType permission_type) | |
33 : MediaStreamDevicePermissionContext( | |
34 profile, | |
35 permission_type == CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA | |
36 ? content::PermissionType::VIDEO_CAPTURE | |
37 : content::PermissionType::AUDIO_CAPTURE, | |
38 permission_type) {} | |
39 | |
40 ~TestPermissionContext() override {} | |
41 }; | |
42 | |
43 } // anonymous namespace | |
44 | |
45 // TODO(raymes): many tests in MediaStreamDevicesControllerTest should be | |
46 // converted to tests in this file. | |
47 class MediaStreamDevicePermissionContextTests | |
48 : public ChromeRenderViewHostTestHarness { | |
49 protected: | |
50 MediaStreamDevicePermissionContextTests() = default; | |
51 | |
52 void TestInsecureQueryingUrl(ContentSettingsType permission_type) { | |
53 TestPermissionContext permission_context(profile(), permission_type); | |
54 GURL insecure_url("http://www.example.com"); | |
55 GURL secure_url("https://www.example.com"); | |
56 | |
57 // Check that there is no saved content settings. | |
58 EXPECT_EQ(CONTENT_SETTING_ASK, | |
59 HostContentSettingsMapFactory::GetForProfile(profile()) | |
60 ->GetContentSetting(insecure_url.GetOrigin(), | |
61 insecure_url.GetOrigin(), | |
62 permission_type, | |
63 std::string())); | |
64 EXPECT_EQ(CONTENT_SETTING_ASK, | |
65 HostContentSettingsMapFactory::GetForProfile(profile()) | |
66 ->GetContentSetting(secure_url.GetOrigin(), | |
67 insecure_url.GetOrigin(), | |
68 permission_type, | |
69 std::string())); | |
70 EXPECT_EQ(CONTENT_SETTING_ASK, | |
71 HostContentSettingsMapFactory::GetForProfile(profile()) | |
72 ->GetContentSetting(insecure_url.GetOrigin(), | |
73 secure_url.GetOrigin(), | |
74 permission_type, | |
75 std::string())); | |
76 | |
77 EXPECT_EQ(CONTENT_SETTING_ASK, permission_context.GetPermissionStatus( | |
78 insecure_url, insecure_url)); | |
79 EXPECT_EQ(CONTENT_SETTING_ASK, | |
80 permission_context.GetPermissionStatus(insecure_url, secure_url)); | |
81 } | |
82 | |
83 void TestSecureQueryingUrl(ContentSettingsType permission_type) { | |
84 TestPermissionContext permission_context(profile(), permission_type); | |
85 GURL secure_url("https://www.example.com"); | |
86 | |
87 // Check that there is no saved content settings. | |
88 EXPECT_EQ(CONTENT_SETTING_ASK, | |
89 HostContentSettingsMapFactory::GetForProfile(profile()) | |
90 ->GetContentSetting(secure_url.GetOrigin(), | |
91 secure_url.GetOrigin(), | |
92 permission_type, | |
93 std::string())); | |
94 | |
95 EXPECT_EQ(CONTENT_SETTING_ASK, | |
96 permission_context.GetPermissionStatus(secure_url, secure_url)); | |
97 } | |
98 | |
99 private: | |
100 // ChromeRenderViewHostTestHarness: | |
101 void SetUp() override { | |
102 ChromeRenderViewHostTestHarness::SetUp(); | |
103 #if defined(OS_ANDROID) | |
104 InfoBarService::CreateForWebContents(web_contents()); | |
105 #else | |
106 PermissionRequestManager::CreateForWebContents(web_contents()); | |
107 #endif | |
108 } | |
109 | |
110 DISALLOW_COPY_AND_ASSIGN(MediaStreamDevicePermissionContextTests); | |
111 }; | |
112 | |
113 // MEDIASTREAM_MIC permission status should be ask for insecure origin to | |
114 // accommodate the usage case of Flash. | |
115 TEST_F(MediaStreamDevicePermissionContextTests, TestMicInsecureQueryingUrl) { | |
116 TestInsecureQueryingUrl(CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC); | |
117 } | |
118 | |
119 // MEDIASTREAM_CAMERA permission status should be ask for insecure origin to | |
120 // accommodate the usage case of Flash. | |
121 TEST_F(MediaStreamDevicePermissionContextTests, TestCameraInsecureQueryingUrl) { | |
122 TestInsecureQueryingUrl(CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA); | |
123 } | |
124 | |
125 // MEDIASTREAM_MIC permission status should be ask for Secure origin. | |
126 TEST_F(MediaStreamDevicePermissionContextTests, TestMicSecureQueryingUrl) { | |
127 TestSecureQueryingUrl(CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC); | |
128 } | |
129 | |
130 // MEDIASTREAM_CAMERA permission status should be ask for Secure origin. | |
131 TEST_F(MediaStreamDevicePermissionContextTests, TestCameraSecureQueryingUrl) { | |
132 TestSecureQueryingUrl(CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA); | |
133 } | |
OLD | NEW |