| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include <stdint.h> | |
| 6 | |
| 7 #include <string> | |
| 8 #include <vector> | |
| 9 | |
| 10 #include "base/bind.h" | |
| 11 #include "base/memory/ptr_util.h" | |
| 12 #include "base/time/time.h" | |
| 13 #include "components/audio_modem/test/stub_whispernet_client.h" | |
| 14 #include "components/copresence/handlers/audio/audio_directive_handler.h" | |
| 15 #include "components/copresence/handlers/directive_handler_impl.h" | |
| 16 #include "components/copresence/proto/data.pb.h" | |
| 17 #include "testing/gmock/include/gmock/gmock.h" | |
| 18 | |
| 19 using testing::ElementsAre; | |
| 20 using testing::IsEmpty; | |
| 21 | |
| 22 namespace { | |
| 23 | |
| 24 const int64_t kMaxUnlabeledTtl = 60000; // 1 minute | |
| 25 const int64_t kExcessiveUnlabeledTtl = 120000; // 2 minutes | |
| 26 const int64_t kDefaultTtl = 600000; // 10 minutes | |
| 27 | |
| 28 } // namespace | |
| 29 | |
| 30 namespace copresence { | |
| 31 | |
| 32 const Directive CreateDirective(const std::string& publish_id, | |
| 33 const std::string& subscribe_id, | |
| 34 const std::string& token, | |
| 35 int64_t ttl_ms) { | |
| 36 Directive directive; | |
| 37 directive.set_instruction_type(TOKEN); | |
| 38 directive.set_published_message_id(publish_id); | |
| 39 directive.set_subscription_id(subscribe_id); | |
| 40 directive.set_ttl_millis(ttl_ms); | |
| 41 | |
| 42 TokenInstruction* instruction = new TokenInstruction; | |
| 43 instruction->set_token_id(token); | |
| 44 instruction->set_medium(AUDIO_ULTRASOUND_PASSBAND); | |
| 45 directive.set_allocated_token_instruction(instruction); | |
| 46 | |
| 47 return directive; | |
| 48 } | |
| 49 | |
| 50 const Directive CreateDirective(const std::string& publish_id, | |
| 51 const std::string& subscribe_id, | |
| 52 const std::string& token) { | |
| 53 return CreateDirective(publish_id, subscribe_id, token, kDefaultTtl); | |
| 54 } | |
| 55 | |
| 56 class FakeAudioDirectiveHandler final : public AudioDirectiveHandler { | |
| 57 public: | |
| 58 FakeAudioDirectiveHandler() {} | |
| 59 | |
| 60 void Initialize( | |
| 61 audio_modem::WhispernetClient* /* whispernet_client */, | |
| 62 const audio_modem::TokensCallback& /* tokens_cb */) override {} | |
| 63 | |
| 64 void AddInstruction(const Directive& directive, | |
| 65 const std::string& /* op_id */) override { | |
| 66 added_tokens_.push_back(directive.token_instruction().token_id()); | |
| 67 added_ttls_.push_back(directive.ttl_millis()); | |
| 68 } | |
| 69 | |
| 70 void RemoveInstructions(const std::string& op_id) override { | |
| 71 removed_operations_.push_back(op_id); | |
| 72 } | |
| 73 | |
| 74 const std::string | |
| 75 PlayingToken(audio_modem::AudioType /* type */) const override { | |
| 76 NOTREACHED(); | |
| 77 return ""; | |
| 78 } | |
| 79 | |
| 80 bool IsPlayingTokenHeard(audio_modem::AudioType /* type */) const override { | |
| 81 NOTREACHED(); | |
| 82 return false; | |
| 83 } | |
| 84 | |
| 85 const std::vector<std::string>& added_tokens() const { | |
| 86 return added_tokens_; | |
| 87 } | |
| 88 | |
| 89 const std::vector<int64_t>& added_ttls() const { return added_ttls_; } | |
| 90 | |
| 91 const std::vector<std::string>& removed_operations() const { | |
| 92 return removed_operations_; | |
| 93 } | |
| 94 | |
| 95 private: | |
| 96 std::vector<std::string> added_tokens_; | |
| 97 std::vector<int64_t> added_ttls_; | |
| 98 std::vector<std::string> removed_operations_; | |
| 99 }; | |
| 100 | |
| 101 class DirectiveHandlerTest : public testing::Test { | |
| 102 public: | |
| 103 DirectiveHandlerTest() | |
| 104 : whispernet_client_(new audio_modem::StubWhispernetClient), | |
| 105 audio_handler_(new FakeAudioDirectiveHandler), | |
| 106 directive_handler_( | |
| 107 base::WrapUnique<AudioDirectiveHandler>(audio_handler_)) {} | |
| 108 | |
| 109 protected: | |
| 110 void StartDirectiveHandler() { | |
| 111 directive_handler_.Start(whispernet_client_.get(), | |
| 112 audio_modem::TokensCallback()); | |
| 113 } | |
| 114 | |
| 115 std::unique_ptr<audio_modem::WhispernetClient> whispernet_client_; | |
| 116 FakeAudioDirectiveHandler* audio_handler_; | |
| 117 DirectiveHandlerImpl directive_handler_; | |
| 118 }; | |
| 119 | |
| 120 TEST_F(DirectiveHandlerTest, DirectiveTtl) { | |
| 121 StartDirectiveHandler(); | |
| 122 directive_handler_.AddDirective( | |
| 123 CreateDirective("", "", "token 1", kMaxUnlabeledTtl)); | |
| 124 directive_handler_.AddDirective( | |
| 125 CreateDirective("", "", "token 2", kExcessiveUnlabeledTtl)); | |
| 126 EXPECT_THAT(audio_handler_->added_ttls(), | |
| 127 ElementsAre(kMaxUnlabeledTtl, kMaxUnlabeledTtl)); | |
| 128 } | |
| 129 | |
| 130 TEST_F(DirectiveHandlerTest, Queuing) { | |
| 131 directive_handler_.AddDirective(CreateDirective("id 1", "", "token 1")); | |
| 132 directive_handler_.AddDirective(CreateDirective("", "id 1", "token 2")); | |
| 133 directive_handler_.AddDirective(CreateDirective("id 2", "", "token 3")); | |
| 134 directive_handler_.RemoveDirectives("id 1"); | |
| 135 | |
| 136 EXPECT_THAT(audio_handler_->added_tokens(), IsEmpty()); | |
| 137 EXPECT_THAT(audio_handler_->removed_operations(), IsEmpty()); | |
| 138 | |
| 139 StartDirectiveHandler(); | |
| 140 directive_handler_.RemoveDirectives("id 3"); | |
| 141 | |
| 142 EXPECT_THAT(audio_handler_->added_tokens(), ElementsAre("token 3")); | |
| 143 EXPECT_THAT(audio_handler_->added_ttls(), ElementsAre(kDefaultTtl)); | |
| 144 EXPECT_THAT(audio_handler_->removed_operations(), ElementsAre("id 3")); | |
| 145 } | |
| 146 | |
| 147 } // namespace copresence | |
| OLD | NEW |