Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
|
jennyz
2016/04/14 00:12:31
2012 -> 2016
remove (c): This is deprecated style.
Qiang(Joe) Xu
2016/04/14 01:25:48
Done.
| |
| 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 // Expect the VolumeState result. | |
| 98 void ExpectVolumeStateResult(const VolumeState* expected_volume_state, | |
| 99 const VolumeState& volume_state, | |
| 100 bool call_status) { | |
| 101 EXPECT_EQ(true, call_status); | |
| 102 EXPECT_EQ(expected_volume_state->output_volume, volume_state.output_volume); | |
| 103 EXPECT_EQ(expected_volume_state->output_system_mute, | |
| 104 volume_state.output_system_mute); | |
| 105 EXPECT_EQ(expected_volume_state->input_gain, volume_state.input_gain); | |
| 106 EXPECT_EQ(expected_volume_state->input_mute, volume_state.input_mute); | |
| 107 EXPECT_EQ(expected_volume_state->output_user_mute, | |
| 108 volume_state.output_user_mute); | |
| 109 } | |
| 110 | |
| 111 // Expect the AudioNodeList result. | |
| 112 void ExpectAudioNodeListResult(const AudioNodeList* expected_node_list, | |
| 113 const AudioNodeList& node_list, | |
| 114 bool call_status) { | |
| 115 EXPECT_EQ(true, call_status); | |
| 116 EXPECT_EQ(expected_node_list->size(), node_list.size()); | |
| 117 for (size_t i = 0; i < node_list.size(); ++i) { | |
| 118 EXPECT_EQ((*expected_node_list)[i].is_input, node_list[i].is_input); | |
| 119 EXPECT_EQ((*expected_node_list)[i].id, node_list[i].id); | |
| 120 EXPECT_EQ((*expected_node_list)[i].stable_device_id, | |
| 121 node_list[i].stable_device_id); | |
| 122 EXPECT_EQ((*expected_node_list)[i].device_name, node_list[i].device_name); | |
| 123 EXPECT_EQ((*expected_node_list)[i].type, node_list[i].type); | |
| 124 EXPECT_EQ((*expected_node_list)[i].name, node_list[i].name); | |
| 125 EXPECT_EQ((*expected_node_list)[i].mic_positions, | |
| 126 node_list[i].mic_positions); | |
| 127 EXPECT_EQ((*expected_node_list)[i].active, node_list[i].active); | |
| 128 EXPECT_EQ((*expected_node_list)[i].plugged_time, node_list[i].plugged_time); | |
| 129 } | |
| 130 } | |
| 131 | |
| 132 } // namespace | |
| 133 | |
| 134 class CrasAudioClientTest : public testing::Test { | |
| 135 public: | |
| 136 CrasAudioClientTest() : interface_name_(cras::kCrasControlInterface), | |
| 137 response_(nullptr) {} | |
| 138 | |
| 139 void SetUp() override { | |
| 140 // Create a mock bus. | |
| 141 dbus::Bus::Options options; | |
| 142 options.bus_type = dbus::Bus::SYSTEM; | |
| 143 mock_bus_ = new dbus::MockBus(options); | |
| 144 | |
| 145 // Create a mock cras proxy. | |
| 146 mock_cras_proxy_ = new dbus::MockObjectProxy( | |
| 147 mock_bus_.get(), | |
| 148 cras::kCrasServiceName, | |
| 149 dbus::ObjectPath(cras::kCrasServicePath)); | |
| 150 | |
| 151 // Set an expectation so mock_cras_proxy's CallMethod() will use | |
| 152 // OnCallMethod() to return responses. | |
| 153 EXPECT_CALL(*mock_cras_proxy_.get(), CallMethod(_, _, _)) | |
| 154 .WillRepeatedly(Invoke(this, &CrasAudioClientTest::OnCallMethod)); | |
| 155 | |
| 156 // Set an expectation so mock_cras_proxy's CallMethodWithErrorCallback() | |
| 157 // will use OnCallMethodWithErrorCallback() to return responses. | |
| 158 EXPECT_CALL(*mock_cras_proxy_.get(), | |
| 159 CallMethodWithErrorCallback(_, _, _, _)) | |
| 160 .WillRepeatedly( | |
| 161 Invoke(this, &CrasAudioClientTest::OnCallMethodWithErrorCallback)); | |
| 162 | |
| 163 // Set an expectation so mock_cras_proxy's monitoring OutputMuteChanged | |
| 164 // ConnectToSignal will use OnConnectToOutputMuteChanged() to run the | |
| 165 // callback. | |
| 166 EXPECT_CALL( | |
| 167 *mock_cras_proxy_.get(), | |
| 168 ConnectToSignal(interface_name_, cras::kOutputMuteChanged, _, _)) | |
| 169 .WillRepeatedly( | |
| 170 Invoke(this, &CrasAudioClientTest::OnConnectToOutputMuteChanged)); | |
| 171 | |
| 172 // Set an expectation so mock_cras_proxy's monitoring InputMuteChanged | |
| 173 // ConnectToSignal will use OnConnectToInputMuteChanged() to run the | |
| 174 // callback. | |
| 175 EXPECT_CALL( | |
| 176 *mock_cras_proxy_.get(), | |
| 177 ConnectToSignal(interface_name_, cras::kInputMuteChanged, _, _)) | |
| 178 .WillRepeatedly( | |
| 179 Invoke(this, &CrasAudioClientTest::OnConnectToInputMuteChanged)); | |
| 180 | |
| 181 // Set an expectation so mock_cras_proxy's monitoring NodesChanged | |
| 182 // ConnectToSignal will use OnConnectToNodesChanged() to run the callback. | |
| 183 EXPECT_CALL( | |
| 184 *mock_cras_proxy_.get(), | |
| 185 ConnectToSignal(interface_name_, cras::kNodesChanged, _, _)) | |
| 186 .WillRepeatedly( | |
| 187 Invoke(this, &CrasAudioClientTest::OnConnectToNodesChanged)); | |
| 188 | |
| 189 // Set an expectation so mock_cras_proxy's monitoring | |
| 190 // ActiveOutputNodeChanged ConnectToSignal will use | |
| 191 // OnConnectToActiveOutputNodeChanged() to run the callback. | |
| 192 EXPECT_CALL( | |
| 193 *mock_cras_proxy_.get(), | |
| 194 ConnectToSignal(interface_name_, cras::kActiveOutputNodeChanged, _, _)) | |
| 195 .WillRepeatedly( | |
| 196 Invoke(this, | |
| 197 &CrasAudioClientTest::OnConnectToActiveOutputNodeChanged)); | |
| 198 | |
| 199 // Set an expectation so mock_cras_proxy's monitoring | |
| 200 // ActiveInputNodeChanged ConnectToSignal will use | |
| 201 // OnConnectToActiveInputNodeChanged() to run the callback. | |
| 202 EXPECT_CALL( | |
| 203 *mock_cras_proxy_.get(), | |
| 204 ConnectToSignal(interface_name_, cras::kActiveInputNodeChanged, _, _)) | |
| 205 .WillRepeatedly( | |
| 206 Invoke(this, | |
| 207 &CrasAudioClientTest::OnConnectToActiveInputNodeChanged)); | |
| 208 | |
| 209 // Set an expectation so mock_bus's GetObjectProxy() for the given | |
| 210 // service name and the object path will return mock_cras_proxy_. | |
| 211 EXPECT_CALL(*mock_bus_.get(), | |
| 212 GetObjectProxy(cras::kCrasServiceName, | |
| 213 dbus::ObjectPath(cras::kCrasServicePath))) | |
| 214 .WillOnce(Return(mock_cras_proxy_.get())); | |
| 215 | |
| 216 // ShutdownAndBlock() will be called in TearDown(). | |
| 217 EXPECT_CALL(*mock_bus_.get(), ShutdownAndBlock()).WillOnce(Return()); | |
| 218 | |
| 219 // Create a client with the mock bus. | |
| 220 client_.reset(CrasAudioClient::Create()); | |
| 221 client_->Init(mock_bus_.get()); | |
| 222 // Run the message loop to run the signal connection result callback. | |
| 223 message_loop_.RunUntilIdle(); | |
| 224 } | |
| 225 | |
| 226 void TearDown() override { mock_bus_->ShutdownAndBlock(); } | |
| 227 | |
| 228 protected: | |
| 229 // A callback to intercept and check the method call arguments. | |
| 230 typedef base::Callback<void( | |
| 231 dbus::MessageReader* reader)> ArgumentCheckCallback; | |
| 232 | |
| 233 // Sets expectations for called method name and arguments, and sets response. | |
| 234 void PrepareForMethodCall(const std::string& method_name, | |
| 235 const ArgumentCheckCallback& argument_checker, | |
| 236 dbus::Response* response) { | |
| 237 expected_method_name_ = method_name; | |
| 238 argument_checker_ = argument_checker; | |
| 239 response_ = response; | |
| 240 } | |
| 241 | |
| 242 // Send output mute changed signal to the tested client. | |
| 243 void SendOutputMuteChangedSignal(dbus::Signal* signal) { | |
| 244 ASSERT_FALSE(output_mute_changed_handler_.is_null()); | |
| 245 output_mute_changed_handler_.Run(signal); | |
| 246 } | |
| 247 | |
| 248 // Send input mute changed signal to the tested client. | |
| 249 void SendInputMuteChangedSignal(dbus::Signal* signal) { | |
| 250 ASSERT_FALSE(input_mute_changed_handler_.is_null()); | |
| 251 input_mute_changed_handler_.Run(signal); | |
| 252 } | |
| 253 | |
| 254 // Send nodes changed signal to the tested client. | |
| 255 void SendNodesChangedSignal(dbus::Signal* signal) { | |
| 256 ASSERT_FALSE(nodes_changed_handler_.is_null()); | |
| 257 nodes_changed_handler_.Run(signal); | |
| 258 } | |
| 259 | |
| 260 // Send active output node changed signal to the tested client. | |
| 261 void SendActiveOutputNodeChangedSignal(dbus::Signal* signal) { | |
| 262 ASSERT_FALSE(active_output_node_changed_handler_.is_null()); | |
| 263 active_output_node_changed_handler_.Run(signal); | |
| 264 } | |
| 265 | |
| 266 // Send active input node changed signal to the tested client. | |
| 267 void SendActiveInputNodeChangedSignal(dbus::Signal* signal) { | |
| 268 ASSERT_FALSE(active_input_node_changed_handler_.is_null()); | |
| 269 active_input_node_changed_handler_.Run(signal); | |
| 270 } | |
| 271 | |
| 272 // The interface name. | |
| 273 const std::string interface_name_; | |
| 274 // The client to be tested. | |
| 275 std::unique_ptr<CrasAudioClient> client_; | |
| 276 // A message loop to emulate asynchronous behavior. | |
| 277 base::MessageLoop message_loop_; | |
| 278 // The mock bus. | |
| 279 scoped_refptr<dbus::MockBus> mock_bus_; | |
| 280 // The mock object proxy. | |
| 281 scoped_refptr<dbus::MockObjectProxy> mock_cras_proxy_; | |
| 282 // The OutputMuteChanged signal handler given by the tested client. | |
| 283 dbus::ObjectProxy::SignalCallback output_mute_changed_handler_; | |
| 284 // The InputMuteChanged signal handler given by the tested client. | |
| 285 dbus::ObjectProxy::SignalCallback input_mute_changed_handler_; | |
| 286 // The NodesChanged signal handler given by the tested client. | |
| 287 dbus::ObjectProxy::SignalCallback nodes_changed_handler_; | |
| 288 // The ActiveOutputNodeChanged signal handler given by the tested client. | |
| 289 dbus::ObjectProxy::SignalCallback active_output_node_changed_handler_; | |
| 290 // The ActiveInputNodeChanged signal handler given by the tested client. | |
| 291 dbus::ObjectProxy::SignalCallback active_input_node_changed_handler_; | |
| 292 // The name of the method which is expected to be called. | |
| 293 std::string expected_method_name_; | |
| 294 // The response which the mock cras proxy returns. | |
| 295 dbus::Response* response_; | |
| 296 // A callback to intercept and check the method call arguments. | |
| 297 ArgumentCheckCallback argument_checker_; | |
| 298 | |
| 299 private: | |
| 300 // Checks the requested interface name and signal name. | |
| 301 // Used to implement the mock cras proxy. | |
| 302 void OnConnectToOutputMuteChanged( | |
| 303 const std::string& interface_name, | |
| 304 const std::string& signal_name, | |
| 305 const dbus::ObjectProxy::SignalCallback& signal_callback, | |
| 306 const dbus::ObjectProxy::OnConnectedCallback& on_connected_callback) { | |
| 307 output_mute_changed_handler_ = signal_callback; | |
| 308 const bool success = true; | |
| 309 message_loop_.task_runner()->PostTask(FROM_HERE, | |
| 310 base::Bind(on_connected_callback, | |
| 311 interface_name, | |
| 312 signal_name, | |
| 313 success)); | |
| 314 } | |
| 315 | |
| 316 // Checks the requested interface name and signal name. | |
| 317 // Used to implement the mock cras proxy. | |
| 318 void OnConnectToInputMuteChanged( | |
| 319 const std::string& interface_name, | |
| 320 const std::string& signal_name, | |
| 321 const dbus::ObjectProxy::SignalCallback& signal_callback, | |
| 322 const dbus::ObjectProxy::OnConnectedCallback& on_connected_callback) { | |
| 323 input_mute_changed_handler_ = signal_callback; | |
| 324 const bool success = true; | |
| 325 message_loop_.task_runner()->PostTask(FROM_HERE, | |
| 326 base::Bind(on_connected_callback, | |
| 327 interface_name, | |
| 328 signal_name, | |
| 329 success)); | |
| 330 } | |
| 331 | |
| 332 // Checks the requested interface name and signal name. | |
| 333 // Used to implement the mock cras proxy. | |
| 334 void OnConnectToNodesChanged( | |
| 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 nodes_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 OnConnectToActiveOutputNodeChanged( | |
| 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 active_output_node_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 OnConnectToActiveInputNodeChanged( | |
| 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 active_input_node_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 content of the method call and returns the response. | |
| 381 // Used to implement the mock cras proxy. | |
| 382 void OnCallMethod(dbus::MethodCall* method_call, | |
| 383 int timeout_ms, | |
| 384 const dbus::ObjectProxy::ResponseCallback& response) { | |
| 385 EXPECT_EQ(interface_name_, method_call->GetInterface()); | |
| 386 EXPECT_EQ(expected_method_name_, method_call->GetMember()); | |
| 387 dbus::MessageReader reader(method_call); | |
| 388 argument_checker_.Run(&reader); | |
| 389 message_loop_.task_runner()->PostTask( | |
| 390 FROM_HERE, base::Bind(response, response_)); | |
| 391 } | |
| 392 | |
| 393 // Checks the content of the method call and returns the response. | |
| 394 // Used to implement the mock cras proxy. | |
| 395 void OnCallMethodWithErrorCallback( | |
| 396 dbus::MethodCall* method_call, | |
| 397 int timeout_ms, | |
| 398 const dbus::ObjectProxy::ResponseCallback& response_callback, | |
| 399 const dbus::ObjectProxy::ErrorCallback& error_callback) { | |
| 400 OnCallMethod(method_call, timeout_ms, response_callback); | |
| 401 } | |
| 402 }; | |
| 403 | |
| 404 TEST_F(CrasAudioClientTest, OutputMuteChanged) { | |
| 405 const bool kSystemMuteOn = false; | |
| 406 const bool kUserMuteOn = true; | |
| 407 // Create a signal. | |
| 408 dbus::Signal signal(cras::kCrasControlInterface, | |
| 409 cras::kOutputMuteChanged); | |
| 410 dbus::MessageWriter writer(&signal); | |
| 411 writer.AppendBool(kSystemMuteOn); | |
| 412 writer.AppendBool(kUserMuteOn); | |
| 413 | |
| 414 // Set expectations. | |
| 415 MockObserver observer; | |
| 416 EXPECT_CALL(observer, OutputMuteChanged(kUserMuteOn)).Times(1); | |
| 417 | |
| 418 // Add the observer. | |
| 419 client_->AddObserver(&observer); | |
| 420 | |
| 421 // Run the signal callback. | |
| 422 SendOutputMuteChangedSignal(&signal); | |
| 423 | |
| 424 // Remove the observer. | |
| 425 client_->RemoveObserver(&observer); | |
| 426 | |
| 427 EXPECT_CALL(observer, OutputMuteChanged(_)).Times(0); | |
| 428 | |
| 429 // Run the signal callback again and make sure the observer isn't called. | |
| 430 SendOutputMuteChangedSignal(&signal); | |
| 431 | |
| 432 message_loop_.RunUntilIdle(); | |
| 433 } | |
| 434 | |
| 435 TEST_F(CrasAudioClientTest, InputMuteChanged) { | |
| 436 const bool kInputMuteOn = true; | |
| 437 // Create a signal. | |
| 438 dbus::Signal signal(cras::kCrasControlInterface, | |
| 439 cras::kInputMuteChanged); | |
| 440 dbus::MessageWriter writer(&signal); | |
| 441 writer.AppendBool(kInputMuteOn); | |
| 442 | |
| 443 // Set expectations. | |
| 444 MockObserver observer; | |
| 445 EXPECT_CALL(observer, InputMuteChanged(kInputMuteOn)).Times(1); | |
| 446 | |
| 447 // Add the observer. | |
| 448 client_->AddObserver(&observer); | |
| 449 | |
| 450 // Run the signal callback. | |
| 451 SendInputMuteChangedSignal(&signal); | |
| 452 | |
| 453 // Remove the observer. | |
| 454 client_->RemoveObserver(&observer); | |
| 455 | |
| 456 EXPECT_CALL(observer, InputMuteChanged(_)).Times(0); | |
| 457 | |
| 458 // Run the signal callback again and make sure the observer isn't called. | |
| 459 SendInputMuteChangedSignal(&signal); | |
| 460 | |
| 461 message_loop_.RunUntilIdle(); | |
| 462 } | |
| 463 | |
| 464 TEST_F(CrasAudioClientTest, NodesChanged) { | |
| 465 // Create a signal. | |
| 466 dbus::Signal signal(cras::kCrasControlInterface, | |
| 467 cras::kNodesChanged); | |
| 468 // Set expectations. | |
| 469 MockObserver observer; | |
| 470 EXPECT_CALL(observer, NodesChanged()).Times(1); | |
| 471 | |
| 472 // Add the observer. | |
| 473 client_->AddObserver(&observer); | |
| 474 | |
| 475 // Run the signal callback. | |
| 476 SendNodesChangedSignal(&signal); | |
| 477 | |
| 478 // Remove the observer. | |
| 479 client_->RemoveObserver(&observer); | |
| 480 | |
| 481 EXPECT_CALL(observer, NodesChanged()).Times(0); | |
| 482 | |
| 483 // Run the signal callback again and make sure the observer isn't called. | |
| 484 SendNodesChangedSignal(&signal); | |
| 485 | |
| 486 message_loop_.RunUntilIdle(); | |
| 487 } | |
| 488 | |
| 489 TEST_F(CrasAudioClientTest, ActiveOutputNodeChanged) { | |
| 490 const uint64_t kNodeId = 10002; | |
| 491 // Create a signal. | |
| 492 dbus::Signal signal(cras::kCrasControlInterface, | |
| 493 cras::kActiveOutputNodeChanged); | |
| 494 dbus::MessageWriter writer(&signal); | |
| 495 writer.AppendUint64(kNodeId); | |
| 496 | |
| 497 // Set expectations. | |
| 498 MockObserver observer; | |
| 499 EXPECT_CALL(observer, ActiveOutputNodeChanged(kNodeId)).Times(1); | |
| 500 | |
| 501 // Add the observer. | |
| 502 client_->AddObserver(&observer); | |
| 503 | |
| 504 // Run the signal callback. | |
| 505 SendActiveOutputNodeChangedSignal(&signal); | |
| 506 | |
| 507 // Remove the observer. | |
| 508 client_->RemoveObserver(&observer); | |
| 509 EXPECT_CALL(observer, ActiveOutputNodeChanged(_)).Times(0); | |
| 510 | |
| 511 // Run the signal callback again and make sure the observer isn't called. | |
| 512 SendActiveOutputNodeChangedSignal(&signal); | |
| 513 | |
| 514 message_loop_.RunUntilIdle(); | |
| 515 } | |
| 516 | |
| 517 TEST_F(CrasAudioClientTest, ActiveInputNodeChanged) { | |
| 518 const uint64_t kNodeId = 20002; | |
| 519 // Create a signal. | |
| 520 dbus::Signal signal(cras::kCrasControlInterface, | |
| 521 cras::kActiveInputNodeChanged); | |
| 522 dbus::MessageWriter writer(&signal); | |
| 523 writer.AppendUint64(kNodeId); | |
| 524 | |
| 525 // Set expectations. | |
| 526 MockObserver observer; | |
| 527 EXPECT_CALL(observer, ActiveInputNodeChanged(kNodeId)).Times(1); | |
| 528 | |
| 529 // Add the observer. | |
| 530 client_->AddObserver(&observer); | |
| 531 | |
| 532 // Run the signal callback. | |
| 533 SendActiveInputNodeChangedSignal(&signal); | |
| 534 | |
| 535 // Remove the observer. | |
| 536 client_->RemoveObserver(&observer); | |
| 537 EXPECT_CALL(observer, ActiveInputNodeChanged(_)).Times(0); | |
| 538 | |
| 539 // Run the signal callback again and make sure the observer isn't called. | |
| 540 SendActiveInputNodeChangedSignal(&signal); | |
| 541 | |
| 542 message_loop_.RunUntilIdle(); | |
| 543 } | |
| 544 | |
| 545 TEST_F(CrasAudioClientTest, GetVolumeState) { | |
|
jennyz
2016/04/14 00:12:31
GetVolumeState isn't not really useful, we don't n
Qiang(Joe) Xu
2016/04/14 01:25:49
done by removing related contents. I do see GetVol
| |
| 546 // Create response. | |
| 547 std::unique_ptr<dbus::Response> response(dbus::Response::CreateEmpty()); | |
| 548 dbus::MessageWriter writer(response.get()); | |
| 549 const int32_t kOutputVolume = 8; | |
| 550 writer.AppendInt32(kOutputVolume); | |
| 551 const bool kOutputSystemMute = false; | |
| 552 writer.AppendBool(kOutputSystemMute); | |
| 553 const int32_t kInputGain = 1; | |
| 554 writer.AppendInt32(kInputGain); | |
| 555 const bool kInputMute = false; | |
| 556 writer.AppendBool(kInputMute); | |
| 557 const bool kOutputUserMute = true; | |
| 558 writer.AppendBool(kOutputUserMute); | |
| 559 | |
| 560 // Create the expected value. | |
| 561 VolumeState expected_volume_state; | |
| 562 expected_volume_state.output_volume = kOutputVolume; | |
| 563 expected_volume_state.output_system_mute = kOutputSystemMute; | |
| 564 expected_volume_state.input_gain = kInputGain; | |
| 565 expected_volume_state.input_mute = kInputMute; | |
| 566 expected_volume_state.output_user_mute = kOutputUserMute; | |
| 567 | |
| 568 // Set expectations. | |
| 569 PrepareForMethodCall(cras::kGetVolumeState, | |
| 570 base::Bind(&ExpectNoArgument), | |
| 571 response.get()); | |
| 572 // Call method. | |
| 573 client_->GetVolumeState(base::Bind(&ExpectVolumeStateResult, | |
| 574 &expected_volume_state)); | |
| 575 // Run the message loop. | |
| 576 message_loop_.RunUntilIdle(); | |
| 577 } | |
| 578 | |
| 579 TEST_F(CrasAudioClientTest, GetNodes) { | |
| 580 // Create response. | |
| 581 std::unique_ptr<dbus::Response> response(dbus::Response::CreateEmpty()); | |
| 582 dbus::MessageWriter writer(response.get()); | |
| 583 dbus::MessageWriter sub_writer(nullptr); | |
| 584 dbus::MessageWriter entry_writer(nullptr); | |
| 585 // response for kInternalSpeaker | |
|
jennyz
2016/04/14 00:12:31
Can the code for adding a audio node be wrapped in
Qiang(Joe) Xu
2016/04/14 01:25:49
done by creating a func WriteNodesToResponse
| |
| 586 writer.OpenArray("{sv}", &sub_writer); | |
| 587 sub_writer.OpenDictEntry(&entry_writer); | |
| 588 entry_writer.AppendString(cras::kIsInputProperty); | |
| 589 entry_writer.AppendVariantOfBool(kInternalSpeaker.is_input); | |
| 590 sub_writer.CloseContainer(&entry_writer); | |
| 591 sub_writer.OpenDictEntry(&entry_writer); | |
| 592 entry_writer.AppendString(cras::kIdProperty); | |
| 593 entry_writer.AppendVariantOfUint64(kInternalSpeaker.id); | |
| 594 sub_writer.CloseContainer(&entry_writer); | |
| 595 sub_writer.OpenDictEntry(&entry_writer); | |
| 596 entry_writer.AppendString(cras::kDeviceNameProperty); | |
| 597 entry_writer.AppendVariantOfString(kInternalSpeaker.device_name); | |
| 598 sub_writer.CloseContainer(&entry_writer); | |
| 599 sub_writer.OpenDictEntry(&entry_writer); | |
| 600 entry_writer.AppendString(cras::kTypeProperty); | |
| 601 entry_writer.AppendVariantOfString(kInternalSpeaker.type); | |
| 602 sub_writer.CloseContainer(&entry_writer); | |
| 603 sub_writer.OpenDictEntry(&entry_writer); | |
| 604 entry_writer.AppendString(cras::kNameProperty); | |
| 605 entry_writer.AppendVariantOfString(kInternalSpeaker.name); | |
| 606 sub_writer.CloseContainer(&entry_writer); | |
| 607 sub_writer.OpenDictEntry(&entry_writer); | |
| 608 entry_writer.AppendString(cras::kActiveProperty); | |
| 609 entry_writer.AppendVariantOfBool(kInternalSpeaker.active); | |
| 610 sub_writer.CloseContainer(&entry_writer); | |
| 611 sub_writer.OpenDictEntry(&entry_writer); | |
| 612 entry_writer.AppendString(cras::kPluggedTimeProperty); | |
| 613 entry_writer.AppendVariantOfUint64(kInternalSpeaker.plugged_time); | |
| 614 sub_writer.CloseContainer(&entry_writer); | |
| 615 sub_writer.OpenDictEntry(&entry_writer); | |
| 616 entry_writer.AppendString(cras::kMicPositionsProperty); | |
| 617 entry_writer.AppendVariantOfString(kInternalSpeaker.mic_positions); | |
| 618 sub_writer.CloseContainer(&entry_writer); | |
| 619 sub_writer.OpenDictEntry(&entry_writer); | |
| 620 entry_writer.AppendString(cras::kStableDeviceIdProperty); | |
| 621 entry_writer.AppendVariantOfUint64(kInternalSpeaker.stable_device_id); | |
| 622 sub_writer.CloseContainer(&entry_writer); | |
| 623 writer.CloseContainer(&sub_writer); | |
| 624 // response for kInternalMic | |
| 625 writer.OpenArray("{sv}", &sub_writer); | |
| 626 sub_writer.OpenDictEntry(&entry_writer); | |
| 627 entry_writer.AppendString(cras::kIsInputProperty); | |
| 628 entry_writer.AppendVariantOfBool(kInternalMic.is_input); | |
| 629 sub_writer.CloseContainer(&entry_writer); | |
| 630 sub_writer.OpenDictEntry(&entry_writer); | |
| 631 entry_writer.AppendString(cras::kIdProperty); | |
| 632 entry_writer.AppendVariantOfUint64(kInternalMic.id); | |
| 633 sub_writer.CloseContainer(&entry_writer); | |
| 634 sub_writer.OpenDictEntry(&entry_writer); | |
| 635 entry_writer.AppendString(cras::kDeviceNameProperty); | |
| 636 entry_writer.AppendVariantOfString(kInternalMic.device_name); | |
| 637 sub_writer.CloseContainer(&entry_writer); | |
| 638 sub_writer.OpenDictEntry(&entry_writer); | |
| 639 entry_writer.AppendString(cras::kTypeProperty); | |
| 640 entry_writer.AppendVariantOfString(kInternalMic.type); | |
| 641 sub_writer.CloseContainer(&entry_writer); | |
| 642 sub_writer.OpenDictEntry(&entry_writer); | |
| 643 entry_writer.AppendString(cras::kNameProperty); | |
| 644 entry_writer.AppendVariantOfString(kInternalMic.name); | |
| 645 sub_writer.CloseContainer(&entry_writer); | |
| 646 sub_writer.OpenDictEntry(&entry_writer); | |
| 647 entry_writer.AppendString(cras::kActiveProperty); | |
| 648 entry_writer.AppendVariantOfBool(kInternalMic.active); | |
| 649 sub_writer.CloseContainer(&entry_writer); | |
| 650 sub_writer.OpenDictEntry(&entry_writer); | |
| 651 entry_writer.AppendString(cras::kPluggedTimeProperty); | |
| 652 entry_writer.AppendVariantOfUint64(kInternalMic.plugged_time); | |
| 653 sub_writer.CloseContainer(&entry_writer); | |
| 654 sub_writer.OpenDictEntry(&entry_writer); | |
| 655 entry_writer.AppendString(cras::kMicPositionsProperty); | |
| 656 entry_writer.AppendVariantOfString(kInternalMic.mic_positions); | |
| 657 sub_writer.CloseContainer(&entry_writer); | |
| 658 sub_writer.OpenDictEntry(&entry_writer); | |
| 659 entry_writer.AppendString(cras::kStableDeviceIdProperty); | |
| 660 entry_writer.AppendVariantOfUint64(kInternalMic.stable_device_id); | |
| 661 sub_writer.CloseContainer(&entry_writer); | |
| 662 writer.CloseContainer(&sub_writer); | |
| 663 | |
| 664 // Create the expected value. | |
| 665 AudioNodeList expected_node_list; | |
| 666 expected_node_list.push_back(kInternalSpeaker); | |
| 667 expected_node_list.push_back(kInternalMic); | |
| 668 | |
| 669 // Set expectations. | |
| 670 PrepareForMethodCall(cras::kGetNodes, | |
| 671 base::Bind(&ExpectNoArgument), | |
| 672 response.get()); | |
| 673 // Call method. | |
| 674 MockErrorCallback error_callback; | |
| 675 client_->GetNodes(base::Bind(&ExpectAudioNodeListResult, | |
| 676 &expected_node_list), | |
| 677 error_callback.GetCallback()); | |
| 678 EXPECT_CALL(error_callback, Run(_, _)).Times(0); | |
| 679 // Run the message loop. | |
| 680 message_loop_.RunUntilIdle(); | |
| 681 } | |
| 682 | |
| 683 TEST_F(CrasAudioClientTest, SetGlobalOutputChannelRemix) { | |
| 684 const int32_t kChannels = 2; | |
| 685 const std::vector<double> kMixer = {0, 0.1, 0.5, 1}; | |
| 686 // Create response. | |
| 687 std::unique_ptr<dbus::Response> response(dbus::Response::CreateEmpty()); | |
| 688 | |
| 689 // Set expectations. | |
| 690 PrepareForMethodCall(cras::kSetGlobalOutputChannelRemix, | |
| 691 base::Bind(&ExpectInt32AndArrayOfDoublesArguments, | |
| 692 kChannels, | |
| 693 kMixer), | |
| 694 response.get()); | |
| 695 | |
| 696 // Call method. | |
| 697 client_->SetGlobalOutputChannelRemix(kChannels, kMixer); | |
| 698 // Run the message loop. | |
| 699 message_loop_.RunUntilIdle(); | |
| 700 } | |
| 701 | |
| 702 } // namespace chromeos | |
| OLD | NEW |