Index: content/browser/web_contents/web_contents_impl_unittest.cc |
diff --git a/content/browser/web_contents/web_contents_impl_unittest.cc b/content/browser/web_contents/web_contents_impl_unittest.cc |
index e2ef0e578ae3c1c8ea288b508e7229fdf1343233..d227e78b403db99221d29bf56bb095680dd309e0 100644 |
--- a/content/browser/web_contents/web_contents_impl_unittest.cc |
+++ b/content/browser/web_contents/web_contents_impl_unittest.cc |
@@ -7,6 +7,7 @@ |
#include "content/browser/frame_host/cross_site_transferring_request.h" |
#include "content/browser/frame_host/interstitial_page_impl.h" |
#include "content/browser/frame_host/navigation_entry_impl.h" |
+#include "content/browser/media/audio_stream_monitor.h" |
#include "content/browser/renderer_host/render_view_host_impl.h" |
#include "content/browser/site_instance_impl.h" |
#include "content/browser/webui/web_ui_controller_factory_registry.h" |
@@ -2685,4 +2686,79 @@ TEST_F(WebContentsImplTest, ActiveContentsCountChangeBrowsingInstance) { |
EXPECT_EQ(0u, instance_webui->GetRelatedActiveContentsCount()); |
} |
+TEST_F(WebContentsImplTest, MediaPowerSaveBlocking) { |
+ // PlayerIDs are actually pointers cast to int64, so verify that both negative |
+ // and positive player ids don't blow up. |
+ const int kPlayerAudioVideoId = 15; |
+ const int kPlayerAudioOnlyId = -15; |
+ const int kPlayerVideoOnlyId = 30; |
+ |
+ EXPECT_FALSE(contents()->has_audio_power_save_blocker_for_testing()); |
+ EXPECT_FALSE(contents()->has_video_power_save_blocker_for_testing()); |
+ |
+ TestRenderFrameHost* rfh = contents()->GetMainFrame(); |
+ AudioStreamMonitor::CreateForWebContents(contents()); |
+ AudioStreamMonitor* monitor = AudioStreamMonitor::FromWebContents(contents()); |
+ |
+ // The audio power save blocker should not be based on having a media player |
+ // when the tab audio indicator is available. |
+ if (AudioStreamMonitor::tab_audio_indicator_available()) { |
+ // Send a fake tab audio indicator notification. The audio power save |
+ // blocker should be created. |
+ monitor->set_was_recently_audible_for_testing(true); |
+ contents()->NotifyNavigationStateChanged(INVALIDATE_TYPE_TAB); |
+ EXPECT_TRUE(contents()->has_audio_power_save_blocker_for_testing()); |
+ |
+ // Toggle the tab audio indicator off. |
+ monitor->set_was_recently_audible_for_testing(false); |
+ contents()->NotifyNavigationStateChanged(INVALIDATE_TYPE_TAB); |
+ EXPECT_FALSE(contents()->has_audio_power_save_blocker_for_testing()); |
+ } |
+ |
+ // Start a player with both audio and video. A video power save blocker |
+ // should be created. If the tab audio indicator is available, an audio power |
+ // save blocker should be created too. |
+ rfh->OnMessageReceived(FrameHostMsg_MediaPlayingNotification( |
+ 0, kPlayerAudioVideoId, true, true)); |
+ EXPECT_TRUE(contents()->has_video_power_save_blocker_for_testing()); |
+ EXPECT_EQ(contents()->has_audio_power_save_blocker_for_testing(), |
+ !AudioStreamMonitor::tab_audio_indicator_available()); |
+ |
+ // Start another player that only has audio. There should be no change in |
+ // the power save blockers. |
+ rfh->OnMessageReceived(FrameHostMsg_MediaPlayingNotification( |
+ 0, kPlayerAudioOnlyId, false, true)); |
+ EXPECT_TRUE(contents()->has_video_power_save_blocker_for_testing()); |
+ EXPECT_EQ(contents()->has_audio_power_save_blocker_for_testing(), |
+ !AudioStreamMonitor::tab_audio_indicator_available()); |
+ |
+ // Start another player that only has video. There should be no change in |
+ // the power save blockers. |
+ rfh->OnMessageReceived(FrameHostMsg_MediaPlayingNotification( |
+ 0, kPlayerVideoOnlyId, true, false)); |
+ EXPECT_TRUE(contents()->has_video_power_save_blocker_for_testing()); |
+ EXPECT_EQ(contents()->has_audio_power_save_blocker_for_testing(), |
+ !AudioStreamMonitor::tab_audio_indicator_available()); |
+ |
+ // Destroy the original audio video player. Both power save blockers should |
+ // remain. |
+ rfh->OnMessageReceived( |
+ FrameHostMsg_MediaPausedNotification(0, kPlayerAudioVideoId)); |
+ EXPECT_TRUE(contents()->has_video_power_save_blocker_for_testing()); |
+ EXPECT_EQ(contents()->has_audio_power_save_blocker_for_testing(), |
+ !AudioStreamMonitor::tab_audio_indicator_available()); |
+ |
+ // Destroy the audio only player. The video power save blocker should remain. |
+ rfh->OnMessageReceived( |
+ FrameHostMsg_MediaPausedNotification(0, kPlayerAudioOnlyId)); |
+ EXPECT_TRUE(contents()->has_video_power_save_blocker_for_testing()); |
+ EXPECT_FALSE(contents()->has_audio_power_save_blocker_for_testing()); |
+ |
+ // Destroy the video only player. No power save blockers should remain. |
+ rfh->OnMessageReceived( |
+ FrameHostMsg_MediaPausedNotification(0, kPlayerVideoOnlyId)); |
+ EXPECT_FALSE(contents()->has_video_power_save_blocker_for_testing()); |
+ EXPECT_FALSE(contents()->has_audio_power_save_blocker_for_testing()); |
+} |
+ |
} // namespace content |