Chromium Code Reviews| 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" | |
| 6 #include "base/process/process.h" | 7 #include "base/process/process.h" |
| 8 #include "base/test/test_timeouts.h" | |
| 7 #include "chrome/browser/chrome_notification_types.h" | 9 #include "chrome/browser/chrome_notification_types.h" |
| 8 #include "chrome/browser/devtools/devtools_window.h" | 10 #include "chrome/browser/devtools/devtools_window.h" |
| 9 #include "chrome/browser/search/search.h" | 11 #include "chrome/browser/search/search.h" |
| 10 #include "chrome/browser/ui/browser.h" | 12 #include "chrome/browser/ui/browser.h" |
| 11 #include "chrome/browser/ui/browser_commands.h" | 13 #include "chrome/browser/ui/browser_commands.h" |
| 12 #include "chrome/browser/ui/singleton_tabs.h" | 14 #include "chrome/browser/ui/singleton_tabs.h" |
| 13 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 15 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 14 #include "chrome/common/chrome_switches.h" | 16 #include "chrome/common/chrome_switches.h" |
| 15 #include "chrome/common/url_constants.h" | 17 #include "chrome/common/url_constants.h" |
| 16 #include "chrome/test/base/in_process_browser_test.h" | 18 #include "chrome/test/base/in_process_browser_test.h" |
| 17 #include "chrome/test/base/test_switches.h" | 19 #include "chrome/test/base/test_switches.h" |
| 18 #include "chrome/test/base/ui_test_utils.h" | 20 #include "chrome/test/base/ui_test_utils.h" |
| 19 #include "content/public/browser/notification_service.h" | 21 #include "content/public/browser/notification_service.h" |
| 20 #include "content/public/browser/render_process_host.h" | 22 #include "content/public/browser/render_process_host.h" |
| 21 #include "content/public/browser/render_view_host.h" | 23 #include "content/public/browser/render_view_host.h" |
| 22 #include "content/public/browser/render_widget_host_iterator.h" | 24 #include "content/public/browser/render_widget_host_iterator.h" |
| 23 #include "content/public/browser/web_contents.h" | 25 #include "content/public/browser/web_contents.h" |
| 24 #include "content/public/browser/web_contents_observer.h" | 26 #include "content/public/browser/web_contents_observer.h" |
| 25 #include "content/public/test/browser_test_utils.h" | 27 #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 | |
| 31 #if defined(OS_MACOSX) | |
| 32 #include "content/browser/mach_broker_mac.h" | |
| 33 #endif | |
| 26 | 34 |
| 27 using content::RenderViewHost; | 35 using content::RenderViewHost; |
| 28 using content::RenderWidgetHost; | 36 using content::RenderWidgetHost; |
| 29 using content::WebContents; | 37 using content::WebContents; |
| 30 | 38 |
| 31 namespace { | 39 namespace { |
| 32 | 40 |
| 33 int RenderProcessHostCount() { | 41 int RenderProcessHostCount() { |
| 34 content::RenderProcessHost::iterator hosts = | 42 content::RenderProcessHost::iterator hosts = |
| 35 content::RenderProcessHost::AllHostsIterator(); | 43 content::RenderProcessHost::AllHostsIterator(); |
| (...skipping 488 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 524 chrome::NOTIFICATION_BROWSER_CLOSED, | 532 chrome::NOTIFICATION_BROWSER_CLOSED, |
| 525 content::NotificationService::AllSources()); | 533 content::NotificationService::AllSources()); |
| 526 | 534 |
| 527 // Kill the renderer process, simulating a crash. This should the ProcessDied | 535 // Kill the renderer process, simulating a crash. This should the ProcessDied |
| 528 // method to be called. Alternatively, RenderProcessHost::OnChannelError can | 536 // method to be called. Alternatively, RenderProcessHost::OnChannelError can |
| 529 // be called to directly force a call to ProcessDied. | 537 // be called to directly force a call to ProcessDied. |
| 530 wc1->GetRenderProcessHost()->Shutdown(-1, true); | 538 wc1->GetRenderProcessHost()->Shutdown(-1, true); |
| 531 | 539 |
| 532 observer.Wait(); | 540 observer.Wait(); |
| 533 } | 541 } |
| 542 | |
| 543 // Sets up the browser to end off control to the test with two tabs open: one | |
|
Avi (use Gerrit)
2015/08/19 00:30:29
"to end off control"? I can't parse that.
sebsg
2015/08/19 15:22:21
Done.
| |
| 544 // called "no audio" in foreground and another called "audio" in background with | |
| 545 // audio in playing state. Also sets up the variables containing the process | |
| 546 // associated with each tab, the urls of the two pages and the WebContents of | |
| 547 // the "audio" page. | |
| 548 class ChromeRenderProcessHostBackgroundingTest | |
| 549 : public ChromeRenderProcessHostTest { | |
| 550 public: | |
| 551 ChromeRenderProcessHostBackgroundingTest() {} | |
| 552 | |
| 553 void SetUpCommandLine(base::CommandLine* command_line) override { | |
| 554 command_line->AppendSwitch(switches::kProcessPerTab); | |
| 555 } | |
| 556 | |
| 557 void SetUpOnMainThread() override { | |
| 558 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); | |
| 559 | |
| 560 // Set up the server and get the test pages. | |
| 561 base::FilePath test_data_dir; | |
| 562 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &test_data_dir)); | |
| 563 embedded_test_server()->ServeFilesFromDirectory( | |
| 564 test_data_dir.AppendASCII("chrome/test/data/")); | |
| 565 audio_url_ = embedded_test_server()->GetURL("/extensions/loop_audio.html"); | |
| 566 no_audio_url_ = embedded_test_server()->GetURL("/title1.html"); | |
| 567 | |
| 568 // Open a browser, navigate to the audio page and get its WebContent. | |
|
Avi (use Gerrit)
2015/08/19 00:30:29
WebContents. It's a singular plural noun. Or somet
sebsg
2015/08/19 15:22:20
Done.
| |
| 569 ui_test_utils::NavigateToURL(browser(), audio_url_); | |
| 570 audio_tab_web_content_ = | |
| 571 browser()->tab_strip_model()->GetActiveWebContents(); | |
| 572 | |
| 573 // Create a new tab for the no audio page and get the processes | |
| 574 // corresponding to each page. | |
| 575 no_audio_process_ = ShowSingletonTab(no_audio_url_); | |
| 576 audio_process_ = ProcessFromHandle( | |
| 577 audio_tab_web_content_->GetRenderProcessHost()->GetHandle()); | |
| 578 | |
| 579 ASSERT_TRUE(no_audio_process_.IsValid()); | |
| 580 ASSERT_TRUE(audio_process_.IsValid()); | |
| 581 ASSERT_NE(audio_process_.Pid(), no_audio_process_.Pid()); | |
| 582 } | |
| 583 | |
| 584 protected: | |
| 585 bool IsProcessBackgroundedHelper(const base::Process& process) { | |
| 586 #if defined(OS_MACOSX) | |
| 587 content::MachBroker* broker = content::MachBroker::GetInstance(); | |
| 588 mach_port_t task_port = broker->TaskForPid(process.Pid()); | |
| 589 return process.IsProcessBackgrounded(task_port); | |
| 590 #else | |
| 591 return process.IsProcessBackgrounded(); | |
| 592 #endif // defined(OS_MACOSX) | |
| 593 } | |
| 594 | |
| 595 base::Process audio_process_; | |
| 596 base::Process no_audio_process_; | |
| 597 | |
| 598 GURL audio_url_; | |
| 599 GURL no_audio_url_; | |
| 600 | |
| 601 content::WebContents* audio_tab_web_content_; | |
| 602 | |
| 603 private: | |
| 604 DISALLOW_COPY_AND_ASSIGN(ChromeRenderProcessHostBackgroundingTest); | |
| 605 }; | |
| 606 | |
| 607 // Test to make sure that a process is backgrounded when the audio stops playing | |
| 608 // from the active tab and there is an immediate tab switch. | |
| 609 IN_PROC_BROWSER_TEST_F(ChromeRenderProcessHostBackgroundingTest, | |
| 610 ProcessPriorityAfterStoppedAudio) { | |
| 611 // This test is invalid on platforms that can't background. | |
| 612 if (!base::Process::CanBackgroundProcesses()) | |
| 613 return; | |
| 614 | |
| 615 ShowSingletonTab(audio_url_); | |
| 616 | |
| 617 // Wait until the no audio page is backgrounded and the audio page is not | |
| 618 // backgrounded. | |
| 619 while (!IsProcessBackgroundedHelper(no_audio_process_) || | |
| 620 IsProcessBackgroundedHelper(audio_process_)) { | |
| 621 base::RunLoop().RunUntilIdle(); | |
| 622 base::PlatformThread::Sleep(TestTimeouts::tiny_timeout()); | |
| 623 } | |
| 624 | |
| 625 // Pause the audio and immediately switch to the no audio tab. | |
| 626 ASSERT_TRUE(content::ExecuteScript( | |
| 627 audio_tab_web_content_, | |
| 628 "document.getElementById('audioPlayer').pause();")); | |
| 629 ShowSingletonTab(no_audio_url_); | |
| 630 | |
| 631 // Wait until the no audio page is not backgrounded and the audio page is | |
| 632 // backgrounded. | |
| 633 while (IsProcessBackgroundedHelper(no_audio_process_) || | |
| 634 !IsProcessBackgroundedHelper(audio_process_)) { | |
| 635 base::RunLoop().RunUntilIdle(); | |
| 636 base::PlatformThread::Sleep(TestTimeouts::tiny_timeout()); | |
| 637 } | |
| 638 } | |
| 639 | |
| 640 // Test to make sure that a process is backgrounded automatically when audio | |
| 641 // stops playing from a hidden tab. | |
| 642 IN_PROC_BROWSER_TEST_F(ChromeRenderProcessHostBackgroundingTest, | |
| 643 ProcessPriorityAfterAudioStopsOnNotVisibleTab) { | |
| 644 // This test is invalid on platforms that can't background. | |
| 645 if (!base::Process::CanBackgroundProcesses()) | |
| 646 return; | |
| 647 | |
| 648 // Wait until the two pages are not backgrounded. | |
| 649 while (IsProcessBackgroundedHelper(no_audio_process_) || | |
| 650 IsProcessBackgroundedHelper(audio_process_)) { | |
| 651 base::RunLoop().RunUntilIdle(); | |
| 652 base::PlatformThread::Sleep(TestTimeouts::tiny_timeout()); | |
| 653 } | |
| 654 | |
| 655 // Stop the audio. | |
| 656 ASSERT_TRUE(content::ExecuteScript( | |
| 657 audio_tab_web_content_, | |
| 658 "document.getElementById('audioPlayer').pause();")); | |
| 659 | |
| 660 // Wait until the no audio page is not backgrounded and the audio page is | |
| 661 // backgrounded. | |
| 662 while (IsProcessBackgroundedHelper(no_audio_process_) || | |
| 663 !IsProcessBackgroundedHelper(audio_process_)) { | |
| 664 base::RunLoop().RunUntilIdle(); | |
| 665 base::PlatformThread::Sleep(TestTimeouts::tiny_timeout()); | |
| 666 } | |
| 667 } | |
| 668 | |
| 669 // Test to make sure that a process is un-backgrounded automatically when audio | |
| 670 // starts playing from a backgrounded tab. | |
| 671 IN_PROC_BROWSER_TEST_F(ChromeRenderProcessHostBackgroundingTest, | |
| 672 ProcessPriorityAfterAudioStartsFromBackgroundTab) { | |
| 673 // This test is invalid on platforms that can't background. | |
| 674 if (!base::Process::CanBackgroundProcesses()) | |
| 675 return; | |
| 676 | |
| 677 // Stop the audio. | |
| 678 ASSERT_TRUE(content::ExecuteScript( | |
| 679 audio_tab_web_content_, | |
| 680 "document.getElementById('audioPlayer').pause();")); | |
| 681 | |
| 682 // Wait until the no audio page is not backgrounded and the audio page is | |
| 683 // backgrounded. | |
| 684 while (IsProcessBackgroundedHelper(no_audio_process_) || | |
| 685 !IsProcessBackgroundedHelper(audio_process_)) { | |
| 686 base::RunLoop().RunUntilIdle(); | |
| 687 base::PlatformThread::Sleep(TestTimeouts::tiny_timeout()); | |
| 688 } | |
| 689 | |
| 690 // Start the audio from the backgrounded tab. | |
| 691 ASSERT_TRUE( | |
| 692 content::ExecuteScript(audio_tab_web_content_, | |
| 693 "document.getElementById('audioPlayer').play();")); | |
| 694 | |
| 695 // Wait until the two pages are not backgrounded. | |
| 696 while (IsProcessBackgroundedHelper(no_audio_process_) || | |
| 697 IsProcessBackgroundedHelper(audio_process_)) { | |
| 698 base::RunLoop().RunUntilIdle(); | |
| 699 base::PlatformThread::Sleep(TestTimeouts::tiny_timeout()); | |
| 700 } | |
| 701 } | |
| OLD | NEW |