summaryrefslogtreecommitdiffstats
path: root/components/copresence
diff options
context:
space:
mode:
authorrkc <rkc@chromium.org>2014-10-27 11:12:26 -0700
committerCommit bot <commit-bot@chromium.org>2014-10-27 18:12:52 +0000
commit48d872c84c0b1a6d74b3fd0fa6e04673d787d247 (patch)
treeb3b86a62d09ec88f16ee1e3047088783f20c726b /components/copresence
parent90bbd533b95d95a5e65df0a192ab54f456b91d91 (diff)
downloadchromium_src-48d872c84c0b1a6d74b3fd0fa6e04673d787d247.zip
chromium_src-48d872c84c0b1a6d74b3fd0fa6e04673d787d247.tar.gz
chromium_src-48d872c84c0b1a6d74b3fd0fa6e04673d787d247.tar.bz2
Add AudioDirectiveHandler timed tests.
This CL adds tests for AudioDirectiveHandler which move time forward to verify functionality. It also adds a ref counted tick clock proxy that can be used to pass around a tick clock among multiple classes. BUG=None. R=xiyuan@chromium.org Review URL: https://codereview.chromium.org/665353002 Cr-Commit-Position: refs/heads/master@{#301400}
Diffstat (limited to 'components/copresence')
-rw-r--r--components/copresence/BUILD.gn2
-rw-r--r--components/copresence/handlers/audio/audio_directive_handler.cc20
-rw-r--r--components/copresence/handlers/audio/audio_directive_handler.h8
-rw-r--r--components/copresence/handlers/audio/audio_directive_handler_unittest.cc68
-rw-r--r--components/copresence/handlers/audio/audio_directive_list.cc13
-rw-r--r--components/copresence/handlers/audio/audio_directive_list.h11
-rw-r--r--components/copresence/handlers/audio/tick_clock_ref_counted.cc22
-rw-r--r--components/copresence/handlers/audio/tick_clock_ref_counted.h36
8 files changed, 169 insertions, 11 deletions
diff --git a/components/copresence/BUILD.gn b/components/copresence/BUILD.gn
index e0130d0..c4356bb 100644
--- a/components/copresence/BUILD.gn
+++ b/components/copresence/BUILD.gn
@@ -13,6 +13,8 @@ static_library("copresence") {
"handlers/audio/audio_directive_handler.h",
"handlers/audio/audio_directive_list.cc",
"handlers/audio/audio_directive_list.h",
+ "handlers/audio/tick_clock_ref_counted.cc",
+ "handlers/audio/tick_clock_ref_counted.h",
"handlers/directive_handler.cc",
"handlers/directive_handler.h",
"mediums/audio/audio_manager.h",
diff --git a/components/copresence/handlers/audio/audio_directive_handler.cc b/components/copresence/handlers/audio/audio_directive_handler.cc
index 9a79738..7563193 100644
--- a/components/copresence/handlers/audio/audio_directive_handler.cc
+++ b/components/copresence/handlers/audio/audio_directive_handler.cc
@@ -10,6 +10,7 @@
#include "base/time/default_tick_clock.h"
#include "base/time/time.h"
#include "base/timer/timer.h"
+#include "components/copresence/handlers/audio/tick_clock_ref_counted.h"
#include "components/copresence/mediums/audio/audio_manager_impl.h"
#include "components/copresence/proto/data.pb.h"
#include "components/copresence/public/copresence_constants.h"
@@ -36,7 +37,8 @@ base::TimeTicks GetEarliestEventTime(AudioDirectiveList* list,
AudioDirectiveHandler::AudioDirectiveHandler()
: audio_event_timer_(new base::OneShotTimer<AudioDirectiveHandler>),
- clock_(new base::DefaultTickClock) {
+ clock_(new TickClockRefCounted(
+ make_scoped_ptr(new base::DefaultTickClock))) {
}
AudioDirectiveHandler::~AudioDirectiveHandler() {
@@ -107,6 +109,22 @@ void AudioDirectiveHandler::RemoveInstructions(const std::string& op_id) {
const std::string AudioDirectiveHandler::PlayingToken(AudioType type) const {
return audio_manager_->GetToken(type);
}
+
+void AudioDirectiveHandler::set_clock_for_testing(
+ const scoped_refptr<TickClockRefCounted>& clock) {
+ clock_ = clock;
+
+ transmits_list_[AUDIBLE].set_clock_for_testing(clock);
+ transmits_list_[INAUDIBLE].set_clock_for_testing(clock);
+ receives_list_[AUDIBLE].set_clock_for_testing(clock);
+ receives_list_[INAUDIBLE].set_clock_for_testing(clock);
+}
+
+void AudioDirectiveHandler::set_timer_for_testing(
+ scoped_ptr<base::Timer> timer) {
+ audio_event_timer_.swap(timer);
+}
+
// Private methods.
void AudioDirectiveHandler::ProcessNextInstruction() {
diff --git a/components/copresence/handlers/audio/audio_directive_handler.h b/components/copresence/handlers/audio/audio_directive_handler.h
index 54885b9..fa690b6 100644
--- a/components/copresence/handlers/audio/audio_directive_handler.h
+++ b/components/copresence/handlers/audio/audio_directive_handler.h
@@ -17,7 +17,6 @@
#include "components/copresence/proto/data.pb.h"
namespace base {
-class TickClock;
class Timer;
}
@@ -27,6 +26,8 @@ class AudioBusRefCounted;
namespace copresence {
+class TickClockRefCounted;
+
// The AudioDirectiveHandler handles audio transmit and receive instructions.
// TODO(rkc): Currently since WhispernetClient can only have one token encoded
// callback at a time, we need to have both the audible and inaudible in this
@@ -60,6 +61,9 @@ class AudioDirectiveHandler final {
audio_manager_ = manager.Pass();
}
+ void set_clock_for_testing(const scoped_refptr<TickClockRefCounted>& clock);
+ void set_timer_for_testing(scoped_ptr<base::Timer> timer);
+
private:
// Processes the next active instruction, updating our audio manager state
// accordingly.
@@ -80,7 +84,7 @@ class AudioDirectiveHandler final {
scoped_ptr<base::Timer> audio_event_timer_;
- scoped_ptr<base::TickClock> clock_;
+ scoped_refptr<TickClockRefCounted> clock_;
DISALLOW_COPY_AND_ASSIGN(AudioDirectiveHandler);
};
diff --git a/components/copresence/handlers/audio/audio_directive_handler_unittest.cc b/components/copresence/handlers/audio/audio_directive_handler_unittest.cc
index e5b1dc6..ede0ad8 100644
--- a/components/copresence/handlers/audio/audio_directive_handler_unittest.cc
+++ b/components/copresence/handlers/audio/audio_directive_handler_unittest.cc
@@ -7,6 +7,9 @@
#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/tick_clock_ref_counted.h"
#include "components/copresence/mediums/audio/audio_manager.h"
#include "components/copresence/test/audio_test_support.h"
#include "testing/gtest/include/gtest/gtest.h"
@@ -135,8 +138,71 @@ TEST_F(AudioDirectiveHandlerTest, Basic) {
EXPECT_FALSE(IsRecording(INAUDIBLE));
}
+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);
+
+ 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));
+
+ // We are now at base + 1337ms.
+ // This instruction expires at base + (1337 + 1337 = 2674)
+ directive_handler_->AddInstruction(
+ CreateReceiveInstruction(true), "op_id4", kTtl1);
+ 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.
+ 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.
+ timer_ptr->Fire();
+ EXPECT_FALSE(IsPlaying(INAUDIBLE));
+ EXPECT_TRUE(IsRecording(AUDIBLE));
+
+ clock_ptr->Advance(kTtl3);
+
+ // We are now at base + 2676ms.
+ timer_ptr->Fire();
+ EXPECT_FALSE(IsRecording(AUDIBLE));
+}
+
// TODO(rkc): Write more tests that check more convoluted sequences of
// transmits/receives.
-// TODO(rkc): Write tests to move time forward and test functionality.
} // namespace copresence
diff --git a/components/copresence/handlers/audio/audio_directive_list.cc b/components/copresence/handlers/audio/audio_directive_list.cc
index 8b9b76e..2060ef5 100644
--- a/components/copresence/handlers/audio/audio_directive_list.cc
+++ b/components/copresence/handlers/audio/audio_directive_list.cc
@@ -8,8 +8,8 @@
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "base/time/default_tick_clock.h"
-#include "base/time/tick_clock.h"
#include "base/time/time.h"
+#include "components/copresence/handlers/audio/tick_clock_ref_counted.h"
namespace copresence {
@@ -23,7 +23,9 @@ AudioDirective::AudioDirective(const std::string& op_id,
: op_id(op_id), end_time(end_time) {
}
-AudioDirectiveList::AudioDirectiveList() : clock_(new base::DefaultTickClock) {
+AudioDirectiveList::AudioDirectiveList()
+ : clock_(new TickClockRefCounted(
+ make_scoped_ptr(new base::DefaultTickClock))) {
}
AudioDirectiveList::~AudioDirectiveList() {
@@ -75,6 +77,13 @@ scoped_ptr<AudioDirective> AudioDirectiveList::GetActiveDirective() {
return make_scoped_ptr(new AudioDirective(active_directives_.front()));
}
+void AudioDirectiveList::set_clock_for_testing(
+ const scoped_refptr<TickClockRefCounted>& clock) {
+ clock_ = clock;
+}
+
+// Private methods.
+
std::vector<AudioDirective>::iterator AudioDirectiveList::FindDirectiveByOpId(
const std::string& op_id) {
for (std::vector<AudioDirective>::iterator it = active_directives_.begin();
diff --git a/components/copresence/handlers/audio/audio_directive_list.h b/components/copresence/handlers/audio/audio_directive_list.h
index e9d0634..aa71a99 100644
--- a/components/copresence/handlers/audio/audio_directive_list.h
+++ b/components/copresence/handlers/audio/audio_directive_list.h
@@ -10,19 +10,18 @@
#include "base/callback.h"
#include "base/macros.h"
+#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/time/time.h"
-namespace base {
-class TickClock;
-}
-
namespace media {
class AudioBusRefCounted;
}
namespace copresence {
+class TickClockRefCounted;
+
struct AudioDirective final {
// Default ctor, required by the priority queue.
AudioDirective();
@@ -50,6 +49,8 @@ class AudioDirectiveList {
scoped_ptr<AudioDirective> GetActiveDirective();
+ void set_clock_for_testing(const scoped_refptr<TickClockRefCounted>& clock);
+
private:
// Comparator for comparing end_times on audio tokens.
class LatestFirstComparator {
@@ -68,7 +69,7 @@ class AudioDirectiveList {
// element. Only currently active directives will exist in this list.
std::vector<AudioDirective> active_directives_;
- scoped_ptr<base::TickClock> clock_;
+ scoped_refptr<TickClockRefCounted> clock_;
DISALLOW_COPY_AND_ASSIGN(AudioDirectiveList);
};
diff --git a/components/copresence/handlers/audio/tick_clock_ref_counted.cc b/components/copresence/handlers/audio/tick_clock_ref_counted.cc
new file mode 100644
index 0000000..3e7e28d
--- /dev/null
+++ b/components/copresence/handlers/audio/tick_clock_ref_counted.cc
@@ -0,0 +1,22 @@
+// 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/tick_clock_ref_counted.h"
+
+#include "base/time/tick_clock.h"
+
+namespace copresence {
+
+TickClockRefCounted::TickClockRefCounted(scoped_ptr<base::TickClock> clock)
+ : clock_(clock.Pass()) {
+}
+
+base::TimeTicks TickClockRefCounted::NowTicks() {
+ return clock_->NowTicks();
+}
+
+TickClockRefCounted::~TickClockRefCounted() {
+}
+
+} // namespace copresence
diff --git a/components/copresence/handlers/audio/tick_clock_ref_counted.h b/components/copresence/handlers/audio/tick_clock_ref_counted.h
new file mode 100644
index 0000000..5bece75
--- /dev/null
+++ b/components/copresence/handlers/audio/tick_clock_ref_counted.h
@@ -0,0 +1,36 @@
+// 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.
+
+#ifndef COMPONENTS_COPRESENCE_HANDLERS_AUDIO_TICK_CLOCK_REF_COUNTED_H_
+#define COMPONENTS_COPRESENCE_HANDLERS_AUDIO_TICK_CLOCK_REF_COUNTED_H_
+
+#include "base/macros.h"
+#include "base/memory/ref_counted.h"
+#include "base/memory/scoped_ptr.h"
+#include "base/time/time.h"
+
+namespace base {
+class TickClock;
+}
+
+namespace copresence {
+
+class TickClockRefCounted
+ : public base::RefCountedThreadSafe<TickClockRefCounted> {
+ public:
+ explicit TickClockRefCounted(scoped_ptr<base::TickClock> clock);
+ base::TimeTicks NowTicks();
+
+ private:
+ friend class base::RefCountedThreadSafe<TickClockRefCounted>;
+ virtual ~TickClockRefCounted();
+
+ scoped_ptr<base::TickClock> clock_;
+
+ DISALLOW_COPY_AND_ASSIGN(TickClockRefCounted);
+};
+
+} // namespace copresence
+
+#endif // COMPONENTS_COPRESENCE_HANDLERS_AUDIO_TICK_CLOCK_REF_COUNTED_H_