| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <string> | 5 #include <string> |
| 6 #include <vector> | 6 #include <vector> |
| 7 | 7 |
| 8 #include "base/bind.h" | 8 #include "base/bind.h" |
| 9 #include "base/time/time.h" | 9 #include "base/time/time.h" |
| 10 #include "components/audio_modem/test/stub_whispernet_client.h" |
| 10 #include "components/copresence/handlers/audio/audio_directive_handler.h" | 11 #include "components/copresence/handlers/audio/audio_directive_handler.h" |
| 11 #include "components/copresence/handlers/directive_handler_impl.h" | 12 #include "components/copresence/handlers/directive_handler_impl.h" |
| 12 #include "components/copresence/proto/data.pb.h" | 13 #include "components/copresence/proto/data.pb.h" |
| 13 #include "components/copresence/test/stub_whispernet_client.h" | |
| 14 #include "testing/gmock/include/gmock/gmock.h" | 14 #include "testing/gmock/include/gmock/gmock.h" |
| 15 | 15 |
| 16 using testing::ElementsAre; | 16 using testing::ElementsAre; |
| 17 using testing::IsEmpty; | 17 using testing::IsEmpty; |
| 18 | 18 |
| 19 namespace { | 19 namespace { |
| 20 | 20 |
| 21 const int64 kMaxUnlabeledTtl = 60000; // 1 minute | 21 const int64 kMaxUnlabeledTtl = 60000; // 1 minute |
| 22 const int64 kExcessiveUnlabeledTtl = 120000; // 2 minutes | 22 const int64 kExcessiveUnlabeledTtl = 120000; // 2 minutes |
| 23 const int64 kDefaultTtl = 600000; // 10 minutes | 23 const int64 kDefaultTtl = 600000; // 10 minutes |
| (...skipping 25 matching lines...) Expand all Loading... |
| 49 const Directive CreateDirective(const std::string& publish_id, | 49 const Directive CreateDirective(const std::string& publish_id, |
| 50 const std::string& subscribe_id, | 50 const std::string& subscribe_id, |
| 51 const std::string& token) { | 51 const std::string& token) { |
| 52 return CreateDirective(publish_id, subscribe_id, token, kDefaultTtl); | 52 return CreateDirective(publish_id, subscribe_id, token, kDefaultTtl); |
| 53 } | 53 } |
| 54 | 54 |
| 55 class FakeAudioDirectiveHandler final : public AudioDirectiveHandler { | 55 class FakeAudioDirectiveHandler final : public AudioDirectiveHandler { |
| 56 public: | 56 public: |
| 57 FakeAudioDirectiveHandler() {} | 57 FakeAudioDirectiveHandler() {} |
| 58 | 58 |
| 59 void Initialize(WhispernetClient* /* whispernet_client */, | 59 void Initialize( |
| 60 const TokensCallback& /* tokens_cb */) override {} | 60 audio_modem::WhispernetClient* /* whispernet_client */, |
| 61 const audio_modem::TokensCallback& /* tokens_cb */) override {} |
| 61 | 62 |
| 62 void AddInstruction(const Directive& directive, | 63 void AddInstruction(const Directive& directive, |
| 63 const std::string& /* op_id */) override { | 64 const std::string& /* op_id */) override { |
| 64 added_tokens_.push_back(directive.token_instruction().token_id()); | 65 added_tokens_.push_back(directive.token_instruction().token_id()); |
| 65 added_ttls_.push_back(directive.ttl_millis()); | 66 added_ttls_.push_back(directive.ttl_millis()); |
| 66 } | 67 } |
| 67 | 68 |
| 68 void RemoveInstructions(const std::string& op_id) override { | 69 void RemoveInstructions(const std::string& op_id) override { |
| 69 removed_operations_.push_back(op_id); | 70 removed_operations_.push_back(op_id); |
| 70 } | 71 } |
| 71 | 72 |
| 72 const std::string PlayingToken(AudioType /* type */) const override { | 73 const std::string |
| 74 PlayingToken(audio_modem::AudioType /* type */) const override { |
| 73 NOTREACHED(); | 75 NOTREACHED(); |
| 74 return ""; | 76 return ""; |
| 75 } | 77 } |
| 76 | 78 |
| 77 bool IsPlayingTokenHeard(AudioType /* type */) const override { | 79 bool IsPlayingTokenHeard(audio_modem::AudioType /* type */) const override { |
| 78 NOTREACHED(); | 80 NOTREACHED(); |
| 79 return false; | 81 return false; |
| 80 } | 82 } |
| 81 | 83 |
| 82 const std::vector<std::string>& added_tokens() const { | 84 const std::vector<std::string>& added_tokens() const { |
| 83 return added_tokens_; | 85 return added_tokens_; |
| 84 } | 86 } |
| 85 | 87 |
| 86 const std::vector<int64>& added_ttls() const { | 88 const std::vector<int64>& added_ttls() const { |
| 87 return added_ttls_; | 89 return added_ttls_; |
| 88 } | 90 } |
| 89 | 91 |
| 90 const std::vector<std::string>& removed_operations() const { | 92 const std::vector<std::string>& removed_operations() const { |
| 91 return removed_operations_; | 93 return removed_operations_; |
| 92 } | 94 } |
| 93 | 95 |
| 94 private: | 96 private: |
| 95 std::vector<std::string> added_tokens_; | 97 std::vector<std::string> added_tokens_; |
| 96 std::vector<int64> added_ttls_; | 98 std::vector<int64> added_ttls_; |
| 97 std::vector<std::string> removed_operations_; | 99 std::vector<std::string> removed_operations_; |
| 98 }; | 100 }; |
| 99 | 101 |
| 100 class DirectiveHandlerTest : public testing::Test { | 102 class DirectiveHandlerTest : public testing::Test { |
| 101 public: | 103 public: |
| 102 DirectiveHandlerTest() | 104 DirectiveHandlerTest() |
| 103 : whispernet_client_(new StubWhispernetClient), | 105 : whispernet_client_(new audio_modem::StubWhispernetClient), |
| 104 audio_handler_(new FakeAudioDirectiveHandler), | 106 audio_handler_(new FakeAudioDirectiveHandler), |
| 105 directive_handler_( | 107 directive_handler_( |
| 106 base::Bind(&IgnoreDirectiveUpdates), | 108 base::Bind(&IgnoreDirectiveUpdates), |
| 107 make_scoped_ptr<AudioDirectiveHandler>(audio_handler_)) {} | 109 make_scoped_ptr<AudioDirectiveHandler>(audio_handler_)) {} |
| 108 | 110 |
| 109 protected: | 111 protected: |
| 110 void StartDirectiveHandler() { | 112 void StartDirectiveHandler() { |
| 111 directive_handler_.Start(whispernet_client_.get(), TokensCallback()); | 113 directive_handler_.Start(whispernet_client_.get(), |
| 114 audio_modem::TokensCallback()); |
| 112 } | 115 } |
| 113 | 116 |
| 114 scoped_ptr<WhispernetClient> whispernet_client_; | 117 scoped_ptr<audio_modem::WhispernetClient> whispernet_client_; |
| 115 FakeAudioDirectiveHandler* audio_handler_; | 118 FakeAudioDirectiveHandler* audio_handler_; |
| 116 DirectiveHandlerImpl directive_handler_; | 119 DirectiveHandlerImpl directive_handler_; |
| 117 }; | 120 }; |
| 118 | 121 |
| 119 TEST_F(DirectiveHandlerTest, DirectiveTtl) { | 122 TEST_F(DirectiveHandlerTest, DirectiveTtl) { |
| 120 StartDirectiveHandler(); | 123 StartDirectiveHandler(); |
| 121 directive_handler_.AddDirective( | 124 directive_handler_.AddDirective( |
| 122 CreateDirective("", "", "token 1", kMaxUnlabeledTtl)); | 125 CreateDirective("", "", "token 1", kMaxUnlabeledTtl)); |
| 123 directive_handler_.AddDirective( | 126 directive_handler_.AddDirective( |
| 124 CreateDirective("", "", "token 2", kExcessiveUnlabeledTtl)); | 127 CreateDirective("", "", "token 2", kExcessiveUnlabeledTtl)); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 137 | 140 |
| 138 StartDirectiveHandler(); | 141 StartDirectiveHandler(); |
| 139 directive_handler_.RemoveDirectives("id 3"); | 142 directive_handler_.RemoveDirectives("id 3"); |
| 140 | 143 |
| 141 EXPECT_THAT(audio_handler_->added_tokens(), ElementsAre("token 3")); | 144 EXPECT_THAT(audio_handler_->added_tokens(), ElementsAre("token 3")); |
| 142 EXPECT_THAT(audio_handler_->added_ttls(), ElementsAre(kDefaultTtl)); | 145 EXPECT_THAT(audio_handler_->added_ttls(), ElementsAre(kDefaultTtl)); |
| 143 EXPECT_THAT(audio_handler_->removed_operations(), ElementsAre("id 3")); | 146 EXPECT_THAT(audio_handler_->removed_operations(), ElementsAre("id 3")); |
| 144 } | 147 } |
| 145 | 148 |
| 146 } // namespace copresence | 149 } // namespace copresence |
| OLD | NEW |