OLD | NEW |
(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 "chrome/browser/copresence/chrome_whispernet_client.h" |
| 6 |
| 7 #include <cstdlib> |
| 8 #include <string> |
| 9 |
| 10 #include "base/bind.h" |
| 11 #include "base/macros.h" |
| 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/run_loop.h" |
| 14 #include "base/stl_util.h" |
| 15 #include "chrome/browser/extensions/api/copresence_private/copresence_private_ap
i.h" |
| 16 #include "chrome/browser/extensions/extension_browsertest.h" |
| 17 #include "chrome/browser/profiles/profile.h" |
| 18 #include "chrome/browser/ui/browser.h" |
| 19 #include "chrome/test/base/in_process_browser_test.h" |
| 20 #include "media/base/audio_bus.h" |
| 21 |
| 22 namespace { |
| 23 |
| 24 // Copied from src/components/copresence/mediums/audio/audio_recorder.cc |
| 25 std::string AudioBusToString(scoped_refptr<media::AudioBusRefCounted> source) { |
| 26 std::string buffer; |
| 27 buffer.resize(source->frames() * source->channels() * sizeof(float)); |
| 28 float* buffer_view = reinterpret_cast<float*>(string_as_array(&buffer)); |
| 29 |
| 30 const int channels = source->channels(); |
| 31 for (int ch = 0; ch < channels; ++ch) { |
| 32 for (int si = 0, di = ch; si < source->frames(); ++si, di += channels) |
| 33 buffer_view[di] = source->channel(ch)[si]; |
| 34 } |
| 35 |
| 36 return buffer; |
| 37 } |
| 38 |
| 39 } // namespace |
| 40 |
| 41 class ChromeWhispernetClientTest : public ExtensionBrowserTest { |
| 42 public: |
| 43 ChromeWhispernetClientTest() : initialized_(false) {} |
| 44 |
| 45 virtual ~ChromeWhispernetClientTest() { |
| 46 if (client_) |
| 47 extensions::SetWhispernetClientForTesting(NULL); |
| 48 } |
| 49 |
| 50 void InitializeWhispernet() { |
| 51 run_loop_.reset(new base::RunLoop()); |
| 52 client_.reset(new ChromeWhispernetClient(browser()->profile())); |
| 53 extensions::SetWhispernetClientForTesting(client_.get()); |
| 54 |
| 55 client_->Initialize(base::Bind(&ChromeWhispernetClientTest::InitCallback, |
| 56 base::Unretained(this))); |
| 57 run_loop_->Run(); |
| 58 |
| 59 EXPECT_TRUE(initialized_); |
| 60 } |
| 61 |
| 62 void EncodeTokenAndSaveSamples() { |
| 63 ASSERT_TRUE(client_); |
| 64 |
| 65 // This is the base64 encoding for 000000. |
| 66 const std::string kZeroToken = "MDAwMDAw"; |
| 67 |
| 68 run_loop_.reset(new base::RunLoop()); |
| 69 client_->RegisterSamplesCallback(base::Bind( |
| 70 &ChromeWhispernetClientTest::SamplesCallback, base::Unretained(this))); |
| 71 expected_token_ = kZeroToken; |
| 72 |
| 73 client_->EncodeToken(kZeroToken); |
| 74 run_loop_->Run(); |
| 75 |
| 76 EXPECT_GT(saved_samples_->frames(), 0); |
| 77 } |
| 78 |
| 79 void DecodeSamplesAndVerifyToken() { |
| 80 ASSERT_TRUE(client_); |
| 81 |
| 82 const std::string kZeroToken = "MDAwMDAw"; |
| 83 |
| 84 run_loop_.reset(new base::RunLoop()); |
| 85 client_->RegisterTokensCallback(base::Bind( |
| 86 &ChromeWhispernetClientTest::TokensCallback, base::Unretained(this))); |
| 87 expected_token_ = kZeroToken; |
| 88 |
| 89 ASSERT_GT(saved_samples_->frames(), 0); |
| 90 |
| 91 // Convert our single channel samples to two channel. Decode samples |
| 92 // expects 2 channel data. |
| 93 scoped_refptr<media::AudioBusRefCounted> samples_bus = |
| 94 media::AudioBusRefCounted::Create(2, saved_samples_->frames()); |
| 95 memcpy(samples_bus->channel(0), |
| 96 saved_samples_->channel(0), |
| 97 sizeof(float) * saved_samples_->frames()); |
| 98 memcpy(samples_bus->channel(1), |
| 99 saved_samples_->channel(0), |
| 100 sizeof(float) * saved_samples_->frames()); |
| 101 |
| 102 client_->DecodeSamples(AudioBusToString(samples_bus)); |
| 103 run_loop_->Run(); |
| 104 } |
| 105 |
| 106 void DetectBroadcast() { |
| 107 ASSERT_TRUE(client_); |
| 108 |
| 109 run_loop_.reset(new base::RunLoop()); |
| 110 client_->RegisterDetectBroadcastCallback( |
| 111 base::Bind(&ChromeWhispernetClientTest::DetectBroadcastCallback, |
| 112 base::Unretained(this))); |
| 113 client_->DetectBroadcast(); |
| 114 run_loop_->Run(); |
| 115 } |
| 116 |
| 117 protected: |
| 118 void InitCallback(bool success) { |
| 119 EXPECT_TRUE(success); |
| 120 initialized_ = true; |
| 121 ASSERT_TRUE(run_loop_); |
| 122 run_loop_->Quit(); |
| 123 } |
| 124 |
| 125 void SamplesCallback( |
| 126 const std::string& token, |
| 127 const scoped_refptr<media::AudioBusRefCounted>& samples) { |
| 128 EXPECT_EQ(expected_token_, token); |
| 129 saved_samples_ = samples; |
| 130 ASSERT_TRUE(run_loop_); |
| 131 run_loop_->Quit(); |
| 132 } |
| 133 |
| 134 void TokensCallback(const std::vector<std::string>& tokens) { |
| 135 ASSERT_TRUE(run_loop_); |
| 136 run_loop_->Quit(); |
| 137 |
| 138 EXPECT_EQ(expected_token_, tokens[0]); |
| 139 } |
| 140 |
| 141 void DetectBroadcastCallback(bool success) { |
| 142 EXPECT_TRUE(success); |
| 143 ASSERT_TRUE(run_loop_); |
| 144 run_loop_->Quit(); |
| 145 } |
| 146 |
| 147 private: |
| 148 scoped_ptr<base::RunLoop> run_loop_; |
| 149 scoped_ptr<ChromeWhispernetClient> client_; |
| 150 |
| 151 std::string expected_token_; |
| 152 scoped_refptr<media::AudioBusRefCounted> saved_samples_; |
| 153 |
| 154 bool initialized_; |
| 155 |
| 156 DISALLOW_COPY_AND_ASSIGN(ChromeWhispernetClientTest); |
| 157 }; |
| 158 |
| 159 IN_PROC_BROWSER_TEST_F(ChromeWhispernetClientTest, Initialize) { |
| 160 InitializeWhispernet(); |
| 161 } |
| 162 |
| 163 IN_PROC_BROWSER_TEST_F(ChromeWhispernetClientTest, EncodeToken) { |
| 164 InitializeWhispernet(); |
| 165 EncodeTokenAndSaveSamples(); |
| 166 } |
| 167 |
| 168 IN_PROC_BROWSER_TEST_F(ChromeWhispernetClientTest, DecodeSamples) { |
| 169 InitializeWhispernet(); |
| 170 EncodeTokenAndSaveSamples(); |
| 171 DecodeSamplesAndVerifyToken(); |
| 172 } |
| 173 |
| 174 IN_PROC_BROWSER_TEST_F(ChromeWhispernetClientTest, DetectBroadcast) { |
| 175 InitializeWhispernet(); |
| 176 EncodeTokenAndSaveSamples(); |
| 177 DecodeSamplesAndVerifyToken(); |
| 178 DetectBroadcast(); |
| 179 } |
OLD | NEW |