1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/strings/utf_string_conversions.h"
6 #include "build/build_config.h"
7 #include "chrome/browser/ui/browser.h"
8 #include "chrome/browser/ui/tabs/tab_strip_model.h"
9 #include "chrome/test/base/in_process_browser_test.h"
10 #include "chrome/test/base/ui_test_utils.h"
11 #include "content/public/test/browser_test_utils.h"
12 #include "media/base/test_data_util.h"
14 // TODO(dalecurtis): Android does not correctly defer media today, fix and
15 // enable this test (needs disable of user gesture). http://crbug.com/522157
16 #if !defined(OS_ANDROID)
17 typedef InProcessBrowserTest MediaBrowserTest
;
19 IN_PROC_BROWSER_TEST_F(MediaBrowserTest
, BackgroundMediaIsDeferred
) {
20 // Navigate to a video file, which would autoplay in the foreground, but won't
21 // in the background due to deferred media loading for hidden tabs.
22 ui_test_utils::NavigateToURLWithDisposition(
23 browser(), content::GetFileUrlWithQuery(
24 media::GetTestDataFilePath("bear-640x360.webm"), ""),
25 NEW_BACKGROUND_TAB
, ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION
);
27 ASSERT_EQ(2, browser()->tab_strip_model()->count());
28 content::WebContents
* background_contents
=
29 browser()->tab_strip_model()->GetWebContentsAt(1);
31 content::WaitForRenderFrameReady(background_contents
->GetMainFrame()));
32 EXPECT_NE(background_contents
,
33 browser()->tab_strip_model()->GetActiveWebContents());
35 // If autoplay for background tabs isn't deferred the play event listener will
36 // be attached too late to catch the event, and subsequently the test will hit
37 // the ended event before the play event.
39 content::ExecuteScript(background_contents
,
40 "var video = document.querySelector('video');"
41 "video.addEventListener('ended', function(event) {"
42 " document.title = 'ended';"
44 "video.addEventListener('play', function(event) {"
45 " document.title = 'playing';"
48 // Make the background tab w/ our video the active tab.
49 browser()->tab_strip_model()->SelectNextTab();
50 EXPECT_EQ(background_contents
,
51 browser()->tab_strip_model()->GetActiveWebContents());
53 // If everything worked, we should see "playing" and not "ended".
54 const base::string16 playing_str
= base::UTF8ToUTF16("playing");
55 const base::string16 ended_str
= base::UTF8ToUTF16("ended");
56 content::TitleWatcher
watcher(background_contents
, playing_str
);
57 watcher
.AlsoWaitForTitle(ended_str
);
58 EXPECT_EQ(playing_str
, watcher
.WaitAndGetTitle());
60 #endif // !defined(OS_ANDROID)