summaryrefslogtreecommitdiffstats
path: root/components/audio_modem/modem_unittest.cc
blob: e82c097f727a5efa2c7a42d9a95925a3231671fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/audio_modem/public/modem.h"

#include <vector>

#include "base/bind.h"
#include "base/macros.h"
#include "base/message_loop/message_loop.h"
#include "components/audio_modem/audio_player.h"
#include "components/audio_modem/audio_recorder.h"
#include "components/audio_modem/modem_impl.h"
#include "components/audio_modem/test/random_samples.h"
#include "components/audio_modem/test/stub_whispernet_client.h"
#include "media/base/audio_bus.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace audio_modem {

class AudioPlayerStub final : public AudioPlayer {
 public:
  AudioPlayerStub() : is_playing_(false) {}
  ~AudioPlayerStub() override {}

  // AudioPlayer overrides:
  void Initialize() override {}
  void Play(const scoped_refptr<media::AudioBusRefCounted>&) override {
    is_playing_ = true;
  }
  void Stop() override { is_playing_ = false; }
  void Finalize() override { delete this; }

  bool IsPlaying() { return is_playing_; }

 private:
  bool is_playing_;
  DISALLOW_COPY_AND_ASSIGN(AudioPlayerStub);
};

class AudioRecorderStub final : public AudioRecorder {
 public:
  AudioRecorderStub() : is_recording_(false) {}
  ~AudioRecorderStub() override {}

  // AudioRecorder overrides:
  void Initialize(const RecordedSamplesCallback& cb) override { cb_ = cb; }
  void Record() override { is_recording_ = true; }
  void Stop() override { is_recording_ = false; }
  void Finalize() override { delete this; }

  bool IsRecording() { return is_recording_; }

  void TriggerDecodeRequest() {
    if (!cb_.is_null())
      cb_.Run(std::string(0x1337, 'a'));
  }

 private:
  RecordedSamplesCallback cb_;
  bool is_recording_;

  DISALLOW_COPY_AND_ASSIGN(AudioRecorderStub);
};

class ModemTest : public testing::Test {
 public:
  ModemTest()
      : modem_(new ModemImpl),
        audible_player_(new AudioPlayerStub),
        inaudible_player_(new AudioPlayerStub),
        recorder_(new AudioRecorderStub),
        last_received_decode_type_(AUDIO_TYPE_UNKNOWN) {
    std::vector<AudioToken> tokens;
    tokens.push_back(AudioToken("abcdef", true));
    tokens.push_back(AudioToken("123456", false));
    client_.reset(new StubWhispernetClient(
        CreateRandomAudioRefCounted(0x123, 1, 0x321), tokens));

    // TODO(ckehoe): Pass these into the Modem constructor instead.
    modem_->set_player_for_testing(AUDIBLE, audible_player_);
    modem_->set_player_for_testing(INAUDIBLE, inaudible_player_);
    modem_->set_recorder_for_testing(recorder_);
    modem_->Initialize(
        client_.get(),
        base::Bind(&ModemTest::GetTokens, base::Unretained(this)));
  }

  ~ModemTest() override {}

 protected:
  void GetTokens(const std::vector<AudioToken>& tokens) {
    last_received_decode_type_ = AUDIO_TYPE_UNKNOWN;
    for (const auto& token : tokens) {
      if (token.audible && last_received_decode_type_ == INAUDIBLE) {
        last_received_decode_type_ = BOTH;
      } else if (!token.audible && last_received_decode_type_ == AUDIBLE) {
        last_received_decode_type_ = BOTH;
      } else if (token.audible) {
        last_received_decode_type_ = AUDIBLE;
      } else {
        last_received_decode_type_ = INAUDIBLE;
      }
    }
  }

  base::MessageLoop message_loop_;
  // This order is important. The WhispernetClient needs to outlive the Modem.
  scoped_ptr<WhispernetClient> client_;
  scoped_ptr<ModemImpl> modem_;

  // These will be deleted by the Modem's destructor calling finalize on them.
  AudioPlayerStub* audible_player_;
  AudioPlayerStub* inaudible_player_;
  AudioRecorderStub* recorder_;

  AudioType last_received_decode_type_;

 private:
  DISALLOW_COPY_AND_ASSIGN(ModemTest);
};

TEST_F(ModemTest, EncodeToken) {
  modem_->StartPlaying(AUDIBLE);
  // No token yet, player shouldn't be playing.
  EXPECT_FALSE(audible_player_->IsPlaying());

  modem_->SetToken(INAUDIBLE, "abcd");
  // No *audible* token yet, so player still shouldn't be playing.
  EXPECT_FALSE(audible_player_->IsPlaying());

  modem_->SetToken(AUDIBLE, "abcd");
  EXPECT_TRUE(audible_player_->IsPlaying());
}

TEST_F(ModemTest, Record) {
  recorder_->TriggerDecodeRequest();
  EXPECT_EQ(AUDIO_TYPE_UNKNOWN, last_received_decode_type_);

  modem_->StartRecording(AUDIBLE);
  recorder_->TriggerDecodeRequest();
  EXPECT_EQ(AUDIBLE, last_received_decode_type_);

  modem_->StartRecording(INAUDIBLE);
  recorder_->TriggerDecodeRequest();
  EXPECT_EQ(BOTH, last_received_decode_type_);

  modem_->StopRecording(AUDIBLE);
  recorder_->TriggerDecodeRequest();
  EXPECT_EQ(INAUDIBLE, last_received_decode_type_);
}

}  // namespace audio_modem