OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 <vector> | 5 #include <vector> |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
74 properties_.uuid = BluetoothMediaClient::kBluetoothAudioSinkUUID; | 74 properties_.uuid = BluetoothMediaClient::kBluetoothAudioSinkUUID; |
75 properties_.codec = FakeBluetoothMediaTransportClient::kTransportCodec; | 75 properties_.codec = FakeBluetoothMediaTransportClient::kTransportCodec; |
76 properties_.configuration = | 76 properties_.configuration = |
77 FakeBluetoothMediaTransportClient::kTransportConfiguration; | 77 FakeBluetoothMediaTransportClient::kTransportConfiguration; |
78 properties_.state = BluetoothMediaTransportClient::kStateIdle; | 78 properties_.state = BluetoothMediaTransportClient::kStateIdle; |
79 properties_.delay.reset( | 79 properties_.delay.reset( |
80 new uint16_t(FakeBluetoothMediaTransportClient::kTransportDelay)); | 80 new uint16_t(FakeBluetoothMediaTransportClient::kTransportDelay)); |
81 properties_.volume.reset( | 81 properties_.volume.reset( |
82 new uint16_t(FakeBluetoothMediaTransportClient::kTransportVolume)); | 82 new uint16_t(FakeBluetoothMediaTransportClient::kTransportVolume)); |
83 | 83 |
| 84 media_ = static_cast<FakeBluetoothMediaClient*>( |
| 85 DBusThreadManager::Get()->GetBluetoothMediaClient()); |
| 86 transport_ = static_cast<FakeBluetoothMediaTransportClient*>( |
| 87 DBusThreadManager::Get()->GetBluetoothMediaTransportClient()); |
| 88 |
84 GetAdapter(); | 89 GetAdapter(); |
85 } | 90 } |
86 | 91 |
87 void TearDown() override { | 92 void TearDown() override { |
88 callback_count_ = 0; | 93 callback_count_ = 0; |
89 error_callback_count_ = 0; | 94 error_callback_count_ = 0; |
| 95 observer_.reset(); |
| 96 |
| 97 media_->SetVisible(true); |
90 | 98 |
91 // The adapter should outlive audio sink. | 99 // The adapter should outlive audio sink. |
92 audio_sink_ = nullptr; | 100 audio_sink_ = nullptr; |
93 adapter_ = nullptr; | 101 adapter_ = nullptr; |
94 DBusThreadManager::Shutdown(); | 102 DBusThreadManager::Shutdown(); |
95 } | 103 } |
96 | 104 |
97 // Gets the existing Bluetooth adapter. | 105 // Gets the existing Bluetooth adapter. |
98 void GetAdapter() { | 106 void GetAdapter() { |
99 BluetoothAdapterFactory::GetAdapter( | 107 BluetoothAdapterFactory::GetAdapter( |
(...skipping 16 matching lines...) Expand all Loading... |
116 ASSERT_TRUE(adapter_->IsPresent()); | 124 ASSERT_TRUE(adapter_->IsPresent()); |
117 ASSERT_TRUE(adapter_->IsPowered()); | 125 ASSERT_TRUE(adapter_->IsPowered()); |
118 EXPECT_EQ(callback_count_, 1); | 126 EXPECT_EQ(callback_count_, 1); |
119 EXPECT_EQ(error_callback_count_, 0); | 127 EXPECT_EQ(error_callback_count_, 0); |
120 | 128 |
121 // Resets callback_count_. | 129 // Resets callback_count_. |
122 --callback_count_; | 130 --callback_count_; |
123 } | 131 } |
124 | 132 |
125 // Registers BluetoothAudioSinkChromeOS with default codec and capabilities. | 133 // Registers BluetoothAudioSinkChromeOS with default codec and capabilities. |
| 134 // If the audio sink is retrieved successfully, the state changes to |
| 135 // STATE_DISCONNECTED. |
126 void GetAudioSink() { | 136 void GetAudioSink() { |
127 // Sets up valid codec and capabilities. | 137 // Sets up valid codec and capabilities. |
128 BluetoothAudioSink::Options options; | 138 BluetoothAudioSink::Options options; |
129 ASSERT_EQ(options.codec, 0x00); | 139 ASSERT_EQ(options.codec, 0x00); |
130 ASSERT_EQ(options.capabilities, | 140 ASSERT_EQ(options.capabilities, |
131 std::vector<uint8_t>({0x3f, 0xff, 0x12, 0x35})); | 141 std::vector<uint8_t>({0x3f, 0xff, 0x12, 0x35})); |
132 | 142 |
133 // Registers |audio_sink_| with valid codec and capabilities | 143 // Registers |audio_sink_| with valid codec and capabilities |
134 adapter_->RegisterAudioSink( | 144 adapter_->RegisterAudioSink( |
135 options, | 145 options, |
136 base::Bind(&BluetoothAudioSinkChromeOSTest::RegisterCallback, | 146 base::Bind(&BluetoothAudioSinkChromeOSTest::RegisterCallback, |
137 base::Unretained(this)), | 147 base::Unretained(this)), |
138 base::Bind(&BluetoothAudioSinkChromeOSTest::RegisterErrorCallback, | 148 base::Bind(&BluetoothAudioSinkChromeOSTest::RegisterErrorCallback, |
139 base::Unretained(this))); | 149 base::Unretained(this))); |
140 | 150 |
| 151 observer_.reset(new TestAudioSinkObserver(audio_sink_)); |
141 EXPECT_EQ(callback_count_, 1); | 152 EXPECT_EQ(callback_count_, 1); |
142 EXPECT_EQ(error_callback_count_, 0); | 153 EXPECT_EQ(error_callback_count_, 0); |
| 154 EXPECT_EQ(observer_->state_changed_count_, 0); |
| 155 EXPECT_EQ(observer_->volume_changed_count_, 0); |
| 156 } |
| 157 |
| 158 void GetFakeMediaEndpoint() { |
| 159 BluetoothAudioSinkChromeOS* audio_sink_chromeos = |
| 160 static_cast<BluetoothAudioSinkChromeOS*>(audio_sink_.get()); |
| 161 ASSERT_NE(audio_sink_chromeos, nullptr); |
| 162 |
| 163 media_endpoint_ = static_cast<FakeBluetoothMediaEndpointServiceProvider*>( |
| 164 audio_sink_chromeos->GetEndpointServiceProvider()); |
143 } | 165 } |
144 | 166 |
145 // Called whenever RegisterAudioSink is completed successfully. | 167 // Called whenever RegisterAudioSink is completed successfully. |
146 void RegisterCallback( | 168 void RegisterCallback( |
147 scoped_refptr<BluetoothAudioSink> audio_sink) { | 169 scoped_refptr<BluetoothAudioSink> audio_sink) { |
148 ++callback_count_; | 170 ++callback_count_; |
149 audio_sink_ = audio_sink; | 171 audio_sink_ = audio_sink; |
| 172 |
| 173 GetFakeMediaEndpoint(); |
| 174 ASSERT_NE(media_endpoint_, nullptr); |
| 175 media_->SetEndpointRegistered(media_endpoint_, true); |
| 176 |
150 ASSERT_NE(audio_sink_.get(), nullptr); | 177 ASSERT_NE(audio_sink_.get(), nullptr); |
151 ASSERT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_DISCONNECTED); | 178 ASSERT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_DISCONNECTED); |
152 } | 179 } |
153 | 180 |
154 // Called whenever RegisterAudioSink failed. | 181 // Called whenever RegisterAudioSink failed. |
155 void RegisterErrorCallback(BluetoothAudioSink::ErrorCode error_code) { | 182 void RegisterErrorCallback(BluetoothAudioSink::ErrorCode error_code) { |
156 ++error_callback_count_; | 183 ++error_callback_count_; |
157 EXPECT_EQ(error_code, BluetoothAudioSink::ERROR_NOT_REGISTERED); | 184 EXPECT_EQ(error_code, BluetoothAudioSink::ERROR_NOT_REGISTERED); |
158 } | 185 } |
159 | 186 |
160 // Called whenever there capabilities are returned from SelectConfiguration. | 187 // Called whenever there capabilities are returned from SelectConfiguration. |
161 void SelectConfigurationCallback(const std::vector<uint8_t>& capabilities) { | 188 void SelectConfigurationCallback(const std::vector<uint8_t>& capabilities) { |
162 ++callback_count_; | 189 ++callback_count_; |
163 | 190 |
164 // |capabilities| should be the same as the capabilities for registering an | 191 // |capabilities| should be the same as the capabilities for registering an |
165 // audio sink in GetAudioSink(). | 192 // audio sink in GetAudioSink(). |
166 EXPECT_EQ(capabilities, std::vector<uint8_t>({0x3f, 0xff, 0x12, 0x35})); | 193 EXPECT_EQ(capabilities, std::vector<uint8_t>({0x3f, 0xff, 0x12, 0x35})); |
167 } | 194 } |
168 | 195 |
| 196 void UnregisterErrorCallback(BluetoothAudioSink::ErrorCode error_code) { |
| 197 ++error_callback_count_; |
| 198 EXPECT_EQ(error_code, BluetoothAudioSink::ERROR_NOT_UNREGISTERED); |
| 199 } |
| 200 |
169 // Generic callbacks. | 201 // Generic callbacks. |
170 void Callback() { | 202 void Callback() { |
171 ++callback_count_; | 203 ++callback_count_; |
172 } | 204 } |
173 | 205 |
174 void ErrorCallback() { | 206 void ErrorCallback() { |
175 ++error_callback_count_; | 207 ++error_callback_count_; |
176 } | 208 } |
177 | 209 |
178 protected: | 210 protected: |
179 int callback_count_; | 211 int callback_count_; |
180 int error_callback_count_; | 212 int error_callback_count_; |
181 | 213 |
182 base::MessageLoop message_loop_; | 214 base::MessageLoop message_loop_; |
183 | 215 |
| 216 FakeBluetoothMediaClient* media_; |
| 217 FakeBluetoothMediaTransportClient* transport_; |
| 218 FakeBluetoothMediaEndpointServiceProvider* media_endpoint_; |
| 219 scoped_ptr<TestAudioSinkObserver> observer_; |
184 scoped_refptr<BluetoothAdapter> adapter_; | 220 scoped_refptr<BluetoothAdapter> adapter_; |
185 scoped_refptr<BluetoothAudioSink> audio_sink_; | 221 scoped_refptr<BluetoothAudioSink> audio_sink_; |
186 | 222 |
187 // The default property set used while calling SetConfiguration on a media | 223 // The default property set used while calling SetConfiguration on a media |
188 // endpoint object. | 224 // endpoint object. |
189 BluetoothMediaEndpointServiceProvider::Delegate::TransportProperties | 225 BluetoothMediaEndpointServiceProvider::Delegate::TransportProperties |
190 properties_; | 226 properties_; |
191 }; | 227 }; |
192 | 228 |
193 TEST_F(BluetoothAudioSinkChromeOSTest, RegisterSucceeded) { | 229 TEST_F(BluetoothAudioSinkChromeOSTest, RegisterSucceeded) { |
194 GetAudioSink(); | 230 GetAudioSink(); |
195 | |
196 // Adds an observer for |audio_sink_|. | |
197 TestAudioSinkObserver observer(audio_sink_); | |
198 EXPECT_EQ(observer.state_changed_count_, 0); | |
199 EXPECT_EQ(observer.volume_changed_count_, 0); | |
200 } | 231 } |
201 | 232 |
202 TEST_F(BluetoothAudioSinkChromeOSTest, RegisterFailedWithInvalidOptions) { | 233 TEST_F(BluetoothAudioSinkChromeOSTest, RegisterFailedWithInvalidOptions) { |
203 // Sets options with an invalid codec and valid capabilities. | 234 // Sets options with an invalid codec and valid capabilities. |
204 BluetoothAudioSink::Options options; | 235 BluetoothAudioSink::Options options; |
205 options.codec = 0xff; | 236 options.codec = 0xff; |
206 options.capabilities = std::vector<uint8_t>({0x3f, 0xff, 0x12, 0x35}); | 237 options.capabilities = std::vector<uint8_t>({0x3f, 0xff, 0x12, 0x35}); |
207 | 238 |
208 adapter_->RegisterAudioSink( | 239 adapter_->RegisterAudioSink( |
209 options, | 240 options, |
(...skipping 15 matching lines...) Expand all Loading... |
225 base::Bind(&BluetoothAudioSinkChromeOSTest::RegisterErrorCallback, | 256 base::Bind(&BluetoothAudioSinkChromeOSTest::RegisterErrorCallback, |
226 base::Unretained(this))); | 257 base::Unretained(this))); |
227 | 258 |
228 EXPECT_EQ(callback_count_, 0); | 259 EXPECT_EQ(callback_count_, 0); |
229 EXPECT_EQ(error_callback_count_, 2); | 260 EXPECT_EQ(error_callback_count_, 2); |
230 } | 261 } |
231 | 262 |
232 TEST_F(BluetoothAudioSinkChromeOSTest, SelectConfiguration) { | 263 TEST_F(BluetoothAudioSinkChromeOSTest, SelectConfiguration) { |
233 GetAudioSink(); | 264 GetAudioSink(); |
234 | 265 |
235 // Adds an observer for |audio_sink_|. | |
236 TestAudioSinkObserver observer(audio_sink_); | |
237 EXPECT_EQ(observer.state_changed_count_, 0); | |
238 EXPECT_EQ(observer.volume_changed_count_, 0); | |
239 | |
240 // Simulates calling SelectConfiguration on the media endpoint object owned by | 266 // Simulates calling SelectConfiguration on the media endpoint object owned by |
241 // |audio_sink_| with some fake capabilities. | 267 // |audio_sink_| with some fake capabilities. |
242 BluetoothAudioSinkChromeOS* audio_sink_chromeos = | 268 media_endpoint_->SelectConfiguration( |
243 static_cast<BluetoothAudioSinkChromeOS*>(audio_sink_.get()); | |
244 FakeBluetoothMediaEndpointServiceProvider* media_endpoint = | |
245 static_cast<FakeBluetoothMediaEndpointServiceProvider*>( | |
246 audio_sink_chromeos->GetEndpointServiceProvider()); | |
247 ASSERT_NE(media_endpoint, nullptr); | |
248 | |
249 media_endpoint->SelectConfiguration( | |
250 std::vector<uint8_t>({0x21, 0x15, 0x33, 0x2C}), | 269 std::vector<uint8_t>({0x21, 0x15, 0x33, 0x2C}), |
251 base::Bind(&BluetoothAudioSinkChromeOSTest::SelectConfigurationCallback, | 270 base::Bind(&BluetoothAudioSinkChromeOSTest::SelectConfigurationCallback, |
252 base::Unretained(this))); | 271 base::Unretained(this))); |
253 | 272 |
254 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_DISCONNECTED); | 273 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_DISCONNECTED); |
255 EXPECT_EQ(callback_count_, 2); | 274 EXPECT_EQ(callback_count_, 2); |
256 EXPECT_EQ(error_callback_count_, 0); | 275 EXPECT_EQ(error_callback_count_, 0); |
257 EXPECT_EQ(observer.state_changed_count_, 0); | 276 EXPECT_EQ(observer_->state_changed_count_, 0); |
258 EXPECT_EQ(observer.volume_changed_count_, 0); | 277 EXPECT_EQ(observer_->volume_changed_count_, 0); |
259 } | 278 } |
260 | 279 |
261 TEST_F(BluetoothAudioSinkChromeOSTest, SetConfiguration) { | 280 TEST_F(BluetoothAudioSinkChromeOSTest, SetConfiguration) { |
262 GetAudioSink(); | 281 GetAudioSink(); |
263 | 282 |
264 // Adds an observer for |audio_sink_|. | 283 media_endpoint_->SelectConfiguration( |
265 TestAudioSinkObserver observer(audio_sink_); | 284 std::vector<uint8_t>({0x21, 0x15, 0x33, 0x2C}), |
266 EXPECT_EQ(observer.state_changed_count_, 0); | 285 base::Bind(&BluetoothAudioSinkChromeOSTest::SelectConfigurationCallback, |
267 EXPECT_EQ(observer.volume_changed_count_, 0); | 286 base::Unretained(this))); |
| 287 |
| 288 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_DISCONNECTED); |
| 289 EXPECT_EQ(callback_count_, 2); |
| 290 EXPECT_EQ(error_callback_count_, 0); |
| 291 EXPECT_EQ(observer_->state_changed_count_, 0); |
| 292 EXPECT_EQ(observer_->volume_changed_count_, 0); |
268 | 293 |
269 // Simulates calling SetConfiguration on the media endpoint object owned by | 294 // Simulates calling SetConfiguration on the media endpoint object owned by |
270 // |audio_sink_| with a fake transport path and a | 295 // |audio_sink_| with a fake transport path and a |
271 // Delegate::TransportProperties structure. | 296 // Delegate::TransportProperties structure. |
272 BluetoothAudioSinkChromeOS* audio_sink_chromeos = | 297 media_endpoint_->SetConfiguration( |
273 static_cast<BluetoothAudioSinkChromeOS*>(audio_sink_.get()); | 298 transport_->GetTransportPath(media_endpoint_->object_path()), |
274 FakeBluetoothMediaEndpointServiceProvider* media_endpoint = | 299 properties_); |
275 static_cast<FakeBluetoothMediaEndpointServiceProvider*>( | 300 |
276 audio_sink_chromeos->GetEndpointServiceProvider()); | 301 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_IDLE); |
277 ASSERT_NE(media_endpoint, nullptr); | 302 EXPECT_EQ(callback_count_, 2); |
278 | 303 EXPECT_EQ(error_callback_count_, 0); |
279 media_endpoint->SetConfiguration( | 304 EXPECT_EQ(observer_->state_changed_count_, 1); |
280 ObjectPath(FakeBluetoothMediaTransportClient::kTransportPath), | 305 EXPECT_EQ(observer_->volume_changed_count_, 1); |
281 properties_); | |
282 | |
283 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_IDLE); | |
284 EXPECT_EQ(callback_count_, 1); | |
285 EXPECT_EQ(error_callback_count_, 0); | |
286 EXPECT_EQ(observer.state_changed_count_, 1); | |
287 EXPECT_EQ(observer.volume_changed_count_, 1); | |
288 } | 306 } |
289 | 307 |
290 TEST_F(BluetoothAudioSinkChromeOSTest, SetConfigurationWithUnexpectedState) { | 308 TEST_F(BluetoothAudioSinkChromeOSTest, SetConfigurationWithUnexpectedState) { |
291 GetAudioSink(); | 309 GetAudioSink(); |
292 | 310 |
293 // Adds an observer for |audio_sink_|. | 311 media_endpoint_->SelectConfiguration( |
294 TestAudioSinkObserver observer(audio_sink_); | 312 std::vector<uint8_t>({0x21, 0x15, 0x33, 0x2C}), |
295 EXPECT_EQ(observer.state_changed_count_, 0); | 313 base::Bind(&BluetoothAudioSinkChromeOSTest::SelectConfigurationCallback, |
296 EXPECT_EQ(observer.volume_changed_count_, 0); | 314 base::Unretained(this))); |
297 | 315 |
298 // Simulates calling SetConfiguration on the media endpoint object owned by | 316 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_DISCONNECTED); |
299 // |audio_sink_| with a fake transport path and a | 317 EXPECT_EQ(callback_count_, 2); |
300 // Delegate::TransportProperties structure. | 318 EXPECT_EQ(error_callback_count_, 0); |
301 BluetoothAudioSinkChromeOS* audio_sink_chromeos = | 319 EXPECT_EQ(observer_->state_changed_count_, 0); |
302 static_cast<BluetoothAudioSinkChromeOS*>(audio_sink_.get()); | 320 EXPECT_EQ(observer_->volume_changed_count_, 0); |
303 FakeBluetoothMediaEndpointServiceProvider* media_endpoint = | |
304 static_cast<FakeBluetoothMediaEndpointServiceProvider*>( | |
305 audio_sink_chromeos->GetEndpointServiceProvider()); | |
306 ASSERT_NE(media_endpoint, nullptr); | |
307 | 321 |
308 // Set state of Delegate::TransportProperties with an unexpected value. | 322 // Set state of Delegate::TransportProperties with an unexpected value. |
309 properties_.state = "active"; | 323 properties_.state = "pending"; |
310 | 324 |
311 media_endpoint->SetConfiguration( | 325 media_endpoint_->SetConfiguration( |
312 ObjectPath(FakeBluetoothMediaTransportClient::kTransportPath), | 326 transport_->GetTransportPath(media_endpoint_->object_path()), |
313 properties_); | 327 properties_); |
314 | 328 |
315 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_DISCONNECTED); | 329 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_DISCONNECTED); |
316 EXPECT_EQ(callback_count_, 1); | 330 EXPECT_EQ(callback_count_, 2); |
317 EXPECT_EQ(error_callback_count_, 0); | 331 EXPECT_EQ(error_callback_count_, 0); |
318 EXPECT_EQ(observer.state_changed_count_, 0); | 332 EXPECT_EQ(observer_->state_changed_count_, 0); |
319 EXPECT_EQ(observer.volume_changed_count_, 0); | 333 EXPECT_EQ(observer_->volume_changed_count_, 0); |
320 } | 334 } |
321 | 335 |
322 // Checks if the observer is notified while the media object is | 336 // TODO(mcchou): Adds test on media-removed events for STATE_PENDING and |
323 // removed(invisible). Once the media object is removed, the audio sink is no | 337 // STATE_ACTIVE. |
324 // longer valid. | 338 // Checks if the observer is notified on media-removed event when the state of |
325 TEST_F(BluetoothAudioSinkChromeOSTest, ObserverNotifiedWhenMediaRemoved) { | 339 // |audio_sink_| is STATE_DISCONNECTED. Once the media object is removed, the |
326 GetAudioSink(); | 340 // audio sink is no longer valid. |
327 | 341 TEST_F(BluetoothAudioSinkChromeOSTest, MediaRemovedDuringDisconnectedState) { |
328 // Adds an observer for |audio_sink_|. | 342 GetAudioSink(); |
329 TestAudioSinkObserver observer(audio_sink_); | |
330 EXPECT_EQ(observer.state_changed_count_, 0); | |
331 EXPECT_EQ(observer.volume_changed_count_, 0); | |
332 | 343 |
333 // Gets the media object and makes it invisible to see if the state of the | 344 // Gets the media object and makes it invisible to see if the state of the |
334 // audio sink changes accordingly. | 345 // audio sink changes accordingly. |
335 FakeBluetoothMediaClient* media = static_cast<FakeBluetoothMediaClient*>( | 346 media_->SetVisible(false); |
336 DBusThreadManager::Get()->GetBluetoothMediaClient()); | 347 |
337 media->SetVisible(false); | 348 GetFakeMediaEndpoint(); |
338 | |
339 BluetoothAudioSinkChromeOS* audio_sink_chromeos = | |
340 static_cast<BluetoothAudioSinkChromeOS*>(audio_sink_.get()); | |
341 FakeBluetoothMediaEndpointServiceProvider* media_endpoint = | |
342 static_cast<FakeBluetoothMediaEndpointServiceProvider*>( | |
343 audio_sink_chromeos->GetEndpointServiceProvider()); | |
344 | 349 |
345 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_INVALID); | 350 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_INVALID); |
346 EXPECT_EQ(media_endpoint, nullptr); | 351 EXPECT_EQ(media_endpoint_, nullptr); |
347 } | 352 EXPECT_EQ(observer_->state_changed_count_, 1); |
348 | 353 EXPECT_EQ(observer_->volume_changed_count_, 0); |
349 // Checks if the observer is notified while the media transport is | 354 } |
350 // removed(invisible). Once the media transport object is removed, the audio | 355 |
351 // sink is disconnected. | 356 // Checks if the observer is notified on media-removed event when the state of |
352 TEST_F(BluetoothAudioSinkChromeOSTest, ObserverNotifiedWhenTransportRemoved) { | 357 // |audio_sink_| is STATE_IDLE. Once the media object is removed, the audio sink |
353 GetAudioSink(); | 358 // is no longer valid. |
354 | 359 TEST_F(BluetoothAudioSinkChromeOSTest, MediaRemovedDuringIdleState) { |
355 // Adds an observer for |audio_sink_|. | 360 GetAudioSink(); |
356 TestAudioSinkObserver observer(audio_sink_); | 361 |
357 EXPECT_EQ(observer.state_changed_count_, 0); | 362 media_endpoint_->SelectConfiguration( |
358 EXPECT_EQ(observer.volume_changed_count_, 0); | 363 std::vector<uint8_t>({0x21, 0x15, 0x33, 0x2C}), |
359 | 364 base::Bind(&BluetoothAudioSinkChromeOSTest::SelectConfigurationCallback, |
360 // Simulates calling SetConfiguration on the media endpoint object owned by | 365 base::Unretained(this))); |
361 // |audio_sink_| with a fake transport path and a | 366 |
362 // Delegate::TransportProperties structure. | 367 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_DISCONNECTED); |
363 BluetoothAudioSinkChromeOS* audio_sink_chromeos = | 368 EXPECT_EQ(callback_count_, 2); |
364 static_cast<BluetoothAudioSinkChromeOS*>(audio_sink_.get()); | 369 EXPECT_EQ(error_callback_count_, 0); |
365 FakeBluetoothMediaEndpointServiceProvider* media_endpoint = | 370 EXPECT_EQ(observer_->state_changed_count_, 0); |
366 static_cast<FakeBluetoothMediaEndpointServiceProvider*>( | 371 EXPECT_EQ(observer_->volume_changed_count_, 0); |
367 audio_sink_chromeos->GetEndpointServiceProvider()); | 372 |
368 ASSERT_NE(media_endpoint, nullptr); | 373 media_endpoint_->SetConfiguration( |
369 | 374 transport_->GetTransportPath(media_endpoint_->object_path()), |
370 media_endpoint->SetConfiguration( | 375 properties_); |
371 ObjectPath(FakeBluetoothMediaTransportClient::kTransportPath), | 376 |
372 properties_); | 377 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_IDLE); |
373 | 378 EXPECT_EQ(callback_count_, 2); |
374 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_IDLE); | 379 EXPECT_EQ(error_callback_count_, 0); |
375 EXPECT_EQ(callback_count_, 1); | 380 EXPECT_EQ(observer_->state_changed_count_, 1); |
376 EXPECT_EQ(error_callback_count_, 0); | 381 EXPECT_EQ(observer_->volume_changed_count_, 1); |
377 EXPECT_EQ(observer.state_changed_count_, 1); | 382 |
378 EXPECT_EQ(observer.volume_changed_count_, 1); | 383 // Gets the media object and makes it invisible to see if the state of the |
379 | 384 // audio sink changes accordingly. |
380 // Gets the media transport object and makes it invalid to see if the state | 385 media_->SetVisible(false); |
381 // of the audio sink changes accordingly. | 386 |
382 FakeBluetoothMediaTransportClient* transport = | 387 GetFakeMediaEndpoint(); |
383 static_cast<FakeBluetoothMediaTransportClient*>( | 388 |
384 DBusThreadManager::Get()->GetBluetoothMediaTransportClient()); | 389 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_INVALID); |
385 | 390 EXPECT_EQ(media_endpoint_, nullptr); |
386 transport->SetValid(media_endpoint->object_path(), false); | 391 |
387 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_DISCONNECTED); | 392 // The state becomes disconnted and then invalid, since the removal of |
388 EXPECT_NE(media_endpoint, nullptr); | 393 // transport object should happend before media becomes invisible. |
| 394 // State: STATE_IDLE -> STATE_DISCONNECTED -> STATE_INVALID |
| 395 EXPECT_EQ(observer_->state_changed_count_, 3); |
| 396 EXPECT_EQ(observer_->volume_changed_count_, 2); |
| 397 } |
| 398 |
| 399 // TODO(mcchou): Add tests on transport-removed event for STATE_PENDING and |
| 400 // STATE_ACTIVE. |
| 401 // Checks if the observer is notified on transport-removed event when the state |
| 402 // of |audio_sink_| is STATE_IDEL. Once the media transport object is removed, |
| 403 // the audio sink is disconnected. |
| 404 TEST_F(BluetoothAudioSinkChromeOSTest, TransportRemovedDuringIdleState) { |
| 405 GetAudioSink(); |
| 406 |
| 407 media_endpoint_->SelectConfiguration( |
| 408 std::vector<uint8_t>({0x21, 0x15, 0x33, 0x2C}), |
| 409 base::Bind(&BluetoothAudioSinkChromeOSTest::SelectConfigurationCallback, |
| 410 base::Unretained(this))); |
| 411 |
| 412 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_DISCONNECTED); |
| 413 EXPECT_EQ(callback_count_, 2); |
| 414 EXPECT_EQ(error_callback_count_, 0); |
| 415 EXPECT_EQ(observer_->state_changed_count_, 0); |
| 416 EXPECT_EQ(observer_->volume_changed_count_, 0); |
| 417 |
| 418 media_endpoint_->SetConfiguration( |
| 419 transport_->GetTransportPath(media_endpoint_->object_path()), |
| 420 properties_); |
| 421 |
| 422 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_IDLE); |
| 423 EXPECT_EQ(callback_count_, 2); |
| 424 EXPECT_EQ(error_callback_count_, 0); |
| 425 EXPECT_EQ(observer_->state_changed_count_, 1); |
| 426 EXPECT_EQ(observer_->volume_changed_count_, 1); |
| 427 |
| 428 // Makes the transport object invalid to see if the state of the audio sink |
| 429 // changes accordingly. |
| 430 transport_->SetValid(media_endpoint_, false); |
| 431 |
| 432 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_DISCONNECTED); |
| 433 EXPECT_NE(media_endpoint_, nullptr); |
| 434 EXPECT_EQ(observer_->state_changed_count_, 2); |
| 435 EXPECT_EQ(observer_->volume_changed_count_, 2); |
| 436 } |
| 437 |
| 438 TEST_F(BluetoothAudioSinkChromeOSTest, |
| 439 AdapterPoweredChangedDuringDisconnectedState) { |
| 440 GetAudioSink(); |
| 441 |
| 442 adapter_->SetPowered( |
| 443 false, |
| 444 base::Bind(&BluetoothAudioSinkChromeOSTest::Callback, |
| 445 base::Unretained(this)), |
| 446 base::Bind(&BluetoothAudioSinkChromeOSTest::ErrorCallback, |
| 447 base::Unretained(this))); |
| 448 |
| 449 EXPECT_TRUE(adapter_->IsPresent()); |
| 450 EXPECT_FALSE(adapter_->IsPowered()); |
| 451 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_DISCONNECTED); |
| 452 EXPECT_EQ(callback_count_, 2); |
| 453 EXPECT_EQ(error_callback_count_, 0); |
| 454 EXPECT_EQ(observer_->state_changed_count_, 0); |
| 455 EXPECT_EQ(observer_->volume_changed_count_, 0); |
| 456 |
| 457 adapter_->SetPowered( |
| 458 true, |
| 459 base::Bind(&BluetoothAudioSinkChromeOSTest::Callback, |
| 460 base::Unretained(this)), |
| 461 base::Bind(&BluetoothAudioSinkChromeOSTest::ErrorCallback, |
| 462 base::Unretained(this))); |
| 463 |
| 464 EXPECT_TRUE(adapter_->IsPresent()); |
| 465 EXPECT_TRUE(adapter_->IsPowered()); |
| 466 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_DISCONNECTED); |
| 467 EXPECT_EQ(callback_count_, 3); |
| 468 EXPECT_EQ(error_callback_count_, 0); |
| 469 EXPECT_EQ(observer_->state_changed_count_, 0); |
| 470 EXPECT_EQ(observer_->volume_changed_count_, 0); |
| 471 } |
| 472 |
| 473 TEST_F(BluetoothAudioSinkChromeOSTest, AdapterPoweredChangedDuringIdleState) { |
| 474 GetAudioSink(); |
| 475 |
| 476 media_endpoint_->SelectConfiguration( |
| 477 std::vector<uint8_t>({0x21, 0x15, 0x33, 0x2C}), |
| 478 base::Bind(&BluetoothAudioSinkChromeOSTest::SelectConfigurationCallback, |
| 479 base::Unretained(this))); |
| 480 |
| 481 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_DISCONNECTED); |
| 482 EXPECT_EQ(callback_count_, 2); |
| 483 EXPECT_EQ(error_callback_count_, 0); |
| 484 EXPECT_EQ(observer_->state_changed_count_, 0); |
| 485 EXPECT_EQ(observer_->volume_changed_count_, 0); |
| 486 |
| 487 media_endpoint_->SetConfiguration( |
| 488 transport_->GetTransportPath(media_endpoint_->object_path()), |
| 489 properties_); |
| 490 |
| 491 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_IDLE); |
| 492 EXPECT_EQ(callback_count_, 2); |
| 493 EXPECT_EQ(error_callback_count_, 0); |
| 494 EXPECT_EQ(observer_->state_changed_count_, 1); |
| 495 EXPECT_EQ(observer_->volume_changed_count_, 1); |
| 496 |
| 497 adapter_->SetPowered( |
| 498 false, |
| 499 base::Bind(&BluetoothAudioSinkChromeOSTest::Callback, |
| 500 base::Unretained(this)), |
| 501 base::Bind(&BluetoothAudioSinkChromeOSTest::ErrorCallback, |
| 502 base::Unretained(this))); |
| 503 GetFakeMediaEndpoint(); |
| 504 |
| 505 EXPECT_TRUE(adapter_->IsPresent()); |
| 506 EXPECT_FALSE(adapter_->IsPowered()); |
| 507 EXPECT_NE(media_endpoint_, nullptr); |
| 508 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_DISCONNECTED); |
| 509 EXPECT_EQ(callback_count_, 3); |
| 510 EXPECT_EQ(error_callback_count_, 0); |
| 511 EXPECT_EQ(observer_->state_changed_count_, 2); |
| 512 EXPECT_EQ(observer_->volume_changed_count_, 2); |
| 513 } |
| 514 |
| 515 // TODO(mcchou): Add tests on UnregisterAudioSink for STATE_PENDING and |
| 516 // STATE_ACTIVE. |
| 517 |
| 518 TEST_F(BluetoothAudioSinkChromeOSTest, |
| 519 UnregisterAudioSinkDuringDisconnectedState) { |
| 520 GetAudioSink(); |
| 521 |
| 522 audio_sink_->Unregister( |
| 523 base::Bind(&BluetoothAudioSinkChromeOSTest::Callback, |
| 524 base::Unretained(this)), |
| 525 base::Bind(&BluetoothAudioSinkChromeOSTest::UnregisterErrorCallback, |
| 526 base::Unretained(this))); |
| 527 |
| 528 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_INVALID); |
| 529 EXPECT_EQ(callback_count_, 2); |
| 530 EXPECT_EQ(error_callback_count_, 0); |
| 531 EXPECT_EQ(observer_->state_changed_count_, 1); |
| 532 EXPECT_EQ(observer_->volume_changed_count_, 0); |
| 533 } |
| 534 |
| 535 TEST_F(BluetoothAudioSinkChromeOSTest, UnregisterAudioSinkDuringIdleState) { |
| 536 GetAudioSink(); |
| 537 |
| 538 media_endpoint_->SelectConfiguration( |
| 539 std::vector<uint8_t>({0x21, 0x15, 0x33, 0x2C}), |
| 540 base::Bind(&BluetoothAudioSinkChromeOSTest::SelectConfigurationCallback, |
| 541 base::Unretained(this))); |
| 542 |
| 543 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_DISCONNECTED); |
| 544 EXPECT_EQ(callback_count_, 2); |
| 545 EXPECT_EQ(error_callback_count_, 0); |
| 546 EXPECT_EQ(observer_->state_changed_count_, 0); |
| 547 EXPECT_EQ(observer_->volume_changed_count_, 0); |
| 548 |
| 549 media_endpoint_->SetConfiguration( |
| 550 transport_->GetTransportPath(media_endpoint_->object_path()), |
| 551 properties_); |
| 552 |
| 553 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_IDLE); |
| 554 EXPECT_EQ(callback_count_, 2); |
| 555 EXPECT_EQ(error_callback_count_, 0); |
| 556 EXPECT_EQ(observer_->state_changed_count_, 1); |
| 557 EXPECT_EQ(observer_->volume_changed_count_, 1); |
| 558 |
| 559 audio_sink_->Unregister( |
| 560 base::Bind(&BluetoothAudioSinkChromeOSTest::Callback, |
| 561 base::Unretained(this)), |
| 562 base::Bind(&BluetoothAudioSinkChromeOSTest::UnregisterErrorCallback, |
| 563 base::Unretained(this))); |
| 564 |
| 565 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_INVALID); |
| 566 EXPECT_EQ(callback_count_, 3); |
| 567 EXPECT_EQ(error_callback_count_, 0); |
| 568 |
| 569 // The state becomes disconnted and then invalid, since the removal of |
| 570 // transport object should happend before the unregistration of endpoint. |
| 571 // State: STATE_IDLE -> STATE_DISCONNECTED -> STATE_INVALID |
| 572 EXPECT_EQ(observer_->state_changed_count_, 3); |
| 573 EXPECT_EQ(observer_->volume_changed_count_, 2); |
| 574 } |
| 575 |
| 576 TEST_F(BluetoothAudioSinkChromeOSTest, StateChanged) { |
| 577 GetAudioSink(); |
| 578 |
| 579 media_endpoint_->SelectConfiguration( |
| 580 std::vector<uint8_t>({0x21, 0x15, 0x33, 0x2C}), |
| 581 base::Bind(&BluetoothAudioSinkChromeOSTest::SelectConfigurationCallback, |
| 582 base::Unretained(this))); |
| 583 |
| 584 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_DISCONNECTED); |
| 585 EXPECT_EQ(callback_count_, 2); |
| 586 EXPECT_EQ(error_callback_count_, 0); |
| 587 EXPECT_EQ(observer_->state_changed_count_, 0); |
| 588 EXPECT_EQ(observer_->volume_changed_count_, 0); |
| 589 |
| 590 media_endpoint_->SetConfiguration( |
| 591 transport_->GetTransportPath(media_endpoint_->object_path()), |
| 592 properties_); |
| 593 |
| 594 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_IDLE); |
| 595 EXPECT_EQ(callback_count_, 2); |
| 596 EXPECT_EQ(error_callback_count_, 0); |
| 597 EXPECT_EQ(observer_->state_changed_count_, 1); |
| 598 EXPECT_EQ(observer_->volume_changed_count_, 1); |
| 599 |
| 600 // Changes the current state of transport to pending. |
| 601 transport_->SetState(media_endpoint_->object_path(), "pending"); |
| 602 |
| 603 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_PENDING); |
| 604 EXPECT_EQ(observer_->state_changed_count_, 2); |
| 605 EXPECT_EQ(observer_->volume_changed_count_, 1); |
| 606 } |
| 607 |
| 608 TEST_F(BluetoothAudioSinkChromeOSTest, VolumeChanged) { |
| 609 GetAudioSink(); |
| 610 |
| 611 media_endpoint_->SelectConfiguration( |
| 612 std::vector<uint8_t>({0x21, 0x15, 0x33, 0x2C}), |
| 613 base::Bind(&BluetoothAudioSinkChromeOSTest::SelectConfigurationCallback, |
| 614 base::Unretained(this))); |
| 615 |
| 616 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_DISCONNECTED); |
| 617 EXPECT_EQ(callback_count_, 2); |
| 618 EXPECT_EQ(error_callback_count_, 0); |
| 619 EXPECT_EQ(observer_->state_changed_count_, 0); |
| 620 EXPECT_EQ(observer_->volume_changed_count_, 0); |
| 621 |
| 622 media_endpoint_->SetConfiguration( |
| 623 transport_->GetTransportPath(media_endpoint_->object_path()), |
| 624 properties_); |
| 625 |
| 626 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_IDLE); |
| 627 EXPECT_EQ(callback_count_, 2); |
| 628 EXPECT_EQ(error_callback_count_, 0); |
| 629 EXPECT_EQ(observer_->state_changed_count_, 1); |
| 630 EXPECT_EQ(observer_->volume_changed_count_, 1); |
| 631 |
| 632 // |kTransportVolume| is the initial volume of the transport, and this |
| 633 // value is propagated to the audio sink via SetConfiguration. |
| 634 EXPECT_EQ(audio_sink_->GetVolume(), |
| 635 FakeBluetoothMediaTransportClient::kTransportVolume); |
| 636 |
| 637 // Changes volume to a valid level. |
| 638 transport_->SetVolume(media_endpoint_->object_path(), 100); |
| 639 |
| 640 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_IDLE); |
| 641 EXPECT_EQ(observer_->state_changed_count_, 1); |
| 642 EXPECT_EQ(observer_->volume_changed_count_, 2); |
| 643 EXPECT_EQ(audio_sink_->GetVolume(), 100); |
| 644 |
| 645 // Changes volume to an invalid level. |
| 646 transport_->SetVolume(media_endpoint_->object_path(), 200); |
| 647 |
| 648 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_IDLE); |
| 649 EXPECT_EQ(observer_->state_changed_count_, 1); |
| 650 EXPECT_EQ(observer_->volume_changed_count_, 3); |
| 651 EXPECT_EQ(audio_sink_->GetVolume(), BluetoothAudioSink::kInvalidVolume); |
389 } | 652 } |
390 | 653 |
391 } // namespace chromeos | 654 } // namespace chromeos |
OLD | NEW |