| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "base/path_service.h" | |
| 7 #include "base/process/process.h" | 6 #include "base/process/process.h" |
| 8 #include "base/test/test_timeouts.h" | |
| 9 #include "chrome/browser/chrome_notification_types.h" | 7 #include "chrome/browser/chrome_notification_types.h" |
| 10 #include "chrome/browser/devtools/devtools_window.h" | 8 #include "chrome/browser/devtools/devtools_window.h" |
| 11 #include "chrome/browser/search/search.h" | 9 #include "chrome/browser/search/search.h" |
| 12 #include "chrome/browser/ui/browser.h" | 10 #include "chrome/browser/ui/browser.h" |
| 13 #include "chrome/browser/ui/browser_commands.h" | 11 #include "chrome/browser/ui/browser_commands.h" |
| 14 #include "chrome/browser/ui/singleton_tabs.h" | 12 #include "chrome/browser/ui/singleton_tabs.h" |
| 15 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 13 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 16 #include "chrome/common/chrome_switches.h" | 14 #include "chrome/common/chrome_switches.h" |
| 17 #include "chrome/common/url_constants.h" | 15 #include "chrome/common/url_constants.h" |
| 18 #include "chrome/test/base/in_process_browser_test.h" | 16 #include "chrome/test/base/in_process_browser_test.h" |
| 19 #include "chrome/test/base/test_switches.h" | 17 #include "chrome/test/base/test_switches.h" |
| 20 #include "chrome/test/base/ui_test_utils.h" | 18 #include "chrome/test/base/ui_test_utils.h" |
| 21 #include "content/public/browser/notification_service.h" | 19 #include "content/public/browser/notification_service.h" |
| 22 #include "content/public/browser/render_process_host.h" | 20 #include "content/public/browser/render_process_host.h" |
| 23 #include "content/public/browser/render_view_host.h" | 21 #include "content/public/browser/render_view_host.h" |
| 24 #include "content/public/browser/render_widget_host_iterator.h" | 22 #include "content/public/browser/render_widget_host_iterator.h" |
| 25 #include "content/public/browser/web_contents.h" | 23 #include "content/public/browser/web_contents.h" |
| 26 #include "content/public/browser/web_contents_observer.h" | 24 #include "content/public/browser/web_contents_observer.h" |
| 27 #include "content/public/test/browser_test_utils.h" | 25 #include "content/public/test/browser_test_utils.h" |
| 28 #include "net/base/filename_util.h" | |
| 29 #include "net/test/embedded_test_server/embedded_test_server.h" | |
| 30 | 26 |
| 31 using content::RenderViewHost; | 27 using content::RenderViewHost; |
| 32 using content::RenderWidgetHost; | 28 using content::RenderWidgetHost; |
| 33 using content::WebContents; | 29 using content::WebContents; |
| 34 | 30 |
| 35 namespace { | 31 namespace { |
| 36 | 32 |
| 37 int RenderProcessHostCount() { | 33 int RenderProcessHostCount() { |
| 38 content::RenderProcessHost::iterator hosts = | 34 content::RenderProcessHost::iterator hosts = |
| 39 content::RenderProcessHost::AllHostsIterator(); | 35 content::RenderProcessHost::AllHostsIterator(); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 64 } | 60 } |
| 65 | 61 |
| 66 // TODO(rvargas) crbug.com/417532: Remove this code. | 62 // TODO(rvargas) crbug.com/417532: Remove this code. |
| 67 base::Process ProcessFromHandle(base::ProcessHandle handle) { | 63 base::Process ProcessFromHandle(base::ProcessHandle handle) { |
| 68 #if defined(OS_WIN) | 64 #if defined(OS_WIN) |
| 69 if (handle == GetCurrentProcess()) | 65 if (handle == GetCurrentProcess()) |
| 70 return base::Process::Current(); | 66 return base::Process::Current(); |
| 71 | 67 |
| 72 base::ProcessHandle out_handle; | 68 base::ProcessHandle out_handle; |
| 73 if (!::DuplicateHandle(GetCurrentProcess(), handle, GetCurrentProcess(), | 69 if (!::DuplicateHandle(GetCurrentProcess(), handle, GetCurrentProcess(), |
| 74 &out_handle, 0, FALSE, DUPLICATE_SAME_ACCESS)) { | 70 &out_handle, 0, FALSE, DUPLICATE_SAME_ACCESS)) { |
| 75 return base::Process(); | 71 return base::Process(); |
| 76 } | 72 } |
| 77 handle = out_handle; | 73 handle = out_handle; |
| 78 #endif // defined(OS_WIN) | 74 #endif // defined(OS_WIN) |
| 79 return base::Process(handle); | 75 return base::Process(handle); |
| 80 } | 76 } |
| 81 | 77 |
| 82 } // namespace | 78 } // namespace |
| 83 | 79 |
| 84 class ChromeRenderProcessHostTest : public InProcessBrowserTest { | 80 class ChromeRenderProcessHostTest : public InProcessBrowserTest { |
| 85 public: | 81 public: |
| 86 ChromeRenderProcessHostTest() {} | 82 ChromeRenderProcessHostTest() {} |
| 87 | 83 |
| 88 // Show a tab, activating the current one if there is one, and wait for | 84 // Show a tab, activating the current one if there is one, and wait for |
| 89 // the renderer process to be created or foregrounded, returning the process | 85 // the renderer process to be created or foregrounded, returning the process |
| 90 // handle. | 86 // handle. |
| 91 base::Process ShowSingletonTab(const GURL& page) { | 87 base::Process ShowSingletonTab(const GURL& page) { |
| 92 chrome::ShowSingletonTab(browser(), page); | 88 chrome::ShowSingletonTab(browser(), page); |
| 93 WebContents* wc = browser()->tab_strip_model()->GetActiveWebContents(); | 89 WebContents* wc = browser()->tab_strip_model()->GetActiveWebContents(); |
| 94 CHECK(wc->GetURL() == page); | 90 CHECK(wc->GetURL() == page); |
| 95 | 91 |
| 96 WaitForLauncherThread(); | 92 WaitForLauncherThread(); |
| 97 WaitForMessageProcessing(wc); | 93 WaitForMessageProcessing(wc); |
| 98 return ProcessFromHandle(wc->GetRenderProcessHost()->GetHandle()); | 94 return ProcessFromHandle(wc->GetRenderProcessHost()->GetHandle()); |
| 99 } | 95 } |
| 100 | 96 |
| 101 // Loads the given url in a new background tab and returns the handle of its | 97 // Loads the given url in a new background tab and returns the handle of its |
| 102 // renderer. | 98 // renderer. |
| 103 base::Process OpenBackgroundTab(const GURL& page) { | 99 base::Process OpenBackgroundTab(const GURL& page) { |
| 104 ui_test_utils::NavigateToURLWithDisposition( | 100 ui_test_utils::NavigateToURLWithDisposition(browser(), page, |
| 105 browser(), page, NEW_BACKGROUND_TAB, | 101 NEW_BACKGROUND_TAB, ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); |
| 106 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); | |
| 107 | 102 |
| 108 TabStripModel* tab_strip = browser()->tab_strip_model(); | 103 TabStripModel* tab_strip = browser()->tab_strip_model(); |
| 109 WebContents* wc = | 104 WebContents* wc = tab_strip->GetWebContentsAt( |
| 110 tab_strip->GetWebContentsAt(tab_strip->active_index() + 1); | 105 tab_strip->active_index() + 1); |
| 111 CHECK(wc->GetVisibleURL() == page); | 106 CHECK(wc->GetVisibleURL() == page); |
| 112 | 107 |
| 113 WaitForLauncherThread(); | 108 WaitForLauncherThread(); |
| 114 WaitForMessageProcessing(wc); | 109 WaitForMessageProcessing(wc); |
| 115 return ProcessFromHandle(wc->GetRenderProcessHost()->GetHandle()); | 110 return ProcessFromHandle(wc->GetRenderProcessHost()->GetHandle()); |
| 116 } | 111 } |
| 117 | 112 |
| 118 // Ensures that the backgrounding / foregrounding gets a chance to run. | 113 // Ensures that the backgrounding / foregrounding gets a chance to run. |
| 119 void WaitForLauncherThread() { | 114 void WaitForLauncherThread() { |
| 120 content::BrowserThread::PostTaskAndReply( | 115 content::BrowserThread::PostTaskAndReply( |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 213 EXPECT_EQ(tab_count, browser()->tab_strip_model()->count()); | 208 EXPECT_EQ(tab_count, browser()->tab_strip_model()->count()); |
| 214 tab1 = browser()->tab_strip_model()->GetWebContentsAt(tab_count - 1); | 209 tab1 = browser()->tab_strip_model()->GetWebContentsAt(tab_count - 1); |
| 215 rph3 = tab1->GetRenderProcessHost(); | 210 rph3 = tab1->GetRenderProcessHost(); |
| 216 EXPECT_EQ(tab1->GetURL(), bookmarks); | 211 EXPECT_EQ(tab1->GetURL(), bookmarks); |
| 217 EXPECT_EQ(host_count, RenderProcessHostCount()); | 212 EXPECT_EQ(host_count, RenderProcessHostCount()); |
| 218 EXPECT_NE(rph1, rph3); | 213 EXPECT_NE(rph1, rph3); |
| 219 EXPECT_NE(rph2, rph3); | 214 EXPECT_NE(rph2, rph3); |
| 220 } | 215 } |
| 221 }; | 216 }; |
| 222 | 217 |
| 218 |
| 223 class ChromeRenderProcessHostTestWithCommandLine | 219 class ChromeRenderProcessHostTestWithCommandLine |
| 224 : public ChromeRenderProcessHostTest { | 220 : public ChromeRenderProcessHostTest { |
| 225 protected: | 221 protected: |
| 226 void SetUpCommandLine(base::CommandLine* command_line) override { | 222 void SetUpCommandLine(base::CommandLine* command_line) override { |
| 227 command_line->AppendSwitchASCII(switches::kRendererProcessLimit, "1"); | 223 command_line->AppendSwitchASCII(switches::kRendererProcessLimit, "1"); |
| 228 } | 224 } |
| 229 }; | 225 }; |
| 230 | 226 |
| 231 // Disable on Mac and Windows due to ongoing flakiness. (crbug.com/442785) | 227 // Disable on Mac and Windows due to ongoing flakiness. (crbug.com/442785) |
| 232 #if defined(OS_MACOSX) || defined(OS_WIN) | 228 #if defined(OS_MACOSX) || defined(OS_WIN) |
| (...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 475 chrome::ToggleDevToolsWindow(browser(), DevToolsToggleAction::Toggle()); | 471 chrome::ToggleDevToolsWindow(browser(), DevToolsToggleAction::Toggle()); |
| 476 close_observer.Wait(); | 472 close_observer.Wait(); |
| 477 } | 473 } |
| 478 | 474 |
| 479 // This class's goal is to close the browser window when a renderer process has | 475 // This class's goal is to close the browser window when a renderer process has |
| 480 // crashed. It does so by monitoring WebContents for RenderProcessGone event and | 476 // crashed. It does so by monitoring WebContents for RenderProcessGone event and |
| 481 // closing the passed in TabStripModel. This is used in the following test case. | 477 // closing the passed in TabStripModel. This is used in the following test case. |
| 482 class WindowDestroyer : public content::WebContentsObserver { | 478 class WindowDestroyer : public content::WebContentsObserver { |
| 483 public: | 479 public: |
| 484 WindowDestroyer(content::WebContents* web_contents, TabStripModel* model) | 480 WindowDestroyer(content::WebContents* web_contents, TabStripModel* model) |
| 485 : content::WebContentsObserver(web_contents), tab_strip_model_(model) {} | 481 : content::WebContentsObserver(web_contents), |
| 482 tab_strip_model_(model) { |
| 483 } |
| 486 | 484 |
| 487 void RenderProcessGone(base::TerminationStatus status) override { | 485 void RenderProcessGone(base::TerminationStatus status) override { |
| 488 // Wait for the window to be destroyed, which will ensure all other | 486 // Wait for the window to be destroyed, which will ensure all other |
| 489 // RenderViewHost objects are deleted before we return and proceed with | 487 // RenderViewHost objects are deleted before we return and proceed with |
| 490 // the next iteration of notifications. | 488 // the next iteration of notifications. |
| 491 content::WindowedNotificationObserver observer( | 489 content::WindowedNotificationObserver observer( |
| 492 chrome::NOTIFICATION_BROWSER_CLOSED, | 490 chrome::NOTIFICATION_BROWSER_CLOSED, |
| 493 content::NotificationService::AllSources()); | 491 content::NotificationService::AllSources()); |
| 494 tab_strip_model_->CloseAllTabs(); | 492 tab_strip_model_->CloseAllTabs(); |
| 495 observer.Wait(); | 493 observer.Wait(); |
| (...skipping 30 matching lines...) Expand all Loading... |
| 526 chrome::NOTIFICATION_BROWSER_CLOSED, | 524 chrome::NOTIFICATION_BROWSER_CLOSED, |
| 527 content::NotificationService::AllSources()); | 525 content::NotificationService::AllSources()); |
| 528 | 526 |
| 529 // Kill the renderer process, simulating a crash. This should the ProcessDied | 527 // Kill the renderer process, simulating a crash. This should the ProcessDied |
| 530 // method to be called. Alternatively, RenderProcessHost::OnChannelError can | 528 // method to be called. Alternatively, RenderProcessHost::OnChannelError can |
| 531 // be called to directly force a call to ProcessDied. | 529 // be called to directly force a call to ProcessDied. |
| 532 wc1->GetRenderProcessHost()->Shutdown(-1, true); | 530 wc1->GetRenderProcessHost()->Shutdown(-1, true); |
| 533 | 531 |
| 534 observer.Wait(); | 532 observer.Wait(); |
| 535 } | 533 } |
| 536 | |
| 537 // Sets up the browser in order to start the tests with two tabs open: one | |
| 538 // called "no audio" in foreground and another called "audio" in background with | |
| 539 // audio in playing state. Also sets up the variables containing the process | |
| 540 // associated with each tab, the urls of the two pages and the WebContents of | |
| 541 // the "audio" page. | |
| 542 class ChromeRenderProcessHostBackgroundingTest | |
| 543 : public ChromeRenderProcessHostTest { | |
| 544 public: | |
| 545 ChromeRenderProcessHostBackgroundingTest() {} | |
| 546 | |
| 547 void SetUpCommandLine(base::CommandLine* command_line) override { | |
| 548 command_line->AppendSwitch(switches::kProcessPerTab); | |
| 549 } | |
| 550 | |
| 551 void SetUpOnMainThread() override { | |
| 552 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); | |
| 553 | |
| 554 // Set up the server and get the test pages. | |
| 555 base::FilePath test_data_dir; | |
| 556 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &test_data_dir)); | |
| 557 embedded_test_server()->ServeFilesFromDirectory( | |
| 558 test_data_dir.AppendASCII("chrome/test/data/")); | |
| 559 audio_url_ = embedded_test_server()->GetURL("/extensions/loop_audio.html"); | |
| 560 no_audio_url_ = embedded_test_server()->GetURL("/title1.html"); | |
| 561 | |
| 562 // Open a browser, navigate to the audio page and get its WebContents. | |
| 563 ui_test_utils::NavigateToURL(browser(), audio_url_); | |
| 564 audio_tab_web_contents_ = | |
| 565 browser()->tab_strip_model()->GetActiveWebContents(); | |
| 566 | |
| 567 // Create a new tab for the no audio page and confirm that the process of | |
| 568 // each tab is different and that both are valid. | |
| 569 audio_process_ = ProcessFromHandle( | |
| 570 audio_tab_web_contents_->GetRenderProcessHost()->GetHandle()); | |
| 571 no_audio_process_ = ShowSingletonTab(no_audio_url_); | |
| 572 ASSERT_NE(audio_process_.Pid(), no_audio_process_.Pid()); | |
| 573 ASSERT_TRUE(no_audio_process_.IsValid()); | |
| 574 ASSERT_TRUE(audio_process_.IsValid()); | |
| 575 } | |
| 576 | |
| 577 protected: | |
| 578 GURL audio_url_; | |
| 579 GURL no_audio_url_; | |
| 580 | |
| 581 base::Process audio_process_; | |
| 582 base::Process no_audio_process_; | |
| 583 | |
| 584 content::WebContents* audio_tab_web_contents_; | |
| 585 | |
| 586 private: | |
| 587 DISALLOW_COPY_AND_ASSIGN(ChromeRenderProcessHostBackgroundingTest); | |
| 588 }; | |
| 589 | |
| 590 // Test to make sure that a process is backgrounded when the audio stops playing | |
| 591 // from the active tab and there is an immediate tab switch. | |
| 592 IN_PROC_BROWSER_TEST_F(ChromeRenderProcessHostBackgroundingTest, | |
| 593 ProcessPriorityAfterStoppedAudio) { | |
| 594 // This test is invalid on platforms that can't background. | |
| 595 if (!base::Process::CanBackgroundProcesses()) | |
| 596 return; | |
| 597 | |
| 598 ShowSingletonTab(audio_url_); | |
| 599 | |
| 600 // Wait until the no audio page is backgrounded and the audio page is not | |
| 601 // backgrounded. | |
| 602 while (!no_audio_process_.IsProcessBackgrounded() || | |
| 603 audio_process_.IsProcessBackgrounded()) { | |
| 604 base::RunLoop().RunUntilIdle(); | |
| 605 base::PlatformThread::Sleep(TestTimeouts::tiny_timeout()); | |
| 606 } | |
| 607 | |
| 608 // Pause the audio and immediately switch to the no audio tab. | |
| 609 ASSERT_TRUE(content::ExecuteScript( | |
| 610 audio_tab_web_contents_, | |
| 611 "document.getElementById('audioPlayer').pause();")); | |
| 612 ShowSingletonTab(no_audio_url_); | |
| 613 | |
| 614 // Wait until the no audio page is not backgrounded and the audio page is | |
| 615 // backgrounded. | |
| 616 while (no_audio_process_.IsProcessBackgrounded() || | |
| 617 !audio_process_.IsProcessBackgrounded()) { | |
| 618 base::RunLoop().RunUntilIdle(); | |
| 619 base::PlatformThread::Sleep(TestTimeouts::tiny_timeout()); | |
| 620 } | |
| 621 } | |
| 622 | |
| 623 // Test to make sure that a process is backgrounded automatically when audio | |
| 624 // stops playing from a hidden tab. | |
| 625 IN_PROC_BROWSER_TEST_F(ChromeRenderProcessHostBackgroundingTest, | |
| 626 ProcessPriorityAfterAudioStopsOnNotVisibleTab) { | |
| 627 // This test is invalid on platforms that can't background. | |
| 628 if (!base::Process::CanBackgroundProcesses()) | |
| 629 return; | |
| 630 | |
| 631 // Wait until the two pages are not backgrounded. | |
| 632 while (no_audio_process_.IsProcessBackgrounded() || | |
| 633 audio_process_.IsProcessBackgrounded()) { | |
| 634 base::RunLoop().RunUntilIdle(); | |
| 635 base::PlatformThread::Sleep(TestTimeouts::tiny_timeout()); | |
| 636 } | |
| 637 | |
| 638 // Stop the audio. | |
| 639 ASSERT_TRUE(content::ExecuteScript( | |
| 640 audio_tab_web_contents_, | |
| 641 "document.getElementById('audioPlayer').pause();")); | |
| 642 | |
| 643 // Wait until the no audio page is not backgrounded and the audio page is | |
| 644 // backgrounded. | |
| 645 while (no_audio_process_.IsProcessBackgrounded() || | |
| 646 !audio_process_.IsProcessBackgrounded()) { | |
| 647 base::RunLoop().RunUntilIdle(); | |
| 648 base::PlatformThread::Sleep(TestTimeouts::tiny_timeout()); | |
| 649 } | |
| 650 } | |
| 651 | |
| 652 // Test to make sure that a process is un-backgrounded automatically when audio | |
| 653 // starts playing from a backgrounded tab. | |
| 654 IN_PROC_BROWSER_TEST_F(ChromeRenderProcessHostBackgroundingTest, | |
| 655 ProcessPriorityAfterAudioStartsFromBackgroundTab) { | |
| 656 // This test is invalid on platforms that can't background. | |
| 657 if (!base::Process::CanBackgroundProcesses()) | |
| 658 return; | |
| 659 | |
| 660 // Stop the audio. | |
| 661 ASSERT_TRUE(content::ExecuteScript( | |
| 662 audio_tab_web_contents_, | |
| 663 "document.getElementById('audioPlayer').pause();")); | |
| 664 | |
| 665 // Wait until the no audio page is not backgrounded and the audio page is | |
| 666 // backgrounded. | |
| 667 while (no_audio_process_.IsProcessBackgrounded() || | |
| 668 !audio_process_.IsProcessBackgrounded()) { | |
| 669 base::RunLoop().RunUntilIdle(); | |
| 670 base::PlatformThread::Sleep(TestTimeouts::tiny_timeout()); | |
| 671 } | |
| 672 | |
| 673 // Start the audio from the backgrounded tab. | |
| 674 ASSERT_TRUE( | |
| 675 content::ExecuteScript(audio_tab_web_contents_, | |
| 676 "document.getElementById('audioPlayer').play();")); | |
| 677 | |
| 678 // Wait until the two pages are not backgrounded. | |
| 679 while (no_audio_process_.IsProcessBackgrounded() || | |
| 680 audio_process_.IsProcessBackgrounded()) { | |
| 681 base::RunLoop().RunUntilIdle(); | |
| 682 base::PlatformThread::Sleep(TestTimeouts::tiny_timeout()); | |
| 683 } | |
| 684 } | |
| OLD | NEW |