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