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 connection_
= application_impl()->ConnectToApplication(request
.Pass());
63 connection_
->SetRemoteServiceProviderConnectionErrorHandler(
64 base::Bind(&MediaAppTest::ConnectionClosed
, base::Unretained(this)));
66 connection_
->ConnectToService(&service_factory_
);
67 service_factory_
->CreateCdm(mojo::GetProxy(&cdm_
));
68 service_factory_
->CreateRenderer(mojo::GetProxy(&renderer_
));
70 run_loop_
.reset(new base::RunLoop());
73 // MOCK_METHOD* doesn't support move only types. Work around this by having
75 MOCK_METHOD1(OnCdmInitializedInternal
, void(bool result
));
76 void OnCdmInitialized(interfaces::CdmPromiseResultPtr result
) {
77 OnCdmInitializedInternal(result
->success
);
80 void InitializeCdm(const std::string
& key_system
, bool expected_result
) {
81 EXPECT_CALL(*this, OnCdmInitializedInternal(expected_result
))
83 .WillOnce(InvokeWithoutArgs(run_loop_
.get(), &base::RunLoop::Quit
));
85 key_system
, kSecurityOrigin
, interfaces::CdmConfig::From(CdmConfig()),
86 1, base::Bind(&MediaAppTest::OnCdmInitialized
, base::Unretained(this)));
89 MOCK_METHOD1(OnRendererInitialized
, void(bool));
91 void InitializeRenderer(const VideoDecoderConfig
& video_config
,
92 bool expected_result
) {
93 video_demuxer_stream_
.set_video_decoder_config(video_config
);
95 interfaces::DemuxerStreamPtr video_stream
;
96 new MojoDemuxerStreamImpl(&video_demuxer_stream_
, GetProxy(&video_stream
));
98 interfaces::RendererClientPtr client_ptr
;
99 renderer_client_binding_
.Bind(GetProxy(&client_ptr
));
101 EXPECT_CALL(*this, OnRendererInitialized(expected_result
))
103 .WillOnce(InvokeWithoutArgs(run_loop_
.get(), &base::RunLoop::Quit
));
104 renderer_
->Initialize(client_ptr
.Pass(), nullptr, video_stream
.Pass(),
105 base::Bind(&MediaAppTest::OnRendererInitialized
,
106 base::Unretained(this)));
109 MOCK_METHOD0(ConnectionClosed
, void());
112 scoped_ptr
<base::RunLoop
> run_loop_
;
114 interfaces::ServiceFactoryPtr service_factory_
;
115 interfaces::ContentDecryptionModulePtr cdm_
;
116 interfaces::RendererPtr renderer_
;
118 StrictMock
<MockRendererClient
> renderer_client_
;
119 mojo::Binding
<interfaces::RendererClient
> renderer_client_binding_
;
121 StrictMock
<MockDemuxerStream
> video_demuxer_stream_
;
124 scoped_ptr
<mojo::ApplicationConnection
> connection_
;
126 DISALLOW_COPY_AND_ASSIGN(MediaAppTest
);
131 // Note: base::RunLoop::RunUntilIdle() does not work well in these tests because
132 // even when the loop is idle, we may still have pending events in the pipe.
134 TEST_F(MediaAppTest
, InitializeCdm_Success
) {
135 InitializeCdm(kClearKey
, true);
139 TEST_F(MediaAppTest
, InitializeCdm_InvalidKeySystem
) {
140 InitializeCdm(kInvalidKeySystem
, false);
144 TEST_F(MediaAppTest
, InitializeRenderer_Success
) {
145 InitializeRenderer(TestVideoConfig::Normal(), true);
149 TEST_F(MediaAppTest
, InitializeRenderer_InvalidConfig
) {
150 InitializeRenderer(TestVideoConfig::Invalid(), false);
154 TEST_F(MediaAppTest
, Lifetime
) {
155 // Disconnecting CDM and Renderer services doesn't terminate the app.
159 // Disconnecting ServiceFactory service should terminate the app, which will
160 // close the connection.
161 EXPECT_CALL(*this, ConnectionClosed())
163 .WillOnce(Invoke(run_loop_
.get(), &base::RunLoop::Quit
));
164 service_factory_
.reset();