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> |
| 6 #include <vector> |
| 7 |
| 8 #include "base/bind.h" |
5 #include "base/time/time.h" | 9 #include "base/time/time.h" |
6 #include "components/copresence/handlers/audio/audio_directive_handler.h" | 10 #include "components/copresence/handlers/audio/audio_directive_handler.h" |
7 #include "components/copresence/handlers/directive_handler_impl.h" | 11 #include "components/copresence/handlers/directive_handler_impl.h" |
8 #include "components/copresence/proto/data.pb.h" | 12 #include "components/copresence/proto/data.pb.h" |
9 #include "components/copresence/test/stub_whispernet_client.h" | 13 #include "components/copresence/test/stub_whispernet_client.h" |
10 #include "testing/gmock/include/gmock/gmock.h" | 14 #include "testing/gmock/include/gmock/gmock.h" |
11 | 15 |
12 using testing::ElementsAre; | 16 using testing::ElementsAre; |
13 using testing::IsEmpty; | 17 using testing::IsEmpty; |
14 | 18 |
15 namespace { | 19 namespace { |
16 | 20 |
17 const int64 kMaxUnlabeledTtl = 60000; // 1 minute | 21 const int64 kMaxUnlabeledTtl = 60000; // 1 minute |
18 const int64 kExcessiveUnlabeledTtl = 120000; // 2 minutes | 22 const int64 kExcessiveUnlabeledTtl = 120000; // 2 minutes |
19 const int64 kDefaultTtl = 600000; // 10 minutes | 23 const int64 kDefaultTtl = 600000; // 10 minutes |
20 | 24 |
21 } // namespace | 25 } // namespace |
22 | 26 |
23 namespace copresence { | 27 namespace copresence { |
24 | 28 |
25 Directive CreateDirective(const std::string& publish_id, | 29 void IgnoreDirectiveUpdates(const std::vector<Directive>& /* directives */) {} |
26 const std::string& subscribe_id, | 30 |
27 const std::string& token, | 31 const Directive CreateDirective(const std::string& publish_id, |
28 int64 ttl_ms) { | 32 const std::string& subscribe_id, |
| 33 const std::string& token, |
| 34 int64 ttl_ms) { |
29 Directive directive; | 35 Directive directive; |
30 directive.set_instruction_type(TOKEN); | 36 directive.set_instruction_type(TOKEN); |
31 directive.set_published_message_id(publish_id); | 37 directive.set_published_message_id(publish_id); |
32 directive.set_subscription_id(subscribe_id); | 38 directive.set_subscription_id(subscribe_id); |
33 directive.set_ttl_millis(ttl_ms); | 39 directive.set_ttl_millis(ttl_ms); |
34 | 40 |
35 TokenInstruction* instruction = new TokenInstruction; | 41 TokenInstruction* instruction = new TokenInstruction; |
36 instruction->set_token_id(token); | 42 instruction->set_token_id(token); |
37 instruction->set_medium(AUDIO_ULTRASOUND_PASSBAND); | 43 instruction->set_medium(AUDIO_ULTRASOUND_PASSBAND); |
38 directive.set_allocated_token_instruction(instruction); | 44 directive.set_allocated_token_instruction(instruction); |
39 | 45 |
40 return directive; | 46 return directive; |
41 } | 47 } |
42 | 48 |
43 Directive CreateDirective(const std::string& publish_id, | 49 const Directive CreateDirective(const std::string& publish_id, |
44 const std::string& subscribe_id, | 50 const std::string& subscribe_id, |
45 const std::string& token) { | 51 const std::string& token) { |
46 return CreateDirective(publish_id, subscribe_id, token, kDefaultTtl); | 52 return CreateDirective(publish_id, subscribe_id, token, kDefaultTtl); |
47 } | 53 } |
48 | 54 |
49 class FakeAudioDirectiveHandler final : public AudioDirectiveHandler { | 55 class FakeAudioDirectiveHandler final : public AudioDirectiveHandler { |
50 public: | 56 public: |
51 FakeAudioDirectiveHandler() {} | 57 FakeAudioDirectiveHandler() {} |
52 | 58 |
53 void Initialize(WhispernetClient* /* whispernet_client */, | 59 void Initialize(WhispernetClient* /* whispernet_client */, |
54 const TokensCallback& /* tokens_cb */) override {} | 60 const TokensCallback& /* tokens_cb */) override {} |
55 | 61 |
56 void AddInstruction(const TokenInstruction& instruction, | 62 void AddInstruction(const Directive& directive, |
57 const std::string& /* op_id */, | 63 const std::string& /* op_id */) override { |
58 base::TimeDelta ttl) override { | 64 added_tokens_.push_back(directive.token_instruction().token_id()); |
59 added_tokens_.push_back(instruction.token_id()); | 65 added_ttls_.push_back(directive.ttl_millis()); |
60 added_ttls_.push_back(ttl.InMilliseconds()); | |
61 } | 66 } |
62 | 67 |
63 void RemoveInstructions(const std::string& op_id) override { | 68 void RemoveInstructions(const std::string& op_id) override { |
64 removed_operations_.push_back(op_id); | 69 removed_operations_.push_back(op_id); |
65 } | 70 } |
66 | 71 |
67 const std::string PlayingToken(AudioType /* type */) const override { | 72 const std::string PlayingToken(AudioType /* type */) const override { |
68 NOTREACHED(); | 73 NOTREACHED(); |
69 return ""; | 74 return ""; |
70 } | 75 } |
(...skipping 20 matching lines...) Expand all Loading... |
91 std::vector<int64> added_ttls_; | 96 std::vector<int64> added_ttls_; |
92 std::vector<std::string> removed_operations_; | 97 std::vector<std::string> removed_operations_; |
93 }; | 98 }; |
94 | 99 |
95 class DirectiveHandlerTest : public testing::Test { | 100 class DirectiveHandlerTest : public testing::Test { |
96 public: | 101 public: |
97 DirectiveHandlerTest() | 102 DirectiveHandlerTest() |
98 : whispernet_client_(new StubWhispernetClient), | 103 : whispernet_client_(new StubWhispernetClient), |
99 audio_handler_(new FakeAudioDirectiveHandler), | 104 audio_handler_(new FakeAudioDirectiveHandler), |
100 directive_handler_( | 105 directive_handler_( |
| 106 base::Bind(&IgnoreDirectiveUpdates), |
101 make_scoped_ptr<AudioDirectiveHandler>(audio_handler_)) {} | 107 make_scoped_ptr<AudioDirectiveHandler>(audio_handler_)) {} |
102 | 108 |
103 protected: | 109 protected: |
104 void StartDirectiveHandler() { | 110 void StartDirectiveHandler() { |
105 directive_handler_.Start(whispernet_client_.get(), TokensCallback()); | 111 directive_handler_.Start(whispernet_client_.get(), TokensCallback()); |
106 } | 112 } |
107 | 113 |
108 scoped_ptr<WhispernetClient> whispernet_client_; | 114 scoped_ptr<WhispernetClient> whispernet_client_; |
109 FakeAudioDirectiveHandler* audio_handler_; | 115 FakeAudioDirectiveHandler* audio_handler_; |
110 DirectiveHandlerImpl directive_handler_; | 116 DirectiveHandlerImpl directive_handler_; |
(...skipping 20 matching lines...) Expand all Loading... |
131 | 137 |
132 StartDirectiveHandler(); | 138 StartDirectiveHandler(); |
133 directive_handler_.RemoveDirectives("id 3"); | 139 directive_handler_.RemoveDirectives("id 3"); |
134 | 140 |
135 EXPECT_THAT(audio_handler_->added_tokens(), ElementsAre("token 3")); | 141 EXPECT_THAT(audio_handler_->added_tokens(), ElementsAre("token 3")); |
136 EXPECT_THAT(audio_handler_->added_ttls(), ElementsAre(kDefaultTtl)); | 142 EXPECT_THAT(audio_handler_->added_ttls(), ElementsAre(kDefaultTtl)); |
137 EXPECT_THAT(audio_handler_->removed_operations(), ElementsAre("id 3")); | 143 EXPECT_THAT(audio_handler_->removed_operations(), ElementsAre("id 3")); |
138 } | 144 } |
139 | 145 |
140 } // namespace copresence | 146 } // namespace copresence |
OLD | NEW |