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> | 5 #include <string> |
6 #include <vector> | 6 #include <vector> |
7 | 7 |
8 #include "base/bind.h" | 8 #include "base/bind.h" |
9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
11 #include "base/test/simple_test_tick_clock.h" | 11 #include "base/test/simple_test_tick_clock.h" |
12 #include "base/timer/mock_timer.h" | 12 #include "base/timer/mock_timer.h" |
| 13 #include "components/audio_modem/public/modem.h" |
| 14 #include "components/audio_modem/test/random_samples.h" |
13 #include "components/copresence/handlers/audio/audio_directive_handler_impl.h" | 15 #include "components/copresence/handlers/audio/audio_directive_handler_impl.h" |
14 #include "components/copresence/handlers/audio/tick_clock_ref_counted.h" | 16 #include "components/copresence/handlers/audio/tick_clock_ref_counted.h" |
15 #include "components/copresence/mediums/audio/audio_manager.h" | |
16 #include "components/copresence/proto/data.pb.h" | 17 #include "components/copresence/proto/data.pb.h" |
17 #include "components/copresence/test/audio_test_support.h" | |
18 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
19 | 19 |
| 20 using audio_modem::AUDIBLE; |
| 21 using audio_modem::AudioType; |
| 22 using audio_modem::INAUDIBLE; |
| 23 |
20 namespace copresence { | 24 namespace copresence { |
21 | 25 |
22 namespace { | 26 namespace { |
23 | 27 |
24 const Directive CreateDirective(TokenInstructionType type, | 28 const Directive CreateDirective(TokenInstructionType type, |
25 bool audible, | 29 bool audible, |
26 int64 ttl) { | 30 int64 ttl) { |
27 Directive directive; | 31 Directive directive; |
28 directive.mutable_token_instruction()->set_token_instruction_type(type); | 32 directive.mutable_token_instruction()->set_token_instruction_type(type); |
29 directive.mutable_token_instruction()->set_token_id("token"); | 33 directive.mutable_token_instruction()->set_token_id("token"); |
30 directive.mutable_token_instruction()->set_medium(audible ? | 34 directive.mutable_token_instruction()->set_medium(audible ? |
31 AUDIO_AUDIBLE_DTMF : AUDIO_ULTRASOUND_PASSBAND); | 35 AUDIO_AUDIBLE_DTMF : AUDIO_ULTRASOUND_PASSBAND); |
32 directive.set_ttl_millis(ttl); | 36 directive.set_ttl_millis(ttl); |
33 return directive; | 37 return directive; |
34 } | 38 } |
35 | 39 |
36 } // namespace | 40 } // namespace |
37 | 41 |
38 class AudioManagerStub final : public AudioManager { | 42 class StubModem final : public audio_modem::Modem { |
39 public: | 43 public: |
40 AudioManagerStub() {} | 44 StubModem() {} |
41 ~AudioManagerStub() override {} | 45 ~StubModem() override {} |
42 | 46 |
43 // AudioManager overrides: | 47 // AudioManager overrides: |
44 void Initialize(WhispernetClient* whispernet_client, | 48 void Initialize(audio_modem::WhispernetClient* whispernet_client, |
45 const TokensCallback& tokens_cb) override {} | 49 const audio_modem::TokensCallback& tokens_cb) override {} |
46 void StartPlaying(AudioType type) override { playing_[type] = true; } | 50 void StartPlaying(AudioType type) override { playing_[type] = true; } |
47 void StopPlaying(AudioType type) override { playing_[type] = false; } | 51 void StopPlaying(AudioType type) override { playing_[type] = false; } |
48 void StartRecording(AudioType type) override { recording_[type] = true; } | 52 void StartRecording(AudioType type) override { recording_[type] = true; } |
49 void StopRecording(AudioType type) override { recording_[type] = false; } | 53 void StopRecording(AudioType type) override { recording_[type] = false; } |
50 void SetToken(AudioType type, const std::string& url_unsafe_token) override {} | 54 void SetToken(AudioType type, const std::string& url_unsafe_token) override {} |
51 const std::string GetToken(AudioType type) override { return std::string(); } | 55 const std::string GetToken(AudioType type) override { return std::string(); } |
52 bool IsPlayingTokenHeard(AudioType type) override { return false; } | 56 bool IsPlayingTokenHeard(AudioType type) override { return false; } |
53 void SetTokenLength(AudioType type, size_t token_length) override {} | 57 void SetTokenLength(AudioType type, size_t token_length) override {} |
54 | 58 |
55 bool IsRecording(AudioType type) { return recording_[type]; } | 59 bool IsRecording(AudioType type) { return recording_[type]; } |
56 bool IsPlaying(AudioType type) { return playing_[type]; } | 60 bool IsPlaying(AudioType type) { return playing_[type]; } |
57 | 61 |
58 private: | 62 private: |
59 // Indexed using enum AudioType. | 63 // Indexed using enum AudioType. |
60 bool playing_[2]; | 64 bool playing_[2]; |
61 bool recording_[2]; | 65 bool recording_[2]; |
62 | 66 |
63 DISALLOW_COPY_AND_ASSIGN(AudioManagerStub); | 67 DISALLOW_COPY_AND_ASSIGN(StubModem); |
64 }; | 68 }; |
65 | 69 |
66 class AudioDirectiveHandlerTest : public testing::Test { | 70 class AudioDirectiveHandlerTest : public testing::Test { |
67 public: | 71 public: |
68 AudioDirectiveHandlerTest() { | 72 AudioDirectiveHandlerTest() { |
69 manager_ptr_ = new AudioManagerStub; | 73 modem_ptr_ = new StubModem; |
70 timer_ptr_ = new base::MockTimer(false, false); | 74 timer_ptr_ = new base::MockTimer(false, false); |
71 clock_ptr_ = new base::SimpleTestTickClock; | 75 clock_ptr_ = new base::SimpleTestTickClock; |
72 | 76 |
73 directive_handler_.reset(new AudioDirectiveHandlerImpl( | 77 directive_handler_.reset(new AudioDirectiveHandlerImpl( |
74 base::Bind(&AudioDirectiveHandlerTest::GetDirectiveUpdates, | 78 base::Bind(&AudioDirectiveHandlerTest::GetDirectiveUpdates, |
75 base::Unretained(this)), | 79 base::Unretained(this)), |
76 make_scoped_ptr<AudioManager>(manager_ptr_), | 80 make_scoped_ptr<audio_modem::Modem>(modem_ptr_), |
77 make_scoped_ptr<base::Timer>(timer_ptr_), | 81 make_scoped_ptr<base::Timer>(timer_ptr_), |
78 make_scoped_refptr(new TickClockRefCounted(clock_ptr_)))); | 82 make_scoped_refptr(new TickClockRefCounted(clock_ptr_)))); |
79 directive_handler_->Initialize(nullptr, TokensCallback()); | 83 directive_handler_->Initialize(nullptr, audio_modem::TokensCallback()); |
80 } | 84 } |
81 ~AudioDirectiveHandlerTest() override {} | 85 ~AudioDirectiveHandlerTest() override {} |
82 | 86 |
83 protected: | 87 protected: |
84 const std::vector<Directive>& current_directives() { | 88 const std::vector<Directive>& current_directives() { |
85 return current_directives_; | 89 return current_directives_; |
86 } | 90 } |
87 | 91 |
88 bool IsPlaying(AudioType type) { return manager_ptr_->IsPlaying(type); } | 92 bool IsPlaying(AudioType type) { return modem_ptr_->IsPlaying(type); } |
89 | 93 |
90 bool IsRecording(AudioType type) { return manager_ptr_->IsRecording(type); } | 94 bool IsRecording(AudioType type) { return modem_ptr_->IsRecording(type); } |
91 | 95 |
92 // 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 |
93 // our the audio directive handler since the directive list ctor (invoked | 97 // our the audio directive handler since the directive list ctor (invoked |
94 // from the directive handler ctor) will post tasks. | 98 // from the directive handler ctor) will post tasks. |
95 base::MessageLoop message_loop_; | 99 base::MessageLoop message_loop_; |
96 scoped_ptr<AudioDirectiveHandler> directive_handler_; | 100 scoped_ptr<AudioDirectiveHandler> directive_handler_; |
97 | 101 |
98 std::vector<Directive> current_directives_; | 102 std::vector<Directive> current_directives_; |
99 | 103 |
100 // Unowned. | 104 // Unowned. |
101 AudioManagerStub* manager_ptr_; | 105 StubModem* modem_ptr_; |
102 base::MockTimer* timer_ptr_; | 106 base::MockTimer* timer_ptr_; |
103 base::SimpleTestTickClock* clock_ptr_; | 107 base::SimpleTestTickClock* clock_ptr_; |
104 | 108 |
105 private: | 109 private: |
106 void GetDirectiveUpdates(const std::vector<Directive>& current_directives) { | 110 void GetDirectiveUpdates(const std::vector<Directive>& current_directives) { |
107 current_directives_ = current_directives; | 111 current_directives_ = current_directives; |
108 } | 112 } |
109 | 113 |
110 DISALLOW_COPY_AND_ASSIGN(AudioDirectiveHandlerTest); | 114 DISALLOW_COPY_AND_ASSIGN(AudioDirectiveHandlerTest); |
111 }; | 115 }; |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
188 // Advance to +11ms. | 192 // Advance to +11ms. |
189 clock_ptr_->Advance(twoMs); | 193 clock_ptr_->Advance(twoMs); |
190 timer_ptr_->Fire(); | 194 timer_ptr_->Fire(); |
191 EXPECT_FALSE(IsRecording(AUDIBLE)); | 195 EXPECT_FALSE(IsRecording(AUDIBLE)); |
192 } | 196 } |
193 | 197 |
194 // TODO(rkc): Write more tests that check more convoluted sequences of | 198 // TODO(rkc): Write more tests that check more convoluted sequences of |
195 // transmits/receives. | 199 // transmits/receives. |
196 | 200 |
197 } // namespace copresence | 201 } // namespace copresence |
OLD | NEW |