OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2014 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. | |
4 | |
5 #include "components/copresence/handlers/audio/audio_directive_handler.h" | |
6 | |
7 #include <cstdlib> | |
8 | |
9 #include "base/bind.h" | |
10 #include "base/message_loop/message_loop.h" | |
11 #include "media/base/audio_bus.h" | |
12 #include "testing/gmock/include/gmock/gmock.h" | |
13 #include "testing/gtest/include/gtest/gtest.h" | |
14 | |
15 using ::testing::_; | |
16 using ::testing::Le; | |
17 | |
18 namespace copresence { | |
19 | |
20 class MockAudioDirectiveHandler : public AudioDirectiveHandler { | |
21 public: | |
22 MockAudioDirectiveHandler( | |
23 const AudioDirectiveList::EncodeTokenCallback& encode_cb) | |
24 : AudioDirectiveHandler(encode_cb) {} | |
25 virtual ~MockAudioDirectiveHandler() {} | |
26 | |
27 // Mock out the play/record methods. | |
28 MOCK_METHOD2(PlayAudio, | |
29 void(const scoped_refptr<media::AudioBusRefCounted>&, | |
30 base::TimeDelta)); | |
Daniel Erat
2014/07/28 21:18:16
add OVERRIDE on all of these
rkc
2014/07/29 00:33:34
Done.
| |
31 MOCK_METHOD1(RecordAudio, void(base::TimeDelta)); | |
32 | |
33 MOCK_METHOD0(StopPlayback, void(void)); | |
34 MOCK_METHOD0(StopRecording, void(void)); | |
35 | |
36 private: | |
37 DISALLOW_COPY_AND_ASSIGN(MockAudioDirectiveHandler); | |
38 }; | |
39 | |
40 class AudioDirectiveHandlerTest : public testing::Test { | |
41 public: | |
42 AudioDirectiveHandlerTest() | |
43 : directive_handler_(new MockAudioDirectiveHandler( | |
44 base::Bind(&AudioDirectiveHandlerTest::EncodeToken, | |
45 base::Unretained(this)))) {} | |
46 | |
47 virtual ~AudioDirectiveHandlerTest() {} | |
48 | |
49 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.
| |
50 | |
51 protected: | |
52 void EncodeToken(const std::string& token, | |
53 const AudioDirectiveList::SamplesCallback& callback) { | |
54 const int kSamplesSize = 0x1337; | |
55 scoped_refptr<media::AudioBusRefCounted> samples = | |
56 media::AudioBusRefCounted::Create(1, kSamplesSize); | |
57 callback.Run(token, samples); | |
58 } | |
59 | |
60 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
| |
61 const std::string& token) { | |
62 copresence::TokenInstruction instruction; | |
63 instruction.set_token_instruction_type(copresence::TRANSMIT); | |
64 instruction.set_token_id(token); | |
65 return instruction; | |
66 } | |
67 | |
68 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.
| |
69 copresence::TokenInstruction instruction; | |
70 instruction.set_token_instruction_type(copresence::RECEIVE); | |
71 return instruction; | |
72 } | |
73 | |
74 // This order is important. We want the message loop to get created before | |
75 // 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.
| |
76 // ctor (invoked from the directive handler ctor) will post tasks. | |
77 base::MessageLoop message_loop_; | |
78 scoped_ptr<MockAudioDirectiveHandler> directive_handler_; | |
79 }; | |
xiyuan
2014/07/25 21:02:09
nit:
private:
DISALLOW_COPY_AND_ASSIGN(AudioDi
rkc
2014/07/28 21:02:02
Done.
| |
80 | |
81 TEST_F(AudioDirectiveHandlerTest, Basic) { | |
82 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.
| |
83 const base::TimeDelta large_ttl = base::TimeDelta::FromSeconds(0x7331); | |
84 | |
85 // Expect to play and record instructions for 'less' than the TTL specified, | |
86 // since by the time that the token would have gotten encoded, we would | |
87 // have (TTL - time_to_encode) left to play on that instruction. | |
88 EXPECT_CALL(*directive_handler_, PlayAudio(_, testing::Le(large_ttl))) | |
89 .Times(3); | |
90 directive_handler_->AddInstruction(CreateTransmitInstruction("token1"), | |
91 large_ttl); | |
92 directive_handler_->AddInstruction(CreateTransmitInstruction("token2"), | |
93 large_ttl); | |
94 directive_handler_->AddInstruction(CreateTransmitInstruction("token3"), | |
95 small_ttl); | |
96 | |
97 EXPECT_CALL(*directive_handler_, RecordAudio(Le(large_ttl))).Times(3); | |
98 directive_handler_->AddInstruction(CreateReceiveInstruction(), large_ttl); | |
99 directive_handler_->AddInstruction(CreateReceiveInstruction(), small_ttl); | |
100 directive_handler_->AddInstruction(CreateReceiveInstruction(), large_ttl); | |
101 } | |
102 | |
103 // TODO(rkc): When we are keeping track of which token we're currently playing, | |
104 // add tests to make sure we don't replay if we get a token with a lower ttl | |
105 // than the current active. | |
106 | |
107 } // namespace copresence | |
OLD | NEW |