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 copresence::WhispernetClient* client = |
| 59 extensions::GetWhispernetClient(context_); |
| 60 ASSERT_TRUE(client); |
64 | 61 |
65 // This is the base64 encoding for 000000. | 62 // This is the base64 encoding for "000000". |
66 const std::string kZeroToken = "MDAwMDAw"; | 63 const std::string kZeroToken = "MDAwMDAw"; |
67 | 64 |
68 run_loop_.reset(new base::RunLoop()); | 65 run_loop_.reset(new base::RunLoop()); |
69 client_->RegisterSamplesCallback(base::Bind( | 66 client->RegisterSamplesCallback(base::Bind( |
70 &ChromeWhispernetClientTest::SamplesCallback, base::Unretained(this))); | 67 &ChromeWhispernetClientTest::SamplesCallback, base::Unretained(this))); |
71 expected_token_ = kZeroToken; | 68 expected_token_ = kZeroToken; |
72 | 69 |
73 client_->EncodeToken(kZeroToken); | 70 client->EncodeToken(kZeroToken); |
74 run_loop_->Run(); | 71 run_loop_->Run(); |
75 | 72 |
76 EXPECT_GT(saved_samples_->frames(), 0); | 73 EXPECT_GT(saved_samples_->frames(), 0); |
77 } | 74 } |
78 | 75 |
79 void DecodeSamplesAndVerifyToken() { | 76 void DecodeSamplesAndVerifyToken() { |
80 ASSERT_TRUE(client_); | 77 copresence::WhispernetClient* client = |
| 78 extensions::GetWhispernetClient(context_); |
| 79 ASSERT_TRUE(client); |
81 | 80 |
82 const std::string kZeroToken = "MDAwMDAw"; | 81 const std::string kZeroToken = "MDAwMDAw"; |
83 | 82 |
84 run_loop_.reset(new base::RunLoop()); | 83 run_loop_.reset(new base::RunLoop()); |
85 client_->RegisterTokensCallback(base::Bind( | 84 client->RegisterTokensCallback(base::Bind( |
86 &ChromeWhispernetClientTest::TokensCallback, base::Unretained(this))); | 85 &ChromeWhispernetClientTest::TokensCallback, base::Unretained(this))); |
87 expected_token_ = kZeroToken; | 86 expected_token_ = kZeroToken; |
88 | 87 |
89 ASSERT_GT(saved_samples_->frames(), 0); | 88 ASSERT_GT(saved_samples_->frames(), 0); |
90 | 89 |
91 // Convert our single channel samples to two channel. Decode samples | 90 // Convert our single channel samples to two channel. Decode samples |
92 // expects 2 channel data. | 91 // expects 2 channel data. |
93 scoped_refptr<media::AudioBusRefCounted> samples_bus = | 92 scoped_refptr<media::AudioBusRefCounted> samples_bus = |
94 media::AudioBusRefCounted::Create(2, saved_samples_->frames()); | 93 media::AudioBusRefCounted::Create(2, saved_samples_->frames()); |
95 memcpy(samples_bus->channel(0), | 94 memcpy(samples_bus->channel(0), |
96 saved_samples_->channel(0), | 95 saved_samples_->channel(0), |
97 sizeof(float) * saved_samples_->frames()); | 96 sizeof(float) * saved_samples_->frames()); |
98 memcpy(samples_bus->channel(1), | 97 memcpy(samples_bus->channel(1), |
99 saved_samples_->channel(0), | 98 saved_samples_->channel(0), |
100 sizeof(float) * saved_samples_->frames()); | 99 sizeof(float) * saved_samples_->frames()); |
101 | 100 |
102 client_->DecodeSamples(AudioBusToString(samples_bus)); | 101 client->DecodeSamples(AudioBusToString(samples_bus)); |
103 run_loop_->Run(); | 102 run_loop_->Run(); |
104 } | 103 } |
105 | 104 |
106 void DetectBroadcast() { | 105 void DetectBroadcast() { |
107 ASSERT_TRUE(client_); | 106 copresence::WhispernetClient* client = |
| 107 extensions::GetWhispernetClient(context_); |
| 108 ASSERT_TRUE(client); |
108 | 109 |
109 run_loop_.reset(new base::RunLoop()); | 110 run_loop_.reset(new base::RunLoop()); |
110 client_->RegisterDetectBroadcastCallback( | 111 client->RegisterDetectBroadcastCallback( |
111 base::Bind(&ChromeWhispernetClientTest::DetectBroadcastCallback, | 112 base::Bind(&ChromeWhispernetClientTest::DetectBroadcastCallback, |
112 base::Unretained(this))); | 113 base::Unretained(this))); |
113 client_->DetectBroadcast(); | 114 client->DetectBroadcast(); |
114 run_loop_->Run(); | 115 run_loop_->Run(); |
115 } | 116 } |
116 | 117 |
117 protected: | 118 protected: |
118 void InitCallback(bool success) { | 119 void InitCallback(bool success) { |
119 EXPECT_TRUE(success); | 120 EXPECT_TRUE(success); |
120 initialized_ = true; | 121 initialized_ = true; |
121 ASSERT_TRUE(run_loop_); | 122 ASSERT_TRUE(run_loop_); |
122 run_loop_->Quit(); | 123 run_loop_->Quit(); |
123 } | 124 } |
(...skipping 15 matching lines...) Expand all Loading... |
139 } | 140 } |
140 | 141 |
141 void DetectBroadcastCallback(bool success) { | 142 void DetectBroadcastCallback(bool success) { |
142 EXPECT_TRUE(success); | 143 EXPECT_TRUE(success); |
143 ASSERT_TRUE(run_loop_); | 144 ASSERT_TRUE(run_loop_); |
144 run_loop_->Quit(); | 145 run_loop_->Quit(); |
145 } | 146 } |
146 | 147 |
147 private: | 148 private: |
148 scoped_ptr<base::RunLoop> run_loop_; | 149 scoped_ptr<base::RunLoop> run_loop_; |
149 scoped_ptr<ChromeWhispernetClient> client_; | 150 content::BrowserContext* context_; |
150 | 151 |
151 std::string expected_token_; | 152 std::string expected_token_; |
152 scoped_refptr<media::AudioBusRefCounted> saved_samples_; | 153 scoped_refptr<media::AudioBusRefCounted> saved_samples_; |
153 | 154 |
154 bool initialized_; | 155 bool initialized_; |
155 | 156 |
156 DISALLOW_COPY_AND_ASSIGN(ChromeWhispernetClientTest); | 157 DISALLOW_COPY_AND_ASSIGN(ChromeWhispernetClientTest); |
157 }; | 158 }; |
158 | 159 |
159 IN_PROC_BROWSER_TEST_F(ChromeWhispernetClientTest, Initialize) { | 160 IN_PROC_BROWSER_TEST_F(ChromeWhispernetClientTest, Initialize) { |
(...skipping 10 matching lines...) Expand all Loading... |
170 EncodeTokenAndSaveSamples(); | 171 EncodeTokenAndSaveSamples(); |
171 DecodeSamplesAndVerifyToken(); | 172 DecodeSamplesAndVerifyToken(); |
172 } | 173 } |
173 | 174 |
174 IN_PROC_BROWSER_TEST_F(ChromeWhispernetClientTest, DetectBroadcast) { | 175 IN_PROC_BROWSER_TEST_F(ChromeWhispernetClientTest, DetectBroadcast) { |
175 InitializeWhispernet(); | 176 InitializeWhispernet(); |
176 EncodeTokenAndSaveSamples(); | 177 EncodeTokenAndSaveSamples(); |
177 DecodeSamplesAndVerifyToken(); | 178 DecodeSamplesAndVerifyToken(); |
178 DetectBroadcast(); | 179 DetectBroadcast(); |
179 } | 180 } |
OLD | NEW |