Index: components/copresence/handlers/audio/audio_directive_handler_unittest.cc |
diff --git a/components/copresence/handlers/audio/audio_directive_handler_unittest.cc b/components/copresence/handlers/audio/audio_directive_handler_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..b534fb62e401f3805bc323381c25a169bcb9391d |
--- /dev/null |
+++ b/components/copresence/handlers/audio/audio_directive_handler_unittest.cc |
@@ -0,0 +1,107 @@ |
+// Copyright 2014 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 "components/copresence/handlers/audio/audio_directive_handler.h" |
+ |
+#include <cstdlib> |
+ |
+#include "base/bind.h" |
+#include "base/message_loop/message_loop.h" |
+#include "media/base/audio_bus.h" |
+#include "testing/gmock/include/gmock/gmock.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+using ::testing::_; |
+using ::testing::Le; |
+ |
+namespace copresence { |
+ |
+class MockAudioDirectiveHandler : public AudioDirectiveHandler { |
+ public: |
+ MockAudioDirectiveHandler( |
+ const AudioDirectiveList::EncodeTokenCallback& encode_cb) |
+ : AudioDirectiveHandler(encode_cb) {} |
+ virtual ~MockAudioDirectiveHandler() {} |
+ |
+ // Mock out the play/record methods. |
+ MOCK_METHOD2(PlayAudio, |
+ void(const scoped_refptr<media::AudioBusRefCounted>&, |
+ base::TimeDelta)); |
Daniel Erat
2014/07/28 21:18:16
add OVERRIDE on all of these
rkc
2014/07/29 00:33:34
Done.
|
+ MOCK_METHOD1(RecordAudio, void(base::TimeDelta)); |
+ |
+ MOCK_METHOD0(StopPlayback, void(void)); |
+ MOCK_METHOD0(StopRecording, void(void)); |
+ |
+ private: |
+ DISALLOW_COPY_AND_ASSIGN(MockAudioDirectiveHandler); |
+}; |
+ |
+class AudioDirectiveHandlerTest : public testing::Test { |
+ public: |
+ AudioDirectiveHandlerTest() |
+ : directive_handler_(new MockAudioDirectiveHandler( |
+ base::Bind(&AudioDirectiveHandlerTest::EncodeToken, |
+ base::Unretained(this)))) {} |
+ |
+ virtual ~AudioDirectiveHandlerTest() {} |
+ |
+ void DirectiveAdded() {} |
Daniel Erat
2014/07/28 21:18:17
i don't see this called anywhere. what's it used f
rkc
2014/07/29 00:33:34
Leftover code. Removed.
|
+ |
+ protected: |
+ void EncodeToken(const std::string& token, |
+ const AudioDirectiveList::SamplesCallback& callback) { |
+ const int kSamplesSize = 0x1337; |
+ scoped_refptr<media::AudioBusRefCounted> samples = |
+ media::AudioBusRefCounted::Create(1, kSamplesSize); |
+ callback.Run(token, samples); |
+ } |
+ |
+ const copresence::TokenInstruction CreateTransmitInstruction( |
Daniel Erat
2014/07/28 21:18:16
why is the return value const?
rkc
2014/07/29 00:33:34
Doesn't need to be. I think I had made it constref
|
+ const std::string& token) { |
+ copresence::TokenInstruction instruction; |
+ instruction.set_token_instruction_type(copresence::TRANSMIT); |
+ instruction.set_token_id(token); |
+ return instruction; |
+ } |
+ |
+ const copresence::TokenInstruction CreateReceiveInstruction() { |
Daniel Erat
2014/07/28 21:18:16
why is the return value const?
rkc
2014/07/29 00:33:34
Done.
|
+ copresence::TokenInstruction instruction; |
+ instruction.set_token_instruction_type(copresence::RECEIVE); |
+ return instruction; |
+ } |
+ |
+ // This order is important. We want the message loop to get created before |
+ // our the audio directive handler since, since the directive list |
Daniel Erat
2014/07/28 21:18:16
s/since, since/since/
rkc
2014/07/29 00:33:34
Done.
|
+ // ctor (invoked from the directive handler ctor) will post tasks. |
+ base::MessageLoop message_loop_; |
+ scoped_ptr<MockAudioDirectiveHandler> directive_handler_; |
+}; |
xiyuan
2014/07/25 21:02:09
nit:
private:
DISALLOW_COPY_AND_ASSIGN(AudioDi
rkc
2014/07/28 21:02:02
Done.
|
+ |
+TEST_F(AudioDirectiveHandlerTest, Basic) { |
+ const base::TimeDelta small_ttl = base::TimeDelta::FromMilliseconds(0x1337); |
Daniel Erat
2014/07/28 21:18:16
nit: kSmallTtl, kLargeTtl (or TTL; don't think chr
rkc
2014/07/29 00:33:34
Done.
|
+ const base::TimeDelta large_ttl = base::TimeDelta::FromSeconds(0x7331); |
+ |
+ // Expect to play and record instructions for 'less' than the TTL specified, |
+ // since by the time that the token would have gotten encoded, we would |
+ // have (TTL - time_to_encode) left to play on that instruction. |
+ EXPECT_CALL(*directive_handler_, PlayAudio(_, testing::Le(large_ttl))) |
+ .Times(3); |
+ directive_handler_->AddInstruction(CreateTransmitInstruction("token1"), |
+ large_ttl); |
+ directive_handler_->AddInstruction(CreateTransmitInstruction("token2"), |
+ large_ttl); |
+ directive_handler_->AddInstruction(CreateTransmitInstruction("token3"), |
+ small_ttl); |
+ |
+ EXPECT_CALL(*directive_handler_, RecordAudio(Le(large_ttl))).Times(3); |
+ directive_handler_->AddInstruction(CreateReceiveInstruction(), large_ttl); |
+ directive_handler_->AddInstruction(CreateReceiveInstruction(), small_ttl); |
+ directive_handler_->AddInstruction(CreateReceiveInstruction(), large_ttl); |
+} |
+ |
+// TODO(rkc): When we are keeping track of which token we're currently playing, |
+// add tests to make sure we don't replay if we get a token with a lower ttl |
+// than the current active. |
+ |
+} // namespace copresence |