Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(389)

Unified Diff: components/copresence/handlers/audio/audio_directive_handler_unittest.cc

Issue 690213004: Refactoring AudioDirectiveHandler to support testing (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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 598a358584b01d46fb21e4a50653af7c9a4be1ce..0400662149ec58ca854406e17abf6fe5ac2caadc 100644
--- a/components/copresence/handlers/audio/audio_directive_handler_unittest.cc
+++ b/components/copresence/handlers/audio/audio_directive_handler_unittest.cc
@@ -2,15 +2,15 @@
// 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 "base/bind.h"
#include "base/memory/scoped_ptr.h"
#include "base/message_loop/message_loop.h"
#include "base/test/simple_test_tick_clock.h"
#include "base/timer/mock_timer.h"
+#include "components/copresence/handlers/audio/audio_directive_handler_impl.h"
#include "components/copresence/handlers/audio/tick_clock_ref_counted.h"
#include "components/copresence/mediums/audio/audio_manager.h"
+#include "components/copresence/proto/data.pb.h"
#include "components/copresence/test/audio_test_support.h"
#include "testing/gtest/include/gtest/gtest.h"
@@ -55,33 +55,34 @@ class AudioManagerStub final : public AudioManager {
class AudioDirectiveHandlerTest : public testing::Test {
public:
- AudioDirectiveHandlerTest()
- : directive_handler_(new AudioDirectiveHandler()) {
- scoped_ptr<AudioManagerStub> manager(new AudioManagerStub);
- manager_ptr_ = manager.get();
- directive_handler_->set_audio_manager_for_testing(manager.Pass());
+ AudioDirectiveHandlerTest() {
+ manager_ptr_ = new AudioManagerStub;
+ timer_ptr_ = new base::MockTimer(false, false);
+ clock_ptr_ = new base::SimpleTestTickClock;
+
+ directive_handler_.reset(new AudioDirectiveHandlerImpl(
+ make_scoped_ptr<AudioManager>(manager_ptr_),
+ make_scoped_ptr<base::Timer>(timer_ptr_),
+ make_scoped_refptr(new TickClockRefCounted(clock_ptr_))));
directive_handler_->Initialize(base::Bind(&DecodeSamples),
base::Bind(&EncodeToken));
}
~AudioDirectiveHandlerTest() override {}
- void DirectiveAdded() {}
-
protected:
- copresence::TokenInstruction CreateTransmitInstruction(
- const std::string& token,
- bool audible) {
- copresence::TokenInstruction instruction;
- instruction.set_token_instruction_type(copresence::TRANSMIT);
+ 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;
}
- copresence::TokenInstruction CreateReceiveInstruction(bool audible) {
- copresence::TokenInstruction instruction;
- instruction.set_token_instruction_type(copresence::RECEIVE);
+ TokenInstruction CreateReceiveInstruction(bool audible) {
+ TokenInstruction instruction;
+ instruction.set_token_instruction_type(RECEIVE);
instruction.set_medium(audible ? AUDIO_AUDIBLE_DTMF
: AUDIO_ULTRASOUND_PASSBAND);
return instruction;
@@ -96,8 +97,11 @@ class AudioDirectiveHandlerTest : public testing::Test {
// from the directive handler ctor) will post tasks.
base::MessageLoop message_loop_;
scoped_ptr<AudioDirectiveHandler> directive_handler_;
+
// Unowned.
AudioManagerStub* manager_ptr_;
+ base::MockTimer* timer_ptr_;
+ base::SimpleTestTickClock* clock_ptr_;
private:
DISALLOW_COPY_AND_ASSIGN(AudioDirectiveHandlerTest);
@@ -139,17 +143,6 @@ TEST_F(AudioDirectiveHandlerTest, Basic) {
}
TEST_F(AudioDirectiveHandlerTest, Timed) {
- scoped_ptr<base::SimpleTestTickClock> clock(new base::SimpleTestTickClock());
- base::SimpleTestTickClock* clock_ptr = clock.get();
-
- scoped_refptr<TickClockRefCounted> clock_proxy =
- new TickClockRefCounted(clock.Pass());
- directive_handler_->set_clock_for_testing(clock_proxy);
-
- scoped_ptr<base::Timer> timer(new base::MockTimer(false, false));
- base::MockTimer* timer_ptr = static_cast<base::MockTimer*>(timer.get());
- directive_handler_->set_timer_for_testing(timer.Pass());
-
const base::TimeDelta kTtl1 = base::TimeDelta::FromMilliseconds(1337);
directive_handler_->AddInstruction(
CreateTransmitInstruction("token", true), "op_id1", kTtl1);
@@ -169,7 +162,7 @@ TEST_F(AudioDirectiveHandlerTest, Timed) {
// 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));
+ clock_ptr_->Advance(kTtl3 + base::TimeDelta::FromMilliseconds(1));
// We are now at base + 1337ms.
// This instruction expires at base + (1337 + 1337 = 2674)
@@ -180,25 +173,25 @@ TEST_F(AudioDirectiveHandlerTest, Timed) {
EXPECT_TRUE(IsRecording(AUDIBLE));
EXPECT_FALSE(IsRecording(INAUDIBLE));
- clock_ptr->Advance(base::TimeDelta::FromMilliseconds(1));
+ clock_ptr_->Advance(base::TimeDelta::FromMilliseconds(1));
// We are now at base + 1338ms.
- timer_ptr->Fire();
+ timer_ptr_->Fire();
EXPECT_FALSE(IsPlaying(AUDIBLE));
EXPECT_TRUE(IsPlaying(INAUDIBLE));
EXPECT_TRUE(IsRecording(AUDIBLE));
- clock_ptr->Advance(base::TimeDelta::FromMilliseconds(1));
+ clock_ptr_->Advance(base::TimeDelta::FromMilliseconds(1));
// We are now at base + 1339ms.
- timer_ptr->Fire();
+ timer_ptr_->Fire();
EXPECT_FALSE(IsPlaying(INAUDIBLE));
EXPECT_TRUE(IsRecording(AUDIBLE));
- clock_ptr->Advance(kTtl3);
+ clock_ptr_->Advance(kTtl3);
// We are now at base + 2676ms.
- timer_ptr->Fire();
+ timer_ptr_->Fire();
EXPECT_FALSE(IsRecording(AUDIBLE));
}

Powered by Google App Engine
This is Rietveld 408576698