| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/browser/media/session/media_session_impl.h" | 5 #include "content/browser/media/session/media_session_impl.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 #include <memory> | 8 #include <memory> |
| 9 | 9 |
| 10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
| 11 #include "base/run_loop.h" | 11 #include "base/run_loop.h" |
| 12 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
| 13 #include "content/browser/media/session/media_session_player_observer.h" | 13 #include "content/browser/media/session/media_session_player_observer.h" |
| 14 #include "content/browser/media/session/media_session_service_impl.h" | 14 #include "content/browser/media/session/media_session_service_impl.h" |
| 15 #include "content/browser/media/session/mock_media_session_observer.h" | 15 #include "content/browser/media/session/mock_media_session_observer.h" |
| 16 #include "content/test/test_render_view_host.h" | 16 #include "content/test/test_render_view_host.h" |
| 17 #include "content/test/test_web_contents.h" | 17 #include "content/test/test_web_contents.h" |
| 18 #include "media/base/media_content_type.h" | 18 #include "media/base/media_content_type.h" |
| 19 #include "third_party/WebKit/public/platform/modules/mediasession/media_session.
mojom.h" | 19 #include "third_party/WebKit/public/platform/modules/mediasession/media_session.
mojom.h" |
| 20 | 20 |
| 21 using ::testing::_; | 21 using ::testing::_; |
| 22 using ::testing::AnyNumber; | 22 using ::testing::AnyNumber; |
| 23 using ::testing::Eq; | 23 using ::testing::Eq; |
| 24 using ::testing::InvokeWithoutArgs; | 24 using ::testing::InvokeWithoutArgs; |
| 25 using ::testing::NiceMock; |
| 25 | 26 |
| 26 namespace content { | 27 namespace content { |
| 27 | 28 |
| 28 namespace { | 29 namespace { |
| 29 | 30 |
| 30 static const int kPlayerId = 0; | 31 static const int kPlayerId = 0; |
| 31 | 32 |
| 32 class MockMediaSessionServiceImpl : public MediaSessionServiceImpl { | 33 class MockMediaSessionServiceImpl : public MediaSessionServiceImpl { |
| 33 public: | 34 public: |
| 34 explicit MockMediaSessionServiceImpl(RenderFrameHost* rfh) | 35 explicit MockMediaSessionServiceImpl(RenderFrameHost* rfh) |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 77 class MediaSessionImplServiceRoutingTest | 78 class MediaSessionImplServiceRoutingTest |
| 78 : public RenderViewHostImplTestHarness { | 79 : public RenderViewHostImplTestHarness { |
| 79 public: | 80 public: |
| 80 MediaSessionImplServiceRoutingTest() = default; | 81 MediaSessionImplServiceRoutingTest() = default; |
| 81 ~MediaSessionImplServiceRoutingTest() override = default; | 82 ~MediaSessionImplServiceRoutingTest() override = default; |
| 82 | 83 |
| 83 void SetUp() override { | 84 void SetUp() override { |
| 84 RenderViewHostImplTestHarness::SetUp(); | 85 RenderViewHostImplTestHarness::SetUp(); |
| 85 | 86 |
| 86 contents()->GetMainFrame()->InitializeRenderFrameIfNeeded(); | 87 contents()->GetMainFrame()->InitializeRenderFrameIfNeeded(); |
| 87 mock_media_session_observer_.reset( | 88 mock_media_session_observer_.reset(new NiceMock<MockMediaSessionObserver>( |
| 88 new MockMediaSessionObserver(MediaSessionImpl::Get(contents()))); | 89 MediaSessionImpl::Get(contents()))); |
| 89 main_frame_ = contents()->GetMainFrame(); | 90 main_frame_ = contents()->GetMainFrame(); |
| 90 sub_frame_ = main_frame_->AppendChild("sub_frame"); | 91 sub_frame_ = main_frame_->AppendChild("sub_frame"); |
| 91 | 92 |
| 92 player_in_main_frame_.reset( | 93 player_in_main_frame_.reset( |
| 93 new MockMediaSessionPlayerObserver(main_frame_)); | 94 new MockMediaSessionPlayerObserver(main_frame_)); |
| 94 player_in_sub_frame_.reset(new MockMediaSessionPlayerObserver(sub_frame_)); | 95 player_in_sub_frame_.reset(new MockMediaSessionPlayerObserver(sub_frame_)); |
| 95 } | 96 } |
| 96 | 97 |
| 97 void TearDown() override { | 98 void TearDown() override { |
| 98 mock_media_session_observer_.reset(); | 99 mock_media_session_observer_.reset(); |
| 99 services_.clear(); | 100 services_.clear(); |
| 100 clients_.clear(); | 101 clients_.clear(); |
| 101 | 102 |
| 102 RenderViewHostImplTestHarness::TearDown(); | 103 RenderViewHostImplTestHarness::TearDown(); |
| 103 } | 104 } |
| 104 | 105 |
| 105 protected: | 106 protected: |
| 106 MockMediaSessionObserver* mock_media_session_observer() { | 107 MockMediaSessionObserver* mock_media_session_observer() { |
| 107 return mock_media_session_observer_.get(); | 108 return mock_media_session_observer_.get(); |
| 108 } | 109 } |
| 109 | 110 |
| 110 void CreateServiceForFrame(TestRenderFrameHost* frame) { | 111 void CreateServiceForFrame(TestRenderFrameHost* frame) { |
| 111 services_[frame] = base::MakeUnique<MockMediaSessionServiceImpl>(frame); | 112 services_[frame] = |
| 112 clients_[frame] = base::MakeUnique<MockMediaSessionClient>(); | 113 base::MakeUnique<NiceMock<MockMediaSessionServiceImpl>>(frame); |
| 114 clients_[frame] = base::MakeUnique<NiceMock<MockMediaSessionClient>>(); |
| 113 services_[frame]->SetClient(clients_[frame]->CreateInterfacePtrAndBind()); | 115 services_[frame]->SetClient(clients_[frame]->CreateInterfacePtrAndBind()); |
| 114 } | 116 } |
| 115 | 117 |
| 116 void DestroyServiceForFrame(TestRenderFrameHost* frame) { | 118 void DestroyServiceForFrame(TestRenderFrameHost* frame) { |
| 117 services_.erase(frame); | 119 services_.erase(frame); |
| 118 clients_.erase(frame); | 120 clients_.erase(frame); |
| 119 } | 121 } |
| 120 | 122 |
| 121 MockMediaSessionClient* GetClientForFrame(TestRenderFrameHost* frame) { | 123 MockMediaSessionClient* GetClientForFrame(TestRenderFrameHost* frame) { |
| 122 auto iter = clients_.find(frame); | 124 auto iter = clients_.find(frame); |
| 123 return (iter != clients_.end()) ? iter->second.get() : nullptr; | 125 return (iter != clients_.end()) ? iter->second.get() : nullptr; |
| 124 } | 126 } |
| 125 | 127 |
| 126 void StartPlayerForFrame(TestRenderFrameHost* frame) { | 128 void StartPlayerForFrame(TestRenderFrameHost* frame) { |
| 127 players_[frame] = base::MakeUnique<MockMediaSessionPlayerObserver>(frame); | 129 players_[frame] = |
| 130 base::MakeUnique<NiceMock<MockMediaSessionPlayerObserver>>(frame); |
| 128 MediaSessionImpl::Get(contents()) | 131 MediaSessionImpl::Get(contents()) |
| 129 ->AddPlayer(players_[frame].get(), kPlayerId, | 132 ->AddPlayer(players_[frame].get(), kPlayerId, |
| 130 media::MediaContentType::Persistent); | 133 media::MediaContentType::Persistent); |
| 131 } | 134 } |
| 132 | 135 |
| 133 void ClearPlayersForFrame(TestRenderFrameHost* frame) { | 136 void ClearPlayersForFrame(TestRenderFrameHost* frame) { |
| 134 if (!players_.count(frame)) | 137 if (!players_.count(frame)) |
| 135 return; | 138 return; |
| 136 | 139 |
| 137 MediaSessionImpl::Get(contents()) | 140 MediaSessionImpl::Get(contents()) |
| (...skipping 270 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 408 CreateServiceForFrame(sub_frame_); | 411 CreateServiceForFrame(sub_frame_); |
| 409 | 412 |
| 410 EXPECT_EQ(nullptr, ComputeServiceForRouting()); | 413 EXPECT_EQ(nullptr, ComputeServiceForRouting()); |
| 411 | 414 |
| 412 // This should not crash. | 415 // This should not crash. |
| 413 MediaSessionImpl::Get(contents()) | 416 MediaSessionImpl::Get(contents()) |
| 414 ->DidReceiveAction(blink::mojom::MediaSessionAction::PAUSE); | 417 ->DidReceiveAction(blink::mojom::MediaSessionAction::PAUSE); |
| 415 } | 418 } |
| 416 | 419 |
| 417 } // namespace content | 420 } // namespace content |
| OLD | NEW |