Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(220)

Side by Side Diff: chromeos/dbus/cras_audio_client_unittest.cc

Issue 1952983002: Listen for OutputNodeVolumeChanged signal (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix unimplemented method Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « chromeos/dbus/cras_audio_client.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 "chromeos/dbus/cras_audio_client.h" 5 #include "chromeos/dbus/cras_audio_client.h"
6 6
7 #include <memory> 7 #include <memory>
8 #include <string> 8 #include <string>
9 #include <vector> 9 #include <vector>
10 10
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
62 // A mock CrasAudioClient Observer. 62 // A mock CrasAudioClient Observer.
63 class MockObserver : public CrasAudioClient::Observer { 63 class MockObserver : public CrasAudioClient::Observer {
64 public: 64 public:
65 MockObserver() {} 65 MockObserver() {}
66 ~MockObserver() override {} 66 ~MockObserver() override {}
67 MOCK_METHOD1(OutputMuteChanged, void(bool mute_on)); 67 MOCK_METHOD1(OutputMuteChanged, void(bool mute_on));
68 MOCK_METHOD1(InputMuteChanged, void(bool mute_on)); 68 MOCK_METHOD1(InputMuteChanged, void(bool mute_on));
69 MOCK_METHOD0(NodesChanged, void()); 69 MOCK_METHOD0(NodesChanged, void());
70 MOCK_METHOD1(ActiveOutputNodeChanged, void(uint64_t node_id)); 70 MOCK_METHOD1(ActiveOutputNodeChanged, void(uint64_t node_id));
71 MOCK_METHOD1(ActiveInputNodeChanged, void(uint64_t node_id)); 71 MOCK_METHOD1(ActiveInputNodeChanged, void(uint64_t node_id));
72 MOCK_METHOD2(OutputNodeVolumeChanged, void(uint64_t node_id, int volume));
72 }; 73 };
73 74
74 // Expect the reader to be empty. 75 // Expect the reader to be empty.
75 void ExpectNoArgument(dbus::MessageReader* reader) { 76 void ExpectNoArgument(dbus::MessageReader* reader) {
76 EXPECT_FALSE(reader->HasMoreData()); 77 EXPECT_FALSE(reader->HasMoreData());
77 } 78 }
78 79
79 // Expect the reader to have an int32_t and an array of doubles. 80 // Expect the reader to have an int32_t and an array of doubles.
80 void ExpectInt32AndArrayOfDoublesArguments( 81 void ExpectInt32AndArrayOfDoublesArguments(
81 int32_t expected_value, 82 int32_t expected_value,
(...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after
274 // Set an expectation so mock_cras_proxy's monitoring 275 // Set an expectation so mock_cras_proxy's monitoring
275 // ActiveInputNodeChanged ConnectToSignal will use 276 // ActiveInputNodeChanged ConnectToSignal will use
276 // OnConnectToActiveInputNodeChanged() to run the callback. 277 // OnConnectToActiveInputNodeChanged() to run the callback.
277 EXPECT_CALL( 278 EXPECT_CALL(
278 *mock_cras_proxy_.get(), 279 *mock_cras_proxy_.get(),
279 ConnectToSignal(interface_name_, cras::kActiveInputNodeChanged, _, _)) 280 ConnectToSignal(interface_name_, cras::kActiveInputNodeChanged, _, _))
280 .WillRepeatedly( 281 .WillRepeatedly(
281 Invoke(this, 282 Invoke(this,
282 &CrasAudioClientTest::OnConnectToActiveInputNodeChanged)); 283 &CrasAudioClientTest::OnConnectToActiveInputNodeChanged));
283 284
285 // Set an expectation so mock_cras_proxy's monitoring
286 // OutputNodeVolumeChanged ConnectToSignal will use
287 // OnConnectToOutputNodeVolumeChanged() to run the callback.
288 EXPECT_CALL(
289 *mock_cras_proxy_.get(),
290 ConnectToSignal(interface_name_, cras::kOutputNodeVolumeChanged, _, _))
291 .WillRepeatedly(
292 Invoke(this,
293 &CrasAudioClientTest::OnConnectToOutputNodeVolumeChanged));
294
284 // Set an expectation so mock_bus's GetObjectProxy() for the given 295 // Set an expectation so mock_bus's GetObjectProxy() for the given
285 // service name and the object path will return mock_cras_proxy_. 296 // service name and the object path will return mock_cras_proxy_.
286 EXPECT_CALL(*mock_bus_.get(), 297 EXPECT_CALL(*mock_bus_.get(),
287 GetObjectProxy(cras::kCrasServiceName, 298 GetObjectProxy(cras::kCrasServiceName,
288 dbus::ObjectPath(cras::kCrasServicePath))) 299 dbus::ObjectPath(cras::kCrasServicePath)))
289 .WillOnce(Return(mock_cras_proxy_.get())); 300 .WillOnce(Return(mock_cras_proxy_.get()));
290 301
291 // ShutdownAndBlock() will be called in TearDown(). 302 // ShutdownAndBlock() will be called in TearDown().
292 EXPECT_CALL(*mock_bus_.get(), ShutdownAndBlock()).WillOnce(Return()); 303 EXPECT_CALL(*mock_bus_.get(), ShutdownAndBlock()).WillOnce(Return());
293 304
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
337 ASSERT_FALSE(active_output_node_changed_handler_.is_null()); 348 ASSERT_FALSE(active_output_node_changed_handler_.is_null());
338 active_output_node_changed_handler_.Run(signal); 349 active_output_node_changed_handler_.Run(signal);
339 } 350 }
340 351
341 // Send active input node changed signal to the tested client. 352 // Send active input node changed signal to the tested client.
342 void SendActiveInputNodeChangedSignal(dbus::Signal* signal) { 353 void SendActiveInputNodeChangedSignal(dbus::Signal* signal) {
343 ASSERT_FALSE(active_input_node_changed_handler_.is_null()); 354 ASSERT_FALSE(active_input_node_changed_handler_.is_null());
344 active_input_node_changed_handler_.Run(signal); 355 active_input_node_changed_handler_.Run(signal);
345 } 356 }
346 357
358 // Send output node volume changed signal to the tested client.
359 void SendOutputNodeVolumeChangedSignal(dbus::Signal *signal) {
360 ASSERT_FALSE(output_node_volume_changed_handler_.is_null());
361 output_node_volume_changed_handler_.Run(signal);
362 }
363
347 // The interface name. 364 // The interface name.
348 const std::string interface_name_; 365 const std::string interface_name_;
349 // The client to be tested. 366 // The client to be tested.
350 std::unique_ptr<CrasAudioClient> client_; 367 std::unique_ptr<CrasAudioClient> client_;
351 // A message loop to emulate asynchronous behavior. 368 // A message loop to emulate asynchronous behavior.
352 base::MessageLoop message_loop_; 369 base::MessageLoop message_loop_;
353 // The mock bus. 370 // The mock bus.
354 scoped_refptr<dbus::MockBus> mock_bus_; 371 scoped_refptr<dbus::MockBus> mock_bus_;
355 // The mock object proxy. 372 // The mock object proxy.
356 scoped_refptr<dbus::MockObjectProxy> mock_cras_proxy_; 373 scoped_refptr<dbus::MockObjectProxy> mock_cras_proxy_;
357 // The OutputMuteChanged signal handler given by the tested client. 374 // The OutputMuteChanged signal handler given by the tested client.
358 dbus::ObjectProxy::SignalCallback output_mute_changed_handler_; 375 dbus::ObjectProxy::SignalCallback output_mute_changed_handler_;
359 // The InputMuteChanged signal handler given by the tested client. 376 // The InputMuteChanged signal handler given by the tested client.
360 dbus::ObjectProxy::SignalCallback input_mute_changed_handler_; 377 dbus::ObjectProxy::SignalCallback input_mute_changed_handler_;
361 // The NodesChanged signal handler given by the tested client. 378 // The NodesChanged signal handler given by the tested client.
362 dbus::ObjectProxy::SignalCallback nodes_changed_handler_; 379 dbus::ObjectProxy::SignalCallback nodes_changed_handler_;
363 // The ActiveOutputNodeChanged signal handler given by the tested client. 380 // The ActiveOutputNodeChanged signal handler given by the tested client.
364 dbus::ObjectProxy::SignalCallback active_output_node_changed_handler_; 381 dbus::ObjectProxy::SignalCallback active_output_node_changed_handler_;
365 // The ActiveInputNodeChanged signal handler given by the tested client. 382 // The ActiveInputNodeChanged signal handler given by the tested client.
366 dbus::ObjectProxy::SignalCallback active_input_node_changed_handler_; 383 dbus::ObjectProxy::SignalCallback active_input_node_changed_handler_;
384 // The OutputNodeVolumeChanged signal handler given by the tested client.
385 dbus::ObjectProxy::SignalCallback output_node_volume_changed_handler_;
367 // The name of the method which is expected to be called. 386 // The name of the method which is expected to be called.
368 std::string expected_method_name_; 387 std::string expected_method_name_;
369 // The response which the mock cras proxy returns. 388 // The response which the mock cras proxy returns.
370 dbus::Response* response_; 389 dbus::Response* response_;
371 // A callback to intercept and check the method call arguments. 390 // A callback to intercept and check the method call arguments.
372 ArgumentCheckCallback argument_checker_; 391 ArgumentCheckCallback argument_checker_;
373 392
374 private: 393 private:
375 // Checks the requested interface name and signal name. 394 // Checks the requested interface name and signal name.
376 // Used to implement the mock cras proxy. 395 // Used to implement the mock cras proxy.
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
445 const dbus::ObjectProxy::OnConnectedCallback& on_connected_callback) { 464 const dbus::ObjectProxy::OnConnectedCallback& on_connected_callback) {
446 active_input_node_changed_handler_ = signal_callback; 465 active_input_node_changed_handler_ = signal_callback;
447 const bool success = true; 466 const bool success = true;
448 message_loop_.task_runner()->PostTask(FROM_HERE, 467 message_loop_.task_runner()->PostTask(FROM_HERE,
449 base::Bind(on_connected_callback, 468 base::Bind(on_connected_callback,
450 interface_name, 469 interface_name,
451 signal_name, 470 signal_name,
452 success)); 471 success));
453 } 472 }
454 473
474 // Checks the requested interface name and signal name.
475 // Used to implement the mock cras proxy.
476 void OnConnectToOutputNodeVolumeChanged(
477 const std::string& interface_name,
478 const std::string& signal_name,
479 const dbus::ObjectProxy::SignalCallback& signal_callback,
480 const dbus::ObjectProxy::OnConnectedCallback& on_connected_callback) {
481 output_node_volume_changed_handler_ = signal_callback;
482 const bool success = true;
483 message_loop_.task_runner()->PostTask(FROM_HERE,
484 base::Bind(on_connected_callback,
485 interface_name,
486 signal_name,
487 success));
488 }
489
455 // Checks the content of the method call and returns the response. 490 // Checks the content of the method call and returns the response.
456 // Used to implement the mock cras proxy. 491 // Used to implement the mock cras proxy.
457 void OnCallMethod(dbus::MethodCall* method_call, 492 void OnCallMethod(dbus::MethodCall* method_call,
458 int timeout_ms, 493 int timeout_ms,
459 const dbus::ObjectProxy::ResponseCallback& response) { 494 const dbus::ObjectProxy::ResponseCallback& response) {
460 EXPECT_EQ(interface_name_, method_call->GetInterface()); 495 EXPECT_EQ(interface_name_, method_call->GetInterface());
461 EXPECT_EQ(expected_method_name_, method_call->GetMember()); 496 EXPECT_EQ(expected_method_name_, method_call->GetMember());
462 dbus::MessageReader reader(method_call); 497 dbus::MessageReader reader(method_call);
463 argument_checker_.Run(&reader); 498 argument_checker_.Run(&reader);
464 message_loop_.task_runner()->PostTask( 499 message_loop_.task_runner()->PostTask(
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after
610 // Remove the observer. 645 // Remove the observer.
611 client_->RemoveObserver(&observer); 646 client_->RemoveObserver(&observer);
612 EXPECT_CALL(observer, ActiveInputNodeChanged(_)).Times(0); 647 EXPECT_CALL(observer, ActiveInputNodeChanged(_)).Times(0);
613 648
614 // Run the signal callback again and make sure the observer isn't called. 649 // Run the signal callback again and make sure the observer isn't called.
615 SendActiveInputNodeChangedSignal(&signal); 650 SendActiveInputNodeChangedSignal(&signal);
616 651
617 message_loop_.RunUntilIdle(); 652 message_loop_.RunUntilIdle();
618 } 653 }
619 654
655 TEST_F(CrasAudioClientTest, OutputNodeVolumeChanged) {
656 const uint64_t kNodeId = 20003;
657 const int32_t volume = 82;
658 // Create a signal
659 dbus::Signal signal(cras::kCrasControlInterface,
660 cras::kOutputNodeVolumeChanged);
661 dbus::MessageWriter writer(&signal);
662 writer.AppendUint64(kNodeId);
663 writer.AppendInt32(volume);
664
665 // Set expectations
666 MockObserver observer;
667 EXPECT_CALL(observer, OutputNodeVolumeChanged(kNodeId, volume)).Times(1);
668
669 // Add the observer.
670 client_->AddObserver(&observer);
671
672 // Run the signal callback.
673 SendOutputNodeVolumeChangedSignal(&signal);
674
675 // Remove the observer.
676 client_->RemoveObserver(&observer);
677 EXPECT_CALL(observer, OutputNodeVolumeChanged(_, _)).Times(0);
678
679 // Run the signal callback again and make sure the observer isn't called.
680 SendOutputNodeVolumeChangedSignal(&signal);
681
682 message_loop_.RunUntilIdle();
683 }
684
620 TEST_F(CrasAudioClientTest, GetNodes) { 685 TEST_F(CrasAudioClientTest, GetNodes) {
621 // Create the expected value. 686 // Create the expected value.
622 AudioNodeList expected_node_list; 687 AudioNodeList expected_node_list;
623 expected_node_list.push_back(kInternalSpeaker); 688 expected_node_list.push_back(kInternalSpeaker);
624 expected_node_list.push_back(kInternalMic); 689 expected_node_list.push_back(kInternalMic);
625 690
626 // Create response. 691 // Create response.
627 std::unique_ptr<dbus::Response> response(dbus::Response::CreateEmpty()); 692 std::unique_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
628 dbus::MessageWriter writer(response.get()); 693 dbus::MessageWriter writer(response.get());
629 WriteNodesToResponse(writer, expected_node_list); 694 WriteNodesToResponse(writer, expected_node_list);
(...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after
829 kMixer), 894 kMixer),
830 response.get()); 895 response.get());
831 896
832 // Call method. 897 // Call method.
833 client_->SetGlobalOutputChannelRemix(kChannels, kMixer); 898 client_->SetGlobalOutputChannelRemix(kChannels, kMixer);
834 // Run the message loop. 899 // Run the message loop.
835 message_loop_.RunUntilIdle(); 900 message_loop_.RunUntilIdle();
836 } 901 }
837 902
838 } // namespace chromeos 903 } // namespace chromeos
OLDNEW
« no previous file with comments | « chromeos/dbus/cras_audio_client.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698