| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/message_loop.h" | 5 #include "base/message_loop.h" |
| 6 #include "base/process.h" | 6 #include "base/process.h" |
| 7 #include "base/process_util.h" | |
| 8 #include "base/scoped_ptr.h" | 7 #include "base/scoped_ptr.h" |
| 9 #include "chrome/browser/renderer_host/audio_renderer_host.h" | 8 #include "chrome/browser/renderer_host/audio_renderer_host.h" |
| 10 #include "chrome/common/render_messages.h" | |
| 11 #include "testing/gmock/include/gmock/gmock.h" | |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
| 13 | 10 |
| 14 using ::testing::_; | |
| 15 using ::testing::DoAll; | |
| 16 using ::testing::InSequence; | |
| 17 using ::testing::Return; | |
| 18 using ::testing::SetArgumentPointee; | |
| 19 | |
| 20 namespace { | |
| 21 | |
| 22 const int kInvalidId = -1; | |
| 23 const int kProcessId = 100; | |
| 24 const int kRouteId = 200; | |
| 25 const int kBufferCapacity = 65536; | |
| 26 const int kPacketSize = 16384; | |
| 27 | |
| 28 } // namespace | |
| 29 | |
| 30 class MockAudioRendererHost : public AudioRendererHost { | |
| 31 public: | |
| 32 MockAudioRendererHost(MessageLoop* loop) | |
| 33 : AudioRendererHost(loop) { | |
| 34 } | |
| 35 | |
| 36 // A list of mock methods. | |
| 37 MOCK_METHOD4(OnRequestPacket, | |
| 38 void(int routing_id, int stream_id, | |
| 39 size_t bytes_in_buffer, int64 message_timestamp)); | |
| 40 | |
| 41 MOCK_METHOD3(OnStreamCreated, | |
| 42 void(int routing_id, int stream_id, int length)); | |
| 43 | |
| 44 MOCK_METHOD4(OnStreamStateChanged, | |
| 45 void(int routing_id, int stream_id, | |
| 46 AudioOutputStream::State state, int info)); | |
| 47 | |
| 48 MOCK_METHOD4(OnStreamVolume, | |
| 49 void(int routing_id, int stream_id, double left, double right)); | |
| 50 | |
| 51 base::SharedMemory* shared_memory() { return shared_memory_.get(); } | |
| 52 | |
| 53 protected: | |
| 54 // This method is used to dispatch IPC messages to the renderer. We intercept | |
| 55 // these messages here and dispatch to our mock methods to verify the | |
| 56 // conversation between this object and the renderer. | |
| 57 virtual void Send(IPC::Message* message) { | |
| 58 CHECK(message); | |
| 59 | |
| 60 // In this method we dispatch the messages to the according handlers as if | |
| 61 // we are the renderer. | |
| 62 bool handled = true; | |
| 63 IPC_BEGIN_MESSAGE_MAP(MockAudioRendererHost, *message) | |
| 64 IPC_MESSAGE_HANDLER(ViewMsg_RequestAudioPacket, OnRequestPacket) | |
| 65 IPC_MESSAGE_HANDLER(ViewMsg_NotifyAudioStreamCreated, OnStreamCreated) | |
| 66 IPC_MESSAGE_HANDLER(ViewMsg_NotifyAudioStreamStateChanged, | |
| 67 OnStreamStateChanged) | |
| 68 IPC_MESSAGE_HANDLER(ViewMsg_NotifyAudioStreamVolume, OnStreamVolume) | |
| 69 IPC_MESSAGE_UNHANDLED(handled = false) | |
| 70 IPC_END_MESSAGE_MAP() | |
| 71 EXPECT_TRUE(handled); | |
| 72 | |
| 73 delete message; | |
| 74 } | |
| 75 | |
| 76 private: | |
| 77 // These handler methods do minimal things and delegate to the mock methods. | |
| 78 void OnRequestPacket(const IPC::Message& msg, int stream_id, | |
| 79 size_t bytes_in_buffer, int64 message_timestamp) { | |
| 80 OnRequestPacket(msg.routing_id(), stream_id, bytes_in_buffer, | |
| 81 message_timestamp); | |
| 82 } | |
| 83 | |
| 84 void OnStreamCreated(const IPC::Message& msg, int stream_id, | |
| 85 base::SharedMemoryHandle handle, int length) { | |
| 86 // Maps the shared memory. | |
| 87 shared_memory_.reset(new base::SharedMemory(handle, true)); | |
| 88 CHECK(shared_memory_->Map(length)); | |
| 89 CHECK(shared_memory_->memory()); | |
| 90 | |
| 91 // And then delegate the call to the mock method. | |
| 92 OnStreamCreated(msg.routing_id(), stream_id, length); | |
| 93 } | |
| 94 | |
| 95 void OnStreamStateChanged(const IPC::Message& msg, int stream_id, | |
| 96 AudioOutputStream::State state, int info) { | |
| 97 OnStreamStateChanged(msg.routing_id(), stream_id, state, info); | |
| 98 } | |
| 99 | |
| 100 void OnStreamVolume(const IPC::Message& msg, int stream_id, | |
| 101 double left, double right) { | |
| 102 OnStreamVolume(msg.routing_id(), stream_id, left, right); | |
| 103 } | |
| 104 | |
| 105 scoped_ptr<base::SharedMemory> shared_memory_; | |
| 106 | |
| 107 DISALLOW_COPY_AND_ASSIGN(MockAudioRendererHost); | |
| 108 }; | |
| 109 | |
| 110 class AudioRendererHostTest : public testing::Test { | 11 class AudioRendererHostTest : public testing::Test { |
| 111 public: | |
| 112 AudioRendererHostTest() | |
| 113 : current_stream_id_(0) { | |
| 114 } | |
| 115 | |
| 116 protected: | 12 protected: |
| 117 virtual void SetUp() { | 13 virtual void SetUp() { |
| 118 // Create a message loop so AudioRendererHost can use it. | 14 // Create a message loop so AudioRendererHost can use it. |
| 119 message_loop_.reset(new MessageLoop(MessageLoop::TYPE_IO)); | 15 message_loop_.reset(new MessageLoop(MessageLoop::TYPE_IO)); |
| 120 host_ = new MockAudioRendererHost(message_loop_.get()); | 16 host_ = new AudioRendererHost(message_loop_.get()); |
| 121 CHECK(host_); | |
| 122 } | 17 } |
| 123 | 18 |
| 124 virtual void TearDown() { | 19 virtual void TearDown() { |
| 125 // This task post a task to message_loop_ to do internal destruction on | 20 // This task post a task to message_loop_ to do internal destruction on |
| 126 // message_loop_. | 21 // message_loop_. |
| 127 host_->Destroy(); | 22 host_->Destroy(); |
| 128 // We need to continue running message_loop_ to complete all destructions. | 23 // We need to continue running message_loop_ to complete all destructions. |
| 129 message_loop_->RunAllPending(); | 24 message_loop_->RunAllPending(); |
| 130 } | 25 } |
| 131 | 26 |
| 132 AudioRendererHost::IPCAudioSource* CreateAudioStream( | 27 scoped_refptr<AudioRendererHost> host_; |
| 133 AudioManager::Format format) { | |
| 134 InSequence s; | |
| 135 | |
| 136 // 1. We will first receive a OnStreamCreated() signal. | |
| 137 EXPECT_CALL(*host_, | |
| 138 OnStreamCreated(kRouteId, current_stream_id_, kPacketSize)); | |
| 139 | |
| 140 // 2. First packet request will arrive. This request is sent by | |
| 141 // IPCAudioSource::CreateIPCAudioSource to start buffering. | |
| 142 EXPECT_CALL(*host_, OnRequestPacket(kRouteId, current_stream_id_, 0, _)); | |
| 143 | |
| 144 AudioRendererHost::IPCAudioSource* source = | |
| 145 AudioRendererHost::IPCAudioSource::CreateIPCAudioSource( | |
| 146 host_, | |
| 147 kProcessId, | |
| 148 kRouteId, | |
| 149 current_stream_id_, | |
| 150 base::GetCurrentProcessHandle(), | |
| 151 format, | |
| 152 2, | |
| 153 AudioManager::kAudioCDSampleRate, | |
| 154 16, | |
| 155 kPacketSize, | |
| 156 kBufferCapacity); | |
| 157 EXPECT_TRUE(source); | |
| 158 EXPECT_EQ(kProcessId, source->process_id()); | |
| 159 EXPECT_EQ(kRouteId, source->route_id()); | |
| 160 EXPECT_EQ(current_stream_id_, source->stream_id()); | |
| 161 return source; | |
| 162 } | |
| 163 | |
| 164 AudioRendererHost::IPCAudioSource* CreateRealStream() { | |
| 165 return CreateAudioStream(AudioManager::AUDIO_PCM_LINEAR); | |
| 166 } | |
| 167 | |
| 168 AudioRendererHost::IPCAudioSource* CreateMockStream() { | |
| 169 return CreateAudioStream(AudioManager::AUDIO_MOCK); | |
| 170 } | |
| 171 | |
| 172 int current_stream_id_; | |
| 173 scoped_refptr<MockAudioRendererHost> host_; | |
| 174 scoped_ptr<MessageLoop> message_loop_; | 28 scoped_ptr<MessageLoop> message_loop_; |
| 175 | |
| 176 private: | |
| 177 DISALLOW_COPY_AND_ASSIGN(AudioRendererHostTest); | |
| 178 }; | 29 }; |
| 179 | 30 |
| 180 // Audio output stream only works stably on windows. Also there's no mock | 31 TEST_F(AudioRendererHostTest, NoTest) { |
| 181 // audio output streams for mac and linux. | 32 // TODO(hclam): come up with useful tests. |
| 182 // TODO(hclam): make these tests work on mac and linux. | |
| 183 #if defined(OS_WIN) | |
| 184 | |
| 185 TEST_F(AudioRendererHostTest, CreateMockStream) { | |
| 186 scoped_ptr<AudioRendererHost::IPCAudioSource> source(CreateMockStream()); | |
| 187 } | 33 } |
| 188 | |
| 189 TEST_F(AudioRendererHostTest, MockStreamDataConversation) { | |
| 190 scoped_ptr<AudioRendererHost::IPCAudioSource> source(CreateMockStream()); | |
| 191 | |
| 192 // We will receive packet requests until the buffer is full. We first send | |
| 193 // three packets of 16KB, then we send packets of 1KB until the buffer is | |
| 194 // full. Then there will no more packet requests. | |
| 195 EXPECT_CALL(*host_, | |
| 196 OnRequestPacket(kRouteId, current_stream_id_, kPacketSize, _)); | |
| 197 EXPECT_CALL(*host_, | |
| 198 OnRequestPacket(kRouteId, current_stream_id_, 2 * kPacketSize, _)); | |
| 199 for (int size = 3 * kPacketSize; size < kBufferCapacity; size += 1024) { | |
| 200 EXPECT_CALL(*host_, OnRequestPacket(kRouteId, current_stream_id_, size, _)); | |
| 201 } | |
| 202 | |
| 203 source->NotifyPacketReady(kPacketSize); | |
| 204 source->NotifyPacketReady(kPacketSize); | |
| 205 source->NotifyPacketReady(kPacketSize); | |
| 206 for (int size = 0; size < kPacketSize; size += 1024) { | |
| 207 source->NotifyPacketReady(1024); | |
| 208 } | |
| 209 } | |
| 210 | |
| 211 #endif | |
| OLD | NEW |