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 "content/browser/media/android/media_session.h"
10 #include "content/browser/media/android/media_session_observer.h"
11 #include "content/public/browser/web_contents.h"
12 #include "content/public/browser/web_contents_observer.h"
13 #include "content/public/test/content_browser_test.h"
14 #include "content/shell/browser/shell.h"
15 #include "testing/gmock/include/gmock/gmock.h"
17 using content::WebContents
;
18 using content::WebContentsObserver
;
19 using content::MediaSession
;
20 using content::MediaSessionObserver
;
22 using ::testing::Expectation
;
26 class MockMediaSessionObserver
: public MediaSessionObserver
{
28 MockMediaSessionObserver()
29 : received_resume_calls_(0),
30 received_suspend_calls_(0) {
33 ~MockMediaSessionObserver() override
= default;
35 // Implements MediaSessionObserver.
36 void OnSuspend(int player_id
) override
{
37 DCHECK(player_id
>= 0);
38 DCHECK(players_
.size() > static_cast<size_t>(player_id
));
40 ++received_suspend_calls_
;
41 players_
[player_id
] = false;
43 void OnResume(int player_id
) override
{
44 DCHECK(player_id
>= 0);
45 DCHECK(players_
.size() > static_cast<size_t>(player_id
));
47 ++received_resume_calls_
;
48 players_
[player_id
] = true;
51 int StartNewPlayer() {
52 players_
.push_back(true);
53 return players_
.size() - 1;
56 bool IsPlaying(size_t player_id
) {
57 DCHECK(players_
.size() > player_id
);
58 return players_
[player_id
];
61 void SetPlaying(size_t player_id
, bool playing
) {
62 DCHECK(players_
.size() > player_id
);
63 players_
[player_id
] = playing
;
66 int received_suspend_calls() const {
67 return received_suspend_calls_
;
70 int received_resume_calls() const {
71 return received_resume_calls_
;
75 // Basic representation of the players. The position in the vector is the
76 // player_id. The value of the vector is the playing status.
77 std::vector
<bool> players_
;
79 int received_resume_calls_
;
80 int received_suspend_calls_
;
83 class MockWebContentsObserver
: public WebContentsObserver
{
85 MockWebContentsObserver(WebContents
* web_contents
)
86 : WebContentsObserver(web_contents
) {}
88 MOCK_METHOD2(MediaSessionStateChanged
,
89 void(bool is_controllable
, bool is_suspended
));
94 class MediaSessionBrowserTest
: public content::ContentBrowserTest
{
96 MediaSessionBrowserTest() = default;
98 void SetUpOnMainThread() override
{
99 ContentBrowserTest::SetUpOnMainThread();
101 mock_web_contents_observer_
.reset(
102 new MockWebContentsObserver(shell()->web_contents()));
103 media_session_
= MediaSession::Get(shell()->web_contents());
104 media_session_
->ResetJavaRefForTest();
105 ASSERT_TRUE(media_session_
);
108 void TearDownOnMainThread() override
{
109 mock_web_contents_observer_
.reset();
111 media_session_
->RemoveAllPlayersForTest();
112 media_session_
= nullptr;
114 ContentBrowserTest::TearDownOnMainThread();
117 void StartNewPlayer(MockMediaSessionObserver
* media_session_observer
,
118 MediaSession::Type type
) {
119 bool result
= AddPlayer(
120 media_session_observer
,
121 media_session_observer
->StartNewPlayer(),
126 bool AddPlayer(MockMediaSessionObserver
* media_session_observer
,
128 MediaSession::Type type
) {
129 return media_session_
->AddPlayer(media_session_observer
, player_id
, type
);
132 void RemovePlayer(MockMediaSessionObserver
* media_session_observer
,
134 media_session_
->RemovePlayer(media_session_observer
, player_id
);
137 void RemovePlayers(MockMediaSessionObserver
* media_session_observer
) {
138 media_session_
->RemovePlayers(media_session_observer
);
141 void OnSuspendSession(bool isTemporary
) {
142 media_session_
->OnSuspend(nullptr, nullptr, isTemporary
);
145 void OnResumeSession() { media_session_
->OnResume(nullptr, nullptr); }
147 bool HasAudioFocus() { return media_session_
->IsActiveForTest(); }
149 MediaSession::Type
GetSessionType() {
150 return media_session_
->audio_focus_type_for_test();
153 bool IsControllable() { return media_session_
->IsControllable(); }
155 bool IsSuspended() { return media_session_
->IsSuspended(); }
158 media_session_
->OnResumeInternal(content::MediaSession::SuspendType::UI
);
161 void SystemResume() {
162 media_session_
->OnResumeInternal(
163 content::MediaSession::SuspendType::SYSTEM
);
167 media_session_
->OnSuspendInternal(content::MediaSession::SuspendType::UI
);
170 void SystemSuspend() {
171 media_session_
->OnSuspendInternal(
172 content::MediaSession::SuspendType::SYSTEM
);
175 MockWebContentsObserver
* mock_web_contents_observer() {
176 return mock_web_contents_observer_
.get();
180 MediaSession
* media_session_
;
181 scoped_ptr
<MockWebContentsObserver
> mock_web_contents_observer_
;
183 DISALLOW_COPY_AND_ASSIGN(MediaSessionBrowserTest
);
186 IN_PROC_BROWSER_TEST_F(MediaSessionBrowserTest
,
187 PlayersFromSameObserverDoNotStopEachOtherInSameSession
) {
188 scoped_ptr
<MockMediaSessionObserver
> media_session_observer(
189 new MockMediaSessionObserver
);
191 StartNewPlayer(media_session_observer
.get(), MediaSession::Type::Content
);
192 StartNewPlayer(media_session_observer
.get(), MediaSession::Type::Content
);
193 StartNewPlayer(media_session_observer
.get(), MediaSession::Type::Content
);
195 EXPECT_TRUE(media_session_observer
->IsPlaying(0));
196 EXPECT_TRUE(media_session_observer
->IsPlaying(1));
197 EXPECT_TRUE(media_session_observer
->IsPlaying(2));
200 IN_PROC_BROWSER_TEST_F(MediaSessionBrowserTest
,
201 PlayersFromManyObserverDoNotStopEachOtherInSameSession
) {
202 scoped_ptr
<MockMediaSessionObserver
> media_session_observer_1(
203 new MockMediaSessionObserver
);
204 scoped_ptr
<MockMediaSessionObserver
> media_session_observer_2(
205 new MockMediaSessionObserver
);
206 scoped_ptr
<MockMediaSessionObserver
> media_session_observer_3(
207 new MockMediaSessionObserver
);
209 StartNewPlayer(media_session_observer_1
.get(), MediaSession::Type::Content
);
211 StartNewPlayer(media_session_observer_2
.get(), MediaSession::Type::Content
);
212 StartNewPlayer(media_session_observer_3
.get(), MediaSession::Type::Content
);
214 EXPECT_TRUE(media_session_observer_1
->IsPlaying(0));
215 EXPECT_TRUE(media_session_observer_2
->IsPlaying(0));
216 EXPECT_TRUE(media_session_observer_3
->IsPlaying(0));
219 IN_PROC_BROWSER_TEST_F(MediaSessionBrowserTest
,
220 SuspendedMediaSessionStopsPlayers
) {
221 scoped_ptr
<MockMediaSessionObserver
> media_session_observer(
222 new MockMediaSessionObserver
);
224 StartNewPlayer(media_session_observer
.get(), MediaSession::Type::Content
);
225 StartNewPlayer(media_session_observer
.get(), MediaSession::Type::Content
);
226 StartNewPlayer(media_session_observer
.get(), MediaSession::Type::Content
);
228 OnSuspendSession(true);
230 EXPECT_FALSE(media_session_observer
->IsPlaying(0));
231 EXPECT_FALSE(media_session_observer
->IsPlaying(1));
232 EXPECT_FALSE(media_session_observer
->IsPlaying(2));
235 IN_PROC_BROWSER_TEST_F(MediaSessionBrowserTest
,
236 ResumedMediaSessionRestartsPlayers
) {
237 scoped_ptr
<MockMediaSessionObserver
> media_session_observer(
238 new MockMediaSessionObserver
);
240 StartNewPlayer(media_session_observer
.get(), MediaSession::Type::Content
);
241 StartNewPlayer(media_session_observer
.get(), MediaSession::Type::Content
);
242 StartNewPlayer(media_session_observer
.get(), MediaSession::Type::Content
);
244 OnSuspendSession(true);
247 EXPECT_TRUE(media_session_observer
->IsPlaying(0));
248 EXPECT_TRUE(media_session_observer
->IsPlaying(1));
249 EXPECT_TRUE(media_session_observer
->IsPlaying(2));
252 IN_PROC_BROWSER_TEST_F(MediaSessionBrowserTest
,
253 StartedPlayerOnSuspendedSessionPlaysAlone
) {
254 scoped_ptr
<MockMediaSessionObserver
> media_session_observer(
255 new MockMediaSessionObserver
);
257 StartNewPlayer(media_session_observer
.get(), MediaSession::Type::Content
);
259 EXPECT_TRUE(media_session_observer
->IsPlaying(0));
261 OnSuspendSession(true);
263 EXPECT_FALSE(media_session_observer
->IsPlaying(0));
265 StartNewPlayer(media_session_observer
.get(), MediaSession::Type::Content
);
267 EXPECT_FALSE(media_session_observer
->IsPlaying(0));
268 EXPECT_TRUE(media_session_observer
->IsPlaying(1));
270 StartNewPlayer(media_session_observer
.get(), MediaSession::Type::Content
);
272 EXPECT_FALSE(media_session_observer
->IsPlaying(0));
273 EXPECT_TRUE(media_session_observer
->IsPlaying(1));
274 EXPECT_TRUE(media_session_observer
->IsPlaying(2));
277 IN_PROC_BROWSER_TEST_F(MediaSessionBrowserTest
, AudioFocusInitialState
) {
278 EXPECT_FALSE(HasAudioFocus());
281 IN_PROC_BROWSER_TEST_F(MediaSessionBrowserTest
, StartPlayerGivesFocus
) {
282 scoped_ptr
<MockMediaSessionObserver
> media_session_observer(
283 new MockMediaSessionObserver
);
285 StartNewPlayer(media_session_observer
.get(), MediaSession::Type::Content
);
287 EXPECT_TRUE(HasAudioFocus());
290 IN_PROC_BROWSER_TEST_F(MediaSessionBrowserTest
, SuspendGivesAwayAudioFocus
) {
291 scoped_ptr
<MockMediaSessionObserver
> media_session_observer(
292 new MockMediaSessionObserver
);
294 StartNewPlayer(media_session_observer
.get(), MediaSession::Type::Content
);
296 OnSuspendSession(true);
298 EXPECT_FALSE(HasAudioFocus());
301 IN_PROC_BROWSER_TEST_F(MediaSessionBrowserTest
, ResumeGivesBackAudioFocus
) {
302 scoped_ptr
<MockMediaSessionObserver
> media_session_observer(
303 new MockMediaSessionObserver
);
305 StartNewPlayer(media_session_observer
.get(), MediaSession::Type::Content
);
307 OnSuspendSession(true);
310 EXPECT_TRUE(HasAudioFocus());
313 IN_PROC_BROWSER_TEST_F(MediaSessionBrowserTest
,
314 RemovingLastPlayerDropsAudioFocus
) {
315 scoped_ptr
<MockMediaSessionObserver
> media_session_observer(
316 new MockMediaSessionObserver
);
318 StartNewPlayer(media_session_observer
.get(), MediaSession::Type::Content
);
319 StartNewPlayer(media_session_observer
.get(), MediaSession::Type::Content
);
320 StartNewPlayer(media_session_observer
.get(), MediaSession::Type::Content
);
322 RemovePlayer(media_session_observer
.get(), 0);
323 EXPECT_TRUE(HasAudioFocus());
324 RemovePlayer(media_session_observer
.get(), 1);
325 EXPECT_TRUE(HasAudioFocus());
326 RemovePlayer(media_session_observer
.get(), 2);
327 EXPECT_FALSE(HasAudioFocus());
330 IN_PROC_BROWSER_TEST_F(MediaSessionBrowserTest
,
331 RemovingLastPlayerFromManyObserversDropsAudioFocus
) {
332 scoped_ptr
<MockMediaSessionObserver
> media_session_observer_1(
333 new MockMediaSessionObserver
);
334 scoped_ptr
<MockMediaSessionObserver
> media_session_observer_2(
335 new MockMediaSessionObserver
);
336 scoped_ptr
<MockMediaSessionObserver
> media_session_observer_3(
337 new MockMediaSessionObserver
);
339 StartNewPlayer(media_session_observer_1
.get(), MediaSession::Type::Content
);
340 StartNewPlayer(media_session_observer_2
.get(), MediaSession::Type::Content
);
341 StartNewPlayer(media_session_observer_3
.get(), MediaSession::Type::Content
);
343 RemovePlayer(media_session_observer_1
.get(), 0);
344 EXPECT_TRUE(HasAudioFocus());
345 RemovePlayer(media_session_observer_2
.get(), 0);
346 EXPECT_TRUE(HasAudioFocus());
347 RemovePlayer(media_session_observer_3
.get(), 0);
348 EXPECT_FALSE(HasAudioFocus());
351 IN_PROC_BROWSER_TEST_F(MediaSessionBrowserTest
,
352 RemovingAllPlayersFromObserversDropsAudioFocus
) {
353 scoped_ptr
<MockMediaSessionObserver
> media_session_observer_1(
354 new MockMediaSessionObserver
);
355 scoped_ptr
<MockMediaSessionObserver
> media_session_observer_2(
356 new MockMediaSessionObserver
);
358 StartNewPlayer(media_session_observer_1
.get(), MediaSession::Type::Content
);
359 StartNewPlayer(media_session_observer_1
.get(), MediaSession::Type::Content
);
360 StartNewPlayer(media_session_observer_2
.get(), MediaSession::Type::Content
);
361 StartNewPlayer(media_session_observer_2
.get(), MediaSession::Type::Content
);
363 RemovePlayers(media_session_observer_1
.get());
364 EXPECT_TRUE(HasAudioFocus());
365 RemovePlayers(media_session_observer_2
.get());
366 EXPECT_FALSE(HasAudioFocus());
369 IN_PROC_BROWSER_TEST_F(MediaSessionBrowserTest
, ResumePlayGivesAudioFocus
) {
370 scoped_ptr
<MockMediaSessionObserver
> media_session_observer(
371 new MockMediaSessionObserver
);
373 StartNewPlayer(media_session_observer
.get(), MediaSession::Type::Content
);
375 RemovePlayer(media_session_observer
.get(), 0);
376 EXPECT_FALSE(HasAudioFocus());
379 AddPlayer(media_session_observer
.get(), 0, MediaSession::Type::Content
));
380 EXPECT_TRUE(HasAudioFocus());
383 IN_PROC_BROWSER_TEST_F(MediaSessionBrowserTest
,
384 ResumeSuspendAreSentOnlyOncePerPlayers
) {
385 scoped_ptr
<MockMediaSessionObserver
> media_session_observer(
386 new MockMediaSessionObserver
);
388 StartNewPlayer(media_session_observer
.get(), MediaSession::Type::Content
);
389 StartNewPlayer(media_session_observer
.get(), MediaSession::Type::Content
);
390 StartNewPlayer(media_session_observer
.get(), MediaSession::Type::Content
);
392 EXPECT_EQ(0, media_session_observer
->received_suspend_calls());
393 EXPECT_EQ(0, media_session_observer
->received_resume_calls());
395 OnSuspendSession(true);
396 EXPECT_EQ(3, media_session_observer
->received_suspend_calls());
399 EXPECT_EQ(3, media_session_observer
->received_resume_calls());
402 IN_PROC_BROWSER_TEST_F(MediaSessionBrowserTest
,
403 ResumeSuspendAreSentOnlyOncePerPlayersAddedTwice
) {
404 scoped_ptr
<MockMediaSessionObserver
> media_session_observer(
405 new MockMediaSessionObserver
);
407 StartNewPlayer(media_session_observer
.get(), MediaSession::Type::Content
);
408 StartNewPlayer(media_session_observer
.get(), MediaSession::Type::Content
);
409 StartNewPlayer(media_session_observer
.get(), MediaSession::Type::Content
);
411 // Adding the three players above again.
413 AddPlayer(media_session_observer
.get(), 0, MediaSession::Type::Content
));
415 AddPlayer(media_session_observer
.get(), 1, MediaSession::Type::Content
));
417 AddPlayer(media_session_observer
.get(), 2, MediaSession::Type::Content
));
419 EXPECT_EQ(0, media_session_observer
->received_suspend_calls());
420 EXPECT_EQ(0, media_session_observer
->received_resume_calls());
422 OnSuspendSession(true);
423 EXPECT_EQ(3, media_session_observer
->received_suspend_calls());
426 EXPECT_EQ(3, media_session_observer
->received_resume_calls());
429 IN_PROC_BROWSER_TEST_F(MediaSessionBrowserTest
,
430 RemovingTheSamePlayerTwiceIsANoop
) {
431 scoped_ptr
<MockMediaSessionObserver
> media_session_observer(
432 new MockMediaSessionObserver
);
434 StartNewPlayer(media_session_observer
.get(), MediaSession::Type::Content
);
436 RemovePlayer(media_session_observer
.get(), 0);
437 RemovePlayer(media_session_observer
.get(), 0);
440 IN_PROC_BROWSER_TEST_F(MediaSessionBrowserTest
, MediaSessionType
) {
441 scoped_ptr
<MockMediaSessionObserver
> media_session_observer(
442 new MockMediaSessionObserver
);
444 // Starting a player with a given type should set the session to that type.
445 StartNewPlayer(media_session_observer
.get(), MediaSession::Type::Transient
);
446 EXPECT_EQ(MediaSession::Type::Transient
, GetSessionType());
448 // Adding a player of the same type should have no effect on the type.
449 StartNewPlayer(media_session_observer
.get(), MediaSession::Type::Transient
);
450 EXPECT_EQ(MediaSession::Type::Transient
, GetSessionType());
452 // Adding a player of Content type should override the current type.
453 StartNewPlayer(media_session_observer
.get(), MediaSession::Type::Content
);
454 EXPECT_EQ(MediaSession::Type::Content
, GetSessionType());
456 // Adding a player of the Transient type should have no effect on the type.
457 StartNewPlayer(media_session_observer
.get(), MediaSession::Type::Transient
);
458 EXPECT_EQ(MediaSession::Type::Content
, GetSessionType());
460 EXPECT_TRUE(media_session_observer
->IsPlaying(0));
461 EXPECT_TRUE(media_session_observer
->IsPlaying(1));
462 EXPECT_TRUE(media_session_observer
->IsPlaying(2));
463 EXPECT_TRUE(media_session_observer
->IsPlaying(3));
465 OnSuspendSession(true);
467 EXPECT_FALSE(media_session_observer
->IsPlaying(0));
468 EXPECT_FALSE(media_session_observer
->IsPlaying(1));
469 EXPECT_FALSE(media_session_observer
->IsPlaying(2));
470 EXPECT_FALSE(media_session_observer
->IsPlaying(3));
472 EXPECT_EQ(MediaSession::Type::Content
, GetSessionType());
476 EXPECT_TRUE(media_session_observer
->IsPlaying(0));
477 EXPECT_TRUE(media_session_observer
->IsPlaying(1));
478 EXPECT_TRUE(media_session_observer
->IsPlaying(2));
479 EXPECT_TRUE(media_session_observer
->IsPlaying(3));
481 EXPECT_EQ(MediaSession::Type::Content
, GetSessionType());
484 IN_PROC_BROWSER_TEST_F(MediaSessionBrowserTest
, ControlsShowForContent
) {
485 EXPECT_CALL(*mock_web_contents_observer(),
486 MediaSessionStateChanged(true, false));
488 scoped_ptr
<MockMediaSessionObserver
> media_session_observer(
489 new MockMediaSessionObserver
);
491 // Starting a player with a content type should show the media controls.
492 StartNewPlayer(media_session_observer
.get(), MediaSession::Type::Content
);
494 EXPECT_TRUE(IsControllable());
495 EXPECT_FALSE(IsSuspended());
498 IN_PROC_BROWSER_TEST_F(MediaSessionBrowserTest
, ControlsNoShowForTransient
) {
499 EXPECT_CALL(*mock_web_contents_observer(),
500 MediaSessionStateChanged(false, false));
502 scoped_ptr
<MockMediaSessionObserver
> media_session_observer(
503 new MockMediaSessionObserver
);
505 // Starting a player with a transient type should not show the media controls.
506 StartNewPlayer(media_session_observer
.get(), MediaSession::Type::Transient
);
508 EXPECT_FALSE(IsControllable());
509 EXPECT_FALSE(IsSuspended());
512 IN_PROC_BROWSER_TEST_F(MediaSessionBrowserTest
, ControlsHideWhenStopped
) {
513 Expectation showControls
= EXPECT_CALL(*mock_web_contents_observer(),
514 MediaSessionStateChanged(true, false));
515 EXPECT_CALL(*mock_web_contents_observer(),
516 MediaSessionStateChanged(false, true))
517 .After(showControls
);
519 scoped_ptr
<MockMediaSessionObserver
> media_session_observer(
520 new MockMediaSessionObserver
);
522 StartNewPlayer(media_session_observer
.get(), MediaSession::Type::Content
);
524 RemovePlayers(media_session_observer
.get());
526 EXPECT_FALSE(IsControllable());
527 EXPECT_TRUE(IsSuspended());
530 IN_PROC_BROWSER_TEST_F(MediaSessionBrowserTest
, ControlsShownAcceptTransient
) {
531 EXPECT_CALL(*mock_web_contents_observer(),
532 MediaSessionStateChanged(true, false));
534 scoped_ptr
<MockMediaSessionObserver
> media_session_observer(
535 new MockMediaSessionObserver
);
537 StartNewPlayer(media_session_observer
.get(), MediaSession::Type::Content
);
539 // Transient player join the session without affecting the controls.
540 StartNewPlayer(media_session_observer
.get(), MediaSession::Type::Transient
);
542 EXPECT_TRUE(IsControllable());
543 EXPECT_FALSE(IsSuspended());
546 IN_PROC_BROWSER_TEST_F(MediaSessionBrowserTest
,
547 ControlsShownAfterContentAdded
) {
548 Expectation dontShowControls
= EXPECT_CALL(
549 *mock_web_contents_observer(), MediaSessionStateChanged(false, false));
550 EXPECT_CALL(*mock_web_contents_observer(),
551 MediaSessionStateChanged(true, false))
552 .After(dontShowControls
);
554 scoped_ptr
<MockMediaSessionObserver
> media_session_observer(
555 new MockMediaSessionObserver
);
557 StartNewPlayer(media_session_observer
.get(), MediaSession::Type::Transient
);
559 // The controls are shown when the content player is added.
560 StartNewPlayer(media_session_observer
.get(), MediaSession::Type::Content
);
562 EXPECT_TRUE(IsControllable());
563 EXPECT_FALSE(IsSuspended());
566 IN_PROC_BROWSER_TEST_F(MediaSessionBrowserTest
,
567 ControlsStayIfOnlyOnePlayerHasBeenPaused
) {
568 EXPECT_CALL(*mock_web_contents_observer(),
569 MediaSessionStateChanged(true, false));
571 scoped_ptr
<MockMediaSessionObserver
> media_session_observer(
572 new MockMediaSessionObserver
);
574 StartNewPlayer(media_session_observer
.get(), MediaSession::Type::Content
);
575 StartNewPlayer(media_session_observer
.get(), MediaSession::Type::Transient
);
577 // Removing only content player doesn't hide the controls since the session
579 RemovePlayer(media_session_observer
.get(), 0);
581 EXPECT_TRUE(IsControllable());
582 EXPECT_FALSE(IsSuspended());
585 IN_PROC_BROWSER_TEST_F(MediaSessionBrowserTest
,
586 ControlsHideWhenTheLastPlayerIsStopped
) {
587 Expectation showControls
= EXPECT_CALL(*mock_web_contents_observer(),
588 MediaSessionStateChanged(true, false));
589 EXPECT_CALL(*mock_web_contents_observer(),
590 MediaSessionStateChanged(false, true))
591 .After(showControls
);
592 scoped_ptr
<MockMediaSessionObserver
> media_session_observer(
593 new MockMediaSessionObserver
);
595 StartNewPlayer(media_session_observer
.get(), MediaSession::Type::Content
);
596 StartNewPlayer(media_session_observer
.get(), MediaSession::Type::Content
);
598 RemovePlayer(media_session_observer
.get(), 0);
600 EXPECT_TRUE(IsControllable());
601 EXPECT_FALSE(IsSuspended());
603 RemovePlayer(media_session_observer
.get(), 1);
605 EXPECT_FALSE(IsControllable());
606 EXPECT_TRUE(IsSuspended());
609 IN_PROC_BROWSER_TEST_F(MediaSessionBrowserTest
,
610 ControlsHideWhenAllThePlayersAreRemoved
) {
611 Expectation showControls
= EXPECT_CALL(*mock_web_contents_observer(),
612 MediaSessionStateChanged(true, false));
613 EXPECT_CALL(*mock_web_contents_observer(),
614 MediaSessionStateChanged(false, true))
615 .After(showControls
);
617 scoped_ptr
<MockMediaSessionObserver
> media_session_observer(
618 new MockMediaSessionObserver
);
620 StartNewPlayer(media_session_observer
.get(), MediaSession::Type::Content
);
621 StartNewPlayer(media_session_observer
.get(), MediaSession::Type::Content
);
623 RemovePlayers(media_session_observer
.get());
625 EXPECT_FALSE(IsControllable());
626 EXPECT_TRUE(IsSuspended());
629 IN_PROC_BROWSER_TEST_F(MediaSessionBrowserTest
,
630 SuspendTemporaryUpdatesControls
) {
631 Expectation showControls
= EXPECT_CALL(*mock_web_contents_observer(),
632 MediaSessionStateChanged(true, false));
633 EXPECT_CALL(*mock_web_contents_observer(),
634 MediaSessionStateChanged(true, true))
635 .After(showControls
);
637 scoped_ptr
<MockMediaSessionObserver
> media_session_observer(
638 new MockMediaSessionObserver
);
640 StartNewPlayer(media_session_observer
.get(), MediaSession::Type::Content
);
642 OnSuspendSession(true);
644 EXPECT_TRUE(IsControllable());
645 EXPECT_TRUE(IsSuspended());
648 IN_PROC_BROWSER_TEST_F(MediaSessionBrowserTest
, ControlsUpdatedWhenResumed
) {
649 Expectation showControls
= EXPECT_CALL(*mock_web_contents_observer(),
650 MediaSessionStateChanged(true, false));
651 Expectation pauseControls
= EXPECT_CALL(*mock_web_contents_observer(),
652 MediaSessionStateChanged(true, true))
653 .After(showControls
);
654 EXPECT_CALL(*mock_web_contents_observer(),
655 MediaSessionStateChanged(true, false))
656 .After(pauseControls
);
658 scoped_ptr
<MockMediaSessionObserver
> media_session_observer(
659 new MockMediaSessionObserver
);
661 StartNewPlayer(media_session_observer
.get(), MediaSession::Type::Content
);
662 OnSuspendSession(true);
665 EXPECT_TRUE(IsControllable());
666 EXPECT_FALSE(IsSuspended());
669 IN_PROC_BROWSER_TEST_F(MediaSessionBrowserTest
,
670 ControlsHideWhenSessionSuspendedPermanently
) {
671 Expectation showControls
= EXPECT_CALL(*mock_web_contents_observer(),
672 MediaSessionStateChanged(true, false));
673 EXPECT_CALL(*mock_web_contents_observer(),
674 MediaSessionStateChanged(false, true))
675 .After(showControls
);
677 scoped_ptr
<MockMediaSessionObserver
> media_session_observer(
678 new MockMediaSessionObserver
);
680 StartNewPlayer(media_session_observer
.get(), MediaSession::Type::Content
);
682 OnSuspendSession(false);
684 EXPECT_FALSE(IsControllable());
685 EXPECT_TRUE(IsSuspended());
688 IN_PROC_BROWSER_TEST_F(MediaSessionBrowserTest
,
689 ControlsHideWhenSessionChangesFromContentToTransient
) {
690 Expectation showControls
= EXPECT_CALL(*mock_web_contents_observer(),
691 MediaSessionStateChanged(true, false));
692 Expectation pauseControls
= EXPECT_CALL(*mock_web_contents_observer(),
693 MediaSessionStateChanged(true, true))
694 .After(showControls
);
695 EXPECT_CALL(*mock_web_contents_observer(),
696 MediaSessionStateChanged(false, false))
697 .After(pauseControls
);
699 scoped_ptr
<MockMediaSessionObserver
> media_session_observer(
700 new MockMediaSessionObserver
);
702 StartNewPlayer(media_session_observer
.get(), MediaSession::Type::Content
);
703 OnSuspendSession(true);
705 // This should reset the session and change it to a transient, so
706 // hide the controls.
707 StartNewPlayer(media_session_observer
.get(), MediaSession::Type::Transient
);
709 EXPECT_FALSE(IsControllable());
710 EXPECT_FALSE(IsSuspended());
713 IN_PROC_BROWSER_TEST_F(MediaSessionBrowserTest
,
714 ControlsUpdatedWhenNewPlayerResetsSession
) {
715 Expectation showControls
= EXPECT_CALL(*mock_web_contents_observer(),
716 MediaSessionStateChanged(true, false));
717 Expectation pauseControls
= EXPECT_CALL(*mock_web_contents_observer(),
718 MediaSessionStateChanged(true, true))
719 .After(showControls
);
720 EXPECT_CALL(*mock_web_contents_observer(),
721 MediaSessionStateChanged(true, false))
722 .After(pauseControls
);
724 scoped_ptr
<MockMediaSessionObserver
> media_session_observer(
725 new MockMediaSessionObserver
);
727 StartNewPlayer(media_session_observer
.get(), MediaSession::Type::Content
);
728 OnSuspendSession(true);
730 // This should reset the session and update the controls.
731 StartNewPlayer(media_session_observer
.get(), MediaSession::Type::Content
);
733 EXPECT_TRUE(IsControllable());
734 EXPECT_FALSE(IsSuspended());
737 IN_PROC_BROWSER_TEST_F(MediaSessionBrowserTest
,
738 ControlsResumedWhenPlayerIsResumed
) {
739 Expectation showControls
= EXPECT_CALL(*mock_web_contents_observer(),
740 MediaSessionStateChanged(true, false));
741 Expectation pauseControls
= EXPECT_CALL(*mock_web_contents_observer(),
742 MediaSessionStateChanged(true, true))
743 .After(showControls
);
744 EXPECT_CALL(*mock_web_contents_observer(),
745 MediaSessionStateChanged(true, false))
746 .After(pauseControls
);
748 scoped_ptr
<MockMediaSessionObserver
> media_session_observer(
749 new MockMediaSessionObserver
);
751 StartNewPlayer(media_session_observer
.get(), MediaSession::Type::Content
);
752 OnSuspendSession(true);
754 // This should resume the session and update the controls.
755 AddPlayer(media_session_observer
.get(), 0, MediaSession::Type::Content
);
757 EXPECT_TRUE(IsControllable());
758 EXPECT_FALSE(IsSuspended());
761 IN_PROC_BROWSER_TEST_F(MediaSessionBrowserTest
,
762 ControlsNotUpdatedDueToResumeSessionAction
) {
763 EXPECT_CALL(*mock_web_contents_observer(),
764 MediaSessionStateChanged(true, false));
766 scoped_ptr
<MockMediaSessionObserver
> media_session_observer(
767 new MockMediaSessionObserver
);
769 StartNewPlayer(media_session_observer
.get(), MediaSession::Type::Content
);
772 EXPECT_TRUE(IsControllable());
773 EXPECT_TRUE(IsSuspended());
776 IN_PROC_BROWSER_TEST_F(MediaSessionBrowserTest
,
777 ControlsNotUpdatedDueToSuspendSessionAction
) {
778 EXPECT_CALL(*mock_web_contents_observer(),
779 MediaSessionStateChanged(true, false));
781 scoped_ptr
<MockMediaSessionObserver
> media_session_observer(
782 new MockMediaSessionObserver
);
784 StartNewPlayer(media_session_observer
.get(), MediaSession::Type::Content
);
788 EXPECT_TRUE(IsControllable());
789 EXPECT_FALSE(IsSuspended());
792 IN_PROC_BROWSER_TEST_F(MediaSessionBrowserTest
,
793 DontResumeBySystemUISuspendedSessions
) {
794 scoped_ptr
<MockMediaSessionObserver
> media_session_observer(
795 new MockMediaSessionObserver
);
796 StartNewPlayer(media_session_observer
.get(), MediaSession::Type::Content
);
799 EXPECT_TRUE(IsControllable());
800 EXPECT_TRUE(IsSuspended());
803 EXPECT_TRUE(IsControllable());
804 EXPECT_TRUE(IsSuspended());
807 IN_PROC_BROWSER_TEST_F(MediaSessionBrowserTest
,
808 AllowUIResumeForSystemSuspend
) {
809 scoped_ptr
<MockMediaSessionObserver
> media_session_observer(
810 new MockMediaSessionObserver
);
811 StartNewPlayer(media_session_observer
.get(), MediaSession::Type::Content
);
814 EXPECT_TRUE(IsControllable());
815 EXPECT_TRUE(IsSuspended());
818 EXPECT_TRUE(IsControllable());
819 EXPECT_FALSE(IsSuspended());
822 IN_PROC_BROWSER_TEST_F(MediaSessionBrowserTest
, ResumeSuspendFromUI
) {
823 scoped_ptr
<MockMediaSessionObserver
> media_session_observer(
824 new MockMediaSessionObserver
);
825 StartNewPlayer(media_session_observer
.get(), MediaSession::Type::Content
);
828 EXPECT_TRUE(IsControllable());
829 EXPECT_TRUE(IsSuspended());
832 EXPECT_TRUE(IsControllable());
833 EXPECT_FALSE(IsSuspended());
836 IN_PROC_BROWSER_TEST_F(MediaSessionBrowserTest
, ResumeSuspendFromSystem
) {
837 scoped_ptr
<MockMediaSessionObserver
> media_session_observer(
838 new MockMediaSessionObserver
);
839 StartNewPlayer(media_session_observer
.get(), MediaSession::Type::Content
);
842 EXPECT_TRUE(IsControllable());
843 EXPECT_TRUE(IsSuspended());
846 EXPECT_TRUE(IsControllable());
847 EXPECT_FALSE(IsSuspended());