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