[Android] A prototype of the interactive media notification.
[chromium-blink-merge.git] / content / browser / media / android / media_session_browsertest.cc
blob9707330afec853a1a947286fa2b5e678c57a7707
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"
7 #include <list>
8 #include <vector>
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;
24 namespace {
26 class MockMediaSessionObserver : public MediaSessionObserver {
27 public:
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_;
74 private:
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 {
84 public:
85 MockWebContentsObserver(WebContents* web_contents)
86 : WebContentsObserver(web_contents) {}
88 MOCK_METHOD2(MediaSessionStateChanged,
89 void(bool is_controllable, bool is_suspended));
92 } // namespace
94 class MediaSessionBrowserTest : public content::ContentBrowserTest {
95 protected:
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(),
122 type);
123 EXPECT_TRUE(result);
126 bool AddPlayer(MockMediaSessionObserver* media_session_observer,
127 int player_id,
128 MediaSession::Type type) {
129 return media_session_->AddPlayer(media_session_observer, player_id, type);
132 void RemovePlayer(MockMediaSessionObserver* media_session_observer,
133 int player_id) {
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(); }
157 void ResumeSession() { media_session_->Resume(); }
159 void SuspendSession() { media_session_->Suspend(); }
161 MockWebContentsObserver* mock_web_contents_observer() {
162 return mock_web_contents_observer_.get();
165 protected:
166 MediaSession* media_session_;
167 scoped_ptr<MockWebContentsObserver> mock_web_contents_observer_;
169 DISALLOW_COPY_AND_ASSIGN(MediaSessionBrowserTest);
172 IN_PROC_BROWSER_TEST_F(MediaSessionBrowserTest,
173 PlayersFromSameObserverDoNotStopEachOtherInSameSession) {
174 scoped_ptr<MockMediaSessionObserver> media_session_observer(
175 new MockMediaSessionObserver);
177 StartNewPlayer(media_session_observer.get(), MediaSession::Type::Content);
178 StartNewPlayer(media_session_observer.get(), MediaSession::Type::Content);
179 StartNewPlayer(media_session_observer.get(), MediaSession::Type::Content);
181 EXPECT_TRUE(media_session_observer->IsPlaying(0));
182 EXPECT_TRUE(media_session_observer->IsPlaying(1));
183 EXPECT_TRUE(media_session_observer->IsPlaying(2));
186 IN_PROC_BROWSER_TEST_F(MediaSessionBrowserTest,
187 PlayersFromManyObserverDoNotStopEachOtherInSameSession) {
188 scoped_ptr<MockMediaSessionObserver> media_session_observer_1(
189 new MockMediaSessionObserver);
190 scoped_ptr<MockMediaSessionObserver> media_session_observer_2(
191 new MockMediaSessionObserver);
192 scoped_ptr<MockMediaSessionObserver> media_session_observer_3(
193 new MockMediaSessionObserver);
195 StartNewPlayer(media_session_observer_1.get(), MediaSession::Type::Content);
197 StartNewPlayer(media_session_observer_2.get(), MediaSession::Type::Content);
198 StartNewPlayer(media_session_observer_3.get(), MediaSession::Type::Content);
200 EXPECT_TRUE(media_session_observer_1->IsPlaying(0));
201 EXPECT_TRUE(media_session_observer_2->IsPlaying(0));
202 EXPECT_TRUE(media_session_observer_3->IsPlaying(0));
205 IN_PROC_BROWSER_TEST_F(MediaSessionBrowserTest,
206 SuspendedMediaSessionStopsPlayers) {
207 scoped_ptr<MockMediaSessionObserver> media_session_observer(
208 new MockMediaSessionObserver);
210 StartNewPlayer(media_session_observer.get(), MediaSession::Type::Content);
211 StartNewPlayer(media_session_observer.get(), MediaSession::Type::Content);
212 StartNewPlayer(media_session_observer.get(), MediaSession::Type::Content);
214 OnSuspendSession(true);
216 EXPECT_FALSE(media_session_observer->IsPlaying(0));
217 EXPECT_FALSE(media_session_observer->IsPlaying(1));
218 EXPECT_FALSE(media_session_observer->IsPlaying(2));
221 IN_PROC_BROWSER_TEST_F(MediaSessionBrowserTest,
222 ResumedMediaSessionRestartsPlayers) {
223 scoped_ptr<MockMediaSessionObserver> media_session_observer(
224 new MockMediaSessionObserver);
226 StartNewPlayer(media_session_observer.get(), MediaSession::Type::Content);
227 StartNewPlayer(media_session_observer.get(), MediaSession::Type::Content);
228 StartNewPlayer(media_session_observer.get(), MediaSession::Type::Content);
230 OnSuspendSession(true);
231 OnResumeSession();
233 EXPECT_TRUE(media_session_observer->IsPlaying(0));
234 EXPECT_TRUE(media_session_observer->IsPlaying(1));
235 EXPECT_TRUE(media_session_observer->IsPlaying(2));
238 IN_PROC_BROWSER_TEST_F(MediaSessionBrowserTest,
239 StartedPlayerOnSuspendedSessionPlaysAlone) {
240 scoped_ptr<MockMediaSessionObserver> media_session_observer(
241 new MockMediaSessionObserver);
243 StartNewPlayer(media_session_observer.get(), MediaSession::Type::Content);
245 EXPECT_TRUE(media_session_observer->IsPlaying(0));
247 OnSuspendSession(true);
249 EXPECT_FALSE(media_session_observer->IsPlaying(0));
251 StartNewPlayer(media_session_observer.get(), MediaSession::Type::Content);
253 EXPECT_FALSE(media_session_observer->IsPlaying(0));
254 EXPECT_TRUE(media_session_observer->IsPlaying(1));
256 StartNewPlayer(media_session_observer.get(), MediaSession::Type::Content);
258 EXPECT_FALSE(media_session_observer->IsPlaying(0));
259 EXPECT_TRUE(media_session_observer->IsPlaying(1));
260 EXPECT_TRUE(media_session_observer->IsPlaying(2));
263 IN_PROC_BROWSER_TEST_F(MediaSessionBrowserTest, AudioFocusInitialState) {
264 EXPECT_FALSE(HasAudioFocus());
267 IN_PROC_BROWSER_TEST_F(MediaSessionBrowserTest, StartPlayerGivesFocus) {
268 scoped_ptr<MockMediaSessionObserver> media_session_observer(
269 new MockMediaSessionObserver);
271 StartNewPlayer(media_session_observer.get(), MediaSession::Type::Content);
273 EXPECT_TRUE(HasAudioFocus());
276 IN_PROC_BROWSER_TEST_F(MediaSessionBrowserTest, SuspendGivesAwayAudioFocus) {
277 scoped_ptr<MockMediaSessionObserver> media_session_observer(
278 new MockMediaSessionObserver);
280 StartNewPlayer(media_session_observer.get(), MediaSession::Type::Content);
282 OnSuspendSession(true);
284 EXPECT_FALSE(HasAudioFocus());
287 IN_PROC_BROWSER_TEST_F(MediaSessionBrowserTest, ResumeGivesBackAudioFocus) {
288 scoped_ptr<MockMediaSessionObserver> media_session_observer(
289 new MockMediaSessionObserver);
291 StartNewPlayer(media_session_observer.get(), MediaSession::Type::Content);
293 OnSuspendSession(true);
294 OnResumeSession();
296 EXPECT_TRUE(HasAudioFocus());
299 IN_PROC_BROWSER_TEST_F(MediaSessionBrowserTest,
300 RemovingLastPlayerDropsAudioFocus) {
301 scoped_ptr<MockMediaSessionObserver> media_session_observer(
302 new MockMediaSessionObserver);
304 StartNewPlayer(media_session_observer.get(), MediaSession::Type::Content);
305 StartNewPlayer(media_session_observer.get(), MediaSession::Type::Content);
306 StartNewPlayer(media_session_observer.get(), MediaSession::Type::Content);
308 RemovePlayer(media_session_observer.get(), 0);
309 EXPECT_TRUE(HasAudioFocus());
310 RemovePlayer(media_session_observer.get(), 1);
311 EXPECT_TRUE(HasAudioFocus());
312 RemovePlayer(media_session_observer.get(), 2);
313 EXPECT_FALSE(HasAudioFocus());
316 IN_PROC_BROWSER_TEST_F(MediaSessionBrowserTest,
317 RemovingLastPlayerFromManyObserversDropsAudioFocus) {
318 scoped_ptr<MockMediaSessionObserver> media_session_observer_1(
319 new MockMediaSessionObserver);
320 scoped_ptr<MockMediaSessionObserver> media_session_observer_2(
321 new MockMediaSessionObserver);
322 scoped_ptr<MockMediaSessionObserver> media_session_observer_3(
323 new MockMediaSessionObserver);
325 StartNewPlayer(media_session_observer_1.get(), MediaSession::Type::Content);
326 StartNewPlayer(media_session_observer_2.get(), MediaSession::Type::Content);
327 StartNewPlayer(media_session_observer_3.get(), MediaSession::Type::Content);
329 RemovePlayer(media_session_observer_1.get(), 0);
330 EXPECT_TRUE(HasAudioFocus());
331 RemovePlayer(media_session_observer_2.get(), 0);
332 EXPECT_TRUE(HasAudioFocus());
333 RemovePlayer(media_session_observer_3.get(), 0);
334 EXPECT_FALSE(HasAudioFocus());
337 IN_PROC_BROWSER_TEST_F(MediaSessionBrowserTest,
338 RemovingAllPlayersFromObserversDropsAudioFocus) {
339 scoped_ptr<MockMediaSessionObserver> media_session_observer_1(
340 new MockMediaSessionObserver);
341 scoped_ptr<MockMediaSessionObserver> media_session_observer_2(
342 new MockMediaSessionObserver);
344 StartNewPlayer(media_session_observer_1.get(), MediaSession::Type::Content);
345 StartNewPlayer(media_session_observer_1.get(), MediaSession::Type::Content);
346 StartNewPlayer(media_session_observer_2.get(), MediaSession::Type::Content);
347 StartNewPlayer(media_session_observer_2.get(), MediaSession::Type::Content);
349 RemovePlayers(media_session_observer_1.get());
350 EXPECT_TRUE(HasAudioFocus());
351 RemovePlayers(media_session_observer_2.get());
352 EXPECT_FALSE(HasAudioFocus());
355 IN_PROC_BROWSER_TEST_F(MediaSessionBrowserTest, ResumePlayGivesAudioFocus) {
356 scoped_ptr<MockMediaSessionObserver> media_session_observer(
357 new MockMediaSessionObserver);
359 StartNewPlayer(media_session_observer.get(), MediaSession::Type::Content);
361 RemovePlayer(media_session_observer.get(), 0);
362 EXPECT_FALSE(HasAudioFocus());
364 EXPECT_TRUE(
365 AddPlayer(media_session_observer.get(), 0, MediaSession::Type::Content));
366 EXPECT_TRUE(HasAudioFocus());
369 IN_PROC_BROWSER_TEST_F(MediaSessionBrowserTest,
370 ResumeSuspendAreSentOnlyOncePerPlayers) {
371 scoped_ptr<MockMediaSessionObserver> media_session_observer(
372 new MockMediaSessionObserver);
374 StartNewPlayer(media_session_observer.get(), MediaSession::Type::Content);
375 StartNewPlayer(media_session_observer.get(), MediaSession::Type::Content);
376 StartNewPlayer(media_session_observer.get(), MediaSession::Type::Content);
378 EXPECT_EQ(0, media_session_observer->received_suspend_calls());
379 EXPECT_EQ(0, media_session_observer->received_resume_calls());
381 OnSuspendSession(true);
382 EXPECT_EQ(3, media_session_observer->received_suspend_calls());
384 OnResumeSession();
385 EXPECT_EQ(3, media_session_observer->received_resume_calls());
388 IN_PROC_BROWSER_TEST_F(MediaSessionBrowserTest,
389 ResumeSuspendAreSentOnlyOncePerPlayersAddedTwice) {
390 scoped_ptr<MockMediaSessionObserver> media_session_observer(
391 new MockMediaSessionObserver);
393 StartNewPlayer(media_session_observer.get(), MediaSession::Type::Content);
394 StartNewPlayer(media_session_observer.get(), MediaSession::Type::Content);
395 StartNewPlayer(media_session_observer.get(), MediaSession::Type::Content);
397 // Adding the three players above again.
398 EXPECT_TRUE(
399 AddPlayer(media_session_observer.get(), 0, MediaSession::Type::Content));
400 EXPECT_TRUE(
401 AddPlayer(media_session_observer.get(), 1, MediaSession::Type::Content));
402 EXPECT_TRUE(
403 AddPlayer(media_session_observer.get(), 2, MediaSession::Type::Content));
405 EXPECT_EQ(0, media_session_observer->received_suspend_calls());
406 EXPECT_EQ(0, media_session_observer->received_resume_calls());
408 OnSuspendSession(true);
409 EXPECT_EQ(3, media_session_observer->received_suspend_calls());
411 OnResumeSession();
412 EXPECT_EQ(3, media_session_observer->received_resume_calls());
415 IN_PROC_BROWSER_TEST_F(MediaSessionBrowserTest,
416 RemovingTheSamePlayerTwiceIsANoop) {
417 scoped_ptr<MockMediaSessionObserver> media_session_observer(
418 new MockMediaSessionObserver);
420 StartNewPlayer(media_session_observer.get(), MediaSession::Type::Content);
422 RemovePlayer(media_session_observer.get(), 0);
423 RemovePlayer(media_session_observer.get(), 0);
426 IN_PROC_BROWSER_TEST_F(MediaSessionBrowserTest, MediaSessionType) {
427 scoped_ptr<MockMediaSessionObserver> media_session_observer(
428 new MockMediaSessionObserver);
430 // Starting a player with a given type should set the session to that type.
431 StartNewPlayer(media_session_observer.get(), MediaSession::Type::Transient);
432 EXPECT_EQ(MediaSession::Type::Transient, GetSessionType());
434 // Adding a player of the same type should have no effect on the type.
435 StartNewPlayer(media_session_observer.get(), MediaSession::Type::Transient);
436 EXPECT_EQ(MediaSession::Type::Transient, GetSessionType());
438 // Adding a player of Content type should override the current type.
439 StartNewPlayer(media_session_observer.get(), MediaSession::Type::Content);
440 EXPECT_EQ(MediaSession::Type::Content, GetSessionType());
442 // Adding a player of the Transient type should have no effect on the type.
443 StartNewPlayer(media_session_observer.get(), MediaSession::Type::Transient);
444 EXPECT_EQ(MediaSession::Type::Content, GetSessionType());
446 EXPECT_TRUE(media_session_observer->IsPlaying(0));
447 EXPECT_TRUE(media_session_observer->IsPlaying(1));
448 EXPECT_TRUE(media_session_observer->IsPlaying(2));
449 EXPECT_TRUE(media_session_observer->IsPlaying(3));
451 OnSuspendSession(true);
453 EXPECT_FALSE(media_session_observer->IsPlaying(0));
454 EXPECT_FALSE(media_session_observer->IsPlaying(1));
455 EXPECT_FALSE(media_session_observer->IsPlaying(2));
456 EXPECT_FALSE(media_session_observer->IsPlaying(3));
458 EXPECT_EQ(MediaSession::Type::Content, GetSessionType());
460 OnResumeSession();
462 EXPECT_TRUE(media_session_observer->IsPlaying(0));
463 EXPECT_TRUE(media_session_observer->IsPlaying(1));
464 EXPECT_TRUE(media_session_observer->IsPlaying(2));
465 EXPECT_TRUE(media_session_observer->IsPlaying(3));
467 EXPECT_EQ(MediaSession::Type::Content, GetSessionType());
470 IN_PROC_BROWSER_TEST_F(MediaSessionBrowserTest, ControlsShowForContent) {
471 EXPECT_CALL(*mock_web_contents_observer(),
472 MediaSessionStateChanged(true, false));
474 scoped_ptr<MockMediaSessionObserver> media_session_observer(
475 new MockMediaSessionObserver);
477 // Starting a player with a content type should show the media controls.
478 StartNewPlayer(media_session_observer.get(), MediaSession::Type::Content);
480 EXPECT_TRUE(IsControllable());
481 EXPECT_FALSE(IsSuspended());
484 IN_PROC_BROWSER_TEST_F(MediaSessionBrowserTest, ControlsNoShowForTransient) {
485 EXPECT_CALL(*mock_web_contents_observer(),
486 MediaSessionStateChanged(false, false));
488 scoped_ptr<MockMediaSessionObserver> media_session_observer(
489 new MockMediaSessionObserver);
491 // Starting a player with a transient type should not show the media controls.
492 StartNewPlayer(media_session_observer.get(), MediaSession::Type::Transient);
494 EXPECT_FALSE(IsControllable());
495 EXPECT_FALSE(IsSuspended());
498 IN_PROC_BROWSER_TEST_F(MediaSessionBrowserTest, ControlsHideWhenStopped) {
499 Expectation showControls = EXPECT_CALL(*mock_web_contents_observer(),
500 MediaSessionStateChanged(true, false));
501 EXPECT_CALL(*mock_web_contents_observer(),
502 MediaSessionStateChanged(false, true))
503 .After(showControls);
505 scoped_ptr<MockMediaSessionObserver> media_session_observer(
506 new MockMediaSessionObserver);
508 StartNewPlayer(media_session_observer.get(), MediaSession::Type::Content);
510 RemovePlayers(media_session_observer.get());
512 EXPECT_FALSE(IsControllable());
513 EXPECT_TRUE(IsSuspended());
516 IN_PROC_BROWSER_TEST_F(MediaSessionBrowserTest, ControlsShownAcceptTransient) {
517 EXPECT_CALL(*mock_web_contents_observer(),
518 MediaSessionStateChanged(true, false));
520 scoped_ptr<MockMediaSessionObserver> media_session_observer(
521 new MockMediaSessionObserver);
523 StartNewPlayer(media_session_observer.get(), MediaSession::Type::Content);
525 // Transient player join the session without affecting the controls.
526 StartNewPlayer(media_session_observer.get(), MediaSession::Type::Transient);
528 EXPECT_TRUE(IsControllable());
529 EXPECT_FALSE(IsSuspended());
532 IN_PROC_BROWSER_TEST_F(MediaSessionBrowserTest,
533 ControlsShownAfterContentAdded) {
534 Expectation dontShowControls = EXPECT_CALL(
535 *mock_web_contents_observer(), MediaSessionStateChanged(false, false));
536 EXPECT_CALL(*mock_web_contents_observer(),
537 MediaSessionStateChanged(true, false))
538 .After(dontShowControls);
540 scoped_ptr<MockMediaSessionObserver> media_session_observer(
541 new MockMediaSessionObserver);
543 StartNewPlayer(media_session_observer.get(), MediaSession::Type::Transient);
545 // The controls are shown when the content player is added.
546 StartNewPlayer(media_session_observer.get(), MediaSession::Type::Content);
548 EXPECT_TRUE(IsControllable());
549 EXPECT_FALSE(IsSuspended());
552 IN_PROC_BROWSER_TEST_F(MediaSessionBrowserTest,
553 ControlsStayIfOnlyOnePlayerHasBeenPaused) {
554 EXPECT_CALL(*mock_web_contents_observer(),
555 MediaSessionStateChanged(true, false));
557 scoped_ptr<MockMediaSessionObserver> media_session_observer(
558 new MockMediaSessionObserver);
560 StartNewPlayer(media_session_observer.get(), MediaSession::Type::Content);
561 StartNewPlayer(media_session_observer.get(), MediaSession::Type::Transient);
563 // Removing only content player doesn't hide the controls since the session
564 // is still active.
565 RemovePlayer(media_session_observer.get(), 0);
567 EXPECT_TRUE(IsControllable());
568 EXPECT_FALSE(IsSuspended());
571 IN_PROC_BROWSER_TEST_F(MediaSessionBrowserTest,
572 ControlsHideWhenTheLastPlayerIsStopped) {
573 Expectation showControls = EXPECT_CALL(*mock_web_contents_observer(),
574 MediaSessionStateChanged(true, false));
575 EXPECT_CALL(*mock_web_contents_observer(),
576 MediaSessionStateChanged(false, true))
577 .After(showControls);
578 scoped_ptr<MockMediaSessionObserver> media_session_observer(
579 new MockMediaSessionObserver);
581 StartNewPlayer(media_session_observer.get(), MediaSession::Type::Content);
582 StartNewPlayer(media_session_observer.get(), MediaSession::Type::Content);
584 RemovePlayer(media_session_observer.get(), 0);
586 EXPECT_TRUE(IsControllable());
587 EXPECT_FALSE(IsSuspended());
589 RemovePlayer(media_session_observer.get(), 1);
591 EXPECT_FALSE(IsControllable());
592 EXPECT_TRUE(IsSuspended());
595 IN_PROC_BROWSER_TEST_F(MediaSessionBrowserTest,
596 ControlsHideWhenAllThePlayersAreRemoved) {
597 Expectation showControls = EXPECT_CALL(*mock_web_contents_observer(),
598 MediaSessionStateChanged(true, false));
599 EXPECT_CALL(*mock_web_contents_observer(),
600 MediaSessionStateChanged(false, true))
601 .After(showControls);
603 scoped_ptr<MockMediaSessionObserver> media_session_observer(
604 new MockMediaSessionObserver);
606 StartNewPlayer(media_session_observer.get(), MediaSession::Type::Content);
607 StartNewPlayer(media_session_observer.get(), MediaSession::Type::Content);
609 RemovePlayers(media_session_observer.get());
611 EXPECT_FALSE(IsControllable());
612 EXPECT_TRUE(IsSuspended());
615 IN_PROC_BROWSER_TEST_F(MediaSessionBrowserTest,
616 SuspendTemporaryUpdatesControls) {
617 Expectation showControls = EXPECT_CALL(*mock_web_contents_observer(),
618 MediaSessionStateChanged(true, false));
619 EXPECT_CALL(*mock_web_contents_observer(),
620 MediaSessionStateChanged(true, true))
621 .After(showControls);
623 scoped_ptr<MockMediaSessionObserver> media_session_observer(
624 new MockMediaSessionObserver);
626 StartNewPlayer(media_session_observer.get(), MediaSession::Type::Content);
628 OnSuspendSession(true);
630 EXPECT_TRUE(IsControllable());
631 EXPECT_TRUE(IsSuspended());
634 IN_PROC_BROWSER_TEST_F(MediaSessionBrowserTest, ControlsUpdatedWhenResumed) {
635 Expectation showControls = EXPECT_CALL(*mock_web_contents_observer(),
636 MediaSessionStateChanged(true, false));
637 Expectation pauseControls = EXPECT_CALL(*mock_web_contents_observer(),
638 MediaSessionStateChanged(true, true))
639 .After(showControls);
640 EXPECT_CALL(*mock_web_contents_observer(),
641 MediaSessionStateChanged(true, false))
642 .After(pauseControls);
644 scoped_ptr<MockMediaSessionObserver> media_session_observer(
645 new MockMediaSessionObserver);
647 StartNewPlayer(media_session_observer.get(), MediaSession::Type::Content);
648 OnSuspendSession(true);
649 OnResumeSession();
651 EXPECT_TRUE(IsControllable());
652 EXPECT_FALSE(IsSuspended());
655 IN_PROC_BROWSER_TEST_F(MediaSessionBrowserTest,
656 ControlsHideWhenSessionSuspendedPermanently) {
657 Expectation showControls = EXPECT_CALL(*mock_web_contents_observer(),
658 MediaSessionStateChanged(true, false));
659 EXPECT_CALL(*mock_web_contents_observer(),
660 MediaSessionStateChanged(false, true))
661 .After(showControls);
663 scoped_ptr<MockMediaSessionObserver> media_session_observer(
664 new MockMediaSessionObserver);
666 StartNewPlayer(media_session_observer.get(), MediaSession::Type::Content);
668 OnSuspendSession(false);
670 EXPECT_FALSE(IsControllable());
671 EXPECT_TRUE(IsSuspended());
674 IN_PROC_BROWSER_TEST_F(MediaSessionBrowserTest,
675 ControlsHideWhenSessionChangesFromContentToTransient) {
676 Expectation showControls = EXPECT_CALL(*mock_web_contents_observer(),
677 MediaSessionStateChanged(true, false));
678 Expectation pauseControls = EXPECT_CALL(*mock_web_contents_observer(),
679 MediaSessionStateChanged(true, true))
680 .After(showControls);
681 EXPECT_CALL(*mock_web_contents_observer(),
682 MediaSessionStateChanged(false, false))
683 .After(pauseControls);
685 scoped_ptr<MockMediaSessionObserver> media_session_observer(
686 new MockMediaSessionObserver);
688 StartNewPlayer(media_session_observer.get(), MediaSession::Type::Content);
689 OnSuspendSession(true);
691 // This should reset the session and change it to a transient, so
692 // hide the controls.
693 StartNewPlayer(media_session_observer.get(), MediaSession::Type::Transient);
695 EXPECT_FALSE(IsControllable());
696 EXPECT_FALSE(IsSuspended());
699 IN_PROC_BROWSER_TEST_F(MediaSessionBrowserTest,
700 ControlsUpdatedWhenNewPlayerResetsSession) {
701 Expectation showControls = EXPECT_CALL(*mock_web_contents_observer(),
702 MediaSessionStateChanged(true, false));
703 Expectation pauseControls = EXPECT_CALL(*mock_web_contents_observer(),
704 MediaSessionStateChanged(true, true))
705 .After(showControls);
706 EXPECT_CALL(*mock_web_contents_observer(),
707 MediaSessionStateChanged(true, false))
708 .After(pauseControls);
710 scoped_ptr<MockMediaSessionObserver> media_session_observer(
711 new MockMediaSessionObserver);
713 StartNewPlayer(media_session_observer.get(), MediaSession::Type::Content);
714 OnSuspendSession(true);
716 // This should reset the session and update the controls.
717 StartNewPlayer(media_session_observer.get(), MediaSession::Type::Content);
719 EXPECT_TRUE(IsControllable());
720 EXPECT_FALSE(IsSuspended());
723 IN_PROC_BROWSER_TEST_F(MediaSessionBrowserTest,
724 ControlsResumedWhenPlayerIsResumed) {
725 Expectation showControls = EXPECT_CALL(*mock_web_contents_observer(),
726 MediaSessionStateChanged(true, false));
727 Expectation pauseControls = EXPECT_CALL(*mock_web_contents_observer(),
728 MediaSessionStateChanged(true, true))
729 .After(showControls);
730 EXPECT_CALL(*mock_web_contents_observer(),
731 MediaSessionStateChanged(true, false))
732 .After(pauseControls);
734 scoped_ptr<MockMediaSessionObserver> media_session_observer(
735 new MockMediaSessionObserver);
737 StartNewPlayer(media_session_observer.get(), MediaSession::Type::Content);
738 OnSuspendSession(true);
740 // This should resume the session and update the controls.
741 AddPlayer(media_session_observer.get(), 0, MediaSession::Type::Content);
743 EXPECT_TRUE(IsControllable());
744 EXPECT_FALSE(IsSuspended());
747 IN_PROC_BROWSER_TEST_F(MediaSessionBrowserTest,
748 ControlsNotUpdatedDueToResumeSessionAction) {
749 EXPECT_CALL(*mock_web_contents_observer(),
750 MediaSessionStateChanged(true, false));
752 scoped_ptr<MockMediaSessionObserver> media_session_observer(
753 new MockMediaSessionObserver);
755 StartNewPlayer(media_session_observer.get(), MediaSession::Type::Content);
756 SuspendSession();
758 EXPECT_TRUE(IsControllable());
759 EXPECT_TRUE(IsSuspended());
762 IN_PROC_BROWSER_TEST_F(MediaSessionBrowserTest,
763 ControlsNotUpdatedDueToSuspendSessionAction) {
764 EXPECT_CALL(*mock_web_contents_observer(),
765 MediaSessionStateChanged(true, false));
767 scoped_ptr<MockMediaSessionObserver> media_session_observer(
768 new MockMediaSessionObserver);
770 StartNewPlayer(media_session_observer.get(), MediaSession::Type::Content);
771 SuspendSession();
772 ResumeSession();
774 EXPECT_TRUE(IsControllable());
775 EXPECT_FALSE(IsSuspended());