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_private/copresence_private_ap i.h" | 15 #include "chrome/browser/extensions/api/copresence/copresence_util.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 "media/base/audio_bus.h" | 20 #include "media/base/audio_bus.h" |
21 | 21 |
22 namespace { | 22 namespace { |
23 | 23 |
24 // Copied from src/components/copresence/mediums/audio/audio_recorder.cc | 24 // Copied from src/components/copresence/mediums/audio/audio_recorder.cc |
25 std::string AudioBusToString(scoped_refptr<media::AudioBusRefCounted> source) { | 25 std::string AudioBusToString(scoped_refptr<media::AudioBusRefCounted> source) { |
26 std::string buffer; | 26 std::string buffer; |
27 buffer.resize(source->frames() * source->channels() * sizeof(float)); | 27 buffer.resize(source->frames() * source->channels() * sizeof(float)); |
28 float* buffer_view = reinterpret_cast<float*>(string_as_array(&buffer)); | 28 float* buffer_view = reinterpret_cast<float*>(string_as_array(&buffer)); |
29 | 29 |
30 const int channels = source->channels(); | 30 const int channels = source->channels(); |
31 for (int ch = 0; ch < channels; ++ch) { | 31 for (int ch = 0; ch < channels; ++ch) { |
32 for (int si = 0, di = ch; si < source->frames(); ++si, di += channels) | 32 for (int si = 0, di = ch; si < source->frames(); ++si, di += channels) |
33 buffer_view[di] = source->channel(ch)[si]; | 33 buffer_view[di] = source->channel(ch)[si]; |
34 } | 34 } |
35 | 35 |
36 return buffer; | 36 return buffer; |
37 } | 37 } |
38 | 38 |
39 } // namespace | 39 } // namespace |
40 | 40 |
41 class ChromeWhispernetClientTest : public ExtensionBrowserTest { | 41 class ChromeWhispernetClientTest : public ExtensionBrowserTest { |
42 public: | 42 public: |
43 ChromeWhispernetClientTest() : initialized_(false) {} | 43 ChromeWhispernetClientTest() : context_(NULL), initialized_(false) {} |
44 | 44 |
45 virtual ~ChromeWhispernetClientTest() { | 45 virtual ~ChromeWhispernetClientTest() {} |
46 if (client_) | |
47 extensions::SetWhispernetClientForTesting(NULL); | |
48 } | |
49 | 46 |
50 void InitializeWhispernet() { | 47 void InitializeWhispernet() { |
48 context_ = browser()->profile(); | |
51 run_loop_.reset(new base::RunLoop()); | 49 run_loop_.reset(new base::RunLoop()); |
52 client_.reset(new ChromeWhispernetClient(browser()->profile())); | 50 extensions::GetWhispernetClient(context_)->Initialize(base::Bind( |
53 extensions::SetWhispernetClientForTesting(client_.get()); | 51 &ChromeWhispernetClientTest::InitCallback, base::Unretained(this))); |
54 | |
55 client_->Initialize(base::Bind(&ChromeWhispernetClientTest::InitCallback, | |
56 base::Unretained(this))); | |
57 run_loop_->Run(); | 52 run_loop_->Run(); |
58 | 53 |
59 EXPECT_TRUE(initialized_); | 54 EXPECT_TRUE(initialized_); |
60 } | 55 } |
61 | 56 |
62 void EncodeTokenAndSaveSamples() { | 57 void EncodeTokenAndSaveSamples() { |
63 ASSERT_TRUE(client_); | 58 ASSERT_TRUE(extensions::GetWhispernetClient(context_)); |
64 | 59 |
65 // This is the base64 encoding for 000000. | 60 // This is the base64 encoding for "000000". |
66 const std::string kZeroToken = "MDAwMDAw"; | 61 const std::string kZeroToken = "MDAwMDAw"; |
67 | 62 |
68 run_loop_.reset(new base::RunLoop()); | 63 run_loop_.reset(new base::RunLoop()); |
69 client_->RegisterSamplesCallback(base::Bind( | 64 extensions::GetWhispernetClient(context_)->RegisterSamplesCallback( |
70 &ChromeWhispernetClientTest::SamplesCallback, base::Unretained(this))); | 65 base::Bind(&ChromeWhispernetClientTest::SamplesCallback, |
66 base::Unretained(this))); | |
71 expected_token_ = kZeroToken; | 67 expected_token_ = kZeroToken; |
72 | 68 |
73 client_->EncodeToken(kZeroToken); | 69 extensions::GetWhispernetClient(context_)->EncodeToken(kZeroToken); |
74 run_loop_->Run(); | 70 run_loop_->Run(); |
75 | 71 |
76 EXPECT_GT(saved_samples_->frames(), 0); | 72 EXPECT_GT(saved_samples_->frames(), 0); |
77 } | 73 } |
78 | 74 |
79 void DecodeSamplesAndVerifyToken() { | 75 void DecodeSamplesAndVerifyToken() { |
80 ASSERT_TRUE(client_); | 76 ASSERT_TRUE(extensions::GetWhispernetClient(context_)); |
81 | 77 |
82 const std::string kZeroToken = "MDAwMDAw"; | 78 const std::string kZeroToken = "MDAwMDAw"; |
83 | 79 |
84 run_loop_.reset(new base::RunLoop()); | 80 run_loop_.reset(new base::RunLoop()); |
85 client_->RegisterTokensCallback(base::Bind( | 81 extensions::GetWhispernetClient(context_)->RegisterTokensCallback( |
86 &ChromeWhispernetClientTest::TokensCallback, base::Unretained(this))); | 82 base::Bind(&ChromeWhispernetClientTest::TokensCallback, |
83 base::Unretained(this))); | |
87 expected_token_ = kZeroToken; | 84 expected_token_ = kZeroToken; |
88 | 85 |
89 ASSERT_GT(saved_samples_->frames(), 0); | 86 ASSERT_GT(saved_samples_->frames(), 0); |
90 | 87 |
91 // Convert our single channel samples to two channel. Decode samples | 88 // Convert our single channel samples to two channel. Decode samples |
92 // expects 2 channel data. | 89 // expects 2 channel data. |
93 scoped_refptr<media::AudioBusRefCounted> samples_bus = | 90 scoped_refptr<media::AudioBusRefCounted> samples_bus = |
94 media::AudioBusRefCounted::Create(2, saved_samples_->frames()); | 91 media::AudioBusRefCounted::Create(2, saved_samples_->frames()); |
95 memcpy(samples_bus->channel(0), | 92 memcpy(samples_bus->channel(0), |
96 saved_samples_->channel(0), | 93 saved_samples_->channel(0), |
97 sizeof(float) * saved_samples_->frames()); | 94 sizeof(float) * saved_samples_->frames()); |
98 memcpy(samples_bus->channel(1), | 95 memcpy(samples_bus->channel(1), |
99 saved_samples_->channel(0), | 96 saved_samples_->channel(0), |
100 sizeof(float) * saved_samples_->frames()); | 97 sizeof(float) * saved_samples_->frames()); |
101 | 98 |
102 client_->DecodeSamples(AudioBusToString(samples_bus)); | 99 extensions::GetWhispernetClient(context_) |
100 ->DecodeSamples(AudioBusToString(samples_bus)); | |
103 run_loop_->Run(); | 101 run_loop_->Run(); |
104 } | 102 } |
105 | 103 |
106 void DetectBroadcast() { | 104 void DetectBroadcast() { |
107 ASSERT_TRUE(client_); | 105 ASSERT_TRUE(extensions::GetWhispernetClient(context_)); |
Daniel Erat
2014/08/06 22:59:36
nit: cache this so you don't need to get it multip
rkc
2014/08/07 02:18:33
Done.
| |
108 | 106 |
109 run_loop_.reset(new base::RunLoop()); | 107 run_loop_.reset(new base::RunLoop()); |
110 client_->RegisterDetectBroadcastCallback( | 108 extensions::GetWhispernetClient(context_)->RegisterDetectBroadcastCallback( |
111 base::Bind(&ChromeWhispernetClientTest::DetectBroadcastCallback, | 109 base::Bind(&ChromeWhispernetClientTest::DetectBroadcastCallback, |
112 base::Unretained(this))); | 110 base::Unretained(this))); |
113 client_->DetectBroadcast(); | 111 extensions::GetWhispernetClient(context_)->DetectBroadcast(); |
114 run_loop_->Run(); | 112 run_loop_->Run(); |
115 } | 113 } |
116 | 114 |
117 protected: | 115 protected: |
118 void InitCallback(bool success) { | 116 void InitCallback(bool success) { |
119 EXPECT_TRUE(success); | 117 EXPECT_TRUE(success); |
120 initialized_ = true; | 118 initialized_ = true; |
121 ASSERT_TRUE(run_loop_); | 119 ASSERT_TRUE(run_loop_); |
122 run_loop_->Quit(); | 120 run_loop_->Quit(); |
123 } | 121 } |
(...skipping 15 matching lines...) Expand all Loading... | |
139 } | 137 } |
140 | 138 |
141 void DetectBroadcastCallback(bool success) { | 139 void DetectBroadcastCallback(bool success) { |
142 EXPECT_TRUE(success); | 140 EXPECT_TRUE(success); |
143 ASSERT_TRUE(run_loop_); | 141 ASSERT_TRUE(run_loop_); |
144 run_loop_->Quit(); | 142 run_loop_->Quit(); |
145 } | 143 } |
146 | 144 |
147 private: | 145 private: |
148 scoped_ptr<base::RunLoop> run_loop_; | 146 scoped_ptr<base::RunLoop> run_loop_; |
149 scoped_ptr<ChromeWhispernetClient> client_; | 147 content::BrowserContext* context_; |
150 | 148 |
151 std::string expected_token_; | 149 std::string expected_token_; |
152 scoped_refptr<media::AudioBusRefCounted> saved_samples_; | 150 scoped_refptr<media::AudioBusRefCounted> saved_samples_; |
153 | 151 |
154 bool initialized_; | 152 bool initialized_; |
155 | 153 |
156 DISALLOW_COPY_AND_ASSIGN(ChromeWhispernetClientTest); | 154 DISALLOW_COPY_AND_ASSIGN(ChromeWhispernetClientTest); |
157 }; | 155 }; |
158 | 156 |
159 IN_PROC_BROWSER_TEST_F(ChromeWhispernetClientTest, Initialize) { | 157 IN_PROC_BROWSER_TEST_F(ChromeWhispernetClientTest, Initialize) { |
(...skipping 10 matching lines...) Expand all Loading... | |
170 EncodeTokenAndSaveSamples(); | 168 EncodeTokenAndSaveSamples(); |
171 DecodeSamplesAndVerifyToken(); | 169 DecodeSamplesAndVerifyToken(); |
172 } | 170 } |
173 | 171 |
174 IN_PROC_BROWSER_TEST_F(ChromeWhispernetClientTest, DetectBroadcast) { | 172 IN_PROC_BROWSER_TEST_F(ChromeWhispernetClientTest, DetectBroadcast) { |
175 InitializeWhispernet(); | 173 InitializeWhispernet(); |
176 EncodeTokenAndSaveSamples(); | 174 EncodeTokenAndSaveSamples(); |
177 DecodeSamplesAndVerifyToken(); | 175 DecodeSamplesAndVerifyToken(); |
178 DetectBroadcast(); | 176 DetectBroadcast(); |
179 } | 177 } |
OLD | NEW |