Revert of Two small bugfixes (patchset #3 id:60001 of https://codereview.chromium...
[chromium-blink-merge.git] / chrome / browser / copresence / chrome_whispernet_client_browsertest.cc
blob61d3b3cbd5e1af6e1587b31e7f26c4006c802219
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.
5 #include "chrome/browser/copresence/chrome_whispernet_client.h"
7 #include <cmath>
8 #include <cstdlib>
9 #include <string>
11 #include "base/bind.h"
12 #include "base/macros.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/run_loop.h"
15 #include "base/stl_util.h"
16 #include "chrome/browser/extensions/api/copresence/copresence_api.h"
17 #include "chrome/browser/extensions/extension_browsertest.h"
18 #include "chrome/browser/profiles/profile.h"
19 #include "chrome/browser/ui/browser.h"
20 #include "chrome/test/base/in_process_browser_test.h"
21 #include "components/audio_modem/public/audio_modem_types.h"
22 #include "components/audio_modem/public/whispernet_client.h"
23 #include "media/audio/audio_manager.h"
24 #include "media/audio/audio_manager_base.h"
25 #include "media/audio/audio_parameters.h"
26 #include "media/base/audio_bus.h"
27 #include "media/base/audio_converter.h"
29 using audio_modem::WhispernetClient;
30 using audio_modem::AUDIBLE;
31 using audio_modem::INAUDIBLE;
32 using audio_modem::TokenParameters;
34 namespace {
36 // TODO(rkc): Add more varied test input.
37 const char kSixZeros[] = "MDAwMDAw";
38 const char kEightZeros[] = "MDAwMDAwMDA";
39 const char kNineZeros[] = "MDAwMDAwMDAw";
41 const size_t kTokenLengths[2] = {6, 6};
43 // Copied from src/components/copresence/mediums/audio/audio_recorder.cc
44 std::string AudioBusToString(scoped_refptr<media::AudioBusRefCounted> source) {
45 std::string buffer;
46 buffer.resize(source->frames() * source->channels() * sizeof(float));
47 float* buffer_view = reinterpret_cast<float*>(string_as_array(&buffer));
49 const int channels = source->channels();
50 for (int ch = 0; ch < channels; ++ch) {
51 for (int si = 0, di = ch; si < source->frames(); ++si, di += channels)
52 buffer_view[di] = source->channel(ch)[si];
55 return buffer;
58 void GetTokenParamsForLengths(const size_t token_lengths[2],
59 TokenParameters* params) {
60 params[0].length = token_lengths[0];
61 params[1].length = token_lengths[1];
64 void IgnoreResult(bool success) {}
66 } // namespace
68 class ChromeWhispernetClientTest : public ExtensionBrowserTest,
69 public media::AudioConverter::InputCallback {
70 protected:
71 ChromeWhispernetClientTest()
72 : initialized_(false),
73 expected_audible_(false),
74 saved_samples_index_(0) {}
76 ~ChromeWhispernetClientTest() override {}
78 void InitializeWhispernet() {
79 scoped_ptr<WhispernetClient> client(
80 new ChromeWhispernetClient(browser()->profile()));
81 client->Initialize(base::Bind(
82 &ChromeWhispernetClientTest::InitCallback, base::Unretained(this)));
84 run_loop_.reset(new base::RunLoop());
85 run_loop_->Run();
86 EXPECT_TRUE(initialized_);
89 // This needs to be called before any of the decoder tests are run. We can't
90 // run this code in the constructor or the SetUp methods because the audio
91 // manager seems to get initialized only *after* ExtensionBrowserTest::SetUp
92 // has finished executing. Setting up a testing AudioMager causes the actual
93 // create happening later in the browser initialization to fail. The only way
94 // around this at the moment seems to be to have this method called from
95 // every test before they try to decode.
96 void SetupDecode() {
97 // We get default parameters here instead of the constructor since
98 // initializing Whispernet also creates our AudioManager. Initializing from
99 // the test instead causes issues.
100 default_params_ = media::AudioManager::Get()->GetInputStreamParameters(
101 media::AudioManagerBase::kDefaultDeviceId);
103 coder_params_ = media::AudioParameters(
104 default_params_.format(), audio_modem::kDefaultChannelLayout,
105 audio_modem::kDefaultSampleRate, audio_modem::kDefaultBitsPerSample,
106 default_params_.frames_per_buffer(),
107 media::AudioParameters::NO_EFFECTS);
109 converter_.reset(new media::AudioConverter(
110 coder_params_, default_params_,
111 default_params_.sample_rate() == coder_params_.sample_rate()));
112 converter_->AddInput(this);
115 void EncodeTokenAndSaveSamples(WhispernetClient* client,
116 bool audible,
117 const std::string& token) {
118 run_loop_.reset(new base::RunLoop());
119 client->RegisterSamplesCallback(
120 base::Bind(&ChromeWhispernetClientTest::SamplesCallback,
121 base::Unretained(this)));
122 expected_token_ = token;
123 expected_audible_ = audible;
125 TokenParameters token_params[2];
126 client->EncodeToken(token, audible ? AUDIBLE : INAUDIBLE, token_params);
127 run_loop_->Run();
129 EXPECT_GT(saved_samples_->frames(), 0);
132 void DecodeSamplesAndVerifyToken(WhispernetClient* client,
133 bool expect_audible,
134 const std::string& expected_token,
135 const TokenParameters token_params[2]) {
136 run_loop_.reset(new base::RunLoop());
137 client->RegisterTokensCallback(base::Bind(
138 &ChromeWhispernetClientTest::TokensCallback, base::Unretained(this)));
139 expected_token_ = expected_token;
140 expected_audible_ = expect_audible;
142 ASSERT_GT(saved_samples_->frames(), 0);
144 scoped_refptr<media::AudioBusRefCounted> samples_bus =
145 ConvertSavedSamplesToSystemParams();
146 client->DecodeSamples(expect_audible ? AUDIBLE : INAUDIBLE,
147 AudioBusToString(samples_bus), token_params);
148 run_loop_->Run();
151 void InitCallback(bool success) {
152 EXPECT_TRUE(success);
153 initialized_ = true;
154 ASSERT_TRUE(run_loop_);
155 run_loop_->Quit();
158 void SamplesCallback(
159 audio_modem::AudioType type,
160 const std::string& token,
161 const scoped_refptr<media::AudioBusRefCounted>& samples) {
162 EXPECT_EQ(expected_token_, token);
163 EXPECT_EQ(expected_audible_, type == AUDIBLE);
164 saved_samples_ = samples;
165 ASSERT_TRUE(run_loop_);
166 run_loop_->Quit();
169 void TokensCallback(const std::vector<audio_modem::AudioToken>& tokens) {
170 ASSERT_TRUE(run_loop_);
171 run_loop_->Quit();
173 EXPECT_EQ(expected_token_, tokens[0].token);
174 EXPECT_EQ(expected_audible_, tokens[0].audible);
177 private:
178 scoped_refptr<media::AudioBusRefCounted> ConvertSavedSamplesToSystemParams() {
179 int new_size =
180 saved_samples_->frames() *
181 std::ceil(static_cast<double>(default_params_.sample_rate()) /
182 coder_params_.sample_rate());
183 new_size =
184 std::ceil(static_cast<double>(new_size) / converter_->ChunkSize()) *
185 converter_->ChunkSize();
187 scoped_refptr<media::AudioBusRefCounted> converted_samples =
188 media::AudioBusRefCounted::Create(default_params_.channels(), new_size);
190 // Convert our single channel samples to two channel. Decode samples
191 // expects 2 channel data.
192 saved_samples_stereo_ =
193 media::AudioBusRefCounted::Create(2, saved_samples_->frames());
194 memcpy(saved_samples_stereo_->channel(0), saved_samples_->channel(0),
195 sizeof(float) * saved_samples_->frames());
196 memcpy(saved_samples_stereo_->channel(1), saved_samples_->channel(0),
197 sizeof(float) * saved_samples_->frames());
199 saved_samples_index_ = 0;
200 converter_->Convert(converted_samples.get());
202 return converted_samples;
205 // AudioConverter::InputCallback overrides:
206 double ProvideInput(media::AudioBus* dest,
207 base::TimeDelta /* buffer_delay */) override {
208 int remaining_frames = saved_samples_->frames() - saved_samples_index_;
209 int frames_to_copy = std::min(remaining_frames, dest->frames());
210 saved_samples_stereo_->CopyPartialFramesTo(saved_samples_index_,
211 frames_to_copy, 0, dest);
212 saved_samples_index_ += frames_to_copy;
213 return 1.0;
216 scoped_ptr<base::RunLoop> run_loop_;
217 bool initialized_;
219 std::string expected_token_;
220 bool expected_audible_;
222 scoped_refptr<media::AudioBusRefCounted> saved_samples_;
223 scoped_refptr<media::AudioBusRefCounted> saved_samples_stereo_;
224 int saved_samples_index_;
226 scoped_ptr<media::AudioConverter> converter_;
228 media::AudioParameters default_params_;
229 media::AudioParameters coder_params_;
231 DISALLOW_COPY_AND_ASSIGN(ChromeWhispernetClientTest);
234 // These tests are irrelevant if NACL is disabled. See crbug.com/449198
235 #if defined(DISABLE_NACL)
236 #define MAYBE_Initialize DISABLED_Initialize
237 #define MAYBE_EncodeAndDecode DISABLED_EncodeAndDecode
238 #define MAYBE_TokenLengths DISABLED_TokenLengths
239 #define MAYBE_MultipleClients DISABLED_MultipleClients
240 #else
241 #define MAYBE_Initialize Initialize
242 #define MAYBE_EncodeAndDecode EncodeAndDecode
243 #define MAYBE_TokenLengths TokenLengths
244 #define MAYBE_MultipleClients MultipleClients
245 #endif
247 IN_PROC_BROWSER_TEST_F(ChromeWhispernetClientTest, MAYBE_Initialize) {
248 InitializeWhispernet();
251 IN_PROC_BROWSER_TEST_F(ChromeWhispernetClientTest, MAYBE_EncodeAndDecode) {
252 scoped_ptr<WhispernetClient> client(
253 new ChromeWhispernetClient(browser()->profile()));
254 client->Initialize(base::Bind(&IgnoreResult));
255 SetupDecode();
257 TokenParameters token_params[2];
258 GetTokenParamsForLengths(kTokenLengths, token_params);
260 EncodeTokenAndSaveSamples(client.get(), true, kSixZeros);
261 DecodeSamplesAndVerifyToken(client.get(), true, kSixZeros, token_params);
263 EncodeTokenAndSaveSamples(client.get(), false, kSixZeros);
264 DecodeSamplesAndVerifyToken(client.get(), false, kSixZeros, token_params);
267 IN_PROC_BROWSER_TEST_F(ChromeWhispernetClientTest, MAYBE_TokenLengths) {
268 scoped_ptr<WhispernetClient> client(
269 new ChromeWhispernetClient(browser()->profile()));
270 client->Initialize(base::Bind(&IgnoreResult));
271 SetupDecode();
273 const size_t kLongTokenLengths[2] = {8, 9};
274 TokenParameters token_params[2];
275 GetTokenParamsForLengths(kLongTokenLengths, token_params);
277 EncodeTokenAndSaveSamples(client.get(), true, kEightZeros);
278 DecodeSamplesAndVerifyToken(client.get(), true, kEightZeros, token_params);
280 EncodeTokenAndSaveSamples(client.get(), false, kNineZeros);
281 DecodeSamplesAndVerifyToken(client.get(), false, kNineZeros, token_params);
284 IN_PROC_BROWSER_TEST_F(ChromeWhispernetClientTest, MAYBE_MultipleClients) {
285 scoped_ptr<WhispernetClient> client_1(
286 new ChromeWhispernetClient(browser()->profile()));
287 scoped_ptr<WhispernetClient> client_2(
288 new ChromeWhispernetClient(browser()->profile()));
289 scoped_ptr<WhispernetClient> client_3(
290 new ChromeWhispernetClient(browser()->profile()));
291 SetupDecode();
293 TokenParameters token_params[2];
294 GetTokenParamsForLengths(kTokenLengths, token_params);
296 // Test concurrent initialization.
297 client_1->Initialize(base::Bind(&IgnoreResult));
298 client_2->Initialize(base::Bind(&IgnoreResult));
300 EncodeTokenAndSaveSamples(client_1.get(), true, kSixZeros);
301 DecodeSamplesAndVerifyToken(client_1.get(), true, kSixZeros, token_params);
303 EncodeTokenAndSaveSamples(client_2.get(), false, kSixZeros);
304 DecodeSamplesAndVerifyToken(client_2.get(), false, kSixZeros, token_params);
306 // Test sequential initialization.
307 client_3->Initialize(base::Bind(&IgnoreResult));
309 EncodeTokenAndSaveSamples(client_3.get(), true, kSixZeros);
310 DecodeSamplesAndVerifyToken(client_3.get(), true, kSixZeros, token_params);
312 const size_t kLongTokenLengths[2] = {8, 9};
313 GetTokenParamsForLengths(kLongTokenLengths, token_params);
315 EncodeTokenAndSaveSamples(client_2.get(), true, kEightZeros);
316 DecodeSamplesAndVerifyToken(client_2.get(), true, kEightZeros, token_params);
319 // TODO(ckehoe): Test crc and parity
320 // TODO(ckehoe): More multi-client testing