| 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<TickClockRefCounted>( |
| 67 new TickClockRefCounted(clock_ptr_)))); |
| 63 directive_handler_->Initialize(base::Bind(&DecodeSamples), | 68 directive_handler_->Initialize(base::Bind(&DecodeSamples), |
| 64 base::Bind(&EncodeToken)); | 69 base::Bind(&EncodeToken)); |
| 65 } | 70 } |
| 66 ~AudioDirectiveHandlerTest() override {} | 71 ~AudioDirectiveHandlerTest() override {} |
| 67 | 72 |
| 68 void DirectiveAdded() {} | |
| 69 | |
| 70 protected: | 73 protected: |
| 71 copresence::TokenInstruction CreateTransmitInstruction( | 74 TokenInstruction CreateTransmitInstruction(const std::string& token, |
| 72 const std::string& token, | 75 bool audible) { |
| 73 bool audible) { | 76 TokenInstruction instruction; |
| 74 copresence::TokenInstruction instruction; | 77 instruction.set_token_instruction_type(TRANSMIT); |
| 75 instruction.set_token_instruction_type(copresence::TRANSMIT); | |
| 76 instruction.set_token_id(token); | 78 instruction.set_token_id(token); |
| 77 instruction.set_medium(audible ? AUDIO_AUDIBLE_DTMF | 79 instruction.set_medium(audible ? AUDIO_AUDIBLE_DTMF |
| 78 : AUDIO_ULTRASOUND_PASSBAND); | 80 : AUDIO_ULTRASOUND_PASSBAND); |
| 79 return instruction; | 81 return instruction; |
| 80 } | 82 } |
| 81 | 83 |
| 82 copresence::TokenInstruction CreateReceiveInstruction(bool audible) { | 84 TokenInstruction CreateReceiveInstruction(bool audible) { |
| 83 copresence::TokenInstruction instruction; | 85 TokenInstruction instruction; |
| 84 instruction.set_token_instruction_type(copresence::RECEIVE); | 86 instruction.set_token_instruction_type(RECEIVE); |
| 85 instruction.set_medium(audible ? AUDIO_AUDIBLE_DTMF | 87 instruction.set_medium(audible ? AUDIO_AUDIBLE_DTMF |
| 86 : AUDIO_ULTRASOUND_PASSBAND); | 88 : AUDIO_ULTRASOUND_PASSBAND); |
| 87 return instruction; | 89 return instruction; |
| 88 } | 90 } |
| 89 | 91 |
| 90 bool IsPlaying(AudioType type) { return manager_ptr_->IsPlaying(type); } | 92 bool IsPlaying(AudioType type) { return manager_ptr_->IsPlaying(type); } |
| 91 | 93 |
| 92 bool IsRecording(AudioType type) { return manager_ptr_->IsRecording(type); } | 94 bool IsRecording(AudioType type) { return manager_ptr_->IsRecording(type); } |
| 93 | 95 |
| 94 // This order is important. We want the message loop to get created before | 96 // 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 | 97 // our the audio directive handler since the directive list ctor (invoked |
| 96 // from the directive handler ctor) will post tasks. | 98 // from the directive handler ctor) will post tasks. |
| 97 base::MessageLoop message_loop_; | 99 base::MessageLoop message_loop_; |
| 98 scoped_ptr<AudioDirectiveHandler> directive_handler_; | 100 scoped_ptr<AudioDirectiveHandler> directive_handler_; |
| 101 |
| 99 // Unowned. | 102 // Unowned. |
| 100 AudioManagerStub* manager_ptr_; | 103 AudioManagerStub* manager_ptr_; |
| 104 base::MockTimer* timer_ptr_; |
| 105 base::SimpleTestTickClock* clock_ptr_; |
| 101 | 106 |
| 102 private: | 107 private: |
| 103 DISALLOW_COPY_AND_ASSIGN(AudioDirectiveHandlerTest); | 108 DISALLOW_COPY_AND_ASSIGN(AudioDirectiveHandlerTest); |
| 104 }; | 109 }; |
| 105 | 110 |
| 106 TEST_F(AudioDirectiveHandlerTest, Basic) { | 111 TEST_F(AudioDirectiveHandlerTest, Basic) { |
| 107 const base::TimeDelta kTtl = base::TimeDelta::FromMilliseconds(9999); | 112 const base::TimeDelta kTtl = base::TimeDelta::FromMilliseconds(9999); |
| 108 directive_handler_->AddInstruction( | 113 directive_handler_->AddInstruction( |
| 109 CreateTransmitInstruction("token", true), "op_id1", kTtl); | 114 CreateTransmitInstruction("token", true), "op_id1", kTtl); |
| 110 directive_handler_->AddInstruction( | 115 directive_handler_->AddInstruction( |
| (...skipping 21 matching lines...) Expand all Loading... |
| 132 directive_handler_->RemoveInstructions("op_id2"); | 137 directive_handler_->RemoveInstructions("op_id2"); |
| 133 EXPECT_FALSE(IsPlaying(INAUDIBLE)); | 138 EXPECT_FALSE(IsPlaying(INAUDIBLE)); |
| 134 EXPECT_FALSE(IsRecording(AUDIBLE)); | 139 EXPECT_FALSE(IsRecording(AUDIBLE)); |
| 135 EXPECT_TRUE(IsRecording(INAUDIBLE)); | 140 EXPECT_TRUE(IsRecording(INAUDIBLE)); |
| 136 | 141 |
| 137 directive_handler_->RemoveInstructions("op_id3"); | 142 directive_handler_->RemoveInstructions("op_id3"); |
| 138 EXPECT_FALSE(IsRecording(INAUDIBLE)); | 143 EXPECT_FALSE(IsRecording(INAUDIBLE)); |
| 139 } | 144 } |
| 140 | 145 |
| 141 TEST_F(AudioDirectiveHandlerTest, Timed) { | 146 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); | 147 const base::TimeDelta kTtl1 = base::TimeDelta::FromMilliseconds(1337); |
| 154 directive_handler_->AddInstruction( | 148 directive_handler_->AddInstruction( |
| 155 CreateTransmitInstruction("token", true), "op_id1", kTtl1); | 149 CreateTransmitInstruction("token", true), "op_id1", kTtl1); |
| 156 | 150 |
| 157 const base::TimeDelta kTtl2 = base::TimeDelta::FromMilliseconds(1338); | 151 const base::TimeDelta kTtl2 = base::TimeDelta::FromMilliseconds(1338); |
| 158 directive_handler_->AddInstruction( | 152 directive_handler_->AddInstruction( |
| 159 CreateTransmitInstruction("token", false), "op_id1", kTtl2); | 153 CreateTransmitInstruction("token", false), "op_id1", kTtl2); |
| 160 | 154 |
| 161 const base::TimeDelta kTtl3 = base::TimeDelta::FromMilliseconds(1336); | 155 const base::TimeDelta kTtl3 = base::TimeDelta::FromMilliseconds(1336); |
| 162 directive_handler_->AddInstruction( | 156 directive_handler_->AddInstruction( |
| 163 CreateReceiveInstruction(false), "op_id3", kTtl3); | 157 CreateReceiveInstruction(false), "op_id3", kTtl3); |
| 164 EXPECT_TRUE(IsPlaying(AUDIBLE)); | 158 EXPECT_TRUE(IsPlaying(AUDIBLE)); |
| 165 EXPECT_TRUE(IsPlaying(INAUDIBLE)); | 159 EXPECT_TRUE(IsPlaying(INAUDIBLE)); |
| 166 EXPECT_FALSE(IsRecording(AUDIBLE)); | 160 EXPECT_FALSE(IsRecording(AUDIBLE)); |
| 167 EXPECT_TRUE(IsRecording(INAUDIBLE)); | 161 EXPECT_TRUE(IsRecording(INAUDIBLE)); |
| 168 | 162 |
| 169 // We *have* to call an operation on the directive handler after we advance | 163 // 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 | 164 // time to trigger the next set of operations, so ensure that after calling |
| 171 // advance, we are also calling another operation. | 165 // advance, we are also calling another operation. |
| 172 clock_ptr->Advance(kTtl3 + base::TimeDelta::FromMilliseconds(1)); | 166 clock_ptr_->Advance(kTtl3 + base::TimeDelta::FromMilliseconds(1)); |
| 173 | 167 |
| 174 // We are now at base + 1337ms. | 168 // We are now at base + 1337ms. |
| 175 // This instruction expires at base + (1337 + 1337 = 2674) | 169 // This instruction expires at base + (1337 + 1337 = 2674) |
| 176 directive_handler_->AddInstruction( | 170 directive_handler_->AddInstruction( |
| 177 CreateReceiveInstruction(true), "op_id4", kTtl1); | 171 CreateReceiveInstruction(true), "op_id4", kTtl1); |
| 178 EXPECT_TRUE(IsPlaying(AUDIBLE)); | 172 EXPECT_TRUE(IsPlaying(AUDIBLE)); |
| 179 EXPECT_TRUE(IsPlaying(INAUDIBLE)); | 173 EXPECT_TRUE(IsPlaying(INAUDIBLE)); |
| 180 EXPECT_TRUE(IsRecording(AUDIBLE)); | 174 EXPECT_TRUE(IsRecording(AUDIBLE)); |
| 181 EXPECT_FALSE(IsRecording(INAUDIBLE)); | 175 EXPECT_FALSE(IsRecording(INAUDIBLE)); |
| 182 | 176 |
| 183 clock_ptr->Advance(base::TimeDelta::FromMilliseconds(1)); | 177 clock_ptr_->Advance(base::TimeDelta::FromMilliseconds(1)); |
| 184 | 178 |
| 185 // We are now at base + 1338ms. | 179 // We are now at base + 1338ms. |
| 186 timer_ptr->Fire(); | 180 timer_ptr_->Fire(); |
| 187 EXPECT_FALSE(IsPlaying(AUDIBLE)); | 181 EXPECT_FALSE(IsPlaying(AUDIBLE)); |
| 188 EXPECT_TRUE(IsPlaying(INAUDIBLE)); | 182 EXPECT_TRUE(IsPlaying(INAUDIBLE)); |
| 189 EXPECT_TRUE(IsRecording(AUDIBLE)); | 183 EXPECT_TRUE(IsRecording(AUDIBLE)); |
| 190 | 184 |
| 191 clock_ptr->Advance(base::TimeDelta::FromMilliseconds(1)); | 185 clock_ptr_->Advance(base::TimeDelta::FromMilliseconds(1)); |
| 192 | 186 |
| 193 // We are now at base + 1339ms. | 187 // We are now at base + 1339ms. |
| 194 timer_ptr->Fire(); | 188 timer_ptr_->Fire(); |
| 195 EXPECT_FALSE(IsPlaying(INAUDIBLE)); | 189 EXPECT_FALSE(IsPlaying(INAUDIBLE)); |
| 196 EXPECT_TRUE(IsRecording(AUDIBLE)); | 190 EXPECT_TRUE(IsRecording(AUDIBLE)); |
| 197 | 191 |
| 198 clock_ptr->Advance(kTtl3); | 192 clock_ptr_->Advance(kTtl3); |
| 199 | 193 |
| 200 // We are now at base + 2676ms. | 194 // We are now at base + 2676ms. |
| 201 timer_ptr->Fire(); | 195 timer_ptr_->Fire(); |
| 202 EXPECT_FALSE(IsRecording(AUDIBLE)); | 196 EXPECT_FALSE(IsRecording(AUDIBLE)); |
| 203 } | 197 } |
| 204 | 198 |
| 205 // TODO(rkc): Write more tests that check more convoluted sequences of | 199 // TODO(rkc): Write more tests that check more convoluted sequences of |
| 206 // transmits/receives. | 200 // transmits/receives. |
| 207 | 201 |
| 208 } // namespace copresence | 202 } // namespace copresence |
| OLD | NEW |