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.
7 #include "base/files/file_path.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "chrome/browser/content_settings/tab_specific_content_settings.h"
10 #include "chrome/browser/ui/browser.h"
11 #include "chrome/browser/ui/browser_content_setting_bubble_model_delegate.h"
12 #include "chrome/browser/ui/browser_finder.h"
13 #include "chrome/browser/ui/content_settings/content_setting_bubble_model.h"
14 #include "chrome/browser/ui/tabs/tab_strip_model.h"
15 #include "chrome/test/base/in_process_browser_test.h"
16 #include "chrome/test/base/ui_test_utils.h"
17 #include "components/content_settings/core/common/content_settings_types.h"
18 #include "content/public/test/test_navigation_observer.h"
19 #include "net/test/spawned_test_server/spawned_test_server.h"
20 #include "testing/gtest/include/gtest/gtest.h"
22 const base::FilePath::CharType kDocRoot
[] =
23 FILE_PATH_LITERAL("chrome/test/data");
25 class ContentSettingBubbleModelMixedScriptTest
: public InProcessBrowserTest
{
27 void SetUpInProcessBrowserTestFixture() override
{
29 new net::SpawnedTestServer(
30 net::SpawnedTestServer::TYPE_HTTPS
,
31 net::SpawnedTestServer::SSLOptions(
32 net::SpawnedTestServer::SSLOptions::CERT_OK
),
33 base::FilePath(kDocRoot
)));
34 ASSERT_TRUE(https_server_
->Start());
37 TabSpecificContentSettings
* GetActiveTabSpecificContentSettings() {
38 return TabSpecificContentSettings::FromWebContents(
39 browser()->tab_strip_model()->GetActiveWebContents());
42 scoped_ptr
<net::SpawnedTestServer
> https_server_
;
45 // Tests that a MIXEDSCRIPT type ContentSettingBubbleModel sends appropriate
46 // IPCs to allow the renderer to load unsafe scripts and refresh the page
48 IN_PROC_BROWSER_TEST_F(ContentSettingBubbleModelMixedScriptTest
, MainFrame
) {
49 GURL
url(https_server_
->GetURL(
50 "files/content_setting_bubble/mixed_script.html"));
52 // Load a page with mixed content and do quick verification by looking at
54 ui_test_utils::NavigateToURL(browser(), url
);
56 EXPECT_TRUE(GetActiveTabSpecificContentSettings()->IsContentBlocked(
57 CONTENT_SETTINGS_TYPE_MIXEDSCRIPT
));
59 // Emulate link clicking on the mixed script bubble.
60 scoped_ptr
<ContentSettingBubbleModel
> model(
61 ContentSettingBubbleModel::CreateContentSettingBubbleModel(
62 browser()->content_setting_bubble_model_delegate(),
63 browser()->tab_strip_model()->GetActiveWebContents(),
65 CONTENT_SETTINGS_TYPE_MIXEDSCRIPT
));
66 model
->OnCustomLinkClicked();
69 content::TestNavigationObserver
observer(
70 browser()->tab_strip_model()->GetActiveWebContents());
73 EXPECT_FALSE(GetActiveTabSpecificContentSettings()->IsContentBlocked(
74 CONTENT_SETTINGS_TYPE_MIXEDSCRIPT
));
77 // Tests that a MIXEDSCRIPT type ContentSettingBubbleModel works for an iframe.
78 IN_PROC_BROWSER_TEST_F(ContentSettingBubbleModelMixedScriptTest
, Iframe
) {
79 GURL
url(https_server_
->GetURL(
80 "files/content_setting_bubble/mixed_script_in_iframe.html"));
82 ui_test_utils::NavigateToURL(browser(), url
);
84 EXPECT_TRUE(GetActiveTabSpecificContentSettings()->IsContentBlocked(
85 CONTENT_SETTINGS_TYPE_MIXEDSCRIPT
));
87 scoped_ptr
<ContentSettingBubbleModel
> model(
88 ContentSettingBubbleModel::CreateContentSettingBubbleModel(
89 browser()->content_setting_bubble_model_delegate(),
90 browser()->tab_strip_model()->GetActiveWebContents(),
92 CONTENT_SETTINGS_TYPE_MIXEDSCRIPT
));
93 model
->OnCustomLinkClicked();
95 content::TestNavigationObserver
observer(
96 browser()->tab_strip_model()->GetActiveWebContents());
99 EXPECT_FALSE(GetActiveTabSpecificContentSettings()->IsContentBlocked(
100 CONTENT_SETTINGS_TYPE_MIXEDSCRIPT
));
103 class ContentSettingBubbleModelMediaStreamTest
: public InProcessBrowserTest
{
105 void ManageMediaStreamSettings(
106 TabSpecificContentSettings::MicrophoneCameraState state
) {
107 // Open a tab for which we will invoke the media bubble.
108 GURL
url(ui_test_utils::GetTestUrl(
109 base::FilePath().AppendASCII("content_setting_bubble"),
110 base::FilePath().AppendASCII("mixed_script.html")));
111 ui_test_utils::NavigateToURL(browser(), url
);
112 content::WebContents
* original_tab
= GetActiveTab();
114 // Create a bubble with the given camera and microphone access state.
115 TabSpecificContentSettings::FromWebContents(original_tab
)->
116 OnMediaStreamPermissionSet(
117 original_tab
->GetLastCommittedURL(),
118 state
, std::string(), std::string(), std::string(), std::string());
119 scoped_ptr
<ContentSettingBubbleModel
> bubble(
120 ContentSettingBubbleModel::CreateContentSettingBubbleModel(
121 browser()->content_setting_bubble_model_delegate(),
123 browser()->profile(),
124 CONTENT_SETTINGS_TYPE_MEDIASTREAM
));
126 // Click the management link, which opens in a new tab or window.
127 // Wait until it loads.
128 bubble
->OnManageLinkClicked();
129 ASSERT_NE(GetActiveTab(), original_tab
);
130 content::TestNavigationObserver
observer(GetActiveTab());
134 content::WebContents
* GetActiveTab() {
135 // First, we need to find the active browser window. It should be at
136 // the same desktop as the browser in which we invoked the bubble.
137 Browser
* active_browser
= chrome::FindLastActiveWithHostDesktopType(
138 browser()->host_desktop_type());
139 return active_browser
->tab_strip_model()->GetActiveWebContents();
143 // Tests that clicking on the management link in the media bubble opens
144 // the correct section of the settings UI.
145 IN_PROC_BROWSER_TEST_F(ContentSettingBubbleModelMediaStreamTest
, ManageLink
) {
146 // For each of the three options, we click the management link and check if
147 // the active tab loads the correct internal url.
149 // The microphone bubble links to microphone exceptions.
150 ManageMediaStreamSettings(TabSpecificContentSettings::MICROPHONE_ACCESSED
);
151 EXPECT_EQ(GURL("chrome://settings/contentExceptions#media-stream-mic"),
152 GetActiveTab()->GetLastCommittedURL());
154 // The camera bubble links to camera exceptions.
155 ManageMediaStreamSettings(TabSpecificContentSettings::CAMERA_ACCESSED
);
156 EXPECT_EQ(GURL("chrome://settings/contentExceptions#media-stream-camera"),
157 GetActiveTab()->GetLastCommittedURL());
159 // The bubble for both media devices links to the the first section of the
160 // default media content settings, which is the microphone section.
161 ManageMediaStreamSettings(TabSpecificContentSettings::MICROPHONE_ACCESSED
|
162 TabSpecificContentSettings::CAMERA_ACCESSED
);
163 EXPECT_EQ(GURL("chrome://settings/content#media-stream-mic"),
164 GetActiveTab()->GetLastCommittedURL());