| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chromeos/dbus/cras_audio_client.h" |
| 6 |
| 7 #include <memory> |
| 8 #include <string> |
| 9 #include <vector> |
| 10 |
| 11 #include "base/bind.h" |
| 12 #include "base/message_loop/message_loop.h" |
| 13 #include "dbus/message.h" |
| 14 #include "dbus/mock_bus.h" |
| 15 #include "dbus/mock_object_proxy.h" |
| 16 #include "dbus/object_path.h" |
| 17 #include "testing/gmock/include/gmock/gmock.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" |
| 19 #include "third_party/cros_system_api/dbus/service_constants.h" |
| 20 |
| 21 using ::testing::_; |
| 22 using ::testing::Invoke; |
| 23 using ::testing::Return; |
| 24 |
| 25 namespace chromeos { |
| 26 |
| 27 namespace { |
| 28 |
| 29 // Audio nodes for GetNodes unit test. |
| 30 const uint64_t kInternalSpeakerId = 10001; |
| 31 const uint64_t kInternalMicId = 20001; |
| 32 |
| 33 const AudioNode kInternalSpeaker(false, |
| 34 kInternalSpeakerId, |
| 35 kInternalSpeakerId, |
| 36 "Fake Speaker", |
| 37 "INTERNAL_SPEAKER", |
| 38 "Speaker", |
| 39 false, |
| 40 0); |
| 41 |
| 42 const AudioNode kInternalMic(true, |
| 43 kInternalMicId, |
| 44 kInternalMicId, |
| 45 "Fake Mic", |
| 46 "INTERNAL_MIC", |
| 47 "Internal Mic", |
| 48 false, |
| 49 0); |
| 50 // A mock ErrorCallback. |
| 51 class MockErrorCallback { |
| 52 public: |
| 53 MockErrorCallback() {} |
| 54 ~MockErrorCallback() {} |
| 55 MOCK_METHOD2(Run, void(const std::string& error_name, |
| 56 const std::string& error_message)); |
| 57 CrasAudioClient::ErrorCallback GetCallback() { |
| 58 return base::Bind(&MockErrorCallback::Run, base::Unretained(this)); |
| 59 } |
| 60 }; |
| 61 |
| 62 // A mock CrasAudioClient Observer. |
| 63 class MockObserver : public CrasAudioClient::Observer { |
| 64 public: |
| 65 MockObserver() {} |
| 66 ~MockObserver() override {} |
| 67 MOCK_METHOD1(OutputMuteChanged, void(bool mute_on)); |
| 68 MOCK_METHOD1(InputMuteChanged, void(bool mute_on)); |
| 69 MOCK_METHOD0(NodesChanged, void()); |
| 70 MOCK_METHOD1(ActiveOutputNodeChanged, void(uint64_t node_id)); |
| 71 MOCK_METHOD1(ActiveInputNodeChanged, void(uint64_t node_id)); |
| 72 }; |
| 73 |
| 74 // Expect the reader to be empty. |
| 75 void ExpectNoArgument(dbus::MessageReader* reader) { |
| 76 EXPECT_FALSE(reader->HasMoreData()); |
| 77 } |
| 78 |
| 79 // Expect the reader to have an int32_t and an array of doubles. |
| 80 void ExpectInt32AndArrayOfDoublesArguments( |
| 81 int32_t expected_value, |
| 82 const std::vector<double>& expected_doubles, |
| 83 dbus::MessageReader* reader) { |
| 84 int32_t value; |
| 85 ASSERT_TRUE(reader->PopInt32(&value)); |
| 86 EXPECT_EQ(expected_value, value); |
| 87 const double* doubles = nullptr; |
| 88 size_t size = 0; |
| 89 ASSERT_TRUE(reader->PopArrayOfDoubles(&doubles, &size)); |
| 90 EXPECT_EQ(expected_doubles.size(), size); |
| 91 for (size_t i = 0; i < size; ++i) { |
| 92 EXPECT_EQ(expected_doubles[i], doubles[i]); |
| 93 } |
| 94 EXPECT_FALSE(reader->HasMoreData()); |
| 95 } |
| 96 |
| 97 void WriteNodesToResponse(dbus::MessageWriter& writer, |
| 98 const AudioNodeList& node_list) { |
| 99 dbus::MessageWriter sub_writer(nullptr); |
| 100 dbus::MessageWriter entry_writer(nullptr); |
| 101 for (size_t i = 0; i < node_list.size(); ++i) { |
| 102 writer.OpenArray("{sv}", &sub_writer); |
| 103 sub_writer.OpenDictEntry(&entry_writer); |
| 104 entry_writer.AppendString(cras::kIsInputProperty); |
| 105 entry_writer.AppendVariantOfBool(node_list[i].is_input); |
| 106 sub_writer.CloseContainer(&entry_writer); |
| 107 sub_writer.OpenDictEntry(&entry_writer); |
| 108 entry_writer.AppendString(cras::kIdProperty); |
| 109 entry_writer.AppendVariantOfUint64(node_list[i].id); |
| 110 sub_writer.CloseContainer(&entry_writer); |
| 111 sub_writer.OpenDictEntry(&entry_writer); |
| 112 entry_writer.AppendString(cras::kDeviceNameProperty); |
| 113 entry_writer.AppendVariantOfString(node_list[i].device_name); |
| 114 sub_writer.CloseContainer(&entry_writer); |
| 115 sub_writer.OpenDictEntry(&entry_writer); |
| 116 entry_writer.AppendString(cras::kTypeProperty); |
| 117 entry_writer.AppendVariantOfString(node_list[i].type); |
| 118 sub_writer.CloseContainer(&entry_writer); |
| 119 sub_writer.OpenDictEntry(&entry_writer); |
| 120 entry_writer.AppendString(cras::kNameProperty); |
| 121 entry_writer.AppendVariantOfString(node_list[i].name); |
| 122 sub_writer.CloseContainer(&entry_writer); |
| 123 sub_writer.OpenDictEntry(&entry_writer); |
| 124 entry_writer.AppendString(cras::kActiveProperty); |
| 125 entry_writer.AppendVariantOfBool(node_list[i].active); |
| 126 sub_writer.CloseContainer(&entry_writer); |
| 127 sub_writer.OpenDictEntry(&entry_writer); |
| 128 entry_writer.AppendString(cras::kPluggedTimeProperty); |
| 129 entry_writer.AppendVariantOfUint64(node_list[i].plugged_time); |
| 130 sub_writer.CloseContainer(&entry_writer); |
| 131 sub_writer.OpenDictEntry(&entry_writer); |
| 132 entry_writer.AppendString(cras::kMicPositionsProperty); |
| 133 entry_writer.AppendVariantOfString(node_list[i].mic_positions); |
| 134 sub_writer.CloseContainer(&entry_writer); |
| 135 sub_writer.OpenDictEntry(&entry_writer); |
| 136 entry_writer.AppendString(cras::kStableDeviceIdProperty); |
| 137 entry_writer.AppendVariantOfUint64(node_list[i].stable_device_id); |
| 138 sub_writer.CloseContainer(&entry_writer); |
| 139 writer.CloseContainer(&sub_writer); |
| 140 } |
| 141 } |
| 142 |
| 143 // Expect the AudioNodeList result. |
| 144 void ExpectAudioNodeListResult(const AudioNodeList* expected_node_list, |
| 145 const AudioNodeList& node_list, |
| 146 bool call_status) { |
| 147 EXPECT_EQ(true, call_status); |
| 148 EXPECT_EQ(expected_node_list->size(), node_list.size()); |
| 149 for (size_t i = 0; i < node_list.size(); ++i) { |
| 150 EXPECT_EQ((*expected_node_list)[i].is_input, node_list[i].is_input); |
| 151 EXPECT_EQ((*expected_node_list)[i].id, node_list[i].id); |
| 152 EXPECT_EQ((*expected_node_list)[i].stable_device_id, |
| 153 node_list[i].stable_device_id); |
| 154 EXPECT_EQ((*expected_node_list)[i].device_name, node_list[i].device_name); |
| 155 EXPECT_EQ((*expected_node_list)[i].type, node_list[i].type); |
| 156 EXPECT_EQ((*expected_node_list)[i].name, node_list[i].name); |
| 157 EXPECT_EQ((*expected_node_list)[i].mic_positions, |
| 158 node_list[i].mic_positions); |
| 159 EXPECT_EQ((*expected_node_list)[i].active, node_list[i].active); |
| 160 EXPECT_EQ((*expected_node_list)[i].plugged_time, node_list[i].plugged_time); |
| 161 } |
| 162 } |
| 163 |
| 164 } // namespace |
| 165 |
| 166 class CrasAudioClientTest : public testing::Test { |
| 167 public: |
| 168 CrasAudioClientTest() : interface_name_(cras::kCrasControlInterface), |
| 169 response_(nullptr) {} |
| 170 |
| 171 void SetUp() override { |
| 172 // Create a mock bus. |
| 173 dbus::Bus::Options options; |
| 174 options.bus_type = dbus::Bus::SYSTEM; |
| 175 mock_bus_ = new dbus::MockBus(options); |
| 176 |
| 177 // Create a mock cras proxy. |
| 178 mock_cras_proxy_ = new dbus::MockObjectProxy( |
| 179 mock_bus_.get(), |
| 180 cras::kCrasServiceName, |
| 181 dbus::ObjectPath(cras::kCrasServicePath)); |
| 182 |
| 183 // Set an expectation so mock_cras_proxy's CallMethod() will use |
| 184 // OnCallMethod() to return responses. |
| 185 EXPECT_CALL(*mock_cras_proxy_.get(), CallMethod(_, _, _)) |
| 186 .WillRepeatedly(Invoke(this, &CrasAudioClientTest::OnCallMethod)); |
| 187 |
| 188 // Set an expectation so mock_cras_proxy's CallMethodWithErrorCallback() |
| 189 // will use OnCallMethodWithErrorCallback() to return responses. |
| 190 EXPECT_CALL(*mock_cras_proxy_.get(), |
| 191 CallMethodWithErrorCallback(_, _, _, _)) |
| 192 .WillRepeatedly( |
| 193 Invoke(this, &CrasAudioClientTest::OnCallMethodWithErrorCallback)); |
| 194 |
| 195 // Set an expectation so mock_cras_proxy's monitoring OutputMuteChanged |
| 196 // ConnectToSignal will use OnConnectToOutputMuteChanged() to run the |
| 197 // callback. |
| 198 EXPECT_CALL( |
| 199 *mock_cras_proxy_.get(), |
| 200 ConnectToSignal(interface_name_, cras::kOutputMuteChanged, _, _)) |
| 201 .WillRepeatedly( |
| 202 Invoke(this, &CrasAudioClientTest::OnConnectToOutputMuteChanged)); |
| 203 |
| 204 // Set an expectation so mock_cras_proxy's monitoring InputMuteChanged |
| 205 // ConnectToSignal will use OnConnectToInputMuteChanged() to run the |
| 206 // callback. |
| 207 EXPECT_CALL( |
| 208 *mock_cras_proxy_.get(), |
| 209 ConnectToSignal(interface_name_, cras::kInputMuteChanged, _, _)) |
| 210 .WillRepeatedly( |
| 211 Invoke(this, &CrasAudioClientTest::OnConnectToInputMuteChanged)); |
| 212 |
| 213 // Set an expectation so mock_cras_proxy's monitoring NodesChanged |
| 214 // ConnectToSignal will use OnConnectToNodesChanged() to run the callback. |
| 215 EXPECT_CALL( |
| 216 *mock_cras_proxy_.get(), |
| 217 ConnectToSignal(interface_name_, cras::kNodesChanged, _, _)) |
| 218 .WillRepeatedly( |
| 219 Invoke(this, &CrasAudioClientTest::OnConnectToNodesChanged)); |
| 220 |
| 221 // Set an expectation so mock_cras_proxy's monitoring |
| 222 // ActiveOutputNodeChanged ConnectToSignal will use |
| 223 // OnConnectToActiveOutputNodeChanged() to run the callback. |
| 224 EXPECT_CALL( |
| 225 *mock_cras_proxy_.get(), |
| 226 ConnectToSignal(interface_name_, cras::kActiveOutputNodeChanged, _, _)) |
| 227 .WillRepeatedly( |
| 228 Invoke(this, |
| 229 &CrasAudioClientTest::OnConnectToActiveOutputNodeChanged)); |
| 230 |
| 231 // Set an expectation so mock_cras_proxy's monitoring |
| 232 // ActiveInputNodeChanged ConnectToSignal will use |
| 233 // OnConnectToActiveInputNodeChanged() to run the callback. |
| 234 EXPECT_CALL( |
| 235 *mock_cras_proxy_.get(), |
| 236 ConnectToSignal(interface_name_, cras::kActiveInputNodeChanged, _, _)) |
| 237 .WillRepeatedly( |
| 238 Invoke(this, |
| 239 &CrasAudioClientTest::OnConnectToActiveInputNodeChanged)); |
| 240 |
| 241 // Set an expectation so mock_bus's GetObjectProxy() for the given |
| 242 // service name and the object path will return mock_cras_proxy_. |
| 243 EXPECT_CALL(*mock_bus_.get(), |
| 244 GetObjectProxy(cras::kCrasServiceName, |
| 245 dbus::ObjectPath(cras::kCrasServicePath))) |
| 246 .WillOnce(Return(mock_cras_proxy_.get())); |
| 247 |
| 248 // ShutdownAndBlock() will be called in TearDown(). |
| 249 EXPECT_CALL(*mock_bus_.get(), ShutdownAndBlock()).WillOnce(Return()); |
| 250 |
| 251 // Create a client with the mock bus. |
| 252 client_.reset(CrasAudioClient::Create()); |
| 253 client_->Init(mock_bus_.get()); |
| 254 // Run the message loop to run the signal connection result callback. |
| 255 message_loop_.RunUntilIdle(); |
| 256 } |
| 257 |
| 258 void TearDown() override { mock_bus_->ShutdownAndBlock(); } |
| 259 |
| 260 protected: |
| 261 // A callback to intercept and check the method call arguments. |
| 262 typedef base::Callback<void( |
| 263 dbus::MessageReader* reader)> ArgumentCheckCallback; |
| 264 |
| 265 // Sets expectations for called method name and arguments, and sets response. |
| 266 void PrepareForMethodCall(const std::string& method_name, |
| 267 const ArgumentCheckCallback& argument_checker, |
| 268 dbus::Response* response) { |
| 269 expected_method_name_ = method_name; |
| 270 argument_checker_ = argument_checker; |
| 271 response_ = response; |
| 272 } |
| 273 |
| 274 // Send output mute changed signal to the tested client. |
| 275 void SendOutputMuteChangedSignal(dbus::Signal* signal) { |
| 276 ASSERT_FALSE(output_mute_changed_handler_.is_null()); |
| 277 output_mute_changed_handler_.Run(signal); |
| 278 } |
| 279 |
| 280 // Send input mute changed signal to the tested client. |
| 281 void SendInputMuteChangedSignal(dbus::Signal* signal) { |
| 282 ASSERT_FALSE(input_mute_changed_handler_.is_null()); |
| 283 input_mute_changed_handler_.Run(signal); |
| 284 } |
| 285 |
| 286 // Send nodes changed signal to the tested client. |
| 287 void SendNodesChangedSignal(dbus::Signal* signal) { |
| 288 ASSERT_FALSE(nodes_changed_handler_.is_null()); |
| 289 nodes_changed_handler_.Run(signal); |
| 290 } |
| 291 |
| 292 // Send active output node changed signal to the tested client. |
| 293 void SendActiveOutputNodeChangedSignal(dbus::Signal* signal) { |
| 294 ASSERT_FALSE(active_output_node_changed_handler_.is_null()); |
| 295 active_output_node_changed_handler_.Run(signal); |
| 296 } |
| 297 |
| 298 // Send active input node changed signal to the tested client. |
| 299 void SendActiveInputNodeChangedSignal(dbus::Signal* signal) { |
| 300 ASSERT_FALSE(active_input_node_changed_handler_.is_null()); |
| 301 active_input_node_changed_handler_.Run(signal); |
| 302 } |
| 303 |
| 304 // The interface name. |
| 305 const std::string interface_name_; |
| 306 // The client to be tested. |
| 307 std::unique_ptr<CrasAudioClient> client_; |
| 308 // A message loop to emulate asynchronous behavior. |
| 309 base::MessageLoop message_loop_; |
| 310 // The mock bus. |
| 311 scoped_refptr<dbus::MockBus> mock_bus_; |
| 312 // The mock object proxy. |
| 313 scoped_refptr<dbus::MockObjectProxy> mock_cras_proxy_; |
| 314 // The OutputMuteChanged signal handler given by the tested client. |
| 315 dbus::ObjectProxy::SignalCallback output_mute_changed_handler_; |
| 316 // The InputMuteChanged signal handler given by the tested client. |
| 317 dbus::ObjectProxy::SignalCallback input_mute_changed_handler_; |
| 318 // The NodesChanged signal handler given by the tested client. |
| 319 dbus::ObjectProxy::SignalCallback nodes_changed_handler_; |
| 320 // The ActiveOutputNodeChanged signal handler given by the tested client. |
| 321 dbus::ObjectProxy::SignalCallback active_output_node_changed_handler_; |
| 322 // The ActiveInputNodeChanged signal handler given by the tested client. |
| 323 dbus::ObjectProxy::SignalCallback active_input_node_changed_handler_; |
| 324 // The name of the method which is expected to be called. |
| 325 std::string expected_method_name_; |
| 326 // The response which the mock cras proxy returns. |
| 327 dbus::Response* response_; |
| 328 // A callback to intercept and check the method call arguments. |
| 329 ArgumentCheckCallback argument_checker_; |
| 330 |
| 331 private: |
| 332 // Checks the requested interface name and signal name. |
| 333 // Used to implement the mock cras proxy. |
| 334 void OnConnectToOutputMuteChanged( |
| 335 const std::string& interface_name, |
| 336 const std::string& signal_name, |
| 337 const dbus::ObjectProxy::SignalCallback& signal_callback, |
| 338 const dbus::ObjectProxy::OnConnectedCallback& on_connected_callback) { |
| 339 output_mute_changed_handler_ = signal_callback; |
| 340 const bool success = true; |
| 341 message_loop_.task_runner()->PostTask(FROM_HERE, |
| 342 base::Bind(on_connected_callback, |
| 343 interface_name, |
| 344 signal_name, |
| 345 success)); |
| 346 } |
| 347 |
| 348 // Checks the requested interface name and signal name. |
| 349 // Used to implement the mock cras proxy. |
| 350 void OnConnectToInputMuteChanged( |
| 351 const std::string& interface_name, |
| 352 const std::string& signal_name, |
| 353 const dbus::ObjectProxy::SignalCallback& signal_callback, |
| 354 const dbus::ObjectProxy::OnConnectedCallback& on_connected_callback) { |
| 355 input_mute_changed_handler_ = signal_callback; |
| 356 const bool success = true; |
| 357 message_loop_.task_runner()->PostTask(FROM_HERE, |
| 358 base::Bind(on_connected_callback, |
| 359 interface_name, |
| 360 signal_name, |
| 361 success)); |
| 362 } |
| 363 |
| 364 // Checks the requested interface name and signal name. |
| 365 // Used to implement the mock cras proxy. |
| 366 void OnConnectToNodesChanged( |
| 367 const std::string& interface_name, |
| 368 const std::string& signal_name, |
| 369 const dbus::ObjectProxy::SignalCallback& signal_callback, |
| 370 const dbus::ObjectProxy::OnConnectedCallback& on_connected_callback) { |
| 371 nodes_changed_handler_ = signal_callback; |
| 372 const bool success = true; |
| 373 message_loop_.task_runner()->PostTask(FROM_HERE, |
| 374 base::Bind(on_connected_callback, |
| 375 interface_name, |
| 376 signal_name, |
| 377 success)); |
| 378 } |
| 379 |
| 380 // Checks the requested interface name and signal name. |
| 381 // Used to implement the mock cras proxy. |
| 382 void OnConnectToActiveOutputNodeChanged( |
| 383 const std::string& interface_name, |
| 384 const std::string& signal_name, |
| 385 const dbus::ObjectProxy::SignalCallback& signal_callback, |
| 386 const dbus::ObjectProxy::OnConnectedCallback& on_connected_callback) { |
| 387 active_output_node_changed_handler_ = signal_callback; |
| 388 const bool success = true; |
| 389 message_loop_.task_runner()->PostTask(FROM_HERE, |
| 390 base::Bind(on_connected_callback, |
| 391 interface_name, |
| 392 signal_name, |
| 393 success)); |
| 394 } |
| 395 |
| 396 // Checks the requested interface name and signal name. |
| 397 // Used to implement the mock cras proxy. |
| 398 void OnConnectToActiveInputNodeChanged( |
| 399 const std::string& interface_name, |
| 400 const std::string& signal_name, |
| 401 const dbus::ObjectProxy::SignalCallback& signal_callback, |
| 402 const dbus::ObjectProxy::OnConnectedCallback& on_connected_callback) { |
| 403 active_input_node_changed_handler_ = signal_callback; |
| 404 const bool success = true; |
| 405 message_loop_.task_runner()->PostTask(FROM_HERE, |
| 406 base::Bind(on_connected_callback, |
| 407 interface_name, |
| 408 signal_name, |
| 409 success)); |
| 410 } |
| 411 |
| 412 // Checks the content of the method call and returns the response. |
| 413 // Used to implement the mock cras proxy. |
| 414 void OnCallMethod(dbus::MethodCall* method_call, |
| 415 int timeout_ms, |
| 416 const dbus::ObjectProxy::ResponseCallback& response) { |
| 417 EXPECT_EQ(interface_name_, method_call->GetInterface()); |
| 418 EXPECT_EQ(expected_method_name_, method_call->GetMember()); |
| 419 dbus::MessageReader reader(method_call); |
| 420 argument_checker_.Run(&reader); |
| 421 message_loop_.task_runner()->PostTask( |
| 422 FROM_HERE, base::Bind(response, response_)); |
| 423 } |
| 424 |
| 425 // Checks the content of the method call and returns the response. |
| 426 // Used to implement the mock cras proxy. |
| 427 void OnCallMethodWithErrorCallback( |
| 428 dbus::MethodCall* method_call, |
| 429 int timeout_ms, |
| 430 const dbus::ObjectProxy::ResponseCallback& response_callback, |
| 431 const dbus::ObjectProxy::ErrorCallback& error_callback) { |
| 432 OnCallMethod(method_call, timeout_ms, response_callback); |
| 433 } |
| 434 }; |
| 435 |
| 436 TEST_F(CrasAudioClientTest, OutputMuteChanged) { |
| 437 const bool kSystemMuteOn = false; |
| 438 const bool kUserMuteOn = true; |
| 439 // Create a signal. |
| 440 dbus::Signal signal(cras::kCrasControlInterface, |
| 441 cras::kOutputMuteChanged); |
| 442 dbus::MessageWriter writer(&signal); |
| 443 writer.AppendBool(kSystemMuteOn); |
| 444 writer.AppendBool(kUserMuteOn); |
| 445 |
| 446 // Set expectations. |
| 447 MockObserver observer; |
| 448 EXPECT_CALL(observer, OutputMuteChanged(kUserMuteOn)).Times(1); |
| 449 |
| 450 // Add the observer. |
| 451 client_->AddObserver(&observer); |
| 452 |
| 453 // Run the signal callback. |
| 454 SendOutputMuteChangedSignal(&signal); |
| 455 |
| 456 // Remove the observer. |
| 457 client_->RemoveObserver(&observer); |
| 458 |
| 459 EXPECT_CALL(observer, OutputMuteChanged(_)).Times(0); |
| 460 |
| 461 // Run the signal callback again and make sure the observer isn't called. |
| 462 SendOutputMuteChangedSignal(&signal); |
| 463 |
| 464 message_loop_.RunUntilIdle(); |
| 465 } |
| 466 |
| 467 TEST_F(CrasAudioClientTest, InputMuteChanged) { |
| 468 const bool kInputMuteOn = true; |
| 469 // Create a signal. |
| 470 dbus::Signal signal(cras::kCrasControlInterface, |
| 471 cras::kInputMuteChanged); |
| 472 dbus::MessageWriter writer(&signal); |
| 473 writer.AppendBool(kInputMuteOn); |
| 474 |
| 475 // Set expectations. |
| 476 MockObserver observer; |
| 477 EXPECT_CALL(observer, InputMuteChanged(kInputMuteOn)).Times(1); |
| 478 |
| 479 // Add the observer. |
| 480 client_->AddObserver(&observer); |
| 481 |
| 482 // Run the signal callback. |
| 483 SendInputMuteChangedSignal(&signal); |
| 484 |
| 485 // Remove the observer. |
| 486 client_->RemoveObserver(&observer); |
| 487 |
| 488 EXPECT_CALL(observer, InputMuteChanged(_)).Times(0); |
| 489 |
| 490 // Run the signal callback again and make sure the observer isn't called. |
| 491 SendInputMuteChangedSignal(&signal); |
| 492 |
| 493 message_loop_.RunUntilIdle(); |
| 494 } |
| 495 |
| 496 TEST_F(CrasAudioClientTest, NodesChanged) { |
| 497 // Create a signal. |
| 498 dbus::Signal signal(cras::kCrasControlInterface, |
| 499 cras::kNodesChanged); |
| 500 // Set expectations. |
| 501 MockObserver observer; |
| 502 EXPECT_CALL(observer, NodesChanged()).Times(1); |
| 503 |
| 504 // Add the observer. |
| 505 client_->AddObserver(&observer); |
| 506 |
| 507 // Run the signal callback. |
| 508 SendNodesChangedSignal(&signal); |
| 509 |
| 510 // Remove the observer. |
| 511 client_->RemoveObserver(&observer); |
| 512 |
| 513 EXPECT_CALL(observer, NodesChanged()).Times(0); |
| 514 |
| 515 // Run the signal callback again and make sure the observer isn't called. |
| 516 SendNodesChangedSignal(&signal); |
| 517 |
| 518 message_loop_.RunUntilIdle(); |
| 519 } |
| 520 |
| 521 TEST_F(CrasAudioClientTest, ActiveOutputNodeChanged) { |
| 522 const uint64_t kNodeId = 10002; |
| 523 // Create a signal. |
| 524 dbus::Signal signal(cras::kCrasControlInterface, |
| 525 cras::kActiveOutputNodeChanged); |
| 526 dbus::MessageWriter writer(&signal); |
| 527 writer.AppendUint64(kNodeId); |
| 528 |
| 529 // Set expectations. |
| 530 MockObserver observer; |
| 531 EXPECT_CALL(observer, ActiveOutputNodeChanged(kNodeId)).Times(1); |
| 532 |
| 533 // Add the observer. |
| 534 client_->AddObserver(&observer); |
| 535 |
| 536 // Run the signal callback. |
| 537 SendActiveOutputNodeChangedSignal(&signal); |
| 538 |
| 539 // Remove the observer. |
| 540 client_->RemoveObserver(&observer); |
| 541 EXPECT_CALL(observer, ActiveOutputNodeChanged(_)).Times(0); |
| 542 |
| 543 // Run the signal callback again and make sure the observer isn't called. |
| 544 SendActiveOutputNodeChangedSignal(&signal); |
| 545 |
| 546 message_loop_.RunUntilIdle(); |
| 547 } |
| 548 |
| 549 TEST_F(CrasAudioClientTest, ActiveInputNodeChanged) { |
| 550 const uint64_t kNodeId = 20002; |
| 551 // Create a signal. |
| 552 dbus::Signal signal(cras::kCrasControlInterface, |
| 553 cras::kActiveInputNodeChanged); |
| 554 dbus::MessageWriter writer(&signal); |
| 555 writer.AppendUint64(kNodeId); |
| 556 |
| 557 // Set expectations. |
| 558 MockObserver observer; |
| 559 EXPECT_CALL(observer, ActiveInputNodeChanged(kNodeId)).Times(1); |
| 560 |
| 561 // Add the observer. |
| 562 client_->AddObserver(&observer); |
| 563 |
| 564 // Run the signal callback. |
| 565 SendActiveInputNodeChangedSignal(&signal); |
| 566 |
| 567 // Remove the observer. |
| 568 client_->RemoveObserver(&observer); |
| 569 EXPECT_CALL(observer, ActiveInputNodeChanged(_)).Times(0); |
| 570 |
| 571 // Run the signal callback again and make sure the observer isn't called. |
| 572 SendActiveInputNodeChangedSignal(&signal); |
| 573 |
| 574 message_loop_.RunUntilIdle(); |
| 575 } |
| 576 |
| 577 TEST_F(CrasAudioClientTest, GetNodes) { |
| 578 // Create the expected value. |
| 579 AudioNodeList expected_node_list; |
| 580 expected_node_list.push_back(kInternalSpeaker); |
| 581 expected_node_list.push_back(kInternalMic); |
| 582 |
| 583 // Create response. |
| 584 std::unique_ptr<dbus::Response> response(dbus::Response::CreateEmpty()); |
| 585 dbus::MessageWriter writer(response.get()); |
| 586 WriteNodesToResponse(writer, expected_node_list); |
| 587 |
| 588 // Set expectations. |
| 589 PrepareForMethodCall(cras::kGetNodes, |
| 590 base::Bind(&ExpectNoArgument), |
| 591 response.get()); |
| 592 // Call method. |
| 593 MockErrorCallback error_callback; |
| 594 client_->GetNodes(base::Bind(&ExpectAudioNodeListResult, |
| 595 &expected_node_list), |
| 596 error_callback.GetCallback()); |
| 597 EXPECT_CALL(error_callback, Run(_, _)).Times(0); |
| 598 // Run the message loop. |
| 599 message_loop_.RunUntilIdle(); |
| 600 } |
| 601 |
| 602 TEST_F(CrasAudioClientTest, SetGlobalOutputChannelRemix) { |
| 603 const int32_t kChannels = 2; |
| 604 const std::vector<double> kMixer = {0, 0.1, 0.5, 1}; |
| 605 // Create response. |
| 606 std::unique_ptr<dbus::Response> response(dbus::Response::CreateEmpty()); |
| 607 |
| 608 // Set expectations. |
| 609 PrepareForMethodCall(cras::kSetGlobalOutputChannelRemix, |
| 610 base::Bind(&ExpectInt32AndArrayOfDoublesArguments, |
| 611 kChannels, |
| 612 kMixer), |
| 613 response.get()); |
| 614 |
| 615 // Call method. |
| 616 client_->SetGlobalOutputChannelRemix(kChannels, kMixer); |
| 617 // Run the message loop. |
| 618 message_loop_.RunUntilIdle(); |
| 619 } |
| 620 |
| 621 } // namespace chromeos |
| OLD | NEW |