| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/copresence/chrome_whispernet_client.h" | 5 #include "chrome/browser/copresence/chrome_whispernet_client.h" |
| 6 | 6 |
| 7 #include <cstdlib> | 7 #include <cstdlib> |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/macros.h" | 11 #include "base/macros.h" |
| 12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/run_loop.h" | 13 #include "base/run_loop.h" |
| 14 #include "base/stl_util.h" | 14 #include "base/stl_util.h" |
| 15 #include "chrome/browser/extensions/api/copresence/copresence_api.h" | 15 #include "chrome/browser/extensions/api/copresence/copresence_api.h" |
| 16 #include "chrome/browser/extensions/extension_browsertest.h" | 16 #include "chrome/browser/extensions/extension_browsertest.h" |
| 17 #include "chrome/browser/profiles/profile.h" | 17 #include "chrome/browser/profiles/profile.h" |
| 18 #include "chrome/browser/ui/browser.h" | 18 #include "chrome/browser/ui/browser.h" |
| 19 #include "chrome/test/base/in_process_browser_test.h" | 19 #include "chrome/test/base/in_process_browser_test.h" |
| 20 #include "components/copresence/public/copresence_constants.h" | 20 #include "components/audio_modem/public/whispernet_client.h" |
| 21 #include "media/base/audio_bus.h" | 21 #include "media/base/audio_bus.h" |
| 22 | 22 |
| 23 using audio_modem::WhispernetClient; |
| 24 using audio_modem::AUDIBLE; |
| 25 using audio_modem::INAUDIBLE; |
| 26 |
| 23 namespace { | 27 namespace { |
| 24 | 28 |
| 25 // TODO(rkc): Add more varied test input. | 29 // TODO(rkc): Add more varied test input. |
| 26 const char kSixZeros[] = "MDAwMDAw"; | 30 const char kSixZeros[] = "MDAwMDAw"; |
| 27 const char kEightZeros[] = "MDAwMDAwMDA"; | 31 const char kEightZeros[] = "MDAwMDAwMDA"; |
| 28 const char kNineZeros[] = "MDAwMDAwMDAw"; | 32 const char kNineZeros[] = "MDAwMDAwMDAw"; |
| 29 | 33 |
| 30 const size_t kTokenLengths[] = {6, 6}; | 34 const size_t kTokenLengths[] = {6, 6}; |
| 31 | 35 |
| 32 copresence::WhispernetClient* GetWhispernetClient( | 36 WhispernetClient* GetWhispernetClient(content::BrowserContext* context) { |
| 33 content::BrowserContext* context) { | |
| 34 extensions::CopresenceService* service = | 37 extensions::CopresenceService* service = |
| 35 extensions::CopresenceService::GetFactoryInstance()->Get(context); | 38 extensions::CopresenceService::GetFactoryInstance()->Get(context); |
| 36 return service ? service->whispernet_client() : NULL; | 39 return service ? service->whispernet_client() : NULL; |
| 37 } | 40 } |
| 38 | 41 |
| 39 // Copied from src/components/copresence/mediums/audio/audio_recorder.cc | 42 // Copied from src/components/copresence/mediums/audio/audio_recorder.cc |
| 40 std::string AudioBusToString(scoped_refptr<media::AudioBusRefCounted> source) { | 43 std::string AudioBusToString(scoped_refptr<media::AudioBusRefCounted> source) { |
| 41 std::string buffer; | 44 std::string buffer; |
| 42 buffer.resize(source->frames() * source->channels() * sizeof(float)); | 45 buffer.resize(source->frames() * source->channels() * sizeof(float)); |
| 43 float* buffer_view = reinterpret_cast<float*>(string_as_array(&buffer)); | 46 float* buffer_view = reinterpret_cast<float*>(string_as_array(&buffer)); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 64 context_ = browser()->profile(); | 67 context_ = browser()->profile(); |
| 65 run_loop_.reset(new base::RunLoop()); | 68 run_loop_.reset(new base::RunLoop()); |
| 66 GetWhispernetClient(context_)->Initialize(base::Bind( | 69 GetWhispernetClient(context_)->Initialize(base::Bind( |
| 67 &ChromeWhispernetClientTest::InitCallback, base::Unretained(this))); | 70 &ChromeWhispernetClientTest::InitCallback, base::Unretained(this))); |
| 68 run_loop_->Run(); | 71 run_loop_->Run(); |
| 69 | 72 |
| 70 EXPECT_TRUE(initialized_); | 73 EXPECT_TRUE(initialized_); |
| 71 } | 74 } |
| 72 | 75 |
| 73 void EncodeTokenAndSaveSamples(bool audible, const std::string& token) { | 76 void EncodeTokenAndSaveSamples(bool audible, const std::string& token) { |
| 74 copresence::WhispernetClient* client = GetWhispernetClient(context_); | 77 WhispernetClient* client = GetWhispernetClient(context_); |
| 75 ASSERT_TRUE(client); | 78 ASSERT_TRUE(client); |
| 76 | 79 |
| 77 run_loop_.reset(new base::RunLoop()); | 80 run_loop_.reset(new base::RunLoop()); |
| 78 client->RegisterSamplesCallback(base::Bind( | 81 client->RegisterSamplesCallback( |
| 79 &ChromeWhispernetClientTest::SamplesCallback, base::Unretained(this))); | 82 base::Bind(&ChromeWhispernetClientTest::SamplesCallback, |
| 83 base::Unretained(this))); |
| 80 expected_token_ = token; | 84 expected_token_ = token; |
| 81 expected_audible_ = audible; | 85 expected_audible_ = audible; |
| 82 | 86 |
| 83 client->EncodeToken(token, | 87 client->EncodeToken(token, audible ? AUDIBLE : INAUDIBLE); |
| 84 audible ? copresence::AUDIBLE : copresence::INAUDIBLE); | |
| 85 run_loop_->Run(); | 88 run_loop_->Run(); |
| 86 | 89 |
| 87 EXPECT_GT(saved_samples_->frames(), 0); | 90 EXPECT_GT(saved_samples_->frames(), 0); |
| 88 } | 91 } |
| 89 | 92 |
| 90 void DecodeSamplesAndVerifyToken(bool expect_audible, | 93 void DecodeSamplesAndVerifyToken(bool expect_audible, |
| 91 const std::string& expected_token, | 94 const std::string& expected_token, |
| 92 const size_t token_length[2]) { | 95 const size_t token_length[2]) { |
| 93 copresence::WhispernetClient* client = GetWhispernetClient(context_); | 96 WhispernetClient* client = GetWhispernetClient(context_); |
| 94 ASSERT_TRUE(client); | 97 ASSERT_TRUE(client); |
| 95 | 98 |
| 96 run_loop_.reset(new base::RunLoop()); | 99 run_loop_.reset(new base::RunLoop()); |
| 97 client->RegisterTokensCallback(base::Bind( | 100 client->RegisterTokensCallback(base::Bind( |
| 98 &ChromeWhispernetClientTest::TokensCallback, base::Unretained(this))); | 101 &ChromeWhispernetClientTest::TokensCallback, base::Unretained(this))); |
| 99 expected_token_ = expected_token; | 102 expected_token_ = expected_token; |
| 100 expected_audible_ = expect_audible; | 103 expected_audible_ = expect_audible; |
| 101 | 104 |
| 102 ASSERT_GT(saved_samples_->frames(), 0); | 105 ASSERT_GT(saved_samples_->frames(), 0); |
| 103 | 106 |
| 104 // Convert our single channel samples to two channel. Decode samples | 107 // Convert our single channel samples to two channel. Decode samples |
| 105 // expects 2 channel data. | 108 // expects 2 channel data. |
| 106 scoped_refptr<media::AudioBusRefCounted> samples_bus = | 109 scoped_refptr<media::AudioBusRefCounted> samples_bus = |
| 107 media::AudioBusRefCounted::Create(2, saved_samples_->frames()); | 110 media::AudioBusRefCounted::Create(2, saved_samples_->frames()); |
| 108 memcpy(samples_bus->channel(0), | 111 memcpy(samples_bus->channel(0), |
| 109 saved_samples_->channel(0), | 112 saved_samples_->channel(0), |
| 110 sizeof(float) * saved_samples_->frames()); | 113 sizeof(float) * saved_samples_->frames()); |
| 111 memcpy(samples_bus->channel(1), | 114 memcpy(samples_bus->channel(1), |
| 112 saved_samples_->channel(0), | 115 saved_samples_->channel(0), |
| 113 sizeof(float) * saved_samples_->frames()); | 116 sizeof(float) * saved_samples_->frames()); |
| 114 | 117 |
| 115 client->DecodeSamples( | 118 client->DecodeSamples( |
| 116 expect_audible ? copresence::AUDIBLE : copresence::INAUDIBLE, | 119 expect_audible ? AUDIBLE : INAUDIBLE, |
| 117 AudioBusToString(samples_bus), token_length); | 120 AudioBusToString(samples_bus), token_length); |
| 118 run_loop_->Run(); | 121 run_loop_->Run(); |
| 119 } | 122 } |
| 120 | 123 |
| 121 void DetectBroadcast() { | 124 void DetectBroadcast() { |
| 122 copresence::WhispernetClient* client = GetWhispernetClient(context_); | 125 WhispernetClient* client = GetWhispernetClient(context_); |
| 123 ASSERT_TRUE(client); | 126 ASSERT_TRUE(client); |
| 124 | 127 |
| 125 run_loop_.reset(new base::RunLoop()); | 128 run_loop_.reset(new base::RunLoop()); |
| 126 client->RegisterDetectBroadcastCallback( | 129 client->RegisterDetectBroadcastCallback( |
| 127 base::Bind(&ChromeWhispernetClientTest::DetectBroadcastCallback, | 130 base::Bind(&ChromeWhispernetClientTest::DetectBroadcastCallback, |
| 128 base::Unretained(this))); | 131 base::Unretained(this))); |
| 129 client->DetectBroadcast(); | 132 client->DetectBroadcast(); |
| 130 run_loop_->Run(); | 133 run_loop_->Run(); |
| 131 } | 134 } |
| 132 | 135 |
| 133 protected: | 136 protected: |
| 134 void InitCallback(bool success) { | 137 void InitCallback(bool success) { |
| 135 EXPECT_TRUE(success); | 138 EXPECT_TRUE(success); |
| 136 initialized_ = true; | 139 initialized_ = true; |
| 137 ASSERT_TRUE(run_loop_); | 140 ASSERT_TRUE(run_loop_); |
| 138 run_loop_->Quit(); | 141 run_loop_->Quit(); |
| 139 } | 142 } |
| 140 | 143 |
| 141 void SamplesCallback( | 144 void SamplesCallback( |
| 142 copresence::AudioType type, | 145 audio_modem::AudioType type, |
| 143 const std::string& token, | 146 const std::string& token, |
| 144 const scoped_refptr<media::AudioBusRefCounted>& samples) { | 147 const scoped_refptr<media::AudioBusRefCounted>& samples) { |
| 145 EXPECT_EQ(expected_token_, token); | 148 EXPECT_EQ(expected_token_, token); |
| 146 EXPECT_EQ(expected_audible_, type == copresence::AUDIBLE); | 149 EXPECT_EQ(expected_audible_, type == AUDIBLE); |
| 147 saved_samples_ = samples; | 150 saved_samples_ = samples; |
| 148 ASSERT_TRUE(run_loop_); | 151 ASSERT_TRUE(run_loop_); |
| 149 run_loop_->Quit(); | 152 run_loop_->Quit(); |
| 150 } | 153 } |
| 151 | 154 |
| 152 void TokensCallback(const std::vector<copresence::AudioToken>& tokens) { | 155 void TokensCallback(const std::vector<audio_modem::AudioToken>& tokens) { |
| 153 ASSERT_TRUE(run_loop_); | 156 ASSERT_TRUE(run_loop_); |
| 154 run_loop_->Quit(); | 157 run_loop_->Quit(); |
| 155 | 158 |
| 156 EXPECT_EQ(expected_token_, tokens[0].token); | 159 EXPECT_EQ(expected_token_, tokens[0].token); |
| 157 EXPECT_EQ(expected_audible_, tokens[0].audible); | 160 EXPECT_EQ(expected_audible_, tokens[0].audible); |
| 158 } | 161 } |
| 159 | 162 |
| 160 void DetectBroadcastCallback(bool success) { | 163 void DetectBroadcastCallback(bool success) { |
| 161 EXPECT_TRUE(success); | 164 EXPECT_TRUE(success); |
| 162 ASSERT_TRUE(run_loop_); | 165 ASSERT_TRUE(run_loop_); |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 224 IN_PROC_BROWSER_TEST_F(ChromeWhispernetClientTest, MAYBE_TokenLengths) { | 227 IN_PROC_BROWSER_TEST_F(ChromeWhispernetClientTest, MAYBE_TokenLengths) { |
| 225 InitializeWhispernet(); | 228 InitializeWhispernet(); |
| 226 size_t kLongTokenLengths[2] = {8, 9}; | 229 size_t kLongTokenLengths[2] = {8, 9}; |
| 227 | 230 |
| 228 EncodeTokenAndSaveSamples(true, kEightZeros); | 231 EncodeTokenAndSaveSamples(true, kEightZeros); |
| 229 DecodeSamplesAndVerifyToken(true, kEightZeros, kLongTokenLengths); | 232 DecodeSamplesAndVerifyToken(true, kEightZeros, kLongTokenLengths); |
| 230 | 233 |
| 231 EncodeTokenAndSaveSamples(false, kNineZeros); | 234 EncodeTokenAndSaveSamples(false, kNineZeros); |
| 232 DecodeSamplesAndVerifyToken(false, kNineZeros, kLongTokenLengths); | 235 DecodeSamplesAndVerifyToken(false, kNineZeros, kLongTokenLengths); |
| 233 } | 236 } |
| OLD | NEW |