| 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 "media/base/audio_bus.h" | 21 #include "media/base/audio_bus.h" |
| 21 | 22 |
| 22 namespace { | 23 namespace { |
| 23 | 24 |
| 24 const char kSixZeros[] = "MDAwMDAw"; | 25 const char kSixZeros[] = "MDAwMDAw"; |
| 25 | 26 |
| 26 copresence::WhispernetClient* GetWhispernetClient( | 27 copresence::WhispernetClient* GetWhispernetClient( |
| 27 content::BrowserContext* context) { | 28 content::BrowserContext* context) { |
| 28 extensions::CopresenceService* service = | 29 extensions::CopresenceService* service = |
| 29 extensions::CopresenceService::GetFactoryInstance()->Get(context); | 30 extensions::CopresenceService::GetFactoryInstance()->Get(context); |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 66 void EncodeTokenAndSaveSamples(bool audible) { | 67 void EncodeTokenAndSaveSamples(bool audible) { |
| 67 copresence::WhispernetClient* client = GetWhispernetClient(context_); | 68 copresence::WhispernetClient* client = GetWhispernetClient(context_); |
| 68 ASSERT_TRUE(client); | 69 ASSERT_TRUE(client); |
| 69 | 70 |
| 70 run_loop_.reset(new base::RunLoop()); | 71 run_loop_.reset(new base::RunLoop()); |
| 71 client->RegisterSamplesCallback(base::Bind( | 72 client->RegisterSamplesCallback(base::Bind( |
| 72 &ChromeWhispernetClientTest::SamplesCallback, base::Unretained(this))); | 73 &ChromeWhispernetClientTest::SamplesCallback, base::Unretained(this))); |
| 73 expected_token_ = kSixZeros; | 74 expected_token_ = kSixZeros; |
| 74 expected_audible_ = audible; | 75 expected_audible_ = audible; |
| 75 | 76 |
| 76 client->EncodeToken(kSixZeros, audible); | 77 client->EncodeToken(kSixZeros, |
| 78 audible ? copresence::AUDIBLE : copresence::INAUDIBLE); |
| 77 run_loop_->Run(); | 79 run_loop_->Run(); |
| 78 | 80 |
| 79 EXPECT_GT(saved_samples_->frames(), 0); | 81 EXPECT_GT(saved_samples_->frames(), 0); |
| 80 } | 82 } |
| 81 | 83 |
| 82 void DecodeSamplesAndVerifyToken(bool expect_audible) { | 84 void DecodeSamplesAndVerifyToken(bool expect_audible) { |
| 83 copresence::WhispernetClient* client = GetWhispernetClient(context_); | 85 copresence::WhispernetClient* client = GetWhispernetClient(context_); |
| 84 ASSERT_TRUE(client); | 86 ASSERT_TRUE(client); |
| 85 | 87 |
| 86 run_loop_.reset(new base::RunLoop()); | 88 run_loop_.reset(new base::RunLoop()); |
| 87 client->RegisterTokensCallback(base::Bind( | 89 client->RegisterTokensCallback(base::Bind( |
| 88 &ChromeWhispernetClientTest::TokensCallback, base::Unretained(this))); | 90 &ChromeWhispernetClientTest::TokensCallback, base::Unretained(this))); |
| 89 expected_token_ = kSixZeros; | 91 expected_token_ = kSixZeros; |
| 90 expected_audible_ = expect_audible; | 92 expected_audible_ = expect_audible; |
| 91 | 93 |
| 92 ASSERT_GT(saved_samples_->frames(), 0); | 94 ASSERT_GT(saved_samples_->frames(), 0); |
| 93 | 95 |
| 94 // Convert our single channel samples to two channel. Decode samples | 96 // Convert our single channel samples to two channel. Decode samples |
| 95 // expects 2 channel data. | 97 // expects 2 channel data. |
| 96 scoped_refptr<media::AudioBusRefCounted> samples_bus = | 98 scoped_refptr<media::AudioBusRefCounted> samples_bus = |
| 97 media::AudioBusRefCounted::Create(2, saved_samples_->frames()); | 99 media::AudioBusRefCounted::Create(2, saved_samples_->frames()); |
| 98 memcpy(samples_bus->channel(0), | 100 memcpy(samples_bus->channel(0), |
| 99 saved_samples_->channel(0), | 101 saved_samples_->channel(0), |
| 100 sizeof(float) * saved_samples_->frames()); | 102 sizeof(float) * saved_samples_->frames()); |
| 101 memcpy(samples_bus->channel(1), | 103 memcpy(samples_bus->channel(1), |
| 102 saved_samples_->channel(0), | 104 saved_samples_->channel(0), |
| 103 sizeof(float) * saved_samples_->frames()); | 105 sizeof(float) * saved_samples_->frames()); |
| 104 | 106 |
| 105 client->DecodeSamples(AudioBusToString(samples_bus)); | 107 client->DecodeSamples( |
| 108 expect_audible ? copresence::AUDIBLE : copresence::INAUDIBLE, |
| 109 AudioBusToString(samples_bus)); |
| 106 run_loop_->Run(); | 110 run_loop_->Run(); |
| 107 } | 111 } |
| 108 | 112 |
| 109 void DetectBroadcast() { | 113 void DetectBroadcast() { |
| 110 copresence::WhispernetClient* client = GetWhispernetClient(context_); | 114 copresence::WhispernetClient* client = GetWhispernetClient(context_); |
| 111 ASSERT_TRUE(client); | 115 ASSERT_TRUE(client); |
| 112 | 116 |
| 113 run_loop_.reset(new base::RunLoop()); | 117 run_loop_.reset(new base::RunLoop()); |
| 114 client->RegisterDetectBroadcastCallback( | 118 client->RegisterDetectBroadcastCallback( |
| 115 base::Bind(&ChromeWhispernetClientTest::DetectBroadcastCallback, | 119 base::Bind(&ChromeWhispernetClientTest::DetectBroadcastCallback, |
| 116 base::Unretained(this))); | 120 base::Unretained(this))); |
| 117 client->DetectBroadcast(); | 121 client->DetectBroadcast(); |
| 118 run_loop_->Run(); | 122 run_loop_->Run(); |
| 119 } | 123 } |
| 120 | 124 |
| 121 protected: | 125 protected: |
| 122 void InitCallback(bool success) { | 126 void InitCallback(bool success) { |
| 123 EXPECT_TRUE(success); | 127 EXPECT_TRUE(success); |
| 124 initialized_ = true; | 128 initialized_ = true; |
| 125 ASSERT_TRUE(run_loop_); | 129 ASSERT_TRUE(run_loop_); |
| 126 run_loop_->Quit(); | 130 run_loop_->Quit(); |
| 127 } | 131 } |
| 128 | 132 |
| 129 void SamplesCallback( | 133 void SamplesCallback( |
| 130 const std::string& token, | 134 const std::string& token, |
| 131 bool audible, | 135 copresence::AudioType type, |
| 132 const scoped_refptr<media::AudioBusRefCounted>& samples) { | 136 const scoped_refptr<media::AudioBusRefCounted>& samples) { |
| 133 EXPECT_EQ(expected_token_, token); | 137 EXPECT_EQ(expected_token_, token); |
| 134 EXPECT_EQ(expected_audible_, audible); | 138 EXPECT_EQ(expected_audible_, type == copresence::AUDIBLE); |
| 135 saved_samples_ = samples; | 139 saved_samples_ = samples; |
| 136 ASSERT_TRUE(run_loop_); | 140 ASSERT_TRUE(run_loop_); |
| 137 run_loop_->Quit(); | 141 run_loop_->Quit(); |
| 138 } | 142 } |
| 139 | 143 |
| 140 void TokensCallback(const std::vector<copresence::AudioToken>& tokens) { | 144 void TokensCallback(const std::vector<copresence::AudioToken>& tokens) { |
| 141 ASSERT_TRUE(run_loop_); | 145 ASSERT_TRUE(run_loop_); |
| 142 run_loop_->Quit(); | 146 run_loop_->Quit(); |
| 143 | 147 |
| 144 EXPECT_EQ(expected_token_, tokens[0].token); | 148 EXPECT_EQ(expected_token_, tokens[0].token); |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 184 EncodeTokenAndSaveSamples(false); | 188 EncodeTokenAndSaveSamples(false); |
| 185 DecodeSamplesAndVerifyToken(false); | 189 DecodeSamplesAndVerifyToken(false); |
| 186 DetectBroadcast(); | 190 DetectBroadcast(); |
| 187 } | 191 } |
| 188 | 192 |
| 189 IN_PROC_BROWSER_TEST_F(ChromeWhispernetClientTest, Audible) { | 193 IN_PROC_BROWSER_TEST_F(ChromeWhispernetClientTest, Audible) { |
| 190 InitializeWhispernet(); | 194 InitializeWhispernet(); |
| 191 EncodeTokenAndSaveSamples(true); | 195 EncodeTokenAndSaveSamples(true); |
| 192 DecodeSamplesAndVerifyToken(true); | 196 DecodeSamplesAndVerifyToken(true); |
| 193 } | 197 } |
| OLD | NEW |