OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 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 "base/file_util.h" | |
6 #include "base/path_service.h" | |
7 #include "base/ref_counted.h" | |
8 #include "chrome/browser/download/download_manager.h" | |
9 #include "chrome/browser/extensions/extension_error_reporter.h" | |
10 #include "chrome/browser/profiles/profile.h" | |
11 #include "chrome/browser/renderer_host/site_instance.h" | |
12 #include "chrome/browser/tab_contents/tab_contents.h" | |
13 #include "chrome/browser/ui/browser.h" | |
14 #include "chrome/common/chrome_paths.h" | |
15 #include "chrome/common/notification_details.h" | |
16 #include "chrome/common/notification_observer.h" | |
17 #include "chrome/common/notification_registrar.h" | |
18 #include "chrome/common/notification_type.h" | |
19 #include "chrome/test/in_process_browser_test.h" | |
20 #include "chrome/test/ui_test_utils.h" | |
21 #include "net/base/net_util.h" | |
22 #include "net/test/test_server.h" | |
23 | |
24 class RenderViewHostManagerTest : public InProcessBrowserTest { | |
25 public: | |
26 RenderViewHostManagerTest() { | |
27 EnableDOMAutomation(); | |
28 } | |
29 | |
30 static bool GetFilePathWithHostAndPortReplacement( | |
31 const std::string& original_file_path, | |
32 const net::HostPortPair& host_port_pair, | |
33 std::string* replacement_path) { | |
34 std::vector<net::TestServer::StringPair> replacement_text; | |
35 replacement_text.push_back( | |
36 make_pair("REPLACE_WITH_HOST_AND_PORT", host_port_pair.ToString())); | |
37 return net::TestServer::GetFilePathWithReplacements( | |
38 original_file_path, replacement_text, replacement_path); | |
39 } | |
40 }; | |
41 | |
42 // Test for crbug.com/24447. Following a cross-site link with rel=noreferrer | |
43 // and target=_blank should create a new SiteInstance. | |
44 IN_PROC_BROWSER_TEST_F(RenderViewHostManagerTest, | |
45 SwapProcessWithRelNoreferrerAndTargetBlank) { | |
46 // Start two servers with different sites. | |
47 ASSERT_TRUE(test_server()->Start()); | |
48 net::TestServer https_server_( | |
49 net::TestServer::TYPE_HTTPS, | |
50 FilePath(FILE_PATH_LITERAL("chrome/test/data"))); | |
51 ASSERT_TRUE(https_server_.Start()); | |
52 | |
53 // Load a page with links that open in a new window. | |
54 std::string replacement_path; | |
55 ASSERT_TRUE(GetFilePathWithHostAndPortReplacement( | |
56 "files/click-noreferrer-links.html", | |
57 https_server_.host_port_pair(), | |
58 &replacement_path)); | |
59 ui_test_utils::NavigateToURL(browser(), | |
60 test_server()->GetURL(replacement_path)); | |
61 | |
62 // Get the original SiteInstance for later comparison. | |
63 scoped_refptr<SiteInstance> orig_site_instance( | |
64 browser()->GetSelectedTabContents()->GetSiteInstance()); | |
65 EXPECT_TRUE(orig_site_instance != NULL); | |
66 | |
67 // Test clicking a rel=noreferrer + target=blank link. | |
68 bool success = false; | |
69 EXPECT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( | |
70 browser()->GetSelectedTabContents()->render_view_host(), L"", | |
71 L"window.domAutomationController.send(clickNoRefTargetBlankLink());", | |
72 &success)); | |
73 EXPECT_TRUE(success); | |
74 // Wait for the cross-site transition to finish. | |
75 ui_test_utils::WaitForLoadStop( | |
76 &(browser()->GetSelectedTabContents()->controller())); | |
77 | |
78 // Opens in new tab. | |
79 EXPECT_EQ(2, browser()->tab_count()); | |
80 EXPECT_EQ(1, browser()->selected_index()); | |
81 EXPECT_EQ(L"Title Of Awesomeness", | |
82 browser()->GetSelectedTabContents()->GetTitle()); | |
83 | |
84 // Should have a new SiteInstance. | |
85 scoped_refptr<SiteInstance> noref_blank_site_instance( | |
86 browser()->GetSelectedTabContents()->GetSiteInstance()); | |
87 EXPECT_NE(orig_site_instance, noref_blank_site_instance); | |
88 } | |
89 | |
90 // Test for crbug.com/24447. Following a cross-site link with just | |
91 // target=_blank should not create a new SiteInstance. | |
92 // Disabled, http://crbug.com/67532. | |
93 IN_PROC_BROWSER_TEST_F(RenderViewHostManagerTest, | |
94 DISABLED_DontSwapProcessWithOnlyTargetBlank) { | |
95 // Start two servers with different sites. | |
96 ASSERT_TRUE(test_server()->Start()); | |
97 net::TestServer https_server_( | |
98 net::TestServer::TYPE_HTTPS, | |
99 FilePath(FILE_PATH_LITERAL("chrome/test/data"))); | |
100 ASSERT_TRUE(https_server_.Start()); | |
101 | |
102 // Load a page with links that open in a new window. | |
103 std::string replacement_path; | |
104 ASSERT_TRUE(GetFilePathWithHostAndPortReplacement( | |
105 "files/click-noreferrer-links.html", | |
106 https_server_.host_port_pair(), | |
107 &replacement_path)); | |
108 ui_test_utils::NavigateToURL(browser(), | |
109 test_server()->GetURL(replacement_path)); | |
110 | |
111 // Get the original SiteInstance for later comparison. | |
112 scoped_refptr<SiteInstance> orig_site_instance( | |
113 browser()->GetSelectedTabContents()->GetSiteInstance()); | |
114 EXPECT_TRUE(orig_site_instance != NULL); | |
115 | |
116 // Test clicking a target=blank link. | |
117 bool success = false; | |
118 EXPECT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( | |
119 browser()->GetSelectedTabContents()->render_view_host(), L"", | |
120 L"window.domAutomationController.send(clickTargetBlankLink());", | |
121 &success)); | |
122 EXPECT_TRUE(success); | |
123 // Wait for the cross-site transition to finish. | |
124 ui_test_utils::WaitForLoadStop( | |
125 &(browser()->GetSelectedTabContents()->controller())); | |
126 | |
127 // Opens in new tab. | |
128 EXPECT_EQ(2, browser()->tab_count()); | |
129 EXPECT_EQ(1, browser()->selected_index()); | |
130 EXPECT_EQ(L"Title Of Awesomeness", | |
131 browser()->GetSelectedTabContents()->GetTitle()); | |
132 | |
133 // Should have the same SiteInstance. | |
134 scoped_refptr<SiteInstance> blank_site_instance( | |
135 browser()->GetSelectedTabContents()->GetSiteInstance()); | |
136 EXPECT_EQ(orig_site_instance, blank_site_instance); | |
137 } | |
138 | |
139 // Test for crbug.com/24447. Following a cross-site link with rel=noreferrer | |
140 // and no target=_blank should not create a new SiteInstance. | |
141 IN_PROC_BROWSER_TEST_F(RenderViewHostManagerTest, | |
142 DontSwapProcessWithOnlyRelNoreferrer) { | |
143 // Start two servers with different sites. | |
144 ASSERT_TRUE(test_server()->Start()); | |
145 net::TestServer https_server_( | |
146 net::TestServer::TYPE_HTTPS, | |
147 FilePath(FILE_PATH_LITERAL("chrome/test/data"))); | |
148 ASSERT_TRUE(https_server_.Start()); | |
149 | |
150 // Load a page with links that open in a new window. | |
151 std::string replacement_path; | |
152 ASSERT_TRUE(GetFilePathWithHostAndPortReplacement( | |
153 "files/click-noreferrer-links.html", | |
154 https_server_.host_port_pair(), | |
155 &replacement_path)); | |
156 ui_test_utils::NavigateToURL(browser(), | |
157 test_server()->GetURL(replacement_path)); | |
158 | |
159 // Get the original SiteInstance for later comparison. | |
160 scoped_refptr<SiteInstance> orig_site_instance( | |
161 browser()->GetSelectedTabContents()->GetSiteInstance()); | |
162 EXPECT_TRUE(orig_site_instance != NULL); | |
163 | |
164 // Test clicking a rel=noreferrer link. | |
165 bool success = false; | |
166 EXPECT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( | |
167 browser()->GetSelectedTabContents()->render_view_host(), L"", | |
168 L"window.domAutomationController.send(clickNoRefLink());", | |
169 &success)); | |
170 EXPECT_TRUE(success); | |
171 // Wait for the cross-site transition to finish. | |
172 ui_test_utils::WaitForLoadStop( | |
173 &(browser()->GetSelectedTabContents()->controller())); | |
174 | |
175 // Opens in same tab. | |
176 EXPECT_EQ(1, browser()->tab_count()); | |
177 EXPECT_EQ(0, browser()->selected_index()); | |
178 EXPECT_EQ(L"Title Of Awesomeness", | |
179 browser()->GetSelectedTabContents()->GetTitle()); | |
180 | |
181 // Should have the same SiteInstance. | |
182 scoped_refptr<SiteInstance> noref_site_instance( | |
183 browser()->GetSelectedTabContents()->GetSiteInstance()); | |
184 EXPECT_EQ(orig_site_instance, noref_site_instance); | |
185 } | |
186 | |
187 // Hangs flakily in Win, http://crbug.com/45040. | |
188 #if defined(OS_WIN) | |
189 #define MAYBE_ChromeURLAfterDownload DISABLED_ChromeURLAfterDownload | |
190 #else | |
191 #defne MAYBE_ChromeURLAfterDownload ChromeURLAfterDownload | |
192 #endif // defined(OS_WIN) | |
193 | |
194 // Test for crbug.com/14505. This tests that chrome:// urls are still functional | |
195 // after download of a file while viewing another chrome://. | |
196 IN_PROC_BROWSER_TEST_F(RenderViewHostManagerTest, | |
197 MAYBE_ChromeURLAfterDownload) { | |
198 GURL downloads_url("chrome://downloads"); | |
199 GURL extensions_url("chrome://extensions"); | |
200 FilePath zip_download; | |
201 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &zip_download)); | |
202 zip_download = zip_download.AppendASCII("zip").AppendASCII("test.zip"); | |
203 GURL zip_url = net::FilePathToFileURL(zip_download); | |
204 | |
205 ui_test_utils::NavigateToURL(browser(), downloads_url); | |
206 ui_test_utils::NavigateToURL(browser(), zip_url); | |
207 ui_test_utils::WaitForDownloadCount( | |
208 browser()->profile()->GetDownloadManager(), 1); | |
209 ui_test_utils::NavigateToURL(browser(), extensions_url); | |
210 | |
211 TabContents *contents = browser()->GetSelectedTabContents(); | |
212 ASSERT_TRUE(contents); | |
213 bool webui_responded = false; | |
214 EXPECT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( | |
215 contents->render_view_host(), | |
216 L"", | |
217 L"window.domAutomationController.send(window.webui_responded_);", | |
218 &webui_responded)); | |
219 EXPECT_TRUE(webui_responded); | |
220 } | |
221 | |
222 class BrowserClosedObserver : public NotificationObserver { | |
223 public: | |
224 explicit BrowserClosedObserver(Browser* browser) { | |
225 registrar_.Add(this, NotificationType::BROWSER_CLOSED, | |
226 Source<Browser>(browser)); | |
227 ui_test_utils::RunMessageLoop(); | |
228 } | |
229 | |
230 // NotificationObserver | |
231 virtual void Observe(NotificationType type, | |
232 const NotificationSource& source, | |
233 const NotificationDetails& details) { | |
234 switch (type.value) { | |
235 case NotificationType::BROWSER_CLOSED: | |
236 MessageLoopForUI::current()->Quit(); | |
237 break; | |
238 } | |
239 } | |
240 | |
241 private: | |
242 NotificationRegistrar registrar_; | |
243 }; | |
244 | |
245 // Test for crbug.com/12745. This tests that if a download is initiated from | |
246 // a chrome:// page that has registered and onunload handler, the browser | |
247 // will be able to close. | |
248 // TODO(rafaelw): The fix for 12745 has now also been reverted. Another fix | |
249 // must be found before this can be re-enabled. | |
250 IN_PROC_BROWSER_TEST_F(RenderViewHostManagerTest, | |
251 DISABLED_BrowserCloseAfterDownload) { | |
252 GURL downloads_url("chrome://downloads"); | |
253 FilePath zip_download; | |
254 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &zip_download)); | |
255 zip_download = zip_download.AppendASCII("zip").AppendASCII("test.zip"); | |
256 ASSERT_TRUE(file_util::PathExists(zip_download)); | |
257 GURL zip_url = net::FilePathToFileURL(zip_download); | |
258 | |
259 ui_test_utils::NavigateToURL(browser(), downloads_url); | |
260 TabContents *contents = browser()->GetSelectedTabContents(); | |
261 ASSERT_TRUE(contents); | |
262 bool result = false; | |
263 EXPECT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( | |
264 contents->render_view_host(), | |
265 L"", | |
266 L"window.onunload = function() { var do_nothing = 0; }; " | |
267 L"window.domAutomationController.send(true);", | |
268 &result)); | |
269 EXPECT_TRUE(result); | |
270 ui_test_utils::NavigateToURL(browser(), zip_url); | |
271 | |
272 ui_test_utils::WaitForDownloadCount( | |
273 browser()->profile()->GetDownloadManager(), 1); | |
274 | |
275 browser()->CloseWindow(); | |
276 BrowserClosedObserver wait_for_close(browser()); | |
277 } | |
OLD | NEW |