summaryrefslogtreecommitdiffstats
path: root/chrome
diff options
context:
space:
mode:
authorhclam@chromium.org <hclam@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-06-04 22:45:12 +0000
committerhclam@chromium.org <hclam@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-06-04 22:45:12 +0000
commitdd3126b2cd8ee5796cf1c0ced7cebffe9a1f889a (patch)
tree31e6d4bbe05252969d1a41ba72600388acf7d9a9 /chrome
parent63314f53ab6dba058ea6ec50d0e037d8e8789391 (diff)
downloadchromium_src-dd3126b2cd8ee5796cf1c0ced7cebffe9a1f889a.zip
chromium_src-dd3126b2cd8ee5796cf1c0ced7cebffe9a1f889a.tar.gz
chromium_src-dd3126b2cd8ee5796cf1c0ced7cebffe9a1f889a.tar.bz2
Testing AudioMessageFilter
Unit test for AudioMessageFilter Review URL: http://codereview.chromium.org/119136 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@17685 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome')
-rw-r--r--chrome/chrome.gyp1
-rw-r--r--chrome/renderer/audio_message_filter.h4
-rw-r--r--chrome/renderer/audio_message_filter_unittest.cc193
-rw-r--r--chrome/test/unit/unittests.vcproj4
4 files changed, 202 insertions, 0 deletions
diff --git a/chrome/chrome.gyp b/chrome/chrome.gyp
index 7b2f92c..133a102 100644
--- a/chrome/chrome.gyp
+++ b/chrome/chrome.gyp
@@ -3235,6 +3235,7 @@
'common/time_format_unittest.cc',
'common/worker_thread_ticker_unittest.cc',
'common/zip_unittest.cc',
+ 'renderer/audio_message_filter_unittest.cc',
'renderer/extensions/extension_api_client_unittest.cc',
'renderer/extensions/greasemonkey_api_unittest.cc',
'renderer/extensions/json_schema_unittest.cc',
diff --git a/chrome/renderer/audio_message_filter.h b/chrome/renderer/audio_message_filter.h
index 46d46df..6e90edb 100644
--- a/chrome/renderer/audio_message_filter.h
+++ b/chrome/renderer/audio_message_filter.h
@@ -14,6 +14,7 @@
#include "base/shared_memory.h"
#include "chrome/common/ipc_channel_proxy.h"
#include "media/audio/audio_output.h"
+#include "testing/gtest/include/gtest/gtest_prod.h"
class AudioMessageFilter : public IPC::ChannelProxy::MessageFilter {
public:
@@ -48,6 +49,9 @@ class AudioMessageFilter : public IPC::ChannelProxy::MessageFilter {
MessageLoop* message_loop() { return message_loop_; }
private:
+ FRIEND_TEST(AudioMessageFilterTest, Basic);
+ FRIEND_TEST(AudioMessageFilterTest, Delegates);
+
// IPC::ChannelProxy::MessageFilter override. Called on IO thread.
virtual bool OnMessageReceived(const IPC::Message& message);
virtual void OnFilterAdded(IPC::Channel* channel);
diff --git a/chrome/renderer/audio_message_filter_unittest.cc b/chrome/renderer/audio_message_filter_unittest.cc
new file mode 100644
index 0000000..bc9fc8e
--- /dev/null
+++ b/chrome/renderer/audio_message_filter_unittest.cc
@@ -0,0 +1,193 @@
+// Copyright (c) 2009 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 "base/logging.h"
+#include "base/message_loop.h"
+#include "chrome/common/render_messages.h"
+#include "chrome/renderer/audio_message_filter.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace {
+
+class MockAudioDelegate : public AudioMessageFilter::Delegate {
+ public:
+ MockAudioDelegate() {
+ Reset();
+ }
+
+ virtual void OnRequestPacket() {
+ request_packet_received_ = true;
+ }
+
+ virtual void OnStateChanged(AudioOutputStream::State state, int info) {
+ state_changed_received_ = true;
+ state_ = state;
+ info_ = info;
+ }
+
+ virtual void OnCreated(base::SharedMemoryHandle handle, size_t length) {
+ created_received_ = true;
+ handle_ = handle;
+ length_ = length;
+ }
+
+ virtual void OnVolume(double left, double right) {
+ volume_received_ = true;
+ left_ = left;
+ right_ = right;
+ }
+
+ void Reset() {
+ request_packet_received_ = false;
+
+ state_changed_received_ = false;
+ state_ = AudioOutputStream::STATE_ERROR;
+ info_ = 0;
+
+ created_received_ = false;
+ handle_ = base::SharedMemory::NULLHandle();
+ length_ = 0;
+
+ volume_received_ = false;
+ left_ = 0;
+ right_ = 0;
+ }
+
+ bool request_packet_received() { return request_packet_received_; }
+
+ bool state_changed_received() { return state_changed_received_; }
+ AudioOutputStream::State state() { return state_; }
+ int info() { return info_; }
+
+ bool created_received() { return created_received_; }
+ base::SharedMemoryHandle handle() { return handle_; }
+ size_t length() { return length_; }
+
+ bool volume_received() { return volume_received_; }
+ double left() { return left_; }
+ double right() { return right_; }
+
+ private:
+ bool request_packet_received_;
+
+ bool state_changed_received_;
+ AudioOutputStream::State state_;
+ int info_;
+
+ bool created_received_;
+ base::SharedMemoryHandle handle_;
+ size_t length_;
+
+ bool volume_received_;
+ double left_;
+ double right_;
+
+ DISALLOW_COPY_AND_ASSIGN(MockAudioDelegate);
+};
+
+} // namespace
+
+TEST(AudioMessageFilterTest, Basic) {
+ MessageLoop message_loop(MessageLoop::TYPE_IO);
+
+ const int kRouteId = 0;
+ scoped_refptr<AudioMessageFilter> filter = new AudioMessageFilter(kRouteId);
+
+ MockAudioDelegate delegate;
+ int stream_id = filter->AddDelegate(&delegate);
+
+ // ViewMsg_RequestAudioPacket
+ EXPECT_FALSE(delegate.request_packet_received());
+ filter->OnMessageReceived(ViewMsg_RequestAudioPacket(kRouteId, stream_id));
+ EXPECT_TRUE(delegate.request_packet_received());
+ delegate.Reset();
+
+ // ViewMsg_NotifyAudioStreamStateChanged
+ const AudioOutputStream::State kState = AudioOutputStream::STATE_STARTED;
+ const int kStateInfo = 100;
+ EXPECT_FALSE(delegate.state_changed_received());
+ filter->OnMessageReceived(
+ ViewMsg_NotifyAudioStreamStateChanged(kRouteId,
+ stream_id,
+ kState,
+ kStateInfo));
+ EXPECT_TRUE(delegate.state_changed_received());
+ EXPECT_TRUE(kState == delegate.state());
+ EXPECT_EQ(kStateInfo, delegate.info());
+ delegate.Reset();
+
+ // ViewMsg_NotifyAudioStreamCreated
+ const size_t kLength = 1024;
+ EXPECT_FALSE(delegate.created_received());
+ filter->OnMessageReceived(
+ ViewMsg_NotifyAudioStreamCreated(kRouteId,
+ stream_id,
+ base::SharedMemory::NULLHandle(),
+ kLength));
+ EXPECT_TRUE(delegate.created_received());
+ EXPECT_FALSE(base::SharedMemory::IsHandleValid(delegate.handle()));
+ EXPECT_EQ(kLength, delegate.length());
+ delegate.Reset();
+
+ // ViewMsg_NotifyAudioStreamVolume
+ const double kLeftVolume = 1.0;
+ const double kRightVolume = 2.0;
+ EXPECT_FALSE(delegate.volume_received());
+ filter->OnMessageReceived(
+ ViewMsg_NotifyAudioStreamVolume(kRouteId, stream_id,
+ kLeftVolume, kRightVolume));
+ EXPECT_TRUE(delegate.volume_received());
+ EXPECT_EQ(kLeftVolume, delegate.left());
+ EXPECT_EQ(kRightVolume, delegate.right());
+ delegate.Reset();
+
+ message_loop.RunAllPending();
+}
+
+TEST(AudioMessageFilterTest, Delegates) {
+ MessageLoop message_loop(MessageLoop::TYPE_IO);
+
+ const int kRouteId = 0;
+ scoped_refptr<AudioMessageFilter> filter = new AudioMessageFilter(kRouteId);
+
+ MockAudioDelegate delegate1;
+ MockAudioDelegate delegate2;
+
+ int stream_id1 = filter->AddDelegate(&delegate1);
+ int stream_id2 = filter->AddDelegate(&delegate2);
+
+ // Send an IPC message. Make sure the correct delegate gets called.
+ EXPECT_FALSE(delegate1.request_packet_received());
+ EXPECT_FALSE(delegate2.request_packet_received());
+ filter->OnMessageReceived(ViewMsg_RequestAudioPacket(kRouteId, stream_id1));
+ EXPECT_TRUE(delegate1.request_packet_received());
+ EXPECT_FALSE(delegate2.request_packet_received());
+ delegate1.Reset();
+
+ EXPECT_FALSE(delegate1.request_packet_received());
+ EXPECT_FALSE(delegate2.request_packet_received());
+ filter->OnMessageReceived(ViewMsg_RequestAudioPacket(kRouteId, stream_id2));
+ EXPECT_FALSE(delegate1.request_packet_received());
+ EXPECT_TRUE(delegate2.request_packet_received());
+ delegate2.Reset();
+
+ // Send a message of a different route id, a message is not received.
+ EXPECT_FALSE(delegate1.request_packet_received());
+ filter->OnMessageReceived(ViewMsg_RequestAudioPacket(kRouteId + 1,
+ stream_id1));
+ EXPECT_FALSE(delegate1.request_packet_received());
+
+ // Remove the delegates. Make sure they won't get called.
+ filter->RemoveDelegate(stream_id1);
+ EXPECT_FALSE(delegate1.request_packet_received());
+ filter->OnMessageReceived(ViewMsg_RequestAudioPacket(kRouteId, stream_id1));
+ EXPECT_FALSE(delegate1.request_packet_received());
+
+ filter->RemoveDelegate(stream_id2);
+ EXPECT_FALSE(delegate2.request_packet_received());
+ filter->OnMessageReceived(ViewMsg_RequestAudioPacket(kRouteId, stream_id2));
+ EXPECT_FALSE(delegate2.request_packet_received());
+
+ message_loop.RunAllPending();
+}
diff --git a/chrome/test/unit/unittests.vcproj b/chrome/test/unit/unittests.vcproj
index 09ab8e0..7c91621 100644
--- a/chrome/test/unit/unittests.vcproj
+++ b/chrome/test/unit/unittests.vcproj
@@ -926,6 +926,10 @@
Name="renderer"
>
<File
+ RelativePath="..\..\renderer\audio_message_filter_unittest.cc"
+ >
+ </File>
+ <File
RelativePath="..\..\renderer\extensions\extension_api_client_unittest.cc"
>
</File>