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 void InitCallback(bool success) { | 136 void InitCallback(bool success) { |
134 EXPECT_TRUE(success); | 137 EXPECT_TRUE(success); |
135 initialized_ = true; | 138 initialized_ = true; |
136 ASSERT_TRUE(run_loop_); | 139 ASSERT_TRUE(run_loop_); |
137 run_loop_->Quit(); | 140 run_loop_->Quit(); |
138 } | 141 } |
139 | 142 |
140 void SamplesCallback( | 143 void SamplesCallback( |
141 copresence::AudioType type, | 144 audio_modem::AudioType type, |
142 const std::string& token, | 145 const std::string& token, |
143 const scoped_refptr<media::AudioBusRefCounted>& samples) { | 146 const scoped_refptr<media::AudioBusRefCounted>& samples) { |
144 EXPECT_EQ(expected_token_, token); | 147 EXPECT_EQ(expected_token_, token); |
145 EXPECT_EQ(expected_audible_, type == copresence::AUDIBLE); | 148 EXPECT_EQ(expected_audible_, type == AUDIBLE); |
146 saved_samples_ = samples; | 149 saved_samples_ = samples; |
147 ASSERT_TRUE(run_loop_); | 150 ASSERT_TRUE(run_loop_); |
148 run_loop_->Quit(); | 151 run_loop_->Quit(); |
149 } | 152 } |
150 | 153 |
151 void TokensCallback(const std::vector<copresence::AudioToken>& tokens) { | 154 void TokensCallback(const std::vector<audio_modem::AudioToken>& tokens) { |
152 ASSERT_TRUE(run_loop_); | 155 ASSERT_TRUE(run_loop_); |
153 run_loop_->Quit(); | 156 run_loop_->Quit(); |
154 | 157 |
155 EXPECT_EQ(expected_token_, tokens[0].token); | 158 EXPECT_EQ(expected_token_, tokens[0].token); |
156 EXPECT_EQ(expected_audible_, tokens[0].audible); | 159 EXPECT_EQ(expected_audible_, tokens[0].audible); |
157 } | 160 } |
158 | 161 |
159 void DetectBroadcastCallback(bool success) { | 162 void DetectBroadcastCallback(bool success) { |
160 EXPECT_TRUE(success); | 163 EXPECT_TRUE(success); |
161 ASSERT_TRUE(run_loop_); | 164 ASSERT_TRUE(run_loop_); |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
223 IN_PROC_BROWSER_TEST_F(ChromeWhispernetClientTest, MAYBE_TokenLengths) { | 226 IN_PROC_BROWSER_TEST_F(ChromeWhispernetClientTest, MAYBE_TokenLengths) { |
224 InitializeWhispernet(); | 227 InitializeWhispernet(); |
225 size_t kLongTokenLengths[2] = {8, 9}; | 228 size_t kLongTokenLengths[2] = {8, 9}; |
226 | 229 |
227 EncodeTokenAndSaveSamples(true, kEightZeros); | 230 EncodeTokenAndSaveSamples(true, kEightZeros); |
228 DecodeSamplesAndVerifyToken(true, kEightZeros, kLongTokenLengths); | 231 DecodeSamplesAndVerifyToken(true, kEightZeros, kLongTokenLengths); |
229 | 232 |
230 EncodeTokenAndSaveSamples(false, kNineZeros); | 233 EncodeTokenAndSaveSamples(false, kNineZeros); |
231 DecodeSamplesAndVerifyToken(false, kNineZeros, kLongTokenLengths); | 234 DecodeSamplesAndVerifyToken(false, kNineZeros, kLongTokenLengths); |
232 } | 235 } |
OLD | NEW |