blob: 042b9787570f478984e8df5530eabd543b4864d9 [file] [log] [blame]
ckehoea9408e142015-02-17 20:05:241// Copyright 2015 The Chromium Authors. All rights reserved.
rkca05223cb2014-10-23 02:39:032// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
ckehoea9408e142015-02-17 20:05:245#include "components/audio_modem/public/modem.h"
rkca05223cb2014-10-23 02:39:036
ckehoe88444c0b2015-02-05 03:26:597#include <vector>
8
rkca05223cb2014-10-23 02:39:039#include "base/bind.h"
10#include "base/message_loop/message_loop.h"
ckehoea9408e142015-02-17 20:05:2411#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"
rkca05223cb2014-10-23 02:39:0316#include "media/base/audio_bus.h"
rkca05223cb2014-10-23 02:39:0317#include "testing/gtest/include/gtest/gtest.h"
18
ckehoea9408e142015-02-17 20:05:2419namespace audio_modem {
rkca05223cb2014-10-23 02:39:0320
21class AudioPlayerStub final : public AudioPlayer {
22 public:
23 AudioPlayerStub() : is_playing_(false) {}
dcheng30a1b1542014-10-29 21:27:5024 ~AudioPlayerStub() override {}
rkca05223cb2014-10-23 02:39:0325
26 // AudioPlayer overrides:
27 void Initialize() override {}
28 void Play(const scoped_refptr<media::AudioBusRefCounted>&) override {
29 is_playing_ = true;
30 }
31 void Stop() override { is_playing_ = false; }
32 void Finalize() override { delete this; }
rkcb4437e92014-12-18 18:47:3033
34 bool IsPlaying() { return is_playing_; }
rkca05223cb2014-10-23 02:39:0335
36 private:
37 bool is_playing_;
38 DISALLOW_COPY_AND_ASSIGN(AudioPlayerStub);
39};
40
41class AudioRecorderStub final : public AudioRecorder {
42 public:
43 AudioRecorderStub() : is_recording_(false) {}
dcheng30a1b1542014-10-29 21:27:5044 ~AudioRecorderStub() override {}
rkca05223cb2014-10-23 02:39:0345
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; }
rkcb4437e92014-12-18 18:47:3051
52 bool IsRecording() { return is_recording_; }
rkca05223cb2014-10-23 02:39:0353
54 void TriggerDecodeRequest() {
55 if (!cb_.is_null())
56 cb_.Run(std::string(0x1337, 'a'));
57 }
58
59 private:
60 RecordedSamplesCallback cb_;
61 bool is_recording_;
62
63 DISALLOW_COPY_AND_ASSIGN(AudioRecorderStub);
64};
65
ckehoea9408e142015-02-17 20:05:2466class ModemTest : public testing::Test {
rkca05223cb2014-10-23 02:39:0367 public:
ckehoea9408e142015-02-17 20:05:2468 ModemTest()
69 : modem_(new ModemImpl),
rkca05223cb2014-10-23 02:39:0370 audible_player_(new AudioPlayerStub),
71 inaudible_player_(new AudioPlayerStub),
72 recorder_(new AudioRecorderStub),
73 last_received_decode_type_(AUDIO_TYPE_UNKNOWN) {
ckehoe88444c0b2015-02-05 03:26:5974 std::vector<AudioToken> tokens;
75 tokens.push_back(AudioToken("abcdef", true));
76 tokens.push_back(AudioToken("123456", false));
ckehoea9408e142015-02-17 20:05:2477 client_.reset(new StubWhispernetClient(
ckehoe88444c0b2015-02-05 03:26:5978 CreateRandomAudioRefCounted(0x123, 1, 0x321), tokens));
79
ckehoea9408e142015-02-17 20:05:2480 // 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_);
84 modem_->Initialize(
85 client_.get(),
86 base::Bind(&ModemTest::GetTokens, base::Unretained(this)));
rkca05223cb2014-10-23 02:39:0387 }
rkcab25c6e72014-11-06 22:59:2688
ckehoea9408e142015-02-17 20:05:2489 ~ModemTest() override {}
rkca05223cb2014-10-23 02:39:0390
91 protected:
rkcab25c6e72014-11-06 22:59:2692 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;
101 } else {
102 last_received_decode_type_ = INAUDIBLE;
103 }
104 }
rkca05223cb2014-10-23 02:39:03105 }
106
107 base::MessageLoop message_loop_;
ckehoea9408e142015-02-17 20:05:24108 // This order is important. The WhispernetClient needs to outlive the Modem.
109 scoped_ptr<WhispernetClient> client_;
110 scoped_ptr<ModemImpl> modem_;
rkca05223cb2014-10-23 02:39:03111
ckehoea9408e142015-02-17 20:05:24112 // These will be deleted by the Modem's destructor calling finalize on them.
rkca05223cb2014-10-23 02:39:03113 AudioPlayerStub* audible_player_;
114 AudioPlayerStub* inaudible_player_;
115 AudioRecorderStub* recorder_;
116
117 AudioType last_received_decode_type_;
118
119 private:
ckehoea9408e142015-02-17 20:05:24120 DISALLOW_COPY_AND_ASSIGN(ModemTest);
rkca05223cb2014-10-23 02:39:03121};
122
ckehoea9408e142015-02-17 20:05:24123TEST_F(ModemTest, EncodeToken) {
124 modem_->StartPlaying(AUDIBLE);
rkca05223cb2014-10-23 02:39:03125 // No token yet, player shouldn't be playing.
126 EXPECT_FALSE(audible_player_->IsPlaying());
127
ckehoea9408e142015-02-17 20:05:24128 modem_->SetToken(INAUDIBLE, "abcd");
rkca05223cb2014-10-23 02:39:03129 // No *audible* token yet, so player still shouldn't be playing.
130 EXPECT_FALSE(audible_player_->IsPlaying());
131
ckehoea9408e142015-02-17 20:05:24132 modem_->SetToken(AUDIBLE, "abcd");
rkca05223cb2014-10-23 02:39:03133 EXPECT_TRUE(audible_player_->IsPlaying());
134}
135
ckehoea9408e142015-02-17 20:05:24136TEST_F(ModemTest, Record) {
rkca05223cb2014-10-23 02:39:03137 recorder_->TriggerDecodeRequest();
138 EXPECT_EQ(AUDIO_TYPE_UNKNOWN, last_received_decode_type_);
139
ckehoea9408e142015-02-17 20:05:24140 modem_->StartRecording(AUDIBLE);
rkca05223cb2014-10-23 02:39:03141 recorder_->TriggerDecodeRequest();
142 EXPECT_EQ(AUDIBLE, last_received_decode_type_);
143
ckehoea9408e142015-02-17 20:05:24144 modem_->StartRecording(INAUDIBLE);
rkca05223cb2014-10-23 02:39:03145 recorder_->TriggerDecodeRequest();
146 EXPECT_EQ(BOTH, last_received_decode_type_);
147
ckehoea9408e142015-02-17 20:05:24148 modem_->StopRecording(AUDIBLE);
rkca05223cb2014-10-23 02:39:03149 recorder_->TriggerDecodeRequest();
150 EXPECT_EQ(INAUDIBLE, last_received_decode_type_);
151}
152
ckehoea9408e142015-02-17 20:05:24153} // namespace audio_modem