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