Extension syncing: Introduce a NeedsSync pref
[chromium-blink-merge.git] / components / copresence / handlers / directive_handler_unittest.cc
bloba18c77598eb48b35cb6b8fc5ceead3115047f2e5
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include <string>
6 #include <vector>
8 #include "base/bind.h"
9 #include "base/time/time.h"
10 #include "components/audio_modem/test/stub_whispernet_client.h"
11 #include "components/copresence/handlers/audio/audio_directive_handler.h"
12 #include "components/copresence/handlers/directive_handler_impl.h"
13 #include "components/copresence/proto/data.pb.h"
14 #include "testing/gmock/include/gmock/gmock.h"
16 using testing::ElementsAre;
17 using testing::IsEmpty;
19 namespace {
21 const int64 kMaxUnlabeledTtl = 60000; // 1 minute
22 const int64 kExcessiveUnlabeledTtl = 120000; // 2 minutes
23 const int64 kDefaultTtl = 600000; // 10 minutes
25 } // namespace
27 namespace copresence {
29 const Directive CreateDirective(const std::string& publish_id,
30 const std::string& subscribe_id,
31 const std::string& token,
32 int64 ttl_ms) {
33 Directive directive;
34 directive.set_instruction_type(TOKEN);
35 directive.set_published_message_id(publish_id);
36 directive.set_subscription_id(subscribe_id);
37 directive.set_ttl_millis(ttl_ms);
39 TokenInstruction* instruction = new TokenInstruction;
40 instruction->set_token_id(token);
41 instruction->set_medium(AUDIO_ULTRASOUND_PASSBAND);
42 directive.set_allocated_token_instruction(instruction);
44 return directive;
47 const Directive CreateDirective(const std::string& publish_id,
48 const std::string& subscribe_id,
49 const std::string& token) {
50 return CreateDirective(publish_id, subscribe_id, token, kDefaultTtl);
53 class FakeAudioDirectiveHandler final : public AudioDirectiveHandler {
54 public:
55 FakeAudioDirectiveHandler() {}
57 void Initialize(
58 audio_modem::WhispernetClient* /* whispernet_client */,
59 const audio_modem::TokensCallback& /* tokens_cb */) override {}
61 void AddInstruction(const Directive& directive,
62 const std::string& /* op_id */) override {
63 added_tokens_.push_back(directive.token_instruction().token_id());
64 added_ttls_.push_back(directive.ttl_millis());
67 void RemoveInstructions(const std::string& op_id) override {
68 removed_operations_.push_back(op_id);
71 const std::string
72 PlayingToken(audio_modem::AudioType /* type */) const override {
73 NOTREACHED();
74 return "";
77 bool IsPlayingTokenHeard(audio_modem::AudioType /* type */) const override {
78 NOTREACHED();
79 return false;
82 const std::vector<std::string>& added_tokens() const {
83 return added_tokens_;
86 const std::vector<int64>& added_ttls() const {
87 return added_ttls_;
90 const std::vector<std::string>& removed_operations() const {
91 return removed_operations_;
94 private:
95 std::vector<std::string> added_tokens_;
96 std::vector<int64> added_ttls_;
97 std::vector<std::string> removed_operations_;
100 class DirectiveHandlerTest : public testing::Test {
101 public:
102 DirectiveHandlerTest()
103 : whispernet_client_(new audio_modem::StubWhispernetClient),
104 audio_handler_(new FakeAudioDirectiveHandler),
105 directive_handler_(
106 make_scoped_ptr<AudioDirectiveHandler>(audio_handler_)) {}
108 protected:
109 void StartDirectiveHandler() {
110 directive_handler_.Start(whispernet_client_.get(),
111 audio_modem::TokensCallback());
114 scoped_ptr<audio_modem::WhispernetClient> whispernet_client_;
115 FakeAudioDirectiveHandler* audio_handler_;
116 DirectiveHandlerImpl directive_handler_;
119 TEST_F(DirectiveHandlerTest, DirectiveTtl) {
120 StartDirectiveHandler();
121 directive_handler_.AddDirective(
122 CreateDirective("", "", "token 1", kMaxUnlabeledTtl));
123 directive_handler_.AddDirective(
124 CreateDirective("", "", "token 2", kExcessiveUnlabeledTtl));
125 EXPECT_THAT(audio_handler_->added_ttls(),
126 ElementsAre(kMaxUnlabeledTtl, kMaxUnlabeledTtl));
129 TEST_F(DirectiveHandlerTest, Queuing) {
130 directive_handler_.AddDirective(CreateDirective("id 1", "", "token 1"));
131 directive_handler_.AddDirective(CreateDirective("", "id 1", "token 2"));
132 directive_handler_.AddDirective(CreateDirective("id 2", "", "token 3"));
133 directive_handler_.RemoveDirectives("id 1");
135 EXPECT_THAT(audio_handler_->added_tokens(), IsEmpty());
136 EXPECT_THAT(audio_handler_->removed_operations(), IsEmpty());
138 StartDirectiveHandler();
139 directive_handler_.RemoveDirectives("id 3");
141 EXPECT_THAT(audio_handler_->added_tokens(), ElementsAre("token 3"));
142 EXPECT_THAT(audio_handler_->added_ttls(), ElementsAre(kDefaultTtl));
143 EXPECT_THAT(audio_handler_->removed_operations(), ElementsAre("id 3"));
146 } // namespace copresence