1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "components/audio_modem/public/modem.h"
10 #include "base/message_loop/message_loop.h"
11 #include "components/audio_modem/audio_player.h"
12 #include "components/audio_modem/audio_recorder.h"
13 #include "components/audio_modem/modem_impl.h"
14 #include "components/audio_modem/test/random_samples.h"
15 #include "components/audio_modem/test/stub_whispernet_client.h"
16 #include "media/base/audio_bus.h"
17 #include "testing/gtest/include/gtest/gtest.h"
19 namespace audio_modem
{
21 class AudioPlayerStub final
: public AudioPlayer
{
23 AudioPlayerStub() : is_playing_(false) {}
24 ~AudioPlayerStub() override
{}
26 // AudioPlayer overrides:
27 void Initialize() override
{}
28 void Play(const scoped_refptr
<media::AudioBusRefCounted
>&) override
{
31 void Stop() override
{ is_playing_
= false; }
32 void Finalize() override
{ delete this; }
34 bool IsPlaying() { return is_playing_
; }
38 DISALLOW_COPY_AND_ASSIGN(AudioPlayerStub
);
41 class AudioRecorderStub final
: public AudioRecorder
{
43 AudioRecorderStub() : is_recording_(false) {}
44 ~AudioRecorderStub() override
{}
46 // AudioRecorder overrides:
47 void Initialize(const RecordedSamplesCallback
& cb
) override
{ cb_
= cb
; }
48 void Record() override
{ is_recording_
= true; }
49 void Stop() override
{ is_recording_
= false; }
50 void Finalize() override
{ delete this; }
52 bool IsRecording() { return is_recording_
; }
54 void TriggerDecodeRequest() {
56 cb_
.Run(std::string(0x1337, 'a'));
60 RecordedSamplesCallback cb_
;
63 DISALLOW_COPY_AND_ASSIGN(AudioRecorderStub
);
66 class ModemTest
: public testing::Test
{
69 : modem_(new ModemImpl
),
70 audible_player_(new AudioPlayerStub
),
71 inaudible_player_(new AudioPlayerStub
),
72 recorder_(new AudioRecorderStub
),
73 last_received_decode_type_(AUDIO_TYPE_UNKNOWN
) {
74 std::vector
<AudioToken
> tokens
;
75 tokens
.push_back(AudioToken("abcdef", true));
76 tokens
.push_back(AudioToken("123456", false));
77 client_
.reset(new StubWhispernetClient(
78 CreateRandomAudioRefCounted(0x123, 1, 0x321), tokens
));
80 // TODO(ckehoe): Pass these into the Modem constructor instead.
81 modem_
->set_player_for_testing(AUDIBLE
, audible_player_
);
82 modem_
->set_player_for_testing(INAUDIBLE
, inaudible_player_
);
83 modem_
->set_recorder_for_testing(recorder_
);
86 base::Bind(&ModemTest::GetTokens
, base::Unretained(this)));
89 ~ModemTest() override
{}
92 void GetTokens(const std::vector
<AudioToken
>& tokens
) {
93 last_received_decode_type_
= AUDIO_TYPE_UNKNOWN
;
94 for (const auto& token
: tokens
) {
95 if (token
.audible
&& last_received_decode_type_
== INAUDIBLE
) {
96 last_received_decode_type_
= BOTH
;
97 } else if (!token
.audible
&& last_received_decode_type_
== AUDIBLE
) {
98 last_received_decode_type_
= BOTH
;
99 } else if (token
.audible
) {
100 last_received_decode_type_
= AUDIBLE
;
102 last_received_decode_type_
= INAUDIBLE
;
107 base::MessageLoop message_loop_
;
108 // This order is important. The WhispernetClient needs to outlive the Modem.
109 scoped_ptr
<WhispernetClient
> client_
;
110 scoped_ptr
<ModemImpl
> modem_
;
112 // These will be deleted by the Modem's destructor calling finalize on them.
113 AudioPlayerStub
* audible_player_
;
114 AudioPlayerStub
* inaudible_player_
;
115 AudioRecorderStub
* recorder_
;
117 AudioType last_received_decode_type_
;
120 DISALLOW_COPY_AND_ASSIGN(ModemTest
);
123 TEST_F(ModemTest
, EncodeToken
) {
124 modem_
->StartPlaying(AUDIBLE
);
125 // No token yet, player shouldn't be playing.
126 EXPECT_FALSE(audible_player_
->IsPlaying());
128 modem_
->SetToken(INAUDIBLE
, "abcd");
129 // No *audible* token yet, so player still shouldn't be playing.
130 EXPECT_FALSE(audible_player_
->IsPlaying());
132 modem_
->SetToken(AUDIBLE
, "abcd");
133 EXPECT_TRUE(audible_player_
->IsPlaying());
136 TEST_F(ModemTest
, Record
) {
137 recorder_
->TriggerDecodeRequest();
138 EXPECT_EQ(AUDIO_TYPE_UNKNOWN
, last_received_decode_type_
);
140 modem_
->StartRecording(AUDIBLE
);
141 recorder_
->TriggerDecodeRequest();
142 EXPECT_EQ(AUDIBLE
, last_received_decode_type_
);
144 modem_
->StartRecording(INAUDIBLE
);
145 recorder_
->TriggerDecodeRequest();
146 EXPECT_EQ(BOTH
, last_received_decode_type_
);
148 modem_
->StopRecording(AUDIBLE
);
149 recorder_
->TriggerDecodeRequest();
150 EXPECT_EQ(INAUDIBLE
, last_received_decode_type_
);
153 } // namespace audio_modem