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