test_audio_config.cc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Copyright (c) 2012 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 "ppapi/tests/test_audio_config.h"
  5. #include <stddef.h>
  6. #include <stdint.h>
  7. #include "ppapi/c/ppb_audio_config.h"
  8. #include "ppapi/cpp/module.h"
  9. #include "ppapi/tests/testing_instance.h"
  10. REGISTER_TEST_CASE(AudioConfig);
  11. bool TestAudioConfig::Init() {
  12. audio_config_interface_ = static_cast<const PPB_AudioConfig*>(
  13. pp::Module::Get()->GetBrowserInterface(PPB_AUDIO_CONFIG_INTERFACE));
  14. core_interface_ = static_cast<const PPB_Core*>(
  15. pp::Module::Get()->GetBrowserInterface(PPB_CORE_INTERFACE));
  16. return audio_config_interface_ && core_interface_;
  17. }
  18. void TestAudioConfig::RunTests(const std::string& filter) {
  19. RUN_TEST(RecommendSampleRate, filter);
  20. RUN_TEST(ValidConfigs, filter);
  21. RUN_TEST(InvalidConfigs, filter);
  22. }
  23. std::string TestAudioConfig::TestRecommendSampleRate() {
  24. // Ask PPB_AudioConfig about the recommended sample rate.
  25. PP_AudioSampleRate sample_rate = audio_config_interface_->RecommendSampleRate(
  26. instance_->pp_instance());
  27. ASSERT_TRUE(sample_rate == PP_AUDIOSAMPLERATE_NONE ||
  28. sample_rate == PP_AUDIOSAMPLERATE_44100 ||
  29. sample_rate == PP_AUDIOSAMPLERATE_48000);
  30. PASS();
  31. }
  32. std::string TestAudioConfig::TestValidConfigs() {
  33. static const PP_AudioSampleRate kSampleRates[] = {
  34. PP_AUDIOSAMPLERATE_44100,
  35. PP_AUDIOSAMPLERATE_48000
  36. };
  37. static const uint32_t kRequestFrameCounts[] = {
  38. PP_AUDIOMINSAMPLEFRAMECOUNT,
  39. PP_AUDIOMAXSAMPLEFRAMECOUNT,
  40. // Include some "okay-looking" frame counts; check their validity below.
  41. 1024,
  42. 2048,
  43. 4096
  44. };
  45. for (size_t i = 0; i < sizeof(kSampleRates)/sizeof(kSampleRates[0]); i++) {
  46. PP_AudioSampleRate sample_rate = kSampleRates[i];
  47. for (size_t j = 0;
  48. j < sizeof(kRequestFrameCounts)/sizeof(kRequestFrameCounts[0]);
  49. j++) {
  50. uint32_t request_frame_count = kRequestFrameCounts[j];
  51. ASSERT_TRUE(request_frame_count >= PP_AUDIOMINSAMPLEFRAMECOUNT);
  52. ASSERT_TRUE(request_frame_count <= PP_AUDIOMAXSAMPLEFRAMECOUNT);
  53. uint32_t frame_count = audio_config_interface_->RecommendSampleFrameCount(
  54. instance_->pp_instance(), sample_rate, request_frame_count);
  55. ASSERT_TRUE(frame_count >= PP_AUDIOMINSAMPLEFRAMECOUNT);
  56. ASSERT_TRUE(frame_count <= PP_AUDIOMAXSAMPLEFRAMECOUNT);
  57. PP_Resource ac = audio_config_interface_->CreateStereo16Bit(
  58. instance_->pp_instance(), sample_rate, frame_count);
  59. ASSERT_TRUE(ac);
  60. ASSERT_TRUE(audio_config_interface_->IsAudioConfig(ac));
  61. ASSERT_EQ(sample_rate, audio_config_interface_->GetSampleRate(ac));
  62. ASSERT_EQ(frame_count, audio_config_interface_->GetSampleFrameCount(ac));
  63. core_interface_->ReleaseResource(ac);
  64. }
  65. }
  66. PASS();
  67. }
  68. std::string TestAudioConfig::TestInvalidConfigs() {
  69. // |PP_AUDIOSAMPLERATE_NONE| is not a valid rate, so this should fail.
  70. PP_Resource ac = audio_config_interface_->CreateStereo16Bit(
  71. instance_->pp_instance(),
  72. PP_AUDIOSAMPLERATE_NONE,
  73. PP_AUDIOMINSAMPLEFRAMECOUNT);
  74. ASSERT_EQ(0, ac);
  75. // Test invalid frame counts.
  76. ASSERT_TRUE(PP_AUDIOMINSAMPLEFRAMECOUNT >= 1);
  77. ac = audio_config_interface_->CreateStereo16Bit(
  78. instance_->pp_instance(),
  79. PP_AUDIOSAMPLERATE_44100,
  80. PP_AUDIOMINSAMPLEFRAMECOUNT - 1u);
  81. ASSERT_EQ(0, ac);
  82. ac = audio_config_interface_->CreateStereo16Bit(
  83. instance_->pp_instance(),
  84. PP_AUDIOSAMPLERATE_44100,
  85. PP_AUDIOMAXSAMPLEFRAMECOUNT + 1u);
  86. ASSERT_EQ(0, ac);
  87. // Test rest of API whose failure cases are defined.
  88. ASSERT_FALSE(audio_config_interface_->IsAudioConfig(0));
  89. ASSERT_EQ(PP_AUDIOSAMPLERATE_NONE, audio_config_interface_->GetSampleRate(0));
  90. ASSERT_EQ(0u, audio_config_interface_->GetSampleFrameCount(0));
  91. PASS();
  92. }