| 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 4a64e6d633edd7d8fb481fa20e6b49f317136b00..3af0859f74559d24fcd8984b1a33560148106b70 100644
|
| --- a/components/copresence/handlers/audio/audio_directive_handler_unittest.cc
|
| +++ b/components/copresence/handlers/audio/audio_directive_handler_unittest.cc
|
| @@ -1,6 +1,9 @@
|
| // 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 <string>
|
| +#include <vector>
|
|
|
| #include "base/bind.h"
|
| #include "base/memory/scoped_ptr.h"
|
| @@ -15,6 +18,22 @@
|
| #include "testing/gtest/include/gtest/gtest.h"
|
|
|
| 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;
|
| +}
|
| +
|
| +} // namespace
|
|
|
| class AudioManagerStub final : public AudioManager {
|
| public:
|
| @@ -51,6 +70,8 @@
|
| 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_))));
|
| @@ -59,22 +80,8 @@
|
| ~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); }
|
| @@ -87,29 +94,35 @@
|
| 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));
|
| @@ -132,54 +145,47 @@
|
| }
|
|
|
| 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));
|
| + // Advance to +7ms.
|
| + const base::TimeDelta twoMs = base::TimeDelta::FromMilliseconds(2);
|
| + clock_ptr_->Advance(twoMs);
|
| + timer_ptr_->Fire();
|
|
|
| - // We are now at base + 1338ms.
|
| - 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));
|
| }
|
|
|