Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1815)

Unified Diff: media/mojo/services/mojo_audio_output_unittest.cc

Issue 2697793002: Add mojo interface+impl for audio stream control. (Closed)
Patch Set: Dale's comments and add MEDIA_MOJO_EXPORT Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: media/mojo/services/mojo_audio_output_unittest.cc
diff --git a/media/mojo/services/mojo_audio_output_unittest.cc b/media/mojo/services/mojo_audio_output_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..17095b2b59a40858e34ff3a6ba3ae33040c9a886
--- /dev/null
+++ b/media/mojo/services/mojo_audio_output_unittest.cc
@@ -0,0 +1,328 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "media/mojo/services/mojo_audio_output.h"
+
+#include <utility>
+
+#include "base/memory/shared_memory.h"
+#include "base/message_loop/message_loop.h"
+#include "base/run_loop.h"
+#include "base/sync_socket.h"
+#include "media/audio/audio_output_controller.h"
+#include "media/mojo/interfaces/audio_output.mojom.h"
+#include "mojo/public/cpp/system/platform_handle.h"
+#include "testing/gmock/include/gmock/gmock.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace media {
+
+namespace {
+
+const double kNewVolume = 0.618;
+// Not actually used, but sent from the AudioOutputDelegate.
+const int kStreamId = 0;
+
+using testing::_;
+using testing::Mock;
+using testing::NotNull;
+using testing::Return;
+using testing::SaveArg;
+using testing::StrictMock;
+using testing::Test;
+using AudioOutput = mojom::AudioOutput;
+using AudioOutputPtr = mojo::InterfacePtr<AudioOutput>;
+
+class MockDelegate : public AudioOutputDelegate {
+ public:
+ MOCK_CONST_METHOD0(GetController, scoped_refptr<AudioOutputController>());
+ MOCK_CONST_METHOD0(GetStreamId, int());
+ MOCK_METHOD0(OnPlayStream, void());
+ MOCK_METHOD0(OnPauseStream, void());
+ MOCK_METHOD1(OnSetVolume, void(double));
+};
+
+class MockDelegateFactory {
+ public:
+ void PrepareDelegateForCreation(
+ std::unique_ptr<AudioOutputDelegate> delegate) {
+ ASSERT_EQ(nullptr, delegate_);
+ delegate_.swap(delegate);
+ }
+
+ std::unique_ptr<AudioOutputDelegate> CreateDelegate(
+ AudioOutputDelegate::EventHandler* handler,
+ const AudioParameters& params) {
+ MockCreateDelegate(handler, params);
+ EXPECT_NE(nullptr, delegate_);
+ return std::move(delegate_);
+ }
+
+ MOCK_METHOD2(MockCreateDelegate,
+ void(AudioOutputDelegate::EventHandler*,
+ const AudioParameters&));
+
+ private:
+ std::unique_ptr<AudioOutputDelegate> delegate_;
+};
+
+class MockFinishedListener {
+ public:
+ MOCK_METHOD1(Finished, void(AudioOutput*));
+};
+
+class MockClient {
+ public:
+ MockClient() {}
+
+ void Initialized(mojo::ScopedSharedBufferHandle shared_buffer,
+ mojo::ScopedHandle socket_handle) {
+ ASSERT_TRUE(shared_buffer.is_valid());
+ ASSERT_TRUE(socket_handle.is_valid());
+
+ base::PlatformFile fd;
+ mojo::UnwrapPlatformFile(std::move(socket_handle), &fd);
+ socket_ = base::MakeUnique<base::CancelableSyncSocket>(fd);
+ EXPECT_NE(socket_->handle(), base::CancelableSyncSocket::kInvalidHandle);
+
+ size_t memory_length;
+ base::SharedMemoryHandle shmem_handle;
+ bool read_only;
+ EXPECT_EQ(
+ mojo::UnwrapSharedMemoryHandle(std::move(shared_buffer), &shmem_handle,
+ &memory_length, &read_only),
+ MOJO_RESULT_OK);
+ EXPECT_FALSE(read_only);
+ buffer_ = base::MakeUnique<base::SharedMemory>(shmem_handle, read_only);
+
+ GotNotification();
+ }
+
+ MOCK_METHOD0(GotNotification, void());
+
+ private:
+ std::unique_ptr<base::SharedMemory> buffer_;
+ std::unique_ptr<base::CancelableSyncSocket> socket_;
+};
+} // namespace
+
+class MojoAudioOutputTest : public Test {
+ public:
+ MojoAudioOutputTest()
+ : impl_(base::MakeUnique<MojoAudioOutput>(
+ mojo::MakeRequest(&audio_output_ptr_),
+ base::BindOnce(&MockDelegateFactory::CreateDelegate,
+ base::Unretained(&mock_delegate_factory_)),
+ base::BindOnce(&MockFinishedListener::Finished,
+ base::Unretained(&listener_)))) {}
+
+ void ExpectDelegateCreation() {
+ delegate_ = new StrictMock<MockDelegate>();
+ mock_delegate_factory_.PrepareDelegateForCreation(
+ base::WrapUnique(delegate_));
+ EXPECT_TRUE(
+ base::CancelableSyncSocket::CreatePair(&local_, &foreign_socket_));
+ EXPECT_TRUE(mem_.CreateAnonymous(100));
+ EXPECT_CALL(mock_delegate_factory_, MockCreateDelegate(NotNull(), _))
+ .WillOnce(SaveArg<0>(&delegate_event_handler_));
+ }
+
+ base::MessageLoop loop_;
+ base::CancelableSyncSocket local_, foreign_socket_;
+ base::SharedMemory mem_;
+ StrictMock<MockDelegate>* delegate_ = nullptr;
+ AudioOutputDelegate::EventHandler* delegate_event_handler_ = nullptr;
+ StrictMock<MockDelegateFactory> mock_delegate_factory_;
+ StrictMock<MockFinishedListener> listener_;
+ AudioOutputPtr audio_output_ptr_;
+ std::unique_ptr<MojoAudioOutput> impl_;
+};
+
+TEST_F(MojoAudioOutputTest, Play_Plays) {
+ ExpectDelegateCreation();
+ EXPECT_CALL(*delegate_, OnPlayStream());
+ MockClient client;
+ audio_output_ptr_->Initialize(
+ AudioParameters::UnavailableDeviceParams(),
+ base::Bind(&MockClient::Initialized, base::Unretained(&client)));
+ audio_output_ptr_->Play();
+ base::RunLoop().RunUntilIdle();
+}
+
+TEST_F(MojoAudioOutputTest, Pause_Pauses) {
+ ExpectDelegateCreation();
+ EXPECT_CALL(*delegate_, OnPauseStream());
+ MockClient client;
+ audio_output_ptr_->Initialize(
+ AudioParameters::UnavailableDeviceParams(),
+ base::Bind(&MockClient::Initialized, base::Unretained(&client)));
+ audio_output_ptr_->Pause();
+ base::RunLoop().RunUntilIdle();
+}
+
+TEST_F(MojoAudioOutputTest, SetVolume_SetsVolume) {
+ ExpectDelegateCreation();
+ EXPECT_CALL(*delegate_, OnSetVolume(kNewVolume));
+ MockClient client;
+ audio_output_ptr_->Initialize(
+ AudioParameters::UnavailableDeviceParams(),
+ base::Bind(&MockClient::Initialized, base::Unretained(&client)));
+ audio_output_ptr_->SetVolume(kNewVolume);
+ base::RunLoop().RunUntilIdle();
+}
+
+TEST_F(MojoAudioOutputTest, DestructWithCallPending_CancelsCall) {
+ ExpectDelegateCreation();
+ MockClient client;
+ audio_output_ptr_->Initialize(
+ AudioParameters::UnavailableDeviceParams(),
+ base::Bind(&MockClient::Initialized, base::Unretained(&client)));
+ base::RunLoop().RunUntilIdle();
+
+ ASSERT_NE(nullptr, delegate_event_handler_);
+ delegate_event_handler_->OnStreamCreated(kStreamId, &mem_, &foreign_socket_);
+ audio_output_ptr_->Play();
+ impl_.reset();
+ base::RunLoop().RunUntilIdle();
+}
+
+TEST_F(MojoAudioOutputTest, Initialize_NotifiesClient) {
+ ExpectDelegateCreation();
+ MockClient client;
+ audio_output_ptr_->Initialize(
+ AudioParameters::UnavailableDeviceParams(),
+ base::Bind(&MockClient::Initialized, base::Unretained(&client)));
+ base::RunLoop().RunUntilIdle();
+
+ EXPECT_CALL(client, GotNotification());
+
+ ASSERT_NE(nullptr, delegate_event_handler_);
+ delegate_event_handler_->OnStreamCreated(kStreamId, &mem_, &foreign_socket_);
+
+ base::RunLoop().RunUntilIdle();
+}
+
+TEST_F(MojoAudioOutputTest, PlayBeforeInitialize_Error) {
+ EXPECT_CALL(listener_, Finished(impl_.get()));
+ audio_output_ptr_->Play();
+ base::RunLoop().RunUntilIdle();
+}
+
+TEST_F(MojoAudioOutputTest, PauseBeforeInitialize_Error) {
+ EXPECT_CALL(listener_, Finished(impl_.get()));
+ audio_output_ptr_->Pause();
+ base::RunLoop().RunUntilIdle();
+}
+
+TEST_F(MojoAudioOutputTest, SetVolumeBeforeInitialize_Error) {
+ EXPECT_CALL(listener_, Finished(impl_.get()));
+ audio_output_ptr_->SetVolume(kNewVolume);
+ base::RunLoop().RunUntilIdle();
+}
+
+TEST_F(MojoAudioOutputTest, SetVolumeTooLarge_Error) {
+ ExpectDelegateCreation();
+ EXPECT_CALL(listener_, Finished(impl_.get()));
+ MockClient client;
+ audio_output_ptr_->Initialize(
+ AudioParameters::UnavailableDeviceParams(),
+ base::Bind(&MockClient::Initialized, base::Unretained(&client)));
+ audio_output_ptr_->SetVolume(15);
+ base::RunLoop().RunUntilIdle();
+ ASSERT_NE(nullptr, delegate_event_handler_);
+ delegate_event_handler_->OnStreamCreated(kStreamId, &mem_, &foreign_socket_);
+
+ base::RunLoop().RunUntilIdle();
+}
+
+TEST_F(MojoAudioOutputTest, SetVolumeNegative_Error) {
+ ExpectDelegateCreation();
+ EXPECT_CALL(listener_, Finished(impl_.get()));
+ MockClient client;
+ audio_output_ptr_->Initialize(
+ AudioParameters::UnavailableDeviceParams(),
+ base::Bind(&MockClient::Initialized, base::Unretained(&client)));
+ audio_output_ptr_->SetVolume(-0.5);
+ base::RunLoop().RunUntilIdle();
+ ASSERT_NE(nullptr, delegate_event_handler_);
+ delegate_event_handler_->OnStreamCreated(kStreamId, &mem_, &foreign_socket_);
+
+ base::RunLoop().RunUntilIdle();
+}
+
+TEST_F(MojoAudioOutputTest, InitializeStart_Error) {
+ MockClient client;
+ ExpectDelegateCreation();
+ EXPECT_CALL(listener_, Finished(impl_.get()));
+ audio_output_ptr_->Initialize(
+ AudioParameters::UnavailableDeviceParams(),
+ base::Bind(&MockClient::Initialized, base::Unretained(&client)));
+ audio_output_ptr_->Initialize(
+ AudioParameters::UnavailableDeviceParams(),
+ base::Bind(&MockClient::Initialized, base::Unretained(&client)));
+ base::RunLoop().RunUntilIdle();
+}
+
+TEST_F(MojoAudioOutputTest, DelegateErrorBeforeCreated_PropagatesError) {
+ ExpectDelegateCreation();
+ MockClient client;
+ EXPECT_CALL(listener_, Finished(impl_.get()));
+
+ audio_output_ptr_->Initialize(
+ AudioParameters::UnavailableDeviceParams(),
+ base::Bind(&MockClient::Initialized, base::Unretained(&client)));
+ base::RunLoop().RunUntilIdle();
+
+ ASSERT_NE(nullptr, delegate_event_handler_);
+ delegate_event_handler_->OnStreamError(kStreamId);
+
+ base::RunLoop().RunUntilIdle();
+}
+
+TEST_F(MojoAudioOutputTest, DelegateErrorAfterCreated_PropagatesError) {
+ ExpectDelegateCreation();
+ MockClient client;
+ EXPECT_CALL(listener_, Finished(impl_.get()));
+
+ audio_output_ptr_->Initialize(
+ AudioParameters::UnavailableDeviceParams(),
+ base::Bind(&MockClient::Initialized, base::Unretained(&client)));
+ base::RunLoop().RunUntilIdle();
+
+ ASSERT_NE(nullptr, delegate_event_handler_);
+ delegate_event_handler_->OnStreamCreated(kStreamId, &mem_, &foreign_socket_);
+ delegate_event_handler_->OnStreamError(kStreamId);
+
+ base::RunLoop().RunUntilIdle();
+}
+
+TEST_F(MojoAudioOutputTest, RemoteEndGone_Error) {
+ EXPECT_CALL(listener_, Finished(impl_.get()));
+ audio_output_ptr_.reset();
+ base::RunLoop().RunUntilIdle();
+ Mock::VerifyAndClear(&listener_);
+}
+
+TEST_F(MojoAudioOutputTest,
+ RemoteEndGoneWhileWaitingForDelegateCreation_Error) {
+ // Tests the following sequence of events:
+ // MojoAudioOutput is constructed and started.
+ // MojoAudioOutput gets connection error.
+ // MojoAudioOutput gets event that delegate has finished creating.
+ ExpectDelegateCreation();
+ MockClient client;
+ EXPECT_CALL(listener_, Finished(impl_.get()));
+ audio_output_ptr_->Initialize(
+ AudioParameters::UnavailableDeviceParams(),
+ base::Bind(&MockClient::Initialized, base::Unretained(&client)));
+ base::RunLoop().RunUntilIdle();
+ audio_output_ptr_.reset();
+
+ ASSERT_NE(nullptr, delegate_event_handler_);
+ delegate_event_handler_->OnStreamCreated(kStreamId, &mem_, &foreign_socket_);
+ base::RunLoop().RunUntilIdle();
+ Mock::VerifyAndClear(&listener_);
+}
+
+} // namespace media
« media/mojo/services/mojo_audio_output.cc ('K') | « media/mojo/services/mojo_audio_output.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698