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 |
index 45eaa259e14e32ea854fec683c8f816499426d68..e6c7310f422d863740374eea69b05f62391ae09d 100644 |
--- a/components/copresence/handlers/audio/audio_directive_handler_unittest.cc |
+++ b/components/copresence/handlers/audio/audio_directive_handler_unittest.cc |
@@ -2,6 +2,9 @@ |
// Use of this source code is governed by a BSD-style license that can be |
// found in the LICENSE file. |
+#include <string> |
+#include <vector> |
+ |
#include "base/bind.h" |
#include "base/memory/scoped_ptr.h" |
#include "base/message_loop/message_loop.h" |
@@ -16,6 +19,23 @@ |
namespace copresence { |
+namespace { |
+ |
+const Directive CreateDirective(TokenInstructionType type, |
+ bool audible, |
+ int64 ttl) { |
+ Directive directive; |
+ directive.mutable_token_instruction()->set_token_instruction_type(type); |
+ directive.mutable_token_instruction()->set_token_id("token"); |
+ directive.mutable_token_instruction()->set_medium(audible ? |
+ AUDIO_AUDIBLE_DTMF : AUDIO_ULTRASOUND_PASSBAND); |
+ directive.set_ttl_millis(ttl); |
+ return directive; |
+} |
+ |
+ |
xiyuan
2014/12/18 18:10:52
nit: nuke one empty line
Charlie
2014/12/19 03:53:33
Done.
|
+} // namespace |
+ |
class AudioManagerStub final : public AudioManager { |
public: |
AudioManagerStub() {} |
@@ -50,6 +70,8 @@ class AudioDirectiveHandlerTest : public testing::Test { |
clock_ptr_ = new base::SimpleTestTickClock; |
directive_handler_.reset(new AudioDirectiveHandlerImpl( |
+ base::Bind(&AudioDirectiveHandlerTest::GetDirectiveUpdates, |
+ base::Unretained(this)), |
make_scoped_ptr<AudioManager>(manager_ptr_), |
make_scoped_ptr<base::Timer>(timer_ptr_), |
make_scoped_refptr(new TickClockRefCounted(clock_ptr_)))); |
@@ -58,22 +80,8 @@ class AudioDirectiveHandlerTest : public testing::Test { |
~AudioDirectiveHandlerTest() override {} |
protected: |
- TokenInstruction CreateTransmitInstruction(const std::string& token, |
- bool audible) { |
- TokenInstruction instruction; |
- instruction.set_token_instruction_type(TRANSMIT); |
- instruction.set_token_id(token); |
- instruction.set_medium(audible ? AUDIO_AUDIBLE_DTMF |
- : AUDIO_ULTRASOUND_PASSBAND); |
- return instruction; |
- } |
- |
- TokenInstruction CreateReceiveInstruction(bool audible) { |
- TokenInstruction instruction; |
- instruction.set_token_instruction_type(RECEIVE); |
- instruction.set_medium(audible ? AUDIO_AUDIBLE_DTMF |
- : AUDIO_ULTRASOUND_PASSBAND); |
- return instruction; |
+ const std::vector<Directive>& current_directives() { |
+ return current_directives_; |
} |
bool IsPlaying(AudioType type) { return manager_ptr_->IsPlaying(type); } |
@@ -86,29 +94,35 @@ class AudioDirectiveHandlerTest : public testing::Test { |
base::MessageLoop message_loop_; |
scoped_ptr<AudioDirectiveHandler> directive_handler_; |
+ std::vector<Directive> current_directives_; |
+ |
// Unowned. |
AudioManagerStub* manager_ptr_; |
base::MockTimer* timer_ptr_; |
base::SimpleTestTickClock* clock_ptr_; |
private: |
+ void GetDirectiveUpdates(const std::vector<Directive>& current_directives) { |
+ current_directives_ = current_directives; |
+ } |
+ |
DISALLOW_COPY_AND_ASSIGN(AudioDirectiveHandlerTest); |
}; |
TEST_F(AudioDirectiveHandlerTest, Basic) { |
- const base::TimeDelta kTtl = base::TimeDelta::FromMilliseconds(9999); |
- directive_handler_->AddInstruction( |
- CreateTransmitInstruction("token", true), "op_id1", kTtl); |
- directive_handler_->AddInstruction( |
- CreateTransmitInstruction("token", false), "op_id1", kTtl); |
- directive_handler_->AddInstruction( |
- CreateTransmitInstruction("token", false), "op_id2", kTtl); |
- directive_handler_->AddInstruction( |
- CreateReceiveInstruction(false), "op_id1", kTtl); |
- directive_handler_->AddInstruction( |
- CreateReceiveInstruction(true), "op_id2", kTtl); |
- directive_handler_->AddInstruction( |
- CreateReceiveInstruction(false), "op_id3", kTtl); |
+ const int64 kTtl = 10; |
+ directive_handler_->AddInstruction(CreateDirective(TRANSMIT, true, kTtl), |
+ "op_id1"); |
+ directive_handler_->AddInstruction(CreateDirective(TRANSMIT, false, kTtl), |
+ "op_id1"); |
+ directive_handler_->AddInstruction(CreateDirective(TRANSMIT, false, kTtl), |
+ "op_id2"); |
+ directive_handler_->AddInstruction(CreateDirective(RECEIVE, false, kTtl), |
+ "op_id1"); |
+ directive_handler_->AddInstruction(CreateDirective(RECEIVE, true, kTtl), |
+ "op_id2"); |
+ directive_handler_->AddInstruction(CreateDirective(RECEIVE, false, kTtl), |
+ "op_id3"); |
EXPECT_TRUE(IsPlaying(AUDIBLE)); |
EXPECT_TRUE(IsPlaying(INAUDIBLE)); |
@@ -131,54 +145,47 @@ TEST_F(AudioDirectiveHandlerTest, Basic) { |
} |
TEST_F(AudioDirectiveHandlerTest, Timed) { |
- const base::TimeDelta kTtl1 = base::TimeDelta::FromMilliseconds(1337); |
- directive_handler_->AddInstruction( |
- CreateTransmitInstruction("token", true), "op_id1", kTtl1); |
+ directive_handler_->AddInstruction(CreateDirective(TRANSMIT, true, 6), |
+ "op_id1"); |
+ directive_handler_->AddInstruction(CreateDirective(TRANSMIT, false, 8), |
+ "op_id1"); |
+ directive_handler_->AddInstruction(CreateDirective(RECEIVE, false, 4), |
+ "op_id3"); |
- const base::TimeDelta kTtl2 = base::TimeDelta::FromMilliseconds(1338); |
- directive_handler_->AddInstruction( |
- CreateTransmitInstruction("token", false), "op_id1", kTtl2); |
- |
- const base::TimeDelta kTtl3 = base::TimeDelta::FromMilliseconds(1336); |
- directive_handler_->AddInstruction( |
- CreateReceiveInstruction(false), "op_id3", kTtl3); |
EXPECT_TRUE(IsPlaying(AUDIBLE)); |
EXPECT_TRUE(IsPlaying(INAUDIBLE)); |
EXPECT_FALSE(IsRecording(AUDIBLE)); |
EXPECT_TRUE(IsRecording(INAUDIBLE)); |
- // We *have* to call an operation on the directive handler after we advance |
- // time to trigger the next set of operations, so ensure that after calling |
- // advance, we are also calling another operation. |
- clock_ptr_->Advance(kTtl3 + base::TimeDelta::FromMilliseconds(1)); |
+ // Every time we advance and a directive expires, the timer should fire also. |
+ clock_ptr_->Advance(base::TimeDelta::FromMilliseconds(5)); |
+ timer_ptr_->Fire(); |
- // We are now at base + 1337ms. |
- // This instruction expires at base + (1337 + 1337 = 2674) |
- directive_handler_->AddInstruction( |
- CreateReceiveInstruction(true), "op_id4", kTtl1); |
+ // We are now at +5ms. This instruction expires at +10ms. |
+ directive_handler_->AddInstruction(CreateDirective(RECEIVE, true, 5), |
+ "op_id4"); |
EXPECT_TRUE(IsPlaying(AUDIBLE)); |
EXPECT_TRUE(IsPlaying(INAUDIBLE)); |
EXPECT_TRUE(IsRecording(AUDIBLE)); |
EXPECT_FALSE(IsRecording(INAUDIBLE)); |
- clock_ptr_->Advance(base::TimeDelta::FromMilliseconds(1)); |
- |
- // We are now at base + 1338ms. |
+ // Advance to +7ms. |
+ const base::TimeDelta twoMs = base::TimeDelta::FromMilliseconds(2); |
+ clock_ptr_->Advance(twoMs); |
timer_ptr_->Fire(); |
+ |
EXPECT_FALSE(IsPlaying(AUDIBLE)); |
EXPECT_TRUE(IsPlaying(INAUDIBLE)); |
EXPECT_TRUE(IsRecording(AUDIBLE)); |
- clock_ptr_->Advance(base::TimeDelta::FromMilliseconds(1)); |
- |
- // We are now at base + 1339ms. |
+ // Advance to +9ms. |
+ clock_ptr_->Advance(twoMs); |
timer_ptr_->Fire(); |
EXPECT_FALSE(IsPlaying(INAUDIBLE)); |
EXPECT_TRUE(IsRecording(AUDIBLE)); |
- clock_ptr_->Advance(kTtl3); |
- |
- // We are now at base + 2676ms. |
+ // Advance to +11ms. |
+ clock_ptr_->Advance(twoMs); |
timer_ptr_->Fire(); |
EXPECT_FALSE(IsRecording(AUDIBLE)); |
} |