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