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.
6 #include "base/callback.h"
7 #include "base/macros.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/run_loop.h"
10 #include "media/base/cdm_config.h"
11 #include "media/base/mock_filters.h"
12 #include "media/base/test_helpers.h"
13 #include "media/cdm/key_system_names.h"
14 #include "media/mojo/interfaces/content_decryption_module.mojom.h"
15 #include "media/mojo/interfaces/renderer.mojom.h"
16 #include "media/mojo/interfaces/service_factory.mojom.h"
17 #include "media/mojo/services/media_type_converters.h"
18 #include "media/mojo/services/mojo_demuxer_stream_impl.h"
19 #include "mojo/application/public/cpp/application_connection.h"
20 #include "mojo/application/public/cpp/application_impl.h"
21 #include "mojo/application/public/cpp/application_test_base.h"
22 #include "testing/gmock/include/gmock/gmock.h"
24 using testing::Exactly
;
25 using testing::Invoke
;
26 using testing::InvokeWithoutArgs
;
27 using testing::StrictMock
;
32 const char kInvalidKeySystem
[] = "invalid.key.system";
33 const char kSecurityOrigin
[] = "http://foo.com";
35 class MockRendererClient
: public interfaces::RendererClient
{
37 MockRendererClient(){};
38 ~MockRendererClient() override
{};
40 // interfaces::RendererClient implementation.
41 MOCK_METHOD2(OnTimeUpdate
, void(int64_t time_usec
, int64_t max_time_usec
));
42 MOCK_METHOD1(OnBufferingStateChange
, void(interfaces::BufferingState state
));
43 MOCK_METHOD0(OnEnded
, void());
44 MOCK_METHOD0(OnError
, void());
47 DISALLOW_COPY_AND_ASSIGN(MockRendererClient
);
50 class MediaAppTest
: public mojo::test::ApplicationTestBase
{
53 : renderer_client_binding_(&renderer_client_
),
54 video_demuxer_stream_(DemuxerStream::VIDEO
) {}
55 ~MediaAppTest() override
{}
57 void SetUp() override
{
58 ApplicationTestBase::SetUp();
60 mojo::URLRequestPtr request
= mojo::URLRequest::New();
61 request
->url
= "mojo:media";
62 mojo::ApplicationConnection
* connection
=
63 application_impl()->ConnectToApplication(request
.Pass());
64 connection
->SetRemoteServiceProviderConnectionErrorHandler(
65 base::Bind(&MediaAppTest::ConnectionClosed
, base::Unretained(this)));
67 connection
->ConnectToService(&service_factory_
);
68 service_factory_
->CreateCdm(mojo::GetProxy(&cdm_
));
69 service_factory_
->CreateRenderer(mojo::GetProxy(&renderer_
));
71 run_loop_
.reset(new base::RunLoop());
74 // MOCK_METHOD* doesn't support move only types. Work around this by having
76 MOCK_METHOD1(OnCdmInitializedInternal
, void(bool result
));
77 void OnCdmInitialized(interfaces::CdmPromiseResultPtr result
) {
78 OnCdmInitializedInternal(result
->success
);
81 void InitializeCdm(const std::string
& key_system
, bool expected_result
) {
82 EXPECT_CALL(*this, OnCdmInitializedInternal(expected_result
))
84 .WillOnce(InvokeWithoutArgs(run_loop_
.get(), &base::RunLoop::Quit
));
86 key_system
, kSecurityOrigin
, interfaces::CdmConfig::From(CdmConfig()),
87 1, base::Bind(&MediaAppTest::OnCdmInitialized
, base::Unretained(this)));
90 MOCK_METHOD1(OnRendererInitialized
, void(bool));
92 void InitializeRenderer(const VideoDecoderConfig
& video_config
,
93 bool expected_result
) {
94 video_demuxer_stream_
.set_video_decoder_config(video_config
);
96 interfaces::DemuxerStreamPtr video_stream
;
97 new MojoDemuxerStreamImpl(&video_demuxer_stream_
, GetProxy(&video_stream
));
99 interfaces::RendererClientPtr client_ptr
;
100 renderer_client_binding_
.Bind(GetProxy(&client_ptr
));
102 EXPECT_CALL(*this, OnRendererInitialized(expected_result
))
104 .WillOnce(InvokeWithoutArgs(run_loop_
.get(), &base::RunLoop::Quit
));
105 renderer_
->Initialize(client_ptr
.Pass(), nullptr, video_stream
.Pass(),
106 base::Bind(&MediaAppTest::OnRendererInitialized
,
107 base::Unretained(this)));
110 MOCK_METHOD0(ConnectionClosed
, void());
113 scoped_ptr
<base::RunLoop
> run_loop_
;
115 interfaces::ServiceFactoryPtr service_factory_
;
116 interfaces::ContentDecryptionModulePtr cdm_
;
117 interfaces::RendererPtr renderer_
;
119 StrictMock
<MockRendererClient
> renderer_client_
;
120 mojo::Binding
<interfaces::RendererClient
> renderer_client_binding_
;
122 StrictMock
<MockDemuxerStream
> video_demuxer_stream_
;
125 DISALLOW_COPY_AND_ASSIGN(MediaAppTest
);
130 // Note: base::RunLoop::RunUntilIdle() does not work well in these tests because
131 // even when the loop is idle, we may still have pending events in the pipe.
133 TEST_F(MediaAppTest
, InitializeCdm_Success
) {
134 InitializeCdm(kClearKey
, true);
138 TEST_F(MediaAppTest
, InitializeCdm_InvalidKeySystem
) {
139 InitializeCdm(kInvalidKeySystem
, false);
143 TEST_F(MediaAppTest
, InitializeRenderer_Success
) {
144 InitializeRenderer(TestVideoConfig::Normal(), true);
148 TEST_F(MediaAppTest
, InitializeRenderer_InvalidConfig
) {
149 InitializeRenderer(TestVideoConfig::Invalid(), false);
153 TEST_F(MediaAppTest
, Lifetime
) {
154 // Disconnecting CDM and Renderer services doesn't terminate the app.
158 // Disconnecting ServiceFactory service should terminate the app, which will
159 // close the connection.
160 EXPECT_CALL(*this, ConnectionClosed())
162 .WillOnce(Invoke(run_loop_
.get(), &base::RunLoop::Quit
));
163 service_factory_
.reset();