Roll src/third_party/WebKit a26a5b9:5fc303f (svn 199161:199164)
[chromium-blink-merge.git] / media / mojo / services / media_apptest.cc
blob805ce955a79ff93872e74e6108f26c64eba8684a
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 "base/bind.h"
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/media_renderer.mojom.h"
16 #include "media/mojo/services/media_type_converters.h"
17 #include "media/mojo/services/mojo_demuxer_stream_impl.h"
18 #include "mojo/application/public/cpp/application_connection.h"
19 #include "mojo/application/public/cpp/application_impl.h"
20 #include "mojo/application/public/cpp/application_test_base.h"
21 #include "testing/gmock/include/gmock/gmock.h"
23 using testing::Exactly;
24 using testing::Invoke;
25 using testing::InvokeWithoutArgs;
26 using testing::StrictMock;
28 namespace media {
29 namespace {
31 const char kInvalidKeySystem[] = "invalid.key.system";
32 const char kSecurityOrigin[] = "http://foo.com";
34 class MockMediaRendererClient : public interfaces::MediaRendererClient {
35 public:
36 MockMediaRendererClient(){};
37 ~MockMediaRendererClient() override{};
39 // interfaces::MediaRendererClient implementation.
40 MOCK_METHOD2(OnTimeUpdate, void(int64_t time_usec, int64_t max_time_usec));
41 MOCK_METHOD1(OnBufferingStateChange, void(interfaces::BufferingState state));
42 MOCK_METHOD0(OnEnded, void());
43 MOCK_METHOD0(OnError, void());
45 private:
46 DISALLOW_COPY_AND_ASSIGN(MockMediaRendererClient);
49 class MediaAppTest : public mojo::test::ApplicationTestBase {
50 public:
51 MediaAppTest()
52 : media_renderer_client_binding_(&media_renderer_client_),
53 video_demuxer_stream_(DemuxerStream::VIDEO) {}
54 ~MediaAppTest() override {}
56 void SetUp() override {
57 ApplicationTestBase::SetUp();
59 mojo::URLRequestPtr request = mojo::URLRequest::New();
60 request->url = "mojo:media";
61 mojo::ApplicationConnection* connection =
62 application_impl()->ConnectToApplication(request.Pass());
64 connection->ConnectToService(&cdm_);
65 connection->ConnectToService(&media_renderer_);
67 run_loop_.reset(new base::RunLoop());
70 // MOCK_METHOD* doesn't support move only types. Work around this by having
71 // an extra method.
72 MOCK_METHOD1(OnCdmInitializedInternal, void(bool result));
73 void OnCdmInitialized(interfaces::CdmPromiseResultPtr result) {
74 OnCdmInitializedInternal(result->success);
77 void InitializeCdm(const std::string& key_system, bool expected_result) {
78 EXPECT_CALL(*this, OnCdmInitializedInternal(expected_result))
79 .Times(Exactly(1))
80 .WillOnce(InvokeWithoutArgs(run_loop_.get(), &base::RunLoop::Quit));
81 cdm_->Initialize(
82 key_system, kSecurityOrigin, interfaces::CdmConfig::From(CdmConfig()),
83 1, base::Bind(&MediaAppTest::OnCdmInitialized, base::Unretained(this)));
86 MOCK_METHOD1(OnRendererInitialized, void(bool));
88 void InitializeRenderer(const VideoDecoderConfig& video_config,
89 bool expected_result) {
90 video_demuxer_stream_.set_video_decoder_config(video_config);
92 interfaces::DemuxerStreamPtr video_stream;
93 new MojoDemuxerStreamImpl(&video_demuxer_stream_, GetProxy(&video_stream));
95 interfaces::MediaRendererClientPtr client_ptr;
96 media_renderer_client_binding_.Bind(GetProxy(&client_ptr));
98 EXPECT_CALL(*this, OnRendererInitialized(expected_result))
99 .Times(Exactly(1))
100 .WillOnce(InvokeWithoutArgs(run_loop_.get(), &base::RunLoop::Quit));
101 media_renderer_->Initialize(client_ptr.Pass(), nullptr, video_stream.Pass(),
102 base::Bind(&MediaAppTest::OnRendererInitialized,
103 base::Unretained(this)));
106 protected:
107 scoped_ptr<base::RunLoop> run_loop_;
109 interfaces::ContentDecryptionModulePtr cdm_;
110 interfaces::MediaRendererPtr media_renderer_;
112 StrictMock<MockMediaRendererClient> media_renderer_client_;
113 mojo::Binding<interfaces::MediaRendererClient> media_renderer_client_binding_;
115 StrictMock<MockDemuxerStream> video_demuxer_stream_;
117 private:
118 DISALLOW_COPY_AND_ASSIGN(MediaAppTest);
121 } // namespace
123 // Note: base::RunLoop::RunUntilIdle() does not work well in these tests because
124 // even when the loop is idle, we may still have pending events in the pipe.
126 TEST_F(MediaAppTest, InitializeCdm_Success) {
127 InitializeCdm(kClearKey, true);
128 run_loop_->Run();
131 TEST_F(MediaAppTest, InitializeCdm_InvalidKeySystem) {
132 InitializeCdm(kInvalidKeySystem, false);
133 run_loop_->Run();
136 TEST_F(MediaAppTest, InitializeRenderer_Success) {
137 InitializeRenderer(TestVideoConfig::Normal(), true);
138 run_loop_->Run();
141 TEST_F(MediaAppTest, InitializeRenderer_InvalidConfig) {
142 InitializeRenderer(TestVideoConfig::Invalid(), false);
143 run_loop_->Run();
146 } // namespace media