audio_system_test_util.cc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // Copyright 2017 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. #include "media/audio/audio_system_test_util.h"
  5. #include "base/bind.h"
  6. #include "base/memory/ptr_util.h"
  7. namespace media {
  8. bool operator==(const AudioDeviceDescription& lhs,
  9. const AudioDeviceDescription& rhs) {
  10. return lhs.device_name == rhs.device_name && lhs.unique_id == rhs.unique_id &&
  11. lhs.group_id == rhs.group_id;
  12. }
  13. AudioSystem::OnAudioParamsCallback
  14. AudioSystemCallbackExpectations::GetAudioParamsCallback(
  15. const base::Location& location,
  16. base::OnceClosure on_cb_received,
  17. const absl::optional<AudioParameters>& expected_params) {
  18. return base::BindOnce(&AudioSystemCallbackExpectations::OnAudioParams,
  19. base::Unretained(this), location.ToString(),
  20. std::move(on_cb_received), expected_params);
  21. }
  22. AudioSystem::OnBoolCallback AudioSystemCallbackExpectations::GetBoolCallback(
  23. const base::Location& location,
  24. base::OnceClosure on_cb_received,
  25. bool expected) {
  26. return base::BindOnce(&AudioSystemCallbackExpectations::OnBool,
  27. base::Unretained(this), location.ToString(),
  28. std::move(on_cb_received), expected);
  29. }
  30. AudioSystem::OnDeviceDescriptionsCallback
  31. AudioSystemCallbackExpectations::GetDeviceDescriptionsCallback(
  32. const base::Location& location,
  33. base::OnceClosure on_cb_received,
  34. const AudioDeviceDescriptions& expected_descriptions) {
  35. return base::BindOnce(&AudioSystemCallbackExpectations::OnDeviceDescriptions,
  36. base::Unretained(this), location.ToString(),
  37. std::move(on_cb_received), expected_descriptions);
  38. }
  39. AudioSystem::OnInputDeviceInfoCallback
  40. AudioSystemCallbackExpectations::GetInputDeviceInfoCallback(
  41. const base::Location& location,
  42. base::OnceClosure on_cb_received,
  43. const absl::optional<AudioParameters>& expected_input,
  44. const absl::optional<std::string>& expected_associated_device_id) {
  45. return base::BindOnce(&AudioSystemCallbackExpectations::OnInputDeviceInfo,
  46. base::Unretained(this), location.ToString(),
  47. std::move(on_cb_received), expected_input,
  48. expected_associated_device_id);
  49. }
  50. AudioSystem::OnDeviceIdCallback
  51. AudioSystemCallbackExpectations::GetDeviceIdCallback(
  52. const base::Location& location,
  53. base::OnceClosure on_cb_received,
  54. const absl::optional<std::string>& expected_id) {
  55. return base::BindOnce(&AudioSystemCallbackExpectations::OnDeviceId,
  56. base::Unretained(this), location.ToString(),
  57. std::move(on_cb_received), expected_id);
  58. }
  59. void AudioSystemCallbackExpectations::OnAudioParams(
  60. const std::string& from_here,
  61. base::OnceClosure on_cb_received,
  62. const absl::optional<AudioParameters>& expected,
  63. const absl::optional<AudioParameters>& received) {
  64. DCHECK_CALLED_ON_VALID_THREAD(thread_checker_, from_here);
  65. if (expected) {
  66. EXPECT_TRUE(received) << from_here;
  67. EXPECT_EQ(expected->AsHumanReadableString(),
  68. received->AsHumanReadableString())
  69. << from_here;
  70. } else {
  71. EXPECT_FALSE(received) << from_here;
  72. }
  73. std::move(on_cb_received).Run();
  74. }
  75. void AudioSystemCallbackExpectations::OnBool(const std::string& from_here,
  76. base::OnceClosure on_cb_received,
  77. bool expected,
  78. bool result) {
  79. DCHECK_CALLED_ON_VALID_THREAD(thread_checker_, from_here);
  80. EXPECT_EQ(expected, result) << from_here;
  81. std::move(on_cb_received).Run();
  82. }
  83. void AudioSystemCallbackExpectations::OnDeviceDescriptions(
  84. const std::string& from_here,
  85. base::OnceClosure on_cb_received,
  86. const AudioDeviceDescriptions& expected_descriptions,
  87. AudioDeviceDescriptions descriptions) {
  88. DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
  89. EXPECT_EQ(expected_descriptions, descriptions);
  90. std::move(on_cb_received).Run();
  91. }
  92. void AudioSystemCallbackExpectations::OnInputDeviceInfo(
  93. const std::string& from_here,
  94. base::OnceClosure on_cb_received,
  95. const absl::optional<AudioParameters>& expected_input,
  96. const absl::optional<std::string>& expected_associated_device_id,
  97. const absl::optional<AudioParameters>& input,
  98. const absl::optional<std::string>& associated_device_id) {
  99. DCHECK_CALLED_ON_VALID_THREAD(thread_checker_, from_here);
  100. EXPECT_TRUE(!input || input->IsValid());
  101. if (expected_input) {
  102. EXPECT_TRUE(input) << from_here;
  103. EXPECT_EQ(expected_input->AsHumanReadableString(),
  104. input->AsHumanReadableString())
  105. << from_here;
  106. } else {
  107. EXPECT_FALSE(input) << from_here;
  108. }
  109. EXPECT_TRUE(!associated_device_id || !associated_device_id->empty());
  110. if (expected_associated_device_id) {
  111. EXPECT_TRUE(associated_device_id) << from_here;
  112. EXPECT_EQ(expected_associated_device_id, associated_device_id) << from_here;
  113. } else {
  114. EXPECT_FALSE(associated_device_id) << from_here;
  115. }
  116. std::move(on_cb_received).Run();
  117. }
  118. void AudioSystemCallbackExpectations::OnDeviceId(
  119. const std::string& from_here,
  120. base::OnceClosure on_cb_received,
  121. const absl::optional<std::string>& expected_id,
  122. const absl::optional<std::string>& result_id) {
  123. DCHECK_CALLED_ON_VALID_THREAD(thread_checker_, from_here);
  124. EXPECT_TRUE(!result_id || !result_id->empty());
  125. if (expected_id) {
  126. EXPECT_TRUE(result_id) << from_here;
  127. EXPECT_EQ(expected_id, result_id) << from_here;
  128. } else {
  129. EXPECT_FALSE(result_id) << from_here;
  130. }
  131. std::move(on_cb_received).Run();
  132. }
  133. // This suite is instantiated in binaries that use //media:test_support.
  134. GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(AudioSystemTestTemplate);
  135. } // namespace media