| 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 |
| 5 #include "base/bind.h" | 8 #include "base/bind.h" |
| 6 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 7 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| 8 #include "base/test/simple_test_tick_clock.h" | 11 #include "base/test/simple_test_tick_clock.h" |
| 9 #include "base/timer/mock_timer.h" | 12 #include "base/timer/mock_timer.h" |
| 10 #include "components/copresence/handlers/audio/audio_directive_handler_impl.h" | 13 #include "components/copresence/handlers/audio/audio_directive_handler_impl.h" |
| 11 #include "components/copresence/handlers/audio/tick_clock_ref_counted.h" | 14 #include "components/copresence/handlers/audio/tick_clock_ref_counted.h" |
| 12 #include "components/copresence/mediums/audio/audio_manager.h" | 15 #include "components/copresence/mediums/audio/audio_manager.h" |
| 13 #include "components/copresence/proto/data.pb.h" | 16 #include "components/copresence/proto/data.pb.h" |
| 14 #include "components/copresence/test/audio_test_support.h" | 17 #include "components/copresence/test/audio_test_support.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 16 | 19 |
| 17 namespace copresence { | 20 namespace copresence { |
| 18 | 21 |
| 22 namespace { |
| 23 |
| 24 const Directive CreateDirective(TokenInstructionType type, |
| 25 bool audible, |
| 26 int64 ttl) { |
| 27 Directive directive; |
| 28 directive.mutable_token_instruction()->set_token_instruction_type(type); |
| 29 directive.mutable_token_instruction()->set_token_id("token"); |
| 30 directive.mutable_token_instruction()->set_medium(audible ? |
| 31 AUDIO_AUDIBLE_DTMF : AUDIO_ULTRASOUND_PASSBAND); |
| 32 directive.set_ttl_millis(ttl); |
| 33 return directive; |
| 34 } |
| 35 |
| 36 } // namespace |
| 37 |
| 19 class AudioManagerStub final : public AudioManager { | 38 class AudioManagerStub final : public AudioManager { |
| 20 public: | 39 public: |
| 21 AudioManagerStub() {} | 40 AudioManagerStub() {} |
| 22 ~AudioManagerStub() override {} | 41 ~AudioManagerStub() override {} |
| 23 | 42 |
| 24 // AudioManager overrides: | 43 // AudioManager overrides: |
| 25 void Initialize(WhispernetClient* whispernet_client, | 44 void Initialize(WhispernetClient* whispernet_client, |
| 26 const TokensCallback& tokens_cb) override {} | 45 const TokensCallback& tokens_cb) override {} |
| 27 void StartPlaying(AudioType type) override { playing_[type] = true; } | 46 void StartPlaying(AudioType type) override { playing_[type] = true; } |
| 28 void StopPlaying(AudioType type) override { playing_[type] = false; } | 47 void StopPlaying(AudioType type) override { playing_[type] = false; } |
| (...skipping 15 matching lines...) Expand all Loading... |
| 44 }; | 63 }; |
| 45 | 64 |
| 46 class AudioDirectiveHandlerTest : public testing::Test { | 65 class AudioDirectiveHandlerTest : public testing::Test { |
| 47 public: | 66 public: |
| 48 AudioDirectiveHandlerTest() { | 67 AudioDirectiveHandlerTest() { |
| 49 manager_ptr_ = new AudioManagerStub; | 68 manager_ptr_ = new AudioManagerStub; |
| 50 timer_ptr_ = new base::MockTimer(false, false); | 69 timer_ptr_ = new base::MockTimer(false, false); |
| 51 clock_ptr_ = new base::SimpleTestTickClock; | 70 clock_ptr_ = new base::SimpleTestTickClock; |
| 52 | 71 |
| 53 directive_handler_.reset(new AudioDirectiveHandlerImpl( | 72 directive_handler_.reset(new AudioDirectiveHandlerImpl( |
| 73 base::Bind(&AudioDirectiveHandlerTest::GetDirectiveUpdates, |
| 74 base::Unretained(this)), |
| 54 make_scoped_ptr<AudioManager>(manager_ptr_), | 75 make_scoped_ptr<AudioManager>(manager_ptr_), |
| 55 make_scoped_ptr<base::Timer>(timer_ptr_), | 76 make_scoped_ptr<base::Timer>(timer_ptr_), |
| 56 make_scoped_refptr(new TickClockRefCounted(clock_ptr_)))); | 77 make_scoped_refptr(new TickClockRefCounted(clock_ptr_)))); |
| 57 directive_handler_->Initialize(nullptr, TokensCallback()); | 78 directive_handler_->Initialize(nullptr, TokensCallback()); |
| 58 } | 79 } |
| 59 ~AudioDirectiveHandlerTest() override {} | 80 ~AudioDirectiveHandlerTest() override {} |
| 60 | 81 |
| 61 protected: | 82 protected: |
| 62 TokenInstruction CreateTransmitInstruction(const std::string& token, | 83 const std::vector<Directive>& current_directives() { |
| 63 bool audible) { | 84 return current_directives_; |
| 64 TokenInstruction instruction; | |
| 65 instruction.set_token_instruction_type(TRANSMIT); | |
| 66 instruction.set_token_id(token); | |
| 67 instruction.set_medium(audible ? AUDIO_AUDIBLE_DTMF | |
| 68 : AUDIO_ULTRASOUND_PASSBAND); | |
| 69 return instruction; | |
| 70 } | |
| 71 | |
| 72 TokenInstruction CreateReceiveInstruction(bool audible) { | |
| 73 TokenInstruction instruction; | |
| 74 instruction.set_token_instruction_type(RECEIVE); | |
| 75 instruction.set_medium(audible ? AUDIO_AUDIBLE_DTMF | |
| 76 : AUDIO_ULTRASOUND_PASSBAND); | |
| 77 return instruction; | |
| 78 } | 85 } |
| 79 | 86 |
| 80 bool IsPlaying(AudioType type) { return manager_ptr_->IsPlaying(type); } | 87 bool IsPlaying(AudioType type) { return manager_ptr_->IsPlaying(type); } |
| 81 | 88 |
| 82 bool IsRecording(AudioType type) { return manager_ptr_->IsRecording(type); } | 89 bool IsRecording(AudioType type) { return manager_ptr_->IsRecording(type); } |
| 83 | 90 |
| 84 // This order is important. We want the message loop to get created before | 91 // This order is important. We want the message loop to get created before |
| 85 // our the audio directive handler since the directive list ctor (invoked | 92 // our the audio directive handler since the directive list ctor (invoked |
| 86 // from the directive handler ctor) will post tasks. | 93 // from the directive handler ctor) will post tasks. |
| 87 base::MessageLoop message_loop_; | 94 base::MessageLoop message_loop_; |
| 88 scoped_ptr<AudioDirectiveHandler> directive_handler_; | 95 scoped_ptr<AudioDirectiveHandler> directive_handler_; |
| 89 | 96 |
| 97 std::vector<Directive> current_directives_; |
| 98 |
| 90 // Unowned. | 99 // Unowned. |
| 91 AudioManagerStub* manager_ptr_; | 100 AudioManagerStub* manager_ptr_; |
| 92 base::MockTimer* timer_ptr_; | 101 base::MockTimer* timer_ptr_; |
| 93 base::SimpleTestTickClock* clock_ptr_; | 102 base::SimpleTestTickClock* clock_ptr_; |
| 94 | 103 |
| 95 private: | 104 private: |
| 105 void GetDirectiveUpdates(const std::vector<Directive>& current_directives) { |
| 106 current_directives_ = current_directives; |
| 107 } |
| 108 |
| 96 DISALLOW_COPY_AND_ASSIGN(AudioDirectiveHandlerTest); | 109 DISALLOW_COPY_AND_ASSIGN(AudioDirectiveHandlerTest); |
| 97 }; | 110 }; |
| 98 | 111 |
| 99 TEST_F(AudioDirectiveHandlerTest, Basic) { | 112 TEST_F(AudioDirectiveHandlerTest, Basic) { |
| 100 const base::TimeDelta kTtl = base::TimeDelta::FromMilliseconds(9999); | 113 const int64 kTtl = 10; |
| 101 directive_handler_->AddInstruction( | 114 directive_handler_->AddInstruction(CreateDirective(TRANSMIT, true, kTtl), |
| 102 CreateTransmitInstruction("token", true), "op_id1", kTtl); | 115 "op_id1"); |
| 103 directive_handler_->AddInstruction( | 116 directive_handler_->AddInstruction(CreateDirective(TRANSMIT, false, kTtl), |
| 104 CreateTransmitInstruction("token", false), "op_id1", kTtl); | 117 "op_id1"); |
| 105 directive_handler_->AddInstruction( | 118 directive_handler_->AddInstruction(CreateDirective(TRANSMIT, false, kTtl), |
| 106 CreateTransmitInstruction("token", false), "op_id2", kTtl); | 119 "op_id2"); |
| 107 directive_handler_->AddInstruction( | 120 directive_handler_->AddInstruction(CreateDirective(RECEIVE, false, kTtl), |
| 108 CreateReceiveInstruction(false), "op_id1", kTtl); | 121 "op_id1"); |
| 109 directive_handler_->AddInstruction( | 122 directive_handler_->AddInstruction(CreateDirective(RECEIVE, true, kTtl), |
| 110 CreateReceiveInstruction(true), "op_id2", kTtl); | 123 "op_id2"); |
| 111 directive_handler_->AddInstruction( | 124 directive_handler_->AddInstruction(CreateDirective(RECEIVE, false, kTtl), |
| 112 CreateReceiveInstruction(false), "op_id3", kTtl); | 125 "op_id3"); |
| 113 | 126 |
| 114 EXPECT_TRUE(IsPlaying(AUDIBLE)); | 127 EXPECT_TRUE(IsPlaying(AUDIBLE)); |
| 115 EXPECT_TRUE(IsPlaying(INAUDIBLE)); | 128 EXPECT_TRUE(IsPlaying(INAUDIBLE)); |
| 116 EXPECT_TRUE(IsRecording(AUDIBLE)); | 129 EXPECT_TRUE(IsRecording(AUDIBLE)); |
| 117 EXPECT_TRUE(IsRecording(INAUDIBLE)); | 130 EXPECT_TRUE(IsRecording(INAUDIBLE)); |
| 118 | 131 |
| 119 directive_handler_->RemoveInstructions("op_id1"); | 132 directive_handler_->RemoveInstructions("op_id1"); |
| 120 EXPECT_FALSE(IsPlaying(AUDIBLE)); | 133 EXPECT_FALSE(IsPlaying(AUDIBLE)); |
| 121 EXPECT_TRUE(IsPlaying(INAUDIBLE)); | 134 EXPECT_TRUE(IsPlaying(INAUDIBLE)); |
| 122 EXPECT_TRUE(IsRecording(AUDIBLE)); | 135 EXPECT_TRUE(IsRecording(AUDIBLE)); |
| 123 EXPECT_TRUE(IsRecording(INAUDIBLE)); | 136 EXPECT_TRUE(IsRecording(INAUDIBLE)); |
| 124 | 137 |
| 125 directive_handler_->RemoveInstructions("op_id2"); | 138 directive_handler_->RemoveInstructions("op_id2"); |
| 126 EXPECT_FALSE(IsPlaying(INAUDIBLE)); | 139 EXPECT_FALSE(IsPlaying(INAUDIBLE)); |
| 127 EXPECT_FALSE(IsRecording(AUDIBLE)); | 140 EXPECT_FALSE(IsRecording(AUDIBLE)); |
| 128 EXPECT_TRUE(IsRecording(INAUDIBLE)); | 141 EXPECT_TRUE(IsRecording(INAUDIBLE)); |
| 129 | 142 |
| 130 directive_handler_->RemoveInstructions("op_id3"); | 143 directive_handler_->RemoveInstructions("op_id3"); |
| 131 EXPECT_FALSE(IsRecording(INAUDIBLE)); | 144 EXPECT_FALSE(IsRecording(INAUDIBLE)); |
| 132 } | 145 } |
| 133 | 146 |
| 134 TEST_F(AudioDirectiveHandlerTest, Timed) { | 147 TEST_F(AudioDirectiveHandlerTest, Timed) { |
| 135 const base::TimeDelta kTtl1 = base::TimeDelta::FromMilliseconds(1337); | 148 directive_handler_->AddInstruction(CreateDirective(TRANSMIT, true, 6), |
| 136 directive_handler_->AddInstruction( | 149 "op_id1"); |
| 137 CreateTransmitInstruction("token", true), "op_id1", kTtl1); | 150 directive_handler_->AddInstruction(CreateDirective(TRANSMIT, false, 8), |
| 151 "op_id1"); |
| 152 directive_handler_->AddInstruction(CreateDirective(RECEIVE, false, 4), |
| 153 "op_id3"); |
| 138 | 154 |
| 139 const base::TimeDelta kTtl2 = base::TimeDelta::FromMilliseconds(1338); | |
| 140 directive_handler_->AddInstruction( | |
| 141 CreateTransmitInstruction("token", false), "op_id1", kTtl2); | |
| 142 | |
| 143 const base::TimeDelta kTtl3 = base::TimeDelta::FromMilliseconds(1336); | |
| 144 directive_handler_->AddInstruction( | |
| 145 CreateReceiveInstruction(false), "op_id3", kTtl3); | |
| 146 EXPECT_TRUE(IsPlaying(AUDIBLE)); | 155 EXPECT_TRUE(IsPlaying(AUDIBLE)); |
| 147 EXPECT_TRUE(IsPlaying(INAUDIBLE)); | 156 EXPECT_TRUE(IsPlaying(INAUDIBLE)); |
| 148 EXPECT_FALSE(IsRecording(AUDIBLE)); | 157 EXPECT_FALSE(IsRecording(AUDIBLE)); |
| 149 EXPECT_TRUE(IsRecording(INAUDIBLE)); | 158 EXPECT_TRUE(IsRecording(INAUDIBLE)); |
| 150 | 159 |
| 151 // We *have* to call an operation on the directive handler after we advance | 160 // Every time we advance and a directive expires, the timer should fire also. |
| 152 // time to trigger the next set of operations, so ensure that after calling | 161 clock_ptr_->Advance(base::TimeDelta::FromMilliseconds(5)); |
| 153 // advance, we are also calling another operation. | 162 timer_ptr_->Fire(); |
| 154 clock_ptr_->Advance(kTtl3 + base::TimeDelta::FromMilliseconds(1)); | |
| 155 | 163 |
| 156 // We are now at base + 1337ms. | 164 // We are now at +5ms. This instruction expires at +10ms. |
| 157 // This instruction expires at base + (1337 + 1337 = 2674) | 165 directive_handler_->AddInstruction(CreateDirective(RECEIVE, true, 5), |
| 158 directive_handler_->AddInstruction( | 166 "op_id4"); |
| 159 CreateReceiveInstruction(true), "op_id4", kTtl1); | |
| 160 EXPECT_TRUE(IsPlaying(AUDIBLE)); | 167 EXPECT_TRUE(IsPlaying(AUDIBLE)); |
| 161 EXPECT_TRUE(IsPlaying(INAUDIBLE)); | 168 EXPECT_TRUE(IsPlaying(INAUDIBLE)); |
| 162 EXPECT_TRUE(IsRecording(AUDIBLE)); | 169 EXPECT_TRUE(IsRecording(AUDIBLE)); |
| 163 EXPECT_FALSE(IsRecording(INAUDIBLE)); | 170 EXPECT_FALSE(IsRecording(INAUDIBLE)); |
| 164 | 171 |
| 165 clock_ptr_->Advance(base::TimeDelta::FromMilliseconds(1)); | 172 // Advance to +7ms. |
| 173 const base::TimeDelta twoMs = base::TimeDelta::FromMilliseconds(2); |
| 174 clock_ptr_->Advance(twoMs); |
| 175 timer_ptr_->Fire(); |
| 166 | 176 |
| 167 // We are now at base + 1338ms. | |
| 168 timer_ptr_->Fire(); | |
| 169 EXPECT_FALSE(IsPlaying(AUDIBLE)); | 177 EXPECT_FALSE(IsPlaying(AUDIBLE)); |
| 170 EXPECT_TRUE(IsPlaying(INAUDIBLE)); | 178 EXPECT_TRUE(IsPlaying(INAUDIBLE)); |
| 171 EXPECT_TRUE(IsRecording(AUDIBLE)); | 179 EXPECT_TRUE(IsRecording(AUDIBLE)); |
| 172 | 180 |
| 173 clock_ptr_->Advance(base::TimeDelta::FromMilliseconds(1)); | 181 // Advance to +9ms. |
| 174 | 182 clock_ptr_->Advance(twoMs); |
| 175 // We are now at base + 1339ms. | |
| 176 timer_ptr_->Fire(); | 183 timer_ptr_->Fire(); |
| 177 EXPECT_FALSE(IsPlaying(INAUDIBLE)); | 184 EXPECT_FALSE(IsPlaying(INAUDIBLE)); |
| 178 EXPECT_TRUE(IsRecording(AUDIBLE)); | 185 EXPECT_TRUE(IsRecording(AUDIBLE)); |
| 179 | 186 |
| 180 clock_ptr_->Advance(kTtl3); | 187 // Advance to +11ms. |
| 181 | 188 clock_ptr_->Advance(twoMs); |
| 182 // We are now at base + 2676ms. | |
| 183 timer_ptr_->Fire(); | 189 timer_ptr_->Fire(); |
| 184 EXPECT_FALSE(IsRecording(AUDIBLE)); | 190 EXPECT_FALSE(IsRecording(AUDIBLE)); |
| 185 } | 191 } |
| 186 | 192 |
| 187 // TODO(rkc): Write more tests that check more convoluted sequences of | 193 // TODO(rkc): Write more tests that check more convoluted sequences of |
| 188 // transmits/receives. | 194 // transmits/receives. |
| 189 | 195 |
| 190 } // namespace copresence | 196 } // namespace copresence |
| OLD | NEW |