Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(128)

Side by Side Diff: components/copresence/mediums/audio/audio_manager_unittest.cc

Issue 637223011: Redesign the copresence audio handlers. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git/+/master
Patch Set: Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "components/copresence/mediums/audio/audio_manager.h"
6
7 #include "base/bind.h"
8 #include "base/message_loop/message_loop.h"
9 #include "components/copresence/mediums/audio/audio_manager_impl.h"
10 #include "components/copresence/mediums/audio/audio_player.h"
11 #include "components/copresence/mediums/audio/audio_recorder.h"
12 #include "components/copresence/test/audio_test_support.h"
13 #include "media/base/audio_bus.h"
14 //#include "testing/gmock/include/gmock/gmock.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16
17 namespace copresence {
18
19 class AudioPlayerStub final : public AudioPlayer {
20 public:
21 AudioPlayerStub() : is_playing_(false) {}
22 virtual ~AudioPlayerStub() {}
23
24 // AudioPlayer overrides:
25 void Initialize() override {}
26 void Play(const scoped_refptr<media::AudioBusRefCounted>&) override {
27 is_playing_ = true;
28 }
29 void Stop() override { is_playing_ = false; }
30 void Finalize() override { delete this; }
31 bool IsPlaying() override { return is_playing_; }
32
33 private:
34 bool is_playing_;
35 DISALLOW_COPY_AND_ASSIGN(AudioPlayerStub);
36 };
37
38 class AudioRecorderStub final : public AudioRecorder {
39 public:
40 AudioRecorderStub() : is_recording_(false) {}
41 virtual ~AudioRecorderStub() {}
42
43 // AudioRecorder overrides:
44 void Initialize(const RecordedSamplesCallback& cb) override { cb_ = cb; }
45 void Record() override { is_recording_ = true; }
46 void Stop() override { is_recording_ = false; }
47 void Finalize() override { delete this; }
48 bool IsRecording() override { return is_recording_; }
49
50 void TriggerDecodeRequest() {
51 if (!cb_.is_null())
52 cb_.Run(std::string(0x1337, 'a'));
53 }
54
55 private:
56 RecordedSamplesCallback cb_;
57 bool is_recording_;
58
59 DISALLOW_COPY_AND_ASSIGN(AudioRecorderStub);
60 };
61
62 class AudioManagerTest : public testing::Test {
63 public:
64 AudioManagerTest()
65 : audio_manager_(new AudioManagerImpl()),
66 audible_player_(new AudioPlayerStub),
67 inaudible_player_(new AudioPlayerStub),
68 recorder_(new AudioRecorderStub),
69 last_received_decode_type_(AUDIO_TYPE_UNKNOWN) {
70 audio_manager_->set_player_for_testing(AUDIBLE, audible_player_);
71 audio_manager_->set_player_for_testing(INAUDIBLE, inaudible_player_);
72 audio_manager_->set_recorder_for_testing(recorder_);
73 audio_manager_->Initialize(
74 base::Bind(&AudioManagerTest::DecodeSamples, base::Unretained(this)),
75 base::Bind(&AudioManagerTest::EncodeToken, base::Unretained(this)));
76 }
77 virtual ~AudioManagerTest() {}
78
79 protected:
80 void EncodeToken(const std::string& token,
81 AudioType audible,
82 const AudioManager::SamplesCallback& callback) {
83 callback.Run(
84 token, audible, CreateRandomAudioRefCounted(0x1337, 1, 0x7331));
85 }
86
87 void DecodeSamples(AudioType type, const std::string& /* samples */) {
88 last_received_decode_type_ = type;
89 }
90
91 base::MessageLoop message_loop_;
92 scoped_ptr<AudioManagerImpl> audio_manager_;
93
94 // These will be deleted by |audio_manager_|'s dtor calling finalize on them.
95 AudioPlayerStub* audible_player_;
96 AudioPlayerStub* inaudible_player_;
97 AudioRecorderStub* recorder_;
98
99 AudioType last_received_decode_type_;
100
101 private:
102 DISALLOW_COPY_AND_ASSIGN(AudioManagerTest);
103 };
104
105 TEST_F(AudioManagerTest, Basic) {
106 audio_manager_->StartPlaying(AUDIBLE);
107 EXPECT_TRUE(audio_manager_->IsPlaying(AUDIBLE));
108 EXPECT_FALSE(audio_manager_->IsPlaying(INAUDIBLE));
109
110 audio_manager_->StopPlaying(AUDIBLE);
111 EXPECT_FALSE(audio_manager_->IsPlaying(AUDIBLE));
112
113 audio_manager_->StartRecording(INAUDIBLE);
114 EXPECT_TRUE(audio_manager_->IsRecording(INAUDIBLE));
115 EXPECT_FALSE(audio_manager_->IsRecording(AUDIBLE));
116
117 audio_manager_->StopRecording(INAUDIBLE);
118 EXPECT_FALSE(audio_manager_->IsRecording(INAUDIBLE));
119 }
120
121 TEST_F(AudioManagerTest, EncodeToken) {
122 audio_manager_->StartPlaying(AUDIBLE);
123 // No token yet, player shouldn't be playing.
124 EXPECT_FALSE(audible_player_->IsPlaying());
125
126 audio_manager_->SetToken(INAUDIBLE, "abcd");
127 // No *audible* token yet, so player still shouldn't be playing.
128 EXPECT_FALSE(audible_player_->IsPlaying());
129
130 audio_manager_->SetToken(AUDIBLE, "abcd");
131 EXPECT_TRUE(audible_player_->IsPlaying());
132 }
133
134 TEST_F(AudioManagerTest, Record) {
135 recorder_->TriggerDecodeRequest();
136 EXPECT_EQ(AUDIO_TYPE_UNKNOWN, last_received_decode_type_);
137
138 audio_manager_->StartRecording(AUDIBLE);
139 recorder_->TriggerDecodeRequest();
140 EXPECT_EQ(AUDIBLE, last_received_decode_type_);
141
142 audio_manager_->StartRecording(INAUDIBLE);
143 recorder_->TriggerDecodeRequest();
144 EXPECT_EQ(BOTH, last_received_decode_type_);
145
146 audio_manager_->StopRecording(AUDIBLE);
147 recorder_->TriggerDecodeRequest();
148 EXPECT_EQ(INAUDIBLE, last_received_decode_type_);
149 }
150
151 } // namespace copresence
OLDNEW
« no previous file with comments | « components/copresence/mediums/audio/audio_manager_impl.cc ('k') | components/copresence/mediums/audio/audio_player.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698