Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(809)

Unified Diff: chrome/browser/renderer_host/render_process_host_chrome_browsertest.cc

Issue 1214883004: Fixed the audio backgrounding bug (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed gab's comments Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/renderer_host/render_process_host_chrome_browsertest.cc
diff --git a/chrome/browser/renderer_host/render_process_host_chrome_browsertest.cc b/chrome/browser/renderer_host/render_process_host_chrome_browsertest.cc
index 24c58ef9a6acd950d566e73f5f62e0f5225ad8ed..e68293c04970db6fd5e60d717e420473c0f987fc 100644
--- a/chrome/browser/renderer_host/render_process_host_chrome_browsertest.cc
+++ b/chrome/browser/renderer_host/render_process_host_chrome_browsertest.cc
@@ -3,7 +3,9 @@
// found in the LICENSE file.
#include "base/command_line.h"
+#include "base/path_service.h"
#include "base/process/process.h"
+#include "base/test/test_timeouts.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/devtools/devtools_window.h"
#include "chrome/browser/search/search.h"
@@ -23,6 +25,12 @@
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_observer.h"
#include "content/public/test/browser_test_utils.h"
+#include "net/base/filename_util.h"
+#include "net/test/embedded_test_server/embedded_test_server.h"
+
+#if defined(OS_MACOSX)
+#include "content/browser/mach_broker_mac.h"
+#endif
using content::RenderViewHost;
using content::RenderWidgetHost;
@@ -531,3 +539,163 @@ IN_PROC_BROWSER_TEST_F(ChromeRenderProcessHostTest,
observer.Wait();
}
+
+// 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.
+// called "no audio" in foreground and another called "audio" in background with
+// audio in playing state. Also sets up the variables containing the process
+// associated with each tab, the urls of the two pages and the WebContents of
+// the "audio" page.
+class ChromeRenderProcessHostBackgroundingTest
+ : public ChromeRenderProcessHostTest {
+ public:
+ ChromeRenderProcessHostBackgroundingTest() {}
+
+ void SetUpCommandLine(base::CommandLine* command_line) override {
+ command_line->AppendSwitch(switches::kProcessPerTab);
+ }
+
+ void SetUpOnMainThread() override {
+ ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
+
+ // Set up the server and get the test pages.
+ base::FilePath test_data_dir;
+ ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &test_data_dir));
+ embedded_test_server()->ServeFilesFromDirectory(
+ test_data_dir.AppendASCII("chrome/test/data/"));
+ audio_url_ = embedded_test_server()->GetURL("/extensions/loop_audio.html");
+ no_audio_url_ = embedded_test_server()->GetURL("/title1.html");
+
+ // 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.
+ ui_test_utils::NavigateToURL(browser(), audio_url_);
+ audio_tab_web_content_ =
+ browser()->tab_strip_model()->GetActiveWebContents();
+
+ // Create a new tab for the no audio page and get the processes
+ // corresponding to each page.
+ no_audio_process_ = ShowSingletonTab(no_audio_url_);
+ audio_process_ = ProcessFromHandle(
+ audio_tab_web_content_->GetRenderProcessHost()->GetHandle());
+
+ ASSERT_TRUE(no_audio_process_.IsValid());
+ ASSERT_TRUE(audio_process_.IsValid());
+ ASSERT_NE(audio_process_.Pid(), no_audio_process_.Pid());
+ }
+
+ protected:
+ bool IsProcessBackgroundedHelper(const base::Process& process) {
+#if defined(OS_MACOSX)
+ content::MachBroker* broker = content::MachBroker::GetInstance();
+ mach_port_t task_port = broker->TaskForPid(process.Pid());
+ return process.IsProcessBackgrounded(task_port);
+#else
+ return process.IsProcessBackgrounded();
+#endif // defined(OS_MACOSX)
+ }
+
+ base::Process audio_process_;
+ base::Process no_audio_process_;
+
+ GURL audio_url_;
+ GURL no_audio_url_;
+
+ content::WebContents* audio_tab_web_content_;
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(ChromeRenderProcessHostBackgroundingTest);
+};
+
+// Test to make sure that a process is backgrounded when the audio stops playing
+// from the active tab and there is an immediate tab switch.
+IN_PROC_BROWSER_TEST_F(ChromeRenderProcessHostBackgroundingTest,
+ ProcessPriorityAfterStoppedAudio) {
+ // This test is invalid on platforms that can't background.
+ if (!base::Process::CanBackgroundProcesses())
+ return;
+
+ ShowSingletonTab(audio_url_);
+
+ // Wait until the no audio page is backgrounded and the audio page is not
+ // backgrounded.
+ while (!IsProcessBackgroundedHelper(no_audio_process_) ||
+ IsProcessBackgroundedHelper(audio_process_)) {
+ base::RunLoop().RunUntilIdle();
+ base::PlatformThread::Sleep(TestTimeouts::tiny_timeout());
+ }
+
+ // Pause the audio and immediately switch to the no audio tab.
+ ASSERT_TRUE(content::ExecuteScript(
+ audio_tab_web_content_,
+ "document.getElementById('audioPlayer').pause();"));
+ ShowSingletonTab(no_audio_url_);
+
+ // Wait until the no audio page is not backgrounded and the audio page is
+ // backgrounded.
+ while (IsProcessBackgroundedHelper(no_audio_process_) ||
+ !IsProcessBackgroundedHelper(audio_process_)) {
+ base::RunLoop().RunUntilIdle();
+ base::PlatformThread::Sleep(TestTimeouts::tiny_timeout());
+ }
+}
+
+// Test to make sure that a process is backgrounded automatically when audio
+// stops playing from a hidden tab.
+IN_PROC_BROWSER_TEST_F(ChromeRenderProcessHostBackgroundingTest,
+ ProcessPriorityAfterAudioStopsOnNotVisibleTab) {
+ // This test is invalid on platforms that can't background.
+ if (!base::Process::CanBackgroundProcesses())
+ return;
+
+ // Wait until the two pages are not backgrounded.
+ while (IsProcessBackgroundedHelper(no_audio_process_) ||
+ IsProcessBackgroundedHelper(audio_process_)) {
+ base::RunLoop().RunUntilIdle();
+ base::PlatformThread::Sleep(TestTimeouts::tiny_timeout());
+ }
+
+ // Stop the audio.
+ ASSERT_TRUE(content::ExecuteScript(
+ audio_tab_web_content_,
+ "document.getElementById('audioPlayer').pause();"));
+
+ // Wait until the no audio page is not backgrounded and the audio page is
+ // backgrounded.
+ while (IsProcessBackgroundedHelper(no_audio_process_) ||
+ !IsProcessBackgroundedHelper(audio_process_)) {
+ base::RunLoop().RunUntilIdle();
+ base::PlatformThread::Sleep(TestTimeouts::tiny_timeout());
+ }
+}
+
+// Test to make sure that a process is un-backgrounded automatically when audio
+// starts playing from a backgrounded tab.
+IN_PROC_BROWSER_TEST_F(ChromeRenderProcessHostBackgroundingTest,
+ ProcessPriorityAfterAudioStartsFromBackgroundTab) {
+ // This test is invalid on platforms that can't background.
+ if (!base::Process::CanBackgroundProcesses())
+ return;
+
+ // Stop the audio.
+ ASSERT_TRUE(content::ExecuteScript(
+ audio_tab_web_content_,
+ "document.getElementById('audioPlayer').pause();"));
+
+ // Wait until the no audio page is not backgrounded and the audio page is
+ // backgrounded.
+ while (IsProcessBackgroundedHelper(no_audio_process_) ||
+ !IsProcessBackgroundedHelper(audio_process_)) {
+ base::RunLoop().RunUntilIdle();
+ base::PlatformThread::Sleep(TestTimeouts::tiny_timeout());
+ }
+
+ // Start the audio from the backgrounded tab.
+ ASSERT_TRUE(
+ content::ExecuteScript(audio_tab_web_content_,
+ "document.getElementById('audioPlayer').play();"));
+
+ // Wait until the two pages are not backgrounded.
+ while (IsProcessBackgroundedHelper(no_audio_process_) ||
+ IsProcessBackgroundedHelper(audio_process_)) {
+ base::RunLoop().RunUntilIdle();
+ base::PlatformThread::Sleep(TestTimeouts::tiny_timeout());
+ }
+}
« no previous file with comments | « no previous file | chrome/test/data/extensions/loop_audio.html » ('j') | content/browser/renderer_host/render_process_host_impl.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698