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

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: Refactored tests 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
« no previous file with comments | « no previous file | chrome/test/data/extensions/loop_audio.html » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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..e05767c5d7ee5e449a2d52bff2548999adfcffab 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,8 @@
#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"
using content::RenderViewHost;
using content::RenderWidgetHost;
@@ -224,6 +228,62 @@ class ChromeRenderProcessHostTestWithCommandLine
}
};
+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.
+ ui_test_utils::NavigateToURL(browser(), audio_url_);
+ audio_tab_web_content_ =
+ browser()->tab_strip_model()->GetActiveWebContents();
+
+ // Get the process corresponding to the pages and create a new tab for the
+ // no audio page.
+ audio_process_ = ProcessFromHandle(
+ audio_tab_web_content_->GetRenderProcessHost()->GetHandle());
+ no_audio_process_ = ShowSingletonTab(no_audio_url_);
+
+ 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)
+ MachBroker* broker = 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_;
+};
+
// Disable on Mac and Windows due to ongoing flakiness. (crbug.com/442785)
#if defined(OS_MACOSX) || defined(OS_WIN)
#define MAYBE_ProcessPerTab DISABLED_ProcessPerTab
@@ -531,3 +591,95 @@ IN_PROC_BROWSER_TEST_F(ChromeRenderProcessHostTest,
observer.Wait();
}
+
+// 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.
+ 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 not visible 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.
+ content::ExecuteScript(audio_tab_web_content_,
gab 2015/08/18 01:48:05 Need to ASSERT_TRUE on content::ExecuteScript. Th
sebsg 2015/08/18 20:20:15 Done.
+ "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.
+ 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.
+ 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') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698