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/audio/audio_directive_handler.h" | |
6 | |
7 #include "base/bind.h" | 5 #include "base/bind.h" |
8 #include "base/memory/scoped_ptr.h" | 6 #include "base/memory/scoped_ptr.h" |
9 #include "base/message_loop/message_loop.h" | 7 #include "base/message_loop/message_loop.h" |
10 #include "base/test/simple_test_tick_clock.h" | 8 #include "base/test/simple_test_tick_clock.h" |
11 #include "base/timer/mock_timer.h" | 9 #include "base/timer/mock_timer.h" |
| 10 #include "components/copresence/handlers/audio/audio_directive_handler_impl.h" |
12 #include "components/copresence/handlers/audio/tick_clock_ref_counted.h" | 11 #include "components/copresence/handlers/audio/tick_clock_ref_counted.h" |
13 #include "components/copresence/mediums/audio/audio_manager.h" | 12 #include "components/copresence/mediums/audio/audio_manager.h" |
| 13 #include "components/copresence/proto/data.pb.h" |
14 #include "components/copresence/test/audio_test_support.h" | 14 #include "components/copresence/test/audio_test_support.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
16 | 16 |
17 namespace copresence { | 17 namespace copresence { |
18 | 18 |
19 namespace { | 19 namespace { |
20 | 20 |
21 // Callback stubs to pass into the directive handler. | 21 // Callback stubs to pass into the directive handler. |
22 void DecodeSamples(AudioType, const std::string&) { | 22 void DecodeSamples(AudioType, const std::string&) { |
23 } | 23 } |
(...skipping 24 matching lines...) Expand all Loading... |
48 private: | 48 private: |
49 // Indexed using enum AudioType. | 49 // Indexed using enum AudioType. |
50 bool playing_[2]; | 50 bool playing_[2]; |
51 bool recording_[2]; | 51 bool recording_[2]; |
52 | 52 |
53 DISALLOW_COPY_AND_ASSIGN(AudioManagerStub); | 53 DISALLOW_COPY_AND_ASSIGN(AudioManagerStub); |
54 }; | 54 }; |
55 | 55 |
56 class AudioDirectiveHandlerTest : public testing::Test { | 56 class AudioDirectiveHandlerTest : public testing::Test { |
57 public: | 57 public: |
58 AudioDirectiveHandlerTest() | 58 AudioDirectiveHandlerTest() { |
59 : directive_handler_(new AudioDirectiveHandler()) { | 59 manager_ptr_ = new AudioManagerStub; |
60 scoped_ptr<AudioManagerStub> manager(new AudioManagerStub); | 60 timer_ptr_ = new base::MockTimer(false, false); |
61 manager_ptr_ = manager.get(); | 61 clock_ptr_ = new base::SimpleTestTickClock; |
62 directive_handler_->set_audio_manager_for_testing(manager.Pass()); | 62 |
| 63 directive_handler_.reset(new AudioDirectiveHandlerImpl( |
| 64 make_scoped_ptr<AudioManager>(manager_ptr_), |
| 65 make_scoped_ptr<base::Timer>(timer_ptr_), |
| 66 make_scoped_refptr(new TickClockRefCounted(clock_ptr_)))); |
63 directive_handler_->Initialize(base::Bind(&DecodeSamples), | 67 directive_handler_->Initialize(base::Bind(&DecodeSamples), |
64 base::Bind(&EncodeToken)); | 68 base::Bind(&EncodeToken)); |
65 } | 69 } |
66 ~AudioDirectiveHandlerTest() override {} | 70 ~AudioDirectiveHandlerTest() override {} |
67 | 71 |
68 void DirectiveAdded() {} | |
69 | |
70 protected: | 72 protected: |
71 copresence::TokenInstruction CreateTransmitInstruction( | 73 TokenInstruction CreateTransmitInstruction(const std::string& token, |
72 const std::string& token, | 74 bool audible) { |
73 bool audible) { | 75 TokenInstruction instruction; |
74 copresence::TokenInstruction instruction; | 76 instruction.set_token_instruction_type(TRANSMIT); |
75 instruction.set_token_instruction_type(copresence::TRANSMIT); | |
76 instruction.set_token_id(token); | 77 instruction.set_token_id(token); |
77 instruction.set_medium(audible ? AUDIO_AUDIBLE_DTMF | 78 instruction.set_medium(audible ? AUDIO_AUDIBLE_DTMF |
78 : AUDIO_ULTRASOUND_PASSBAND); | 79 : AUDIO_ULTRASOUND_PASSBAND); |
79 return instruction; | 80 return instruction; |
80 } | 81 } |
81 | 82 |
82 copresence::TokenInstruction CreateReceiveInstruction(bool audible) { | 83 TokenInstruction CreateReceiveInstruction(bool audible) { |
83 copresence::TokenInstruction instruction; | 84 TokenInstruction instruction; |
84 instruction.set_token_instruction_type(copresence::RECEIVE); | 85 instruction.set_token_instruction_type(RECEIVE); |
85 instruction.set_medium(audible ? AUDIO_AUDIBLE_DTMF | 86 instruction.set_medium(audible ? AUDIO_AUDIBLE_DTMF |
86 : AUDIO_ULTRASOUND_PASSBAND); | 87 : AUDIO_ULTRASOUND_PASSBAND); |
87 return instruction; | 88 return instruction; |
88 } | 89 } |
89 | 90 |
90 bool IsPlaying(AudioType type) { return manager_ptr_->IsPlaying(type); } | 91 bool IsPlaying(AudioType type) { return manager_ptr_->IsPlaying(type); } |
91 | 92 |
92 bool IsRecording(AudioType type) { return manager_ptr_->IsRecording(type); } | 93 bool IsRecording(AudioType type) { return manager_ptr_->IsRecording(type); } |
93 | 94 |
94 // This order is important. We want the message loop to get created before | 95 // This order is important. We want the message loop to get created before |
95 // our the audio directive handler since the directive list ctor (invoked | 96 // our the audio directive handler since the directive list ctor (invoked |
96 // from the directive handler ctor) will post tasks. | 97 // from the directive handler ctor) will post tasks. |
97 base::MessageLoop message_loop_; | 98 base::MessageLoop message_loop_; |
98 scoped_ptr<AudioDirectiveHandler> directive_handler_; | 99 scoped_ptr<AudioDirectiveHandler> directive_handler_; |
| 100 |
99 // Unowned. | 101 // Unowned. |
100 AudioManagerStub* manager_ptr_; | 102 AudioManagerStub* manager_ptr_; |
| 103 base::MockTimer* timer_ptr_; |
| 104 base::SimpleTestTickClock* clock_ptr_; |
101 | 105 |
102 private: | 106 private: |
103 DISALLOW_COPY_AND_ASSIGN(AudioDirectiveHandlerTest); | 107 DISALLOW_COPY_AND_ASSIGN(AudioDirectiveHandlerTest); |
104 }; | 108 }; |
105 | 109 |
106 TEST_F(AudioDirectiveHandlerTest, Basic) { | 110 TEST_F(AudioDirectiveHandlerTest, Basic) { |
107 const base::TimeDelta kTtl = base::TimeDelta::FromMilliseconds(9999); | 111 const base::TimeDelta kTtl = base::TimeDelta::FromMilliseconds(9999); |
108 directive_handler_->AddInstruction( | 112 directive_handler_->AddInstruction( |
109 CreateTransmitInstruction("token", true), "op_id1", kTtl); | 113 CreateTransmitInstruction("token", true), "op_id1", kTtl); |
110 directive_handler_->AddInstruction( | 114 directive_handler_->AddInstruction( |
(...skipping 21 matching lines...) Expand all Loading... |
132 directive_handler_->RemoveInstructions("op_id2"); | 136 directive_handler_->RemoveInstructions("op_id2"); |
133 EXPECT_FALSE(IsPlaying(INAUDIBLE)); | 137 EXPECT_FALSE(IsPlaying(INAUDIBLE)); |
134 EXPECT_FALSE(IsRecording(AUDIBLE)); | 138 EXPECT_FALSE(IsRecording(AUDIBLE)); |
135 EXPECT_TRUE(IsRecording(INAUDIBLE)); | 139 EXPECT_TRUE(IsRecording(INAUDIBLE)); |
136 | 140 |
137 directive_handler_->RemoveInstructions("op_id3"); | 141 directive_handler_->RemoveInstructions("op_id3"); |
138 EXPECT_FALSE(IsRecording(INAUDIBLE)); | 142 EXPECT_FALSE(IsRecording(INAUDIBLE)); |
139 } | 143 } |
140 | 144 |
141 TEST_F(AudioDirectiveHandlerTest, Timed) { | 145 TEST_F(AudioDirectiveHandlerTest, Timed) { |
142 scoped_ptr<base::SimpleTestTickClock> clock(new base::SimpleTestTickClock()); | |
143 base::SimpleTestTickClock* clock_ptr = clock.get(); | |
144 | |
145 scoped_refptr<TickClockRefCounted> clock_proxy = | |
146 new TickClockRefCounted(clock.Pass()); | |
147 directive_handler_->set_clock_for_testing(clock_proxy); | |
148 | |
149 scoped_ptr<base::Timer> timer(new base::MockTimer(false, false)); | |
150 base::MockTimer* timer_ptr = static_cast<base::MockTimer*>(timer.get()); | |
151 directive_handler_->set_timer_for_testing(timer.Pass()); | |
152 | |
153 const base::TimeDelta kTtl1 = base::TimeDelta::FromMilliseconds(1337); | 146 const base::TimeDelta kTtl1 = base::TimeDelta::FromMilliseconds(1337); |
154 directive_handler_->AddInstruction( | 147 directive_handler_->AddInstruction( |
155 CreateTransmitInstruction("token", true), "op_id1", kTtl1); | 148 CreateTransmitInstruction("token", true), "op_id1", kTtl1); |
156 | 149 |
157 const base::TimeDelta kTtl2 = base::TimeDelta::FromMilliseconds(1338); | 150 const base::TimeDelta kTtl2 = base::TimeDelta::FromMilliseconds(1338); |
158 directive_handler_->AddInstruction( | 151 directive_handler_->AddInstruction( |
159 CreateTransmitInstruction("token", false), "op_id1", kTtl2); | 152 CreateTransmitInstruction("token", false), "op_id1", kTtl2); |
160 | 153 |
161 const base::TimeDelta kTtl3 = base::TimeDelta::FromMilliseconds(1336); | 154 const base::TimeDelta kTtl3 = base::TimeDelta::FromMilliseconds(1336); |
162 directive_handler_->AddInstruction( | 155 directive_handler_->AddInstruction( |
163 CreateReceiveInstruction(false), "op_id3", kTtl3); | 156 CreateReceiveInstruction(false), "op_id3", kTtl3); |
164 EXPECT_TRUE(IsPlaying(AUDIBLE)); | 157 EXPECT_TRUE(IsPlaying(AUDIBLE)); |
165 EXPECT_TRUE(IsPlaying(INAUDIBLE)); | 158 EXPECT_TRUE(IsPlaying(INAUDIBLE)); |
166 EXPECT_FALSE(IsRecording(AUDIBLE)); | 159 EXPECT_FALSE(IsRecording(AUDIBLE)); |
167 EXPECT_TRUE(IsRecording(INAUDIBLE)); | 160 EXPECT_TRUE(IsRecording(INAUDIBLE)); |
168 | 161 |
169 // We *have* to call an operation on the directive handler after we advance | 162 // We *have* to call an operation on the directive handler after we advance |
170 // time to trigger the next set of operations, so ensure that after calling | 163 // time to trigger the next set of operations, so ensure that after calling |
171 // advance, we are also calling another operation. | 164 // advance, we are also calling another operation. |
172 clock_ptr->Advance(kTtl3 + base::TimeDelta::FromMilliseconds(1)); | 165 clock_ptr_->Advance(kTtl3 + base::TimeDelta::FromMilliseconds(1)); |
173 | 166 |
174 // We are now at base + 1337ms. | 167 // We are now at base + 1337ms. |
175 // This instruction expires at base + (1337 + 1337 = 2674) | 168 // This instruction expires at base + (1337 + 1337 = 2674) |
176 directive_handler_->AddInstruction( | 169 directive_handler_->AddInstruction( |
177 CreateReceiveInstruction(true), "op_id4", kTtl1); | 170 CreateReceiveInstruction(true), "op_id4", kTtl1); |
178 EXPECT_TRUE(IsPlaying(AUDIBLE)); | 171 EXPECT_TRUE(IsPlaying(AUDIBLE)); |
179 EXPECT_TRUE(IsPlaying(INAUDIBLE)); | 172 EXPECT_TRUE(IsPlaying(INAUDIBLE)); |
180 EXPECT_TRUE(IsRecording(AUDIBLE)); | 173 EXPECT_TRUE(IsRecording(AUDIBLE)); |
181 EXPECT_FALSE(IsRecording(INAUDIBLE)); | 174 EXPECT_FALSE(IsRecording(INAUDIBLE)); |
182 | 175 |
183 clock_ptr->Advance(base::TimeDelta::FromMilliseconds(1)); | 176 clock_ptr_->Advance(base::TimeDelta::FromMilliseconds(1)); |
184 | 177 |
185 // We are now at base + 1338ms. | 178 // We are now at base + 1338ms. |
186 timer_ptr->Fire(); | 179 timer_ptr_->Fire(); |
187 EXPECT_FALSE(IsPlaying(AUDIBLE)); | 180 EXPECT_FALSE(IsPlaying(AUDIBLE)); |
188 EXPECT_TRUE(IsPlaying(INAUDIBLE)); | 181 EXPECT_TRUE(IsPlaying(INAUDIBLE)); |
189 EXPECT_TRUE(IsRecording(AUDIBLE)); | 182 EXPECT_TRUE(IsRecording(AUDIBLE)); |
190 | 183 |
191 clock_ptr->Advance(base::TimeDelta::FromMilliseconds(1)); | 184 clock_ptr_->Advance(base::TimeDelta::FromMilliseconds(1)); |
192 | 185 |
193 // We are now at base + 1339ms. | 186 // We are now at base + 1339ms. |
194 timer_ptr->Fire(); | 187 timer_ptr_->Fire(); |
195 EXPECT_FALSE(IsPlaying(INAUDIBLE)); | 188 EXPECT_FALSE(IsPlaying(INAUDIBLE)); |
196 EXPECT_TRUE(IsRecording(AUDIBLE)); | 189 EXPECT_TRUE(IsRecording(AUDIBLE)); |
197 | 190 |
198 clock_ptr->Advance(kTtl3); | 191 clock_ptr_->Advance(kTtl3); |
199 | 192 |
200 // We are now at base + 2676ms. | 193 // We are now at base + 2676ms. |
201 timer_ptr->Fire(); | 194 timer_ptr_->Fire(); |
202 EXPECT_FALSE(IsRecording(AUDIBLE)); | 195 EXPECT_FALSE(IsRecording(AUDIBLE)); |
203 } | 196 } |
204 | 197 |
205 // TODO(rkc): Write more tests that check more convoluted sequences of | 198 // TODO(rkc): Write more tests that check more convoluted sequences of |
206 // transmits/receives. | 199 // transmits/receives. |
207 | 200 |
208 } // namespace copresence | 201 } // namespace copresence |
OLD | NEW |