Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/command_line.h" | 5 #include "base/command_line.h" |
| 6 #include "chrome/app/chrome_command_ids.h" | 6 #include "chrome/app/chrome_command_ids.h" |
| 7 #include "chrome/browser/chrome_notification_types.h" | 7 #include "chrome/browser/chrome_notification_types.h" |
| 8 #include "chrome/browser/renderer_context_menu/render_view_context_menu_test_uti l.h" | 8 #include "chrome/browser/renderer_context_menu/render_view_context_menu_test_uti l.h" |
| 9 #include "chrome/browser/ui/browser.h" | 9 #include "chrome/browser/ui/browser.h" |
| 10 #include "chrome/browser/ui/browser_commands.h" | 10 #include "chrome/browser/ui/browser_commands.h" |
| (...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 182 browser()->tab_strip_model()->GetWebContentsAt(1); | 182 browser()->tab_strip_model()->GetWebContentsAt(1); |
| 183 ASSERT_NE(new_web_contents, web_contents); | 183 ASSERT_NE(new_web_contents, web_contents); |
| 184 WaitForLoadStop(new_web_contents); | 184 WaitForLoadStop(new_web_contents); |
| 185 | 185 |
| 186 GURL view_frame_source_url(content::kViewSourceScheme + std::string(":") + | 186 GURL view_frame_source_url(content::kViewSourceScheme + std::string(":") + |
| 187 iframe_target_url.spec()); | 187 iframe_target_url.spec()); |
| 188 EXPECT_EQ(url_formatter::FormatUrl(view_frame_source_url), | 188 EXPECT_EQ(url_formatter::FormatUrl(view_frame_source_url), |
| 189 new_web_contents->GetTitle()); | 189 new_web_contents->GetTitle()); |
| 190 } | 190 } |
| 191 | 191 |
| 192 // Tests that verify that ctrl-click results 1) open up in a new renderer | |
| 193 // process (https://crbug.com/23815) and 2) are in a new browsing instance (e.g. | |
| 194 // cannot find the opener's window by name - https://crbug.com/658386). | |
| 195 class CtrlClickShouldEndUpInNewProcessTest | |
| 196 : public ChromeNavigationBrowserTest { | |
| 197 protected: | |
| 198 // Simulates ctrl-clicking an anchor with the given name in |main_contents|. | |
| 199 // Verifies that the new contents are in a separate process and separate | |
| 200 // browsing instance from |main|contents. Returns contents of the newly | |
|
Charlie Reis
2017/04/19 20:11:11
nit: |main_contents|
Łukasz Anforowicz
2017/04/20 01:11:29
Ooops. Not sure how that happened. Done.
Also -
| |
| 201 // opened tab. | |
| 202 content::WebContents* SimulateCtrlClick(content::WebContents* main_contents, | |
| 203 const char* id_of_anchor_to_click) { | |
| 204 // Ctrl-click the anchor/link in the page. | |
| 205 content::WebContents* new_contents = nullptr; | |
| 206 { | |
| 207 content::WebContentsAddedObserver new_tab_observer; | |
| 208 #if defined(OS_MACOSX) | |
| 209 const char* new_tab_click_script_template = | |
| 210 "simulateClick(\"%s\", { metaKey: true });"; | |
| 211 #else | |
| 212 const char* new_tab_click_script_template = | |
| 213 "simulateClick(\"%s\", { ctrlKey: true });"; | |
| 214 #endif | |
| 215 std::string new_tab_click_script = base::StringPrintf( | |
| 216 new_tab_click_script_template, id_of_anchor_to_click); | |
| 217 EXPECT_TRUE(ExecuteScript(main_contents, new_tab_click_script)); | |
| 218 | |
| 219 // Wait for a new tab to appear (the whole point of this test). | |
| 220 new_contents = new_tab_observer.GetWebContents(); | |
| 221 } | |
| 222 | |
| 223 // Verify that the new tab has the right contents and is in the right, new | |
| 224 // place in the tab strip. | |
| 225 EXPECT_TRUE(WaitForLoadStop(new_contents)); | |
| 226 int last_tab_index = browser()->tab_strip_model()->count() - 1; | |
| 227 EXPECT_LT(1, browser()->tab_strip_model()->count()); // More than 1 tab? | |
| 228 EXPECT_EQ(new_contents, | |
| 229 browser()->tab_strip_model()->GetWebContentsAt(last_tab_index)); | |
| 230 GURL expected_url(embedded_test_server()->GetURL("/title1.html")); | |
| 231 EXPECT_EQ(expected_url, new_contents->GetLastCommittedURL()); | |
| 232 | |
| 233 // Verify that the new tab is in a different process, SiteInstance and | |
| 234 // BrowsingInstance from the old contents. | |
| 235 EXPECT_NE(main_contents->GetMainFrame()->GetProcess(), | |
| 236 new_contents->GetMainFrame()->GetProcess()); | |
| 237 EXPECT_NE(main_contents->GetMainFrame()->GetSiteInstance(), | |
| 238 new_contents->GetMainFrame()->GetSiteInstance()); | |
| 239 EXPECT_FALSE(main_contents->GetSiteInstance()->IsRelatedSiteInstance( | |
| 240 new_contents->GetSiteInstance())); | |
| 241 | |
| 242 // Verify that the new BrowsingInstance can't see windows from the old one. | |
| 243 { | |
| 244 // Double-check that main_contents has expected window.name set. | |
| 245 // This is a sanity check of test setup; this is not a product test. | |
| 246 std::string name_of_main_contents_window; | |
| 247 EXPECT_TRUE(ExecuteScriptAndExtractString( | |
| 248 main_contents, "window.domAutomationController.send(window.name)", | |
| 249 &name_of_main_contents_window)); | |
| 250 EXPECT_EQ("main_contents", name_of_main_contents_window); | |
| 251 | |
| 252 // Verify that the new contents doesn't have a window.opener set. | |
| 253 bool window_opener_cast_to_bool; | |
| 254 EXPECT_TRUE(ExecuteScriptAndExtractBool( | |
| 255 new_contents, "window.domAutomationController.send(!!window.opener)", | |
| 256 &window_opener_cast_to_bool)); | |
| 257 EXPECT_FALSE(window_opener_cast_to_bool); | |
| 258 | |
| 259 // Verify that the new contents cannot find the old contents via | |
| 260 // window.open. (i.e. window.open should open a new window, rather than | |
| 261 // returning a reference to main_contents / old window). | |
| 262 std::string location_of_opened_window; | |
| 263 EXPECT_TRUE(ExecuteScriptAndExtractString( | |
| 264 new_contents, | |
| 265 "w = window.open('', 'main_contents');" | |
| 266 "window.domAutomationController.send(w.location.href);", | |
| 267 &location_of_opened_window)); | |
| 268 EXPECT_EQ(url::kAboutBlankURL, location_of_opened_window); | |
| 269 } | |
| 270 | |
| 271 return new_contents; | |
| 272 } | |
| 273 }; | |
| 274 | |
| 275 IN_PROC_BROWSER_TEST_F(CtrlClickShouldEndUpInNewProcessTest, NoTarget) { | |
| 276 // Navigate to the test page. | |
| 277 GURL main_url(embedded_test_server()->GetURL( | |
| 278 "/frame_tree/anchor_to_same_site_location.html")); | |
| 279 ui_test_utils::NavigateToURL(browser(), main_url); | |
| 280 const char* kIdOfAnchorToClick = "test-anchor-no-target"; | |
| 281 | |
| 282 // Verify that there is only 1 active tab (with the right contents committed). | |
| 283 EXPECT_EQ(0, browser()->tab_strip_model()->active_index()); | |
| 284 content::WebContents* main_contents = | |
| 285 browser()->tab_strip_model()->GetWebContentsAt(0); | |
| 286 EXPECT_EQ(main_url, main_contents->GetLastCommittedURL()); | |
| 287 | |
| 288 // Test what happens after ctrl-click. SimulateCtrlClick will verify | |
| 289 // that |new_contents1| is in a separate process and browsing instance | |
| 290 // from |main_contents|. | |
| 291 content::WebContents* new_contents1 = | |
| 292 SimulateCtrlClick(main_contents, kIdOfAnchorToClick); | |
| 293 | |
| 294 // Test that each subsequent ctrl-click also gets a new process. | |
|
Charlie Reis
2017/04/19 20:11:11
Nice-- I like this (especially given the unexpecte
| |
| 295 content::WebContents* new_contents2 = | |
| 296 SimulateCtrlClick(main_contents, kIdOfAnchorToClick); | |
| 297 EXPECT_NE(new_contents1->GetMainFrame()->GetProcess(), | |
| 298 new_contents2->GetMainFrame()->GetProcess()); | |
| 299 EXPECT_NE(new_contents1->GetMainFrame()->GetSiteInstance(), | |
| 300 new_contents2->GetMainFrame()->GetSiteInstance()); | |
| 301 EXPECT_FALSE(new_contents1->GetSiteInstance()->IsRelatedSiteInstance( | |
| 302 new_contents2->GetSiteInstance())); | |
| 303 } | |
| 304 | |
| 192 class ChromeNavigationPortMappedBrowserTest : public InProcessBrowserTest { | 305 class ChromeNavigationPortMappedBrowserTest : public InProcessBrowserTest { |
| 193 public: | 306 public: |
| 194 ChromeNavigationPortMappedBrowserTest() {} | 307 ChromeNavigationPortMappedBrowserTest() {} |
| 195 ~ChromeNavigationPortMappedBrowserTest() override {} | 308 ~ChromeNavigationPortMappedBrowserTest() override {} |
| 196 | 309 |
| 197 void SetUpCommandLine(base::CommandLine* command_line) override { | 310 void SetUpCommandLine(base::CommandLine* command_line) override { |
| 198 ASSERT_TRUE(embedded_test_server()->Start()); | 311 ASSERT_TRUE(embedded_test_server()->Start()); |
| 199 | 312 |
| 200 // Use the command line parameter for the host resolver, so URLs without | 313 // Use the command line parameter for the host resolver, so URLs without |
| 201 // explicit port numbers can be mapped under the hood to the port number | 314 // explicit port numbers can be mapped under the hood to the port number |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 250 browser()->tab_strip_model()->GetWebContentsAt( | 363 browser()->tab_strip_model()->GetWebContentsAt( |
| 251 browser()->tab_strip_model()->count() - 1); | 364 browser()->tab_strip_model()->count() - 1); |
| 252 WaitForLoadStop(new_web_contents); | 365 WaitForLoadStop(new_web_contents); |
| 253 | 366 |
| 254 // If the test is unsuccessful, the return value from GetLastCommittedURL | 367 // If the test is unsuccessful, the return value from GetLastCommittedURL |
| 255 // will be the virtual URL for the created NavigationEntry. | 368 // will be the virtual URL for the created NavigationEntry. |
| 256 // Note: Before the bug was fixed, the URL was the new_tab_url with a scheme | 369 // Note: Before the bug was fixed, the URL was the new_tab_url with a scheme |
| 257 // prepended and one less ":" character after the host. | 370 // prepended and one less ":" character after the host. |
| 258 EXPECT_EQ(GURL(), new_web_contents->GetLastCommittedURL()); | 371 EXPECT_EQ(GURL(), new_web_contents->GetLastCommittedURL()); |
| 259 } | 372 } |
| OLD | NEW |