Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / media / mojo / services / media_apptest.cc
blob34f855d03d52b93a5cadb0538eaaee6b3f95151c
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/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;
29 namespace media {
30 namespace {
32 const char kInvalidKeySystem[] = "invalid.key.system";
33 const char kSecurityOrigin[] = "http://foo.com";
35 class MockMediaRendererClient : public interfaces::MediaRendererClient {
36 public:
37 MockMediaRendererClient(){};
38 ~MockMediaRendererClient() override{};
40 // interfaces::MediaRendererClient 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());
46 private:
47 DISALLOW_COPY_AND_ASSIGN(MockMediaRendererClient);
50 class MediaAppTest : public mojo::test::ApplicationTestBase {
51 public:
52 MediaAppTest()
53 : media_renderer_client_binding_(&media_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());
65 connection->ConnectToService(&service_factory_);
66 service_factory_->CreateCdm(mojo::GetProxy(&cdm_));
67 service_factory_->CreateRenderer(mojo::GetProxy(&media_renderer_));
69 run_loop_.reset(new base::RunLoop());
72 // MOCK_METHOD* doesn't support move only types. Work around this by having
73 // an extra method.
74 MOCK_METHOD1(OnCdmInitializedInternal, void(bool result));
75 void OnCdmInitialized(interfaces::CdmPromiseResultPtr result) {
76 OnCdmInitializedInternal(result->success);
79 void InitializeCdm(const std::string& key_system, bool expected_result) {
80 EXPECT_CALL(*this, OnCdmInitializedInternal(expected_result))
81 .Times(Exactly(1))
82 .WillOnce(InvokeWithoutArgs(run_loop_.get(), &base::RunLoop::Quit));
83 cdm_->Initialize(
84 key_system, kSecurityOrigin, interfaces::CdmConfig::From(CdmConfig()),
85 1, base::Bind(&MediaAppTest::OnCdmInitialized, base::Unretained(this)));
88 MOCK_METHOD1(OnRendererInitialized, void(bool));
90 void InitializeRenderer(const VideoDecoderConfig& video_config,
91 bool expected_result) {
92 video_demuxer_stream_.set_video_decoder_config(video_config);
94 interfaces::DemuxerStreamPtr video_stream;
95 new MojoDemuxerStreamImpl(&video_demuxer_stream_, GetProxy(&video_stream));
97 interfaces::MediaRendererClientPtr client_ptr;
98 media_renderer_client_binding_.Bind(GetProxy(&client_ptr));
100 EXPECT_CALL(*this, OnRendererInitialized(expected_result))
101 .Times(Exactly(1))
102 .WillOnce(InvokeWithoutArgs(run_loop_.get(), &base::RunLoop::Quit));
103 media_renderer_->Initialize(client_ptr.Pass(), nullptr, video_stream.Pass(),
104 base::Bind(&MediaAppTest::OnRendererInitialized,
105 base::Unretained(this)));
108 protected:
109 scoped_ptr<base::RunLoop> run_loop_;
111 interfaces::ServiceFactoryPtr service_factory_;
112 interfaces::ContentDecryptionModulePtr cdm_;
113 interfaces::MediaRendererPtr media_renderer_;
115 StrictMock<MockMediaRendererClient> media_renderer_client_;
116 mojo::Binding<interfaces::MediaRendererClient> media_renderer_client_binding_;
118 StrictMock<MockDemuxerStream> video_demuxer_stream_;
120 private:
121 DISALLOW_COPY_AND_ASSIGN(MediaAppTest);
124 } // namespace
126 // Note: base::RunLoop::RunUntilIdle() does not work well in these tests because
127 // even when the loop is idle, we may still have pending events in the pipe.
129 TEST_F(MediaAppTest, InitializeCdm_Success) {
130 InitializeCdm(kClearKey, true);
131 run_loop_->Run();
134 TEST_F(MediaAppTest, InitializeCdm_InvalidKeySystem) {
135 InitializeCdm(kInvalidKeySystem, false);
136 run_loop_->Run();
139 TEST_F(MediaAppTest, InitializeRenderer_Success) {
140 InitializeRenderer(TestVideoConfig::Normal(), true);
141 run_loop_->Run();
144 TEST_F(MediaAppTest, InitializeRenderer_InvalidConfig) {
145 InitializeRenderer(TestVideoConfig::Invalid(), false);
146 run_loop_->Run();
149 } // namespace media