summaryrefslogtreecommitdiffstats
path: root/components/copresence
diff options
context:
space:
mode:
authorckehoe <ckehoe@chromium.org>2014-11-06 10:51:02 -0800
committerCommit bot <commit-bot@chromium.org>2014-11-06 18:51:36 +0000
commit59a7cdb080e5ccb6c766e3beaf47a30f6a02518e (patch)
treef43d33c7588f33012e8e313913a760e422a4f5c3 /components/copresence
parent3813023bde420950c60d9d12a8610510b76da337 (diff)
downloadchromium_src-59a7cdb080e5ccb6c766e3beaf47a30f6a02518e.zip
chromium_src-59a7cdb080e5ccb6c766e3beaf47a30f6a02518e.tar.gz
chromium_src-59a7cdb080e5ccb6c766e3beaf47a30f6a02518e.tar.bz2
Adding a test for the directive handler.
Review URL: https://codereview.chromium.org/697303002 Cr-Commit-Position: refs/heads/master@{#303056}
Diffstat (limited to 'components/copresence')
-rw-r--r--components/copresence/handlers/directive_handler.cc16
-rw-r--r--components/copresence/handlers/directive_handler.h5
-rw-r--r--components/copresence/handlers/directive_handler_unittest.cc100
3 files changed, 111 insertions, 10 deletions
diff --git a/components/copresence/handlers/directive_handler.cc b/components/copresence/handlers/directive_handler.cc
index a916006..1dba911 100644
--- a/components/copresence/handlers/directive_handler.cc
+++ b/components/copresence/handlers/directive_handler.cc
@@ -14,9 +14,10 @@ namespace copresence {
// Public functions
-DirectiveHandler::DirectiveHandler()
- : audio_handler_(new AudioDirectiveHandlerImpl),
- whispernet_client_(nullptr) {}
+DirectiveHandler::DirectiveHandler(
+ scoped_ptr<AudioDirectiveHandler> audio_handler)
+ : audio_handler_(audio_handler.Pass()),
+ whispernet_client_(nullptr) {}
DirectiveHandler::~DirectiveHandler() {}
@@ -46,12 +47,10 @@ void DirectiveHandler::AddDirective(const Directive& directive) {
// WiFi and BLE scans aren't implemented.
DCHECK_EQ(directive.instruction_type(), TOKEN);
- std::string op_id;
- if (directive.has_published_message_id()) {
- op_id = directive.published_message_id();
- } else if (directive.has_subscription_id()) {
+ std::string op_id = directive.published_message_id();
+ if (op_id.empty())
op_id = directive.subscription_id();
- } else {
+ if (op_id.empty()) {
NOTREACHED() << "No operation associated with directive!";
return;
}
@@ -82,6 +81,7 @@ const std::string DirectiveHandler::GetCurrentAudioToken(AudioType type) const {
void DirectiveHandler::StartDirective(const std::string& op_id,
const Directive& directive) {
+ DCHECK(whispernet_client_);
const TokenInstruction& ti = directive.token_instruction();
if (ti.medium() == AUDIO_ULTRASOUND_PASSBAND ||
ti.medium() == AUDIO_AUDIBLE_DTMF) {
diff --git a/components/copresence/handlers/directive_handler.h b/components/copresence/handlers/directive_handler.h
index 7576128..1be7b98 100644
--- a/components/copresence/handlers/directive_handler.h
+++ b/components/copresence/handlers/directive_handler.h
@@ -11,6 +11,7 @@
#include "base/macros.h"
#include "base/memory/scoped_ptr.h"
+#include "components/copresence/handlers/audio/audio_directive_handler_impl.h"
#include "components/copresence/public/whispernet_client.h"
namespace copresence {
@@ -19,11 +20,11 @@ class AudioDirectiveHandler;
class Directive;
// The directive handler manages transmit and receive directives.
-// TODO(ckehoe): Add tests for this class.
// TODO(ckehoe): Turn this into an interface.
class DirectiveHandler {
public:
- DirectiveHandler();
+ explicit DirectiveHandler(scoped_ptr<AudioDirectiveHandler> audio_handler =
+ make_scoped_ptr(new AudioDirectiveHandlerImpl));
virtual ~DirectiveHandler();
// Starts processing directives with the provided Whispernet client.
diff --git a/components/copresence/handlers/directive_handler_unittest.cc b/components/copresence/handlers/directive_handler_unittest.cc
new file mode 100644
index 0000000..09de7ea
--- /dev/null
+++ b/components/copresence/handlers/directive_handler_unittest.cc
@@ -0,0 +1,100 @@
+// 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/directive_handler.h"
+
+#include "base/time/time.h"
+#include "components/copresence/handlers/audio/audio_directive_handler.h"
+#include "components/copresence/proto/data.pb.h"
+#include "components/copresence/test/stub_whispernet_client.h"
+#include "testing/gmock/include/gmock/gmock.h"
+
+using testing::ElementsAre;
+using testing::IsEmpty;
+
+namespace copresence {
+
+Directive CreateDirective(const std::string& publish_id,
+ const std::string& subscribe_id,
+ const std::string& token) {
+ Directive directive;
+ directive.set_instruction_type(TOKEN);
+ directive.set_published_message_id(publish_id);
+ directive.set_subscription_id(subscribe_id);
+
+ TokenInstruction* instruction = new TokenInstruction;
+ instruction->set_token_id(token);
+ instruction->set_medium(AUDIO_ULTRASOUND_PASSBAND);
+ directive.set_allocated_token_instruction(instruction);
+
+ return directive;
+}
+
+class FakeAudioDirectiveHandler final : public AudioDirectiveHandler {
+ public:
+ FakeAudioDirectiveHandler() {}
+
+ void Initialize(
+ const AudioManager::DecodeSamplesCallback& /* decode_cb */,
+ const AudioManager::EncodeTokenCallback& /* encode_cb */) override {}
+
+ void AddInstruction(const TokenInstruction& instruction,
+ const std::string& /* op_id */,
+ base::TimeDelta /* ttl_ms */) override {
+ added_tokens_.push_back(instruction.token_id());
+ }
+
+ void RemoveInstructions(const std::string& op_id) override {
+ removed_operations_.push_back(op_id);
+ }
+
+ const std::string PlayingToken(AudioType /* type */) const override {
+ NOTREACHED();
+ return "";
+ }
+
+ const std::vector<std::string>& added_tokens() const {
+ return added_tokens_;
+ }
+
+ const std::vector<std::string>& removed_operations() const {
+ return removed_operations_;
+ }
+
+ private:
+ std::vector<std::string> added_tokens_;
+ std::vector<std::string> removed_operations_;
+};
+
+class DirectiveHandlerTest : public testing::Test {
+ public:
+ DirectiveHandlerTest()
+ : whispernet_client_(new StubWhispernetClient),
+ audio_handler_(new FakeAudioDirectiveHandler),
+ directive_handler_(
+ make_scoped_ptr<AudioDirectiveHandler>(audio_handler_)) {}
+
+ protected:
+ scoped_ptr<WhispernetClient> whispernet_client_;
+ FakeAudioDirectiveHandler* audio_handler_;
+ DirectiveHandler directive_handler_;
+};
+
+TEST_F(DirectiveHandlerTest, Queuing) {
+ directive_handler_.AddDirective(CreateDirective("id 1", "", "token 1"));
+ directive_handler_.AddDirective(CreateDirective("", "id 1", "token 2"));
+ directive_handler_.AddDirective(CreateDirective("id 2", "", "token 3"));
+ directive_handler_.RemoveDirectives("id 1");
+
+ EXPECT_THAT(audio_handler_->added_tokens(), IsEmpty());
+ EXPECT_THAT(audio_handler_->removed_operations(), IsEmpty());
+
+ directive_handler_.Start(whispernet_client_.get());
+ directive_handler_.RemoveDirectives("id 3");
+
+ EXPECT_THAT(audio_handler_->added_tokens(), ElementsAre("token 3"));
+ EXPECT_THAT(audio_handler_->removed_operations(), ElementsAre("id 3"));
+}
+
+} // namespace copresence