ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / components / copresence / handlers / directive_handler_unittest.cc
blob97853cc202e0e6a8011437ca8a5b9dd38f295ed2
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 void IgnoreDirectiveUpdates(const std::vector<Directive>& /* directives */) {}
31 const Directive CreateDirective(const std::string& publish_id,
32 const std::string& subscribe_id,
33 const std::string& token,
34 int64 ttl_ms) {
35 Directive directive;
36 directive.set_instruction_type(TOKEN);
37 directive.set_published_message_id(publish_id);
38 directive.set_subscription_id(subscribe_id);
39 directive.set_ttl_millis(ttl_ms);
41 TokenInstruction* instruction = new TokenInstruction;
42 instruction->set_token_id(token);
43 instruction->set_medium(AUDIO_ULTRASOUND_PASSBAND);
44 directive.set_allocated_token_instruction(instruction);
46 return directive;
49 const Directive CreateDirective(const std::string& publish_id,
50 const std::string& subscribe_id,
51 const std::string& token) {
52 return CreateDirective(publish_id, subscribe_id, token, kDefaultTtl);
55 class FakeAudioDirectiveHandler final : public AudioDirectiveHandler {
56 public:
57 FakeAudioDirectiveHandler() {}
59 void Initialize(
60 audio_modem::WhispernetClient* /* whispernet_client */,
61 const audio_modem::TokensCallback& /* tokens_cb */) override {}
63 void AddInstruction(const Directive& directive,
64 const std::string& /* op_id */) override {
65 added_tokens_.push_back(directive.token_instruction().token_id());
66 added_ttls_.push_back(directive.ttl_millis());
69 void RemoveInstructions(const std::string& op_id) override {
70 removed_operations_.push_back(op_id);
73 const std::string
74 PlayingToken(audio_modem::AudioType /* type */) const override {
75 NOTREACHED();
76 return "";
79 bool IsPlayingTokenHeard(audio_modem::AudioType /* type */) const override {
80 NOTREACHED();
81 return false;
84 const std::vector<std::string>& added_tokens() const {
85 return added_tokens_;
88 const std::vector<int64>& added_ttls() const {
89 return added_ttls_;
92 const std::vector<std::string>& removed_operations() const {
93 return removed_operations_;
96 private:
97 std::vector<std::string> added_tokens_;
98 std::vector<int64> added_ttls_;
99 std::vector<std::string> removed_operations_;
102 class DirectiveHandlerTest : public testing::Test {
103 public:
104 DirectiveHandlerTest()
105 : whispernet_client_(new audio_modem::StubWhispernetClient),
106 audio_handler_(new FakeAudioDirectiveHandler),
107 directive_handler_(
108 base::Bind(&IgnoreDirectiveUpdates),
109 make_scoped_ptr<AudioDirectiveHandler>(audio_handler_)) {}
111 protected:
112 void StartDirectiveHandler() {
113 directive_handler_.Start(whispernet_client_.get(),
114 audio_modem::TokensCallback());
117 scoped_ptr<audio_modem::WhispernetClient> whispernet_client_;
118 FakeAudioDirectiveHandler* audio_handler_;
119 DirectiveHandlerImpl directive_handler_;
122 TEST_F(DirectiveHandlerTest, DirectiveTtl) {
123 StartDirectiveHandler();
124 directive_handler_.AddDirective(
125 CreateDirective("", "", "token 1", kMaxUnlabeledTtl));
126 directive_handler_.AddDirective(
127 CreateDirective("", "", "token 2", kExcessiveUnlabeledTtl));
128 EXPECT_THAT(audio_handler_->added_ttls(),
129 ElementsAre(kMaxUnlabeledTtl, kMaxUnlabeledTtl));
132 TEST_F(DirectiveHandlerTest, Queuing) {
133 directive_handler_.AddDirective(CreateDirective("id 1", "", "token 1"));
134 directive_handler_.AddDirective(CreateDirective("", "id 1", "token 2"));
135 directive_handler_.AddDirective(CreateDirective("id 2", "", "token 3"));
136 directive_handler_.RemoveDirectives("id 1");
138 EXPECT_THAT(audio_handler_->added_tokens(), IsEmpty());
139 EXPECT_THAT(audio_handler_->removed_operations(), IsEmpty());
141 StartDirectiveHandler();
142 directive_handler_.RemoveDirectives("id 3");
144 EXPECT_THAT(audio_handler_->added_tokens(), ElementsAre("token 3"));
145 EXPECT_THAT(audio_handler_->added_ttls(), ElementsAre(kDefaultTtl));
146 EXPECT_THAT(audio_handler_->removed_operations(), ElementsAre("id 3"));
149 } // namespace copresence